-
Notifications
You must be signed in to change notification settings - Fork 0
/
styledmainwindow.cpp
149 lines (125 loc) · 4.02 KB
/
styledmainwindow.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
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
#include "styledmainwindow.h"
#include <QApplication>
#include <QToolBar>
#include <QVBoxLayout>
#include <QGuiApplication>
#include <QScreen>
#include <QMenuBar>
#include "aboutdialog.h"
//! Range of pixels the window should snap to the screen border
#define SNAP_RANGE 10
StyledMainWindow::StyledMainWindow(QWidget* parent) :
QMainWindow(parent)
{
setWindowFlag(Qt::FramelessWindowHint, true);
setMinimumHeight(12);
initializeComponents();
}
void StyledMainWindow::installMainWindow(MainWindow* w)
{
_mainWindow = w;
setMinimumHeight(minimumHeight() + _mainWindow->minimumHeight() + 5);
centralWidget()->layout()->addWidget(_mainWindow);
auto menuBar = w->menuBar();
auto entries = menuBar->findChildren<QMenu*>();
for (auto &entry : entries)
{
_menu->addMenu(entry);
}
}
void StyledMainWindow::onMinimizeClicked()
{
this->setWindowState(Qt::WindowMinimized);
}
void StyledMainWindow::onCloseClicked()
{
close();
}
void StyledMainWindow::resizeEvent(QResizeEvent* /* event */)
{
moveSizeGrip();
}
void StyledMainWindow::initializeComponents()
{
setContentsMargins(0, 0, 0, 0);
auto layout = new QVBoxLayout();
layout->setContentsMargins(0, 0, 0, 0);
layout->setSpacing(0);
this->setCentralWidget(new QWidget(this));
centralWidget()->setLayout(layout);
setCursor(QCursor(QPixmap(":/res/normal.png"), 0, 0));
//=========================================================================
// title bar
_stb = new StyledTitleBar("foxbox", this);
layout->addWidget(_stb);
connect(_stb, &StyledTitleBar::minimizeClicked, this, &StyledMainWindow::onMinimizeClicked);
connect(_stb, &StyledTitleBar::closeClicked, this, &StyledMainWindow::onCloseClicked);
connect(_stb, &StyledTitleBar::mouseMoved, this, &StyledMainWindow::onTitleBarMouseMoved);
connect(_stb, &StyledTitleBar::mousePressed, this, &StyledMainWindow::onTitleBarMousePressed);
setMinimumHeight(_stb->height());
//=========================================================================
// menu items
_menu = new QMenu(this);
_qaFoxbox = new QAction(QString("foxbox %1").arg(QApplication::instance()->applicationVersion()), _menu);
connect(_qaFoxbox, &QAction::triggered, this, &StyledMainWindow::onqaFoxboxTriggered);
_menu->addAction(_qaFoxbox);
_menu->addSeparator();
_menu->setCursor(QCursor(QPixmap(":/res/normal.png"), 0, 0));
_stb->setMenu(_menu);
//=========================================================================
// size grip
_sizeGrip = new QSizeGrip(this);
_sizeGrip->setMaximumSize(16, 16);
}
void StyledMainWindow::moveSizeGrip()
{
auto size = _sizeGrip->size();
QPoint targetPosition(
width() - size.width(),
height() - size.height()
);
_sizeGrip->move(targetPosition);
}
void StyledMainWindow::onTitleBarMouseMoved(QMouseEvent* event)
{
auto targetPos = event->globalPos() - _dragPosition;
// TODO: handle multi-monitor setups
auto screenSize = QGuiApplication::primaryScreen()->size();
if (qAbs(targetPos.x()) < SNAP_RANGE)
{
targetPos.setX(0);
}
if (qAbs(targetPos.y()) < SNAP_RANGE)
{
targetPos.setY(0);
}
if (qAbs(targetPos.x() + width() - screenSize.width()) < SNAP_RANGE)
{
targetPos.setX(screenSize.width() - width());
}
if (qAbs(targetPos.y() + height() - screenSize.height()) < SNAP_RANGE)
{
targetPos.setY(screenSize.height() - height());
}
move(targetPos);
}
void StyledMainWindow::onTitleBarMousePressed(QMouseEvent* event)
{
_dragPosition = event->globalPos() - frameGeometry().topLeft();
event->accept();
}
void StyledMainWindow::onqaFoxboxTriggered()
{
if (_mainWindow->isHidden())
{
return;
}
auto ad = new AboutDialog(this);
connect(ad, &AboutDialog::accepted, this, [&] {
_stb->setTitle("foxbox");
_mainWindow->show();
});
_stb->setTitle("About foxbox");
_mainWindow->hide();
centralWidget()->layout()->addWidget(ad);
}