author | dag10 <gottlieb.drew@gmail.com> |
Sat, 19 Jan 2013 20:01:01 -0500 | |
changeset 8408 | 286823b10f35 |
parent 8377 | 869f80966a77 |
child 8419 | d99f46b676b5 |
permissions | -rw-r--r-- |
6948 | 1 |
/* |
2 |
* Hedgewars, a free turn based strategy game |
|
6952 | 3 |
* Copyright (c) 2004-2012 Andrey Korotaev <unC0Rr@gmail.com> |
6948 | 4 |
* |
5 |
* This program is free software; you can redistribute it and/or modify |
|
6 |
* it under the terms of the GNU General Public License as published by |
|
7 |
* the Free Software Foundation; version 2 of the License |
|
8 |
* |
|
9 |
* This program is distributed in the hope that it will be useful, |
|
10 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
12 |
* GNU General Public License for more details. |
|
13 |
* |
|
14 |
* You should have received a copy of the GNU General Public License |
|
15 |
* along with this program; if not, write to the Free Software |
|
16 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA |
|
17 |
*/ |
|
18 |
||
19 |
/** |
|
20 |
* @file |
|
21 |
* @brief ThemeModel class implementation |
|
22 |
*/ |
|
5289
9d18b61bd3eb
- Implement ThemesModel (load theme icons once, store in memory, don't reload from disk every time selection changes)
unc0rr
parents:
diff
changeset
|
23 |
|
6937
7f77fa908a4e
messing with the theme model a bit (gets now auto-updated after DLC download too)
sheepluva
parents:
6061
diff
changeset
|
24 |
#include "ThemeModel.h" |
5289
9d18b61bd3eb
- Implement ThemesModel (load theme icons once, store in memory, don't reload from disk every time selection changes)
unc0rr
parents:
diff
changeset
|
25 |
|
6937
7f77fa908a4e
messing with the theme model a bit (gets now auto-updated after DLC download too)
sheepluva
parents:
6061
diff
changeset
|
26 |
ThemeModel::ThemeModel(QObject *parent) : |
5289
9d18b61bd3eb
- Implement ThemesModel (load theme icons once, store in memory, don't reload from disk every time selection changes)
unc0rr
parents:
diff
changeset
|
27 |
QAbstractListModel(parent) |
9d18b61bd3eb
- Implement ThemesModel (load theme icons once, store in memory, don't reload from disk every time selection changes)
unc0rr
parents:
diff
changeset
|
28 |
{ |
6937
7f77fa908a4e
messing with the theme model a bit (gets now auto-updated after DLC download too)
sheepluva
parents:
6061
diff
changeset
|
29 |
m_data = QList<QMap<int, QVariant> >(); |
5289
9d18b61bd3eb
- Implement ThemesModel (load theme icons once, store in memory, don't reload from disk every time selection changes)
unc0rr
parents:
diff
changeset
|
30 |
} |
9d18b61bd3eb
- Implement ThemesModel (load theme icons once, store in memory, don't reload from disk every time selection changes)
unc0rr
parents:
diff
changeset
|
31 |
|
6937
7f77fa908a4e
messing with the theme model a bit (gets now auto-updated after DLC download too)
sheepluva
parents:
6061
diff
changeset
|
32 |
int ThemeModel::rowCount(const QModelIndex &parent) const |
5289
9d18b61bd3eb
- Implement ThemesModel (load theme icons once, store in memory, don't reload from disk every time selection changes)
unc0rr
parents:
diff
changeset
|
33 |
{ |
9d18b61bd3eb
- Implement ThemesModel (load theme icons once, store in memory, don't reload from disk every time selection changes)
unc0rr
parents:
diff
changeset
|
34 |
if(parent.isValid()) |
9d18b61bd3eb
- Implement ThemesModel (load theme icons once, store in memory, don't reload from disk every time selection changes)
unc0rr
parents:
diff
changeset
|
35 |
return 0; |
9d18b61bd3eb
- Implement ThemesModel (load theme icons once, store in memory, don't reload from disk every time selection changes)
unc0rr
parents:
diff
changeset
|
36 |
else |
9d18b61bd3eb
- Implement ThemesModel (load theme icons once, store in memory, don't reload from disk every time selection changes)
unc0rr
parents:
diff
changeset
|
37 |
return m_data.size(); |
9d18b61bd3eb
- Implement ThemesModel (load theme icons once, store in memory, don't reload from disk every time selection changes)
unc0rr
parents:
diff
changeset
|
38 |
} |
9d18b61bd3eb
- Implement ThemesModel (load theme icons once, store in memory, don't reload from disk every time selection changes)
unc0rr
parents:
diff
changeset
|
39 |
|
6937
7f77fa908a4e
messing with the theme model a bit (gets now auto-updated after DLC download too)
sheepluva
parents:
6061
diff
changeset
|
40 |
|
7f77fa908a4e
messing with the theme model a bit (gets now auto-updated after DLC download too)
sheepluva
parents:
6061
diff
changeset
|
41 |
QVariant ThemeModel::data(const QModelIndex &index, int role) const |
5289
9d18b61bd3eb
- Implement ThemesModel (load theme icons once, store in memory, don't reload from disk every time selection changes)
unc0rr
parents:
diff
changeset
|
42 |
{ |
9d18b61bd3eb
- Implement ThemesModel (load theme icons once, store in memory, don't reload from disk every time selection changes)
unc0rr
parents:
diff
changeset
|
43 |
if(index.column() > 0 || index.row() >= m_data.size()) |
9d18b61bd3eb
- Implement ThemesModel (load theme icons once, store in memory, don't reload from disk every time selection changes)
unc0rr
parents:
diff
changeset
|
44 |
return QVariant(); |
9d18b61bd3eb
- Implement ThemesModel (load theme icons once, store in memory, don't reload from disk every time selection changes)
unc0rr
parents:
diff
changeset
|
45 |
else |
9d18b61bd3eb
- Implement ThemesModel (load theme icons once, store in memory, don't reload from disk every time selection changes)
unc0rr
parents:
diff
changeset
|
46 |
return m_data.at(index.row()).value(role); |
9d18b61bd3eb
- Implement ThemesModel (load theme icons once, store in memory, don't reload from disk every time selection changes)
unc0rr
parents:
diff
changeset
|
47 |
} |
9d18b61bd3eb
- Implement ThemesModel (load theme icons once, store in memory, don't reload from disk every time selection changes)
unc0rr
parents:
diff
changeset
|
48 |
|
6937
7f77fa908a4e
messing with the theme model a bit (gets now auto-updated after DLC download too)
sheepluva
parents:
6061
diff
changeset
|
49 |
|
7f77fa908a4e
messing with the theme model a bit (gets now auto-updated after DLC download too)
sheepluva
parents:
6061
diff
changeset
|
50 |
void ThemeModel::loadThemes() |
5289
9d18b61bd3eb
- Implement ThemesModel (load theme icons once, store in memory, don't reload from disk every time selection changes)
unc0rr
parents:
diff
changeset
|
51 |
{ |
6937
7f77fa908a4e
messing with the theme model a bit (gets now auto-updated after DLC download too)
sheepluva
parents:
6061
diff
changeset
|
52 |
beginResetModel(); |
7f77fa908a4e
messing with the theme model a bit (gets now auto-updated after DLC download too)
sheepluva
parents:
6061
diff
changeset
|
53 |
|
7f77fa908a4e
messing with the theme model a bit (gets now auto-updated after DLC download too)
sheepluva
parents:
6061
diff
changeset
|
54 |
|
7f77fa908a4e
messing with the theme model a bit (gets now auto-updated after DLC download too)
sheepluva
parents:
6061
diff
changeset
|
55 |
DataManager & datamgr = DataManager::instance(); |
7f77fa908a4e
messing with the theme model a bit (gets now auto-updated after DLC download too)
sheepluva
parents:
6061
diff
changeset
|
56 |
|
7f77fa908a4e
messing with the theme model a bit (gets now auto-updated after DLC download too)
sheepluva
parents:
6061
diff
changeset
|
57 |
QStringList themes = |
7f77fa908a4e
messing with the theme model a bit (gets now auto-updated after DLC download too)
sheepluva
parents:
6061
diff
changeset
|
58 |
datamgr.entryList("Themes", QDir::AllDirs | QDir::NoDotAndDotDot); |
7f77fa908a4e
messing with the theme model a bit (gets now auto-updated after DLC download too)
sheepluva
parents:
6061
diff
changeset
|
59 |
|
7f77fa908a4e
messing with the theme model a bit (gets now auto-updated after DLC download too)
sheepluva
parents:
6061
diff
changeset
|
60 |
m_data.clear(); |
7f77fa908a4e
messing with the theme model a bit (gets now auto-updated after DLC download too)
sheepluva
parents:
6061
diff
changeset
|
61 |
|
7f77fa908a4e
messing with the theme model a bit (gets now auto-updated after DLC download too)
sheepluva
parents:
6061
diff
changeset
|
62 |
#if QT_VERSION >= QT_VERSION_CHECK(4, 7, 0) |
7f77fa908a4e
messing with the theme model a bit (gets now auto-updated after DLC download too)
sheepluva
parents:
6061
diff
changeset
|
63 |
m_data.reserve(themes.size()); |
7f77fa908a4e
messing with the theme model a bit (gets now auto-updated after DLC download too)
sheepluva
parents:
6061
diff
changeset
|
64 |
#endif |
7f77fa908a4e
messing with the theme model a bit (gets now auto-updated after DLC download too)
sheepluva
parents:
6061
diff
changeset
|
65 |
|
7f77fa908a4e
messing with the theme model a bit (gets now auto-updated after DLC download too)
sheepluva
parents:
6061
diff
changeset
|
66 |
foreach (QString theme, themes) |
5289
9d18b61bd3eb
- Implement ThemesModel (load theme icons once, store in memory, don't reload from disk every time selection changes)
unc0rr
parents:
diff
changeset
|
67 |
{ |
6937
7f77fa908a4e
messing with the theme model a bit (gets now auto-updated after DLC download too)
sheepluva
parents:
6061
diff
changeset
|
68 |
// themes without icon are supposed to be hidden |
8049 | 69 |
QString iconpath = QString("physfs://Themes/%1/icon.png").arg(theme); |
6937
7f77fa908a4e
messing with the theme model a bit (gets now auto-updated after DLC download too)
sheepluva
parents:
6061
diff
changeset
|
70 |
|
7f77fa908a4e
messing with the theme model a bit (gets now auto-updated after DLC download too)
sheepluva
parents:
6061
diff
changeset
|
71 |
if (!QFile::exists(iconpath)) |
7f77fa908a4e
messing with the theme model a bit (gets now auto-updated after DLC download too)
sheepluva
parents:
6061
diff
changeset
|
72 |
continue; |
7f77fa908a4e
messing with the theme model a bit (gets now auto-updated after DLC download too)
sheepluva
parents:
6061
diff
changeset
|
73 |
|
7f77fa908a4e
messing with the theme model a bit (gets now auto-updated after DLC download too)
sheepluva
parents:
6061
diff
changeset
|
74 |
QMap<int, QVariant> dataset; |
5289
9d18b61bd3eb
- Implement ThemesModel (load theme icons once, store in memory, don't reload from disk every time selection changes)
unc0rr
parents:
diff
changeset
|
75 |
|
8377 | 76 |
// set icon path |
77 |
dataset.insert(Qt::UserRole + 1, iconpath); |
|
78 |
||
6937
7f77fa908a4e
messing with the theme model a bit (gets now auto-updated after DLC download too)
sheepluva
parents:
6061
diff
changeset
|
79 |
// set name |
7f77fa908a4e
messing with the theme model a bit (gets now auto-updated after DLC download too)
sheepluva
parents:
6061
diff
changeset
|
80 |
dataset.insert(Qt::DisplayRole, theme); |
7f77fa908a4e
messing with the theme model a bit (gets now auto-updated after DLC download too)
sheepluva
parents:
6061
diff
changeset
|
81 |
|
7f77fa908a4e
messing with the theme model a bit (gets now auto-updated after DLC download too)
sheepluva
parents:
6061
diff
changeset
|
82 |
// load and set icon |
8377 | 83 |
QIcon icon; |
84 |
icon.addPixmap(QPixmap(iconpath), QIcon::Normal); |
|
85 |
icon.addPixmap(QPixmap(iconpath), QIcon::Disabled); |
|
7776 | 86 |
|
6937
7f77fa908a4e
messing with the theme model a bit (gets now auto-updated after DLC download too)
sheepluva
parents:
6061
diff
changeset
|
87 |
dataset.insert(Qt::DecorationRole, icon); |
7f77fa908a4e
messing with the theme model a bit (gets now auto-updated after DLC download too)
sheepluva
parents:
6061
diff
changeset
|
88 |
|
7f77fa908a4e
messing with the theme model a bit (gets now auto-updated after DLC download too)
sheepluva
parents:
6061
diff
changeset
|
89 |
// load and set preview icon |
8049 | 90 |
QIcon preview(QString("physfs://Themes/%1/icon@2x.png").arg(theme)); |
6937
7f77fa908a4e
messing with the theme model a bit (gets now auto-updated after DLC download too)
sheepluva
parents:
6061
diff
changeset
|
91 |
dataset.insert(Qt::UserRole, preview); |
7f77fa908a4e
messing with the theme model a bit (gets now auto-updated after DLC download too)
sheepluva
parents:
6061
diff
changeset
|
92 |
|
7f77fa908a4e
messing with the theme model a bit (gets now auto-updated after DLC download too)
sheepluva
parents:
6061
diff
changeset
|
93 |
m_data.append(dataset); |
5289
9d18b61bd3eb
- Implement ThemesModel (load theme icons once, store in memory, don't reload from disk every time selection changes)
unc0rr
parents:
diff
changeset
|
94 |
} |
9d18b61bd3eb
- Implement ThemesModel (load theme icons once, store in memory, don't reload from disk every time selection changes)
unc0rr
parents:
diff
changeset
|
95 |
|
6937
7f77fa908a4e
messing with the theme model a bit (gets now auto-updated after DLC download too)
sheepluva
parents:
6061
diff
changeset
|
96 |
|
7f77fa908a4e
messing with the theme model a bit (gets now auto-updated after DLC download too)
sheepluva
parents:
6061
diff
changeset
|
97 |
endResetModel(); |
5289
9d18b61bd3eb
- Implement ThemesModel (load theme icons once, store in memory, don't reload from disk every time selection changes)
unc0rr
parents:
diff
changeset
|
98 |
} |
9d18b61bd3eb
- Implement ThemesModel (load theme icons once, store in memory, don't reload from disk every time selection changes)
unc0rr
parents:
diff
changeset
|
99 |
|
9d18b61bd3eb
- Implement ThemesModel (load theme icons once, store in memory, don't reload from disk every time selection changes)
unc0rr
parents:
diff
changeset
|
100 |
|
9d18b61bd3eb
- Implement ThemesModel (load theme icons once, store in memory, don't reload from disk every time selection changes)
unc0rr
parents:
diff
changeset
|
101 |
|
9d18b61bd3eb
- Implement ThemesModel (load theme icons once, store in memory, don't reload from disk every time selection changes)
unc0rr
parents:
diff
changeset
|
102 |