-
Notifications
You must be signed in to change notification settings - Fork 14
/
left_widget.cpp
71 lines (55 loc) · 1.81 KB
/
left_widget.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
#include "left_widget.h"
#include <QPainter>
#include <QMouseEvent>
LeftWidget::LeftWidget(QWidget *parent)
: QWidget(parent)
{
currentIndex = 0;
tabNames.insert(0, "词典");
tabNames.insert(1, "翻译");
tabNames.insert(2, "关于");
setFixedWidth(100);
}
void LeftWidget::paintEvent(QPaintEvent *)
{
QPainter painter(this);
painter.setRenderHint(QPainter::Antialiasing, true);
painter.setPen(QPen(QColor("#FFFFFF")));
painter.setBrush(QColor("#FFFFFF"));
painter.drawRect(rect());
QPainterPath separationLinePath;
separationLinePath.addRect(QRectF(99, 0, 1, this->height()));
painter.fillPath(separationLinePath, QColor("#E6E6E6"));
int tabY = 0;
for (int i=0; i<tabNames.length(); ++i) {
QString tabName = tabNames.at(i);
int width = 100;
int height = 30;
if (i == currentIndex) {
QPainterPath tabBackgroundPath;
tabBackgroundPath.addRect(QRectF(0, tabY, width, height));
painter.fillPath(tabBackgroundPath, QColor("#D5EDFE"));
QPainterPath tabLeftLinePath;
tabLeftLinePath.addRect(QRectF(98, tabY, 2, height));
painter.fillPath(tabLeftLinePath, QColor("#2CA7F8"));
painter.setPen(QPen(QColor("#2CA7F8")));
}else
painter.setPen(QPen(QColor("#000000")));
painter.drawText(QRect(30, tabY + 4, width, height), Qt::AlignTop, tabName);
tabY += height;
}
}
void LeftWidget::mousePressEvent(QMouseEvent *event)
{
int prevIndex = currentIndex;
if (event->y() < 30)
currentIndex = 0;
else if (event->y() < 60)
currentIndex = 1;
else if (event->y() < 90)
currentIndex = 2;
if (prevIndex != currentIndex) {
emit currentIndexChanged(currentIndex);
repaint();
}
}