-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsidebar.cpp
More file actions
172 lines (150 loc) · 4.29 KB
/
sidebar.cpp
File metadata and controls
172 lines (150 loc) · 4.29 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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
#include "sidebar.h"
#include <QPaintEvent>
#include <QPainter>
#include <QDebug>
#include <QEvent>
#define action_height 40
Sidebar::Sidebar(QWidget *parent)
: QWidget(parent)
, mCheckedAction(NULL)
, mOverAction(NULL)
, mSidebarWidth(200)
, mIconSize(QSize(24, 24))
, mSidebarIcon(new QIcon())
{
setFixedWidth(200);
setMouseTracking(true);
}
void Sidebar::paintEvent(QPaintEvent *event)
{
QPainter p(this);
int action_y = 100;
p.fillRect(rect(), QColor(255, 255, 255));
QRect iconRect(0, 0, mSidebarWidth, 100);
// QRect centerRect(0, 0, 150, 100);
// centerRect.moveCenter(iconRect.center());
mSidebarIcon->paint(&p, iconRect);
for(auto action: mActions)
{
QRect actionRect(0, action_y, event->rect().width(), action_height);
p.setPen(QColor(168, 176, 185));
if(action->isChecked())
{
p.fillRect(actionRect, QColor(255, 248, 239));
p.setPen(QColor(248, 161, 69));
}
if(action == mOverAction){
p.fillRect(actionRect, QColor(255, 248, 239));
}
QRect iconRect(24, action_y + action_height/2 - 12, mIconSize.width(), mIconSize.height());
QIcon actionIcon = action->icon();
actionIcon.paint(&p, iconRect, Qt::AlignCenter, action->isChecked() ? QIcon::Normal : QIcon::Disabled);
QSize size = p.fontMetrics().size(Qt::TextSingleLine, action->text());
QRect textRect(
24 + mIconSize.width() + 20,
actionRect.bottom() - size.height()/2 - (action_height/2),
mSidebarWidth - size.width() - 24,
action_height
);
p.drawText(textRect, Qt::AlignLeft, action->text());
action_y += actionRect.height();
}
}
QSize Sidebar::minimumSizeHint() const
{
return action_height*QSize(1, mActions.size());
}
void Sidebar::addAction(QAction *action)
{
mActions.push_back(action);
action->setCheckable(true);
update();
}
QAction *Sidebar::addAction(const QString &text, const QIcon &icon)
{
QAction *action = new QAction(icon, text, this);
action->setCheckable(true);
mActions.push_back(action);
update();
return action;
}
QAction *Sidebar::addDefaultAction(const QString &text, const QIcon &icon)
{
QAction *action = new QAction(icon, text, this);
action->setCheckable(true);
mCheckedAction = action;
action->setChecked(true);
mActions.push_back(action);
qDebug() << (action->isChecked() ? "YES" : "NO") << '\n';
update();
return action;
}
void Sidebar::mousePressEvent(QMouseEvent *event)
{
QAction* tempAction = actionAt(event->pos());
if(tempAction == NULL || tempAction->isChecked())
return;
if(mCheckedAction)
mCheckedAction->setChecked(false);
if(mOverAction == tempAction)
mOverAction = NULL;
mCheckedAction = tempAction;
tempAction->setChecked(true);
emit actionSelected(indexAt(event->pos()));
update();
QWidget::mousePressEvent(event);
}
void Sidebar::mouseMoveEvent(QMouseEvent *event)
{
QAction* tempAction = actionAt(event->pos());
if(tempAction == NULL){
mOverAction = NULL;
update();
return;
}
if(tempAction->isChecked() || mOverAction == tempAction)
return;
mOverAction = tempAction;
update();
QWidget::mouseMoveEvent(event);
}
void Sidebar::leaveEvent(QEvent * event)
{
mOverAction = NULL;
update();
QWidget::leaveEvent(event);
}
QAction* Sidebar::actionAt(const QPoint &at)
{
int action_y = 100;
for(auto action: mActions)
{
QRect actionRect(0, action_y, rect().width(), action_height);
if(actionRect.contains(at))
return action;
action_y += actionRect.height();
}
return NULL;
}
int Sidebar::indexAt(const QPoint &at)
{
int action_y = 100;
int i = 0;
for(auto action: mActions)
{
QRect actionRect(0, action_y, rect().width(), action_height);
if(actionRect.contains(at))
return i;
action_y += actionRect.height();
i++;
}
return -1;
}
void Sidebar::setSidebarIcon(QIcon &icon) {
mSidebarIcon = &icon;
}
void Sidebar::setSelected(int i, bool selected) {
mActions[i]->setChecked(selected);
update();
}
#undef action_height