author | Wuzzy <almikes@aol.com> |
Fri, 11 Sep 2015 03:07:48 +0200 | |
changeset 11642 | 45316b6eb4a0 |
parent 11046 | 47a8c19ecb60 |
child 11758 | 5c192a4751d8 |
permissions | -rw-r--r-- |
6948 | 1 |
/* |
2 |
* Hedgewars, a free turn based strategy game |
|
11046 | 3 |
* Copyright (c) 2004-2015 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 |
|
10108
c68cf030eded
update FSF address. note: two sdl include files (by Sam Lantinga) still have the old FSF address in their copyright - but I ain't gonna touch their copyright headers
sheepluva
parents:
9998
diff
changeset
|
16 |
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
6948 | 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 |
|
8419
d99f46b676b5
Prepends an asterisk on maps, styles, and themes that are DLC. (Resolves issue 515)
dag10
parents:
8377
diff
changeset
|
24 |
#include "physfs.h" |
6937
7f77fa908a4e
messing with the theme model a bit (gets now auto-updated after DLC download too)
sheepluva
parents:
6061
diff
changeset
|
25 |
#include "ThemeModel.h" |
8464 | 26 |
#include "hwconsts.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
|
27 |
|
6937
7f77fa908a4e
messing with the theme model a bit (gets now auto-updated after DLC download too)
sheepluva
parents:
6061
diff
changeset
|
28 |
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
|
29 |
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
|
30 |
{ |
6937
7f77fa908a4e
messing with the theme model a bit (gets now auto-updated after DLC download too)
sheepluva
parents:
6061
diff
changeset
|
31 |
m_data = QList<QMap<int, QVariant> >(); |
9745 | 32 |
|
33 |
m_themesLoaded = false; |
|
5289
9d18b61bd3eb
- Implement ThemesModel (load theme icons once, store in memory, don't reload from disk every time selection changes)
unc0rr
parents:
diff
changeset
|
34 |
} |
9d18b61bd3eb
- Implement ThemesModel (load theme icons once, store in memory, don't reload from disk every time selection changes)
unc0rr
parents:
diff
changeset
|
35 |
|
6937
7f77fa908a4e
messing with the theme model a bit (gets now auto-updated after DLC download too)
sheepluva
parents:
6061
diff
changeset
|
36 |
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
|
37 |
{ |
9d18b61bd3eb
- Implement ThemesModel (load theme icons once, store in memory, don't reload from disk every time selection changes)
unc0rr
parents:
diff
changeset
|
38 |
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
|
39 |
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
|
40 |
else |
9745 | 41 |
{ |
42 |
if(!m_themesLoaded) |
|
43 |
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
|
44 |
return m_data.size(); |
9745 | 45 |
} |
5289
9d18b61bd3eb
- Implement ThemesModel (load theme icons once, store in memory, don't reload from disk every time selection changes)
unc0rr
parents:
diff
changeset
|
46 |
} |
9d18b61bd3eb
- Implement ThemesModel (load theme icons once, store in memory, don't reload from disk every time selection changes)
unc0rr
parents:
diff
changeset
|
47 |
|
6937
7f77fa908a4e
messing with the theme model a bit (gets now auto-updated after DLC download too)
sheepluva
parents:
6061
diff
changeset
|
48 |
|
7f77fa908a4e
messing with the theme model a bit (gets now auto-updated after DLC download too)
sheepluva
parents:
6061
diff
changeset
|
49 |
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
|
50 |
{ |
9d18b61bd3eb
- Implement ThemesModel (load theme icons once, store in memory, don't reload from disk every time selection changes)
unc0rr
parents:
diff
changeset
|
51 |
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
|
52 |
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
|
53 |
else |
9745 | 54 |
{ |
55 |
if(!m_themesLoaded) |
|
56 |
loadThemes(); |
|
57 |
||
5289
9d18b61bd3eb
- Implement ThemesModel (load theme icons once, store in memory, don't reload from disk every time selection changes)
unc0rr
parents:
diff
changeset
|
58 |
return m_data.at(index.row()).value(role); |
9745 | 59 |
} |
5289
9d18b61bd3eb
- Implement ThemesModel (load theme icons once, store in memory, don't reload from disk every time selection changes)
unc0rr
parents:
diff
changeset
|
60 |
} |
9d18b61bd3eb
- Implement ThemesModel (load theme icons once, store in memory, don't reload from disk every time selection changes)
unc0rr
parents:
diff
changeset
|
61 |
|
6937
7f77fa908a4e
messing with the theme model a bit (gets now auto-updated after DLC download too)
sheepluva
parents:
6061
diff
changeset
|
62 |
|
9745 | 63 |
void ThemeModel::loadThemes() 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
|
64 |
{ |
9815 | 65 |
qDebug("[LAZINESS] ThemeModel::loadThemes()"); |
9745 | 66 |
|
67 |
m_themesLoaded = true; |
|
68 |
||
6937
7f77fa908a4e
messing with the theme model a bit (gets now auto-updated after DLC download too)
sheepluva
parents:
6061
diff
changeset
|
69 |
|
7f77fa908a4e
messing with the theme model a bit (gets now auto-updated after DLC download too)
sheepluva
parents:
6061
diff
changeset
|
70 |
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
|
71 |
|
7f77fa908a4e
messing with the theme model a bit (gets now auto-updated after DLC download too)
sheepluva
parents:
6061
diff
changeset
|
72 |
QStringList themes = |
7f77fa908a4e
messing with the theme model a bit (gets now auto-updated after DLC download too)
sheepluva
parents:
6061
diff
changeset
|
73 |
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
|
74 |
|
7f77fa908a4e
messing with the theme model a bit (gets now auto-updated after DLC download too)
sheepluva
parents:
6061
diff
changeset
|
75 |
m_data.clear(); |
7f77fa908a4e
messing with the theme model a bit (gets now auto-updated after DLC download too)
sheepluva
parents:
6061
diff
changeset
|
76 |
|
7f77fa908a4e
messing with the theme model a bit (gets now auto-updated after DLC download too)
sheepluva
parents:
6061
diff
changeset
|
77 |
#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
|
78 |
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
|
79 |
#endif |
7f77fa908a4e
messing with the theme model a bit (gets now auto-updated after DLC download too)
sheepluva
parents:
6061
diff
changeset
|
80 |
|
7f77fa908a4e
messing with the theme model a bit (gets now auto-updated after DLC download too)
sheepluva
parents:
6061
diff
changeset
|
81 |
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
|
82 |
{ |
6937
7f77fa908a4e
messing with the theme model a bit (gets now auto-updated after DLC download too)
sheepluva
parents:
6061
diff
changeset
|
83 |
// themes without icon are supposed to be hidden |
8049 | 84 |
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
|
85 |
|
7f77fa908a4e
messing with the theme model a bit (gets now auto-updated after DLC download too)
sheepluva
parents:
6061
diff
changeset
|
86 |
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
|
87 |
continue; |
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 |
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
|
90 |
|
8419
d99f46b676b5
Prepends an asterisk on maps, styles, and themes that are DLC. (Resolves issue 515)
dag10
parents:
8377
diff
changeset
|
91 |
// detect if theme is dlc |
d99f46b676b5
Prepends an asterisk on maps, styles, and themes that are DLC. (Resolves issue 515)
dag10
parents:
8377
diff
changeset
|
92 |
QString themeDir = PHYSFS_getRealDir(QString("Themes/%1/icon.png").arg(theme).toLocal8Bit().data()); |
8475
f605bc59c603
Initial theme selection is now random. Theme prompt now has a search input and no longer uses FlowLayout. (Resolves issue 520)
dag10
parents:
8464
diff
changeset
|
93 |
bool isDLC = !themeDir.startsWith(datadir->absolutePath()); |
f605bc59c603
Initial theme selection is now random. Theme prompt now has a search input and no longer uses FlowLayout. (Resolves issue 520)
dag10
parents:
8464
diff
changeset
|
94 |
dataset.insert(IsDlcRole, isDLC); |
8419
d99f46b676b5
Prepends an asterisk on maps, styles, and themes that are DLC. (Resolves issue 515)
dag10
parents:
8377
diff
changeset
|
95 |
|
8377 | 96 |
// set icon path |
8475
f605bc59c603
Initial theme selection is now random. Theme prompt now has a search input and no longer uses FlowLayout. (Resolves issue 520)
dag10
parents:
8464
diff
changeset
|
97 |
dataset.insert(IconPathRole, iconpath); |
8377 | 98 |
|
6937
7f77fa908a4e
messing with the theme model a bit (gets now auto-updated after DLC download too)
sheepluva
parents:
6061
diff
changeset
|
99 |
// set name |
8475
f605bc59c603
Initial theme selection is now random. Theme prompt now has a search input and no longer uses FlowLayout. (Resolves issue 520)
dag10
parents:
8464
diff
changeset
|
100 |
dataset.insert(ActualNameRole, theme); |
6937
7f77fa908a4e
messing with the theme model a bit (gets now auto-updated after DLC download too)
sheepluva
parents:
6061
diff
changeset
|
101 |
|
8475
f605bc59c603
Initial theme selection is now random. Theme prompt now has a search input and no longer uses FlowLayout. (Resolves issue 520)
dag10
parents:
8464
diff
changeset
|
102 |
// set displayed name |
f605bc59c603
Initial theme selection is now random. Theme prompt now has a search input and no longer uses FlowLayout. (Resolves issue 520)
dag10
parents:
8464
diff
changeset
|
103 |
dataset.insert(Qt::DisplayRole, (isDLC ? "*" : "") + theme); |
6937
7f77fa908a4e
messing with the theme model a bit (gets now auto-updated after DLC download too)
sheepluva
parents:
6061
diff
changeset
|
104 |
|
7f77fa908a4e
messing with the theme model a bit (gets now auto-updated after DLC download too)
sheepluva
parents:
6061
diff
changeset
|
105 |
// load and set preview icon |
8049 | 106 |
QIcon preview(QString("physfs://Themes/%1/icon@2x.png").arg(theme)); |
8475
f605bc59c603
Initial theme selection is now random. Theme prompt now has a search input and no longer uses FlowLayout. (Resolves issue 520)
dag10
parents:
8464
diff
changeset
|
107 |
dataset.insert(Qt::DecorationRole, preview); |
6937
7f77fa908a4e
messing with the theme model a bit (gets now auto-updated after DLC download too)
sheepluva
parents:
6061
diff
changeset
|
108 |
|
7f77fa908a4e
messing with the theme model a bit (gets now auto-updated after DLC download too)
sheepluva
parents:
6061
diff
changeset
|
109 |
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
|
110 |
} |
8436
b89aacebb9db
- Also pass unknown cmds to the server when in room
unc0rr
parents:
8419
diff
changeset
|
111 |
} |