Skip to content

Commit f964ce2

Browse files
Refactored, fixed and improved drag hover functionality
1 parent eb9b439 commit f964ce2

File tree

3 files changed

+86
-63
lines changed

3 files changed

+86
-63
lines changed

src/AutoHideDockContainer.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -660,8 +660,7 @@ bool CAutoHideDockContainer::event(QEvent* event)
660660
//============================================================================
661661
void CAutoHideDockContainer::dragLeaveEvent(QDragLeaveEvent*)
662662
{
663-
if (CDockManager::testAutoHideConfigFlag(
664-
CDockManager::AutoHideOpenOnDragHover))
663+
if (CDockManager::testAutoHideConfigFlag(CDockManager::AutoHideOpenOnDragHover))
665664
{
666665
collapseView(true);
667666
}

src/AutoHideTab.cpp

Lines changed: 84 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
#include <QElapsedTimer>
3535
#include <QMenu>
3636
#include <QEvent>
37+
#include <QTimer>
3738

3839
#include "AutoHideDockContainer.h"
3940
#include "AutoHideSideBar.h"
@@ -57,7 +58,7 @@ struct AutoHideTabPrivate
5758
CAutoHideSideBar* SideBar = nullptr;
5859
Qt::Orientation Orientation{Qt::Vertical};
5960
QElapsedTimer TimeSinceHoverMousePress;
60-
QElapsedTimer TimeSinceDragOver;
61+
QTimer DragOverTimer;
6162
bool MousePressed = false;
6263
eDragState DragState = DraggingInactive;
6364
QPoint GlobalDragStartMousePosition;
@@ -258,6 +259,11 @@ CAutoHideTab::CAutoHideTab(QWidget* parent) :
258259
if (CDockManager::testAutoHideConfigFlag(CDockManager::AutoHideOpenOnDragHover)) {
259260
setAcceptDrops(true);
260261
}
262+
263+
d->DragOverTimer.setInterval(CDockManager::configParam(
264+
CDockManager::AutoHideOpenOnDragHoverDelay_ms, 500).toInt());
265+
d->DragOverTimer.setSingleShot(true);
266+
connect(&d->DragOverTimer, &QTimer::timeout, this, &CAutoHideTab::onDragHoverDelayExpired);
261267
}
262268

263269

@@ -498,89 +504,83 @@ void CAutoHideTab::mouseReleaseEvent(QMouseEvent* ev)
498504

499505

500506
//============================================================================
501-
void CAutoHideTab::mouseMoveEvent(QMouseEvent* ev)
507+
void CAutoHideTab::mouseMoveEvent(QMouseEvent *ev)
502508
{
503-
if (!(ev->buttons() & Qt::LeftButton) || d->isDraggingState(DraggingInactive))
504-
{
505-
d->DragState = DraggingInactive;
506-
Super::mouseMoveEvent(ev);
507-
return;
508-
}
509+
if (!(ev->buttons() & Qt::LeftButton)
510+
|| d->isDraggingState(DraggingInactive))
511+
{
512+
d->DragState = DraggingInactive;
513+
Super::mouseMoveEvent(ev);
514+
return;
515+
}
509516

510-
// move floating window
511-
if (d->isDraggingState(DraggingFloatingWidget))
512-
{
513-
d->FloatingWidget->moveFloating();
514-
Super::mouseMoveEvent(ev);
515-
return;
516-
}
517+
// move floating window
518+
if (d->isDraggingState(DraggingFloatingWidget))
519+
{
520+
d->FloatingWidget->moveFloating();
521+
Super::mouseMoveEvent(ev);
522+
return;
523+
}
517524

518-
// move tab
519-
if (d->isDraggingState(DraggingTab))
520-
{
521-
// Moving the tab is always allowed because it does not mean moving the
522-
// dock widget around
523-
//d->moveTab(ev);
524-
}
525+
// move tab
526+
if (d->isDraggingState(DraggingTab))
527+
{
528+
// Moving the tab is always allowed because it does not mean moving the
529+
// dock widget around
530+
//d->moveTab(ev);
531+
}
525532

526-
auto MappedPos = mapToParent(ev->pos());
527-
bool MouseOutsideBar = (MappedPos.x() < 0) || (MappedPos.x() > parentWidget()->rect().right());
528-
// Maybe a fixed drag distance is better here ?
529-
int DragDistanceY = qAbs(d->GlobalDragStartMousePosition.y() - internal::globalPositionOf(ev).y());
530-
if (DragDistanceY >= CDockManager::startDragDistance() || MouseOutsideBar)
533+
auto MappedPos = mapToParent(ev->pos());
534+
bool MouseOutsideBar = (MappedPos.x() < 0)
535+
|| (MappedPos.x() > parentWidget()->rect().right());
536+
// Maybe a fixed drag distance is better here ?
537+
int DragDistanceY = qAbs(
538+
d->GlobalDragStartMousePosition.y()
539+
- internal::globalPositionOf(ev).y());
540+
if (DragDistanceY >= CDockManager::startDragDistance() || MouseOutsideBar)
531541
{
532-
// Floating is only allowed for widgets that are floatable
542+
// Floating is only allowed for widgets that are floatable
533543
// We can create the drag preview if the widget is movable.
534544
auto Features = d->DockWidget->features();
535-
if (Features.testFlag(CDockWidget::DockWidgetFloatable) || (Features.testFlag(CDockWidget::DockWidgetMovable)))
536-
{
537-
d->startFloating();
538-
}
539-
return;
545+
if (Features.testFlag(CDockWidget::DockWidgetFloatable)
546+
|| (Features.testFlag(CDockWidget::DockWidgetMovable)))
547+
{
548+
d->startFloating();
549+
}
550+
return;
540551
}
541552

542-
Super::mouseMoveEvent(ev);
553+
Super::mouseMoveEvent(ev);
543554
}
544555

545556
//============================================================================
546-
void CAutoHideTab::dragEnterEvent(QDragEnterEvent* ev) {
547-
if (CDockManager::testAutoHideConfigFlag(CDockManager::AutoHideOpenOnDragHover)) {
548-
if (!d->TimeSinceDragOver.isValid()) {
549-
d->TimeSinceDragOver.restart();
550-
ev->accept();
551-
}
552-
else if (d->TimeSinceDragOver.hasExpired(500)) {
553-
ev->accept();
554-
}
555-
}
557+
void CAutoHideTab::dragEnterEvent(QDragEnterEvent *ev)
558+
{
559+
Q_UNUSED(ev);
560+
if (CDockManager::testAutoHideConfigFlag(CDockManager::AutoHideOpenOnDragHover))
561+
{
562+
d->DragOverTimer.start();
563+
ev->accept();
564+
}
556565
}
557566

558567
//============================================================================
559-
void CAutoHideTab::dragLeaveEvent(QDragLeaveEvent* ev) {
560-
if (CDockManager::testAutoHideConfigFlag(CDockManager::AutoHideOpenOnDragHover)) {
561-
d->TimeSinceDragOver.invalidate();
562-
d->forwardEventToDockContainer(ev);
563-
}
568+
void CAutoHideTab::dragLeaveEvent(QDragLeaveEvent *ev)
569+
{
570+
Q_UNUSED(ev);
571+
if (CDockManager::testAutoHideConfigFlag(CDockManager::AutoHideOpenOnDragHover))
572+
{
573+
d->DragOverTimer.stop();
574+
}
564575
}
565576

566-
//============================================================================
567-
void CAutoHideTab::dragMoveEvent(QDragMoveEvent* ev) {
568-
if (CDockManager::testAutoHideConfigFlag(CDockManager::AutoHideOpenOnDragHover)
569-
&& d->TimeSinceDragOver.isValid()
570-
&& d->TimeSinceDragOver.hasExpired(500)) {
571-
d->TimeSinceDragOver.invalidate();
572-
d->DockWidget->autoHideDockContainer()->collapseView(false);
573-
ev->accept();
574-
}
575-
}
576577

577578
//============================================================================
578579
void CAutoHideTab::requestCloseDockWidget()
579580
{
580581
d->DockWidget->requestCloseDockWidget();
581582
}
582583

583-
584584
//============================================================================
585585
int CAutoHideTab::tabIndex() const
586586
{
@@ -593,4 +593,28 @@ int CAutoHideTab::tabIndex() const
593593
}
594594

595595

596+
//============================================================================
597+
void CAutoHideTab::onDragHoverDelayExpired()
598+
{
599+
static const char* const PropertyId = "ActiveDragOverAutoHideContainer";
600+
601+
// First we check if there is an active auto hide container that is visible
602+
// In this case, we collapse it before we open the new one
603+
auto v = d->DockWidget->dockManager()->property(PropertyId);
604+
if (v.isValid())
605+
{
606+
auto ActiveAutoHideContainer = v.value<QPointer<CAutoHideDockContainer>>();
607+
if (ActiveAutoHideContainer)
608+
{
609+
ActiveAutoHideContainer->collapseView(true);
610+
}
611+
}
612+
613+
auto AutoHideContainer = d->DockWidget->autoHideDockContainer();
614+
AutoHideContainer->collapseView(false);
615+
d->DockWidget->dockManager()->setProperty(PropertyId,
616+
QVariant::fromValue(QPointer(AutoHideContainer)));
617+
}
618+
619+
596620
}

src/AutoHideTab.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ class ADS_EXPORT CAutoHideTab : public CPushButton
6767

6868
private Q_SLOTS:
6969
void onAutoHideToActionClicked();
70+
void onDragHoverDelayExpired();
7071

7172
protected:
7273
void setSideBar(CAutoHideSideBar *SideTabBar);
@@ -78,7 +79,6 @@ private Q_SLOTS:
7879
virtual void mouseMoveEvent(QMouseEvent* ev) override;
7980
virtual void dragEnterEvent(QDragEnterEvent* ev) override;
8081
virtual void dragLeaveEvent(QDragLeaveEvent* ev) override;
81-
virtual void dragMoveEvent(QDragMoveEvent* ev) override;
8282

8383
public:
8484
using Super = CPushButton;

0 commit comments

Comments
 (0)