From 06d55e20d636812a920d5fcdbf691e9b8e77dbe2 Mon Sep 17 00:00:00 2001 From: listenerri Date: Fri, 21 Dec 2018 17:28:21 +0800 Subject: [PATCH] feat: add new plugin for onboard https://github.com/linuxdeepin/internal-discussion/issues/610 Change-Id: Iada7d06f804a6a7d6df7078781f93be08694bce4 --- debian/control | 8 +- debian/dde-dock-onboard-plugin.install | 1 + debian/dde-dock.install | 7 +- plugins/CMakeLists.txt | 1 + plugins/onboard/CMakeLists.txt | 27 ++++ plugins/onboard/onboard.json | 3 + plugins/onboard/onboard.qrc | 4 + plugins/onboard/onboarditem.cpp | 99 ++++++++++++++ plugins/onboard/onboarditem.h | 54 ++++++++ plugins/onboard/onboardplugin.cpp | 174 +++++++++++++++++++++++++ plugins/onboard/onboardplugin.h | 69 ++++++++++ plugins/shutdown/shutdownplugin.cpp | 2 +- 12 files changed, 446 insertions(+), 3 deletions(-) create mode 100644 debian/dde-dock-onboard-plugin.install create mode 100644 plugins/onboard/CMakeLists.txt create mode 100644 plugins/onboard/onboard.json create mode 100644 plugins/onboard/onboard.qrc create mode 100644 plugins/onboard/onboarditem.cpp create mode 100644 plugins/onboard/onboarditem.h create mode 100644 plugins/onboard/onboardplugin.cpp create mode 100644 plugins/onboard/onboardplugin.h diff --git a/debian/control b/debian/control index 750b3e6bc..496247cb7 100644 --- a/debian/control +++ b/debian/control @@ -19,7 +19,7 @@ Homepage: http://www.deepin.org/ Package: dde-dock Architecture: any Depends: ${shlibs:Depends}, ${misc:Depends}, deepin-menu -Recommends: dde-disk-mount-plugin +Recommends: dde-disk-mount-plugin, dde-dock-onboard-plugin Conflicts: dde-workspace (<< 2.90.5), dde-dock-applets, @@ -35,3 +35,9 @@ Architecture: all Depends: ${shlibs:Depends}, ${misc:Depends} Description: deepin desktop-environment - dock module development files Deepin desktop environment - dock module development files. + +Package: dde-dock-onboard-plugin +Architecture: all +Depends: ${shlibs:Depends}, ${misc:Depends}, onboard +Description: deepin desktop-environment - dock plugin + Deepin desktop environment - dock plugin for onboard diff --git a/debian/dde-dock-onboard-plugin.install b/debian/dde-dock-onboard-plugin.install new file mode 100644 index 000000000..f54f9c265 --- /dev/null +++ b/debian/dde-dock-onboard-plugin.install @@ -0,0 +1 @@ +/usr/lib/dde-dock/plugins/libonboard.so diff --git a/debian/dde-dock.install b/debian/dde-dock.install index 5b3ec7185..df239627e 100644 --- a/debian/dde-dock.install +++ b/debian/dde-dock.install @@ -1,4 +1,9 @@ /usr/share /usr/bin -/usr/lib/dde-dock +/usr/lib/dde-dock/plugins/libdatetime.so +/usr/lib/dde-dock/plugins/libkeyboard-layout.so +/usr/lib/dde-dock/plugins/libshutdown.so +/usr/lib/dde-dock/plugins/libtrash.so +/usr/lib/dde-dock/plugins/libtray.so +/usr/lib/dde-dock/plugins/system-trays /etc/dde-dock diff --git a/plugins/CMakeLists.txt b/plugins/CMakeLists.txt index b58cedd0e..f67eb62a2 100644 --- a/plugins/CMakeLists.txt +++ b/plugins/CMakeLists.txt @@ -8,3 +8,4 @@ add_subdirectory("sound") add_subdirectory("tray") add_subdirectory("trash") add_subdirectory("keyboard-layout") +add_subdirectory("onboard") diff --git a/plugins/onboard/CMakeLists.txt b/plugins/onboard/CMakeLists.txt new file mode 100644 index 000000000..1943820e5 --- /dev/null +++ b/plugins/onboard/CMakeLists.txt @@ -0,0 +1,27 @@ + +set(PLUGIN_NAME "onboard") + +project(${PLUGIN_NAME}) + +# Sources files +file(GLOB_RECURSE SRCS "*.h" "*.cpp" "../../widgets/*.h" "../../widgets/*.cpp") + +find_package(PkgConfig REQUIRED) +find_package(Qt5Widgets REQUIRED) +find_package(Qt5Svg REQUIRED) +find_package(Qt5DBus REQUIRED) +find_package(DtkWidget REQUIRED) + +add_definitions("${QT_DEFINITIONS} -DQT_PLUGIN") +add_library(${PLUGIN_NAME} SHARED ${SRCS} onboard.qrc) +set_target_properties(${PLUGIN_NAME} PROPERTIES LIBRARY_OUTPUT_DIRECTORY ../) +target_include_directories(${PLUGIN_NAME} PUBLIC ${DtkWidget_INCLUDE_DIRS} + ${Qt5DBus_INCLUDE_DIRS} + ../../interfaces) +target_link_libraries(${PLUGIN_NAME} PRIVATE + ${DtkWidget_LIBRARIES} + ${Qt5Widgets_LIBRARIES} + ${Qt5Svg_LIBRARIES} +) + +install(TARGETS ${PLUGIN_NAME} LIBRARY DESTINATION lib/dde-dock/plugins) diff --git a/plugins/onboard/onboard.json b/plugins/onboard/onboard.json new file mode 100644 index 000000000..a6508f326 --- /dev/null +++ b/plugins/onboard/onboard.json @@ -0,0 +1,3 @@ +{ + "api": "1.1" +} diff --git a/plugins/onboard/onboard.qrc b/plugins/onboard/onboard.qrc new file mode 100644 index 000000000..8e78458e6 --- /dev/null +++ b/plugins/onboard/onboard.qrc @@ -0,0 +1,4 @@ + + + + diff --git a/plugins/onboard/onboarditem.cpp b/plugins/onboard/onboarditem.cpp new file mode 100644 index 000000000..ccc6f6ecf --- /dev/null +++ b/plugins/onboard/onboarditem.cpp @@ -0,0 +1,99 @@ +/* + * Copyright (C) 2011 ~ 2018 Deepin Technology Co., Ltd. + * + * Author: listenerri + * + * Maintainer: listenerri + * + * 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 3 of the License, or + * any later version. + * + * 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 "onboarditem.h" + +#include +#include +#include +#include +#include + +OnboardItem::OnboardItem(QWidget *parent) + : QWidget(parent), + m_hover(false) +{ +} + +QSize OnboardItem::sizeHint() const +{ + return QSize(26, 26); +} + +void OnboardItem::paintEvent(QPaintEvent *e) +{ + Q_UNUSED(e); + + QPixmap pixmap; + QString iconName = "deepin-virtualkeyboard"; + int iconSize; + const Dock::DisplayMode displayMode = qApp->property(PROP_DISPLAY_MODE).value(); + + if (displayMode == Dock::Efficient) { + iconName = iconName + "-symbolic"; + iconSize = 16; + } else { + iconSize = std::min(width(), height()) * 0.8; + } + + pixmap = loadSvg(iconName, QSize(iconSize, iconSize)); + + QPainter painter(this); + painter.drawPixmap(rect().center() - pixmap.rect().center() / qApp->devicePixelRatio(), pixmap); +} + +void OnboardItem::mousePressEvent(QMouseEvent *e) +{ + if (e->button() != Qt::RightButton) + return QWidget::mousePressEvent(e); + + const QPoint p(e->pos() - rect().center()); + if (p.manhattanLength() < std::min(width(), height()) * 0.8 * 0.5) + { + emit requestContextMenu(); + return; + } + + return QWidget::mousePressEvent(e); +} + +void OnboardItem::enterEvent(QEvent *e) +{ + e->accept(); + m_hover = true; +} + +void OnboardItem::leaveEvent(QEvent *e) +{ + e->accept(); + m_hover = false; +} + +const QPixmap OnboardItem::loadSvg(const QString &fileName, const QSize &size) const +{ + const auto ratio = qApp->devicePixelRatio(); + + QPixmap pixmap; + pixmap = QIcon::fromTheme(fileName).pixmap(size * ratio); + pixmap.setDevicePixelRatio(ratio); + + return pixmap; +} diff --git a/plugins/onboard/onboarditem.h b/plugins/onboard/onboarditem.h new file mode 100644 index 000000000..05096f59d --- /dev/null +++ b/plugins/onboard/onboarditem.h @@ -0,0 +1,54 @@ +/* + * Copyright (C) 2011 ~ 2018 Deepin Technology Co., Ltd. + * + * Author: listenerri + * + * Maintainer: listenerri + * + * 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 3 of the License, or + * any later version. + * + * 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 ONBOARDITEM_H +#define ONBOARDITEM_H + +#include "constants.h" + +#include + +class OnboardItem : public QWidget +{ + Q_OBJECT + +public: + explicit OnboardItem(QWidget *parent = nullptr); + +signals: + void requestContextMenu() const; + +protected: + QSize sizeHint() const; + void paintEvent(QPaintEvent *e); + void mousePressEvent(QMouseEvent *e); + void enterEvent(QEvent *e); + void leaveEvent(QEvent *e); + +private: + const QPixmap loadSvg(const QString &fileName, const QSize &size) const; + +private: + bool m_hover; + Dock::DisplayMode m_displayMode; +}; + +#endif // ONBOARDITEM_H diff --git a/plugins/onboard/onboardplugin.cpp b/plugins/onboard/onboardplugin.cpp new file mode 100644 index 000000000..a59cb63b1 --- /dev/null +++ b/plugins/onboard/onboardplugin.cpp @@ -0,0 +1,174 @@ +/* + * Copyright (C) 2011 ~ 2018 Deepin Technology Co., Ltd. + * + * Author: listenerri + * + * Maintainer: listenerri + * + * 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 3 of the License, or + * any later version. + * + * 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 "onboardplugin.h" + +#include +#include + +#define PLUGIN_STATE_KEY "enable" + +OnboardPlugin::OnboardPlugin(QObject *parent) + : QObject(parent), + + m_pluginLoaded(false), + m_tipsLabel(new TipsWidget) +{ + m_tipsLabel->setText(tr("Onboard")); + m_tipsLabel->setVisible(false); +} + +const QString OnboardPlugin::pluginName() const +{ + return "onboard"; +} + +const QString OnboardPlugin::pluginDisplayName() const +{ + return tr("Onboard"); +} + +QWidget *OnboardPlugin::itemWidget(const QString &itemKey) +{ + Q_UNUSED(itemKey); + + return m_onboardItem; +} + +QWidget *OnboardPlugin::itemTipsWidget(const QString &itemKey) +{ + Q_UNUSED(itemKey); + + return m_tipsLabel; +} + +void OnboardPlugin::init(PluginProxyInterface *proxyInter) +{ + m_proxyInter = proxyInter; + + if (!pluginIsDisable()) { + loadPlugin(); + } +} + +void OnboardPlugin::pluginStateSwitched() +{ + m_proxyInter->saveValue(this, PLUGIN_STATE_KEY, pluginIsDisable()); + + if (pluginIsDisable()) + { + m_proxyInter->itemRemoved(this, pluginName()); + } else { + if (!m_pluginLoaded) { + loadPlugin(); + return; + } + m_proxyInter->itemAdded(this, pluginName()); + } +} + +bool OnboardPlugin::pluginIsDisable() +{ + return !m_proxyInter->getValue(this, PLUGIN_STATE_KEY, false).toBool(); +} + +const QString OnboardPlugin::itemCommand(const QString &itemKey) +{ + Q_UNUSED(itemKey); + + return QString("dbus-send --print-reply --dest=org.onboard.Onboard /org/onboard/Onboard/Keyboard org.onboard.Onboard.Keyboard.ToggleVisible"); +} + +const QString OnboardPlugin::itemContextMenu(const QString &itemKey) +{ + QList items; + + QMap onboardSettings; + onboardSettings["itemId"] = "onboard-settings"; + onboardSettings["itemText"] = tr("Settings"); + onboardSettings["isActive"] = true; + items.push_back(onboardSettings); + + QMap menu; + menu["items"] = items; + menu["checkableMenu"] = false; + menu["singleCheck"] = false; + + return QJsonDocument::fromVariant(menu).toJson(); +} + +void OnboardPlugin::invokedMenuItem(const QString &itemKey, const QString &menuId, const bool checked) +{ + Q_UNUSED(itemKey) + Q_UNUSED(checked) + + if (menuId == "onboard-settings") + QProcess::startDetached("onboard-settings"); +} + +void OnboardPlugin::displayModeChanged(const Dock::DisplayMode displayMode) +{ + Q_UNUSED(displayMode); + + if (!pluginIsDisable()) { + m_onboardItem->update(); + } +} + +int OnboardPlugin::itemSortKey(const QString &itemKey) +{ + Dock::DisplayMode mode = displayMode(); + const QString key = QString("pos_%1_%2").arg(itemKey).arg(mode); + + if (mode == Dock::DisplayMode::Fashion) { + return m_proxyInter->getValue(this, key, 1).toInt(); + } else { + return m_proxyInter->getValue(this, key, 4).toInt(); + } +} + +void OnboardPlugin::setSortKey(const QString &itemKey, const int order) +{ + const QString key = QString("pos_%1_%2").arg(itemKey).arg(displayMode()); + m_proxyInter->saveValue(this, key, order); +} + +void OnboardPlugin::requestContextMenu() +{ + m_proxyInter->requestContextMenu(this, pluginName()); +} + +void OnboardPlugin::loadPlugin() +{ + if (m_pluginLoaded) { + qDebug() << "onboard plugin has been loaded! return"; + return; + } + + m_pluginLoaded = true; + + m_onboardItem = new OnboardItem; + + connect(m_onboardItem, &OnboardItem::requestContextMenu, this, &OnboardPlugin::requestContextMenu); + + m_proxyInter->itemAdded(this, pluginName()); + displayModeChanged(displayMode()); +} diff --git a/plugins/onboard/onboardplugin.h b/plugins/onboard/onboardplugin.h new file mode 100644 index 000000000..4a9c7f060 --- /dev/null +++ b/plugins/onboard/onboardplugin.h @@ -0,0 +1,69 @@ +/* + * Copyright (C) 2011 ~ 2018 Deepin Technology Co., Ltd. + * + * Author: listenerri + * + * Maintainer: listenerri + * + * 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 3 of the License, or + * any later version. + * + * 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 ONBOARDPLUGIN_H +#define ONBOARDPLUGIN_H + +#include "pluginsiteminterface.h" +#include "onboarditem.h" +#include "../widgets/tipswidget.h" + +#include + +class OnboardPlugin : public QObject, PluginsItemInterface +{ + Q_OBJECT + Q_INTERFACES(PluginsItemInterface) + Q_PLUGIN_METADATA(IID "com.deepin.dock.PluginsItemInterface" FILE "onboard.json") + +public: + explicit OnboardPlugin(QObject *parent = 0); + + const QString pluginName() const override; + const QString pluginDisplayName() const override; + void init(PluginProxyInterface *proxyInter) override; + + void pluginStateSwitched() override; + bool pluginIsAllowDisable() override { return true; } + bool pluginIsDisable() override; + + QWidget *itemWidget(const QString &itemKey) override; + QWidget *itemTipsWidget(const QString &itemKey) override; + const QString itemCommand(const QString &itemKey) override; + const QString itemContextMenu(const QString &itemKey) override; + void invokedMenuItem(const QString &itemKey, const QString &menuId, const bool checked) override; + void displayModeChanged(const Dock::DisplayMode displayMode) override; + + int itemSortKey(const QString &itemKey) Q_DECL_OVERRIDE; + void setSortKey(const QString &itemKey, const int order) Q_DECL_OVERRIDE; + +private: + void requestContextMenu(); + void loadPlugin(); + +private: + bool m_pluginLoaded; + + OnboardItem *m_onboardItem; + TipsWidget *m_tipsLabel; +}; + +#endif // ONBOARDPLUGIN_H diff --git a/plugins/shutdown/shutdownplugin.cpp b/plugins/shutdown/shutdownplugin.cpp index 80a162b65..e8261c244 100644 --- a/plugins/shutdown/shutdownplugin.cpp +++ b/plugins/shutdown/shutdownplugin.cpp @@ -198,7 +198,7 @@ int ShutdownPlugin::itemSortKey(const QString &itemKey) if (mode == Dock::DisplayMode::Fashion) { return m_proxyInter->getValue(this, key, 2).toInt(); } else { - return m_proxyInter->getValue(this, key, 4).toInt(); + return m_proxyInter->getValue(this, key, 5).toInt(); } }