-
Notifications
You must be signed in to change notification settings - Fork 0
/
label.cpp
executable file
·100 lines (86 loc) · 1.8 KB
/
label.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
100
// Copyright 2022-present Contributors to the colorpicker project.
// SPDX-License-Identifier: BSD-3-Clause
// https://github.com/mikaelsundell/colorpicker
#include "label.h"
#include <QMouseEvent>
#include <QPainter>
#include <QPointer>
class LabelPrivate : public QObject
{
Q_OBJECT
public:
LabelPrivate();
void init();
void update();
bool eventFilter(QObject* object, QEvent* event);
public:
bool readOnly;
QPixmap buffer;
QPointer<Label> widget;
};
LabelPrivate::LabelPrivate()
: readOnly(true)
{
}
void
LabelPrivate::init()
{
widget->installEventFilter(this);
}
void
LabelPrivate::update()
{
qreal dpr = widget->devicePixelRatio();
buffer = QPixmap(widget->size() * dpr);
buffer.fill(Qt::transparent);
buffer.setDevicePixelRatio(dpr);
// painter
QPainter p(&buffer);
QBrush brush = QBrush(Qt::white);
// readonly
if (!readOnly)
{
QRectF rect = widget->rect();
p.setPen(QPen(brush, 2));
p.drawLine(rect.bottomLeft(), rect.bottomRight());
}
p.end();
}
bool
LabelPrivate::eventFilter(QObject* object, QEvent* event)
{
if (event->type() == QEvent::QEvent::MouseButtonPress)
{
QMouseEvent* mouseEvent = (QMouseEvent*)event;
if (mouseEvent->button() == Qt::LeftButton) {
widget->triggered();
}
}
return false;
}
#include "label.moc"
Label::Label(QWidget* parent)
: QLabel(parent)
, p(new LabelPrivate())
{
p->widget = this;
p->init();
}
Label::~Label()
{
}
void
Label::setReadOnly(bool readOnly)
{
p->readOnly = readOnly;
p->update();
update();
}
void
Label::paintEvent(QPaintEvent* event)
{
QPainter painter(this);
painter.drawPixmap(0, 0, p->buffer);
painter.end();
QLabel::paintEvent(event);
}