Skip to content

Commit 02143ea

Browse files
Added finishDragging() function to IFloatingWidget to prevent installing event filters
1 parent 5e230d8 commit 02143ea

File tree

7 files changed

+70
-76
lines changed

7 files changed

+70
-76
lines changed

src/DockAreaTabBar.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,8 +174,13 @@ void CDockAreaTabBar::mouseReleaseEvent(QMouseEvent* ev)
174174
{
175175
ADS_PRINT("CDockAreaTabBar::mouseReleaseEvent");
176176
ev->accept();
177-
d->FloatingWidget = nullptr;
178177
d->DragStartMousePos = QPoint();
178+
if (d->FloatingWidget)
179+
{
180+
auto FloatingWidget = d->FloatingWidget;
181+
d->FloatingWidget = nullptr;
182+
FloatingWidget->finishDragging();
183+
}
179184
return;
180185
}
181186
QScrollArea::mouseReleaseEvent(ev);

src/DockManager.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,8 @@ bool DockManagerPrivate::checkFormat(const QByteArray &state, int version)
205205
bool DockManagerPrivate::restoreStateFromXml(const QByteArray &state, int version,
206206
bool Testing)
207207
{
208+
Q_UNUSED(version);
209+
208210
if (state.isEmpty())
209211
{
210212
return false;

src/DockWidgetTab.cpp

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -311,14 +311,27 @@ void CDockWidgetTab::mousePressEvent(QMouseEvent* ev)
311311
//============================================================================
312312
void CDockWidgetTab::mouseReleaseEvent(QMouseEvent* ev)
313313
{
314-
// End of tab moving, emit signal
315-
if (d->isDraggingState(DraggingTab) && d->DockArea)
316-
{
317-
emit moved(ev->globalPos());
318-
}
319-
314+
auto CurrentDragState = d->DragState;
320315
d->DragStartMousePosition = QPoint();
321316
d->DragState = DraggingInactive;
317+
318+
switch (CurrentDragState)
319+
{
320+
case DraggingTab:
321+
// End of tab moving, emit signal
322+
if (d->DockArea)
323+
{
324+
emit moved(ev->globalPos());
325+
}
326+
break;
327+
328+
case DraggingFloatingWidget:
329+
d->FloatingWidget->finishDragging();
330+
break;
331+
332+
default:; // do nothing
333+
}
334+
322335
Super::mouseReleaseEvent(ev);
323336
}
324337

src/FloatingDockContainer.cpp

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -260,11 +260,6 @@ CFloatingDockContainer::CFloatingDockContainer(CDockManager *DockManager) :
260260
#endif
261261

262262
DockManager->registerFloatingWidget(this);
263-
264-
// We install an event filter to detect mouse release events because we
265-
// do not receive mouse release event if the floating widget is behind
266-
// the drop overlay cross
267-
qApp->installEventFilter(this);
268263
}
269264

270265
//============================================================================
@@ -493,20 +488,6 @@ bool CFloatingDockContainer::event(QEvent *e)
493488
return QWidget::event(e);
494489
}
495490

496-
//============================================================================
497-
bool CFloatingDockContainer::eventFilter(QObject *watched, QEvent *event)
498-
{
499-
Q_UNUSED(watched);
500-
if (event->type() == QEvent::MouseButtonRelease
501-
&& d->isState(DraggingFloatingWidget))
502-
{
503-
ADS_PRINT("FloatingWidget::eventFilter QEvent::MouseButtonRelease");
504-
finishDragging();
505-
d->titleMouseReleaseEvent();
506-
}
507-
508-
return false;
509-
}
510491

511492
//============================================================================
512493
void CFloatingDockContainer::startFloating(const QPoint &DragStartMousePos,
@@ -640,6 +621,7 @@ void CFloatingDockContainer::finishDragging()
640621
d->MouseEventHandler = nullptr;
641622
}
642623
#endif
624+
d->titleMouseReleaseEvent();
643625
}
644626

645627
} // namespace ads

src/FloatingDockContainer.h

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,11 @@ class IFloatingWidget
7474
* startFloating() was called
7575
*/
7676
virtual void moveFloating() = 0;
77+
78+
/**
79+
* Tells the widget that to finish dragging if the mouse is released
80+
*/
81+
virtual void finishDragging() = 0;
7782
};
7883

7984

@@ -110,8 +115,8 @@ private slots:
110115
* Use moveToGlobalPos() to move the widget to a new position
111116
* depending on the start position given in Pos parameter
112117
*/
113-
void startFloating(const QPoint& DragStartMousePos, const QSize& Size,
114-
eDragState DragState, QWidget* MouseEventHandler);
118+
virtual void startFloating(const QPoint& DragStartMousePos, const QSize& Size,
119+
eDragState DragState, QWidget* MouseEventHandler) override;
115120

116121
/**
117122
* Call this function to start dragging the floating widget
@@ -126,7 +131,7 @@ private slots:
126131
* Call this function if you explicitly want to signal that dragging has
127132
* finished
128133
*/
129-
void finishDragging();
134+
virtual void finishDragging() override;
130135

131136
/**
132137
* Call this function if you just want to initialize the position
@@ -164,7 +169,6 @@ private slots:
164169
virtual void closeEvent(QCloseEvent *event) override;
165170
virtual void hideEvent(QHideEvent *event) override;
166171
virtual void showEvent(QShowEvent *event) override;
167-
virtual bool eventFilter(QObject *watched, QEvent *event) override;
168172

169173
public:
170174
using Super = QWidget;

src/FloatingOverlay.cpp

Lines changed: 29 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ struct FloatingOverlayPrivate
3939
CDockContainerWidget *DropContainer = nullptr;
4040
qreal WindowOpacity;
4141
bool Hidden = false;
42-
bool IgnoreMouseEvents = false;
4342
QPixmap ContentPreviewPixmap;
4443

4544

@@ -184,10 +183,6 @@ CFloatingOverlay::CFloatingOverlay(QWidget* Content, QWidget* parent) :
184183
#endif
185184

186185
setWindowOpacity(0.6);
187-
// We install an event filter to detect mouse release events because we
188-
// do not receive mouse release event if the floating widget is behind
189-
// the drop overlay cross
190-
qApp->installEventFilter(this);
191186

192187
// Create a static image of the widget that should get undocked
193188
// This is like some kind preview image like it is uses in drag and drop
@@ -265,55 +260,44 @@ void CFloatingOverlay::moveEvent(QMoveEvent *event)
265260

266261

267262
//============================================================================
268-
bool CFloatingOverlay::eventFilter(QObject *watched, QEvent *event)
263+
void CFloatingOverlay::finishDragging()
269264
{
270-
Q_UNUSED(watched);
271-
if (event->type() == QEvent::MouseButtonRelease && !d->IgnoreMouseEvents)
265+
ADS_PRINT("CFloatingOverlay::finishDragging");
266+
auto DockDropArea = d->DockManager->dockAreaOverlay()->dropAreaUnderCursor();
267+
auto ContainerDropArea = d->DockManager->containerOverlay()->dropAreaUnderCursor();
268+
bool DropPossible = (DockDropArea != InvalidDockWidgetArea) || (ContainerDropArea != InvalidDockWidgetArea);
269+
if (d->DropContainer && DropPossible)
272270
{
273-
ADS_PRINT("FloatingWidget::eventFilter QEvent::MouseButtonRelease");
274-
275-
auto DockDropArea = d->DockManager->dockAreaOverlay()->dropAreaUnderCursor();
276-
auto ContainerDropArea = d->DockManager->containerOverlay()->dropAreaUnderCursor();
277-
bool DropPossible = (DockDropArea != InvalidDockWidgetArea) || (ContainerDropArea != InvalidDockWidgetArea);
278-
if (d->DropContainer && DropPossible)
271+
d->DropContainer->dropWidget(d->Content, QCursor::pos());
272+
}
273+
else
274+
{
275+
CDockWidget* DockWidget = qobject_cast<CDockWidget*>(d->Content);
276+
CFloatingDockContainer* FloatingWidget;
277+
if (DockWidget)
279278
{
280-
d->DropContainer->dropWidget(d->Content, QCursor::pos());
279+
FloatingWidget = new CFloatingDockContainer(DockWidget);
281280
}
282281
else
283282
{
284-
CDockWidget* DockWidget = qobject_cast<CDockWidget*>(d->Content);
285-
CFloatingDockContainer* FloatingWidget;
286-
if (DockWidget)
287-
{
288-
FloatingWidget = new CFloatingDockContainer(DockWidget);
289-
}
290-
else
291-
{
292-
CDockAreaWidget* DockArea = qobject_cast<CDockAreaWidget*>(d->Content);
293-
FloatingWidget = new CFloatingDockContainer(DockArea);
294-
}
295-
FloatingWidget->setGeometry(this->geometry());
296-
FloatingWidget->show();
297-
if (!CDockManager::configFlags().testFlag(CDockManager::DragPreviewHasWindowFrame))
298-
{
299-
QApplication::processEvents();
300-
int FrameHeight = FloatingWidget->frameGeometry().height() - FloatingWidget->geometry().height();
301-
QRect FixedGeometry = this->geometry();
302-
FixedGeometry.adjust(0, FrameHeight, 0, 0);
303-
FloatingWidget->setGeometry(FixedGeometry);
304-
}
283+
CDockAreaWidget* DockArea = qobject_cast<CDockAreaWidget*>(d->Content);
284+
FloatingWidget = new CFloatingDockContainer(DockArea);
285+
}
286+
FloatingWidget->setGeometry(this->geometry());
287+
FloatingWidget->show();
288+
if (!CDockManager::configFlags().testFlag(CDockManager::DragPreviewHasWindowFrame))
289+
{
290+
QApplication::processEvents();
291+
int FrameHeight = FloatingWidget->frameGeometry().height() - FloatingWidget->geometry().height();
292+
QRect FixedGeometry = this->geometry();
293+
FixedGeometry.adjust(0, FrameHeight, 0, 0);
294+
FloatingWidget->setGeometry(FixedGeometry);
305295
}
306-
307-
this->close();
308-
d->DockManager->containerOverlay()->hideOverlay();
309-
d->DockManager->dockAreaOverlay()->hideOverlay();
310-
// Because we use the event filter, we receive multiple mouse release
311-
// events. To prevent multiple code execution, we ignore all mouse
312-
// events after the first mouse event
313-
d->IgnoreMouseEvents = true;
314296
}
315297

316-
return false;
298+
this->close();
299+
d->DockManager->containerOverlay()->hideOverlay();
300+
d->DockManager->dockAreaOverlay()->hideOverlay();
317301
}
318302

319303

src/FloatingOverlay.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ class CFloatingOverlay : public QWidget, public IFloatingWidget
3131

3232
protected:
3333
virtual void moveEvent(QMoveEvent *event) override;
34-
virtual bool eventFilter(QObject *watched, QEvent *event) override;
3534
virtual void paintEvent(QPaintEvent *e) override;
3635

3736
/**
@@ -57,6 +56,11 @@ class CFloatingOverlay : public QWidget, public IFloatingWidget
5756
* startFloating() was called
5857
*/
5958
virtual void moveFloating() override;
59+
60+
/**
61+
* Finishes dragging
62+
*/
63+
virtual void finishDragging() override;
6064
};
6165

6266

0 commit comments

Comments
 (0)