Changeset View
Changeset View
Standalone View
Standalone View
kcms/colors/colorsmodel.cpp
- This file was added.
1 | /* | ||||
---|---|---|---|---|---|
2 | * Copyright (C) 2007 Matthew Woehlke <mw_triad@users.sourceforge.net> | ||||
3 | * Copyright (C) 2007 Jeremy Whiting <jpwhiting@kde.org> | ||||
4 | * Copyright (C) 2016 Olivier Churlaud <olivier@churlaud.com> | ||||
5 | * Copyright (C) 2019 Kai Uwe Broulik <kde@privat.broulik.de> | ||||
6 | * | ||||
7 | * This program is free software; you can redistribute it and/or | ||||
8 | * modify it under the terms of the GNU General Public License as | ||||
9 | * published by the Free Software Foundation; either version 2 of | ||||
10 | * the License or (at your option) version 3 or any later version | ||||
11 | * accepted by the membership of KDE e.V. (or its successor approved | ||||
12 | * by the membership of KDE e.V.), which shall act as a proxy | ||||
13 | * defined in Section 14 of version 3 of the license. | ||||
14 | * | ||||
15 | * This program is distributed in the hope that it will be useful, | ||||
16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||
17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||||
18 | * GNU General Public License for more details. | ||||
19 | * | ||||
20 | * You should have received a copy of the GNU General Public License | ||||
21 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | ||||
22 | */ | ||||
23 | | ||||
24 | #include "colorsmodel.h" | ||||
25 | | ||||
26 | #include <QCollator> | ||||
27 | #include <QDir> | ||||
28 | #include <QStandardPaths> | ||||
29 | | ||||
30 | #include <KColorScheme> | ||||
31 | #include <KConfigGroup> | ||||
32 | #include <KSharedConfig> | ||||
33 | | ||||
34 | #include <algorithm> | ||||
35 | | ||||
36 | ColorsModel::ColorsModel(QObject *parent) : QAbstractListModel(parent) | ||||
37 | { | ||||
38 | | ||||
39 | } | ||||
40 | | ||||
41 | ColorsModel::~ColorsModel() = default; | ||||
42 | | ||||
43 | int ColorsModel::rowCount(const QModelIndex &parent) const | ||||
44 | { | ||||
45 | if (parent.isValid()) { | ||||
46 | return 0; | ||||
47 | } | ||||
48 | | ||||
49 | return m_data.count(); | ||||
50 | } | ||||
51 | | ||||
52 | QVariant ColorsModel::data(const QModelIndex &index, int role) const | ||||
53 | { | ||||
54 | if (!index.isValid() || index.row() >= m_data.count()) { | ||||
55 | return QVariant(); | ||||
56 | } | ||||
57 | | ||||
58 | const auto &item = m_data.at(index.row()); | ||||
59 | | ||||
60 | switch (role) { | ||||
61 | case Qt::DisplayRole: return item.display; | ||||
62 | case SchemeNameRole: return item.schemeName; | ||||
63 | case PaletteRole: return item.palette; | ||||
64 | case PendingDeletionRole: return item.pendingDeletion; | ||||
65 | case RemovableRole: return item.removable; | ||||
66 | } | ||||
67 | | ||||
68 | return QVariant(); | ||||
69 | } | ||||
70 | | ||||
71 | bool ColorsModel::setData(const QModelIndex &index, const QVariant &value, int role) | ||||
72 | { | ||||
73 | if (!index.isValid() || index.row() >= m_data.count()) { | ||||
74 | return false; | ||||
75 | } | ||||
76 | | ||||
77 | if (role == PendingDeletionRole) { | ||||
78 | auto &item = m_data[index.row()]; | ||||
79 | | ||||
80 | const bool pendingDeletion = value.toBool(); | ||||
81 | | ||||
82 | if (item.pendingDeletion != pendingDeletion) { | ||||
83 | item.pendingDeletion = pendingDeletion; | ||||
84 | emit dataChanged(index, index, {PendingDeletionRole}); | ||||
85 | | ||||
86 | // move to the next non-pending theme | ||||
87 | const auto nonPending = match(index, PendingDeletionRole, false); | ||||
88 | if (!nonPending.isEmpty()) { | ||||
89 | setSelectedScheme(nonPending.first().data(SchemeNameRole).toString()); | ||||
90 | } | ||||
91 | | ||||
92 | emit pendingDeletionsChanged(); | ||||
93 | return true; | ||||
94 | } | ||||
95 | } | ||||
96 | | ||||
97 | return false; | ||||
98 | } | ||||
99 | | ||||
100 | QHash<int, QByteArray> ColorsModel::roleNames() const | ||||
101 | { | ||||
102 | return { | ||||
103 | {Qt::DisplayRole, QByteArrayLiteral("display")}, | ||||
104 | {SchemeNameRole, QByteArrayLiteral("schemeName")}, | ||||
105 | {PaletteRole, QByteArrayLiteral("palette")}, | ||||
106 | {RemovableRole, QByteArrayLiteral("removable")}, | ||||
107 | {PendingDeletionRole, QByteArrayLiteral("pendingDeletion")} | ||||
108 | }; | ||||
109 | } | ||||
110 | | ||||
111 | QString ColorsModel::selectedScheme() const | ||||
112 | { | ||||
113 | return m_selectedScheme; | ||||
114 | } | ||||
115 | | ||||
116 | void ColorsModel::setSelectedScheme(const QString &scheme) | ||||
117 | { | ||||
118 | if (m_selectedScheme == scheme) { | ||||
119 | return; | ||||
120 | } | ||||
121 | | ||||
122 | const bool firstTime = m_selectedScheme.isNull(); | ||||
123 | m_selectedScheme = scheme; | ||||
124 | | ||||
125 | if (!firstTime) { | ||||
126 | emit selectedSchemeChanged(scheme); | ||||
127 | } | ||||
128 | emit selectedSchemeIndexChanged(); | ||||
129 | } | ||||
130 | | ||||
131 | int ColorsModel::indexOfScheme(const QString &scheme) const | ||||
132 | { | ||||
133 | auto it = std::find_if(m_data.begin(), m_data.end(), [this, &scheme](const ColorsModelData &item) { | ||||
134 | return item.schemeName == scheme; | ||||
135 | }); | ||||
136 | | ||||
137 | if (it != m_data.end()) { | ||||
138 | return std::distance(m_data.begin(), it); | ||||
139 | } | ||||
140 | | ||||
141 | return -1; | ||||
142 | } | ||||
143 | | ||||
144 | int ColorsModel::selectedSchemeIndex() const | ||||
145 | { | ||||
146 | return indexOfScheme(m_selectedScheme); | ||||
147 | } | ||||
148 | | ||||
149 | void ColorsModel::load() | ||||
150 | { | ||||
151 | beginResetModel(); | ||||
152 | | ||||
153 | const int oldCount = m_data.count(); | ||||
154 | | ||||
155 | m_data.clear(); | ||||
156 | | ||||
157 | QStringList schemeFiles; | ||||
158 | | ||||
159 | const QStringList schemeDirs = QStandardPaths::locateAll(QStandardPaths::GenericDataLocation, QStringLiteral("color-schemes"), QStandardPaths::LocateDirectory); | ||||
160 | for (const QString &dir : schemeDirs) { | ||||
161 | const QStringList fileNames = QDir(dir).entryList(QStringList{QStringLiteral("*.colors")}); | ||||
162 | for (const QString &file : fileNames) { | ||||
163 | const QString suffixedFileName = QStringLiteral("color-schemes/") + file; | ||||
164 | // can't use QSet because of the transform below (passing const QString as this argument discards qualifiers) | ||||
165 | if (!schemeFiles.contains(suffixedFileName)) { | ||||
166 | schemeFiles.append(suffixedFileName); | ||||
167 | } | ||||
168 | } | ||||
169 | } | ||||
170 | | ||||
171 | std::transform(schemeFiles.begin(), schemeFiles.end(), schemeFiles.begin(), [](const QString &item) { | ||||
172 | return QStandardPaths::locate(QStandardPaths::GenericDataLocation, item); | ||||
173 | }); | ||||
174 | | ||||
175 | for (const QString &schemeFile : schemeFiles) { | ||||
176 | const QFileInfo fi(schemeFile); | ||||
177 | const QString baseName = fi.baseName(); | ||||
178 | | ||||
179 | KSharedConfigPtr config = KSharedConfig::openConfig(schemeFile, KConfig::SimpleConfig); | ||||
180 | KConfigGroup group(config, "General"); | ||||
181 | const QString name = group.readEntry("Name", baseName); | ||||
182 | | ||||
183 | ColorsModelData item{ | ||||
184 | name, | ||||
185 | baseName, | ||||
186 | KColorScheme::createApplicationPalette(config), | ||||
187 | fi.isWritable(), | ||||
188 | false, // pending deletion | ||||
189 | }; | ||||
190 | | ||||
191 | m_data.append(item); | ||||
192 | } | ||||
193 | | ||||
194 | QCollator collator; | ||||
195 | std::sort(m_data.begin(), m_data.end(), [&collator](const ColorsModelData &a, const ColorsModelData &b) { | ||||
196 | return collator.compare(a.display, b.display) < 0; | ||||
197 | }); | ||||
198 | | ||||
199 | endResetModel(); | ||||
200 | | ||||
201 | // an item might have been added before the currently selected one | ||||
202 | if (oldCount != m_data.count()) { | ||||
203 | emit selectedSchemeIndexChanged(); | ||||
204 | } | ||||
205 | } | ||||
206 | | ||||
207 | QStringList ColorsModel::pendingDeletions() const | ||||
208 | { | ||||
209 | QStringList pendingDeletions; | ||||
210 | | ||||
211 | for (const auto &item : m_data) { | ||||
212 | if (item.pendingDeletion) { | ||||
213 | pendingDeletions.append(item.schemeName); | ||||
214 | } | ||||
215 | } | ||||
216 | | ||||
217 | return pendingDeletions; | ||||
218 | } | ||||
219 | | ||||
220 | void ColorsModel::removeItemsPendingDeletion() | ||||
221 | { | ||||
222 | for (int i = m_data.count() - 1; i >= 0; --i) { | ||||
223 | if (m_data.at(i).pendingDeletion) { | ||||
224 | beginRemoveRows(QModelIndex(), i, i); | ||||
225 | m_data.remove(i); | ||||
226 | endRemoveRows(); | ||||
227 | } | ||||
228 | } | ||||
229 | } |