diff --git a/src/kcolorschememanager.cpp b/src/kcolorschememanager.cpp --- a/src/kcolorschememanager.cpp +++ b/src/kcolorschememanager.cpp @@ -27,6 +27,7 @@ #include #include #include +#include #include #include @@ -62,8 +63,13 @@ switch (role) { case Qt::DisplayRole: return m_data.at(index.row()).name; - case Qt::DecorationRole: - return m_data.at(index.row()).preview; + case Qt::DecorationRole: { + auto &item = m_data[index.row()]; + if (item.preview.isNull()) { + item.preview = createPreview(item.path); + } + return item.preview; + } case Qt::UserRole: return m_data.at(index.row()).path; default: @@ -87,22 +93,23 @@ } } for (const QString &schemeFile : qAsConst(schemeFiles)) { - KSharedConfigPtr config = KSharedConfig::openConfig(schemeFile); + KSharedConfigPtr config = KSharedConfig::openConfig(schemeFile, KConfig::SimpleConfig); KConfigGroup group(config, QStringLiteral("General")); const QString name = group.readEntry("Name", QFileInfo(schemeFile).baseName()); - const KColorSchemeModelData data = {name, schemeFile, createPreview(schemeFile)}; + const KColorSchemeModelData data = {name, schemeFile, QIcon()}; m_data.append(data); } std::sort(m_data.begin(), m_data.end(), [](const KColorSchemeModelData & first, const KColorSchemeModelData & second) { return first.name < second.name; }); endResetModel(); } -QIcon KColorSchemeModel::createPreview(const QString &path) +QIcon KColorSchemeModel::createPreview(const QString &path) const { - KSharedConfigPtr schemeConfig = KSharedConfig::openConfig(path); + KSharedConfigPtr schemeConfig = KSharedConfig::openConfig(path, KConfig::SimpleConfig); QIcon result; + KColorScheme activeWindow(QPalette::Active, KColorScheme::Window, schemeConfig); KColorScheme activeButton(QPalette::Active, KColorScheme::Button, schemeConfig); KColorScheme activeView(QPalette::Active, KColorScheme::View, schemeConfig); @@ -166,15 +173,25 @@ }); for (int i = 0; i < d->model->rowCount(); ++i) { QModelIndex index = d->model->index(i); - QAction *action = new QAction(index.data(Qt::DecorationRole).value(), index.data().toString(), menu); + QAction *action = new QAction(index.data(Qt::DisplayRole).toString(), menu); action->setData(index.data(Qt::UserRole)); action->setActionGroup(group); action->setCheckable(true); if (index.data().toString() == selectedSchemeName) { action->setChecked(true); } menu->addAction(action); } + + 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())); + } + } + }); + return menu; } diff --git a/src/kcolorschememanager_p.h b/src/kcolorschememanager_p.h --- a/src/kcolorschememanager_p.h +++ b/src/kcolorschememanager_p.h @@ -37,10 +37,11 @@ QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override; int rowCount(const QModelIndex &parent = QModelIndex()) const override; + QIcon createPreview(const QString &path) const; + private: void init(); - QIcon createPreview(const QString &path); - QVector m_data; + mutable QVector m_data; }; class KColorSchemeManagerPrivate