2017-09-18 14:33:44 +08:00
|
|
|
/*
|
2018-02-07 11:52:47 +08:00
|
|
|
* Copyright (C) 2011 ~ 2018 Deepin Technology Co., Ltd.
|
2017-09-18 14:33:44 +08:00
|
|
|
*
|
|
|
|
* Author: sbw <sbw@sbw.so>
|
|
|
|
*
|
|
|
|
* Maintainer: sbw <sbw@sbw.so>
|
|
|
|
*
|
|
|
|
* 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 <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2016-08-08 13:55:00 +08:00
|
|
|
#include "constants.h"
|
2016-06-15 17:44:38 +08:00
|
|
|
#include "pluginsitem.h"
|
2016-06-16 16:56:21 +08:00
|
|
|
#include "pluginsiteminterface.h"
|
2020-12-16 17:38:41 +08:00
|
|
|
#include "utils.h"
|
2016-06-27 20:16:35 +08:00
|
|
|
|
2022-05-17 20:57:09 +08:00
|
|
|
#include <DFontSizeManager>
|
|
|
|
|
2016-06-16 16:56:21 +08:00
|
|
|
#include <QPainter>
|
|
|
|
#include <QBoxLayout>
|
2016-06-27 10:50:00 +08:00
|
|
|
#include <QMouseEvent>
|
|
|
|
#include <QDrag>
|
|
|
|
#include <QMimeData>
|
2019-08-01 15:59:25 +08:00
|
|
|
#include <QGSettings>
|
2016-06-27 10:50:00 +08:00
|
|
|
|
|
|
|
#define PLUGIN_ITEM_DRAG_THRESHOLD 20
|
|
|
|
|
|
|
|
QPoint PluginsItem::MousePressPoint = QPoint();
|
2016-06-16 16:56:21 +08:00
|
|
|
|
2022-07-11 03:18:24 +00:00
|
|
|
PluginsItem::PluginsItem(PluginsItemInterface *const pluginInter, const QString &itemKey, const QJsonObject &jsonData, QWidget *parent)
|
2019-10-17 19:52:55 +08:00
|
|
|
: DockItem(parent)
|
|
|
|
, m_pluginInter(pluginInter)
|
|
|
|
, m_centralWidget(m_pluginInter->itemWidget(itemKey))
|
2022-07-11 03:18:24 +00:00
|
|
|
, m_jsonData(jsonData)
|
2019-10-17 19:52:55 +08:00
|
|
|
, m_itemKey(itemKey)
|
|
|
|
, m_dragging(false)
|
2021-05-19 14:37:06 +08:00
|
|
|
, m_gsettings(Utils::ModuleSettingsPtr(pluginInter->pluginName(), QByteArray(), this))
|
2016-06-16 16:56:21 +08:00
|
|
|
{
|
2017-04-24 15:41:06 +08:00
|
|
|
qDebug() << "load plugins item: " << pluginInter->pluginName() << itemKey << m_centralWidget;
|
|
|
|
|
2022-11-30 11:13:45 +08:00
|
|
|
if (m_centralWidget) {
|
|
|
|
m_centralWidget->setParent(this);
|
|
|
|
m_centralWidget->setVisible(true);
|
|
|
|
m_centralWidget->setObjectName(pluginInter->pluginName() + "-centralwidget");
|
|
|
|
m_centralWidget->installEventFilter(this);
|
2016-07-13 16:47:59 +08:00
|
|
|
|
2022-11-30 11:13:45 +08:00
|
|
|
QBoxLayout *hLayout = new QHBoxLayout;
|
|
|
|
hLayout->addWidget(m_centralWidget);
|
|
|
|
hLayout->setSpacing(0);
|
|
|
|
hLayout->setMargin(0);
|
2016-06-27 10:50:00 +08:00
|
|
|
|
2022-11-30 11:13:45 +08:00
|
|
|
setLayout(hLayout);
|
|
|
|
}
|
2020-05-08 11:26:09 +08:00
|
|
|
setAccessibleName(pluginInter->pluginName());
|
2016-06-27 10:50:00 +08:00
|
|
|
setAttribute(Qt::WA_TranslucentBackground);
|
2019-08-01 15:59:25 +08:00
|
|
|
|
2021-03-20 12:10:45 +08:00
|
|
|
if (m_gsettings)
|
|
|
|
connect(m_gsettings, &QGSettings::changed, this, &PluginsItem::onGSettingsChanged);
|
2016-06-15 17:44:38 +08:00
|
|
|
}
|
2016-06-24 14:59:56 +08:00
|
|
|
|
2016-06-28 19:35:19 +08:00
|
|
|
PluginsItem::~PluginsItem()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2016-06-24 14:59:56 +08:00
|
|
|
int PluginsItem::itemSortKey() const
|
|
|
|
{
|
|
|
|
return m_pluginInter->itemSortKey(m_itemKey);
|
|
|
|
}
|
|
|
|
|
2016-08-08 20:52:19 +08:00
|
|
|
void PluginsItem::setItemSortKey(const int order) const
|
|
|
|
{
|
|
|
|
m_pluginInter->setSortKey(m_itemKey, order);
|
|
|
|
}
|
|
|
|
|
2016-06-28 19:35:19 +08:00
|
|
|
void PluginsItem::detachPluginWidget()
|
|
|
|
{
|
|
|
|
QWidget *widget = m_pluginInter->itemWidget(m_itemKey);
|
|
|
|
if (widget)
|
|
|
|
widget->setParent(nullptr);
|
|
|
|
}
|
|
|
|
|
2018-10-16 14:33:06 +08:00
|
|
|
QString PluginsItem::pluginName() const
|
|
|
|
{
|
|
|
|
return m_pluginInter->pluginName();
|
|
|
|
}
|
|
|
|
|
2020-11-06 17:52:08 +08:00
|
|
|
PluginsItemInterface::PluginSizePolicy PluginsItem::pluginSizePolicy() const
|
|
|
|
{
|
|
|
|
// 插件版本大于 1.2.2 才能使用 PluginsItemInterface::pluginSizePolicy 函数
|
2022-07-11 03:18:24 +00:00
|
|
|
if (Utils::comparePluginApi(pluginApi(), "1.2.2") > 0) {
|
2020-11-06 17:52:08 +08:00
|
|
|
return m_pluginInter->pluginSizePolicy();
|
|
|
|
} else {
|
|
|
|
return PluginsItemInterface::System;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-03 20:37:30 +08:00
|
|
|
DockItem::ItemType PluginsItem::itemType() const
|
|
|
|
{
|
2019-09-03 20:53:49 +08:00
|
|
|
if (m_pluginInter->type() == PluginsItemInterface::Normal) {
|
2019-09-03 20:37:30 +08:00
|
|
|
return Plugins;
|
|
|
|
} else {
|
2019-09-04 13:18:05 +08:00
|
|
|
return FixedPlugin;
|
2019-09-03 20:37:30 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-26 15:20:16 +08:00
|
|
|
QSize PluginsItem::sizeHint() const
|
|
|
|
{
|
|
|
|
return m_centralWidget->sizeHint();
|
|
|
|
}
|
|
|
|
|
2021-03-06 18:40:13 +08:00
|
|
|
void PluginsItem::refreshIcon()
|
2016-08-18 15:14:50 +08:00
|
|
|
{
|
2018-12-04 16:05:15 +08:00
|
|
|
m_pluginInter->refreshIcon(m_itemKey);
|
2016-08-18 15:14:50 +08:00
|
|
|
}
|
|
|
|
|
2019-08-21 12:52:53 +08:00
|
|
|
void PluginsItem::onGSettingsChanged(const QString &key)
|
|
|
|
{
|
2021-03-20 12:10:45 +08:00
|
|
|
if (key != "enable") {
|
2019-08-01 15:59:25 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-03-20 12:10:45 +08:00
|
|
|
if (m_gsettings && m_gsettings->keys().contains("enable")) {
|
2019-08-01 15:59:25 +08:00
|
|
|
setVisible(m_gsettings->get("enable").toBool());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-31 15:46:22 +08:00
|
|
|
QWidget *PluginsItem::centralWidget() const
|
2018-10-16 14:33:06 +08:00
|
|
|
{
|
|
|
|
return m_centralWidget;
|
|
|
|
}
|
|
|
|
|
2016-06-27 10:50:00 +08:00
|
|
|
void PluginsItem::mousePressEvent(QMouseEvent *e)
|
|
|
|
{
|
2019-08-01 15:59:25 +08:00
|
|
|
if (checkGSettingsControl()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-12-10 10:09:55 +08:00
|
|
|
m_hover = false;
|
|
|
|
update();
|
|
|
|
|
2019-10-17 19:52:55 +08:00
|
|
|
if (PopupWindow->isVisible())
|
2017-12-21 14:55:59 +08:00
|
|
|
hideNonModel();
|
2017-11-23 10:29:23 +08:00
|
|
|
|
2016-06-27 10:50:00 +08:00
|
|
|
if (e->button() == Qt::LeftButton)
|
|
|
|
MousePressPoint = e->pos();
|
2017-11-23 14:47:59 +08:00
|
|
|
|
2021-02-19 11:13:42 +08:00
|
|
|
//handle context menu
|
|
|
|
m_popupTipsDelayTimer->stop();
|
|
|
|
hideNonModel();
|
|
|
|
|
|
|
|
if (e->button() == Qt::RightButton) {
|
|
|
|
if (perfectIconRect().contains(e->pos())) {
|
2021-03-04 16:45:37 +08:00
|
|
|
return (m_gsettings && (!m_gsettings->keys().contains("menuEnable") || m_gsettings->get("menuEnable").toBool())) ? showContextMenu() : void();
|
2021-02-19 11:13:42 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// same as e->ignore above
|
|
|
|
QWidget::mousePressEvent(e);
|
2016-06-27 10:50:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void PluginsItem::mouseMoveEvent(QMouseEvent *e)
|
|
|
|
{
|
2019-08-01 15:59:25 +08:00
|
|
|
if (checkGSettingsControl()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-06-27 10:50:00 +08:00
|
|
|
if (e->buttons() != Qt::LeftButton)
|
|
|
|
return DockItem::mouseMoveEvent(e);
|
|
|
|
|
|
|
|
e->accept();
|
|
|
|
|
|
|
|
const QPoint distance = e->pos() - MousePressPoint;
|
|
|
|
if (distance.manhattanLength() > PLUGIN_ITEM_DRAG_THRESHOLD)
|
|
|
|
startDrag();
|
|
|
|
}
|
|
|
|
|
|
|
|
void PluginsItem::mouseReleaseEvent(QMouseEvent *e)
|
|
|
|
{
|
2019-08-01 15:59:25 +08:00
|
|
|
if (checkGSettingsControl()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-07-18 10:09:26 +08:00
|
|
|
DockItem::mouseReleaseEvent(e);
|
|
|
|
|
2016-06-27 10:50:00 +08:00
|
|
|
if (e->button() != Qt::LeftButton)
|
2016-07-18 10:09:26 +08:00
|
|
|
return;
|
2016-06-27 10:50:00 +08:00
|
|
|
|
2019-08-21 12:52:53 +08:00
|
|
|
if (checkAndResetTapHoldGestureState() && e->source() == Qt::MouseEventSynthesizedByQt) {
|
2018-11-13 16:01:36 +08:00
|
|
|
qDebug() << "tap and hold gesture detected, ignore the synthesized mouse release event";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-06-27 10:50:00 +08:00
|
|
|
e->accept();
|
|
|
|
|
|
|
|
const QPoint distance = e->pos() - MousePressPoint;
|
|
|
|
if (distance.manhattanLength() < PLUGIN_ITEM_DRAG_THRESHOLD)
|
|
|
|
mouseClicked();
|
|
|
|
}
|
|
|
|
|
2018-12-06 10:06:53 +08:00
|
|
|
void PluginsItem::enterEvent(QEvent *event)
|
|
|
|
{
|
2019-08-01 15:59:25 +08:00
|
|
|
if (checkGSettingsControl()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-12-06 10:06:53 +08:00
|
|
|
m_hover = true;
|
|
|
|
update();
|
|
|
|
|
|
|
|
DockItem::enterEvent(event);
|
|
|
|
}
|
|
|
|
|
|
|
|
void PluginsItem::leaveEvent(QEvent *event)
|
|
|
|
{
|
2018-12-07 15:12:01 +08:00
|
|
|
// Note:
|
2019-05-19 15:13:01 +08:00
|
|
|
// here we should check the mouse position to ensure the mouse is really leaved
|
|
|
|
// because this leaveEvent will also be called if setX11PassMouseEvent(false) is invoked
|
2018-12-07 15:12:01 +08:00
|
|
|
// in XWindowTrayWidget::sendHoverEvent()
|
|
|
|
m_hover = false;
|
|
|
|
update();
|
2018-12-06 10:06:53 +08:00
|
|
|
|
|
|
|
DockItem::leaveEvent(event);
|
|
|
|
}
|
|
|
|
|
2019-08-21 12:52:53 +08:00
|
|
|
void PluginsItem::showEvent(QShowEvent *event)
|
|
|
|
{
|
|
|
|
QTimer::singleShot(0, this, [ = ] {
|
2019-08-01 15:59:25 +08:00
|
|
|
onGSettingsChanged("enable");
|
|
|
|
});
|
|
|
|
|
|
|
|
return DockItem::showEvent(event);
|
|
|
|
}
|
|
|
|
|
2018-12-10 10:09:55 +08:00
|
|
|
bool PluginsItem::eventFilter(QObject *watched, QEvent *event)
|
|
|
|
{
|
|
|
|
if (watched == m_centralWidget) {
|
2019-08-01 15:59:25 +08:00
|
|
|
if (event->type() == QEvent::MouseButtonPress ||
|
|
|
|
event->type() == QEvent::MouseButtonRelease) {
|
|
|
|
if (checkGSettingsControl()) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
2018-12-10 10:09:55 +08:00
|
|
|
if (event->type() == QEvent::MouseButtonRelease) {
|
|
|
|
m_hover = false;
|
|
|
|
update();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-09-19 14:11:18 +08:00
|
|
|
void PluginsItem::invokedMenuItem(const QString &itemId, const bool checked)
|
|
|
|
{
|
|
|
|
m_pluginInter->invokedMenuItem(m_itemKey, itemId, checked);
|
|
|
|
}
|
|
|
|
|
2019-08-21 12:52:53 +08:00
|
|
|
void PluginsItem::showPopupWindow(QWidget *const content, const bool model)
|
2017-11-08 14:10:56 +08:00
|
|
|
{
|
|
|
|
DockItem::showPopupWindow(content, model);
|
|
|
|
}
|
|
|
|
|
2016-09-19 14:11:18 +08:00
|
|
|
const QString PluginsItem::contextMenu() const
|
|
|
|
{
|
|
|
|
return m_pluginInter->itemContextMenu(m_itemKey);
|
|
|
|
}
|
|
|
|
|
2016-07-01 11:07:20 +08:00
|
|
|
QWidget *PluginsItem::popupTips()
|
|
|
|
{
|
2019-08-01 15:59:25 +08:00
|
|
|
if (checkGSettingsControl()) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2016-07-13 16:47:59 +08:00
|
|
|
return m_pluginInter->itemTipsWidget(m_itemKey);
|
2016-07-01 11:07:20 +08:00
|
|
|
}
|
|
|
|
|
2016-06-27 10:50:00 +08:00
|
|
|
void PluginsItem::startDrag()
|
|
|
|
{
|
2019-08-21 12:52:53 +08:00
|
|
|
// 拖拽已放到MainPanelControl处理
|
|
|
|
return;
|
2016-06-27 10:50:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void PluginsItem::mouseClicked()
|
|
|
|
{
|
2016-07-13 16:47:59 +08:00
|
|
|
const QString command = m_pluginInter->itemCommand(m_itemKey);
|
2019-08-21 12:52:53 +08:00
|
|
|
if (!command.isEmpty()) {
|
2016-07-15 11:00:55 +08:00
|
|
|
QProcess *proc = new QProcess(this);
|
2016-07-13 16:47:59 +08:00
|
|
|
|
2016-07-15 11:00:55 +08:00
|
|
|
connect(proc, static_cast<void (QProcess::*)(int)>(&QProcess::finished), proc, &QProcess::deleteLater);
|
2016-06-27 11:04:29 +08:00
|
|
|
|
2016-07-15 11:00:55 +08:00
|
|
|
proc->startDetached(command);
|
|
|
|
return;
|
|
|
|
}
|
2016-06-27 10:50:00 +08:00
|
|
|
|
2016-07-15 11:00:55 +08:00
|
|
|
// request popup applet
|
2021-03-20 12:10:45 +08:00
|
|
|
if (QWidget *w = m_pluginInter->itemPopupApplet(m_itemKey))
|
2016-07-27 14:08:23 +08:00
|
|
|
showPopupApplet(w);
|
2016-06-27 10:50:00 +08:00
|
|
|
}
|
2019-08-01 15:59:25 +08:00
|
|
|
|
|
|
|
bool PluginsItem::checkGSettingsControl() const
|
|
|
|
{
|
2021-03-20 12:10:45 +08:00
|
|
|
return m_gsettings ? m_gsettings->keys().contains("control") && m_gsettings->get("control").toBool() : false;
|
2019-08-21 12:52:53 +08:00
|
|
|
}
|
|
|
|
|
2022-07-11 03:18:24 +00:00
|
|
|
QString PluginsItem::pluginApi() const
|
|
|
|
{
|
|
|
|
return m_jsonData.value("api").toString();
|
|
|
|
}
|
|
|
|
|
2019-09-04 13:18:05 +08:00
|
|
|
void PluginsItem::resizeEvent(QResizeEvent *event)
|
|
|
|
{
|
|
|
|
setMaximumSize(m_centralWidget->maximumSize());
|
|
|
|
return DockItem::resizeEvent(event);
|
|
|
|
}
|
|
|
|
|
2019-08-21 12:52:53 +08:00
|
|
|
void PluginsItem::setDraging(bool bDrag)
|
|
|
|
{
|
|
|
|
DockItem::setDraging(bDrag);
|
|
|
|
|
|
|
|
m_centralWidget->setVisible(!bDrag);
|
2019-08-01 15:59:25 +08:00
|
|
|
}
|
2022-05-12 17:35:50 +08:00
|
|
|
|
|
|
|
PluginsItemInterface *PluginsItem::pluginItem() const
|
|
|
|
{
|
|
|
|
return m_pluginInter;
|
|
|
|
}
|