Changeset View
Changeset View
Standalone View
Standalone View
kcms/style/stylesmodel.cpp
- This file was added.
1 | /* | ||||
---|---|---|---|---|---|
2 | * Copyright (C) 2019 Kai Uwe Broulik <kde@broulik.de> | ||||
3 | * | ||||
4 | * This program is free software; you can redistribute it and/or | ||||
5 | * modify it under the terms of the GNU General Public License as | ||||
6 | * published by the Free Software Foundation; either version 2 of | ||||
7 | * the License or (at your option) version 3 or any later version | ||||
8 | * accepted by the membership of KDE e.V. (or its successor approved | ||||
9 | * by the membership of KDE e.V.), which shall act as a proxy | ||||
10 | * defined in Section 14 of version 3 of the license. | ||||
11 | * | ||||
12 | * This program is distributed in the hope that it will be useful, | ||||
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||||
15 | * GNU General Public License for more details. | ||||
16 | * | ||||
17 | * You should have received a copy of the GNU General Public License | ||||
18 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | ||||
19 | */ | ||||
20 | | ||||
21 | #include "stylesmodel.h" | ||||
22 | | ||||
23 | #include <QCollator> | ||||
24 | #include <QDir> | ||||
25 | #include <QStandardPaths> | ||||
26 | #include <QStyleFactory> | ||||
27 | | ||||
28 | #include <KConfig> | ||||
29 | #include <KConfigGroup> | ||||
30 | | ||||
31 | #include <algorithm> | ||||
32 | | ||||
33 | StylesModel::StylesModel(QObject *parent) : QAbstractListModel(parent) | ||||
34 | { | ||||
35 | | ||||
36 | } | ||||
37 | | ||||
38 | StylesModel::~StylesModel() = default; | ||||
39 | | ||||
40 | int StylesModel::rowCount(const QModelIndex &parent) const | ||||
41 | { | ||||
42 | if (parent.isValid()) { | ||||
43 | return 0; | ||||
44 | } | ||||
45 | | ||||
46 | return m_data.count(); | ||||
47 | } | ||||
48 | | ||||
49 | QVariant StylesModel::data(const QModelIndex &index, int role) const | ||||
50 | { | ||||
51 | if (!checkIndex(index)) { | ||||
52 | return QVariant(); | ||||
53 | } | ||||
54 | | ||||
55 | const auto &item = m_data.at(index.row()); | ||||
56 | | ||||
57 | switch (role) { | ||||
58 | case Qt::DisplayRole: | ||||
59 | if (!item.display.isEmpty()) { | ||||
60 | return item.display; | ||||
61 | } | ||||
62 | return item.styleName; | ||||
63 | case StyleNameRole: return item.styleName; | ||||
64 | case DescriptionRole: return item.description; | ||||
65 | case ConfigurableRole: return !item.configPage.isEmpty(); | ||||
66 | } | ||||
67 | | ||||
68 | return QVariant(); | ||||
69 | } | ||||
70 | | ||||
71 | QHash<int, QByteArray> StylesModel::roleNames() const | ||||
72 | { | ||||
73 | return { | ||||
74 | {Qt::DisplayRole, QByteArrayLiteral("display")}, | ||||
75 | {StyleNameRole, QByteArrayLiteral("styleName")}, | ||||
76 | {DescriptionRole, QByteArrayLiteral("description")}, | ||||
77 | {ConfigurableRole, QByteArrayLiteral("configurable")} | ||||
78 | }; | ||||
79 | } | ||||
80 | | ||||
81 | QString StylesModel::selectedStyle() const | ||||
82 | { | ||||
83 | return m_selectedStyle; | ||||
84 | } | ||||
85 | | ||||
86 | void StylesModel::setSelectedStyle(const QString &style) | ||||
87 | { | ||||
88 | if (m_selectedStyle == style) { | ||||
89 | return; | ||||
90 | } | ||||
91 | | ||||
92 | const bool firstTime = m_selectedStyle.isNull(); | ||||
93 | m_selectedStyle = style; | ||||
94 | | ||||
95 | if (!firstTime) { | ||||
96 | emit selectedStyleChanged(style); | ||||
97 | } | ||||
98 | emit selectedStyleIndexChanged(); | ||||
99 | } | ||||
100 | | ||||
101 | int StylesModel::indexOfStyle(const QString &style) const | ||||
102 | { | ||||
103 | auto it = std::find_if(m_data.begin(), m_data.end(), [&style](const StylesModelData &item) { | ||||
104 | return item.styleName == style; | ||||
105 | }); | ||||
106 | | ||||
107 | if (it != m_data.end()) { | ||||
108 | return std::distance(m_data.begin(), it); | ||||
109 | } | ||||
110 | | ||||
111 | return -1; | ||||
112 | } | ||||
113 | | ||||
114 | int StylesModel::selectedStyleIndex() const | ||||
115 | { | ||||
116 | return indexOfStyle(m_selectedStyle); | ||||
117 | } | ||||
118 | | ||||
119 | QString StylesModel::styleConfigPage(const QString &style) const | ||||
120 | { | ||||
121 | const int idx = indexOfStyle(style); | ||||
122 | if (idx == -1) { | ||||
123 | return QString(); | ||||
124 | } | ||||
125 | | ||||
126 | return m_data.at(idx).configPage; | ||||
127 | } | ||||
128 | | ||||
129 | void StylesModel::load() | ||||
130 | { | ||||
131 | beginResetModel(); | ||||
132 | | ||||
133 | const int oldCount = m_data.count(); | ||||
134 | | ||||
135 | m_data.clear(); | ||||
136 | | ||||
137 | // Combines the info we get from QStyleFactory and our themerc files | ||||
138 | QHash<QString, StylesModelData> styleData; | ||||
139 | | ||||
140 | const QStringList allStyles = QStyleFactory::keys(); | ||||
141 | for (const QString &styleName : allStyles) { | ||||
142 | auto &item = styleData[styleName]; | ||||
143 | item.styleName = styleName; | ||||
144 | } | ||||
145 | | ||||
146 | QStringList themeFiles; | ||||
147 | | ||||
148 | const QStringList themeDirs = QStandardPaths::locateAll(QStandardPaths::GenericDataLocation, QStringLiteral("kstyle/themes"), QStandardPaths::LocateDirectory); | ||||
149 | for (const QString &dir : themeDirs) { | ||||
150 | const QStringList fileNames = QDir(dir).entryList(QStringList{QStringLiteral("*.themerc")}); | ||||
151 | for (const QString &file : fileNames) { | ||||
152 | const QString suffixedFileName = QLatin1String("kstyle/themes/") + file; | ||||
153 | if (!themeFiles.contains(suffixedFileName)) { | ||||
154 | themeFiles.append(suffixedFileName); | ||||
155 | } | ||||
156 | } | ||||
157 | } | ||||
158 | | ||||
159 | std::transform(themeFiles.begin(), themeFiles.end(), themeFiles.begin(), [](const QString &item) { | ||||
160 | return QStandardPaths::locate(QStandardPaths::GenericDataLocation, item); | ||||
161 | }); | ||||
162 | | ||||
163 | for (const QString &file : themeFiles) { | ||||
164 | KConfig config(file, KConfig::SimpleConfig); | ||||
165 | if (!config.hasGroup("KDE") || !config.hasGroup("Misc")) { | ||||
166 | continue; | ||||
167 | } | ||||
168 | | ||||
169 | KConfigGroup kdeGroup = config.group("KDE"); | ||||
170 | | ||||
171 | const QString styleName = kdeGroup.readEntry("WidgetStyle", QString()); | ||||
172 | if (styleName.isEmpty()) { | ||||
173 | continue; | ||||
174 | } | ||||
175 | | ||||
176 | auto it = styleData.find(styleName); | ||||
177 | if (it == styleData.end()) { | ||||
178 | continue; | ||||
179 | } | ||||
180 | | ||||
181 | auto &item = *it; | ||||
182 | | ||||
183 | KConfigGroup desktopEntryGroup = config.group("Desktop Entry"); | ||||
184 | if (desktopEntryGroup.readEntry("Hidden", false)) { | ||||
185 | // Don't list hidden styles | ||||
186 | styleData.remove(styleName); | ||||
187 | continue; | ||||
188 | } | ||||
189 | | ||||
190 | KConfigGroup miscGroup = config.group("Misc"); | ||||
191 | | ||||
192 | item.display = miscGroup.readEntry("Name"); | ||||
193 | item.description = miscGroup.readEntry("Comment"); | ||||
194 | item.configPage = miscGroup.readEntry("ConfigPage"); | ||||
195 | } | ||||
196 | | ||||
197 | m_data = styleData.values().toVector(); | ||||
198 | | ||||
199 | QCollator collator; | ||||
200 | std::sort(m_data.begin(), m_data.end(), [&collator](const StylesModelData &a, const StylesModelData &b) { | ||||
201 | const QString aDisplay = !a.display.isEmpty() ? a.display : a.styleName; | ||||
202 | const QString bDisplay = !b.display.isEmpty() ? b.display : b.styleName; | ||||
203 | return collator.compare(aDisplay, bDisplay) < 0; | ||||
204 | }); | ||||
205 | | ||||
206 | endResetModel(); | ||||
207 | | ||||
208 | // an item might have been added before the currently selected one | ||||
209 | if (oldCount != m_data.count()) { | ||||
210 | emit selectedStyleIndexChanged(); | ||||
211 | } | ||||
212 | } |