From 22fe6a2857f37679074d48f6ee2813884e471a8b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Gr=C3=A4=C3=9Flin?= Date: Thu, 27 Feb 2014 15:25:03 +0100 Subject: [PATCH] [kwin] Provide composited outline as QML instead of hand drawn FrameSvg In order to get all our UI being written in QML, the Outline for compositing is ported to QtQuick. It creates a QQmlContext and QQmlComponent and loads the (currently hardcoded) qml file. The context gets to properties exported: outline. The outline property allows the qml to get the geometry it should use. The QQmlEngine is used from Scripting thus all general scripting properties are exported (e.g. workspace). The qml script is expected to create a QQuickWindow as it's root item and style it. The qml file is stored in a new qml subdirectory. The idea is that each of the qml types we have gets an own directory there and for each implementation there should be a further subdirectory. Thus we have outline/plasma/ with outline being the type and plasma being the implementation. But at the moment the script location is still hardcoded. REVIEW: 116123 --- CMakeLists.txt | 1 + outline.cpp | 95 ++++++++++++++-------------------- outline.h | 27 +++++++--- qml/CMakeLists.txt | 1 + qml/outline/plasma/outline.qml | 82 +++++++++++++++++++++++++++++ 5 files changed, 142 insertions(+), 64 deletions(-) create mode 100644 qml/CMakeLists.txt create mode 100644 qml/outline/plasma/outline.qml diff --git a/CMakeLists.txt b/CMakeLists.txt index c67cb9fb0f..5892d64f4e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -349,5 +349,6 @@ install( FILES scripting/kwinscript.desktop DESTINATION ${SERVICETYPES_INSTALL_D ecm_install_icons( ${ICON_INSTALL_DIR} ) +add_subdirectory(qml) add_subdirectory(autotests) add_subdirectory(tests) diff --git a/outline.cpp b/outline.cpp index 314ad7fd34..9e6ef7b907 100644 --- a/outline.cpp +++ b/outline.cpp @@ -22,13 +22,17 @@ along with this program. If not, see . #include "outline.h" // KWin #include "composite.h" +#include "scripting/scripting.h" // KWin libs #include #include "workspace.h" -// Plasma -#include // Qt -#include +#include +#include +#include +#include +#include +#include // xcb #include @@ -50,6 +54,7 @@ Outline::~Outline() void Outline::show() { m_active = true; + emit activeChanged(); if (m_visual.isNull()) { createHelper(); } @@ -66,6 +71,7 @@ void Outline::hide() return; } m_active = false; + emit activeChanged(); if (m_visual.isNull()) { return; } @@ -80,7 +86,11 @@ void Outline::show(const QRect& outlineGeometry) void Outline::setGeometry(const QRect& outlineGeometry) { + if (m_outlineGeometry == outlineGeometry) { + return; + } m_outlineGeometry = outlineGeometry; + emit geometryChanged(); } void Outline::createHelper() @@ -113,17 +123,11 @@ OutlineVisual::~OutlineVisual() } CompositedOutlineVisual::CompositedOutlineVisual(Outline *outline) - : QWidget(NULL, Qt::X11BypassWindowManagerHint) - , OutlineVisual(outline) - , m_background(new Plasma::FrameSvg(this)) + : OutlineVisual(outline) + , m_qmlContext() + , m_qmlComponent() + , m_mainItem(nullptr) { - setAttribute(Qt::WA_TranslucentBackground); - QPalette pal = palette(); - pal.setColor(backgroundRole(), Qt::transparent); - setPalette(pal); - m_background->setImagePath(QStringLiteral("widgets/translucentbackground")); - m_background->setCacheAllRenderedFrames(true); - m_background->setEnabledBorders(Plasma::FrameSvg::AllBorders); } CompositedOutlineVisual::~CompositedOutlineVisual() @@ -132,56 +136,33 @@ CompositedOutlineVisual::~CompositedOutlineVisual() void CompositedOutlineVisual::hide() { - QWidget::hide(); + if (QQuickWindow *w = qobject_cast(m_mainItem)) { + w->hide(); + w->destroy(); + } } void CompositedOutlineVisual::show() { - const QRect &outlineGeometry = outline()->geometry(); - m_background->resizeFrame(outlineGeometry.size()); - setGeometry(outlineGeometry); - - // check which borders to enable - bool left, right, top, bottom; - left = right = top = bottom = false; - const QRect maximizedArea = Workspace::self()->clientArea(MaximizeArea, outlineGeometry.center(), 1); - if (outlineGeometry.x() == maximizedArea.x()) { - left = true; + if (m_qmlContext.isNull()) { + m_qmlContext.reset(new QQmlContext(Scripting::self()->qmlEngine())); + m_qmlContext->setContextProperty(QStringLiteral("outline"), outline()); } - if (outlineGeometry.y() == maximizedArea.y()) { - top = true; + if (m_qmlComponent.isNull()) { + m_qmlComponent.reset(new QQmlComponent(Scripting::self()->qmlEngine())); + // TODO: fileName should be configurable + const QString fileName = QStandardPaths::locate(QStandardPaths::GenericDataLocation, QStringLiteral(KWIN_NAME) + QStringLiteral("/outline/plasma/outline.qml")); + if (fileName.isEmpty()) { + qDebug() << "Could not locate outline.qml"; + return; + } + m_qmlComponent->loadUrl(QUrl::fromLocalFile(fileName)); + if (m_qmlComponent->isError()) { + qDebug() << "Component failed to load: " << m_qmlComponent->errors(); + } else { + m_mainItem = m_qmlComponent->create(m_qmlContext.data()); + } } - if (outlineGeometry.right() == maximizedArea.right()) { - right = true; - } - if (outlineGeometry.bottom() == maximizedArea.bottom()) { - bottom = true; - } - Plasma::FrameSvg::EnabledBorders borders = Plasma::FrameSvg::AllBorders; - if (left) { - borders = borders & ~Plasma::FrameSvg::LeftBorder; - } - if (right) { - borders = borders & ~Plasma::FrameSvg::RightBorder; - } - if (top) { - borders = borders & ~Plasma::FrameSvg::TopBorder; - } - if (bottom) { - borders = borders & ~Plasma::FrameSvg::BottomBorder; - } - if (left && right && bottom && top) { - borders = Plasma::FrameSvg::AllBorders; - } - m_background->setEnabledBorders(borders); - QWidget::show(); -} - -void CompositedOutlineVisual::paintEvent(QPaintEvent *) -{ - QPainter painter(this); - painter.setRenderHint(QPainter::Antialiasing); - m_background->paintFrame(&painter); } NonCompositedOutlineVisual::NonCompositedOutlineVisual(Outline *outline) diff --git a/outline.h b/outline.h index a342454937..ce8bbb50da 100644 --- a/outline.h +++ b/outline.h @@ -25,9 +25,8 @@ along with this program. If not, see . #include #include -namespace Plasma { -class FrameSvg; -} +class QQmlContext; +class QQmlComponent; namespace KWin { class OutlineVisual; @@ -45,6 +44,8 @@ class OutlineVisual; */ class Outline : public QObject { Q_OBJECT + Q_PROPERTY(QRect geometry READ geometry NOTIFY geometryChanged) + Q_PROPERTY(bool active READ isActive NOTIFY activeChanged) public: ~Outline(); @@ -80,9 +81,15 @@ public: const QRect &geometry() const; + bool isActive() const; + private Q_SLOTS: void compositingChanged(); +Q_SIGNALS: + void activeChanged(); + void geometryChanged(); + private: void createHelper(); QScopedPointer m_visual; @@ -105,17 +112,17 @@ private: Outline *m_outline; }; -class CompositedOutlineVisual : public QWidget, public OutlineVisual +class CompositedOutlineVisual : public OutlineVisual { public: CompositedOutlineVisual(Outline *outline); virtual ~CompositedOutlineVisual(); virtual void show(); virtual void hide(); -protected: - virtual void paintEvent(QPaintEvent *); private: - Plasma::FrameSvg *m_background; + QScopedPointer m_qmlContext; + QScopedPointer m_qmlComponent; + QObject *m_mainItem; }; class NonCompositedOutlineVisual : public OutlineVisual @@ -137,6 +144,12 @@ private: Xcb::Window m_leftOutline; }; +inline +bool Outline::isActive() const +{ + return m_active; +} + inline const QRect &Outline::geometry() const { diff --git a/qml/CMakeLists.txt b/qml/CMakeLists.txt new file mode 100644 index 0000000000..15aeae1f90 --- /dev/null +++ b/qml/CMakeLists.txt @@ -0,0 +1 @@ +install( DIRECTORY outline/plasma DESTINATION ${DATA_INSTALL_DIR}/${KWIN_NAME}/outline ) diff --git a/qml/outline/plasma/outline.qml b/qml/outline/plasma/outline.qml new file mode 100644 index 0000000000..8203e82598 --- /dev/null +++ b/qml/outline/plasma/outline.qml @@ -0,0 +1,82 @@ +/* + * 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 . +*/ +import QtQuick 2.1; +import QtQuick.Window 2.1; +import org.kde.plasma.core 2.0 as PlasmaCore; + +Window { + id: window + flags: Qt.BypassWindowManagerHint + color: "transparent" + + // outline is a context property + x: outline.geometry.x + y: outline.geometry.y + width: outline.geometry.width + height: outline.geometry.height + visible: outline.active + + PlasmaCore.FrameSvgItem { + function updateBorders() { + var maximizedArea = workspace.clientArea(workspace.MaximizeArea, Qt.point(outline.geometry.x, outline.geometry.y), workspace.currentDesktop); + var left = false; + var right = false; + var top = false; + var bottom = false; + if (outline.geometry.x == maximizedArea.x) { + left = true; + } + if (outline.geometry.y == maximizedArea.y) { + top = true; + } + if (outline.geometry.x + outline.geometry.width == maximizedArea.x + maximizedArea.width) { + right = true; + } + if (outline.geometry.y + outline.geometry.height == maximizedArea.y + maximizedArea.height) { + bottom = true; + } + var borders = PlasmaCore.FrameSvgItem.AllBorders; + if (left) { + borders = borders & ~PlasmaCore.FrameSvgItem.LeftBorder; + } + if (right) { + borders = borders & ~PlasmaCore.FrameSvgItem.RightBorder; + } + if (top) { + borders = borders & ~PlasmaCore.FrameSvgItem.TopBorder; + } + if (bottom) { + borders = borders & ~PlasmaCore.FrameSvgItem.BottomBorder; + } + if (left && right && bottom && top) { + borders = PlasmaCore.FrameSvgItem.AllBorders; + } + svg.enabledBorders = borders; + } + id: svg + imagePath: "widgets/translucentbackground" + anchors.fill: parent + Connections { + target: outline + onGeometryChanged: svg.updateBorders() + } + Component.onCompleted: svg.updateBorders() + } +}