BindsModel
authorunc0rr
Sun, 17 Jun 2012 22:11:09 +0400
changeset 7260 c3584a3ab730
parent 7258 722e8a0d89dc
child 7262 33e703cc7a7f
BindsModel
QTfrontend/ui/page/pageeditteam.cpp
QTfrontend/util/DataManager.cpp
QTfrontend/util/DataManager.h
--- a/QTfrontend/ui/page/pageeditteam.cpp	Sun Jun 17 21:45:49 2012 +0400
+++ b/QTfrontend/ui/page/pageeditteam.cpp	Sun Jun 17 22:11:09 2012 +0400
@@ -26,8 +26,8 @@
 #include <QGroupBox>
 #include <QToolBox>
 #include <QMessageBox>
-
-#include "sdlkeys.h"
+#include <QStandardItemModel>
+#include <QDebug>
 #include "SquareLabel.h"
 #include "HWApplication.h"
 
@@ -197,9 +197,9 @@
         l->setText(HWApplication::translate("binds", cbinds[i].name));
         l->setAlignment(Qt::AlignRight);
         pagelayout->addWidget(l, num, 0);
+
         CBBind[i] = new QComboBox(curW);
-        for(int j = 0; sdlkeys[j][1][0] != '\0'; j++)
-            CBBind[i]->addItem(HWApplication::translate("binds (keys)", sdlkeys[j][1]).contains(": ") ? HWApplication::translate("binds (keys)", sdlkeys[j][1]) : HWApplication::translate("binds (keys)", "Keyboard") + QString(": ") + HWApplication::translate("binds (keys)", sdlkeys[j][1]), sdlkeys[j][0]);
+        CBBind[i]->setModel(DataManager::instance().bindsModel());
         pagelayout->addWidget(CBBind[i++], num++, 1);
     }
 
@@ -423,9 +423,15 @@
     CBFort->setCurrentIndex(CBFort->findText(team.fort()));
     CBVoicepack->setCurrentIndex(CBVoicepack->findText(team.voicepack()));
 
+    QStandardItemModel * binds = DataManager::instance().bindsModel();
     for(int i = 0; i < BINDS_NUMBER; i++)
     {
-        CBBind[i]->setCurrentIndex(CBBind[i]->findData(team.keyBind(i)));
+        QModelIndexList mdl = binds->match(binds->index(0, 0), Qt::UserRole + 1, team.keyBind(i), 1, Qt::MatchExactly);
+
+        if(mdl.size() == 1)
+            CBBind[i]->setCurrentIndex(mdl[0].row());
+        else
+            qDebug() << "Binds: cannot find" << team.keyBind(i);
     }
 }
 
@@ -451,9 +457,10 @@
     team.setVoicepack(CBVoicepack->currentText());
     team.setFlag(CBFlag->itemData(CBFlag->currentIndex()).toString());
 
+    QStandardItemModel * binds = DataManager::instance().bindsModel();
     for(int i = 0; i < BINDS_NUMBER; i++)
     {
-        team.bindKey(i,CBBind[i]->itemData(CBBind[i]->currentIndex()).toString());
+        team.bindKey(i, binds->index(CBBind[i]->currentIndex(), 0).data(Qt::UserRole + 1).toString());
     }
 
     return team;
--- a/QTfrontend/util/DataManager.cpp	Sun Jun 17 21:45:49 2012 +0400
+++ b/QTfrontend/util/DataManager.cpp	Sun Jun 17 22:11:09 2012 +0400
@@ -27,6 +27,8 @@
 #include <QFileInfo>
 
 #include "hwconsts.h"
+#include "HWApplication.h"
+#include "sdlkeys.h"
 
 #include "DataManager.h"
 
@@ -47,6 +49,7 @@
     m_mapModel = NULL;
     m_themeModel = NULL;
     m_colorsModel = NULL;
+    m_bindsModel = NULL;
 }
 
 
@@ -178,6 +181,24 @@
     return m_colorsModel;
 }
 
+QStandardItemModel * DataManager::bindsModel()
+{
+    if(m_bindsModel == NULL)
+    {
+        m_bindsModel = new QStandardItemModel();
+
+        for(int j = 0; sdlkeys[j][1][0] != '\0'; j++)
+        {
+            QStandardItem * item = new QStandardItem();
+            item->setData(HWApplication::translate("binds (keys)", sdlkeys[j][1]).contains(": ") ? HWApplication::translate("binds (keys)", sdlkeys[j][1]) : HWApplication::translate("binds (keys)", "Keyboard") + QString(": ") + HWApplication::translate("binds (keys)", sdlkeys[j][1]), Qt::DisplayRole);
+            item->setData(sdlkeys[j][0], Qt::UserRole + 1);
+            m_bindsModel->appendRow(item);
+        }
+    }
+
+    return m_bindsModel;
+}
+
 void DataManager::reload()
 {
     m_gameStyleModel->loadGameStyles();
--- a/QTfrontend/util/DataManager.h	Sun Jun 17 21:45:49 2012 +0400
+++ b/QTfrontend/util/DataManager.h	Sun Jun 17 22:11:09 2012 +0400
@@ -128,6 +128,7 @@
         ThemeModel * themeModel();
 
         QStandardItemModel * colorsModel();
+        QStandardItemModel * bindsModel();
 
     public slots:
         /// Reloads data from storage.
@@ -158,6 +159,7 @@
         MapModel * m_mapModel; ///< map model instance
         ThemeModel * m_themeModel; ///< theme model instance
         QStandardItemModel * m_colorsModel;
+        QStandardItemModel * m_bindsModel;
 };
 
 #endif // HEDGEWARS_DATAMANAGER_H