# HG changeset patch # User sheepluva # Date 1335700835 -7200 # Node ID 4c2dd25630a7de1d757dc6d98220d1e79bd1ea0f # Parent 7f70f37bbf0848ffa1d9522d6e6a028682ad52f9 * make HatModel update automatically (also renamed class and files) * removed nemo's ghost file :P diff -r 7f70f37bbf08 -r 4c2dd25630a7 QTfrontend/model/HatModel.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/QTfrontend/model/HatModel.cpp Sun Apr 29 14:00:35 2012 +0200 @@ -0,0 +1,148 @@ +/* + * Hedgewars, a free turn based strategy game + * Copyright (c) 2004-2012 Andrey Korotaev + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; version 2 of the License + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA + */ + +/** + * @file + * @brief HatModel class implementation + */ + +#include "HatModel.h" + +#include +#include +#include +#include "hwform.h" // player hash + +#include "DataManager.h" + +HatModel::HatModel(QObject* parent) : + QAbstractListModel(parent) +{ + hats = QVector >(); +} + +void HatModel::loadHats() +{ + // this method resets the contents of this model (important to know for views). + beginResetModel(); + + // prepare hats Vector + hats.clear(); + + DataManager & dataMgr = DataManager::instance(); + + QPixmap hhpix = QPixmap( + dataMgr.findFileForRead("Graphics/Hedgehog/Idle.png") + ).copy(0, 0, 32, 32); + + // my reserved hats + QStringList hatsList = dataMgr.entryList( + "Graphics/Hats/Reserved", + QDir::Files, + QStringList(playerHash+"*.png") + ); + + int nReserved = hatsList.size(); + + // regular hats + hatsList.append(dataMgr.entryList( + "Graphics/Hats", + QDir::Files, + QStringList("*.png") + ) + ); + + + int nHats = hatsList.size(); + + for (int i = 0; i < nHats; i++) + { + bool isReserved = (i < nReserved); + + QString str = hatsList.at(i); + str = str.remove(QRegExp("\\.png$")); + QPixmap pix( + dataMgr.findFileForRead( + "Graphics/Hats/" + QString(isReserved?"Reserved/":"") + str + + ".png" + ) + ); + + // rename properly + if (isReserved) + str = "Reserved "+str.remove(0,32); + + QPixmap tmppix(32, 37); + tmppix.fill(QColor(Qt::transparent)); + + QPainter painter(&tmppix); + painter.drawPixmap(QPoint(0, 5), hhpix); + painter.drawPixmap(QPoint(0, 0), pix.copy(0, 0, 32, 32)); + if(pix.width() > 32) + painter.drawPixmap(QPoint(0, 0), pix.copy(32, 0, 32, 32)); + painter.end(); + + if (str == "NoHat") + hats.prepend(qMakePair(str, QIcon(tmppix))); + else + hats.append(qMakePair(str, QIcon(tmppix))); + } + + + endResetModel(); +} + +QVariant HatModel::headerData(int section, + Qt::Orientation orientation, int role) const +{ + Q_UNUSED(section); + Q_UNUSED(orientation); + Q_UNUSED(role); + + return QVariant(); +} + +int HatModel::rowCount(const QModelIndex &parent) const +{ + if (parent.isValid()) + return 0; + else + return hats.size(); +} + +/*int HatModel::columnCount(const QModelIndex & parent) const +{ + if (parent.isValid()) + return 0; + else + return 2; +} +*/ +QVariant HatModel::data(const QModelIndex &index, + int role) const +{ + if (!index.isValid() || index.row() < 0 + || index.row() >= hats.size() + || (role != Qt::DisplayRole && role != Qt::DecorationRole)) + return QVariant(); + + if (role == Qt::DisplayRole) + return hats.at(index.row()).first; + else // role == Qt::DecorationRole + return hats.at(index.row()).second; +} diff -r 7f70f37bbf08 -r 4c2dd25630a7 QTfrontend/model/HatModel.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/QTfrontend/model/HatModel.h Sun Apr 29 14:00:35 2012 +0200 @@ -0,0 +1,53 @@ +/* + * Hedgewars, a free turn based strategy game + * Copyright (c) 2004-2012 Andrey Korotaev + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; version 2 of the License + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA + */ + +/** + * @file + * @brief HatModel class definition + */ + +#ifndef HEDGEWARS_HATMODEL_H +#define HEDGEWARS_HATMODEL_H + +#include +#include +#include +#include +#include + +class HatModel : public QAbstractListModel +{ + Q_OBJECT + + public: + HatModel(QObject *parent = 0); + + QVariant headerData(int section, Qt::Orientation orientation, int role) const; + int rowCount(const QModelIndex & parent) const; + //int columnCount(const QModelIndex & parent) const; + + public slots: + /// Reloads hats using the DataManager. + void loadHats(); + + QVariant data(const QModelIndex &index, int role) const; + protected: + QVector > hats; +}; + +#endif // HEDGEWARS_HATMODEL_H diff -r 7f70f37bbf08 -r 4c2dd25630a7 QTfrontend/model/hats.cpp --- a/QTfrontend/model/hats.cpp Sat Apr 28 18:56:31 2012 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,129 +0,0 @@ -/* - * Hedgewars, a free turn based strategy game - * Copyright (c) 2004-2012 Andrey Korotaev - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; version 2 of the License - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA - */ - -#include -#include -#include -#include "hwconsts.h" -#include "hwform.h" -#include "hats.h" - -#include "DataManager.h" - -HatsModel::HatsModel(QObject* parent) : - QAbstractListModel(parent) -{ - DataManager & dataMgr = DataManager::instance(); - - QPixmap hhpix = QPixmap( - dataMgr.findFileForRead("Graphics/Hedgehog/Idle.png") - ).copy(0, 0, 32, 32); - - // my reserved hats - QStringList hatsList = dataMgr.entryList( - "Graphics/Hats/Reserved", - QDir::Files, - QStringList(playerHash+"*.png") - ); - - int nReserved = hatsList.size(); - - // regular hats - hatsList.append(dataMgr.entryList( - "Graphics/Hats", - QDir::Files, - QStringList("*.png") - ) - ); - - - int nHats = hatsList.size(); - - for (int i = 0; i < nHats; i++) - { - bool isReserved = (i < nReserved); - - QString str = hatsList.at(i); - str = str.remove(QRegExp("\\.png$")); - QPixmap pix( - dataMgr.findFileForRead( - "Graphics/Hats/" + QString(isReserved?"Reserved/":"") + str + - ".png" - ) - ); - - // rename properly - if (isReserved) - str = "Reserved "+str.remove(0,32); - - QPixmap tmppix(32, 37); - tmppix.fill(QColor(Qt::transparent)); - - QPainter painter(&tmppix); - painter.drawPixmap(QPoint(0, 5), hhpix); - painter.drawPixmap(QPoint(0, 0), pix.copy(0, 0, 32, 32)); - if(pix.width() > 32) - painter.drawPixmap(QPoint(0, 0), pix.copy(32, 0, 32, 32)); - painter.end(); - - if (str == "NoHat") - hats.prepend(qMakePair(str, QIcon(tmppix))); - else - hats.append(qMakePair(str, QIcon(tmppix))); - } -} - -QVariant HatsModel::headerData(int section, - Qt::Orientation orientation, int role) const -{ - Q_UNUSED(section); - Q_UNUSED(orientation); - Q_UNUSED(role); - - return QVariant(); -} - -int HatsModel::rowCount(const QModelIndex &parent) const -{ - if (parent.isValid()) - return 0; - else - return hats.size(); -} - -/*int HatsModel::columnCount(const QModelIndex & parent) const -{ - if (parent.isValid()) - return 0; - else - return 2; -} -*/ -QVariant HatsModel::data(const QModelIndex &index, - int role) const -{ - if (!index.isValid() || index.row() < 0 - || index.row() >= hats.size() - || (role != Qt::DisplayRole && role != Qt::DecorationRole)) - return QVariant(); - - if (role == Qt::DisplayRole) - return hats.at(index.row()).first; - else // role == Qt::DecorationRole - return hats.at(index.row()).second; -} diff -r 7f70f37bbf08 -r 4c2dd25630a7 QTfrontend/model/hats.h --- a/QTfrontend/model/hats.h Sat Apr 28 18:56:31 2012 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,44 +0,0 @@ -/* - * Hedgewars, a free turn based strategy game - * Copyright (c) 2004-2012 Andrey Korotaev - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; version 2 of the License - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA - */ - -#ifndef _HATS_INCLUDED -#define _HATS_INCLUDED - -#include -#include -#include -#include -#include - -class HatsModel : public QAbstractListModel -{ - Q_OBJECT - - public: - HatsModel(QObject *parent = 0); - - QVariant headerData(int section, Qt::Orientation orientation, int role) const; - int rowCount(const QModelIndex & parent) const; - //int columnCount(const QModelIndex & parent) const; - - QVariant data(const QModelIndex &index, int role) const; - protected: - QVector > hats; -}; - -#endif // _HATS_INCLUDED diff -r 7f70f37bbf08 -r 4c2dd25630a7 QTfrontend/team.cpp --- a/QTfrontend/team.cpp Sat Apr 28 18:56:31 2012 +0200 +++ b/QTfrontend/team.cpp Sun Apr 29 14:00:35 2012 +0200 @@ -25,7 +25,6 @@ #include "team.h" #include "hwform.h" -#include "hats.h" HWTeam::HWTeam(const QString & teamname) : QObject(0) diff -r 7f70f37bbf08 -r 4c2dd25630a7 QTfrontend/ui/page/pageeditteam.cpp --- a/QTfrontend/ui/page/pageeditteam.cpp Sat Apr 28 18:56:31 2012 +0200 +++ b/QTfrontend/ui/page/pageeditteam.cpp Sun Apr 29 14:00:35 2012 +0200 @@ -29,7 +29,6 @@ #include "sdlkeys.h" #include "SquareLabel.h" -#include "hats.h" #include "HWApplication.h" #include "DataManager.h" @@ -61,11 +60,12 @@ GBoxHedgehogs->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed); QGridLayout * GBHLayout = new QGridLayout(GBoxHedgehogs); - HatsModel * hatsModel = new HatsModel(GBoxHedgehogs); + HatModel * hatModel = DataManager::instance().hatModel(); + for(int i = 0; i < HEDGEHOGS_PER_TEAM; i++) { HHHats[i] = new QComboBox(GBoxHedgehogs); - HHHats[i]->setModel(hatsModel); + HHHats[i]->setModel(hatModel); HHHats[i]->setIconSize(QSize(32, 37)); //HHHats[i]->setSizeAdjustPolicy(QComboBox::AdjustToContents); //HHHats[i]->setModelColumn(1); diff -r 7f70f37bbf08 -r 4c2dd25630a7 QTfrontend/util/DataManager.cpp --- a/QTfrontend/util/DataManager.cpp Sat Apr 28 18:56:31 2012 +0200 +++ b/QTfrontend/util/DataManager.cpp Sun Apr 29 14:00:35 2012 +0200 @@ -39,6 +39,7 @@ m_defaultData = new QDir(datadir->absolutePath()); + m_hatModel = NULL; m_mapModel = NULL; m_themeModel = NULL; } @@ -117,6 +118,15 @@ return ""; } +HatModel * DataManager::hatModel() +{ + if (m_hatModel == NULL) { + m_hatModel = new HatModel(); + m_hatModel->loadHats(); + } + return m_hatModel; +} + MapModel * DataManager::mapModel() { if (m_mapModel == NULL) { @@ -137,6 +147,7 @@ void DataManager::reload() { + m_hatModel->loadHats(); m_mapModel->loadMaps(); m_themeModel->loadThemes(); emit updated(); diff -r 7f70f37bbf08 -r 4c2dd25630a7 QTfrontend/util/DataManager.h --- a/QTfrontend/util/DataManager.h Sat Apr 28 18:56:31 2012 +0200 +++ b/QTfrontend/util/DataManager.h Sun Apr 29 14:00:35 2012 +0200 @@ -29,12 +29,14 @@ #include +#include "HatModel.h" #include "MapModel.h" #include "ThemeModel.h" class QDir; class QFile; class QStringList; +class HatModel; class MapModel; class ThemeModel; @@ -94,8 +96,18 @@ */ QString findFileForWrite(const QString & relativeDataFilePath) const; + /** - * @brief Returns pointer to a model for the available maps. + * @brief Returns pointer to a model of available hats. + * + * The model is updated automatically on data reload. + * + * @return hat model pointer. + */ + HatModel * hatModel(); + + /** + * @brief Returns pointer to a model of available maps. * * The model is updated automatically on data reload. * @@ -104,7 +116,7 @@ MapModel * mapModel(); /** - * @brief Returns pointer to a model for the available themes. + * @brief Returns pointer to a model of available themes. * * The model is updated automatically on data reload. * @@ -136,6 +148,7 @@ QDir * m_defaultData; ///< directory of the installed data QDir * m_userData; ///< directory of custom data in the user's directory + HatModel * m_hatModel; ///< hat model instance MapModel * m_mapModel; ///< map model instance ThemeModel * m_themeModel; ///< theme model instance }; diff -r 7f70f37bbf08 -r 4c2dd25630a7 foo