-
Notifications
You must be signed in to change notification settings - Fork 39
Expand file tree
/
Copy pathqnavigationwidget.cpp
More file actions
104 lines (81 loc) · 2.16 KB
/
qnavigationwidget.cpp
File metadata and controls
104 lines (81 loc) · 2.16 KB
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
101
102
103
104
#include "qnavigationwidget.h"
#include <QPainter>
#include <QDebug>
QNavigationWidget::QNavigationWidget(QWidget *parent) : QWidget(parent)
{
backgroundColor = "#E4E4E4";
selectedColor = "#2CA7F8";
rowHeight = 40;
currentIndex = 0;
setMouseTracking(true);
setFixedWidth(150);
}
QNavigationWidget::~QNavigationWidget()
{
}
void QNavigationWidget::addItem(const QString &title)
{
listItems << title;
update();
}
void QNavigationWidget::setWidth(const int &width)
{
setFixedWidth(width);
}
void QNavigationWidget::setBackgroundColor(const QString &color)
{
backgroundColor = color;
update();
}
void QNavigationWidget::setSelectColor(const QString &color)
{
selectedColor = color;
update();
}
void QNavigationWidget::setRowHeight(const int &height)
{
rowHeight = height;
update();
}
void QNavigationWidget::paintEvent(QPaintEvent *)
{
QPainter painter(this);
painter.setRenderHint(QPainter::Antialiasing, true);
// Draw background color.
painter.setPen(Qt::NoPen);
painter.setBrush(QColor(backgroundColor));
painter.drawRect(rect());
// Draw Items
int count = 0;
for (const QString &str : listItems) {
QPainterPath itemPath;
itemPath.addRect(QRect(0, count * rowHeight, width(), rowHeight));
if (currentIndex == count) {
painter.setPen("#FFFFFF");
painter.fillPath(itemPath, QColor(selectedColor));
}else {
painter.setPen("#202020");
painter.fillPath(itemPath, QColor(backgroundColor));
}
painter.drawText(QRect(0, count * rowHeight, width(), rowHeight), Qt::AlignVCenter | Qt::AlignHCenter, str);
++count;
}
}
void QNavigationWidget::mouseMoveEvent(QMouseEvent *e)
{
if (e->y() / rowHeight < listItems.count()) {
// qDebug() << e->y() / rowHeight;
}
}
void QNavigationWidget::mousePressEvent(QMouseEvent *e)
{
if (e->y() / rowHeight < listItems.count()) {
currentIndex = e->y() / rowHeight;
emit currentItemChanged(currentIndex);
update();
}
}
void QNavigationWidget::mouseReleaseEvent(QMouseEvent *e)
{
Q_UNUSED(e);
}