diff --git a/plasma-windowed/plasmawindowedview.cpp b/plasma-windowed/plasmawindowedview.cpp index 6efe609c8..78155381f 100644 --- a/plasma-windowed/plasmawindowedview.cpp +++ b/plasma-windowed/plasmawindowedview.cpp @@ -1,248 +1,256 @@ /* * Copyright 2014 Bhushan Shah * Copyright 2014 Marco Martin * * 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 "plasmawindowedview.h" #include #include #include #include #include #include #include #include #include #include #include PlasmaWindowedView::PlasmaWindowedView(QWindow *parent) : QQuickView(parent), m_applet(0), m_statusNotifier(0), m_withStatusNotifier(false) { engine()->rootContext()->setContextProperty(QStringLiteral("root"), contentItem()); //access appletInterface.Layout.minimumWidth, to create the Layout attached object for appletInterface as a sideeffect QQmlExpression *expr = new QQmlExpression(engine()->rootContext(), contentItem(), QStringLiteral("Qt.createQmlObject('import QtQuick 2.0; import QtQuick.Layouts 1.1; import org.kde.plasma.core 2.0; Rectangle {color: theme.backgroundColor; anchors.fill:parent; property Item appletInterface; onAppletInterfaceChanged: print(appletInterface.Layout.minimumWidth)}', root, \"\");")); m_rootObject = expr->evaluate().value(); } PlasmaWindowedView::~PlasmaWindowedView() { } void PlasmaWindowedView::setHasStatusNotifier(bool stay) { Q_ASSERT(!m_statusNotifier); m_withStatusNotifier = stay; } void PlasmaWindowedView::setApplet(Plasma::Applet *applet) { m_applet = applet; if (!applet) { return; } m_appletInterface = applet->property("_plasma_graphicObject").value(); if (!m_appletInterface) { return; } m_appletInterface->setParentItem(m_rootObject); m_rootObject->setProperty("appletInterface", QVariant::fromValue(m_appletInterface.data())); m_appletInterface->setVisible(true); setTitle(applet->title()); setIcon(QIcon::fromTheme(applet->icon())); const QSize switchSize(m_appletInterface->property("switchWidth").toInt(), m_appletInterface->property("switchHeight").toInt()); QRect geom = m_applet->config().readEntry("geometry", QRect()); if (geom.isValid()) { geom.setWidth(qMax(geom.width(), switchSize.width() + 1)); geom.setHeight(qMax(geom.height(), switchSize.height() + 1)); setGeometry(geom); } setMinimumSize(QSize(qMax((int)KIconLoader::SizeEnormous, switchSize.width() + 1), qMax((int)KIconLoader::SizeEnormous, switchSize.height() + 1))); foreach (QObject *child, m_appletInterface->children()) { //find for the needed property of Layout: minimum/maximum/preferred sizes and fillWidth/fillHeight if (child->property("minimumWidth").isValid() && child->property("minimumHeight").isValid() && child->property("preferredWidth").isValid() && child->property("preferredHeight").isValid() && child->property("maximumWidth").isValid() && child->property("maximumHeight").isValid() && child->property("fillWidth").isValid() && child->property("fillHeight").isValid() ) { m_layout = child; } } if (m_layout) { connect(m_layout, SIGNAL(minimumWidthChanged()), this, SLOT(minimumWidthChanged())); connect(m_layout, SIGNAL(minimumHeightChanged()), this, SLOT(minimumHeightChanged())); } minimumWidthChanged(); minimumHeightChanged(); QObject::connect(applet->containment(), &Plasma::Containment::configureRequested, this, &PlasmaWindowedView::showConfigurationInterface); Q_ASSERT(!m_statusNotifier); if (m_withStatusNotifier) { m_statusNotifier = new KStatusNotifierItem(this); m_statusNotifier->setIconByName(applet->icon()); m_statusNotifier->setTitle(applet->title()); m_statusNotifier->setToolTipTitle(applet->title()); connect(m_statusNotifier, &KStatusNotifierItem::activateRequested, this, [this](bool active, const QPoint& /*pos*/){ setVisible(active); if (active) { raise(); } }); } } void PlasmaWindowedView::resizeEvent(QResizeEvent *ev) { if (!m_applet) { return; } QQuickItem *i = m_applet->property("_plasma_graphicObject").value(); if (!i) { return; } minimumWidthChanged(); minimumHeightChanged(); i->setWidth(ev->size().width()); i->setHeight(ev->size().height()); contentItem()->setWidth(ev->size().width()); contentItem()->setHeight(ev->size().height()); m_applet->config().writeEntry("geometry", QRect(position(), ev->size())); } void PlasmaWindowedView::mouseReleaseEvent(QMouseEvent *ev) { QQuickWindow::mouseReleaseEvent(ev); if ((!(ev->buttons() & Qt::RightButton) && ev->button() != Qt::RightButton) || ev->isAccepted()) { return; } QMenu menu; foreach (QAction *action, m_applet->contextualActions()) { if (action) { menu.addAction(action); } } if (!m_applet->failedToLaunch()) { QAction *runAssociatedApplication = m_applet->actions()->action(QStringLiteral("run associated application")); if (runAssociatedApplication && runAssociatedApplication->isEnabled()) { menu.addAction(runAssociatedApplication); } QAction *configureApplet = m_applet->actions()->action(QStringLiteral("configure")); if (configureApplet && configureApplet->isEnabled()) { menu.addAction(configureApplet); } } menu.exec(ev->globalPos()); ev->setAccepted(true); } +void PlasmaWindowedView::keyPressEvent(QKeyEvent *ev) +{ + if (ev->matches(QKeySequence::Quit)) { + close(); + } + QQuickView::keyReleaseEvent(ev); +} + void PlasmaWindowedView::moveEvent(QMoveEvent *ev) { Q_UNUSED(ev) m_applet->config().writeEntry("geometry", QRect(position(), size())); } void PlasmaWindowedView::hideEvent(QHideEvent *ev) { Q_UNUSED(ev) m_applet->config().sync(); if (!m_withStatusNotifier) { m_applet->deleteLater(); deleteLater(); } } void PlasmaWindowedView::showConfigurationInterface(Plasma::Applet *applet) { if (m_configView) { m_configView->hide(); m_configView->deleteLater(); } if (!applet || !applet->containment()) { return; } m_configView = new PlasmaQuick::ConfigView(applet); m_configView->init(); m_configView->show(); } void PlasmaWindowedView::minimumWidthChanged() { if (!m_layout || !m_appletInterface) { return; } setMinimumWidth(qMax(m_appletInterface->property("switchWidth").toInt() + 1, qMax((int)KIconLoader::SizeEnormous, m_layout->property("minimumWidth").toInt()))); } void PlasmaWindowedView::minimumHeightChanged() { if (!m_layout || !m_appletInterface) { return; } setMinimumHeight(qMax(m_appletInterface->property("switchHeight").toInt() + 1, qMax((int)KIconLoader::SizeEnormous, m_layout->property("minimumHeight").toInt()))); } void PlasmaWindowedView::maximumWidthChanged() { if (!m_layout) { return; } setMaximumWidth(m_layout->property("maximumWidth").toInt()); } void PlasmaWindowedView::maximumHeightChanged() { if (!m_layout) { return; } setMaximumHeight(m_layout->property("maximumHeight").toInt()); } diff --git a/plasma-windowed/plasmawindowedview.h b/plasma-windowed/plasmawindowedview.h index 538a37dcf..f9dc07962 100644 --- a/plasma-windowed/plasmawindowedview.h +++ b/plasma-windowed/plasmawindowedview.h @@ -1,67 +1,68 @@ /* * Copyright 2014 Bhushan Shah * Copyright 2014 Marco Martin * * 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 PLASMAWINDOWEDVIEW_H #define PLASMAWINDOWEDVIEW_H #include #include #include "plasmawindowedcorona.h" #include class KStatusNotifierItem; class PlasmaWindowedView : public QQuickView { Q_OBJECT public: PlasmaWindowedView(QWindow *parent = 0); ~PlasmaWindowedView() override; void setApplet(Plasma::Applet *applet); void setHasStatusNotifier(bool stay); protected: void resizeEvent(QResizeEvent * ev) override; void mouseReleaseEvent(QMouseEvent *ev) override; void moveEvent(QMoveEvent *ev) override; void hideEvent(QHideEvent *ev) override; + void keyPressEvent(QKeyEvent *ev) override; protected Q_SLOTS: void showConfigurationInterface(Plasma::Applet *applet); void minimumWidthChanged(); void minimumHeightChanged(); void maximumWidthChanged(); void maximumHeightChanged(); private: Plasma::Applet *m_applet; QPointer m_layout; QPointer m_configView; QPointer m_rootObject; QPointer m_appletInterface; KStatusNotifierItem* m_statusNotifier; bool m_withStatusNotifier; }; #endif