# HG changeset patch # User sheepluva # Date 1335449737 -7200 # Node ID 7f77fa908a4e6c27a5050ea82849d0e47b537b1b # Parent 8af2bf10ee62398c890432f809b0216203e9edf4 messing with the theme model a bit (gets now auto-updated after DLC download too) diff -r 8af2bf10ee62 -r 7f77fa908a4e QTfrontend/game.cpp --- a/QTfrontend/game.cpp Fri Apr 27 16:57:54 2012 -0400 +++ b/QTfrontend/game.cpp Thu Apr 26 16:15:37 2012 +0200 @@ -115,10 +115,11 @@ void HWGame::SendQuickConfig() { QByteArray teamscfg; + ThemeModel * themeModel = DataManager::instance().themeModel(); HWProto::addStringToBuffer(teamscfg, "TL"); HWProto::addStringToBuffer(teamscfg, QString("etheme %1") - .arg((themesModel->rowCount() > 0) ? themesModel->index(rand() % themesModel->rowCount()).data().toString() : "steel")); + .arg((themeModel->rowCount() > 0) ? themeModel->index(rand() % themeModel->rowCount()).data().toString() : "steel")); HWProto::addStringToBuffer(teamscfg, "eseed " + QUuid::createUuid().toString()); HWTeam team1; diff -r 8af2bf10ee62 -r 7f77fa908a4e QTfrontend/hwconsts.cpp.in --- a/QTfrontend/hwconsts.cpp.in Fri Apr 27 16:57:54 2012 -0400 +++ b/QTfrontend/hwconsts.cpp.in Thu Apr 26 16:15:37 2012 +0200 @@ -27,7 +27,6 @@ QDir * cfgdir = new QDir(); QDir * datadir = new QDir(); -ThemesModel * themesModel; QStringList * scriptList; bool custom_config = false; diff -r 8af2bf10ee62 -r 7f77fa908a4e QTfrontend/hwconsts.h --- a/QTfrontend/hwconsts.h Fri Apr 27 16:57:54 2012 -0400 +++ b/QTfrontend/hwconsts.h Thu Apr 26 16:15:37 2012 +0200 @@ -22,7 +22,6 @@ #include #include -#include "themesmodel.h" extern QString * cProtoVer; extern QString * cVersionString; @@ -41,7 +40,6 @@ class QStringListModel; -extern ThemesModel * themesModel; extern QStringList * scriptList; extern QString * cDefaultAmmoStore; diff -r 8af2bf10ee62 -r 7f77fa908a4e QTfrontend/main.cpp --- a/QTfrontend/main.cpp Fri Apr 27 16:57:54 2012 -0400 +++ b/QTfrontend/main.cpp Thu Apr 26 16:15:37 2012 +0200 @@ -211,53 +211,6 @@ DataManager & dataMgr = DataManager::instance(); - { - QStringList themes; - - themes.append(dataMgr.entryList( - "Themes", - QDir::AllDirs | QDir::NoDotAndDotDot) - ); - - QList > icons; - - themes.sort(); - for(int i = themes.size() - 1; i >= 0; --i) - { - QString file = dataMgr.findFileForRead( - QString("Themes/%1/icon.png").arg(themes.at(i)) - ); - - if(QFile::exists(file)) - { - // load icon - QPair ic; - ic.first = QIcon(file); - - // load preview icon - ic.second = QIcon( - dataMgr.findFileForRead( - QString("Themes/%1/icon@2x.png").arg(themes.at(i)) - ) - ); - - icons.prepend(ic); - } - else - { - themes.removeAt(i); - } - } - - themesModel = new ThemesModel(themes); - Q_ASSERT(themes.size() == icons.size()); - for(int i = 0; i < icons.size(); ++i) - { - themesModel->setData(themesModel->index(i), icons[i].first, Qt::DecorationRole); - themesModel->setData(themesModel->index(i), icons[i].second, Qt::UserRole); - } - } - scriptList = new QStringList(dataMgr.entryList( QString("Scripts/Multiplayer"), QDir::Files, diff -r 8af2bf10ee62 -r 7f77fa908a4e QTfrontend/model/ThemeModel.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/QTfrontend/model/ThemeModel.cpp Thu Apr 26 16:15:37 2012 +0200 @@ -0,0 +1,75 @@ + +#include "ThemeModel.h" + +ThemeModel::ThemeModel(QObject *parent) : + QAbstractListModel(parent) +{ + m_data = QList >(); +} + +int ThemeModel::rowCount(const QModelIndex &parent) const +{ + if(parent.isValid()) + return 0; + else + return m_data.size(); +} + + +QVariant ThemeModel::data(const QModelIndex &index, int role) const +{ + if(index.column() > 0 || index.row() >= m_data.size()) + return QVariant(); + else + return m_data.at(index.row()).value(role); +} + + +void ThemeModel::loadThemes() +{ + beginResetModel(); + + + DataManager & datamgr = DataManager::instance(); + + QStringList themes = + datamgr.entryList("Themes", QDir::AllDirs | QDir::NoDotAndDotDot); + + m_data.clear(); + +#if QT_VERSION >= QT_VERSION_CHECK(4, 7, 0) + m_data.reserve(themes.size()); +#endif + + foreach (QString theme, themes) + { + // themes without icon are supposed to be hidden + QString iconpath = + datamgr.findFileForRead(QString("Themes/%1/icon.png").arg(theme)); + + if (!QFile::exists(iconpath)) + continue; + + QMap dataset; + + // set name + dataset.insert(Qt::DisplayRole, theme); + + // load and set icon + QIcon icon(iconpath); + dataset.insert(Qt::DecorationRole, icon); + + // load and set preview icon + QIcon preview(datamgr.findFileForRead(QString("Themes/%1/icon@2x.png").arg(theme))); + dataset.insert(Qt::UserRole, preview); + + m_data.append(dataset); + } + + + endResetModel(); +} + + + + diff -r 8af2bf10ee62 -r 7f77fa908a4e QTfrontend/model/ThemeModel.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/QTfrontend/model/ThemeModel.h Thu Apr 26 16:15:37 2012 +0200 @@ -0,0 +1,58 @@ +/* + * Hedgewars, a free turn based strategy game + * Copyright (c) 2006-2007 Igor Ulyanov + * Copyright (c) 2007-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 ThemeModel class definition + */ + +#ifndef HEDGEWARS_THEMEMODEL_H +#define HEDGEWARS_THEMEMODEL_H + +#include +#include +#include +#include + +#include "DataManager.h" + +/** + * @brief A model listing available themes + */ +class ThemeModel : public QAbstractListModel +{ + Q_OBJECT + + public: + explicit ThemeModel(QObject *parent = 0); + + int rowCount(const QModelIndex &parent = QModelIndex()) const; + QVariant data(const QModelIndex &index, int role) const; + + + public slots: + /// reloads the themes from the DataManager + void loadThemes(); + + + private: + QList > m_data; +}; + +#endif // HEDGEWARS_THEMEMODEL_H diff -r 8af2bf10ee62 -r 7f77fa908a4e QTfrontend/model/themesmodel.cpp --- a/QTfrontend/model/themesmodel.cpp Fri Apr 27 16:57:54 2012 -0400 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,49 +0,0 @@ - -#include "themesmodel.h" - -ThemesModel::ThemesModel(QStringList themes, QObject *parent) : - QAbstractListModel(parent) -{ -#if QT_VERSION >= QT_VERSION_CHECK(4, 7, 0) - m_data.reserve(themes.size()); -#endif - - foreach(QString theme, themes) - { - m_data.append(QHash()); - m_data.last().insert(Qt::DisplayRole, theme); - } -} - -int ThemesModel::rowCount(const QModelIndex &parent) const -{ - if(parent.isValid()) - return 0; - else - return m_data.size(); -} - -QVariant ThemesModel::data(const QModelIndex &index, int role) const -{ - if(index.column() > 0 || index.row() >= m_data.size()) - return QVariant(); - else - return m_data.at(index.row()).value(role); -} - -bool ThemesModel::setData(const QModelIndex &index, const QVariant &value, int role) -{ - if(index.column() > 0 || index.row() >= m_data.size()) - return false; - else - { - m_data[index.row()].insert(role, value); - - return true; - } - -} - - - - diff -r 8af2bf10ee62 -r 7f77fa908a4e QTfrontend/model/themesmodel.h --- a/QTfrontend/model/themesmodel.h Fri Apr 27 16:57:54 2012 -0400 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,28 +0,0 @@ -#ifndef THEMESMODEL_H -#define THEMESMODEL_H - -#include -#include -#include - -class ThemesModel : public QAbstractListModel -{ - Q_OBJECT - public: - explicit ThemesModel(QStringList themes, QObject *parent = 0); - - int rowCount(const QModelIndex &parent = QModelIndex()) const; - QVariant data(const QModelIndex &index, int role) const; - bool setData(const QModelIndex &index, const QVariant &value, - int role = Qt::EditRole); - - signals: - - public slots: - - private: - - QList > m_data; -}; - -#endif // THEMESMODEL_H diff -r 8af2bf10ee62 -r 7f77fa908a4e QTfrontend/ui/widget/mapContainer.cpp --- a/QTfrontend/ui/widget/mapContainer.cpp Fri Apr 27 16:57:54 2012 -0400 +++ b/QTfrontend/ui/widget/mapContainer.cpp Thu Apr 26 16:15:37 2012 +0200 @@ -122,7 +122,8 @@ lvThemes = new QListView(mapWidget); lvThemes->setMinimumHeight(30); lvThemes->setFixedWidth(140); - lvThemes->setModel(themesModel); + m_themeModel = DataManager::instance().themeModel(); + lvThemes->setModel(m_themeModel); lvThemes->setIconSize(QSize(16, 16)); lvThemes->setEditTriggers(QListView::NoEditTriggers); @@ -400,7 +401,7 @@ void HWMapContainer::setTheme(const QString & theme) { - QModelIndexList mdl = themesModel->match(themesModel->index(0), Qt::DisplayRole, theme); + QModelIndexList mdl = m_themeModel->match(m_themeModel->index(0), Qt::DisplayRole, theme); if(mdl.size()) lvThemes->setCurrentIndex(mdl.at(0)); @@ -452,9 +453,9 @@ void HWMapContainer::setRandomTheme() { - if(!themesModel->rowCount()) return; - quint32 themeNum = rand() % themesModel->rowCount(); - lvThemes->setCurrentIndex(themesModel->index(themeNum)); + if(!m_themeModel->rowCount()) return; + quint32 themeNum = rand() % m_themeModel->rowCount(); + lvThemes->setCurrentIndex(m_themeModel->index(themeNum)); } void HWMapContainer::intSetTemplateFilter(int filter) diff -r 8af2bf10ee62 -r 7f77fa908a4e QTfrontend/ui/widget/mapContainer.h --- a/QTfrontend/ui/widget/mapContainer.h Fri Apr 27 16:57:54 2012 -0400 +++ b/QTfrontend/ui/widget/mapContainer.h Thu Apr 26 16:15:37 2012 +0200 @@ -105,6 +105,7 @@ QComboBox* chooseMap; IconedGroupBox* gbThemes; QListView* lvThemes; + ThemeModel * m_themeModel; HWMap* pMap; QString m_seed; QPushButton* seedSet; diff -r 8af2bf10ee62 -r 7f77fa908a4e QTfrontend/util/DataManager.cpp --- a/QTfrontend/util/DataManager.cpp Fri Apr 27 16:57:54 2012 -0400 +++ b/QTfrontend/util/DataManager.cpp Thu Apr 26 16:15:37 2012 +0200 @@ -34,11 +34,13 @@ DataManager::DataManager() { - userData = new QDir(cfgdir->absolutePath()); - if (!userData->cd("Data")) - userData = NULL; + m_userData = new QDir(cfgdir->absolutePath()); + if (!m_userData->cd("Data")) + m_userData = NULL; - defaultData = new QDir(datadir->absolutePath()); + m_defaultData = new QDir(datadir->absolutePath()); + + m_themeModel = NULL; } @@ -57,14 +59,14 @@ { QStringList result; - if (userData != NULL) + if (m_userData != NULL) { - QDir tmpDir(*userData); + QDir tmpDir(*m_userData); if (tmpDir.cd(subDirectory)) result.append(tmpDir.entryList(nameFilters, filters)); } - QDir tmpDir(*defaultData); + QDir tmpDir(*m_defaultData); if (tmpDir.cd(subDirectory)) result.append(tmpDir.entryList(nameFilters, filters)); @@ -87,11 +89,11 @@ { QString path; - if (userData != NULL) - path = userData->absolutePath()+"/"+relativeDataFilePath; + if (m_userData != NULL) + path = m_userData->absolutePath()+"/"+relativeDataFilePath; if ((!path.isEmpty()) && (!QFile::exists(path))) - path = defaultData->absolutePath()+"/"+relativeDataFilePath; + path = m_defaultData->absolutePath()+"/"+relativeDataFilePath; return path; } @@ -100,9 +102,9 @@ QString DataManager::findFileForWrite( const QString & relativeDataFilePath) const { - if (userData != NULL) + if (m_userData != NULL) { - QString path = userData->absolutePath()+"/"+relativeDataFilePath; + QString path = m_userData->absolutePath()+"/"+relativeDataFilePath; // create folders if needed QDir tmp; @@ -115,7 +117,17 @@ return ""; } +ThemeModel * DataManager::themeModel() +{ + if (m_themeModel == NULL) { + m_themeModel = new ThemeModel(); + m_themeModel->loadThemes(); + } + return m_themeModel; +} + void DataManager::reload() { + m_themeModel->loadThemes(); emit updated(); } diff -r 8af2bf10ee62 -r 7f77fa908a4e QTfrontend/util/DataManager.h --- a/QTfrontend/util/DataManager.h Fri Apr 27 16:57:54 2012 -0400 +++ b/QTfrontend/util/DataManager.h Thu Apr 26 16:15:37 2012 +0200 @@ -30,9 +30,12 @@ #include +#include "ThemeModel.h" + class QDir; class QFile; class QStringList; +class ThemeModel; /** * @brief Offers access to the data files of hedgewars. @@ -90,6 +93,14 @@ */ QString findFileForWrite(const QString & relativeDataFilePath) const; + /** + * @brief Returns pointer to a model for the available themes. + * + * The model is kept up to date automatically. + * + * @return theme model pointer + */ + ThemeModel * themeModel(); public slots: /** @@ -116,8 +127,10 @@ */ DataManager(); - QDir * defaultData; ///< directory of the installed data - QDir * userData; ///< directory of custom data in the user's directory + QDir * m_defaultData; ///< directory of the installed data + QDir * m_userData; ///< directory of custom data in the user's directory + + ThemeModel * m_themeModel; ///< themes model instance }; #endif // HEDGEWARS_DATAMANAGER_H diff -r 8af2bf10ee62 -r 7f77fa908a4e project_files/hedgewars.pro --- a/project_files/hedgewars.pro Fri Apr 27 16:57:54 2012 -0400 +++ b/project_files/hedgewars.pro Thu Apr 26 16:15:37 2012 +0200 @@ -22,7 +22,7 @@ QT += network QT += webkit -HEADERS += ../QTfrontend/model/themesmodel.h \ +HEADERS += ../QTfrontend/model/ThemeModel.h \ ../QTfrontend/model/ammoSchemeModel.h \ ../QTfrontend/model/netserverslist.h \ ../QTfrontend/model/hats.h \ @@ -104,7 +104,7 @@ ../QTfrontend/ui/dialog/input_password.h SOURCES += ../QTfrontend/model/ammoSchemeModel.cpp \ - ../QTfrontend/model/themesmodel.cpp \ + ../QTfrontend/model/ThemeModel.cpp \ ../QTfrontend/model/hats.cpp \ ../QTfrontend/model/netserverslist.cpp \ ../QTfrontend/ui/qaspectratiolayout.cpp \