QTfrontend/ui/widget/hatbutton.cpp
changeset 8374 3a1708759c4f
child 8375 af2d2f56bc45
equal deleted inserted replaced
8372:3c193ec03e09 8374:3a1708759c4f
       
     1 /*
       
     2  * Hedgewars, a free turn based strategy game
       
     3  * Copyright (c) 2004-2012 Andrey Korotaev <unC0Rr@gmail.com>
       
     4  *
       
     5  * This program is free software; you can redistribute it and/or modify
       
     6  * it under the terms of the GNU General Public License as published by
       
     7  * the Free Software Foundation; version 2 of the License
       
     8  *
       
     9  * This program is distributed in the hope that it will be useful,
       
    10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
       
    11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
       
    12  * GNU General Public License for more details.
       
    13  *
       
    14  * You should have received a copy of the GNU General Public License
       
    15  * along with this program; if not, write to the Free Software
       
    16  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
       
    17  */
       
    18 
       
    19 #include <QDebug>
       
    20 
       
    21 #include "hatprompt.h"
       
    22 #include "DataManager.h"
       
    23 #include "hatmodel.h"
       
    24 #include "hatbutton.h"
       
    25 
       
    26 HatButton::HatButton(QWidget* parent) : QPushButton(parent)
       
    27 {
       
    28 	setIconSize(QSize(32, 37));
       
    29 	setFixedSize(44, 44);
       
    30 
       
    31 	m_hatModel = DataManager::instance().hatModel();
       
    32 	connect(this, SIGNAL(clicked()), this, SLOT(showPrompt()));
       
    33 
       
    34 	setCurrentIndex(0);
       
    35 }
       
    36 
       
    37 void HatButton::setCurrentIndex(int index)
       
    38 {
       
    39 	m_hat = m_hatModel->index(index, 0);
       
    40 	setWhatsThis(QString(tr("Change hat (%1)")).arg(m_hat.data(Qt::DisplayRole).toString()));
       
    41 	setToolTip(m_hat.data(Qt::DisplayRole).toString());
       
    42 	setIcon(m_hat.data(Qt::DecorationRole).value<QIcon>());
       
    43 }
       
    44 
       
    45 int HatButton::currentIndex()
       
    46 {
       
    47 	return m_hat.row();
       
    48 }
       
    49 
       
    50 void HatButton::setCurrentHat(const QString & name)
       
    51 {
       
    52 	QList<QStandardItem *> hats = m_hatModel->findItems(name);
       
    53 
       
    54 	if (hats.count() > 0)
       
    55 		setCurrentIndex(hats[0]->row());
       
    56 }
       
    57 
       
    58 QString HatButton::currentHat() const
       
    59 {
       
    60 	return m_hat.data(Qt::DisplayRole).toString();
       
    61 }
       
    62 
       
    63 void HatButton::showPrompt()
       
    64 {
       
    65 	HatPrompt prompt(currentIndex(), this);
       
    66 	int hatID = prompt.exec() - 1; // Since 0 means canceled, so all indexes are +1'd
       
    67 	if (hatID < 0) return;
       
    68 
       
    69 	setCurrentIndex(hatID);
       
    70 	emit currentIndexChanged(hatID);
       
    71 	emit currentHatChanged(currentHat());
       
    72 }