Skip to content

Commit

Permalink
fix: Missing radius for tray's background
Browse files Browse the repository at this point in the history
  Add a common function to draw special background.
  Sub widget request a highlight background for parent.

Issue: linuxdeepin/developer-center#3744
  • Loading branch information
18202781743 committed Mar 9, 2023
1 parent 724edd7 commit ccb2793
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 22 deletions.
13 changes: 2 additions & 11 deletions frame/window/components/datetimedisplayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ DateTimeDisplayer::DateTimeDisplayer(bool showMultiRow, QWidget *parent)
, m_currentSize(0)
, m_oneRow(false)
, m_showMultiRow(showMultiRow)
, m_isEnter(false)
{
m_tipPopupWindow.reset(new DockPopupWindow);
// 日期格式变化的时候,需要重绘
Expand Down Expand Up @@ -271,14 +270,6 @@ void DateTimeDisplayer::paintEvent(QPaintEvent *e)
painter.setRenderHint(QPainter::Antialiasing);
painter.setPen(QPen(palette().brightText(), 1));

// 绘制背景色
if (m_isEnter) {
QColor backColor = DGuiApplicationHelper::ColorType::DarkType == DGuiApplicationHelper::instance()->themeType() ? QColor(20, 20, 20) : Qt::white;
backColor.setAlphaF(0.2);
// 鼠标进入的时候,绘制底色
painter.fillRect(rect(), backColor);
}

int timeAlignFlag = Qt::AlignCenter;
int dateAlignFlag = Qt::AlignCenter;

Expand Down Expand Up @@ -395,15 +386,15 @@ QRect DateTimeDisplayer::textRect(const QRect &sourceRect) const
void DateTimeDisplayer::enterEvent(QEvent *event)
{
Q_UNUSED(event);
m_isEnter = true;
Q_EMIT requestDrawBackground(rect());
update();
m_tipPopupWindow->show(tipsPoint());
}

void DateTimeDisplayer::leaveEvent(QEvent *event)
{
Q_UNUSED(event);
m_isEnter = false;
Q_EMIT requestDrawBackground(QRect());
update();
m_tipPopupWindow->hide();
}
Expand Down
2 changes: 1 addition & 1 deletion frame/window/components/datetimedisplayer.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ class DateTimeDisplayer : public QWidget

Q_SIGNALS:
void requestUpdate(); // 当日期时间格式发生变化的时候,需要通知外面来更新窗口尺寸
void requestDrawBackground(const QRect &rect);

protected:
void mousePressEvent(QMouseEvent *event) override;
Expand Down Expand Up @@ -83,7 +84,6 @@ private Q_SLOTS:
int m_currentSize;
bool m_oneRow;
bool m_showMultiRow;
bool m_isEnter;
};

#endif // DATETIMEDISPLAYER_H
14 changes: 5 additions & 9 deletions frame/window/systempluginwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,6 @@ StretchPluginsItem::StretchPluginsItem(DockInter *dockInter, PluginsItemInterfac
, m_itemKey(itemKey)
, m_displayMode(Dock::DisplayMode::Efficient)
, m_dockInter(dockInter)
, m_isEnter(false)
{
}

Expand Down Expand Up @@ -266,12 +265,6 @@ void StretchPluginsItem::paintEvent(QPaintEvent *event)
rctPixmap.setHeight(ICONSIZE);
}

if (m_isEnter) {
QColor backColor = DGuiApplicationHelper::ColorType::DarkType == DGuiApplicationHelper::instance()->themeType() ? QColor(20, 20, 20) : Qt::white;
backColor.setAlphaF(0.2);
// 鼠标进入的时候,绘制底色
painter.fillRect(rect(), backColor);
}
// 绘制图标
int iconSize = static_cast<int>(ICONSIZE * (QCoreApplication::testAttribute(Qt::AA_UseHighDpiPixmaps) ? 1 : qApp->devicePixelRatio()));
painter.drawPixmap(rctPixmap, icon.pixmap(iconSize, iconSize));
Expand Down Expand Up @@ -393,14 +386,17 @@ void StretchPluginsItem::mouseReleaseEvent(QMouseEvent *e)

void StretchPluginsItem::enterEvent(QEvent *event)
{
m_isEnter = true;
if (auto view = qobject_cast<SystemPluginWindow *>(parentWidget()))
view->requestDrawBackground(rect());

update();
DockItem::enterEvent(event);
}

void StretchPluginsItem::leaveEvent(QEvent *event)
{
m_isEnter = false;
if (auto view = qobject_cast<SystemPluginWindow *>(parentWidget()))
view->requestDrawBackground(QRect());
update();
DockItem::leaveEvent(event);
}
Expand Down
2 changes: 1 addition & 1 deletion frame/window/systempluginwindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ class SystemPluginWindow : public QWidget
Q_SIGNALS:
void itemChanged();
void requestDrop(QDropEvent *dropEvent);
void requestDrawBackground(const QRect &rect);

protected:
bool eventFilter(QObject *watched, QEvent *event) override;
Expand Down Expand Up @@ -99,7 +100,6 @@ class StretchPluginsItem : public DockItem
static Dock::Position m_position;
QPoint m_mousePressPoint;
DockInter *m_dockInter;
bool m_isEnter;
};

#endif // SYSTEMPLUGINWINDOW_H
44 changes: 44 additions & 0 deletions frame/window/traymanagerwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,37 @@ void TrayManagerWindow::onTrayCountChanged()
Q_EMIT requestUpdate();
}

void TrayManagerWindow::updateHighlightArea(const QRect &rect)
{
do {
m_highlightArea.clear();
if (!rect.isValid())
break;

auto widget = qobject_cast<QWidget *>(sender());
Q_ASSERT(widget);

QRect tmp = rect;
tmp.moveTopLeft(widget->mapTo(this, rect.topLeft()));

const auto radius = pathRadius();
m_highlightArea.addRoundedRect(tmp, radius, radius);
if (widget == m_dateTimeWidget) {
if (!m_singleShow) {
QPainterPath path;
if(m_position == Dock::Position::Top)
path.addRect(tmp.adjusted(0, radius, 0, 0));
else
path.addRect(tmp.adjusted(0, 0, 0, -radius));

m_highlightArea += path;
}
}
} while (false);

update();
}

void TrayManagerWindow::resizeEvent(QResizeEvent *event)
{
Q_UNUSED(event);
Expand Down Expand Up @@ -302,6 +333,7 @@ void TrayManagerWindow::initConnection()

Q_EMIT requestUpdate();
});
connect(m_systemPluginWidget, &SystemPluginWindow::requestDrawBackground, this, &TrayManagerWindow::updateHighlightArea);

connect(m_trayView, &TrayGridView::dragLeaved, m_delegate, [ this ]{
Q_EMIT m_delegate->requestDrag(true);
Expand All @@ -313,6 +345,7 @@ void TrayManagerWindow::initConnection()

connect(m_model, &TrayModel::rowCountChanged, m_trayView, &TrayGridView::onUpdateEditorView);
connect(m_dateTimeWidget, &DateTimeDisplayer::requestUpdate, this, &TrayManagerWindow::requestUpdate);
connect(m_dateTimeWidget, &DateTimeDisplayer::requestDrawBackground, this, &TrayManagerWindow::updateHighlightArea);

m_trayView->installEventFilter(this);
m_quickIconWidget->installEventFilter(this);
Expand Down Expand Up @@ -508,6 +541,17 @@ void TrayManagerWindow::paintEvent(QPaintEvent *event)
painter.setPen(maskColor(110));
painter.drawPath(path);
painter.restore();

// draw highlight background for special path.
if (!m_highlightArea.isEmpty()) {
painter.save();
QColor backColor = DGuiApplicationHelper::ColorType::DarkType == DGuiApplicationHelper::instance()->themeType() ? QColor(20, 20, 20) : Qt::white;
backColor.setAlphaF(0.2);
painter.setPen(Qt::NoPen);
painter.setBrush(backColor);
painter.drawPath(m_highlightArea);
painter.restore();
}
}

QColor TrayManagerWindow::maskColor(uint8_t alpha) const
Expand Down
3 changes: 3 additions & 0 deletions frame/window/traymanagerwindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

#include "org_deepin_dde_timedate1.h"

#include <QPainterPath>
#include <QWidget>

namespace Dtk { namespace Widget { class DBlurEffectWidget; } }
Expand Down Expand Up @@ -72,6 +73,7 @@ class TrayManagerWindow : public QWidget

private Q_SLOTS:
void onTrayCountChanged();
void updateHighlightArea(const QRect &rect);

private:
QWidget *m_appPluginDatetimeWidget;
Expand All @@ -90,6 +92,7 @@ private Q_SLOTS:
QLabel *m_splitLine;
bool m_singleShow; // 用于记录当前日期时间和插件区域是显示一行还是显示多行
int m_borderRadius; // 圆角的值
QPainterPath m_highlightArea;
};

#endif // PLUGINWINDOW_H

0 comments on commit ccb2793

Please sign in to comment.