diff --git a/kcmkwin/kwincompositing/model.cpp b/kcmkwin/kwincompositing/model.cpp index 2152dd252..2a3e58701 100644 --- a/kcmkwin/kwincompositing/model.cpp +++ b/kcmkwin/kwincompositing/model.cpp @@ -1,658 +1,659 @@ /************************************************************************** * KWin - the KDE window manager * * This file is part of the KDE project. * * * * Copyright (C) 2013 Antonis Tsiapaliokas * * * * This program is free software; you can redistribute it and/or modify * * it under the terms of the GNU General Public License as published by * * the Free Software Foundation; either version 2 of the License, or * * (at your option) any later version. * * * * This program is distributed in the hope that it will be useful, * * but WITHOUT ANY WARRANTY; without even the implied warranty of * * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * * GNU General Public License for more details. * * * * You should have received a copy of the GNU General Public License * * along with this program. If not, see . * **************************************************************************/ #include "model.h" #include "effectconfig.h" #include "compositing.h" #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include namespace KWin { namespace Compositing { static QString translatedCategory(const QString &category) { static const QVector knownCategories = { QStringLiteral("Accessibility"), QStringLiteral("Appearance"), QStringLiteral("Candy"), QStringLiteral("Focus"), QStringLiteral("Tools"), QStringLiteral("Virtual Desktop Switching Animation"), QStringLiteral("Window Management") }; static const QVector translatedCategories = { i18nc("Category of Desktop Effects, used as section header", "Accessibility"), i18nc("Category of Desktop Effects, used as section header", "Appearance"), i18nc("Category of Desktop Effects, used as section header", "Candy"), i18nc("Category of Desktop Effects, used as section header", "Focus"), i18nc("Category of Desktop Effects, used as section header", "Tools"), i18nc("Category of Desktop Effects, used as section header", "Virtual Desktop Switching Animation"), i18nc("Category of Desktop Effects, used as section header", "Window Management") }; const int index = knownCategories.indexOf(category); if (index == -1) { qDebug() << "Unknown category '" << category << "' and thus not translated"; return category; } return translatedCategories[index]; } static EffectStatus effectStatus(bool enabled) { return enabled ? EffectStatus::Enabled : EffectStatus::Disabled; } EffectModel::EffectModel(QObject *parent) : QAbstractItemModel(parent) { } QHash< int, QByteArray > EffectModel::roleNames() const { QHash roleNames; roleNames[NameRole] = "NameRole"; roleNames[DescriptionRole] = "DescriptionRole"; roleNames[AuthorNameRole] = "AuthorNameRole"; roleNames[AuthorEmailRole] = "AuthorEmailRole"; roleNames[LicenseRole] = "LicenseRole"; roleNames[VersionRole] = "VersionRole"; roleNames[CategoryRole] = "CategoryRole"; roleNames[ServiceNameRole] = "ServiceNameRole"; roleNames[EffectStatusRole] = "EffectStatusRole"; roleNames[VideoRole] = "VideoRole"; roleNames[SupportedRole] = "SupportedRole"; roleNames[ExclusiveRole] = "ExclusiveRole"; roleNames[ConfigurableRole] = "ConfigurableRole"; roleNames[ScriptedRole] = QByteArrayLiteral("ScriptedRole"); return roleNames; } QModelIndex EffectModel::index(int row, int column, const QModelIndex &parent) const { if (parent.isValid() || column > 0 || column < 0 || row < 0 || row >= m_effectsList.count()) { return QModelIndex(); } return createIndex(row, column); } QModelIndex EffectModel::parent(const QModelIndex &child) const { Q_UNUSED(child) return QModelIndex(); } int EffectModel::columnCount(const QModelIndex &parent) const { Q_UNUSED(parent) return 1; } int EffectModel::rowCount(const QModelIndex &parent) const { if (parent.isValid()) { return 0; } return m_effectsList.count(); } QVariant EffectModel::data(const QModelIndex &index, int role) const { if (!index.isValid()) { return QVariant(); } EffectData currentEffect = m_effectsList.at(index.row()); switch (role) { case Qt::DisplayRole: case NameRole: return m_effectsList.at(index.row()).name; case DescriptionRole: return m_effectsList.at(index.row()).description; case AuthorNameRole: return m_effectsList.at(index.row()).authorName; case AuthorEmailRole: return m_effectsList.at(index.row()).authorEmail; case LicenseRole: return m_effectsList.at(index.row()).license; case VersionRole: return m_effectsList.at(index.row()).version; case CategoryRole: return m_effectsList.at(index.row()).category; case ServiceNameRole: return m_effectsList.at(index.row()).serviceName; case EffectStatusRole: return (int)m_effectsList.at(index.row()).effectStatus; case VideoRole: return m_effectsList.at(index.row()).video; case SupportedRole: return m_effectsList.at(index.row()).supported; case ExclusiveRole: return m_effectsList.at(index.row()).exclusiveGroup; case InternalRole: return m_effectsList.at(index.row()).internal; case ConfigurableRole: return m_effectsList.at(index.row()).configurable; case ScriptedRole: return m_effectsList.at(index.row()).scripted; default: return QVariant(); } } bool EffectModel::setData(const QModelIndex& index, const QVariant& value, int role) { if (!index.isValid()) return QAbstractItemModel::setData(index, value, role); if (role == EffectModel::EffectStatusRole) { // note: whenever the StatusRole is modified (even to the same value) the entry // gets marked as changed and will get saved to the config file. This means the // config file could get polluted EffectData &data = m_effectsList[index.row()]; data.effectStatus = EffectStatus(value.toInt()); data.changed = true; emit dataChanged(index, index); if (data.effectStatus == EffectStatus::Enabled && !data.exclusiveGroup.isEmpty()) { // need to disable all other exclusive effects in the same category for (int i = 0; i < m_effectsList.size(); ++i) { if (i == index.row()) { continue; } EffectData &otherData = m_effectsList[i]; if (otherData.exclusiveGroup == data.exclusiveGroup) { otherData.effectStatus = EffectStatus::Disabled; otherData.changed = true; emit dataChanged(this->index(i, 0), this->index(i, 0)); } } } return true; } return QAbstractItemModel::setData(index, value, role); } void EffectModel::loadBuiltInEffects(const KConfigGroup &kwinConfig, const KPluginInfo::List &configs) { const auto builtins = BuiltInEffects::availableEffects(); for (auto builtin : builtins) { const BuiltInEffects::EffectData &data = BuiltInEffects::effectData(builtin); EffectData effect; effect.name = data.displayName; effect.description = data.comment; effect.authorName = i18n("KWin development team"); effect.authorEmail = QString(); // not used at all effect.license = QStringLiteral("GPL"); effect.version = QStringLiteral(KWIN_VERSION_STRING); effect.category = translatedCategory(data.category); effect.serviceName = data.name; effect.enabledByDefault = data.enabled; effect.enabledByDefaultFunction = (data.enabledFunction != nullptr); const QString enabledKey = QStringLiteral("%1Enabled").arg(effect.serviceName); if (kwinConfig.hasKey(enabledKey)) { effect.effectStatus = effectStatus(kwinConfig.readEntry(effect.serviceName + "Enabled", effect.enabledByDefault)); } else if (data.enabledFunction != nullptr) { effect.effectStatus = EffectStatus::EnabledUndeterminded; } else { effect.effectStatus = effectStatus(effect.enabledByDefault); } effect.video = data.video; effect.supported = true; effect.exclusiveGroup = data.exclusiveCategory; effect.internal = data.internal; effect.scripted = false; auto it = std::find_if(configs.begin(), configs.end(), [data](const KPluginInfo &info) { return info.property(QStringLiteral("X-KDE-ParentComponents")).toString() == data.name; }); effect.configurable = it != configs.end(); m_effectsList << effect; } } void EffectModel::loadJavascriptEffects(const KConfigGroup &kwinConfig) { KService::List offers = KServiceTypeTrader::self()->query("KWin/Effect", QStringLiteral("[X-Plasma-API] == 'javascript'")); for(KService::Ptr service : offers) { const QString effectPluginPath = QStandardPaths::locate(QStandardPaths::GenericDataLocation, "kservices5/"+ service->entryPath(), QStandardPaths::LocateFile); KPluginInfo plugin(effectPluginPath); EffectData effect; effect.name = plugin.name(); effect.description = plugin.comment(); effect.authorName = plugin.author(); effect.authorEmail = plugin.email(); effect.license = plugin.license(); effect.version = plugin.version(); effect.category = translatedCategory(plugin.category()); effect.serviceName = plugin.pluginName(); effect.effectStatus = effectStatus(kwinConfig.readEntry(effect.serviceName + "Enabled", plugin.isPluginEnabledByDefault())); effect.enabledByDefault = plugin.isPluginEnabledByDefault(); effect.enabledByDefaultFunction = false; effect.video = service->property(QStringLiteral("X-KWin-Video-Url"), QVariant::Url).toUrl(); effect.supported = true; effect.exclusiveGroup = service->property(QStringLiteral("X-KWin-Exclusive-Category"), QVariant::String).toString(); effect.internal = service->property(QStringLiteral("X-KWin-Internal"), QVariant::Bool).toBool(); effect.scripted = true; if (!service->pluginKeyword().isEmpty()) { // scripted effects have their pluginName() as the keyword effect.configurable = service->property(QStringLiteral("X-KDE-ParentComponents")).toString() == service->pluginKeyword(); } else { effect.configurable = false; } m_effectsList << effect; } } void EffectModel::loadPluginEffects(const KConfigGroup &kwinConfig, const KPluginInfo::List &configs) { static const QString subDir(QStringLiteral("kwin/effects/plugins/")); static const QString serviceType(QStringLiteral("KWin/Effect")); const QVector pluginEffects = KPluginLoader::findPlugins(subDir, [] (const KPluginMetaData &data) { return data.serviceTypes().contains(serviceType); }); for (KPluginMetaData pluginEffect : pluginEffects) { if (!pluginEffect.isValid()) continue; EffectData effect; effect.name = pluginEffect.name(); effect.description = pluginEffect.description(); effect.license = pluginEffect.license(); effect.version = pluginEffect.version(); effect.category = pluginEffect.category(); effect.serviceName = pluginEffect.pluginId(); effect.enabledByDefault = pluginEffect.isEnabledByDefault(); effect.supported = true; effect.enabledByDefaultFunction = false; effect.internal = false; effect.scripted = false; for (int i = 0; i < pluginEffect.authors().count(); ++i) { effect.authorName.append(pluginEffect.authors().at(i).name()); effect.authorEmail.append(pluginEffect.authors().at(i).emailAddress()); if (i+1 < pluginEffect.authors().count()) { effect.authorName.append(", "); effect.authorEmail.append(", "); } } if (pluginEffect.rawData().contains("org.kde.kwin.effect")) { const QJsonObject d(pluginEffect.rawData().value("org.kde.kwin.effect").toObject()); effect.exclusiveGroup = d.value("exclusiveGroup").toString(); effect.video = QUrl::fromUserInput(d.value("video").toString()); effect.enabledByDefaultFunction = d.value("enabledByDefaultMethod").toBool(); } const QString enabledKey = QStringLiteral("%1Enabled").arg(effect.serviceName); if (kwinConfig.hasKey(enabledKey)) { effect.effectStatus = effectStatus(kwinConfig.readEntry(effect.serviceName + "Enabled", effect.enabledByDefault)); } else if (effect.enabledByDefaultFunction) { effect.effectStatus = EffectStatus::EnabledUndeterminded; } else { effect.effectStatus = effectStatus(effect.enabledByDefault); } auto it = std::find_if(configs.begin(), configs.end(), [pluginEffect](const KPluginInfo &info) { return info.property(QStringLiteral("X-KDE-ParentComponents")).toString() == pluginEffect.pluginId(); }); effect.configurable = it != configs.end(); m_effectsList << effect; } } void EffectModel::loadEffects() { KConfigGroup kwinConfig(KSharedConfig::openConfig("kwinrc"), "Plugins"); beginResetModel(); m_effectsChanged.clear(); m_effectsList.clear(); const KPluginInfo::List configs = KPluginTrader::self()->query(QStringLiteral("kwin/effects/configs/")); loadBuiltInEffects(kwinConfig, configs); loadJavascriptEffects(kwinConfig); loadPluginEffects(kwinConfig, configs); qSort(m_effectsList.begin(), m_effectsList.end(), [](const EffectData &a, const EffectData &b) { if (a.category == b.category) { if (a.exclusiveGroup == b.exclusiveGroup) { return a.name < b.name; } return a.exclusiveGroup < b.exclusiveGroup; } return a.category < b.category; }); OrgKdeKwinEffectsInterface interface(QStringLiteral("org.kde.KWin"), QStringLiteral("/Effects"), QDBusConnection::sessionBus()); if (interface.isValid()) { QStringList effectNames; std::for_each(m_effectsList.constBegin(), m_effectsList.constEnd(), [&effectNames](const EffectData &data) { effectNames << data.serviceName; }); QDBusPendingCallWatcher *watcher = new QDBusPendingCallWatcher(interface.areEffectsSupported(effectNames), this); watcher->setProperty("effectNames", effectNames); connect(watcher, &QDBusPendingCallWatcher::finished, [this](QDBusPendingCallWatcher *self) { const QStringList effectNames = self->property("effectNames").toStringList(); const QDBusPendingReply< QList< bool > > reply = *self; QList< bool> supportValues; if (reply.isValid()) { supportValues.append(reply.value()); } if (effectNames.size() == supportValues.size()) { for (int i = 0; i < effectNames.size(); ++i) { const bool supportedValue = supportValues.at(i); const QString &effectName = effectNames.at(i); auto it = std::find_if(m_effectsList.begin(), m_effectsList.end(), [effectName](const EffectData &data) { return data.serviceName == effectName; }); if (it != m_effectsList.end()) { if ((*it).supported != supportedValue) { (*it).supported = supportedValue; QModelIndex i = index(findRowByServiceName(effectName), 0); if (i.isValid()) { emit dataChanged(i, i, QVector() << SupportedRole); } } } } } self->deleteLater(); }); } m_effectsChanged = m_effectsList; endResetModel(); } int EffectModel::findRowByServiceName(const QString &serviceName) { for (int it = 0; it < m_effectsList.size(); it++) { if (m_effectsList.at(it).serviceName == serviceName) { return it; } } return -1; } void EffectModel::syncEffectsToKWin() { OrgKdeKwinEffectsInterface interface(QStringLiteral("org.kde.KWin"), QStringLiteral("/Effects"), QDBusConnection::sessionBus()); for (int it = 0; it < m_effectsList.size(); it++) { if (m_effectsList.at(it).effectStatus != m_effectsChanged.at(it).effectStatus) { if (m_effectsList.at(it).effectStatus != EffectStatus::Disabled) { interface.loadEffect(m_effectsList.at(it).serviceName); } else { interface.unloadEffect(m_effectsList.at(it).serviceName); } } } m_effectsChanged = m_effectsList; } void EffectModel::updateEffectStatus(const QModelIndex &rowIndex, EffectStatus effectState) { setData(rowIndex, (int)effectState, EffectModel::EffectStatusRole); } void EffectModel::syncConfig() { KConfigGroup kwinConfig(KSharedConfig::openConfig("kwinrc"), "Plugins"); for (auto it = m_effectsList.begin(); it != m_effectsList.end(); it++) { EffectData &effect = *(it); if (!effect.changed) { continue; } effect.changed = false; const QString key = effect.serviceName + QStringLiteral("Enabled"); const bool shouldEnable = (effect.effectStatus != EffectStatus::Disabled); const bool restoreToDefault = effect.enabledByDefaultFunction ? effect.effectStatus == EffectStatus::EnabledUndeterminded : shouldEnable == effect.enabledByDefault; if (restoreToDefault) { kwinConfig.deleteEntry(key); } else { kwinConfig.writeEntry(key, shouldEnable); } } kwinConfig.sync(); syncEffectsToKWin(); } void EffectModel::defaults() { for (int i = 0; i < m_effectsList.count(); ++i) { const auto &effect = m_effectsList.at(i); if (effect.enabledByDefaultFunction && effect.effectStatus != EffectStatus::EnabledUndeterminded) { updateEffectStatus(index(i, 0), EffectStatus::EnabledUndeterminded); } else if ((bool)effect.effectStatus != effect.enabledByDefault) { updateEffectStatus(index(i, 0), effect.enabledByDefault ? EffectStatus::Enabled : EffectStatus::Disabled); } } } EffectFilterModel::EffectFilterModel(QObject *parent) : QSortFilterProxyModel(parent) , m_effectModel(new EffectModel(this)) , m_filterOutUnsupported(true) , m_filterOutInternal(true) { setSourceModel(m_effectModel); connect(this, &EffectFilterModel::filterOutUnsupportedChanged, this, &EffectFilterModel::invalidateFilter); connect(this, &EffectFilterModel::filterOutInternalChanged, this, &EffectFilterModel::invalidateFilter); } const QString &EffectFilterModel::filter() const { return m_filter; } void EffectFilterModel::setFilter(const QString &filter) { if (filter == m_filter) { return; } m_filter = filter; emit filterChanged(); invalidateFilter(); } bool EffectFilterModel::filterAcceptsRow(int source_row, const QModelIndex &source_parent) const { if (!m_effectModel) { return false; } QModelIndex index = m_effectModel->index(source_row, 0, source_parent); if (!index.isValid()) { return false; } if (m_filterOutUnsupported) { if (!index.data(EffectModel::SupportedRole).toBool()) { return false; } } if (m_filterOutInternal) { if (index.data(EffectModel::InternalRole).toBool()) { return false; } } if (m_filter.isEmpty()) { return true; } QVariant data = index.data(); if (!data.isValid()) { //An invalid QVariant is valid data return true; } if (m_effectModel->data(index, EffectModel::NameRole).toString().contains(m_filter, Qt::CaseInsensitive)) { return true; } else if (m_effectModel->data(index, EffectModel::DescriptionRole).toString().contains(m_filter, Qt::CaseInsensitive)) { return true; } if (index.data(EffectModel::CategoryRole).toString().contains(m_filter, Qt::CaseInsensitive)) { return true; } return false; } void EffectFilterModel::updateEffectStatus(int rowIndex, int effectState) { const QModelIndex sourceIndex = mapToSource(index(rowIndex, 0)); m_effectModel->updateEffectStatus(sourceIndex, EffectStatus(effectState)); } void EffectFilterModel::syncConfig() { m_effectModel->syncConfig(); } void EffectFilterModel::load() { m_effectModel->loadEffects(); } void EffectFilterModel::defaults() { m_effectModel->defaults(); } EffectView::EffectView(ViewType type, QWidget *parent) : QQuickWidget(parent) { qRegisterMetaType(); qmlRegisterType("org.kde.kwin.kwincompositing", 1, 0, "EffectConfig"); qmlRegisterType("org.kde.kwin.kwincompositing", 1, 0, "EffectFilterModel"); qmlRegisterType("org.kde.kwin.kwincompositing", 1, 0, "Compositing"); qmlRegisterType("org.kde.kwin.kwincompositing", 1, 0, "CompositingType"); init(type); } void EffectView::init(ViewType type) { KDeclarative::KDeclarative kdeclarative; kdeclarative.setDeclarativeEngine(engine()); kdeclarative.setTranslationDomain(QStringLiteral(TRANSLATION_DOMAIN)); - kdeclarative.setupBindings(); + kdeclarative.setupContext(); + kdeclarative.setupEngine(engine()); QString path; switch (type) { case CompositingSettingsView: path = QStringLiteral("kwincompositing/qml/main-compositing.qml"); break; case DesktopEffectsView: path = QStringLiteral("kwincompositing/qml/main.qml"); break; } QString mainFile = QStandardPaths::locate(QStandardPaths::GenericDataLocation, path, QStandardPaths::LocateFile); setResizeMode(QQuickWidget::SizeRootObjectToView); setSource(QUrl(mainFile)); rootObject()->setProperty("color", KColorScheme(QPalette::Active, KColorScheme::Window, KSharedConfigPtr(0)).background(KColorScheme::NormalBackground).color()); connect(rootObject(), SIGNAL(changed()), this, SIGNAL(changed())); setMinimumSize(initialSize()); connect(rootObject(), SIGNAL(implicitWidthChanged()), this, SLOT(slotImplicitSizeChanged())); connect(rootObject(), SIGNAL(implicitHeightChanged()), this, SLOT(slotImplicitSizeChanged())); } void EffectView::save() { if (auto *model = rootObject()->findChild(QStringLiteral("filterModel"))) { model->syncConfig(); } if (auto *compositing = rootObject()->findChild(QStringLiteral("compositing"))) { compositing->save(); } } void EffectView::load() { if (auto *model = rootObject()->findChild(QStringLiteral("filterModel"))) { model->load(); } if (auto *compositing = rootObject()->findChild(QStringLiteral("compositing"))) { compositing->reset(); } } void EffectView::defaults() { if (auto *model = rootObject()->findChild(QStringLiteral("filterModel"))) { model->defaults(); } if (auto *compositing = rootObject()->findChild(QStringLiteral("compositing"))) { compositing->defaults(); } } void EffectView::slotImplicitSizeChanged() { setMinimumSize(QSize(rootObject()->property("implicitWidth").toInt(), rootObject()->property("implicitHeight").toInt())); } }//end namespace Compositing }//end namespace KWin diff --git a/kcmkwin/kwindecoration/kcm.cpp b/kcmkwin/kwindecoration/kcm.cpp index 0baec7630..995041c8b 100644 --- a/kcmkwin/kwindecoration/kcm.cpp +++ b/kcmkwin/kwindecoration/kcm.cpp @@ -1,434 +1,435 @@ /* * Copyright 2014 Martin Gräßlin * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License as * published by the Free Software Foundation; either version 2 of * the License or (at your option) version 3 or any later version * accepted by the membership of KDE e.V. (or its successor approved * by the membership of KDE e.V.), which shall act as a proxy * defined in Section 14 of version 3 of the license. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ #include "kcm.h" #include "decorationmodel.h" #include "declarative-plugin/buttonsmodel.h" #include // KDE #include #include #include #include #include #include // Qt #include #include #include #include #include #include #include #include #include #include #include K_PLUGIN_FACTORY(KDecorationFactory, registerPlugin(); ) Q_DECLARE_METATYPE(KDecoration2::BorderSize) namespace KDecoration2 { namespace Configuration { static const QString s_pluginName = QStringLiteral("org.kde.kdecoration2"); #if HAVE_BREEZE_DECO static const QString s_defaultPlugin = QStringLiteral(BREEZE_KDECORATION_PLUGIN_ID); static const QString s_defaultTheme; #else static const QString s_defaultPlugin = QStringLiteral("org.kde.kwin.aurorae"); static const QString s_defaultTheme = QStringLiteral("kwin4_decoration_qml_plastik"); #endif static const QString s_borderSizeNormal = QStringLiteral("Normal"); static const QString s_ghnsIcon = QStringLiteral("get-hot-new-stuff"); ConfigurationForm::ConfigurationForm(QWidget *parent) : QWidget(parent) { setupUi(this); } static bool s_loading = false; ConfigurationModule::ConfigurationModule(QWidget *parent, const QVariantList &args) : KCModule(parent, args) , m_model(new DecorationsModel(this)) , m_proxyModel(new QSortFilterProxyModel(this)) , m_ui(new ConfigurationForm(this)) , m_leftButtons(new Preview::ButtonsModel(QVector(), this)) , m_rightButtons(new Preview::ButtonsModel(QVector(), this)) , m_availableButtons(new Preview::ButtonsModel(this)) { m_proxyModel->setSourceModel(m_model); m_proxyModel->setFilterCaseSensitivity(Qt::CaseInsensitive); m_proxyModel->setSortCaseSensitivity(Qt::CaseInsensitive); m_proxyModel->sort(0); connect(m_ui->filter, &QLineEdit::textChanged, m_proxyModel, &QSortFilterProxyModel::setFilterFixedString); m_quickView = new QQuickView(0); KDeclarative::KDeclarative kdeclarative; kdeclarative.setDeclarativeEngine(m_quickView->engine()); kdeclarative.setTranslationDomain(QStringLiteral(TRANSLATION_DOMAIN)); - kdeclarative.setupBindings(); + kdeclarative.setupContext(); + kdeclarative.setupEngine(m_quickView->engine()); qmlRegisterType(); QWidget *widget = QWidget::createWindowContainer(m_quickView, this); QVBoxLayout* layout = new QVBoxLayout(m_ui->view); layout->setContentsMargins(0,0,0,0); layout->addWidget(widget); m_quickView->rootContext()->setContextProperty(QStringLiteral("decorationsModel"), m_proxyModel); updateColors(); m_quickView->rootContext()->setContextProperty("_borderSizesIndex", 3); // 3 is normal m_quickView->rootContext()->setContextProperty("leftButtons", m_leftButtons); m_quickView->rootContext()->setContextProperty("rightButtons", m_rightButtons); m_quickView->rootContext()->setContextProperty("availableButtons", m_availableButtons); m_quickView->rootContext()->setContextProperty("titleFont", QFontDatabase::systemFont(QFontDatabase::TitleFont)); m_quickView->setResizeMode(QQuickView::SizeRootObjectToView); m_quickView->setSource(QUrl::fromLocalFile(QStandardPaths::locate(QStandardPaths::GenericDataLocation, QStringLiteral("kwin/kcm_kwindecoration/main.qml")))); if (m_quickView->status() == QQuickView::Ready) { auto listView = m_quickView->rootObject()->findChild("listView"); if (listView) { connect(listView, SIGNAL(currentIndexChanged()), this, SLOT(changed())); } } m_ui->tabWidget->tabBar()->disconnect(); auto setCurrentTab = [this](int index) { if (index == 0) m_ui->doubleClickMessage->hide(); m_ui->filter->setVisible(index == 0); m_ui->knsButton->setVisible(index == 0); if (auto themeList = m_quickView->rootObject()->findChild("themeList")) { themeList->setVisible(index == 0); } m_ui->borderSizesLabel->setVisible(index == 0); m_ui->borderSizesCombo->setVisible(index == 0); m_ui->closeWindowsDoubleClick->setVisible(index == 1); if (auto buttonLayout = m_quickView->rootObject()->findChild("buttonLayout")) { buttonLayout->setVisible(index == 1); } }; connect(m_ui->tabWidget->tabBar(), &QTabBar::currentChanged, this, setCurrentTab); setCurrentTab(0); m_ui->doubleClickMessage->setVisible(false); m_ui->doubleClickMessage->setText(i18n("Close by double clicking:\n To open the menu, keep the button pressed until it appears.")); m_ui->doubleClickMessage->setCloseButtonVisible(true); m_ui->borderSizesCombo->setItemData(0, QVariant::fromValue(BorderSize::None)); m_ui->borderSizesCombo->setItemData(1, QVariant::fromValue(BorderSize::NoSides)); m_ui->borderSizesCombo->setItemData(2, QVariant::fromValue(BorderSize::Tiny)); m_ui->borderSizesCombo->setItemData(3, QVariant::fromValue(BorderSize::Normal)); m_ui->borderSizesCombo->setItemData(4, QVariant::fromValue(BorderSize::Large)); m_ui->borderSizesCombo->setItemData(5, QVariant::fromValue(BorderSize::VeryLarge)); m_ui->borderSizesCombo->setItemData(6, QVariant::fromValue(BorderSize::Huge)); m_ui->borderSizesCombo->setItemData(7, QVariant::fromValue(BorderSize::VeryHuge)); m_ui->borderSizesCombo->setItemData(8, QVariant::fromValue(BorderSize::Oversized)); m_ui->knsButton->setIcon(QIcon::fromTheme(s_ghnsIcon)); auto changedSlot = static_cast(&ConfigurationModule::changed); connect(m_ui->closeWindowsDoubleClick, &QCheckBox::stateChanged, this, changedSlot); connect(m_ui->closeWindowsDoubleClick, &QCheckBox::toggled, this, [this] (bool toggled) { if (s_loading) { return; } if (toggled) m_ui->doubleClickMessage->animatedShow(); else m_ui->doubleClickMessage->animatedHide(); } ); connect(m_ui->borderSizesCombo, static_cast(&QComboBox::currentIndexChanged), this, [this] (int index) { auto listView = m_quickView->rootObject()->findChild("listView"); if (listView) { listView->setProperty("borderSizesIndex", index); } changed(); } ); connect(m_model, &QAbstractItemModel::modelReset, this, [this] { const auto &kns = m_model->knsProviders(); m_ui->knsButton->setEnabled(!kns.isEmpty()); if (kns.isEmpty()) { return; } if (kns.count() > 1) { QMenu *menu = new QMenu(m_ui->knsButton); for (auto it = kns.begin(); it != kns.end(); ++it) { QAction *action = menu->addAction(QIcon::fromTheme(s_ghnsIcon), it.value()); action->setData(it.key()); connect(action, &QAction::triggered, this, [this, action] { showKNS(action->data().toString());}); } m_ui->knsButton->setMenu(menu); } } ); connect(m_ui->knsButton, &QPushButton::clicked, this, [this] { const auto &kns = m_model->knsProviders(); if (kns.isEmpty()) { return; } showKNS(kns.firstKey()); } ); connect(m_leftButtons, &QAbstractItemModel::rowsInserted, this, changedSlot); connect(m_leftButtons, &QAbstractItemModel::rowsMoved, this, changedSlot); connect(m_leftButtons, &QAbstractItemModel::rowsRemoved, this, changedSlot); connect(m_rightButtons, &QAbstractItemModel::rowsInserted, this, changedSlot); connect(m_rightButtons, &QAbstractItemModel::rowsMoved, this, changedSlot); connect(m_rightButtons, &QAbstractItemModel::rowsRemoved, this, changedSlot); QVBoxLayout *l = new QVBoxLayout(this); l->addWidget(m_ui); QMetaObject::invokeMethod(m_model, "init", Qt::QueuedConnection); m_ui->installEventFilter(this); } ConfigurationModule::~ConfigurationModule() = default; void ConfigurationModule::showEvent(QShowEvent *ev) { KCModule::showEvent(ev); } static const QMap s_sizes = QMap({ {QStringLiteral("None"), BorderSize::None}, {QStringLiteral("NoSides"), BorderSize::NoSides}, {QStringLiteral("Tiny"), BorderSize::Tiny}, {s_borderSizeNormal, BorderSize::Normal}, {QStringLiteral("Large"), BorderSize::Large}, {QStringLiteral("VeryLarge"), BorderSize::VeryLarge}, {QStringLiteral("Huge"), BorderSize::Huge}, {QStringLiteral("VeryHuge"), BorderSize::VeryHuge}, {QStringLiteral("Oversized"), BorderSize::Oversized} }); static BorderSize stringToSize(const QString &name) { auto it = s_sizes.constFind(name); if (it == s_sizes.constEnd()) { // non sense values are interpreted just like normal return BorderSize::Normal; } return it.value(); } static QString sizeToString(BorderSize size) { return s_sizes.key(size, s_borderSizeNormal); } static QHash s_buttonNames; static void initButtons() { if (!s_buttonNames.isEmpty()) { return; } s_buttonNames[KDecoration2::DecorationButtonType::Menu] = QChar('M'); s_buttonNames[KDecoration2::DecorationButtonType::ApplicationMenu] = QChar('N'); s_buttonNames[KDecoration2::DecorationButtonType::OnAllDesktops] = QChar('S'); s_buttonNames[KDecoration2::DecorationButtonType::ContextHelp] = QChar('H'); s_buttonNames[KDecoration2::DecorationButtonType::Minimize] = QChar('I'); s_buttonNames[KDecoration2::DecorationButtonType::Maximize] = QChar('A'); s_buttonNames[KDecoration2::DecorationButtonType::Close] = QChar('X'); s_buttonNames[KDecoration2::DecorationButtonType::KeepAbove] = QChar('F'); s_buttonNames[KDecoration2::DecorationButtonType::KeepBelow] = QChar('B'); s_buttonNames[KDecoration2::DecorationButtonType::Shade] = QChar('L'); } static QString buttonsToString(const QVector &buttons) { auto buttonToString = [](KDecoration2::DecorationButtonType button) -> QChar { const auto it = s_buttonNames.constFind(button); if (it != s_buttonNames.constEnd()) { return it.value(); } return QChar(); }; QString ret; for (auto button : buttons) { ret.append(buttonToString(button)); } return ret; } static QVector< KDecoration2::DecorationButtonType > readDecorationButtons(const KConfigGroup &config, const char *key, const QVector< KDecoration2::DecorationButtonType > &defaultValue) { initButtons(); auto buttonsFromString = [](const QString &buttons) -> QVector { QVector ret; for (auto it = buttons.begin(); it != buttons.end(); ++it) { for (auto it2 = s_buttonNames.constBegin(); it2 != s_buttonNames.constEnd(); ++it2) { if (it2.value() == (*it)) { ret << it2.key(); } } } return ret; }; return buttonsFromString(config.readEntry(key, buttonsToString(defaultValue))); } void ConfigurationModule::load() { s_loading = true; const KConfigGroup config = KSharedConfig::openConfig("kwinrc")->group(s_pluginName); const QString plugin = config.readEntry("library", s_defaultPlugin); const QString theme = config.readEntry("theme", s_defaultTheme); m_ui->closeWindowsDoubleClick->setChecked(config.readEntry("CloseOnDoubleClickOnMenu", false)); const QVariant border = QVariant::fromValue(stringToSize(config.readEntry("BorderSize", s_borderSizeNormal))); m_ui->borderSizesCombo->setCurrentIndex(m_ui->borderSizesCombo->findData(border)); int themeIndex = m_proxyModel->mapFromSource(m_model->findDecoration(plugin, theme)).row(); m_quickView->rootContext()->setContextProperty("savedIndex", themeIndex); // buttons const auto &left = readDecorationButtons(config, "ButtonsOnLeft", QVector{ KDecoration2::DecorationButtonType::Menu, KDecoration2::DecorationButtonType::OnAllDesktops }); while (m_leftButtons->rowCount() > 0) { m_leftButtons->remove(0); } for (auto it = left.begin(); it != left.end(); ++it) { m_leftButtons->add(*it); } const auto &right = readDecorationButtons(config, "ButtonsOnRight", QVector{ KDecoration2::DecorationButtonType::ContextHelp, KDecoration2::DecorationButtonType::Minimize, KDecoration2::DecorationButtonType::Maximize, KDecoration2::DecorationButtonType::Close }); while (m_rightButtons->rowCount() > 0) { m_rightButtons->remove(0); } for (auto it = right.begin(); it != right.end(); ++it) { m_rightButtons->add(*it); } KCModule::load(); s_loading = false; } void ConfigurationModule::save() { KConfigGroup config = KSharedConfig::openConfig("kwinrc")->group(s_pluginName); config.writeEntry("CloseOnDoubleClickOnMenu", m_ui->closeWindowsDoubleClick->isChecked()); config.writeEntry("BorderSize", sizeToString(m_ui->borderSizesCombo->currentData().value())); if (auto listView = m_quickView->rootObject()->findChild("listView")) { const int currentIndex = listView->property("currentIndex").toInt(); if (currentIndex != -1) { const QModelIndex index = m_proxyModel->index(currentIndex, 0); if (index.isValid()) { config.writeEntry("library", index.data(Qt::UserRole + 4).toString()); const QString theme = index.data(Qt::UserRole +5).toString(); if (theme.isEmpty()) { config.deleteEntry("theme"); } else { config.writeEntry("theme", theme); } } } } config.writeEntry("ButtonsOnLeft", buttonsToString(m_leftButtons->buttons())); config.writeEntry("ButtonsOnRight", buttonsToString(m_rightButtons->buttons())); config.sync(); KCModule::save(); // Send signal to all kwin instances QDBusMessage message = QDBusMessage::createSignal(QStringLiteral("/KWin"), QStringLiteral("org.kde.KWin"), QStringLiteral("reloadConfig")); QDBusConnection::sessionBus().send(message); } void ConfigurationModule::defaults() { if (auto listView = m_quickView->rootObject()->findChild("listView")) { const QModelIndex index = m_proxyModel->mapFromSource(m_model->findDecoration(s_defaultPlugin)); listView->setProperty("currentIndex", index.isValid() ? index.row() : -1); } m_ui->borderSizesCombo->setCurrentIndex(m_ui->borderSizesCombo->findData(QVariant::fromValue(stringToSize(s_borderSizeNormal)))); m_ui->closeWindowsDoubleClick->setChecked(false); KCModule::defaults(); } void ConfigurationModule::showKNS(const QString &config) { QPointer downloadDialog = new KNS3::DownloadDialog(config, this); if (downloadDialog->exec() == QDialog::Accepted && !downloadDialog->changedEntries().isEmpty()) { auto listView = m_quickView->rootObject()->findChild("listView"); QString selectedPluginName; QString selectedThemeName; if (listView) { const QModelIndex index = m_proxyModel->index(listView->property("currentIndex").toInt(), 0); if (index.isValid()) { selectedPluginName = index.data(Qt::UserRole + 4).toString(); selectedThemeName = index.data(Qt::UserRole + 5).toString(); } } m_model->init(); if (!selectedPluginName.isEmpty()) { const QModelIndex index = m_model->findDecoration(selectedPluginName, selectedThemeName); const QModelIndex proxyIndex = m_proxyModel->mapFromSource(index); if (listView) { listView->setProperty("currentIndex", proxyIndex.isValid() ? proxyIndex.row() : -1); } } } delete downloadDialog; } bool ConfigurationModule::eventFilter(QObject *watched, QEvent *e) { if (watched != m_ui) { return false; } if (e->type() == QEvent::PaletteChange) { updateColors(); } return false; } void ConfigurationModule::updateColors() { m_quickView->rootContext()->setContextProperty("backgroundColor", m_ui->palette().color(QPalette::Active, QPalette::Window)); m_quickView->rootContext()->setContextProperty("highlightColor", m_ui->palette().color(QPalette::Active, QPalette::Highlight)); m_quickView->rootContext()->setContextProperty("baseColor", m_ui->palette().color(QPalette::Active, QPalette::Base)); } } } #include "kcm.moc"