diff --git a/applets/digital-clock/package/contents/ui/DigitalClock.qml b/applets/digital-clock/package/contents/ui/DigitalClock.qml --- a/applets/digital-clock/package/contents/ui/DigitalClock.qml +++ b/applets/digital-clock/package/contents/ui/DigitalClock.qml @@ -69,6 +69,12 @@ onShowDateChanged: { timeFormatCorrection(Qt.locale().timeFormat(Locale.ShortFormat)) } onUse24hFormatChanged: { timeFormatCorrection(Qt.locale().timeFormat(Locale.ShortFormat)) } + Binding { + target: ClipboardMenu + property: "currentDate" + value: main.currentTime + } + Connections { target: plasmoid.configuration onSelectedTimeZonesChanged: { diff --git a/applets/digital-clock/package/contents/ui/main.qml b/applets/digital-clock/package/contents/ui/main.qml --- a/applets/digital-clock/package/contents/ui/main.qml +++ b/applets/digital-clock/package/contents/ui/main.qml @@ -83,6 +83,9 @@ } Component.onCompleted: { + plasmoid.setAction("clipboard", i18n("Copy to Clipboard"), "edit-copy"); + ClipboardMenu.setupMenu(plasmoid.action("clipboard")); + plasmoid.setAction("clockkcm", i18n("Adjust Date and Time..."), "preferences-system-time"); plasmoid.setAction("formatskcm", i18n("Set Time Format...")); } diff --git a/applets/digital-clock/plugin/CMakeLists.txt b/applets/digital-clock/plugin/CMakeLists.txt --- a/applets/digital-clock/plugin/CMakeLists.txt +++ b/applets/digital-clock/plugin/CMakeLists.txt @@ -11,11 +11,13 @@ timezonemodel.cpp timezonesi18n.cpp digitalclockplugin.cpp + clipboardmenu.cpp ) add_library(digitalclockplugin SHARED ${digitalclockplugin_SRCS}) target_link_libraries(digitalclockplugin Qt5::Core Qt5::Qml + Qt5::Widgets # for QAction... KF5::CoreAddons KF5::I18n) diff --git a/applets/digital-clock/plugin/clipboardmenu.h b/applets/digital-clock/plugin/clipboardmenu.h new file mode 100644 --- /dev/null +++ b/applets/digital-clock/plugin/clipboardmenu.h @@ -0,0 +1,54 @@ +/* + * Copyright 2016 Kai Uwe Broulik + * + * 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 CLIPBOARDMENU_H +#define CLIPBOARDMENU_H + +#include +#include + +class QAction; + +class ClipboardMenu : public QObject +{ + Q_OBJECT + + Q_PROPERTY(QDateTime currentDate READ currentDate WRITE setCurrentDate NOTIFY currentDateChanged) + +public: + explicit ClipboardMenu(QObject *parent = nullptr); + virtual ~ClipboardMenu(); + + QDateTime currentDate() const; + void setCurrentDate(const QDateTime &date); + + Q_INVOKABLE void setupMenu(QAction *action); + +signals: + void currentDateChanged(); + +private: + QDateTime m_currentDate; + + +}; + +#endif // CLIPBOARDMENU_H diff --git a/applets/digital-clock/plugin/clipboardmenu.cpp b/applets/digital-clock/plugin/clipboardmenu.cpp new file mode 100644 --- /dev/null +++ b/applets/digital-clock/plugin/clipboardmenu.cpp @@ -0,0 +1,75 @@ +/* + * Copyright 2016 Kai Uwe Broulik + * + * 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 "clipboardmenu.h" + +#include +#include +#include + +ClipboardMenu::ClipboardMenu(QObject *parent) : QObject(parent) +{ + +} + +ClipboardMenu::~ClipboardMenu() = default; + +QDateTime ClipboardMenu::currentDate() const +{ + return m_currentDate; +} + +void ClipboardMenu::setCurrentDate(const QDateTime ¤tDate) +{ + if (m_currentDate != currentDate) { + m_currentDate = currentDate; + emit currentDateChanged(); + } +} + +void ClipboardMenu::setupMenu(QAction *action) +{ + QMenu *menu = new QMenu; + connect(menu, &QMenu::aboutToShow, this, [this, menu] { + menu->clear(); + + const QDate date = m_currentDate.date(); + const QTime time = m_currentDate.time(); + + menu->addAction(date.toString(Qt::SystemLocaleLongDate)); + menu->addAction(date.toString(Qt::SystemLocaleShortDate)); + menu->addAction(m_currentDate.toString(Qt::SystemLocaleShortDate)); + menu->addAction(date.toString(Qt::ISODate)); + menu->addSeparator(); + menu->addAction(time.toString(Qt::SystemLocaleShortDate)); + menu->addAction(time.toString(Qt::SystemLocaleLongDate)); + }); + connect(menu, &QMenu::triggered, menu, [this](QAction *action) { + qApp->clipboard()->setText(action->text()); + }); + + // QMenu cannot have QAction as parent and setMenu doesn't take ownership + connect(action, &QObject::destroyed, this, [menu] { + menu->deleteLater(); + }); + + action->setMenu(menu); +} diff --git a/applets/digital-clock/plugin/digitalclockplugin.cpp b/applets/digital-clock/plugin/digitalclockplugin.cpp --- a/applets/digital-clock/plugin/digitalclockplugin.cpp +++ b/applets/digital-clock/plugin/digitalclockplugin.cpp @@ -17,6 +17,7 @@ */ #include "digitalclockplugin.h" +#include "clipboardmenu.h" #include "timezonemodel.h" #include "timezonesi18n.h" @@ -30,6 +31,14 @@ return new TimezonesI18n(); } +static QObject *clipboardMenu_singletontype_provider(QQmlEngine *engine, QJSEngine *scriptEngine) +{ + Q_UNUSED(engine); + Q_UNUSED(scriptEngine); + + return new ClipboardMenu(); +} + void DigitalClockPlugin::registerTypes(const char *uri) { Q_ASSERT(uri == QLatin1String("org.kde.plasma.private.digitalclock")); @@ -37,4 +46,6 @@ qmlRegisterType(uri, 1, 0, "TimeZoneModel"); qmlRegisterType(uri, 1, 0, "TimeZoneFilterProxy"); qmlRegisterSingletonType(uri, 1, 0, "TimezonesI18n", timezonesi18n_singletontype_provider); + + qmlRegisterSingletonType(uri, 1, 0, "ClipboardMenu", clipboardMenu_singletontype_provider); }