From 5530675d055df0bf1ac4d6070838e9d069b067a4 Mon Sep 17 00:00:00 2001 From: donghualin Date: Mon, 5 Dec 2022 15:00:22 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8DU=E7=9B=98=E6=8F=92?= =?UTF-8?q?=E4=BB=B6=E4=B8=8D=E6=98=BE=E7=A4=BA=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 老版本的U盘插件没有适配icon接口,需要根据实际情况将itemWidget显示 Log: 修复U盘插件不显示的问题 Influence: 系统中使用v20的U盘插件,插入U盘,查看U盘图标是否显示 Task: https://pms.uniontech.com/task-view-223159.html Change-Id: I7d2e7867203962ced078087c66fbddb7020d30df --- frame/pluginadapter/pluginadapter.cpp | 4 ++ frame/window/quickpluginwindow.cpp | 10 +-- frame/window/quickpluginwindow.h | 1 - .../window/tray/widgets/systempluginitem.cpp | 72 ++++++++++++++++--- frame/window/tray/widgets/systempluginitem.h | 6 +- 5 files changed, 77 insertions(+), 16 deletions(-) diff --git a/frame/pluginadapter/pluginadapter.cpp b/frame/pluginadapter/pluginadapter.cpp index 80c39e021..e987fc2ba 100644 --- a/frame/pluginadapter/pluginadapter.cpp +++ b/frame/pluginadapter/pluginadapter.cpp @@ -172,6 +172,10 @@ QIcon PluginAdapter::icon(const DockPart &dockPart, DGuiApplicationHelper::Color switch (dockPart) { case DockPart::QuickPanel: case DockPart::SystemPanel: { + // 如果是托盘插件,则让他返回空图标,直接将QWidget展示 + if (m_pluginLoader->fileName().contains(TRAY_PATH)) + return QIcon(); + // 如果图标为空,就使用itemWidget的截图作为它的图标,这种一般是适用于老版本插件或者没有实现v23接口的插件 QSize oldSize = itemWidget->size(); itemWidget->setFixedSize(ICONWIDTH, ICONHEIGHT); diff --git a/frame/window/quickpluginwindow.cpp b/frame/window/quickpluginwindow.cpp index 3262feccd..a84b50b7e 100644 --- a/frame/window/quickpluginwindow.cpp +++ b/frame/window/quickpluginwindow.cpp @@ -234,10 +234,10 @@ bool QuickPluginWindow::eventFilter(QObject *watched, QEvent *event) break; QMouseEvent *mouseEvent = static_cast(event); - if (m_dragInfo->canDrag(mouseEvent->pos())) { + if (m_dragInfo->canDrag(mouseEvent->pos())) startDrag(); - m_dragInfo->reset(); - } + + m_dragInfo->reset(); break; } case QEvent::Drop: { @@ -694,7 +694,8 @@ void QuickDockItem::showEvent(QShowEvent *event) QWidget *itemWidget = m_pluginItem->itemWidget(m_itemKey); if (itemWidget && m_mainLayout->indexOf(itemWidget) < 0) { - itemWidget->setFixedSize(ICONWIDTH - 2, ICONHEIGHT - 2); + itemWidget->show(); + itemWidget->setFixedSize(size()); m_mainLayout->addWidget(itemWidget); } } @@ -707,6 +708,7 @@ void QuickDockItem::hideEvent(QHideEvent *event) QWidget *itemWidget = m_pluginItem->itemWidget(m_itemKey); if (itemWidget && m_mainLayout->indexOf(itemWidget) >= 0) { itemWidget->setParent(m_dockItemParent); + itemWidget->hide(); m_mainLayout->removeWidget(itemWidget); } } diff --git a/frame/window/quickpluginwindow.h b/frame/window/quickpluginwindow.h index 149db0be3..f2d10e29d 100644 --- a/frame/window/quickpluginwindow.h +++ b/frame/window/quickpluginwindow.h @@ -97,7 +97,6 @@ public: explicit QuickDockItem(PluginsItemInterface *pluginItem, const QString &itemKey, QWidget *parent = nullptr); ~QuickDockItem(); - void setPositon(Dock::Position position); PluginsItemInterface *pluginItem(); bool canInsert() const; void hideToolTip(); diff --git a/frame/window/tray/widgets/systempluginitem.cpp b/frame/window/tray/widgets/systempluginitem.cpp index 29a98b906..1fc92e3ff 100644 --- a/frame/window/tray/widgets/systempluginitem.cpp +++ b/frame/window/tray/widgets/systempluginitem.cpp @@ -47,18 +47,20 @@ SystemPluginItem::SystemPluginItem(PluginsItemInterface *const pluginInter, cons { qDebug() << "load tray plugins item: " << m_pluginInter->pluginName() << itemKey << m_centralWidget; + QIcon icon = m_pluginInter->icon(DockPart::SystemPanel); if (m_centralWidget) { - m_centralWidget->setParent(this); - m_centralWidget->setVisible(true); - m_centralWidget->installEventFilter(this); + if (icon.isNull()) { + // 此处先创建一个Layout,在该窗体show的时候将m_centralWidget添加到layout上面 + QBoxLayout *hLayout = new QHBoxLayout(this); + hLayout->setSpacing(0); + hLayout->setMargin(0); + setLayout(hLayout); + m_centralWidget->installEventFilter(this); + } else { + m_centralWidget->setVisible(false); + } } - QBoxLayout *hLayout = new QHBoxLayout(this); - hLayout->addWidget(m_centralWidget); - hLayout->setSpacing(0); - hLayout->setMargin(0); - - setLayout(hLayout); setAccessibleName(m_itemKey); setAttribute(Qt::WA_TranslucentBackground); @@ -194,8 +196,11 @@ bool SystemPluginItem::event(QEvent *event) bool SystemPluginItem::eventFilter(QObject *watched, QEvent *event) { - if (watched == this && event->type() == QEvent::DeferredDelete) + if (watched == this && event->type() == QEvent::DeferredDelete + && m_centralWidget && m_centralWidget->parentWidget()) { m_centralWidget->setParent(nullptr); + m_centralWidget->setVisible(false); + } return BaseTrayWidget::eventFilter(watched, event); } @@ -282,6 +287,43 @@ void SystemPluginItem::showEvent(QShowEvent *event) return BaseTrayWidget::showEvent(event); } +void SystemPluginItem::hideEvent(QHideEvent *event) +{ + if (m_pluginInter->icon(DockPart::SystemPanel).isNull() + && m_centralWidget && m_centralWidget->parentWidget() == this) { + layout()->removeWidget(m_centralWidget); + m_centralWidget->setParent(nullptr); + m_centralWidget->setVisible(false); + } + BaseTrayWidget::hideEvent(event); +} + +void SystemPluginItem::paintEvent(QPaintEvent *event) +{ + QIcon icon = m_pluginInter->icon(DockPart::SystemPanel); + if (icon.isNull()) { + showCentralWidget(); + return BaseTrayWidget::paintEvent(event); + } + + int x = 0; + int y = 0; + QSize iconSize = this->size(); + QList sizes = icon.availableSizes(); + if (sizes.size() > 0) { + QSize availableSize = sizes.first(); + if (iconSize.width() > availableSize.width()) { + x = (iconSize.width() - availableSize.width()) / 2; + y = (iconSize.height() - availableSize.height()) / 2; + iconSize = availableSize; + } + } + + QPixmap pixmap = icon.pixmap(iconSize); + QPainter painter(this); + painter.drawPixmap(QRect(QPoint(x, y), iconSize), pixmap); +} + const QPoint SystemPluginItem::popupMarkPoint() const { QPoint p(topleftPoint()); @@ -480,6 +522,16 @@ void SystemPluginItem::menuActionClicked(QAction *action) invokedMenuItem(action->data().toString(), true); } +void SystemPluginItem::showCentralWidget() +{ + if (!m_pluginInter->icon(DockPart::SystemPanel).isNull() || !m_centralWidget) + return; + + m_centralWidget->setParent(this); + m_centralWidget->setVisible(true); + layout()->addWidget(m_centralWidget); +} + void SystemPluginItem::onContextMenuAccepted() { emit requestRefershWindowVisible(); diff --git a/frame/window/tray/widgets/systempluginitem.h b/frame/window/tray/widgets/systempluginitem.h index cba056d27..593a53d9b 100644 --- a/frame/window/tray/widgets/systempluginitem.h +++ b/frame/window/tray/widgets/systempluginitem.h @@ -31,6 +31,7 @@ class QGSettings; class QMenu; class DockPopupWindow; +class QHBoxLayout; class SystemPluginItem : public BaseTrayWidget { @@ -72,7 +73,9 @@ protected: void leaveEvent(QEvent *event) override; void mousePressEvent(QMouseEvent *event) override; void mouseReleaseEvent(QMouseEvent *event) override; - void showEvent(QShowEvent* event) override; + void showEvent(QShowEvent *event) override; + void hideEvent(QHideEvent *event) override; + void paintEvent(QPaintEvent *event) override; protected: const QPoint popupMarkPoint() const; @@ -95,6 +98,7 @@ private: void onGSettingsChanged(const QString &key); bool checkGSettingsControl() const; void menuActionClicked(QAction *action); + void showCentralWidget(); private: bool m_popupShown;