Switched from local event pos to global event pos in DockWidgetTab to fix jumping tabs when hiding / showing tabs close button

This commit is contained in:
Uwe Kindler 2020-01-15 10:03:50 +01:00
parent 50c3066eaa
commit 418d0740d2
2 changed files with 19 additions and 16 deletions

View File

@ -408,14 +408,15 @@ CMainWindow::CMainWindow(QWidget *parent) :
// a QToolButton instead of a QPushButton // a QToolButton instead of a QPushButton
// CDockManager::setConfigFlags(CDockManager::configFlags() | CDockManager::TabCloseButtonIsToolButton); // CDockManager::setConfigFlags(CDockManager::configFlags() | CDockManager::TabCloseButtonIsToolButton);
// uncomment the following line if you want a fixed tab width that does
// not change if the visibility of the close button changes
// CDockManager::setConfigFlag(CDockManager::RetainTabSizeWhenCloseButtonHidden, true);
// comment the following line if you want to use opaque undocking and // comment the following line if you want to use opaque undocking and
// opaque splitter resizing // opaque splitter resizing
CDockManager::setConfigFlags(CDockManager::DefaultNonOpaqueConfig); CDockManager::setConfigFlags(CDockManager::DefaultNonOpaqueConfig);
// uncomment the following line if you want a fixed tab width that does
// not change if the visibility of the close button changes
//CDockManager::setConfigFlag(CDockManager::RetainTabSizeWhenCloseButtonHidden, true);
// Now create the dock manager and its content // Now create the dock manager and its content
d->DockManager = new CDockManager(this); d->DockManager = new CDockManager(this);

View File

@ -221,7 +221,7 @@ void DockWidgetTabPrivate::moveTab(QMouseEvent* ev)
ev->accept(); ev->accept();
int left, top, right, bottom; int left, top, right, bottom;
_this->getContentsMargins(&left, &top, &right, &bottom); _this->getContentsMargins(&left, &top, &right, &bottom);
QPoint moveToPos = _this->mapToParent(ev->pos()) - DragStartMousePosition; QPoint moveToPos = ev->globalPos() - DragStartMousePosition;
moveToPos.setY(0); moveToPos.setY(0);
_this->move(moveToPos); _this->move(moveToPos);
_this->raise(); _this->raise();
@ -266,14 +266,16 @@ bool DockWidgetTabPrivate::startFloating(eDragState DraggingState)
if (DraggingFloatingWidget == DraggingState) if (DraggingFloatingWidget == DraggingState)
{ {
FloatingWidget->startFloating(DragStartMousePosition, Size, DraggingFloatingWidget, _this); FloatingWidget->startFloating(_this->mapFromGlobal(DragStartMousePosition),
Size, DraggingFloatingWidget, _this);
auto Overlay = DockWidget->dockManager()->containerOverlay(); auto Overlay = DockWidget->dockManager()->containerOverlay();
Overlay->setAllowedAreas(OuterDockAreas); Overlay->setAllowedAreas(OuterDockAreas);
this->FloatingWidget = FloatingWidget; this->FloatingWidget = FloatingWidget;
} }
else else
{ {
FloatingWidget->startFloating(DragStartMousePosition, Size, DraggingInactive, nullptr); FloatingWidget->startFloating(_this->mapFromGlobal(DragStartMousePosition),
Size, DraggingInactive, nullptr);
} }
return true; return true;
@ -304,7 +306,7 @@ void CDockWidgetTab::mousePressEvent(QMouseEvent* ev)
if (ev->button() == Qt::LeftButton) if (ev->button() == Qt::LeftButton)
{ {
ev->accept(); ev->accept();
d->DragStartMousePosition = ev->pos(); d->DragStartMousePosition = ev->globalPos();
d->DragState = DraggingMousePressed; d->DragState = DraggingMousePressed;
emit clicked(); emit clicked();
return; return;
@ -372,7 +374,7 @@ void CDockWidgetTab::mouseMoveEvent(QMouseEvent* ev)
} }
// Maybe a fixed drag distance is better here ? // Maybe a fixed drag distance is better here ?
int DragDistanceY = qAbs(d->DragStartMousePosition.y() - ev->pos().y()); int DragDistanceY = qAbs(d->DragStartMousePosition.y() - ev->globalPos().y());
if (DragDistanceY >= CDockManager::startDragDistance()) if (DragDistanceY >= CDockManager::startDragDistance())
{ {
// If this is the last dock area in a dock container with only // If this is the last dock area in a dock container with only
@ -399,7 +401,7 @@ void CDockWidgetTab::mouseMoveEvent(QMouseEvent* ev)
return; return;
} }
else if (d->DockArea->openDockWidgetsCount() > 1 else if (d->DockArea->openDockWidgetsCount() > 1
&& (ev->pos() - d->DragStartMousePosition).manhattanLength() >= QApplication::startDragDistance()) // Wait a few pixels before start moving && (ev->globalPos() - d->DragStartMousePosition).manhattanLength() >= QApplication::startDragDistance()) // Wait a few pixels before start moving
{ {
// If we start dragging the tab, we save its inital position to // If we start dragging the tab, we save its inital position to
// restore it later // restore it later
@ -424,7 +426,7 @@ void CDockWidgetTab::contextMenuEvent(QContextMenuEvent* ev)
return; return;
} }
d->DragStartMousePosition = ev->pos(); d->DragStartMousePosition = ev->globalPos();
QMenu Menu(this); QMenu Menu(this);
auto Action = Menu.addAction(tr("Detach"), this, SLOT(detachDockWidget())); auto Action = Menu.addAction(tr("Detach"), this, SLOT(detachDockWidget()));
Action->setEnabled(d->DockWidget->features().testFlag(CDockWidget::DockWidgetFloatable)); Action->setEnabled(d->DockWidget->features().testFlag(CDockWidget::DockWidgetFloatable));
@ -432,7 +434,7 @@ void CDockWidgetTab::contextMenuEvent(QContextMenuEvent* ev)
Action = Menu.addAction(tr("Close"), this, SIGNAL(closeRequested())); Action = Menu.addAction(tr("Close"), this, SIGNAL(closeRequested()));
Action->setEnabled(isClosable()); Action->setEnabled(isClosable());
Menu.addAction(tr("Close Others"), this, SIGNAL(closeOtherTabsRequested())); Menu.addAction(tr("Close Others"), this, SIGNAL(closeOtherTabsRequested()));
Menu.exec(mapToGlobal(ev->pos())); Menu.exec(ev->globalPos());
} }
@ -549,7 +551,7 @@ void CDockWidgetTab::mouseDoubleClickEvent(QMouseEvent *event)
if ((!d->DockArea->dockContainer()->isFloating() || d->DockArea->dockWidgetsCount() > 1) if ((!d->DockArea->dockContainer()->isFloating() || d->DockArea->dockWidgetsCount() > 1)
&& d->DockWidget->features().testFlag(CDockWidget::DockWidgetFloatable)) && d->DockWidget->features().testFlag(CDockWidget::DockWidgetFloatable))
{ {
d->DragStartMousePosition = event->pos(); d->DragStartMousePosition = event->globalPos();
d->startFloating(DraggingInactive); d->startFloating(DraggingInactive);
} }
@ -587,7 +589,7 @@ void CDockWidgetTab::detachDockWidget()
{ {
return; return;
} }
d->DragStartMousePosition = mapFromGlobal(QCursor::pos()); d->DragStartMousePosition = QCursor::pos();
d->startFloating(DraggingInactive); d->startFloating(DraggingInactive);
} }
@ -595,13 +597,13 @@ void CDockWidgetTab::detachDockWidget()
//============================================================================ //============================================================================
bool CDockWidgetTab::event(QEvent *e) bool CDockWidgetTab::event(QEvent *e)
{ {
#ifndef QT_NO_TOOLTIP #ifndef QT_NO_TOOLTIP
if (e->type() == QEvent::ToolTipChange) if (e->type() == QEvent::ToolTipChange)
{ {
const auto text = toolTip(); const auto text = toolTip();
d->TitleLabel->setToolTip(text); d->TitleLabel->setToolTip(text);
} }
#endif #endif
return Super::event(e); return Super::event(e);
} }