Changeset View
Changeset View
Standalone View
Standalone View
src/kcolorschememanager.cpp
Show All 29 Lines | |||||
30 | #include <QFileInfo> | 30 | #include <QFileInfo> | ||
31 | #include <QMenu> | 31 | #include <QMenu> | ||
32 | #include <QPainter> | 32 | #include <QPainter> | ||
33 | #include <QStandardPaths> | 33 | #include <QStandardPaths> | ||
34 | #include <QStyle> | 34 | #include <QStyle> | ||
35 | 35 | | |||
36 | constexpr int defaultSchemeRow = 0; | 36 | constexpr int defaultSchemeRow = 0; | ||
37 | 37 | | |||
38 | static void activateScheme(const QString &colorSchemePath) { | ||||
39 | // hint for plasma-integration to synchronize the color scheme with the window manager/compositor | ||||
40 | // The property needs to be set before the palette change because is is checked upon the | ||||
41 | // ApplicationPaletteChange event. | ||||
42 | qApp->setProperty("KDE_COLOR_SCHEME_PATH", colorSchemePath); | ||||
43 | if (colorSchemePath.isEmpty()) { | ||||
44 | qApp->setPalette(qApp->style()->standardPalette()); | ||||
45 | } else { | ||||
46 | qApp->setPalette(KColorScheme::createApplicationPalette(KSharedConfig::openConfig(colorSchemePath))); | ||||
47 | } | ||||
48 | } | ||||
49 | | ||||
50 | static QIcon createPreview(const QString &path) | ||||
51 | { | ||||
52 | KSharedConfigPtr schemeConfig = KSharedConfig::openConfig(path, KConfig::SimpleConfig); | ||||
53 | QIcon result; | ||||
54 | | ||||
55 | KColorScheme activeWindow(QPalette::Active, KColorScheme::Window, schemeConfig); | ||||
56 | KColorScheme activeButton(QPalette::Active, KColorScheme::Button, schemeConfig); | ||||
57 | KColorScheme activeView(QPalette::Active, KColorScheme::View, schemeConfig); | ||||
58 | KColorScheme activeSelection(QPalette::Active, KColorScheme::Selection, schemeConfig); | ||||
59 | | ||||
60 | auto pixmap = [&](int size) { | ||||
61 | QPixmap pix(size, size); | ||||
62 | pix.fill(Qt::black); | ||||
63 | QPainter p; | ||||
64 | p.begin(&pix); | ||||
65 | const int itemSize = size / 2 - 1; | ||||
66 | p.fillRect(1, 1, itemSize, itemSize, activeWindow.background()); | ||||
67 | p.fillRect(1 + itemSize, 1, itemSize, itemSize, activeButton.background()); | ||||
68 | p.fillRect(1, 1 + itemSize, itemSize, itemSize, activeView.background()); | ||||
69 | p.fillRect(1 + itemSize, 1 + itemSize, itemSize, itemSize, activeSelection.background()); | ||||
70 | p.end(); | ||||
71 | result.addPixmap(pix); | ||||
72 | }; | ||||
73 | // 16x16 | ||||
74 | pixmap(16); | ||||
75 | // 24x24 | ||||
76 | pixmap(24); | ||||
77 | | ||||
78 | return result; | ||||
79 | } | ||||
80 | | ||||
38 | KColorSchemeManagerPrivate::KColorSchemeManagerPrivate() | 81 | KColorSchemeManagerPrivate::KColorSchemeManagerPrivate() | ||
39 | : model(new KColorSchemeModel()) | 82 | : model(new KColorSchemeModel()) | ||
40 | { | 83 | { | ||
41 | } | 84 | } | ||
42 | 85 | | |||
43 | KColorSchemeModel::KColorSchemeModel(QObject *parent) | 86 | KColorSchemeModel::KColorSchemeModel(QObject *parent) | ||
44 | : QAbstractListModel(parent) | 87 | : QAbstractListModel(parent) | ||
45 | { | 88 | { | ||
▲ Show 20 Lines • Show All 59 Lines • ▼ Show 20 Line(s) | 128 | { | |||
105 | } | 148 | } | ||
106 | std::sort(m_data.begin(), m_data.end(), [](const KColorSchemeModelData & first, const KColorSchemeModelData & second) { | 149 | std::sort(m_data.begin(), m_data.end(), [](const KColorSchemeModelData & first, const KColorSchemeModelData & second) { | ||
107 | return first.name < second.name; | 150 | return first.name < second.name; | ||
108 | }); | 151 | }); | ||
109 | m_data.insert(defaultSchemeRow, {i18n("Default"), QString(), QIcon::fromTheme("edit-undo")}); | 152 | m_data.insert(defaultSchemeRow, {i18n("Default"), QString(), QIcon::fromTheme("edit-undo")}); | ||
110 | endResetModel(); | 153 | endResetModel(); | ||
111 | } | 154 | } | ||
112 | 155 | | |||
113 | QIcon KColorSchemeModel::createPreview(const QString &path) const | | |||
114 | { | | |||
115 | KSharedConfigPtr schemeConfig = KSharedConfig::openConfig(path, KConfig::SimpleConfig); | | |||
116 | QIcon result; | | |||
117 | | ||||
118 | KColorScheme activeWindow(QPalette::Active, KColorScheme::Window, schemeConfig); | | |||
119 | KColorScheme activeButton(QPalette::Active, KColorScheme::Button, schemeConfig); | | |||
120 | KColorScheme activeView(QPalette::Active, KColorScheme::View, schemeConfig); | | |||
121 | KColorScheme activeSelection(QPalette::Active, KColorScheme::Selection, schemeConfig); | | |||
122 | | ||||
123 | auto pixmap = [&](int size) { | | |||
124 | QPixmap pix(size, size); | | |||
125 | pix.fill(Qt::black); | | |||
126 | QPainter p; | | |||
127 | p.begin(&pix); | | |||
128 | const int itemSize = size / 2 - 1; | | |||
129 | p.fillRect(1, 1, itemSize, itemSize, activeWindow.background()); | | |||
130 | p.fillRect(1 + itemSize, 1, itemSize, itemSize, activeButton.background()); | | |||
131 | p.fillRect(1, 1 + itemSize, itemSize, itemSize, activeView.background()); | | |||
132 | p.fillRect(1 + itemSize, 1 + itemSize, itemSize, itemSize, activeSelection.background()); | | |||
133 | p.end(); | | |||
134 | result.addPixmap(pix); | | |||
135 | }; | | |||
136 | // 16x16 | | |||
137 | pixmap(16); | | |||
138 | // 24x24 | | |||
139 | pixmap(24); | | |||
140 | | ||||
141 | return result; | | |||
142 | } | | |||
143 | | ||||
144 | KColorSchemeManager::KColorSchemeManager(QObject *parent) | 156 | KColorSchemeManager::KColorSchemeManager(QObject *parent) | ||
145 | : QObject(parent) | 157 | : QObject(parent) | ||
146 | , d(new KColorSchemeManagerPrivate) | 158 | , d(new KColorSchemeManagerPrivate) | ||
147 | { | 159 | { | ||
148 | } | 160 | } | ||
149 | 161 | | |||
150 | KColorSchemeManager::~KColorSchemeManager() | 162 | KColorSchemeManager::~KColorSchemeManager() | ||
151 | { | 163 | { | ||
Show All 16 Lines | 179 | if (index.data().toString() == name) { | |||
168 | return index; | 180 | return index; | ||
169 | } | 181 | } | ||
170 | } | 182 | } | ||
171 | return QModelIndex(); | 183 | return QModelIndex(); | ||
172 | } | 184 | } | ||
173 | 185 | | |||
174 | KActionMenu *KColorSchemeManager::createSchemeSelectionMenu(const QIcon &icon, const QString &name, const QString &selectedSchemeName, QObject *parent) | 186 | KActionMenu *KColorSchemeManager::createSchemeSelectionMenu(const QIcon &icon, const QString &name, const QString &selectedSchemeName, QObject *parent) | ||
175 | { | 187 | { | ||
188 | // Be careful here when connecting to signals. The menu can outlive the manager | ||||
176 | KActionMenu *menu = new KActionMenu(icon, name, parent); | 189 | KActionMenu *menu = new KActionMenu(icon, name, parent); | ||
177 | QActionGroup *group = new QActionGroup(menu); | 190 | QActionGroup *group = new QActionGroup(menu); | ||
178 | connect(group, &QActionGroup::triggered, this, [this](QAction * action) { | 191 | connect(group, &QActionGroup::triggered, qApp, [] (QAction * action) { | ||
179 | activateScheme(d->model->index(action->data().toInt())); | 192 | ::activateScheme(action->data().toString()); | ||
anthonyfieroni: You can connect to `qApp` | |||||
What is meant by "connecting to qApp"? Just doing the scheme switching here? I wanted to avoid the duplication that's why I created the static helper method. davidre: What is meant by "connecting to qApp"? Just doing the scheme switching here? I wanted to avoid… | |||||
broulik: Using `qApp` as context object | |||||
davidre: Thanks! Makes sense! | |||||
180 | }); | 193 | }); | ||
181 | for (int i = 0; i < d->model->rowCount(); ++i) { | 194 | for (int i = 0; i < d->model->rowCount(); ++i) { | ||
182 | QModelIndex index = d->model->index(i); | 195 | QModelIndex index = d->model->index(i); | ||
183 | QAction *action = new QAction(index.data(Qt::DisplayRole).toString(), menu); | 196 | QAction *action = new QAction(index.data(Qt::DisplayRole).toString(), menu); | ||
184 | action->setData(index.row()); | 197 | action->setData(index.data(Qt::UserRole)); | ||
185 | action->setActionGroup(group); | 198 | action->setActionGroup(group); | ||
186 | action->setCheckable(true); | 199 | action->setCheckable(true); | ||
187 | if (index.data().toString() == selectedSchemeName) { | 200 | if (index.data().toString() == selectedSchemeName) { | ||
188 | action->setChecked(true); | 201 | action->setChecked(true); | ||
189 | } | 202 | } | ||
190 | menu->addAction(action); | 203 | menu->addAction(action); | ||
191 | } | 204 | } | ||
192 | if (!group->checkedAction()) { | 205 | if (!group->checkedAction()) { | ||
193 | // If no (valid) color scheme has been selected we select the default one | 206 | // If no (valid) color scheme has been selected we select the default one | ||
194 | group->actions()[defaultSchemeRow]->setChecked(true); | 207 | group->actions()[defaultSchemeRow]->setChecked(true); | ||
195 | } | 208 | } | ||
196 | connect(menu->menu(), &QMenu::aboutToShow, group, [this, group] { | 209 | group->actions()[defaultSchemeRow]->setIcon(QIcon::fromTheme("edit-undo")); | ||
210 | connect(menu->menu(), &QMenu::aboutToShow, group, [group] { | ||||
197 | const auto actions = group->actions(); | 211 | const auto actions = group->actions(); | ||
198 | for (QAction *action : actions) { | 212 | for (QAction *action : actions) { | ||
199 | if (action->icon().isNull()) { | 213 | if (action->icon().isNull()) { | ||
200 | action->setIcon(d->model->index(action->data().toInt()).data(Qt::DecorationRole).value<QIcon>()); | 214 | action->setIcon(createPreview(action->data().toString())); | ||
201 | } | 215 | } | ||
202 | } | 216 | } | ||
203 | }); | 217 | }); | ||
204 | 218 | | |||
205 | return menu; | 219 | return menu; | ||
206 | } | 220 | } | ||
207 | 221 | | |||
208 | KActionMenu *KColorSchemeManager::createSchemeSelectionMenu(const QString &text, const QString &selectedSchemeName, QObject *parent) | 222 | KActionMenu *KColorSchemeManager::createSchemeSelectionMenu(const QString &text, const QString &selectedSchemeName, QObject *parent) | ||
209 | { | 223 | { | ||
210 | return createSchemeSelectionMenu(QIcon::fromTheme("preferences-desktop-color"), text, selectedSchemeName, parent); | 224 | return createSchemeSelectionMenu(QIcon::fromTheme("preferences-desktop-color"), text, selectedSchemeName, parent); | ||
211 | } | 225 | } | ||
212 | 226 | | |||
213 | KActionMenu *KColorSchemeManager::createSchemeSelectionMenu(const QString &selectedSchemeName, QObject *parent) | 227 | KActionMenu *KColorSchemeManager::createSchemeSelectionMenu(const QString &selectedSchemeName, QObject *parent) | ||
214 | { | 228 | { | ||
215 | return createSchemeSelectionMenu(QIcon::fromTheme("preferences-desktop-color"), i18n("Color Scheme"), selectedSchemeName, parent); | 229 | return createSchemeSelectionMenu(QIcon::fromTheme("preferences-desktop-color"), i18n("Color Scheme"), selectedSchemeName, parent); | ||
216 | } | 230 | } | ||
217 | 231 | | |||
218 | KActionMenu *KColorSchemeManager::createSchemeSelectionMenu (QObject *parent) | 232 | KActionMenu *KColorSchemeManager::createSchemeSelectionMenu (QObject *parent) | ||
219 | { | 233 | { | ||
220 | return createSchemeSelectionMenu(QIcon::fromTheme("preferences-desktop-color"), i18n("Color Scheme"), QString(), parent); | 234 | return createSchemeSelectionMenu(QIcon::fromTheme("preferences-desktop-color"), i18n("Color Scheme"), QString(), parent); | ||
221 | } | 235 | } | ||
222 | 236 | | |||
223 | void KColorSchemeManager::activateScheme(const QModelIndex &index) | 237 | void KColorSchemeManager::activateScheme(const QModelIndex &index) | ||
224 | { | 238 | { | ||
225 | // hint for plasma-integration to synchronize the color scheme with the window manager/compositor | 239 | if (index.isValid() && index.model() == d->model.data()) { | ||
226 | // The property needs to be set before the palette change because is is checked upon the | 240 | ::activateScheme(index.data(Qt::UserRole).toString()); | ||
227 | // ApplicationPaletteChange event. | | |||
228 | qApp->setProperty("KDE_COLOR_SCHEME_PATH", index.data(Qt::UserRole)); | | |||
229 | if (index.isValid() && index.model() == d->model.data() && index.row() != defaultSchemeRow) { | | |||
230 | qApp->setPalette(KColorScheme::createApplicationPalette(KSharedConfig::openConfig(index.data(Qt::UserRole).toString()))); | | |||
231 | } else { | 241 | } else { | ||
232 | qApp->setPalette(qApp->style()->standardPalette()); | 242 | ::activateScheme(QString()); | ||
233 | } | 243 | } | ||
234 | } | 244 | } |
You can connect to qApp