diff --git a/kcmkwin/kwindecoration/declarative-plugin/CMakeLists.txt b/kcmkwin/kwindecoration/declarative-plugin/CMakeLists.txt index 7d5c492250..8dd728ea5c 100644 --- a/kcmkwin/kwindecoration/declarative-plugin/CMakeLists.txt +++ b/kcmkwin/kwindecoration/declarative-plugin/CMakeLists.txt @@ -1,4 +1,5 @@ set(plugin_SRCS + previewbutton.cpp previewbridge.cpp previewclient.cpp previewitem.cpp @@ -13,6 +14,7 @@ target_link_libraries(kdecorationprivatedeclarative Qt5::Quick KF5::CoreAddons KF5::ConfigWidgets + KF5::I18n KF5::Service ) diff --git a/kcmkwin/kwindecoration/declarative-plugin/plugin.cpp b/kcmkwin/kwindecoration/declarative-plugin/plugin.cpp index 6339d3b3a7..e9064358ad 100644 --- a/kcmkwin/kwindecoration/declarative-plugin/plugin.cpp +++ b/kcmkwin/kwindecoration/declarative-plugin/plugin.cpp @@ -18,6 +18,7 @@ * along with this program. If not, see . */ #include "plugin.h" +#include "previewbutton.h" #include "previewbridge.h" #include "previewclient.h" #include "previewitem.h" @@ -39,6 +40,8 @@ void Plugin::registerTypes(const char *uri) qmlRegisterType(uri, 1, 0, "Bridge"); qmlRegisterType(uri, 1, 0, "Settings"); qmlRegisterType(uri, 1, 0, "Decoration"); + qmlRegisterType(uri, 1, 0, "Button"); + qmlRegisterType(uri, 1, 0, "ButtonsModel"); qmlRegisterType(); qmlRegisterType(); qmlRegisterType(); diff --git a/kcmkwin/kwindecoration/declarative-plugin/previewbridge.cpp b/kcmkwin/kwindecoration/declarative-plugin/previewbridge.cpp index d6e7dbe26d..b749e0a2eb 100644 --- a/kcmkwin/kwindecoration/declarative-plugin/previewbridge.cpp +++ b/kcmkwin/kwindecoration/declarative-plugin/previewbridge.cpp @@ -161,5 +161,13 @@ Decoration *PreviewBridge::createDecoration(QObject *parent) return m_factory->create(parent, QVariantList({args})); } +DecorationButton *PreviewBridge::createButton(KDecoration2::Decoration *decoration, KDecoration2::DecorationButtonType type, QObject *parent) +{ + if (!m_valid) { + return nullptr; + } + return m_factory->create(QStringLiteral("button"), parent, QVariantList({QVariant::fromValue(type), QVariant::fromValue(decoration)})); +} + } } diff --git a/kcmkwin/kwindecoration/declarative-plugin/previewbridge.h b/kcmkwin/kwindecoration/declarative-plugin/previewbridge.h index 52814376b9..318d4148a8 100644 --- a/kcmkwin/kwindecoration/declarative-plugin/previewbridge.h +++ b/kcmkwin/kwindecoration/declarative-plugin/previewbridge.h @@ -21,6 +21,7 @@ #define KDECOARTIONS_PREVIEW_BRIDGE_H #include +#include #include #include @@ -66,6 +67,7 @@ public: bool isValid() const; KDecoration2::Decoration *createDecoration(QObject *parent = nullptr); + KDecoration2::DecorationButton *createButton(KDecoration2::Decoration *decoration, KDecoration2::DecorationButtonType type, QObject *parent = nullptr); Q_SIGNALS: void pluginChanged(); diff --git a/kcmkwin/kwindecoration/declarative-plugin/previewbutton.cpp b/kcmkwin/kwindecoration/declarative-plugin/previewbutton.cpp new file mode 100644 index 0000000000..b69761517e --- /dev/null +++ b/kcmkwin/kwindecoration/declarative-plugin/previewbutton.cpp @@ -0,0 +1,139 @@ +/* + * 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 "previewbutton.h" +#include "previewbridge.h" +#include "previewclient.h" +#include "previewsettings.h" + +#include + +#include + +namespace KDecoration2 +{ + +namespace Preview +{ + +PreviewButtonItem::PreviewButtonItem(QQuickItem* parent) + : QQuickPaintedItem(parent) +{ +} + +PreviewButtonItem::~PreviewButtonItem() = default; + +void PreviewButtonItem::setType(int type) +{ + setType(KDecoration2::DecorationButtonType(type)); +} + +void PreviewButtonItem::setType(KDecoration2::DecorationButtonType type) +{ + if (m_type == type) { + return; + } + m_type = type; + emit typeChanged(); +} + +KDecoration2::DecorationButtonType PreviewButtonItem::type() const +{ + return m_type; +} + +PreviewBridge *PreviewButtonItem::bridge() const +{ + return m_bridge.data(); +} + +void PreviewButtonItem::setBridge(PreviewBridge *bridge) +{ + if (m_bridge == bridge) { + return; + } + m_bridge = bridge; + emit bridgeChanged(); +} + +Settings *PreviewButtonItem::settings() const +{ + return m_settings.data(); +} + +void PreviewButtonItem::setSettings(Settings *settings) +{ + if (m_settings == settings) { + return; + } + m_settings = settings; + emit settingsChanged(); +} + +int PreviewButtonItem::typeAsInt() const +{ + return int(m_type); +} + +void PreviewButtonItem::componentComplete() +{ + QQuickPaintedItem::componentComplete(); + createButton(); +} + +void PreviewButtonItem::createButton() +{ + if (m_type == KDecoration2::DecorationButtonType::Custom || m_decoration || !m_settings || !m_bridge) { + return; + } + m_decoration = m_bridge->createDecoration(this); + if (!m_decoration) { + return; + } + auto client = m_bridge->lastCreatedClient(); + client->setMinimizable(true); + client->setMaximizable(true); + client->setActive(false); + m_decoration->setSettings(m_settings->settings()); + m_decoration->init(); + m_button = m_bridge->createButton(m_decoration, m_type); + connect(this, &PreviewButtonItem::widthChanged, this, &PreviewButtonItem::syncGeometry); + connect(this, &PreviewButtonItem::heightChanged, this, &PreviewButtonItem::syncGeometry); + syncGeometry(); +} + +void PreviewButtonItem::syncGeometry() +{ + if (!m_button) { + return; + } + m_button->setGeometry(QRect(0, 0, width(), height())); +} + +void PreviewButtonItem::paint(QPainter *painter) +{ + Q_UNUSED(painter) + if (!m_button) { + return; + } + m_button->paint(painter); +} + +} +} diff --git a/kcmkwin/kwindecoration/declarative-plugin/previewbutton.h b/kcmkwin/kwindecoration/declarative-plugin/previewbutton.h new file mode 100644 index 0000000000..0afdf301b4 --- /dev/null +++ b/kcmkwin/kwindecoration/declarative-plugin/previewbutton.h @@ -0,0 +1,81 @@ +/* + * 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 . + */ +#ifndef KDECOARTIONS_PREVIEW_BUTTON_ITEM_H +#define KDECOARTIONS_PREVIEW_BUTTON_ITEM_H + +#include +#include +#include + +namespace KDecoration2 +{ +class Decoration; + +namespace Preview +{ +class PreviewBridge; +class Settings; + +class PreviewButtonItem : public QQuickPaintedItem +{ + Q_OBJECT + Q_PROPERTY(KDecoration2::Preview::PreviewBridge *bridge READ bridge WRITE setBridge NOTIFY bridgeChanged) + Q_PROPERTY(KDecoration2::Preview::Settings *settings READ settings WRITE setSettings NOTIFY settingsChanged) + Q_PROPERTY(int type READ typeAsInt WRITE setType NOTIFY typeChanged) + +public: + explicit PreviewButtonItem(QQuickItem *parent = nullptr); + virtual ~PreviewButtonItem(); + void paint(QPainter *painter) override; + + PreviewBridge *bridge() const; + void setBridge(PreviewBridge *bridge); + + Settings *settings() const; + void setSettings(Settings *settings); + + KDecoration2::DecorationButtonType type() const; + int typeAsInt() const; + void setType(KDecoration2::DecorationButtonType type); + void setType(int type); + +Q_SIGNALS: + void bridgeChanged(); + void typeChanged(); + void settingsChanged(); + +protected: + void componentComplete() override; + +private: + void createButton(); + void syncGeometry(); + QPointer m_bridge; + QPointer m_settings; + KDecoration2::Decoration *m_decoration = nullptr; + KDecoration2::DecorationButton *m_button = nullptr; + KDecoration2::DecorationButtonType m_type = KDecoration2::DecorationButtonType::Custom; + +}; + +} // Preview +} // KDecoration2 + +#endif diff --git a/kcmkwin/kwindecoration/declarative-plugin/previewsettings.cpp b/kcmkwin/kwindecoration/declarative-plugin/previewsettings.cpp index 1db0971f81..c6b54ae5dc 100644 --- a/kcmkwin/kwindecoration/declarative-plugin/previewsettings.cpp +++ b/kcmkwin/kwindecoration/declarative-plugin/previewsettings.cpp @@ -20,6 +20,8 @@ #include "previewsettings.h" #include "previewbridge.h" +#include + #include namespace KDecoration2 @@ -34,6 +36,22 @@ ButtonsModel::ButtonsModel(const QList< DecorationButtonType > &buttons, QObject { } +ButtonsModel::ButtonsModel(QObject* parent) + : ButtonsModel(QList({ + DecorationButtonType::Menu, + DecorationButtonType::ApplicationMenu, + DecorationButtonType::OnAllDesktops, + DecorationButtonType::Minimize, + DecorationButtonType::Maximize, + DecorationButtonType::Close, + DecorationButtonType::QuickHelp, + DecorationButtonType::Shade, + DecorationButtonType::KeepBelow, + DecorationButtonType::KeepAbove + }), parent) +{ +} + ButtonsModel::~ButtonsModel() = default; int ButtonsModel::rowCount(const QModelIndex &parent) const @@ -44,6 +62,34 @@ int ButtonsModel::rowCount(const QModelIndex &parent) const return m_buttons.count(); } +static QString buttonToName(DecorationButtonType type) +{ + switch (type) { + case DecorationButtonType::Menu: + return i18n("Menu"); + case DecorationButtonType::ApplicationMenu: + return i18n("Application menu"); + case DecorationButtonType::OnAllDesktops: + return i18n("On all desktops"); + case DecorationButtonType::Minimize: + return i18n("Minimize"); + case DecorationButtonType::Maximize: + return i18n("Maximize"); + case DecorationButtonType::Close: + return i18n("Close"); + case DecorationButtonType::QuickHelp: + return i18n("Quick help"); + case DecorationButtonType::Shade: + return i18n("Shade"); + case DecorationButtonType::KeepBelow: + return i18n("Keep below"); + case DecorationButtonType::KeepAbove: + return i18n("Keep above"); + default: + return QString(); + } +} + QVariant ButtonsModel::data(const QModelIndex &index, int role) const { if (!index.isValid() || @@ -54,9 +100,9 @@ QVariant ButtonsModel::data(const QModelIndex &index, int role) const } switch (role) { case Qt::DisplayRole: - return QVariant::fromValue(m_buttons.at(index.row())); + return buttonToName(m_buttons.at(index.row())); case Qt::UserRole: - return QVariant::fromValue(m_buttons.at(index.row())); + return QVariant::fromValue(int(m_buttons.at(index.row()))); } return QVariant(); } @@ -65,6 +111,7 @@ QHash< int, QByteArray > ButtonsModel::roleNames() const { QHash roles; roles.insert(Qt::DisplayRole, QByteArrayLiteral("display")); + roles.insert(Qt::UserRole, QByteArrayLiteral("button")); return roles; } diff --git a/kcmkwin/kwindecoration/declarative-plugin/previewsettings.h b/kcmkwin/kwindecoration/declarative-plugin/previewsettings.h index 1b5a43c254..b3eb2a931d 100644 --- a/kcmkwin/kwindecoration/declarative-plugin/previewsettings.h +++ b/kcmkwin/kwindecoration/declarative-plugin/previewsettings.h @@ -37,6 +37,7 @@ class ButtonsModel : public QAbstractListModel Q_OBJECT public: explicit ButtonsModel(const QList< DecorationButtonType > &buttons, QObject *parent = 0); + explicit ButtonsModel(QObject *parent = nullptr); virtual ~ButtonsModel(); QVariant data(const QModelIndex& index, int role = Qt::DisplayRole) const override; int rowCount(const QModelIndex& parent = QModelIndex()) const override;