File: windowmanager.cpp

package info (click to toggle)
cantata 3.3.1.ds1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 38,300 kB
  • sloc: cpp: 105,773; perl: 1,366; xml: 723; python: 139; lex: 110; sh: 105; yacc: 78; makefile: 8
file content (617 lines) | stat: -rw-r--r-- 16,355 bytes parent folder | download
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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
/*
 * Cantata
 *
 * Copyright (c) 2011-2022 Craig Drummond <craig.p.drummond@gmail.com>
 *
 */

// Copied from oxygenwindowmanager.cpp svnversion: 1139230

//////////////////////////////////////////////////////////////////////////////
// oxygenwindowmanager.cpp
// pass some window mouse press/release/move event actions to window manager
// -------------------
//
// Copyright (c) 2010 Hugo Pereira Da Costa <hugo@oxygen-icons.org>
//
// Largely inspired from BeSpin style
// Copyright (C) 2007 Thomas Luebking <thomas.luebking@web.de>
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to
// deal in the Software without restriction, including without limitation the
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
// IN THE SOFTWARE.
//////////////////////////////////////////////////////////////////////////////

#include "windowmanager.h"
#include <QApplication>
#include <QComboBox>
#include <QDialog>
#include <QDockWidget>
#include <QGroupBox>
#include <QLabel>
#include <QListView>
#include <QMainWindow>
#include <QMenuBar>
#include <QMouseEvent>
#include <QProgressBar>
#include <QStatusBar>
#include <QStyle>
#include <QStyleOptionGroupBox>
#include <QTabBar>
#include <QTabWidget>
#include <QToolBar>
#include <QToolButton>
#include <QTreeView>

static inline bool isToolBar(QWidget* w)
{
	return qobject_cast<QToolBar*>(w) || 0 == strcmp(w->metaObject()->className(), "ToolBar");
}

static inline void addEventFilter(QObject* object, QObject* filter)
{
	object->removeEventFilter(filter);
	object->installEventFilter(filter);
}

WindowManager::WindowManager(QObject* parent)
	: QObject(parent), _useWMMoveResize(false), _dragMode(WM_DRAG_NONE), _dragDistance(QApplication::startDragDistance()), _dragDelay(QApplication::startDragTime()), _dragAboutToStart(false), _dragInProgress(false), _locked(false)
#ifndef Q_OS_MAC
	  ,
	  _cursorOverride(false)
#endif
{
	// install application wise event filter
	_appEventFilter = new AppEventFilter(this);
	qApp->installEventFilter(_appEventFilter);
}

void WindowManager::initialize(int windowDrag)
{
	setDragMode(windowDrag);
	setDragDelay(QApplication::startDragTime());
}

void WindowManager::registerWidgetAndChildren(QWidget* w)
{
	QObjectList children = w->children();

	for (QObject* o : children) {
		if (qobject_cast<QWidget*>(o)) {
			registerWidgetAndChildren((QWidget*)o);
		}
	}
	registerWidget(w);
}

void WindowManager::registerWidget(QWidget* widget)
{
	if (isBlackListed(widget)) {
		addEventFilter(widget, this);
	}
	else if (isDragable(widget)) {
		addEventFilter(widget, this);
	}
}

void WindowManager::unregisterWidget(QWidget* widget)
{
	if (widget) {
		widget->removeEventFilter(this);
	}
}

bool WindowManager::eventFilter(QObject* object, QEvent* event)
{
	if (!enabled()) {
		return false;
	}

	switch (event->type()) {
	case QEvent::MouseButtonPress:
		return mousePressEvent(object, event);
		break;
	case QEvent::MouseMove:
		if (object == _target.data()) {
			return mouseMoveEvent(object, event);
		}
		break;
	case QEvent::MouseButtonRelease:
		if (_target) {
			return mouseReleaseEvent(object, event);
		}
		break;
	default:
		break;
	}
	return false;
}

void WindowManager::timerEvent(QTimerEvent* event)
{
	if (event->timerId() == _dragTimer.timerId()) {
		_dragTimer.stop();
		if (_target) {
			startDrag(_target.data(), _globalDragPoint);
		}
	}
	else {
		return QObject::timerEvent(event);
	}
}

bool WindowManager::mousePressEvent(QObject* object, QEvent* event)
{
	// cast event and check buttons/modifiers
	QMouseEvent* mouseEvent = static_cast<QMouseEvent*>(event);
	if (!(Qt::NoModifier == mouseEvent->modifiers() && Qt::LeftButton == mouseEvent->button())) {
		return false;
	}

	// check lock
	if (isLocked()) {
		return false;
	}
	else {
		setLocked(true);
	}

	// cast to widget
	QWidget* widget = static_cast<QWidget*>(object);

	// check if widget can be dragged from current position
	if (isBlackListed(widget) || !canDrag(widget)) {
		return false;
	}

	// retrieve widget's child at event position
	QPoint position(mouseEvent->pos());
	QWidget* child = widget->childAt(position);
	if (!canDrag(widget, child, position)) {
		return false;
	}

	// save target and drag point
	_target = widget;
	_dragPoint = position;
	_globalDragPoint = mouseEvent->globalPosition();
	_dragAboutToStart = true;

	// send a move event to the current child with same position
	// if received, it is caught to actually start the drag
	QPointF localPoint(_dragPoint);
	if (child) {
		localPoint = child->mapFrom(widget, localPoint);
	}
	else {
		child = widget;
	}
	QMouseEvent localMouseEvent(QEvent::MouseMove, localPoint, _globalDragPoint, Qt::LeftButton, Qt::LeftButton, Qt::NoModifier);
	qApp->sendEvent(child, &localMouseEvent);
	// never eat event
	return false;
}

bool WindowManager::mouseMoveEvent(QObject* object, QEvent* event)
{
	Q_UNUSED(object)

	// stop timer
	if (_dragTimer.isActive()) {
		_dragTimer.stop();
	}

	// cast event and check drag distance
	QMouseEvent* mouseEvent = static_cast<QMouseEvent*>(event);
	if (!_dragInProgress) {
		if (_dragAboutToStart) {
			if (mouseEvent->globalPosition() == _globalDragPoint) {
				// start timer,
				_dragAboutToStart = false;
				if (_dragTimer.isActive()) {
					_dragTimer.stop();
				}
				_dragTimer.start(_dragDelay, this);
			}
			else {
				resetDrag();
			}
		}
		else if (QPointF(mouseEvent->globalPosition() - _globalDragPoint).manhattanLength() >= _dragDistance) {
			_dragTimer.start(0, this);
		}
		return true;
	}
	else if (!useWMMoveResize()) {
		// use QWidget::move for the grabbing
		/* this works only if the sending object and the target are identical */
		QWidget* window(_target.data()->window());
		window->move(window->pos() + mouseEvent->pos() - _dragPoint);
		return true;
	}
	else {
		return false;
	}
}

bool WindowManager::mouseReleaseEvent(QObject* object, QEvent* event)
{
	Q_UNUSED(object)
	Q_UNUSED(event)
	resetDrag();
	return false;
}

bool WindowManager::isDragable(QWidget* widget)
{
	// check widget
	if (!widget) {
		return false;
	}

	// accepted default types
	if ((qobject_cast<QDialog*>(widget) && widget->isWindow()) || (qobject_cast<QMainWindow*>(widget) && widget->isWindow()) || qobject_cast<QGroupBox*>(widget)) {
		return true;
	}

	// more accepted types, provided they are not dock widget titles
	if ((qobject_cast<QMenuBar*>(widget) || qobject_cast<QTabBar*>(widget) || qobject_cast<QStatusBar*>(widget) || isToolBar(widget)) && !isDockWidgetTitle(widget)) {
		return true;
	}

	// flat toolbuttons
	if (QToolButton* toolButton = qobject_cast<QToolButton*>(widget)) {
		if (toolButton->autoRaise()) {
			return true;
		}
	}

	// viewports
	/*
        one needs to check that
        1/ the widget parent is a scrollarea
        2/ it matches its parent viewport
        3/ the parent is not blacklisted
        */
	if (QListView* listView = qobject_cast<QListView*>(widget->parentWidget())) {
		if (listView->viewport() == widget && !isBlackListed(listView)) {
			return true;
		}
	}

	if (QTreeView* treeView = qobject_cast<QTreeView*>(widget->parentWidget())) {
		if (treeView->viewport() == widget && !isBlackListed(treeView)) {
			return true;
		}
	}

	/*
        catch labels in status bars.
        this is because of kstatusbar
        who captures buttonPress/release events
        */
	if (QLabel* label = qobject_cast<QLabel*>(widget)) {
		if (label->textInteractionFlags().testFlag(Qt::TextSelectableByMouse)) {
			return false;
		}

		QWidget* parent = label->parentWidget();
		while (parent) {
			if (qobject_cast<QStatusBar*>(parent)) {
				return true;
			}
			parent = parent->parentWidget();
		}
	}

	return false;
}

bool WindowManager::isBlackListed(QWidget* widget)
{
	QVariant propertyValue(widget->property("_kde_no_window_grab"));
	return propertyValue.isValid() && propertyValue.toBool();
}

bool WindowManager::canDrag(QWidget* widget)
{
	// check if enabled
	if (!enabled()) {
		return false;
	}

	// assume isDragable widget is already passed
	// check some special cases where drag should not be effective

	// check mouse grabber
	if (QWidget::mouseGrabber()) {
		return false;
	}

	/*
        check cursor shape.
        Assume that a changed cursor means that some action is in progress
        and should prevent the drag
        */
	if (Qt::ArrowCursor != widget->cursor().shape()) {
		return false;
	}

	// accept
	return true;
}

bool WindowManager::canDrag(QWidget* widget, QWidget* child, const QPoint& position)
{
	// retrieve child at given position and check cursor again
	if (child && Qt::ArrowCursor != child->cursor().shape()) {
		return false;
	}

	/*
        check against children from which drag should never be enabled,
        even if mousePress/Move has been passed to the parent
        */
	if (child && (qobject_cast<QComboBox*>(child) || qobject_cast<QProgressBar*>(child))) {
		return false;
	}

	// tool buttons
	if (QToolButton* toolButton = qobject_cast<QToolButton*>(widget)) {
		if (dragMode() < WM_DRAG_ALL && !isToolBar(widget->parentWidget())) {
			return false;
		}
		return toolButton->autoRaise() && !toolButton->isEnabled();
	}

	// check menubar
	if (QMenuBar* menuBar = qobject_cast<QMenuBar*>(widget)) {
		// check if there is an active action
		if (menuBar->activeAction() && menuBar->activeAction()->isEnabled()) {
			return false;
		}

		// check if action at position exists and is enabled
		if (QAction* action = menuBar->actionAt(position)) {
			if (action->isSeparator()) {
				return true;
			}
			if (action->isEnabled()) {
				return false;
			}
		}

		// return true in all other cases
		return true;
	}

	bool toolbar = isToolBar(widget);
	if (dragMode() < WM_DRAG_MENU_AND_TOOLBAR && toolbar) {
		return false;
	}

	/*
        in MINIMAL mode, anything that has not been already accepted
        and does not come from a toolbar is rejected
        */
	if (dragMode() < WM_DRAG_ALL) {
		return toolbar;
	}

	/* following checks are relevant only for WD_FULL mode */

	// tabbar. Make sure no tab is under the cursor
	if (QTabBar* tabBar = qobject_cast<QTabBar*>(widget)) {
		return -1 == tabBar->tabAt(position);
	}

	/*
        check groupboxes
        prevent drag if unchecking grouboxes
        */
	if (QGroupBox* groupBox = qobject_cast<QGroupBox*>(widget)) {
		// non checkable group boxes are always ok
		if (!groupBox->isCheckable()) {
			return true;
		}
		// gather options to retrieve checkbox subcontrol rect
		QStyleOptionGroupBox opt;
		opt.initFrom(groupBox);
		if (groupBox->isFlat()) {
			opt.features |= QStyleOptionFrame::Flat;
		}
		opt.lineWidth = 1;
		opt.midLineWidth = 0;
		opt.text = groupBox->title();
		opt.textAlignment = groupBox->alignment();
		opt.subControls = (QStyle::SC_GroupBoxFrame | QStyle::SC_GroupBoxCheckBox);
		if (!groupBox->title().isEmpty()) {
			opt.subControls |= QStyle::SC_GroupBoxLabel;
		}

		opt.state |= (groupBox->isChecked() ? QStyle::State_On : QStyle::State_Off);

		// check against groupbox checkbox
		if (groupBox->style()->subControlRect(QStyle::CC_GroupBox, &opt, QStyle::SC_GroupBoxCheckBox, groupBox).contains(position)) {
			return false;
		}

		// check against groupbox label
		if (!groupBox->title().isEmpty() && groupBox->style()->subControlRect(QStyle::CC_GroupBox, &opt, QStyle::SC_GroupBoxLabel, groupBox).contains(position)) {
			return false;
		}

		return true;
	}

	// labels
	if (QLabel* label = qobject_cast<QLabel*>(widget)) {
		if (label->textInteractionFlags().testFlag(Qt::TextSelectableByMouse)) {
			return false;
		}
	}

	// abstract item views
	QAbstractItemView* itemView(NULL);
	if ((itemView = qobject_cast<QListView*>(widget->parentWidget())) || (itemView = qobject_cast<QTreeView*>(widget->parentWidget()))) {
		if (widget == itemView->viewport()) {
			// QListView
			if (QFrame::NoFrame != itemView->frameShape()) {
				return false;
			}
			else if (QAbstractItemView::NoSelection != itemView->selectionMode() && QAbstractItemView::SingleSelection != itemView->selectionMode() && itemView->model() && itemView->model()->rowCount()) {
				return false;
			}
			else if (itemView->model() && itemView->indexAt(position).isValid()) {
				return false;
			}
		}
	}
	else if ((itemView = qobject_cast<QAbstractItemView*>(widget->parentWidget()))) {
		if (widget == itemView->viewport()) {
			// QAbstractItemView
			if (QFrame::NoFrame != itemView->frameShape()) {
				return false;
			}
			else if (itemView->indexAt(position).isValid()) {
				return false;
			}
		}
	}
	return true;
}

void WindowManager::resetDrag(void)
{
#ifndef Q_OS_MAC
	if ((!useWMMoveResize()) && _target && _cursorOverride) {
		qApp->restoreOverrideCursor();
		_cursorOverride = false;
	}
#endif

	_target.clear();
	if (_dragTimer.isActive()) {
		_dragTimer.stop();
	}
	_dragPoint = QPoint();
	_globalDragPoint = QPoint();
	_dragAboutToStart = false;
	_dragInProgress = false;
}

void WindowManager::startDrag(QWidget* widget, const QPointF& position)
{
	if (!(enabled() && widget)) {
		return;
	}
	if (QWidget::mouseGrabber()) {
		return;
	}

	// ungrab pointer
	if (useWMMoveResize()) {
		Q_UNUSED(position)
	}

#ifndef Q_OS_MAC
	if (!useWMMoveResize() && !_cursorOverride) {
		qApp->setOverrideCursor(Qt::DragMoveCursor);
		_cursorOverride = true;
	}
#endif

	_dragInProgress = true;
	return;
}

bool WindowManager::supportWMMoveResize(void) const
{
	return false;
}

bool WindowManager::isDockWidgetTitle(const QWidget* widget) const
{
	if (!widget) {
		return false;
	}
	if (const QDockWidget* dockWidget = qobject_cast<const QDockWidget*>(widget->parent())) {
		return widget == dockWidget->titleBarWidget();
	}
	else {
		return false;
	}
}

bool WindowManager::AppEventFilter::eventFilter(QObject* object, QEvent* event)
{
	if (QEvent::MouseButtonRelease == event->type()) {
		// stop drag timer
		if (_parent->_dragTimer.isActive()) {
			_parent->resetDrag();
		}

		// unlock
		if (_parent->isLocked()) {
			_parent->setLocked(false);
		}
	}

	if (!_parent->enabled()) {
		return false;
	}

	/*
        if a drag is in progress, the widget will not receive any event
        we trigger on the first MouseMove or MousePress events that are received
        by any widget in the application to detect that the drag is finished
        */
	if (_parent->useWMMoveResize() && _parent->_dragInProgress && _parent->_target && (QEvent::MouseMove == event->type() || QEvent::MouseButtonPress == event->type())) {
		return appMouseEvent(object, event);
	}

	return false;
}

bool WindowManager::AppEventFilter::appMouseEvent(QObject* object, QEvent* event)
{
	Q_UNUSED(object)

	// store target window (see later)
	QWidget* window(_parent->_target.data()->window());

	/*
        post some mouseRelease event to the target, in order to counter balance
        the mouse press that triggered the drag. Note that it triggers a resetDrag
        */
	QMouseEvent mouseEvent(QEvent::MouseButtonRelease, _parent->_dragPoint, _parent->_globalDragPoint, Qt::LeftButton, Qt::LeftButton, Qt::NoModifier);
	qApp->sendEvent(_parent->_target.data(), &mouseEvent);

	if (QEvent::MouseMove == event->type()) {
		/*
            HACK: quickly move the main cursor out of the window and back
            this is needed to get the focus right for the window children
            the origin of this issue is unknown at the moment
            */
		const QPoint cursor = QCursor::pos();
		QCursor::setPos(window->mapToGlobal(window->rect().topRight()) + QPoint(1, 0));
		QCursor::setPos(cursor);
	}
	return true;
}

#include "moc_windowmanager.cpp"