Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
108 changes: 81 additions & 27 deletions src/widgets/ColorSelectWdg.cpp
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
// SPDX-FileCopyrightText: 2019 - 2022 UnionTech Software Technology Co., Ltd.
// SPDX-FileCopyrightText: 2019-2026 UnionTech Software Technology Co., Ltd.
//
// SPDX-License-Identifier: GPL-3.0-or-later

#include "ColorSelectWdg.h"
#include "../common/utils.h"
#include "../common/settings.h"
#include <QPainter>

Check warning on line 8 in src/widgets/ColorSelectWdg.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: <QPainter> not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <QEvent>

Check warning on line 9 in src/widgets/ColorSelectWdg.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: <QEvent> not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <DSettingsOption>

Check warning on line 10 in src/widgets/ColorSelectWdg.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: <DSettingsOption> not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <DFontSizeManager>

Check warning on line 11 in src/widgets/ColorSelectWdg.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: <DFontSizeManager> not found. Please note: Cppcheck does not need standard library headers to get proper results.

ColorLabel::ColorLabel(QColor color,QWidget *parent) : DWidget(parent),
m_color(color)
Expand Down Expand Up @@ -39,44 +40,97 @@
Q_UNUSED(event)

int distance = 2;

QRect r = rect();

QPainter painter(this);
painter.setRenderHints(QPainter::Antialiasing |QPainter::SmoothPixmapTransform | QPainter::Qt4CompatiblePainting);


QPainterPath bigCircle;
bigCircle.addEllipse(r);
painter.setRenderHints(QPainter::Antialiasing | QPainter::SmoothPixmapTransform | QPainter::Qt4CompatiblePainting);

if (m_bPressed) {
// press: 内圆微缩 + 颜色加深,模拟按压
QRectF pressedRect = r.adjusted(distance, distance, -distance, -distance);
QPainterPath pressedPath;
pressedPath.addEllipse(pressedRect);
painter.fillPath(pressedPath, m_color.darker(130));
if (m_bSelected) {
QPainterPath ring;
ring.addEllipse(r.adjusted(distance / 2, distance / 2, -distance / 2, -distance / 2));
QPainterPath outerPath;
outerPath.addEllipse(r);
ring = outerPath - ring;
painter.fillPath(ring, m_color);
}
} else if (m_bHover) {
// hover: 外圈光晕,圆略微放大
QRectF hoverRect = r.adjusted(distance, distance, -distance, -distance);
QPainterPath hoverPath;
hoverPath.addEllipse(hoverRect);
painter.fillPath(hoverPath, m_color);
if (m_bSelected) {
QPainterPath ring;
ring.addEllipse(r.adjusted(distance, distance, -distance, -distance));
QPainterPath outerPath;
outerPath.addEllipse(r);
ring = outerPath - ring;
painter.fillPath(ring, m_color);
}
} else {
// normal: 原有逻辑
QPainterPath bigCircle;
bigCircle.addEllipse(r);

QPainterPath smallCircle;
smallCircle.addEllipse(r.adjusted(2 * distance, 2 * distance, -2 * distance, -2 * distance));

// 先画小圆
painter.fillPath(smallCircle, m_color);

// 如果点击选择画圆环
if (m_bSelected) {
r = rect();
QPainterPath sencondCircle;
sencondCircle.addEllipse(r.adjusted(distance, distance, -distance, -distance));
// 大圆减小圆等于圆环
QPainterPath path = bigCircle - sencondCircle;
painter.fillPath(path, m_color);
}
}

QPainterPath smallCircle;
smallCircle.addEllipse(r.adjusted(2*distance,2*distance,-2*distance,-2*distance));
painter.end();
}

//先画小圆
painter.fillPath(smallCircle,m_color);
void ColorLabel::enterEvent(QEvent *event)
{
DWidget::enterEvent(event);
m_bHover = true;
update();
}

void ColorLabel::leaveEvent(QEvent *event)
{
DWidget::leaveEvent(event);
m_bHover = false;
m_bPressed = false;
update();
}

//如果点击选择画 圆环
if(m_bSelected){
r = rect();
QPainterPath sencondCircle;
sencondCircle.addEllipse(r.adjusted(distance,distance,-distance,-distance));
//大圆减小圆等于圆环
QPainterPath path = bigCircle - sencondCircle;
painter.fillPath(path,m_color);
void ColorLabel::mousePressEvent(QMouseEvent *e)
{
if (e->button() == Qt::LeftButton) {
m_bPressed = true;
update();
}

painter.end();
DWidget::mousePressEvent(e);
}

void ColorLabel::mousePressEvent(QMouseEvent *e)
void ColorLabel::mouseReleaseEvent(QMouseEvent *e)
{
//没有选择点击有效
if(e->button() == Qt::LeftButton){
m_bSelected = true;
update();
emit sigColorClicked(m_bSelected,m_color);
if (e->button() == Qt::LeftButton && m_bPressed) {
m_bPressed = false;
m_bSelected = true;
update();
emit sigColorClicked(m_bSelected, m_color);
}
DWidget::mouseReleaseEvent(e);
}

ColorSelectWdg::ColorSelectWdg(QString text,QWidget *parent):DWidget (parent),m_text(text)
Expand Down
11 changes: 8 additions & 3 deletions src/widgets/ColorSelectWdg.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// SPDX-FileCopyrightText: 2019 - 2022 UnionTech Software Technology Co., Ltd.
// SPDX-FileCopyrightText: 2019-2026 UnionTech Software Technology Co., Ltd.
//
// SPDX-License-Identifier: GPL-3.0-or-later

Expand Down Expand Up @@ -37,12 +37,17 @@ class ColorLabel : public DWidget
signals:
void sigColorClicked(bool bSelect,QColor color);
protected:
void paintEvent(QPaintEvent *event);
void mousePressEvent(QMouseEvent *e);
void paintEvent(QPaintEvent *event) override;
void enterEvent(QEvent *event) override;
void leaveEvent(QEvent *event) override;
void mousePressEvent(QMouseEvent *e) override;
void mouseReleaseEvent(QMouseEvent *e) override;
private:
//颜色选择标记

bool m_bSelected = false;
bool m_bHover = false;
bool m_bPressed = false;
QColor m_color;
};

Expand Down
Loading