2016-03-19 06:57:51 +13:00
|
|
|
|
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
// Qt includes
|
2016-03-19 06:57:51 +13:00
|
|
|
#include <QMenu>
|
|
|
|
|
|
|
|
#include "toonzqt/popupbutton.h"
|
|
|
|
|
|
|
|
//********************************************************************************
|
|
|
|
// PopupButton class implementation
|
|
|
|
//********************************************************************************
|
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
PopupButton::PopupButton(QWidget *parent) : QPushButton(parent), m_index(-1) {
|
|
|
|
setFocusPolicy(Qt::NoFocus);
|
2016-03-19 06:57:51 +13:00
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
QMenu *menu = new QMenu(this);
|
|
|
|
setMenu(menu);
|
2017-06-26 14:43:02 +12:00
|
|
|
menu->setToolTipsVisible(true);
|
2016-06-15 18:43:10 +12:00
|
|
|
connect(this, SIGNAL(clicked(bool)), this, SLOT(showMenu()));
|
2016-03-19 06:57:51 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
//--------------------------------------------------------------------------
|
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
QAction *PopupButton::addItem(const QString &text) {
|
|
|
|
QAction *action = menu()->addAction(text);
|
|
|
|
connect(action, SIGNAL(triggered(bool)), this, SLOT(onIndexChange()));
|
|
|
|
m_actions.push_back(action);
|
|
|
|
return action;
|
2016-03-19 06:57:51 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
//--------------------------------------------------------------------------
|
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
QAction *PopupButton::addItem(const QIcon &icon, const QString &text) {
|
|
|
|
QAction *action = menu()->addAction(icon, text);
|
|
|
|
connect(action, SIGNAL(triggered(bool)), this, SLOT(onIndexChange()));
|
|
|
|
m_actions.push_back(action);
|
|
|
|
return action;
|
2016-03-19 06:57:51 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
//--------------------------------------------------------------------------
|
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
QAction *PopupButton::addSeparator() { return menu()->addSeparator(); }
|
2016-03-19 06:57:51 +13:00
|
|
|
|
|
|
|
//--------------------------------------------------------------------------
|
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
int PopupButton::currentIndex() const { return m_index; }
|
2016-03-19 06:57:51 +13:00
|
|
|
|
|
|
|
//--------------------------------------------------------------------------
|
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
const QAction *PopupButton::currentItem() const {
|
|
|
|
return (m_index >= 0) ? m_actions[m_index] : 0;
|
2016-03-19 06:57:51 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
//--------------------------------------------------------------------------
|
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
void PopupButton::setCurrentIndex(int index) {
|
|
|
|
if (index < 0 || index >= m_actions.size()) return;
|
2016-03-19 06:57:51 +13:00
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
m_index = index;
|
|
|
|
const QAction *action = currentItem();
|
|
|
|
setIcon(action->icon());
|
|
|
|
setText(action->text());
|
2016-03-19 06:57:51 +13:00
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
emit currentIndexChanged(index);
|
2016-03-19 06:57:51 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
//--------------------------------------------------------------------------
|
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
int PopupButton::count() const { return m_actions.size(); }
|
2016-03-19 06:57:51 +13:00
|
|
|
|
|
|
|
//--------------------------------------------------------------------------
|
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
int PopupButton::findText(const QString &text, Qt::CaseSensitivity cs) const {
|
|
|
|
int i, size = m_actions.size();
|
|
|
|
for (i = 0; i < size; ++i)
|
|
|
|
if (QString::compare(m_actions[i]->text(), text, cs) == 0) return i;
|
2016-03-19 06:57:51 +13:00
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
return -1;
|
2016-03-19 06:57:51 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
//--------------------------------------------------------------------------
|
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
void PopupButton::onIndexChange() {
|
|
|
|
QAction *action = (QAction *)QObject::sender();
|
|
|
|
int i, size = m_actions.size();
|
|
|
|
for (i = 0; i < size; ++i)
|
|
|
|
if (m_actions[i] == action) break;
|
2016-03-19 06:57:51 +13:00
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
if (i < size) setCurrentIndex(i);
|
2016-03-19 06:57:51 +13:00
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
emit activated(i);
|
2016-03-19 06:57:51 +13:00
|
|
|
}
|