Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
// Qt includes
Toshihiro Shimizu 890ddd
#include <qmenu></qmenu>
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#include "toonzqt/popupbutton.h"
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//********************************************************************************
Toshihiro Shimizu 890ddd
//    PopupButton class implementation
Toshihiro Shimizu 890ddd
//********************************************************************************
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
PopupButton::PopupButton(QWidget *parent) : QPushButton(parent), m_index(-1) {
Shinya Kitaoka 120a6e
  setFocusPolicy(Qt::NoFocus);
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  QMenu *menu = new QMenu(this);
Shinya Kitaoka 120a6e
  setMenu(menu);
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  connect(this, SIGNAL(clicked(bool)), this, SLOT(showMenu()));
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//--------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
QAction *PopupButton::addItem(const QString &text) {
Shinya Kitaoka 120a6e
  QAction *action = menu()->addAction(text);
Shinya Kitaoka 120a6e
  connect(action, SIGNAL(triggered(bool)), this, SLOT(onIndexChange()));
Shinya Kitaoka 120a6e
  m_actions.push_back(action);
Shinya Kitaoka 120a6e
  return action;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//--------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
QAction *PopupButton::addItem(const QIcon &icon, const QString &text) {
Shinya Kitaoka 120a6e
  QAction *action = menu()->addAction(icon, text);
Shinya Kitaoka 120a6e
  connect(action, SIGNAL(triggered(bool)), this, SLOT(onIndexChange()));
Shinya Kitaoka 120a6e
  m_actions.push_back(action);
Shinya Kitaoka 120a6e
  return action;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//--------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
QAction *PopupButton::addSeparator() { return menu()->addSeparator(); }
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//--------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
int PopupButton::currentIndex() const { return m_index; }
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//--------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
const QAction *PopupButton::currentItem() const {
Shinya Kitaoka 120a6e
  return (m_index >= 0) ? m_actions[m_index] : 0;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//--------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void PopupButton::setCurrentIndex(int index) {
Shinya Kitaoka 120a6e
  if (index < 0 || index >= m_actions.size()) return;
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  m_index               = index;
Shinya Kitaoka 120a6e
  const QAction *action = currentItem();
Shinya Kitaoka 120a6e
  setIcon(action->icon());
Shinya Kitaoka 120a6e
  setText(action->text());
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  emit currentIndexChanged(index);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//--------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
int PopupButton::count() const { return m_actions.size(); }
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//--------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
int PopupButton::findText(const QString &text, Qt::CaseSensitivity cs) const {
Shinya Kitaoka 120a6e
  int i, size = m_actions.size();
Shinya Kitaoka 120a6e
  for (i = 0; i < size; ++i)
Shinya Kitaoka 120a6e
    if (QString::compare(m_actions[i]->text(), text, cs) == 0) return i;
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  return -1;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//--------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void PopupButton::onIndexChange() {
Shinya Kitaoka 120a6e
  QAction *action = (QAction *)QObject::sender();
Shinya Kitaoka 120a6e
  int i, size = m_actions.size();
Shinya Kitaoka 120a6e
  for (i = 0; i < size; ++i)
Shinya Kitaoka 120a6e
    if (m_actions[i] == action) break;
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  if (i < size) setCurrentIndex(i);
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  emit activated(i);
Toshihiro Shimizu 890ddd
}