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