Changeset View
Changeset View
Standalone View
Standalone View
kcms/desktoptheme/kcm.cpp
Show First 20 Lines • Show All 50 Lines • ▼ Show 20 Line(s) | |||||
51 | 51 | | |||
52 | KCMDesktopTheme::KCMDesktopTheme(QObject *parent, const QVariantList &args) | 52 | KCMDesktopTheme::KCMDesktopTheme(QObject *parent, const QVariantList &args) | ||
53 | : KQuickAddons::ConfigModule(parent, args) | 53 | : KQuickAddons::ConfigModule(parent, args) | ||
54 | , m_settings(new DesktopThemeSettings) | 54 | , m_settings(new DesktopThemeSettings) | ||
55 | , m_haveThemeExplorerInstalled(false) | 55 | , m_haveThemeExplorerInstalled(false) | ||
56 | { | 56 | { | ||
57 | // Unfortunately doesn't generate a ctor taking the parent as parameter | 57 | // Unfortunately doesn't generate a ctor taking the parent as parameter | ||
58 | m_settings->setParent(this); | 58 | m_settings->setParent(this); | ||
59 | m_currentTheme = m_settings->name(); | ||||
60 | connect(m_settings, &DesktopThemeSettings::configChanged, | ||||
61 | this, &KCMDesktopTheme::updateNeedsSave); | ||||
62 | connect(m_settings, &DesktopThemeSettings::nameChanged, | ||||
63 | this, &KCMDesktopTheme::updateNeedsSave); | ||||
64 | | ||||
65 | qmlRegisterType<DesktopThemeSettings>(); | ||||
59 | qmlRegisterType<QStandardItemModel>(); | 66 | qmlRegisterType<QStandardItemModel>(); | ||
60 | 67 | | |||
61 | KAboutData* about = new KAboutData(QStringLiteral("kcm_desktoptheme"), i18n("Plasma Style"), | 68 | KAboutData* about = new KAboutData(QStringLiteral("kcm_desktoptheme"), i18n("Plasma Style"), | ||
62 | QStringLiteral("0.1"), QString(), KAboutLicense::LGPL); | 69 | QStringLiteral("0.1"), QString(), KAboutLicense::LGPL); | ||
63 | about->addAuthor(i18n("David Rosca"), QString(), QStringLiteral("nowrep@gmail.com")); | 70 | about->addAuthor(i18n("David Rosca"), QString(), QStringLiteral("nowrep@gmail.com")); | ||
64 | setAboutData(about); | 71 | setAboutData(about); | ||
65 | setButtons(Apply | Default | Help); | 72 | setButtons(Apply | Default | Help); | ||
66 | 73 | | |||
67 | m_model = new QStandardItemModel(this); | 74 | m_model = new QStandardItemModel(this); | ||
68 | QHash<int, QByteArray> roles = m_model->roleNames(); | 75 | QHash<int, QByteArray> roles = m_model->roleNames(); | ||
69 | roles[PluginNameRole] = QByteArrayLiteral("pluginName"); | 76 | roles[PluginNameRole] = QByteArrayLiteral("pluginName"); | ||
70 | roles[ThemeNameRole] = QByteArrayLiteral("themeName"); | 77 | roles[ThemeNameRole] = QByteArrayLiteral("themeName"); | ||
71 | roles[DescriptionRole] = QByteArrayLiteral("description"); | 78 | roles[DescriptionRole] = QByteArrayLiteral("description"); | ||
72 | roles[IsLocalRole] = QByteArrayLiteral("isLocal"); | 79 | roles[IsLocalRole] = QByteArrayLiteral("isLocal"); | ||
73 | roles[PendingDeletionRole] = QByteArrayLiteral("pendingDeletion"); | 80 | roles[PendingDeletionRole] = QByteArrayLiteral("pendingDeletion"); | ||
74 | m_model->setItemRoleNames(roles); | 81 | m_model->setItemRoleNames(roles); | ||
75 | 82 | | |||
76 | m_haveThemeExplorerInstalled = !QStandardPaths::findExecutable(QStringLiteral("plasmathemeexplorer")).isEmpty(); | 83 | m_haveThemeExplorerInstalled = !QStandardPaths::findExecutable(QStringLiteral("plasmathemeexplorer")).isEmpty(); | ||
77 | } | 84 | } | ||
78 | 85 | | |||
79 | KCMDesktopTheme::~KCMDesktopTheme() | 86 | KCMDesktopTheme::~KCMDesktopTheme() | ||
80 | { | 87 | { | ||
81 | } | 88 | } | ||
82 | 89 | | |||
83 | QStandardItemModel *KCMDesktopTheme::desktopThemeModel() const | 90 | DesktopThemeSettings *KCMDesktopTheme::desktopThemeSettings() const | ||
84 | { | 91 | { | ||
85 | return m_model; | 92 | return m_settings; | ||
86 | } | 93 | } | ||
87 | 94 | | |||
88 | QString KCMDesktopTheme::selectedPlugin() const | 95 | QStandardItemModel *KCMDesktopTheme::desktopThemeModel() const | ||
89 | { | | |||
90 | return m_selectedPlugin; | | |||
91 | } | | |||
92 | | ||||
93 | void KCMDesktopTheme::setSelectedPlugin(const QString &plugin) | | |||
94 | { | 96 | { | ||
95 | if (m_selectedPlugin == plugin) { | 97 | return m_model; | ||
96 | return; | | |||
97 | } | | |||
98 | m_selectedPlugin = plugin; | | |||
99 | emit selectedPluginChanged(m_selectedPlugin); | | |||
100 | emit selectedPluginIndexChanged(); | | |||
101 | updateNeedsSave(); | | |||
102 | } | 98 | } | ||
103 | 99 | | |||
104 | int KCMDesktopTheme::selectedPluginIndex() const | 100 | int KCMDesktopTheme::pluginIndex(const QString &pluginName) const | ||
105 | { | 101 | { | ||
106 | const auto results = m_model->match(m_model->index(0, 0), PluginNameRole, m_selectedPlugin); | 102 | const auto results = m_model->match(m_model->index(0, 0), PluginNameRole, pluginName); | ||
107 | if (results.count() == 1) { | 103 | if (results.count() == 1) { | ||
108 | return results.first().row(); | 104 | return results.first().row(); | ||
109 | } | 105 | } | ||
110 | 106 | | |||
111 | return -1; | 107 | return -1; | ||
112 | } | 108 | } | ||
113 | 109 | | |||
114 | bool KCMDesktopTheme::downloadingFile() const | 110 | bool KCMDesktopTheme::downloadingFile() const | ||
115 | { | 111 | { | ||
116 | return m_tempCopyJob; | 112 | return m_tempCopyJob; | ||
117 | } | 113 | } | ||
118 | 114 | | |||
119 | void KCMDesktopTheme::setPendingDeletion(int index, bool pending) | 115 | void KCMDesktopTheme::setPendingDeletion(int index, bool pending) | ||
120 | { | 116 | { | ||
121 | QModelIndex idx = m_model->index(index, 0); | 117 | QModelIndex idx = m_model->index(index, 0); | ||
122 | 118 | | |||
123 | m_model->setData(idx, pending, PendingDeletionRole); | 119 | m_model->setData(idx, pending, PendingDeletionRole); | ||
124 | 120 | | |||
125 | if (pending && selectedPluginIndex() == index) { | 121 | if (pending && pluginIndex(m_settings->name()) == index) { | ||
126 | // move to the next non-pending theme | 122 | // move to the next non-pending theme | ||
127 | const auto nonPending = m_model->match(idx, PendingDeletionRole, false); | 123 | const auto nonPending = m_model->match(idx, PendingDeletionRole, false); | ||
128 | setSelectedPlugin(nonPending.first().data(PluginNameRole).toString()); | 124 | m_settings->setName(nonPending.first().data(PluginNameRole).toString()); | ||
129 | } | 125 | } | ||
130 | | ||||
131 | updateNeedsSave(); | | |||
132 | } | 126 | } | ||
133 | 127 | | |||
134 | void KCMDesktopTheme::getNewStuff(QQuickItem *ctx) | 128 | void KCMDesktopTheme::getNewStuff(QQuickItem *ctx) | ||
135 | { | 129 | { | ||
136 | if (!m_newStuffDialog) { | 130 | if (!m_newStuffDialog) { | ||
137 | m_newStuffDialog = new KNS3::DownloadDialog(QStringLiteral("plasma-themes.knsrc")); | 131 | m_newStuffDialog = new KNS3::DownloadDialog(QStringLiteral("plasma-themes.knsrc")); | ||
138 | m_newStuffDialog.data()->setWindowTitle(i18n("Download New Plasma Styles")); | 132 | m_newStuffDialog.data()->setWindowTitle(i18n("Download New Plasma Styles")); | ||
139 | m_newStuffDialog->setWindowModality(Qt::WindowModal); | 133 | m_newStuffDialog->setWindowModality(Qt::WindowModal); | ||
▲ Show 20 Lines • Show All 139 Lines • ▼ Show 20 Line(s) | 266 | if (m_model->findItems(packageName).isEmpty()) { | |||
279 | item->setData(false, PendingDeletionRole); | 273 | item->setData(false, PendingDeletionRole); | ||
280 | m_model->appendRow(item); | 274 | m_model->appendRow(item); | ||
281 | } | 275 | } | ||
282 | } | 276 | } | ||
283 | 277 | | |||
284 | m_model->setSortRole(ThemeNameRole); // FIXME the model should really be just using Qt::DisplayRole | 278 | m_model->setSortRole(ThemeNameRole); // FIXME the model should really be just using Qt::DisplayRole | ||
285 | m_model->sort(0 /*column*/); | 279 | m_model->sort(0 /*column*/); | ||
286 | 280 | | |||
287 | setSelectedPlugin(m_settings->name()); | 281 | m_settings->load(); | ||
282 | m_currentTheme = m_settings->name(); | ||||
288 | 283 | | |||
289 | emit selectedPluginIndexChanged(); | 284 | // Model has been cleared so pretend the theme name changed to force view update | ||
290 | 285 | emit m_settings->nameChanged(); | |||
291 | updateNeedsSave(); | | |||
292 | } | 286 | } | ||
293 | 287 | | |||
294 | void KCMDesktopTheme::save() | 288 | void KCMDesktopTheme::save() | ||
295 | { | 289 | { | ||
296 | if (m_settings->name() != m_selectedPlugin) { | | |||
297 | m_settings->setName(m_selectedPlugin); | | |||
298 | m_settings->save(); | 290 | m_settings->save(); | ||
291 | m_currentTheme = m_settings->name(); | ||||
299 | Plasma::Theme().setThemeName(m_settings->name()); | 292 | Plasma::Theme().setThemeName(m_settings->name()); | ||
300 | } | | |||
301 | | ||||
302 | processPendingDeletions(); | 293 | processPendingDeletions(); | ||
303 | updateNeedsSave(); | 294 | updateNeedsSave(); | ||
304 | } | 295 | } | ||
305 | 296 | | |||
306 | void KCMDesktopTheme::defaults() | 297 | void KCMDesktopTheme::defaults() | ||
307 | { | 298 | { | ||
308 | setSelectedPlugin(m_settings->defaultNameValue()); | 299 | m_settings->setDefaults(); | ||
300 | m_currentTheme = m_settings->name(); | ||||
309 | 301 | | |||
310 | // can this be done more elegantly? | 302 | // can this be done more elegantly? | ||
311 | const auto pendingDeletions = m_model->match(m_model->index(0, 0), PendingDeletionRole, true); | 303 | const auto pendingDeletions = m_model->match(m_model->index(0, 0), PendingDeletionRole, true); | ||
312 | for (const QModelIndex &idx : pendingDeletions) { | 304 | for (const QModelIndex &idx : pendingDeletions) { | ||
313 | m_model->setData(idx, false, PendingDeletionRole); | 305 | m_model->setData(idx, false, PendingDeletionRole); | ||
314 | } | 306 | } | ||
315 | } | 307 | } | ||
316 | 308 | | |||
317 | bool KCMDesktopTheme::canEditThemes() const | 309 | bool KCMDesktopTheme::canEditThemes() const | ||
318 | { | 310 | { | ||
319 | return m_haveThemeExplorerInstalled; | 311 | return m_haveThemeExplorerInstalled; | ||
320 | } | 312 | } | ||
321 | 313 | | |||
322 | void KCMDesktopTheme::editTheme(const QString &theme) | 314 | void KCMDesktopTheme::editTheme(const QString &theme) | ||
323 | { | 315 | { | ||
324 | QProcess::startDetached(QStringLiteral("plasmathemeexplorer -t ") % theme); | 316 | QProcess::startDetached(QStringLiteral("plasmathemeexplorer -t ") % theme); | ||
325 | } | 317 | } | ||
326 | 318 | | |||
327 | void KCMDesktopTheme::updateNeedsSave() | 319 | void KCMDesktopTheme::updateNeedsSave() | ||
328 | { | 320 | { | ||
329 | setNeedsSave(!m_model->match(m_model->index(0, 0), PendingDeletionRole, true).isEmpty() | 321 | setNeedsSave(!m_model->match(m_model->index(0, 0), PendingDeletionRole, true).isEmpty() | ||
330 | || m_selectedPlugin != m_settings->name()); | 322 | || m_currentTheme != m_settings->name()); | ||
331 | } | 323 | } | ||
332 | 324 | | |||
333 | void KCMDesktopTheme::processPendingDeletions() | 325 | void KCMDesktopTheme::processPendingDeletions() | ||
334 | { | 326 | { | ||
335 | const QString program = QStringLiteral("plasmapkg2"); | 327 | const QString program = QStringLiteral("plasmapkg2"); | ||
336 | 328 | | |||
337 | const auto pendingDeletions = m_model->match(m_model->index(0, 0), PendingDeletionRole, true, -1 /*all*/); | 329 | const auto pendingDeletions = m_model->match(m_model->index(0, 0), PendingDeletionRole, true, -1 /*all*/); | ||
338 | QVector<QPersistentModelIndex> persistentPendingDeletions; | 330 | QVector<QPersistentModelIndex> persistentPendingDeletions; | ||
339 | // turn into persistent model index so we can delete as we go | 331 | // turn into persistent model index so we can delete as we go | ||
340 | std::transform(pendingDeletions.begin(), pendingDeletions.end(), | 332 | std::transform(pendingDeletions.begin(), pendingDeletions.end(), | ||
341 | std::back_inserter(persistentPendingDeletions), [](const QModelIndex &idx) { | 333 | std::back_inserter(persistentPendingDeletions), [](const QModelIndex &idx) { | ||
342 | return QPersistentModelIndex(idx); | 334 | return QPersistentModelIndex(idx); | ||
343 | }); | 335 | }); | ||
344 | 336 | | |||
345 | for (const QPersistentModelIndex &idx : persistentPendingDeletions) { | 337 | for (const QPersistentModelIndex &idx : persistentPendingDeletions) { | ||
346 | const QString pluginName = idx.data(PluginNameRole).toString(); | 338 | const QString pluginName = idx.data(PluginNameRole).toString(); | ||
347 | const QString displayName = idx.data(Qt::DisplayRole).toString(); | 339 | const QString displayName = idx.data(Qt::DisplayRole).toString(); | ||
348 | 340 | | |||
349 | Q_ASSERT(pluginName != m_selectedPlugin); | 341 | Q_ASSERT(pluginName != m_settings->name()); | ||
350 | 342 | | |||
351 | const QStringList arguments = {QStringLiteral("-t"), QStringLiteral("theme"), QStringLiteral("-r"), pluginName}; | 343 | const QStringList arguments = {QStringLiteral("-t"), QStringLiteral("theme"), QStringLiteral("-r"), pluginName}; | ||
352 | 344 | | |||
353 | QProcess *process = new QProcess(this); | 345 | QProcess *process = new QProcess(this); | ||
354 | connect(process, static_cast<void (QProcess::*)(int, QProcess::ExitStatus)>(&QProcess::finished), this, | 346 | connect(process, static_cast<void (QProcess::*)(int, QProcess::ExitStatus)>(&QProcess::finished), this, | ||
355 | [this, process, idx, pluginName, displayName](int exitCode, QProcess::ExitStatus exitStatus) { | 347 | [this, process, idx, pluginName, displayName](int exitCode, QProcess::ExitStatus exitStatus) { | ||
356 | Q_UNUSED(exitStatus) | 348 | Q_UNUSED(exitStatus) | ||
357 | if (exitCode == 0) { | 349 | if (exitCode == 0) { | ||
Show All 15 Lines |