QTfrontend/ui/widget/hatbutton.cpp
changeset 8434 4821897a0f10
parent 8375 af2d2f56bc45
child 9080 9b42757d7e71
equal deleted inserted replaced
8432:f2d57e987ba9 8434:4821897a0f10
    23 #include "HatModel.h"
    23 #include "HatModel.h"
    24 #include "hatbutton.h"
    24 #include "hatbutton.h"
    25 
    25 
    26 HatButton::HatButton(QWidget* parent) : QPushButton(parent)
    26 HatButton::HatButton(QWidget* parent) : QPushButton(parent)
    27 {
    27 {
    28 	setIconSize(QSize(32, 37));
    28     setIconSize(QSize(32, 37));
    29 	setFixedSize(44, 44);
    29     setFixedSize(44, 44);
    30 
    30 
    31 	m_hatModel = DataManager::instance().hatModel();
    31     m_hatModel = DataManager::instance().hatModel();
    32 	connect(this, SIGNAL(clicked()), this, SLOT(showPrompt()));
    32     connect(this, SIGNAL(clicked()), this, SLOT(showPrompt()));
    33 
    33 
    34 	setCurrentIndex(0);
    34     setCurrentIndex(0);
    35 }
    35 }
    36 
    36 
    37 void HatButton::setCurrentIndex(int index)
    37 void HatButton::setCurrentIndex(int index)
    38 {
    38 {
    39 	m_hat = m_hatModel->index(index, 0);
    39     m_hat = m_hatModel->index(index, 0);
    40 	setWhatsThis(QString(tr("Change hat (%1)")).arg(m_hat.data(Qt::DisplayRole).toString()));
    40     setWhatsThis(QString(tr("Change hat (%1)")).arg(m_hat.data(Qt::DisplayRole).toString()));
    41 	setToolTip(m_hat.data(Qt::DisplayRole).toString());
    41     setToolTip(m_hat.data(Qt::DisplayRole).toString());
    42 	setIcon(m_hat.data(Qt::DecorationRole).value<QIcon>());
    42     setIcon(m_hat.data(Qt::DecorationRole).value<QIcon>());
    43 }
    43 }
    44 
    44 
    45 int HatButton::currentIndex()
    45 int HatButton::currentIndex()
    46 {
    46 {
    47 	return m_hat.row();
    47     return m_hat.row();
    48 }
    48 }
    49 
    49 
    50 void HatButton::setCurrentHat(const QString & name)
    50 void HatButton::setCurrentHat(const QString & name)
    51 {
    51 {
    52 	QList<QStandardItem *> hats = m_hatModel->findItems(name);
    52     QList<QStandardItem *> hats = m_hatModel->findItems(name);
    53 
    53 
    54 	if (hats.count() > 0)
    54     if (hats.count() > 0)
    55 		setCurrentIndex(hats[0]->row());
    55         setCurrentIndex(hats[0]->row());
    56 }
    56 }
    57 
    57 
    58 QString HatButton::currentHat() const
    58 QString HatButton::currentHat() const
    59 {
    59 {
    60 	return m_hat.data(Qt::DisplayRole).toString();
    60     return m_hat.data(Qt::DisplayRole).toString();
    61 }
    61 }
    62 
    62 
    63 void HatButton::showPrompt()
    63 void HatButton::showPrompt()
    64 {
    64 {
    65 	HatPrompt prompt(currentIndex(), this);
    65     HatPrompt prompt(currentIndex(), this);
    66 	int hatID = prompt.exec() - 1; // Since 0 means canceled, so all indexes are +1'd
    66     int hatID = prompt.exec() - 1; // Since 0 means canceled, so all indexes are +1'd
    67 	if (hatID < 0) return;
    67     if (hatID < 0) return;
    68 
    68 
    69 	setCurrentIndex(hatID);
    69     setCurrentIndex(hatID);
    70 	emit currentIndexChanged(hatID);
    70     emit currentIndexChanged(hatID);
    71 	emit currentHatChanged(currentHat());
    71     emit currentHatChanged(currentHat());
    72 }
    72 }