-
Notifications
You must be signed in to change notification settings - Fork 0
/
TitleBar.cpp
123 lines (104 loc) · 2.69 KB
/
TitleBar.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#include "TitleBar.h"
/*!
* @brief Create and setup a cutom TitleBar
*
* Create and setup a cutom TitleBar
*/
TitleBar::TitleBar(QWidget *parent, QWidget *mainw) : QWidget(parent)
{
mainwin = mainw;
mMaxNormal = false;
setObjectName("titlebar");
// Top right buttons/icons
mMinimizeButton = new QToolButton(this);
mMaximizeButton = new QToolButton(this);
mCloseButton = new QToolButton(this);
QIcon closeicon = QIcon(":/winicons/closeNorm.png");
QIcon minicon = QIcon(":/winicons/minNorm.png");
maxicon = QIcon(":/winicons/maxNorm.png");
resticon = QIcon(":/winicons/restNorm.png");
mCloseButton->setIcon(closeicon);
mCloseButton->setObjectName("closeWindowBtn");
mMinimizeButton->setIcon(minicon);
mMaximizeButton->setIcon(maxicon);
// Logo
QLabel *logo = new QLabel();
QPixmap p = QIcon("://logo3.png").pixmap(QSize(22, 22));
logo->setPixmap(p.scaled(22, 22, Qt::KeepAspectRatio, Qt::SmoothTransformation));
logo->setAlignment(Qt::AlignLeft | Qt::AlignVCenter);
// Title
QFont f("Arial", 12, QFont::Bold);
mLabel = new QLabel();
mLabel->setText(mainwin->windowTitle());
mLabel->setFont(f);
mLabel->setAlignment(Qt::AlignLeft | Qt::AlignVCenter);
// Title layout
QHBoxLayout *hbox = new QHBoxLayout(this);
hbox->setSpacing(0);
hbox->setMargin(0);
hbox->addWidget(logo);
hbox->addWidget(mLabel);
hbox->addWidget(mMinimizeButton);
hbox->addWidget(mMaximizeButton);
hbox->addWidget(mCloseButton);
setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
connect(mCloseButton, SIGNAL(clicked()), mainwin, SLOT(close()));
connect(mMinimizeButton, SIGNAL(clicked()), this, SLOT(showSmall()));
connect(mMaximizeButton, SIGNAL(clicked()), this, SLOT(showMaxRestore()));
setMaximumHeight(24);
}
/*!
* @brief Modify title label if title changed
*
* Modify title label if title changed
*/
void TitleBar::titleChanged()
{
mLabel->setText(mainwin->windowTitle());
}
/*!
* @brief Minimize
*
* Minimize
*/
void TitleBar::showSmall()
{
mainwin->showMinimized();
}
/*!
* @brief Maximize/Normal
*
* Maximize/Normal
*/
void TitleBar::showMaxRestore()
{
if (mMaxNormal) {
mainwin->showNormal();
mMaxNormal = !mMaxNormal;
mMaximizeButton->setIcon(maxicon);
}
else {
mainwin->showMaximized();
mMaxNormal = !mMaxNormal;
mMaximizeButton->setIcon(resticon);
}
}
/*! \brief Manage press event
*
* Manage press event by mapping the position of the cursor
*/
void TitleBar::mousePressEvent(QMouseEvent *me)
{
mStartPos = me->globalPos();
mClickPos = mapToParent(me->pos());
}
/*! \brief Manage window d&d
*
* Manage window d&d by setting the position based on movement
*/
void TitleBar::mouseMoveEvent(QMouseEvent *me)
{
if (mMaxNormal)
return;
mainwin->move(me->globalPos() - mClickPos);
}