-
Notifications
You must be signed in to change notification settings - Fork 6
/
framelesswindow.cpp
181 lines (167 loc) · 5.21 KB
/
framelesswindow.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
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
/*
Drumstick MIDI File Player Multiplatform Program
Copyright (C) 2006-2024, Pedro Lopez-Cabanillas <[email protected]>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <QFlags>
#include <QWindow>
#include <QEvent>
#include <QHoverEvent>
#include <QStyle>
#include "settings.h"
#include "framelesswindow.h"
FramelessWindow::FramelessWindow(QWidget *parent)
: QMainWindow(parent, Qt::FramelessWindowHint)
, m_pseudoCaption(nullptr)
, m_snapped{false}
, m_keepSnapped{true}
{
#if defined(Q_OS_MACOS)
//setUnifiedTitleAndToolBarOnMac(true);
setAttribute(Qt::WA_MacMiniSize, true);
#else
setWindowFlag(Qt::Tool, true);
#endif
setAttribute(Qt::WA_DeleteOnClose, false);
connect(this, &FramelessWindow::sizeAdjustNeeded, this, &FramelessWindow::performAdjustSize, Qt::QueuedConnection);
}
void FramelessWindow::updateCursor(Qt::Edges edges)
{
if ( edges == 0 )
{
setCursor(Qt::ArrowCursor);
}
else if ( (edges == Qt::LeftEdge) ||
(edges == Qt::RightEdge) )
{
setCursor(Qt::SizeHorCursor);
}
else if ( (edges == Qt::TopEdge) ||
(edges == Qt::BottomEdge))
{
setCursor(Qt::SizeVerCursor);
}
else if ( (edges == (Qt::LeftEdge | Qt::TopEdge)) ||
(edges == (Qt::RightEdge | Qt::BottomEdge)) )
{
setCursor(Qt::SizeFDiagCursor);
}
else if ( (edges == (Qt::RightEdge | Qt::TopEdge)) ||
(edges == (Qt::LeftEdge | Qt::BottomEdge)) )
{
setCursor(Qt::SizeBDiagCursor);
}
}
bool FramelessWindow::event(QEvent* ev)
{
static Qt::Edges edges = Qt::Edges();
const int margin = 5;
switch (ev->type()) {
case QEvent::MouseButtonPress:
updateCursor(edges);
if ((m_pseudoCaption != nullptr) && m_pseudoCaption->underMouse()) {
/*auto ok =*/ windowHandle()->startSystemMove();
//qDebug() << "startSystemMove()" << (ok ? "succeeded" : "failed");
} else if (edges != 0) {
/*auto ok =*/ windowHandle()->startSystemResize(edges);
//qDebug() << "startSystemResize()" << (ok ? "succeeded" : "failed");
}
break;
case QEvent::MouseButtonRelease:
//qDebug() << Q_FUNC_INFO << ev;
edges = Qt::Edges();
updateCursor(edges);
setFocus();
break;
case QEvent::HoverEnter:
edges = Qt::Edges();
break;
case QEvent::HoverLeave:
edges = Qt::Edges();
break;
case QEvent::HoverMove: {
edges = Qt::Edges();
QHoverEvent *event = static_cast<QHoverEvent*>(ev);
QPoint p =
#if QT_VERSION < QT_VERSION_CHECK(6,0,0)
event->pos();
#else
event->position().toPoint();
#endif
if ( p.x() < margin ) {
edges |= Qt::LeftEdge;
}
if ( p.x() > (width() - margin) ) {
edges |= Qt::RightEdge;
}
if ( p.y() < margin ) {
edges |= Qt::TopEdge;
}
if ( p.y() > (height() - margin) ) {
edges |= Qt::BottomEdge;
}
updateCursor(edges);
break;
}
default:
break;
}
return QMainWindow::event(ev);
}
QWidget *FramelessWindow::pseudoCaption() const
{
return m_pseudoCaption;
}
void FramelessWindow::setPseudoCaption(QWidget *widget)
{
m_pseudoCaption = widget;
}
void FramelessWindow::performAdjustSize()
{
centralWidget()->adjustSize();
adjustSize();
}
void FramelessWindow::applySettings()
{
#if defined(Q_OS_WINDOWS)
m_snapper.SetEnabled(Settings::instance()->winSnap());
#endif
m_keepSnapped = Settings::instance()->snappedTogether();
}
bool FramelessWindow::nativeEvent(const QByteArray &eventType, void *message,
#if QT_VERSION < QT_VERSION_CHECK(6,0,0)
long *result
#else
qintptr *result
#endif
)
{
#if defined(Q_OS_WINDOWS)
if (m_snapper.HandleMessage(message)) {
result = 0;
return true;
}
#endif
return QMainWindow::nativeEvent(eventType, message, result);
}
void FramelessWindow::moveEvent(QMoveEvent *event)
{
QMainWindow::moveEvent(event);
auto p = dynamic_cast<QMainWindow *>(parent());
if (p && m_keepSnapped) {
auto joined = [](int a, int b) { return qAbs(b - a) < 3; };
m_snapped = joined(frameGeometry().top(), p->frameGeometry().bottom())
|| joined(frameGeometry().bottom(), p->frameGeometry().top())
|| joined(frameGeometry().left(), p->frameGeometry().right())
|| joined(frameGeometry().right(), p->frameGeometry().left());
}
}