|
1 |
|
2 #include "ThemeModel.h" |
|
3 |
|
4 ThemeModel::ThemeModel(QObject *parent) : |
|
5 QAbstractListModel(parent) |
|
6 { |
|
7 m_data = QList<QMap<int, QVariant> >(); |
|
8 } |
|
9 |
|
10 int ThemeModel::rowCount(const QModelIndex &parent) const |
|
11 { |
|
12 if(parent.isValid()) |
|
13 return 0; |
|
14 else |
|
15 return m_data.size(); |
|
16 } |
|
17 |
|
18 |
|
19 QVariant ThemeModel::data(const QModelIndex &index, int role) const |
|
20 { |
|
21 if(index.column() > 0 || index.row() >= m_data.size()) |
|
22 return QVariant(); |
|
23 else |
|
24 return m_data.at(index.row()).value(role); |
|
25 } |
|
26 |
|
27 |
|
28 void ThemeModel::loadThemes() |
|
29 { |
|
30 beginResetModel(); |
|
31 |
|
32 |
|
33 DataManager & datamgr = DataManager::instance(); |
|
34 |
|
35 QStringList themes = |
|
36 datamgr.entryList("Themes", QDir::AllDirs | QDir::NoDotAndDotDot); |
|
37 |
|
38 m_data.clear(); |
|
39 |
|
40 #if QT_VERSION >= QT_VERSION_CHECK(4, 7, 0) |
|
41 m_data.reserve(themes.size()); |
|
42 #endif |
|
43 |
|
44 foreach (QString theme, themes) |
|
45 { |
|
46 // themes without icon are supposed to be hidden |
|
47 QString iconpath = |
|
48 datamgr.findFileForRead(QString("Themes/%1/icon.png").arg(theme)); |
|
49 |
|
50 if (!QFile::exists(iconpath)) |
|
51 continue; |
|
52 |
|
53 QMap<int, QVariant> dataset; |
|
54 |
|
55 // set name |
|
56 dataset.insert(Qt::DisplayRole, theme); |
|
57 |
|
58 // load and set icon |
|
59 QIcon icon(iconpath); |
|
60 dataset.insert(Qt::DecorationRole, icon); |
|
61 |
|
62 // load and set preview icon |
|
63 QIcon preview(datamgr.findFileForRead(QString("Themes/%1/icon@2x.png").arg(theme))); |
|
64 dataset.insert(Qt::UserRole, preview); |
|
65 |
|
66 m_data.append(dataset); |
|
67 } |
|
68 |
|
69 |
|
70 endResetModel(); |
|
71 } |
|
72 |
|
73 |
|
74 |
|
75 |