From 1172b54b9f55c39511b598e12d977e205ec41125 Mon Sep 17 00:00:00 2001 From: donghualin Date: Fri, 20 May 2022 13:58:51 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=E6=89=98=E7=9B=98?= =?UTF-8?q?=E5=8C=BA=E5=9F=9F=E8=B0=83=E6=95=B4=E5=B0=BA=E5=AF=B8=E7=9A=84?= =?UTF-8?q?=E6=97=B6=E5=80=99=E6=89=98=E7=9B=98=E5=9B=BE=E6=A0=87=E4=B8=8D?= =?UTF-8?q?=E5=B1=85=E4=B8=AD=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 修改delegate类的sizeHint方法的返回值,在主窗体尺寸发生变化的时候,发送delegate的sizeHintChanged信号来保证sizeHint方法能实时调用 Log: Influence: 任务栏上下调整尺寸的时候观察托盘图标的位置变化 Task: https://pms.uniontech.com/task-view-110311.html Change-Id: I86b6bdc0fb991ff0a2674b0fa799add4b536a6bf --- frame/window/tray/tray_delegate.cpp | 62 +++++++++++++++++------------ frame/window/tray/tray_delegate.h | 2 +- frame/window/tray/tray_gridview.cpp | 5 +++ frame/window/tray/tray_gridview.h | 1 + frame/window/traymanagerwindow.cpp | 43 +++++++++++--------- frame/window/traymanagerwindow.h | 1 + 6 files changed, 70 insertions(+), 44 deletions(-) diff --git a/frame/window/tray/tray_delegate.cpp b/frame/window/tray/tray_delegate.cpp index 276d9f706..dd6c65b79 100644 --- a/frame/window/tray/tray_delegate.cpp +++ b/frame/window/tray/tray_delegate.cpp @@ -138,7 +138,15 @@ QSize TrayDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelInd Q_UNUSED(option); Q_UNUSED(index); - return QSize(ITEM_SIZE, ITEM_SIZE); + // 如果是弹出托盘,则显示正常大小 + if (isPopupTray()) + return QSize(ITEM_SIZE, ITEM_SIZE); + + // 如果是任务栏的托盘,则高度显示为listView的高度或宽度 + if (m_position == Dock::Position::Top || m_position == Dock::Position::Bottom) + return QSize(ITEM_SIZE, m_listView->height()); + + return QSize(m_listView->width(), ITEM_SIZE); } void TrayDelegate::updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const @@ -153,30 +161,34 @@ void TrayDelegate::updateEditorGeometry(QWidget *editor, const QStyleOptionViewI void TrayDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const { - if (needDrawBackground()) { - QColor borderColor; - QColor backColor; - if (DGuiApplicationHelper::instance()->themeType() == DGuiApplicationHelper::LightType) { - // 白色主题的情况下 - borderColor = Qt::black; - borderColor.setAlpha(static_cast(255 * 0.05)); - backColor = Qt::white; - backColor.setAlpha(static_cast(255 * 0.4)); - } else { - borderColor = Qt::black; - borderColor.setAlpha(static_cast(255 * 0.2)); - backColor = Qt::black; - backColor.setAlpha(static_cast(255 * 0.4)); - } - painter->save(); - QPainterPath path; - path.addRoundedRect(option.rect, 8, 8); - painter->setRenderHint(QPainter::Antialiasing); - painter->fillPath(path, backColor); - painter->setPen(borderColor); - painter->drawPath(path); - painter->restore(); + Q_UNUSED(index); + + if (!isPopupTray()) + return; + + QColor borderColor; + QColor backColor; + if (DGuiApplicationHelper::instance()->themeType() == DGuiApplicationHelper::LightType) { + // 白色主题的情况下 + borderColor = Qt::black; + borderColor.setAlpha(static_cast(255 * 0.05)); + backColor = Qt::white; + backColor.setAlpha(static_cast(255 * 0.4)); + } else { + borderColor = Qt::black; + borderColor.setAlpha(static_cast(255 * 0.2)); + backColor = Qt::black; + backColor.setAlpha(static_cast(255 * 0.4)); } + + painter->save(); + QPainterPath path; + path.addRoundedRect(option.rect, 8, 8); + painter->setRenderHint(QPainter::Antialiasing); + painter->fillPath(path, backColor); + painter->setPen(borderColor); + painter->drawPath(path); + painter->restore(); } ExpandIconWidget *TrayDelegate::expandWidget() @@ -198,7 +210,7 @@ ExpandIconWidget *TrayDelegate::expandWidget() return nullptr; } -bool TrayDelegate::needDrawBackground() const +bool TrayDelegate::isPopupTray() const { if (!m_listView) return false; diff --git a/frame/window/tray/tray_delegate.h b/frame/window/tray/tray_delegate.h index 85125819e..baa843715 100644 --- a/frame/window/tray/tray_delegate.h +++ b/frame/window/tray/tray_delegate.h @@ -59,7 +59,7 @@ protected: private: ExpandIconWidget *expandWidget(); - bool needDrawBackground() const; + bool isPopupTray() const; private: Dock::Position m_position; diff --git a/frame/window/tray/tray_gridview.cpp b/frame/window/tray/tray_gridview.cpp index f346d04d4..8bdfc3ff4 100644 --- a/frame/window/tray/tray_gridview.cpp +++ b/frame/window/tray/tray_gridview.cpp @@ -53,6 +53,11 @@ void TrayGridView::setPosition(Dock::Position position) m_positon = position; } +Dock::Position TrayGridView::position() const +{ + return m_positon; +} + QSize TrayGridView::suitableSize() const { TrayModel *dataModel = qobject_cast(model()); diff --git a/frame/window/tray/tray_gridview.h b/frame/window/tray/tray_gridview.h index fee65b42e..7ae02602b 100644 --- a/frame/window/tray/tray_gridview.h +++ b/frame/window/tray/tray_gridview.h @@ -37,6 +37,7 @@ public: explicit TrayGridView(QWidget *parent = Q_NULLPTR); void setPosition(Dock::Position position); + Dock::Position position() const; QSize suitableSize() const; void setDragDistance(int pixel); void setAnimationProperty(const QEasingCurve::Type easing, const int duringTime = 250); diff --git a/frame/window/traymanagerwindow.cpp b/frame/window/traymanagerwindow.cpp index acf501f2c..3bf8321d5 100644 --- a/frame/window/traymanagerwindow.cpp +++ b/frame/window/traymanagerwindow.cpp @@ -91,14 +91,7 @@ void TrayManagerWindow::setPositon(Dock::Position position) m_dateTimeWidget->setPositon(position); m_systemPluginWidget->setPositon(position); - QMetaObject::invokeMethod(this, [ this ]{ - if (showSingleRow()) - resetSingleDirection(); - else - resetMultiDirection(); - - resetChildWidgetSize(); - }, Qt::QueuedConnection); + QMetaObject::invokeMethod(this, &TrayManagerWindow::resetDirection, Qt::QueuedConnection); } int TrayManagerWindow::appDatetimeSize() @@ -142,12 +135,7 @@ void TrayManagerWindow::resizeEvent(QResizeEvent *event) { Q_UNUSED(event); - if (showSingleRow()) - resetSingleDirection(); - else - resetMultiDirection(); - - resetChildWidgetSize(); + resetDirection(); } void TrayManagerWindow::initUi() @@ -267,6 +255,18 @@ void TrayManagerWindow::initConnection() QMetaObject::invokeMethod(this, &TrayManagerWindow::resetChildWidgetSize, Qt::QueuedConnection); } +void TrayManagerWindow::resetDirection() +{ + if (showSingleRow()) + resetSingleDirection(); + else + resetMultiDirection(); + + resetChildWidgetSize(); + // 当尺寸发生变化的时候,通知托盘区域刷新尺寸,让托盘图标始终保持居中显示 + Q_EMIT m_delegate->sizeHintChanged(m_model->index(0, 0)); +} + bool TrayManagerWindow::showSingleRow() { if (m_postion == Dock::Position::Top || m_postion == Dock::Position::Bottom) @@ -298,10 +298,9 @@ void TrayManagerWindow::resetChildWidgetSize() m_dateTimeWidget->setFixedSize(m_dateTimeWidget->suitableSize().width(), trayHeight); } else { // 多行显示 - int trayHeight = m_appPluginDatetimeWidget->height() / 2 - m.top() - m.bottom(); - m_trayView->setFixedSize(trayWidth, trayHeight); - m_quickIconWidget->setFixedSize(m_quickIconWidget->suitableSize().width(), trayHeight); - m_appPluginWidget->setFixedSize(trayWidth + m_quickIconWidget->suitableSize().width(), trayHeight); + m_trayView->setFixedSize(trayWidth, QWIDGETSIZE_MAX); + m_quickIconWidget->setFixedSize(m_quickIconWidget->suitableSize().width(), QWIDGETSIZE_MAX); + m_appPluginWidget->setFixedSize(trayWidth + m_quickIconWidget->suitableSize().width(), QWIDGETSIZE_MAX); // 因为是两行,所以对于时间控件的尺寸,只能设置最小值 int dateTimeWidth = qMax(m_appPluginWidget->width(), m_dateTimeWidget->suitableSize().width()); m_dateTimeWidget->setMinimumSize(dateTimeWidth, m_appPluginDatetimeWidget->height() / 2); @@ -347,6 +346,8 @@ void TrayManagerWindow::resetSingleDirection() break; } } + m_splitLine->hide(); + m_dateTimeWidget->setOneRow(true); } void TrayManagerWindow::resetMultiDirection() @@ -356,12 +357,16 @@ void TrayManagerWindow::resetMultiDirection() m_appPluginLayout->setDirection(QBoxLayout::Direction::LeftToRight); m_appDatetimeLayout->setDirection(QBoxLayout::Direction::BottomToTop); m_mainLayout->setDirection(QBoxLayout::Direction::LeftToRight); + m_splitLine->show(); + m_dateTimeWidget->setOneRow(false); break; } case Dock::Position::Bottom: { m_appPluginLayout->setDirection(QBoxLayout::Direction::LeftToRight); m_appDatetimeLayout->setDirection(QBoxLayout::Direction::TopToBottom); m_mainLayout->setDirection(QBoxLayout::Direction::LeftToRight); + m_splitLine->show(); + m_dateTimeWidget->setOneRow(false); break; } case Dock::Position::Left: @@ -369,6 +374,8 @@ void TrayManagerWindow::resetMultiDirection() m_appPluginLayout->setDirection(QBoxLayout::Direction::TopToBottom); m_appDatetimeLayout->setDirection(QBoxLayout::Direction::TopToBottom); m_mainLayout->setDirection(QBoxLayout::Direction::TopToBottom); + m_splitLine->hide(); + m_dateTimeWidget->setOneRow(true); break; } } diff --git a/frame/window/traymanagerwindow.h b/frame/window/traymanagerwindow.h index d0bbad211..3acfb1888 100644 --- a/frame/window/traymanagerwindow.h +++ b/frame/window/traymanagerwindow.h @@ -68,6 +68,7 @@ private: void initUi(); void initConnection(); + void resetDirection(); void resetChildWidgetSize(); void resetMultiDirection(); void resetSingleDirection();