[kwindecoration] Add a PreviewButtonItem to the declarative plugin

Allows to create a DecorationButton and renders a preview for it.
Very basic, doesn't forward mouse events, etc.
This commit is contained in:
Martin Gräßlin 2014-11-03 16:12:54 +01:00
parent 8dd0a8163f
commit 062d071fc5
8 changed files with 285 additions and 2 deletions

View file

@ -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
)

View file

@ -18,6 +18,7 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#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<KDecoration2::Preview::PreviewBridge>(uri, 1, 0, "Bridge");
qmlRegisterType<KDecoration2::Preview::Settings>(uri, 1, 0, "Settings");
qmlRegisterType<KDecoration2::Preview::PreviewItem>(uri, 1, 0, "Decoration");
qmlRegisterType<KDecoration2::Preview::PreviewButtonItem>(uri, 1, 0, "Button");
qmlRegisterType<KDecoration2::Preview::ButtonsModel>(uri, 1, 0, "ButtonsModel");
qmlRegisterType<KDecoration2::Preview::PreviewClient>();
qmlRegisterType<KDecoration2::Decoration>();
qmlRegisterType<KDecoration2::DecorationShadow>();

View file

@ -161,5 +161,13 @@ Decoration *PreviewBridge::createDecoration(QObject *parent)
return m_factory->create<KDecoration2::Decoration>(parent, QVariantList({args}));
}
DecorationButton *PreviewBridge::createButton(KDecoration2::Decoration *decoration, KDecoration2::DecorationButtonType type, QObject *parent)
{
if (!m_valid) {
return nullptr;
}
return m_factory->create<KDecoration2::DecorationButton>(QStringLiteral("button"), parent, QVariantList({QVariant::fromValue(type), QVariant::fromValue(decoration)}));
}
}
}

View file

@ -21,6 +21,7 @@
#define KDECOARTIONS_PREVIEW_BRIDGE_H
#include <KDecoration2/Private/DecorationBridge>
#include <KDecoration2/DecorationButton>
#include <QList>
#include <QPointer>
@ -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();

View file

@ -0,0 +1,139 @@
/*
* Copyright 2014 Martin Gräßlin <mgraesslin@kde.org>
*
* 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 <http://www.gnu.org/licenses/>.
*/
#include "previewbutton.h"
#include "previewbridge.h"
#include "previewclient.h"
#include "previewsettings.h"
#include <KDecoration2/Decoration>
#include <QDebug>
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);
}
}
}

View file

@ -0,0 +1,81 @@
/*
* Copyright 2014 Martin Gräßlin <mgraesslin@kde.org>
*
* 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 <http://www.gnu.org/licenses/>.
*/
#ifndef KDECOARTIONS_PREVIEW_BUTTON_ITEM_H
#define KDECOARTIONS_PREVIEW_BUTTON_ITEM_H
#include <QQuickPaintedItem>
#include <QPointer>
#include <KDecoration2/DecorationButton>
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<KDecoration2::Preview::PreviewBridge> m_bridge;
QPointer<KDecoration2::Preview::Settings> m_settings;
KDecoration2::Decoration *m_decoration = nullptr;
KDecoration2::DecorationButton *m_button = nullptr;
KDecoration2::DecorationButtonType m_type = KDecoration2::DecorationButtonType::Custom;
};
} // Preview
} // KDecoration2
#endif

View file

@ -20,6 +20,8 @@
#include "previewsettings.h"
#include "previewbridge.h"
#include <KLocalizedString>
#include <QFontDatabase>
namespace KDecoration2
@ -34,6 +36,22 @@ ButtonsModel::ButtonsModel(const QList< DecorationButtonType > &buttons, QObject
{
}
ButtonsModel::ButtonsModel(QObject* parent)
: ButtonsModel(QList<DecorationButtonType>({
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<int, QByteArray> roles;
roles.insert(Qt::DisplayRole, QByteArrayLiteral("display"));
roles.insert(Qt::UserRole, QByteArrayLiteral("button"));
return roles;
}

View file

@ -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;