* make HatModel update automatically (also renamed class and files)
authorsheepluva
Sun, 29 Apr 2012 14:00:35 +0200
changeset 6953 4c2dd25630a7
parent 6952 7f70f37bbf08
child 6954 a61458a81480
* make HatModel update automatically (also renamed class and files) * removed nemo's ghost file :P
QTfrontend/model/HatModel.cpp
QTfrontend/model/HatModel.h
QTfrontend/model/hats.cpp
QTfrontend/model/hats.h
QTfrontend/team.cpp
QTfrontend/ui/page/pageeditteam.cpp
QTfrontend/util/DataManager.cpp
QTfrontend/util/DataManager.h
foo
--- /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 <unC0Rr@gmail.com>
+ *
+ * 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 <QDir>
+#include <QPixmap>
+#include <QPainter>
+#include "hwform.h" // player hash
+
+#include "DataManager.h"
+
+HatModel::HatModel(QObject* parent) :
+    QAbstractListModel(parent)
+{
+    hats = QVector<QPair<QString, QIcon> >();
+}
+
+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;
+}
--- /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 <unC0Rr@gmail.com>
+ *
+ * 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 <QAbstractListModel>
+#include <QStringList>
+#include <QVector>
+#include <QPair>
+#include <QIcon>
+
+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<QPair<QString, QIcon> > hats;
+};
+
+#endif // HEDGEWARS_HATMODEL_H
--- 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 <unC0Rr@gmail.com>
- *
- * 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 <QDir>
-#include <QPixmap>
-#include <QPainter>
-#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;
-}
--- 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 <unC0Rr@gmail.com>
- *
- * 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 <QAbstractListModel>
-#include <QStringList>
-#include <QVector>
-#include <QPair>
-#include <QIcon>
-
-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<QPair<QString, QIcon> > hats;
-};
-
-#endif // _HATS_INCLUDED
--- 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)
--- 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);
--- 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();
--- 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 <QStringList>
 
+#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
 };