This repository has been archived by the owner on Mar 2, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathroundedwidgets.cpp
99 lines (98 loc) · 3.29 KB
/
roundedwidgets.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#include "roundedwidgets.h"
#include <QDebug>
DGUI_USE_NAMESPACE
RoundedWidget::RoundedWidget(QWidget *parent, int r, int t, QPoint o)
: QWidget(parent), radius(r), margin(0), type(t), offset(o), painter(nullptr) {
setPalette(DGuiApplicationHelper::instance()->applicationPalette());
connect(DGuiApplicationHelper::instance(), &DGuiApplicationHelper::themeTypeChanged, [this]() {
setPalette(DGuiApplicationHelper::instance()->applicationPalette());
repaint();
});
if (!offset.isNull())
connect(this, &RoundedWidget::widgetMoving, [=](QPoint pos) {
this->blockSignals(true);
this->move(pos + this->offset);
this->blockSignals(false);
});
}
void RoundedWidget::paintEvent(QPaintEvent *event) {
if (!painter) {
painter = new QPainter(this);
painter->setRenderHints(QPainter::Antialiasing | QPainter::SmoothPixmapTransform);
}
painter->begin(this);
painter->setRenderHints(QPainter::Antialiasing | QPainter::SmoothPixmapTransform);
QPainterPath path;
auto rect = event->rect();
if (margin)
rect.adjust(margin, margin, -margin, -margin);
path.addRoundedRect(QRectF(rect), radius, radius);
if (type) {
auto tmpRect = rect;
switch (type) {
case TopTwo:
tmpRect.adjust(0, rect.height() - radius, 0, 0);
break;
case BottomTwo:
tmpRect.adjust(0, 0, 0, -(rect.height() - radius));
break;
case LeftTwo:
tmpRect.adjust(0, 0, -(rect.width() - radius), 0);
break;
case RightTwo:
tmpRect.adjust(rect.width() - radius, 0, 0, 0);
break;
}
path.addRect(QRectF(tmpRect));
}
path.setFillRule(Qt::WindingFill);
painter->fillPath(path, palette().window());
painter->end();
}
void RoundedWidget::moveEvent(QMoveEvent *event) {
emit widgetMoving(event->pos());
return QWidget::moveEvent(event);
}
void RoundedWidget::setRadius(int r) {
radius = r;
}
void RoundedWidget::setMargin(int d) // set self margins
{
margin = d;
setContentsMargins(margin, margin, margin, margin);
}
void RoundedWidget::setShadow(int d) {
if (!d)
return;
QGraphicsDropShadowEffect *effect = new QGraphicsDropShadowEffect(this);
effect->setOffset(0, 0);
effect->setColor(QColor(0, 0, 0, 50));
effect->setBlurRadius(d);
setGraphicsEffect(effect);
setMargin(d);
}
CircleButton::CircleButton(QIcon icon, QWidget *parent) : QPushButton(icon, "", parent), painter(nullptr) {
setPalette(DGuiApplicationHelper::instance()->applicationPalette());
connect(DGuiApplicationHelper::instance(), &DGuiApplicationHelper::themeTypeChanged, [this]() {
setPalette(DGuiApplicationHelper::instance()->applicationPalette());
repaint();
});
}
void CircleButton::paintEvent(QPaintEvent *event) {
if (!painter) {
painter = new QPainter(this);
painter->setRenderHints(QPainter::Antialiasing | QPainter::SmoothPixmapTransform);
}
// qDebug() << "renderHints:" << (painter->renderHints() == (QPainter::Antialiasing | QPainter::SmoothPixmapTransform));
// painter->save();
painter->begin(this);
painter->setRenderHints(QPainter::Antialiasing | QPainter::SmoothPixmapTransform);
QPainterPath path;
path.addEllipse(event->rect());
painter->fillPath(path, isChecked() ? palette().highlight() : palette().button());
auto iconRect = event->rect();
int d = int(iconRect.width() * 0.3);
iconRect.adjust(d, d, -d, -d);
painter->drawPixmap(iconRect, this->icon().pixmap(iconRect.width(), iconRect.height()));
painter->end();
}