From 30471d4148a251f76279e941a458a6baed81f106 Mon Sep 17 00:00:00 2001 From: Linus Jahn Date: Thu, 6 Feb 2020 18:17:47 +0100 Subject: [PATCH] Add screenshot option to panel --- containments/panel/CMakeLists.txt | 4 + .../panel/dbus/org.kde.KWin.Screenshot.xml | 114 ++++++++++++++++++ .../ui/quicksettings/QuickSettings.qml | 22 ++++ containments/panel/phonepanel.cpp | 53 ++++++++ containments/panel/phonepanel.h | 1 + 5 files changed, 194 insertions(+) create mode 100644 containments/panel/dbus/org.kde.KWin.Screenshot.xml diff --git a/containments/panel/CMakeLists.txt b/containments/panel/CMakeLists.txt index 7de0fd4c..49cb9c91 100644 --- a/containments/panel/CMakeLists.txt +++ b/containments/panel/CMakeLists.txt @@ -1,5 +1,8 @@ +qt5_add_dbus_interfaces(DBUS_SRCS dbus/org.kde.KWin.Screenshot.xml) + set(phonepanel_SRCS phonepanel.cpp + ${DBUS_SRCS} ) add_library(plasma_applet_phonepanel MODULE ${phonepanel_SRCS}) @@ -8,6 +11,7 @@ kcoreaddons_desktop_to_json(plasma_applet_phonepanel package/metadata.desktop) target_link_libraries(plasma_applet_phonepanel Qt5::Gui + Qt5::DBus KF5::Plasma KF5::I18n ${GSTREAMER_LIBRARIES} GLIB2::GLIB2 ${GOBJECT_LIBRARIES}) diff --git a/containments/panel/dbus/org.kde.KWin.Screenshot.xml b/containments/panel/dbus/org.kde.KWin.Screenshot.xml new file mode 100644 index 00000000..0d2d92cb --- /dev/null +++ b/containments/panel/dbus/org.kde.KWin.Screenshot.xml @@ -0,0 +1,114 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/containments/panel/package/contents/ui/quicksettings/QuickSettings.qml b/containments/panel/package/contents/ui/quicksettings/QuickSettings.qml index 0c7c7ade..82ec7ae8 100644 --- a/containments/panel/package/contents/ui/quicksettings/QuickSettings.qml +++ b/containments/panel/package/contents/ui/quicksettings/QuickSettings.qml @@ -31,6 +31,8 @@ Item { signal closeRequested signal closed + property bool screenshotRequested: false + function toggleAirplane() { print("toggle airplane mode") } @@ -73,6 +75,18 @@ Item { } } + function requestScreenshot() { + screenshotRequested = true; + root.closeRequested(); + } + + onClosed: { + if (screenshotRequested) { + plasmoid.nativeInterface.takeScreenshot(); + screenshotRequested = false; + } + } + PlasmaCore.DataSource { id: pmSource engine: "powermanagement" @@ -160,6 +174,14 @@ Item { "settingsCommand": "", "applet": null }); + settingsModel.append({ + "text": i18n("Screenshot"), + "icon": "spectacle", + "enabled": false, + "settingsCommand": "", + "toggleFunction": "requestScreenshot", + "applet": null + }); brightnessSlider.moved.connect(function() { root.screenBrightness = brightnessSlider.value; diff --git a/containments/panel/phonepanel.cpp b/containments/panel/phonepanel.cpp index b1816a5b..3455fffc 100644 --- a/containments/panel/phonepanel.cpp +++ b/containments/panel/phonepanel.cpp @@ -20,8 +20,15 @@ #include "phonepanel.h" +#include +#include +#include #include +#include #include +#include + +#include "screenshotinterface.h" PhonePanel::PhonePanel(QObject *parent, const QVariantList &args) : Plasma::Containment(parent, args) @@ -71,6 +78,52 @@ void PhonePanel::toggleTorch() } } +void PhonePanel::takeScreenshot() +{ + auto *interface = new org::kde::kwin::Screenshot(QStringLiteral("org.kde.KWin"), QStringLiteral("/Screenshot"), QDBusConnection::sessionBus(), this); + QDBusPendingReply reply = interface->screenshotFullscreen(); + QDBusPendingCallWatcher *watcher = new QDBusPendingCallWatcher(reply, this); + + connect(watcher, &QDBusPendingCallWatcher::finished, this, [=](QDBusPendingCallWatcher *watcher) { + QDBusPendingReply reply = *watcher; + + if (reply.isError()) { + qWarning() << "Creating the screenshot failed:" << reply.error().name() << reply.error().message(); + } else { + QString filePath = QStandardPaths::writableLocation(QStandardPaths::PicturesLocation); + if (filePath.isEmpty()) { + qWarning() << "Couldn't find a writable location for the screenshot! The screenshot is in /tmp."; + return; + } + + QDir picturesDir(filePath); + if (!picturesDir.mkpath(QStringLiteral("Screenshots"))) { + qWarning() << "Couldn't create folder at" + << picturesDir.path() + QStringLiteral("/Screenshots") + << "to take screenshot."; + return; + } + + filePath += QStringLiteral("/Screenshots/Screenshot_%1.png") + .arg(QDateTime::currentDateTime().toString(QStringLiteral("yyyyMMdd_hhmmss"))); + + const QString currentPath = reply.argumentAt<0>(); + QtConcurrent::run(QThreadPool::globalInstance(), [=]() { + QFile screenshotFile(currentPath); + if (!screenshotFile.rename(filePath)) { + qWarning() << "Couldn't move screenshot into Pictures folder:" + << screenshotFile.errorString(); + } + + qDebug() << "Successfully saved screenshot at" << filePath; + }); + } + + watcher->deleteLater(); + interface->deleteLater(); + }); +} + K_EXPORT_PLASMA_APPLET_WITH_JSON(quicksettings, PhonePanel, "metadata.json") #include "phonepanel.moc" diff --git a/containments/panel/phonepanel.h b/containments/panel/phonepanel.h index 205b1f35..08c279a5 100644 --- a/containments/panel/phonepanel.h +++ b/containments/panel/phonepanel.h @@ -38,6 +38,7 @@ public: public Q_SLOTS: void executeCommand(const QString &command); void toggleTorch(); + void takeScreenshot(); private: GstElement* m_pipeline;