-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathwidgetmanager.cpp
More file actions
319 lines (287 loc) · 8.86 KB
/
widgetmanager.cpp
File metadata and controls
319 lines (287 loc) · 8.86 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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
#include "widgetmanager.h"
#include <QStyle>
#include <QCursor>
#include <QApplication>
#include <QDesktopWidget>
#include "customborderstorage.h"
#ifdef Q_WS_X11
#include <QX11Info>
#include <X11/Xutil.h>
#include <X11/Xlib.h>
#include <X11/Xatom.h>
#define MESSAGE_SOURCE_OLD 0
#define MESSAGE_SOURCE_APPLICATION 1
#define MESSAGE_SOURCE_PAGER 2
#endif //Q_WS_X11
namespace WidgetManagerData
{
static bool isAlertEnabled = true;
}
class WindowSticker :
public QObject
{
public:
WindowSticker();
static WindowSticker *instance();
void insertWindow(QWidget *AWindow);
void removeWindow(QWidget *AWindow);
protected:
bool eventFilter(QObject *AWatched, QEvent *AEvent);
private:
int FStickEvent;
QPoint FStickPos;
QWidget *FCurWindow;
};
WindowSticker::WindowSticker()
{
FCurWindow = NULL;
FStickEvent = QEvent::registerEventType();
}
WindowSticker *WindowSticker::instance()
{
static WindowSticker *sticker = NULL;
if (!sticker)
sticker = new WindowSticker;
return sticker;
}
void WindowSticker::insertWindow(QWidget *AWindow)
{
if (AWindow)
{
AWindow->installEventFilter(this);
}
}
void WindowSticker::removeWindow(QWidget *AWindow)
{
if (AWindow)
{
AWindow->removeEventFilter(this);
}
}
bool WindowSticker::eventFilter(QObject *AWatched, QEvent *AEvent)
{
if (AEvent->type() == QEvent::NonClientAreaMouseButtonPress)
{
QWidget *window = qobject_cast<QWidget *>(AWatched);
if (window && window->isWindow())
{
FCurWindow = window;
}
}
else if (AEvent->type() == QEvent::NonClientAreaMouseButtonRelease)
{
FCurWindow = NULL;
}
else if (AEvent->type() == QEvent::NonClientAreaMouseMove)
{
FCurWindow = NULL;
}
else if (AEvent->type() == QEvent::WindowStateChange)
{
FCurWindow = NULL;
}
else if (AWatched==FCurWindow && AEvent->type()==QEvent::Move)
{
const int delta = 15;
QPoint cursorPos = QCursor::pos();
QRect windowRect = FCurWindow->frameGeometry();
QRect desckRect = QApplication::desktop()->availableGeometry(FCurWindow);
int borderTop = cursorPos.y() - windowRect.y();
int borderLeft = cursorPos.x() - windowRect.x();
int borderRight = cursorPos.x() + desckRect.right() - windowRect.right();
int borderBottom = cursorPos.y() + desckRect.bottom() - windowRect.bottom();
FStickPos = windowRect.topLeft();
if (qAbs(borderTop - cursorPos.y()) < delta)
{
FStickPos.setY(0);
}
else if (qAbs(borderBottom - cursorPos.y()) < delta)
{
FStickPos.setY(desckRect.bottom() - windowRect.height());
}
if (qAbs(borderLeft - cursorPos.x()) < delta)
{
FStickPos.setX(0);
}
else if(qAbs(borderRight - cursorPos.x()) < delta)
{
FStickPos.setX(desckRect.right() - windowRect.width());
}
if (FStickPos != windowRect.topLeft())
{
QEvent *stickEvent = new QEvent((QEvent::Type)FStickEvent);
QApplication::postEvent(AWatched,stickEvent,Qt::HighEventPriority);
}
}
else if (FCurWindow==AWatched && AEvent->type()==FStickEvent)
{
FCurWindow->move(FStickPos);
return true;
}
return QObject::eventFilter(AWatched,AEvent);
}
void WidgetManager::raiseWidget(QWidget *AWidget)
{
#ifdef Q_WS_X11
static Atom NET_ACTIVE_WINDOW = 0;
XClientMessageEvent xev;
if (NET_ACTIVE_WINDOW == 0)
{
Display *dpy = QX11Info::display();
NET_ACTIVE_WINDOW = XInternAtom(dpy, "_NET_ACTIVE_WINDOW", False);
}
xev.type = ClientMessage;
xev.window = AWidget->winId();
xev.message_type = NET_ACTIVE_WINDOW;
xev.format = 32;
xev.data.l[0] = MESSAGE_SOURCE_PAGER;
xev.data.l[1] = QX11Info::appUserTime();
xev.data.l[2] = xev.data.l[3] = xev.data.l[4] = 0;
XSendEvent(QX11Info::display(), QX11Info::appRootWindow(), False, SubstructureNotifyMask | SubstructureRedirectMask, (XEvent*)&xev);
#endif//Q_WS_X11
AWidget->raise();
}
void WidgetManager::showActivateRaiseWindow(QWidget *AWindow)
{
if (AWindow)
{
if (AWindow->isVisible())
{
if (AWindow->isMinimized())
{
if (AWindow->isMaximized())
AWindow->showMaximized();
else
AWindow->showNormal();
}
}
else
{
AWindow->show();
}
//#ifndef Q_WS_MAC
AWindow->activateWindow();
WidgetManager::raiseWidget(AWindow);
//#endif
}
}
void WidgetManager::setWindowSticky(QWidget *AWindow, bool ASticky)
{
#ifdef Q_WS_WIN
if (ASticky)
WindowSticker::instance()->insertWindow(AWindow);
else
WindowSticker::instance()->removeWindow(AWindow);
#else
Q_UNUSED(AWindow);
Q_UNUSED(ASticky);
#endif
}
void WidgetManager::alertWidget(QWidget *AWidget)
{
if (AWidget!=NULL && isWidgetAlertEnabled())
{
QApplication::alert(AWidget->window());
}
}
bool WidgetManager::isWidgetAlertEnabled()
{
return WidgetManagerData::isAlertEnabled;
}
void WidgetManager::setWidgetAlertEnabled(bool AEnabled)
{
WidgetManagerData::isAlertEnabled = AEnabled;
}
Qt::Alignment WidgetManager::windowAlignment(const QWidget *AWindow)
{
static const int delta = 4;
Qt::Alignment align = 0;
QRect windowRect = AWindow->frameGeometry();
QRect screenRect = QApplication::desktop()->availableGeometry(AWindow);
if (!screenRect.isEmpty() && !windowRect.isEmpty())
{
if (qAbs(screenRect.left() - windowRect.left()) < delta)
align |= Qt::AlignLeft;
else if (qAbs(screenRect.right() - windowRect.right()) < delta)
align |= Qt::AlignRight;
if (qAbs(screenRect.top() - windowRect.top()) < delta)
align |= Qt::AlignTop;
else if (qAbs(screenRect.bottom() - windowRect.bottom()) < delta)
align |= Qt::AlignBottom;
}
return align;
}
bool WidgetManager::alignWindow(QWidget *AWindow, Qt::Alignment AAlign)
{
if (AAlign > 0)
{
QRect frameRect = AWindow->frameGeometry();
QRect windowRect = AWindow->geometry();
if (!frameRect.isEmpty() && !windowRect.isEmpty() && frameRect.contains(windowRect))
{
QRect availRect = AWindow!=NULL ? QApplication::desktop()->availableGeometry(AWindow) : QApplication::desktop()->availableGeometry();
QRect rect = alignRect(frameRect,availRect,AAlign);
rect.adjust(windowRect.left()-frameRect.left(),windowRect.top()-frameRect.top(),windowRect.right()-frameRect.right(),windowRect.bottom()-frameRect.bottom());
AWindow->setGeometry(rect);
return true;
}
}
return false;
}
QRect WidgetManager::alignRect(const QRect &ARect, const QRect &ABoundary, Qt::Alignment AAlign)
{
QRect rect = ARect;
if (AAlign>0 && !ARect.isEmpty() && !ABoundary.isEmpty())
{
if ((AAlign & Qt::AlignLeft) == Qt::AlignLeft)
rect.moveLeft(ABoundary.left());
else if ((AAlign & Qt::AlignRight) == Qt::AlignRight)
rect.moveRight(ABoundary.right());
else if ((AAlign & Qt::AlignHCenter) == Qt::AlignHCenter)
rect.moveLeft((ABoundary.width()-ARect.width())/2);
if ((AAlign & Qt::AlignTop) == Qt::AlignTop)
rect.moveTop(ABoundary.top());
else if ((AAlign & Qt::AlignBottom) == Qt::AlignBottom)
rect.moveBottom(ABoundary.bottom());
else if ((AAlign & Qt::AlignVCenter) == Qt::AlignVCenter)
rect.moveTop((ABoundary.height() - ARect.height())/2);
}
return rect;
}
QRect WidgetManager::alignGeometry(const QSize &ASize, const QWidget *AWidget, Qt::Alignment AAlign)
{
QRect availRect = AWidget!=NULL ? QApplication::desktop()->availableGeometry(AWidget) : QApplication::desktop()->availableGeometry();
return QStyle::alignedRect(Qt::LeftToRight,AAlign,ASize.boundedTo(availRect.size()),availRect);
}
QRect WidgetManager::correctWindowGeometry(const QRect &AGeometry, QWidget *AWindow)
{
QRect newGeometry = AGeometry;
QRect screenRect = qApp->desktop()->availableGeometry(qApp->desktop()->screenNumber(AGeometry.topLeft()));
CustomBorderContainer *border = AWindow!=NULL ? CustomBorderStorage::widgetBorder(AWindow) : NULL;
if (border)
{
newGeometry.setLeft(newGeometry.left() + border->leftBorderWidth());
newGeometry.setRight(newGeometry.right() - border->rightBorderWidth());
newGeometry.setTop(newGeometry.top() + border->topBorderWidth());
newGeometry.setBottom(newGeometry.bottom() - border->bottomBorderWidth());
}
if (!screenRect.isEmpty() && !screenRect.contains(newGeometry))
{
if (newGeometry.left() < screenRect.left())
newGeometry.moveLeft(screenRect.left());
else if (newGeometry.right() > screenRect.right())
newGeometry.moveRight(screenRect.right());
if (newGeometry.top() < screenRect.top())
newGeometry.moveTop(screenRect.top());
else if (newGeometry.bottom() > screenRect.bottom())
newGeometry.moveBottom(screenRect.bottom());
}
if (border)
{
newGeometry.setLeft(newGeometry.left() - border->leftBorderWidth());
newGeometry.setRight(newGeometry.right() + border->rightBorderWidth());
newGeometry.setTop(newGeometry.top() - border->topBorderWidth());
newGeometry.setBottom(newGeometry.bottom() + border->bottomBorderWidth());
}
return newGeometry;
}