forked from shelllet/cpp-example
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtitlebarwidget.cpp
More file actions
194 lines (167 loc) · 6.21 KB
/
Copy pathtitlebarwidget.cpp
File metadata and controls
194 lines (167 loc) · 6.21 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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
#include "titlebarwidget.h"
#include "qpainter.h"
#include "qstyleoption.h"
#include "private/qwidget_p.h"
#include "qlabel.h"
#include "qtoolbutton.h"
#include "qapplication.h"
#include "qstackedwidget.h"
#include "standardbutton.h"
#include "qboxlayout.h"
#include <boost/core/ignore_unused.hpp>
namespace shelllet {
namespace frameless {
class IconButton : public QLabel {
public:
IconButton(const std::function<void()>& doubleClicked, QWidget* parent) : QLabel(parent), doubleClicked(doubleClicked) {
}
protected:
void mouseDoubleClickEvent(QMouseEvent* /*event*/) override
{
if (doubleClicked)
doubleClicked();
}
private:
std::function<void()> doubleClicked;
};
class TitleBarWidgetPrivate : public QWidgetPrivate {
public:
IconButton* icon;
QLabel* titleText;
StandardButton* minimizeButton;
StandardButton* restoreButton;
StandardButton* maximizeButton;
StandardButton* closeButton;
QStackedWidget* customButton;
QHBoxLayout* horizontalLayout;
const float factor = 0.8;
void setupUi(TitleBarWidget* parent) {
parent->setObjectName("window-title-bar");
horizontalLayout = new QHBoxLayout(parent);
horizontalLayout->setSpacing(0);
horizontalLayout->setContentsMargins(0, 0, 0, 0);
horizontalLayout->addSpacing(2);
icon = new IconButton([parent]() { parent->iconDoubleClicked(); }, parent);
auto extent = static_cast<int>(parent->geometry().height() * factor);
icon->setPixmap(qApp->windowIcon().pixmap(extent));
icon->setContextMenuPolicy(Qt::NoContextMenu);
horizontalLayout->addWidget(icon);
horizontalLayout->addSpacing(1);
titleText = new QLabel(parent);
titleText->setObjectName("window-title-label");
titleText->setAlignment(Qt::AlignLeading | Qt::AlignLeft | Qt::AlignVCenter);
titleText->setContextMenuPolicy(Qt::CustomContextMenu);
titleText->setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::Preferred);
horizontalLayout->addWidget(titleText);
horizontalLayout->addSpacing(9);
customButton = new QStackedWidget(parent);
horizontalLayout->addWidget(customButton);
minimizeButton = new StandardButton(ButtonType::Minimize, parent);
minimizeButton->setFixedWidth(45);
horizontalLayout->addWidget(minimizeButton);
restoreButton = new StandardButton(ButtonType::Restore, parent);
restoreButton->setFixedWidth(45);
horizontalLayout->addWidget(restoreButton);
restoreButton->setStyleSheet("background-color: #3a3a3a");
maximizeButton = new StandardButton(ButtonType::Maximize, parent);
maximizeButton->setFixedWidth(45);
horizontalLayout->addWidget(maximizeButton);
maximizeButton->setStyleSheet("background-color: #3a3a3a");
closeButton = new StandardButton(ButtonType::Close, parent);
closeButton->setFixedWidth(45);
horizontalLayout->addWidget(closeButton);
}
};
}
}
shelllet::frameless::TitleBarWidget::TitleBarWidget(QWidget* parent /*= nullptr*/, WindowTypeFlags f /*= WindowTypes()*/)
: QWidget(*new TitleBarWidgetPrivate, parent, {}) {
Q_D(TitleBarWidget);
d->setupUi(this);
d->restoreButton->setVisible(false);
connect(this, &TitleBarWidget::windowTitleChanged, [d](const QString& title) {d->titleText->setText(title); });
connect(this, &TitleBarWidget::windowIconChanged, [this, d](const QIcon& icon) {d->icon->setPixmap(icon.pixmap(static_cast<int>(geometry().height() * d->factor))); });
connect(d->closeButton, SIGNAL(clicked()), this, SIGNAL(closeButtonClicked()));
connect(d->minimizeButton, SIGNAL(clicked()), this, SIGNAL(minimizeButtonClicked()));
connect(d->maximizeButton, SIGNAL(clicked()), this, SIGNAL(maximizeButtonClicked()));
connect(d->restoreButton, SIGNAL(clicked()), this, SIGNAL(restoreButtonClicked()));
QObject::connect(qApp, &QGuiApplication::applicationStateChanged, this, &TitleBarWidget::applicationStateChanged);
}
void shelllet::frameless::TitleBarWidget::applicationStateChanged(Qt::ApplicationState state)
{
Q_D(TitleBarWidget);
if (state == Qt::ApplicationActive) {
d->titleText->setStyleSheet("color: rgb(240, 240, 240)");
}
else {
d->titleText->setStyleSheet("color: rgb(200, 200, 200)");
}
}
void shelllet::frameless::TitleBarWidget::setWindowTypes(WindowTypeFlags f)
{
Q_D(TitleBarWidget);
if (!f.testFlag(WindowType::CloseButtonHint)) {
d->closeButton->hide();
}
if (!f.testFlag(WindowType::MinimizeButtonHint)) {
d->minimizeButton->hide();
}
if (!f.testFlag(WindowType::MaximizeButtonHint)) {
d->maximizeButton->hide();
}
}
void shelllet::frameless::TitleBarWidget::setCustomButton(QWidget* c)
{
Q_D(TitleBarWidget);
auto* widget = d->customButton->currentWidget();
if (widget) {
d->customButton->removeWidget(widget);
widget->deleteLater();
}
d->customButton->addWidget(c);
}
bool shelllet::frameless::TitleBarWidget::underMouse()
{
Q_D(TitleBarWidget);
QWidget* action = QApplication::widgetAt(QCursor::pos());
if (action == this || action == d->titleText || action == d->customButton) {
return true;
}
return false;
}
void shelllet::frameless::TitleBarWidget::switchToMaxButton()
{
Q_D(TitleBarWidget);
d->restoreButton->setVisible(false);
d->maximizeButton->setVisible(true);
}
void shelllet::frameless::TitleBarWidget::switchToRestoreButton()
{
Q_D(TitleBarWidget);
d->restoreButton->setVisible(true);
d->maximizeButton->setVisible(false);
}
void shelllet::frameless::TitleBarWidget::setTitleTextBlurRadius(qreal blurRadius)
{
Q_D(TitleBarWidget);
QGraphicsDropShadowEffect* textShadow = new QGraphicsDropShadowEffect;
textShadow->setBlurRadius(blurRadius);
textShadow->setColor(QColor(0, 0, 0));
textShadow->setOffset(.0f);
d->titleText->setGraphicsEffect(textShadow);
}
void shelllet::frameless::TitleBarWidget::paintEvent(QPaintEvent* event) {
boost::ignore_unused(event);
QStyleOption styleOption;
styleOption.initFrom(this);
QPainter painter(this);
painter.setRenderHint(QPainter::Antialiasing);
style()->drawPrimitive(QStyle::PE_Widget, &styleOption, &painter, this);
}
void shelllet::frameless::TitleBarWidget::resizeEvent(QResizeEvent* event)
{
//throw std::logic_error("The method or operation is not implemented.");
Q_D(TitleBarWidget);
auto extent = static_cast<int>(event->size().height() * d->factor);
d->icon->setPixmap(qApp->windowIcon().pixmap(extent));
}