diff --git a/src/kcolorschememanager.h b/src/kcolorschememanager.h --- a/src/kcolorschememanager.h +++ b/src/kcolorschememanager.h @@ -36,7 +36,8 @@ * A small helper to get access to all available color schemes and activating a scheme in the * QApplication. This is useful for applications which want to provide a selection of custom color * schemes to their user. For example it is very common for photo and painting applications to use - * a dark color scheme even if the default is a light scheme. + * a dark color scheme even if the default is a light scheme. It also allows going back to following + * the system color scheme. * * The KColorSchemeManager provides access to a QAbstractItemModel which holds all the available * schemes. A possible usage looks like the following: @@ -72,14 +73,16 @@ * A QAbstractItemModel of all available color schemes. * * The model provides the name of the scheme in Qt::DisplayRole, a preview - * in Qt::DelegateRole and the full path to the scheme file in Qt::UserRole. + * in Qt::DelegateRole and the full path to the scheme file in Qt::UserRole. The system theme + * has an empty Qt::UserRole. * * @return Model of all available color schemes. */ QAbstractItemModel *model() const; /** * Returns the model index for the scheme with the given @p name. If no such - * scheme exists an invalid index is returned. + * scheme exists an invalid index is returned. If you pass an empty + * string the index that is equivalent to going back to following the system scheme is returned. * @see model */ QModelIndex indexForScheme(const QString &name) const; @@ -90,7 +93,7 @@ * referenced by this action is activated. * * The color scheme with the same name as @p selectedSchemeName will be checked. If none - * of the available color schemes has the same name, no action will be checked. + * of the available color schemes has the same name, the system theme entry will be checked. * * The KActionMenu will not be updated in case the installed color schemes change. It's the * task of the user of the KActionMenu to monitor for changes if required. @@ -105,6 +108,7 @@ KActionMenu *createSchemeSelectionMenu(const QIcon &icon, const QString &text, const QString &selectedSchemeName, QObject *parent); KActionMenu *createSchemeSelectionMenu(const QString &text, const QString &selectedSchemeName, QObject *parent); KActionMenu *createSchemeSelectionMenu(const QString &selectedSchemeName, QObject *parent); + KActionMenu *createSchemeSelectionMenu(QObject *parent); public Q_SLOTS: /** @@ -118,6 +122,12 @@ */ void activateScheme(const QModelIndex &index); + /** + * Convienience function to go back to following the system color scheme. + * @since 5.66 + */ + void followSystemScheme(); + private: QScopedPointer d; }; diff --git a/src/kcolorschememanager.cpp b/src/kcolorschememanager.cpp --- a/src/kcolorschememanager.cpp +++ b/src/kcolorschememanager.cpp @@ -22,14 +22,16 @@ #include #include #include +#include #include #include #include #include #include #include #include +#include KColorSchemeManagerPrivate::KColorSchemeManagerPrivate() : model(new KColorSchemeModel()) @@ -102,6 +104,7 @@ std::sort(m_data.begin(), m_data.end(), [](const KColorSchemeModelData & first, const KColorSchemeModelData & second) { return first.name < second.name; }); + m_data.prepend({i18n("System color scheme"), QString(), QIcon::fromTheme("edit-undo")}); endResetModel(); } @@ -153,6 +156,9 @@ QModelIndex KColorSchemeManager::indexForScheme(const QString &name) const { + if (name.isEmpty()) { + return d->model->index(0); + } for (int i = 0; i < d->model->rowCount(); ++i) { QModelIndex index = d->model->index(i); if (index.data().toString() == name) { @@ -166,28 +172,29 @@ { KActionMenu *menu = new KActionMenu(icon, name, parent); QActionGroup *group = new QActionGroup(menu); - connect(group, &QActionGroup::triggered, [](QAction * action) { + connect(group, &QActionGroup::triggered, this, [this](QAction * action) { // hint for the style to synchronize the color scheme with the window manager/compositor - qApp->setProperty("KDE_COLOR_SCHEME_PATH", action->data()); - qApp->setPalette(KColorScheme::createApplicationPalette(KSharedConfig::openConfig(action->data().toString()))); + activateScheme(d->model->index(action->data().toInt())); }); for (int i = 0; i < d->model->rowCount(); ++i) { QModelIndex index = d->model->index(i); QAction *action = new QAction(index.data(Qt::DisplayRole).toString(), menu); - action->setData(index.data(Qt::UserRole)); + action->setData(index.row()); action->setActionGroup(group); action->setCheckable(true); if (index.data().toString() == selectedSchemeName) { action->setChecked(true); } menu->addAction(action); } - + if (!group->checkedAction()) { + group->actions()[0]->setChecked(true); + } connect(menu->menu(), &QMenu::aboutToShow, group, [this, group] { const auto actions = group->actions(); for (QAction *action : actions) { if (action->icon().isNull()) { - action->setIcon(d->model->createPreview(action->data().toString())); + action->setIcon(d->model->index(action->data().toInt()).data(Qt::DecorationRole).value()); } } }); @@ -205,6 +212,11 @@ return createSchemeSelectionMenu(QIcon(), QString(), selectedSchemeName, parent); } +KActionMenu *KColorSchemeManager::createSchemeSelectionMenu (QObject *parent) +{ + return createSchemeSelectionMenu(QIcon(),QString(), QString(), parent); +} + void KColorSchemeManager::activateScheme(const QModelIndex &index) { if (!index.isValid()) { @@ -215,5 +227,14 @@ } // hint for the style to synchronize the color scheme with the window manager/compositor qApp->setProperty("KDE_COLOR_SCHEME_PATH", index.data(Qt::UserRole)); - qApp->setPalette(KColorScheme::createApplicationPalette(KSharedConfig::openConfig(index.data(Qt::UserRole).toString()))); + if (index.data(Qt::UserRole).toString().isNull()) { + qApp->setPalette(qApp->style()->standardPalette()); + } else { + qApp->setPalette(KColorScheme::createApplicationPalette(KSharedConfig::openConfig(index.data(Qt::UserRole).toString()))); + } +} + +void KColorSchemeManager::followSystemScheme() +{ + activateScheme(d->model->index(0)); }