38
38
#include < QDebug>
39
39
#include < QAbstractButton>
40
40
#include < QElapsedTimer>
41
+ #include < QTime>
41
42
42
43
#include " DockContainerWidget.h"
43
44
#include " DockAreaWidget.h"
@@ -66,6 +67,7 @@ struct FloatingDockContainerPrivate
66
67
QPoint DragStartMousePosition;
67
68
CDockContainerWidget *DropContainer = nullptr ;
68
69
CDockAreaWidget *SingleDockArea = nullptr ;
70
+ QPoint DragStartPos;
69
71
#ifdef Q_OS_LINUX
70
72
QWidget* MouseEventHandler = nullptr ;
71
73
CFloatingWidgetTitleBar* TitleBar = nullptr ;
@@ -109,6 +111,10 @@ struct FloatingDockContainerPrivate
109
111
#endif
110
112
}
111
113
114
+ /* *
115
+ * Reflect the current dock widget title in the floating widget windowTitle()
116
+ * depending on the CDockManager::FloatingContainerHasWidgetTitle flag
117
+ */
112
118
void reflectCurrentWidget (CDockWidget* CurrentWidget)
113
119
{
114
120
// reflect CurrentWidget's title if configured to do so, otherwise display application name as window title
@@ -133,6 +139,11 @@ struct FloatingDockContainerPrivate
133
139
_this->setWindowIcon (QApplication::windowIcon ());
134
140
}
135
141
}
142
+
143
+ /* *
144
+ * Handles escape key press when dragging around the floating widget
145
+ */
146
+ void handleEscapeKey ();
136
147
};
137
148
// struct FloatingDockContainerPrivate
138
149
@@ -264,6 +275,17 @@ void FloatingDockContainerPrivate::updateDropOverlays(const QPoint &GlobalPos)
264
275
}
265
276
}
266
277
278
+
279
+ // ============================================================================
280
+ void FloatingDockContainerPrivate::handleEscapeKey ()
281
+ {
282
+ ADS_PRINT (" FloatingDockContainerPrivate::handleEscapeKey()" );
283
+ setState (DraggingInactive);
284
+ DockManager->containerOverlay ()->hideOverlay ();
285
+ DockManager->dockAreaOverlay ()->hideOverlay ();
286
+ }
287
+
288
+
267
289
// ============================================================================
268
290
CFloatingDockContainer::CFloatingDockContainer (CDockManager *DockManager) :
269
291
tFloatingWidgetBase (DockManager),
@@ -363,6 +385,7 @@ void CFloatingDockContainer::moveEvent(QMoveEvent *event)
363
385
switch (d->DraggingState )
364
386
{
365
387
case DraggingMousePressed:
388
+ qApp->installEventFilter (this );
366
389
d->setState (DraggingFloatingWidget);
367
390
d->updateDropOverlays (QCursor::pos ());
368
391
break ;
@@ -380,6 +403,8 @@ void CFloatingDockContainer::moveEvent(QMoveEvent *event)
380
403
default :
381
404
break ;
382
405
}
406
+
407
+
383
408
}
384
409
385
410
// ============================================================================
@@ -443,6 +468,7 @@ void CFloatingDockContainer::showEvent(QShowEvent *event)
443
468
Super::showEvent (event);
444
469
}
445
470
471
+
446
472
// ============================================================================
447
473
bool CFloatingDockContainer::event (QEvent *e)
448
474
{
@@ -460,19 +486,16 @@ bool CFloatingDockContainer::event(QEvent *e)
460
486
#if (QT_VERSION >= QT_VERSION_CHECK(5, 12, 2))
461
487
if (e->type ()
462
488
== QEvent::NonClientAreaMouseButtonPress /* && QGuiApplication::mouseButtons().testFlag(Qt::LeftButton)*/ )
463
- {
464
- ADS_PRINT (" FloatingWidget::event Event::NonClientAreaMouseButtonPress" << e->type ());
465
- d->setState (DraggingMousePressed);
466
- }
467
489
#else
468
490
if (e->type () == QEvent::NonClientAreaMouseButtonPress && QGuiApplication::mouseButtons ().testFlag (Qt::LeftButton))
491
+ #endif
469
492
{
470
- ADS_PRINT (" FloatingWidget::event Event::NonClientAreaMouseButtonPress" << e->type ());
493
+ ADS_PRINT (" FloatingWidget::event Event::NonClientAreaMouseButtonPress" << e->type ());
494
+ d->DragStartPos = pos ();
471
495
d->setState (DraggingMousePressed);
472
496
}
473
- #endif
474
497
}
475
- break ;
498
+ break ;
476
499
477
500
case DraggingMousePressed:
478
501
switch (e->type ())
@@ -515,12 +538,49 @@ bool CFloatingDockContainer::event(QEvent *e)
515
538
}
516
539
517
540
#if (ADS_DEBUG_LEVEL > 0)
518
- qDebug () << " CFloatingDockContainer::event " << e->type ();
541
+ qDebug () << QTime::currentTime () << " CFloatingDockContainer::event " << e->type ();
519
542
#endif
520
543
return QWidget::event (e);
521
544
}
522
545
523
546
547
+ // ============================================================================
548
+ bool CFloatingDockContainer::eventFilter (QObject *watched, QEvent *e)
549
+ {
550
+ Q_UNUSED (watched);
551
+ // I have not found a way to detect non client area key press events to
552
+ // handle escape key presses. On Windows, if the escape key is pressed while
553
+ // dragging around a widget, the widget position is reset to its start position
554
+ // which in turn generates a QEvent::NonClientAreaMouseButtonRelease event
555
+ // if the mouse is outside of the widget after the move to its initial position
556
+ // or a QEvent::MouseButtonRelease event, if the mouse is inside of teh widget
557
+ // after the position has been reset.
558
+ // So we can install an event filter on the application to get these events
559
+ // here to properly cancel dragging and hide the overlays.
560
+ // If we are in DraggingFloatingWidget state, it means the widget
561
+ // has been dragged already but if the position is the same like
562
+ // the start position, then this is an indication that the escape
563
+ // key has been pressed.
564
+ if (e->type () == QEvent::MouseButtonRelease || e->type () == QEvent::NonClientAreaMouseButtonRelease)
565
+ {
566
+ ADS_PRINT (" CFloatingDockContainer::eventFilter QEvent::MouseButtonRelease or"
567
+ " QEvent::NonClientAreaMouseButtonRelease" << " d->DragggingState " << d->DraggingState );
568
+ qApp->removeEventFilter (this );
569
+ if (d->DragStartPos == pos ())
570
+ {
571
+ d->handleEscapeKey ();
572
+ return true ;
573
+ }
574
+ return false ;
575
+ }
576
+
577
+ #if (ADS_DEBUG_LEVEL > 0)
578
+ qDebug () << QTime::currentTime () << " CFloatingDockContainer::eventFilter " << e->type ();
579
+ #endif
580
+ return false ;
581
+ }
582
+
583
+
524
584
// ============================================================================
525
585
void CFloatingDockContainer::startFloating (const QPoint &DragStartMousePos,
526
586
const QSize &Size, eDragState DragState, QWidget *MouseEventHandler)
0 commit comments