hello GameStyleModel
authorsheepluva
Sun, 29 Apr 2012 17:12:49 +0200
changeset 6958 8230a516ba93
parent 6957 ab8034208fcd
child 6959 fce378ee4191
hello GameStyleModel
QTfrontend/hwconsts.cpp.in
QTfrontend/hwconsts.h
QTfrontend/main.cpp
QTfrontend/model/GameStyleModel.cpp
QTfrontend/model/GameStyleModel.h
QTfrontend/ui/widget/gamecfgwidget.cpp
QTfrontend/util/DataManager.cpp
QTfrontend/util/DataManager.h
--- a/QTfrontend/hwconsts.cpp.in	Sun Apr 29 16:09:42 2012 +0200
+++ b/QTfrontend/hwconsts.cpp.in	Sun Apr 29 17:12:49 2012 +0200
@@ -27,8 +27,6 @@
 QDir * cfgdir = new QDir();
 QDir * datadir = new QDir();
 
-QStringList * scriptList;
-
 bool custom_config = false;
 bool custom_data = false;
 
--- a/QTfrontend/hwconsts.h	Sun Apr 29 16:09:42 2012 +0200
+++ b/QTfrontend/hwconsts.h	Sun Apr 29 17:12:49 2012 +0200
@@ -40,8 +40,6 @@
 
 class QStringListModel;
 
-extern QStringList * scriptList;
-
 extern QString * cDefaultAmmoStore;
 extern int cAmmoNumber;
 extern QList< QPair<QString, QString> > cDefaultAmmos;
--- a/QTfrontend/main.cpp	Sun Apr 29 16:09:42 2012 +0200
+++ b/QTfrontend/main.cpp	Sun Apr 29 17:12:49 2012 +0200
@@ -211,13 +211,6 @@
 
     DataManager & dataMgr = DataManager::instance();
 
-    scriptList = new QStringList(dataMgr.entryList(
-                                     QString("Scripts/Multiplayer"),
-                                     QDir::Files,
-                                     QStringList("*.lua")
-                                 )
-                                );
-
     QTranslator Translator;
     {
         QSettings settings(cfgdir->absolutePath() + "/hedgewars.ini", QSettings::IniFormat);
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/QTfrontend/model/GameStyleModel.cpp	Sun Apr 29 17:12:49 2012 +0200
@@ -0,0 +1,96 @@
+/*
+ * 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 GameStyleModel class implementation
+ */
+
+#include "GameStyleModel.h"
+
+
+void GameStyleModel::loadGameStyles()
+{
+    beginResetModel();
+
+
+    // empty list, so that we can (re)fill it
+    QStandardItemModel::clear();
+
+    QList<QStandardItem * > items;
+    items.append(new QStandardItem("Normal"));
+
+    // define a separator item
+    QStandardItem * separator = new QStandardItem("---");
+    separator->setData(QLatin1String("separator"), Qt::AccessibleDescriptionRole);
+    separator->setFlags(separator->flags() & ~( Qt::ItemIsEnabled | Qt::ItemIsSelectable ) );
+
+    items.append(separator);
+
+
+    QStringList scripts = DataManager::instance().entryList(
+                             QString("Scripts/Multiplayer"),
+                             QDir::Files,
+                             QStringList("*.lua")
+                         );
+
+    foreach(QString script, scripts)
+    {
+        script = script.remove(".lua", Qt::CaseInsensitive);
+
+        QFile scriptCfgFile(DataManager::instance().findFileForRead(
+                            QString("Scripts/Multiplayer/%2.cfg").arg(script)));
+
+        QString name = script;
+        name = name.replace("_", " ");
+
+        QString scheme = "locked";
+        QString weapons = "locked";
+
+        if (scriptCfgFile.exists() && scriptCfgFile.open(QFile::ReadOnly))
+        {
+            QTextStream input(&scriptCfgFile);
+            input >> scheme;
+            input >> weapons;
+            scriptCfgFile.close();
+
+            if (!scheme.isEmpty())
+                scheme.replace("_", " ");
+
+            if (!weapons.isEmpty())
+                weapons.replace("_", " ");
+        }
+
+        QStandardItem * item = new QStandardItem(name);
+
+        item->setData(script, ScriptRole);
+        item->setData(scheme, SchemeRole);
+        item->setData(weapons, WeaponsRole);
+
+        items.append(item);
+    }
+
+    QStandardItemModel::appendColumn(items);
+
+
+    endResetModel();
+}
+
+
+
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/QTfrontend/model/GameStyleModel.h	Sun Apr 29 17:12:49 2012 +0200
@@ -0,0 +1,48 @@
+/*
+ * 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 GameStyleModel class definition
+ */
+
+#ifndef HEDGEWARS_GAMESTYLEMODEL_H
+#define HEDGEWARS_GAMESTYLEMODEL_H
+
+#include <QStandardItemModel>
+#include <QStringList>
+#include <QMap>
+
+#include "DataManager.h"
+
+/**
+ * @brief A model listing available game styles
+ */
+class GameStyleModel : public QStandardItemModel
+{
+        Q_OBJECT
+
+    public:
+        enum DataRoles { ScriptRole = Qt::UserRole+1, SchemeRole, WeaponsRole };
+
+    public slots:
+        /// reloads the themes from the DataManager
+        void loadGameStyles();
+};
+
+#endif // HEDGEWARS_GAMESTYLEMODEL_H
--- a/QTfrontend/ui/widget/gamecfgwidget.cpp	Sun Apr 29 16:09:42 2012 +0200
+++ b/QTfrontend/ui/widget/gamecfgwidget.cpp	Sun Apr 29 17:12:49 2012 +0200
@@ -56,41 +56,7 @@
     Scripts = new QComboBox(GBoxOptions);
     GBoxOptionsLayout->addWidget(Scripts, 1, 1);
 
-    Scripts->addItem("Normal");
-    Scripts->insertSeparator(1);
-
-    for (int i = 0; i < scriptList->size(); ++i)
-    {
-        QString script = (*scriptList)[i].remove(".lua", Qt::CaseInsensitive);
-        QList<QVariant> scriptInfo;
-        scriptInfo.push_back(script);
-        QFile scriptCfgFile(DataManager::instance().findFileForRead(
-                                QString("Scripts/Multiplayer/%2.cfg").arg(script)));
-        if (scriptCfgFile.exists() && scriptCfgFile.open(QFile::ReadOnly))
-        {
-            QString scheme;
-            QString weapons;
-            QTextStream input(&scriptCfgFile);
-            input >> scheme;
-            input >> weapons;
-            if (scheme.isEmpty())
-                scheme = "locked";
-            scheme.replace("_", " ");
-            if (weapons.isEmpty())
-                weapons = "locked";
-            weapons.replace("_", " ");
-            scriptInfo.push_back(scheme);
-            scriptInfo.push_back(weapons);
-            scriptCfgFile.close();
-        }
-        else
-        {
-            scriptInfo.push_back("locked");
-            scriptInfo.push_back("locked");
-        }
-        Scripts->addItem(script.replace("_", " "), scriptInfo);
-    }
-
+    Scripts->setModel(DataManager::instance().gameStyleModel());
     connect(Scripts, SIGNAL(currentIndexChanged(int)), this, SLOT(scriptChanged(int)));
 
     QWidget *SchemeWidget = new QWidget(GBoxOptions);
@@ -244,7 +210,7 @@
 
     if (Scripts->currentIndex() > 0)
     {
-        bcfg << QString("escript Scripts/Multiplayer/%1.lua").arg(Scripts->itemData(Scripts->currentIndex()).toList()[0].toString()).toUtf8();
+        bcfg << QString("escript Scripts/Multiplayer/%1.lua").arg(Scripts->itemData(Scripts->currentIndex(), GameStyleModel::ScriptRole).toString()).toUtf8();
     }
 
     bcfg << QString("eseed " + pMapContainer->getCurrentSeed()).toUtf8();
@@ -527,8 +493,8 @@
 {
     if(isEnabled() && index > 0)
     {
-        QString scheme = Scripts->itemData(Scripts->currentIndex()).toList()[1].toString();
-        QString weapons = Scripts->itemData(Scripts->currentIndex()).toList()[2].toString();
+        QString scheme = Scripts->itemData(Scripts->currentIndex(), GameStyleModel::SchemeRole).toString();
+        QString weapons = Scripts->itemData(Scripts->currentIndex(), GameStyleModel::WeaponsRole).toString();
 
         if (scheme == "locked")
         {
--- a/QTfrontend/util/DataManager.cpp	Sun Apr 29 16:09:42 2012 +0200
+++ b/QTfrontend/util/DataManager.cpp	Sun Apr 29 17:12:49 2012 +0200
@@ -118,6 +118,15 @@
     return "";
 }
 
+GameStyleModel * DataManager::gameStyleModel()
+{
+    if (m_gameStyleModel == NULL) {
+        m_gameStyleModel = new GameStyleModel();
+        m_gameStyleModel->loadGameStyles();
+    }
+    return m_gameStyleModel;
+}
+
 HatModel * DataManager::hatModel()
 {
     if (m_hatModel == NULL) {
--- a/QTfrontend/util/DataManager.h	Sun Apr 29 16:09:42 2012 +0200
+++ b/QTfrontend/util/DataManager.h	Sun Apr 29 17:12:49 2012 +0200
@@ -29,6 +29,7 @@
 
 #include <QStringList>
 
+#include "GameStyleModel.h"
 #include "HatModel.h"
 #include "MapModel.h"
 #include "ThemeModel.h"
@@ -36,6 +37,7 @@
 class QDir;
 class QFile;
 class QStringList;
+class GameStyleModel;
 class HatModel;
 class MapModel;
 class ThemeModel;
@@ -98,6 +100,15 @@
 
 
         /**
+         * @brief Returns pointer to a model of available game styles.
+         *
+         * The model is updated automatically on data reload.
+         *
+         * @return game style model pointer.
+         */
+        GameStyleModel * gameStyleModel();
+
+        /**
          * @brief Returns pointer to a model of available hats.
          *
          * The model is updated automatically on data reload.
@@ -148,6 +159,7 @@
         QDir * m_defaultData; ///< directory of the installed data
         QDir * m_userData;    ///< directory of custom data in the user's directory
 
+        GameStyleModel * m_gameStyleModel; ///< game style model instance
         HatModel * m_hatModel; ///< hat model instance
         MapModel * m_mapModel; ///< map model instance
         ThemeModel * m_themeModel; ///< theme model instance