Skip to content

Commit

Permalink
chore: add the dde-clipboard plugin
Browse files Browse the repository at this point in the history
add the dde-clipboard plugin

Log:
  • Loading branch information
wangfei committed Oct 29, 2024
1 parent d7dc745 commit 4494faf
Show file tree
Hide file tree
Showing 20 changed files with 1,038 additions and 1 deletion.
36 changes: 36 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ endif ()
find_package(PkgConfig REQUIRED)
find_package(Dtk COMPONENTS Widget Core REQUIRED)
find_package(Qt5 COMPONENTS Core Gui Widgets DBus Test Concurrent REQUIRED)
find_package(DdeTrayLoader REQUIRED)

find_package(ECM REQUIRED NO_MODULE)
set(CMAKE_MODULE_PATH ${ECM_MODULE_PATH})
Expand Down Expand Up @@ -246,3 +247,38 @@ target_link_libraries(${UT_BIN_NAME} PRIVATE
-lgcov
-lgtest
)

#--------------------------dock-plugin---------------------------
set(PLUGIN_NAME dock-clipboard-plugin)

file(GLOB_RECURSE SRCS
"dock-clipboard-plugin/*.h"
"dock-clipboard-plugin/*.cpp"
"dock-clipboard-plugin/widget/*.h"
"dock-clipboard-plugin/widget/*.cpp"
)

add_definitions("${QT_DEFINITIONS} -DQT_PLUGIN")
add_library(${PLUGIN_NAME} SHARED ${SRCS} dock-clipboard-plugin/resources/clipboard.qrc)
target_include_directories(${PLUGIN_NAME} PUBLIC
${DtkWidget_INCLUDE_DIRS}
${DtkCore_INCLUDE_DIRS}
${OBJECT_BINARY_DIR}
${DDE_DOCK_INCLUDE_DIR}
${QGSettings_INCLUDE_DIRS}
${DFrameworkDBus_INCLUDE_DIRS}
dock-clipboard-plugin/widget/
)
target_link_libraries(${PLUGIN_NAME} PRIVATE
${DtkWidget_LIBRARIES}
${QGSettings_LIBRARIES}
${DFrameworkDBus_LIBRARIES}
${Qt5DBus_LIBRARIES}
${Qt5Widgets_LIBRARIES}
${Qt5Svg_LIBRARIES}
${DtkCore_LIBRARIES}
)

# 设置执行 make install 时哪个目标应该被 install 到哪个位置
install(TARGETS ${PLUGIN_NAME} LIBRARY DESTINATION lib/dde-dock/plugins)
install(FILES "dock-clipboard-plugin/resources/clipboard.svg" DESTINATION share/dde-dock/icons/dcc-setting)
2 changes: 1 addition & 1 deletion dde-clipboard/dbus/iteminfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
#include <QDateTime>
#include <QUrl>

#include "constants.h"
#include "dde-clipboard/constants.h"

enum DataType {
Unknown,
Expand Down
3 changes: 3 additions & 0 deletions dock-clipboard-plugin/clipboard.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"api": "2.0.0"
}
37 changes: 37 additions & 0 deletions dock-clipboard-plugin/clipboardcontroller.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// SPDX-FileCopyrightText: 2019 - 2023 UnionTech Software Technology Co., Ltd.
//
// SPDX-License-Identifier: LGPL-3.0-or-later

#ifndef CLIPBOARDCONTROLLER_H
#define CLIPBOARDCONTROLLER_H

#include <DSingleton>

#include <QDBusInterface>
#include <QObject>

class ClipboardController : public QObject, public Dtk::Core::DSingleton<ClipboardController>
{
friend Dtk::Core::DSingleton<ClipboardController>;

Q_OBJECT
public:
ClipboardController()
{
}

~ClipboardController() { }

void toggle()
{
QDBusInterface interface("org.deepin.dde.ClipboardLoader1",
"/org/deepin/dde/ClipboardLoader1",
"org.deepin.dde.ClipboardLoader1",
QDBusConnection::sessionBus());
if (interface.isValid()) {
interface.call("quit");
}
}
};

#endif
107 changes: 107 additions & 0 deletions dock-clipboard-plugin/clipboarditem.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
// SPDX-FileCopyrightText: 2020 - 2022 UnionTech Software Technology Co., Ltd.
//
// SPDX-License-Identifier: LGPL-3.0-or-later

#include "clipboarditem.h"
#include "constants.h"
#include "clipboardcontroller.h"

#include <DDBusSender>
#include <DGuiApplicationHelper>

#include <QDBusConnection>
#include <QIcon>
#include <QJsonDocument>
#include <QPainter>
#include <QVBoxLayout>
#include <QMouseEvent>

DGUI_USE_NAMESPACE

#define SHIFT "shift"

ClipboardItem::ClipboardItem(QWidget* parent)
: QWidget(parent)
, m_tipsLabel(new TipsWidget(this))
, m_icon(new CommonIconButton(this))
{
init();
}

void ClipboardItem::init()
{
m_tipsLabel->setVisible(false);
m_tipsLabel->setText(tr("Clipboard"));

m_icon->setFixedSize(Dock::DOCK_PLUGIN_ITEM_FIXED_SIZE);
m_icon->setIcon(QIcon::fromTheme("clipboard"));
m_icon->setClickable(true);

auto vLayout = new QVBoxLayout(this);
vLayout->setSpacing(0);
vLayout->setMargin(0);
vLayout->addWidget(m_icon, 0, Qt::AlignCenter);

refreshIcon();

connect(DGuiApplicationHelper::instance(), &DGuiApplicationHelper::themeTypeChanged, this, &ClipboardItem::refreshIcon);
connect(m_icon, &CommonIconButton::clicked, this, [] {
ClipboardController::ref().toggle();
});
}

QWidget* ClipboardItem::tipsWidget()
{
return m_tipsLabel;
}

const QString ClipboardItem::contextMenu() const
{
QList<QVariant> items;
items.reserve(1);

QMap<QString, QVariant> shift;
shift["itemId"] = SHIFT;
shift["itemText"] = tr("Open");
shift["isActive"] = true;
items.push_back(shift);

QMap<QString, QVariant> menu;
menu["items"] = items;
menu["checkableMenu"] = false;
menu["singleCheck"] = false;

return QJsonDocument::fromVariant(menu).toJson();
}

void ClipboardItem::invokeMenuItem(const QString menuId, const bool checked)

Check warning on line 77 in dock-clipboard-plugin/clipboarditem.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Parameter 'menuId' is passed by value. It could be passed as a const reference which is usually faster and recommended in C++.
{
Q_UNUSED(menuId);
Q_UNUSED(checked);

if (menuId == SHIFT) {
ClipboardController::ref().toggle();
Q_EMIT requestHideApplet();
}
}

void ClipboardItem::refreshIcon()
{
m_icon->setState(CommonIconButton::Default);
}

void ClipboardItem::resizeEvent(QResizeEvent* e)
{
QWidget::resizeEvent(e);

const Dock::Position position = qApp->property(PROP_POSITION).value<Dock::Position>();
if (position == Dock::Bottom || position == Dock::Top) {
setMaximumWidth(height());
setMaximumHeight(QWIDGETSIZE_MAX);
} else {
setMaximumHeight(width());
setMaximumWidth(QWIDGETSIZE_MAX);
}

refreshIcon();
}
40 changes: 40 additions & 0 deletions dock-clipboard-plugin/clipboarditem.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// SPDX-FileCopyrightText: 2020 - 2022 UnionTech Software Technology Co., Ltd.
//
// SPDX-License-Identifier: LGPL-3.0-or-later

#ifndef CLIPBOARDITEM_H
#define CLIPBOARDITEM_H

#include "commoniconbutton.h"
#include "widget/tipswidget.h"

#include <QWidget>

class ClipboardItem : public QWidget
{
Q_OBJECT

public:
explicit ClipboardItem(QWidget *parent = nullptr);

QWidget *tipsWidget();
const QString contextMenu() const;
void invokeMenuItem(const QString menuId, const bool checked);

Check warning on line 22 in dock-clipboard-plugin/clipboarditem.h

View workflow job for this annotation

GitHub Actions / cppcheck

Parameter 'menuId' is passed by value. It could be passed as a const reference which is usually faster and recommended in C++.
void refreshIcon();

protected:
void resizeEvent(QResizeEvent *e) override;

signals:
void requestHideApplet();

private:
void init();

private:
TipsWidget *m_tipsLabel;
CommonIconButton *m_icon;
QPixmap m_iconPixmap;
};

#endif // CLIPBOARDITEM_H
151 changes: 151 additions & 0 deletions dock-clipboard-plugin/clipboardplugin.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
// SPDX-FileCopyrightText: 2020 - 2022 UnionTech Software Technology Co., Ltd.
//
// SPDX-License-Identifier: LGPL-3.0-or-later

#include "clipboardplugin.h"

#include <QDBusConnection>

#define CLIPBOARD_KEY "clipboard-key"
DCORE_USE_NAMESPACE

ClipboardPlugin::ClipboardPlugin(QObject *parent)
: QObject(parent)
, m_quickPanelWidget(nullptr)
, m_item(nullptr)
, m_messageCallback(nullptr)
{
QTranslator *translator = new QTranslator(this);
translator->load(QString("/usr/share/dde-clipboard/translations/dde-clipboard_%1.qm").arg(QLocale::system().name()));
QCoreApplication::installTranslator(translator);
}

ClipboardPlugin::~ClipboardPlugin()
{
if (m_quickPanelWidget) {
delete m_quickPanelWidget;
m_quickPanelWidget = nullptr;
}

if (m_item) {
delete m_item;
m_item = nullptr;
}
}

const QString ClipboardPlugin::pluginName() const
{
return "clipboard";
}

const QString ClipboardPlugin::pluginDisplayName() const
{
return tr("Clipboard");
}

void ClipboardPlugin::init(PluginProxyInterface *proxyInter)
{
m_proxyInter = proxyInter;

if (m_item || m_quickPanelWidget) {
return;
}
m_item = new ClipboardItem();
m_quickPanelWidget = new QuickPanelWidget();

m_proxyInter->itemAdded(this, CLIPBOARD_KEY);
m_quickPanelWidget->setDescription(pluginDisplayName());
m_quickPanelWidget->setIcon(QIcon::fromTheme("clipboard"));

connect(m_item, &ClipboardItem::requestHideApplet, this, [this] {
m_proxyInter->requestSetAppletVisible(this, CLIPBOARD_KEY, false);
});
connect(m_quickPanelWidget, &QuickPanelWidget::requestHideApplet, this, [this] {
m_proxyInter->requestSetAppletVisible(this, CLIPBOARD_KEY, false);
});
QDBusConnection::sessionBus().connect("com.deepin.dde.Clipboard", "/com/deepin/dde/Clipboard", "com.deepin.dde.Clipboard", "clipboardVisibleChanged", this, SLOT(onClipboardVisibleChanged(bool)));
}

QWidget *ClipboardPlugin::itemWidget(const QString &itemKey)

Check warning on line 69 in dock-clipboard-plugin/clipboardplugin.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

The function 'itemWidget' is never used.
{
if (itemKey == CLIPBOARD_KEY) {
return m_item;
}

if (itemKey == Dock::QUICK_ITEM_KEY) {
return m_quickPanelWidget;
}

return nullptr;
}

QWidget *ClipboardPlugin::itemTipsWidget(const QString &itemKey)

Check warning on line 82 in dock-clipboard-plugin/clipboardplugin.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

The function 'itemTipsWidget' is never used.
{
if (itemKey == CLIPBOARD_KEY) {
return m_item->tipsWidget();
}

return nullptr;
}

QWidget *ClipboardPlugin::itemPopupApplet(const QString &itemKey)

Check warning on line 91 in dock-clipboard-plugin/clipboardplugin.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

The function 'itemPopupApplet' is never used.
{
return nullptr;
}

const QString ClipboardPlugin::itemContextMenu(const QString &itemKey)

Check warning on line 96 in dock-clipboard-plugin/clipboardplugin.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

The function 'itemContextMenu' is never used.
{
if (itemKey == CLIPBOARD_KEY) {
return m_item->contextMenu();
}

return QString();
}

void ClipboardPlugin::invokedMenuItem(const QString &itemKey, const QString &menuId, const bool checked)

Check warning on line 105 in dock-clipboard-plugin/clipboardplugin.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

The function 'invokedMenuItem' is never used.
{
if (itemKey == CLIPBOARD_KEY) {
m_item->invokeMenuItem(menuId, checked);
}
}

int ClipboardPlugin::itemSortKey(const QString &itemKey)

Check warning on line 112 in dock-clipboard-plugin/clipboardplugin.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

The function 'itemSortKey' is never used.
{
const QString key = QString("pos_%1_%2").arg(itemKey).arg(Dock::Efficient);

return m_proxyInter->getValue(this, key, -1).toInt();
}

void ClipboardPlugin::setSortKey(const QString &itemKey, const int order)
{
const QString key = QString("pos_%1_%2").arg(itemKey).arg(Dock::Efficient);

m_proxyInter->saveValue(this, key, order);
}

void ClipboardPlugin::refreshIcon(const QString &itemKey)
{
if (itemKey == CLIPBOARD_KEY) {
m_item->refreshIcon();
}
}

const QString ClipboardPlugin::itemCommand(const QString &itemKey)

Check warning on line 133 in dock-clipboard-plugin/clipboardplugin.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

The function 'itemCommand' is never used.
{
Q_UNUSED(itemKey);
return QString("dbus-send --session --print-reply --dest=org.deepin.dde.Clipboard1 /org/deepin/dde/Clipboard1 org.deepin.dde.Clipboard1.Toggle");
}

void ClipboardPlugin::onClipboardVisibleChanged(bool visible)

Check warning on line 139 in dock-clipboard-plugin/clipboardplugin.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

The function 'onClipboardVisibleChanged' is never used.
{
if (!m_messageCallback) {
qWarning() << "Message callback function is nullptr";
return;
}
QJsonObject msg;
msg[Dock::MSG_TYPE] = Dock::MSG_ITEM_ACTIVE_STATE;
msg[Dock::MSG_DATA] = visible;
QJsonDocument doc;
doc.setObject(msg);
m_messageCallback(this, doc.toJson());
}
Loading

0 comments on commit 4494faf

Please sign in to comment.