From ab71c9c996b217d037e5430d04b3ce36e3c2b928 Mon Sep 17 00:00:00 2001 From: Volker Hilsheimer Date: Fri, 3 Apr 2020 09:57:10 +0200 Subject: [PATCH] Unexport QWidgetResizeHandler and remove move functionality Address FIXME comment. The class is not public, and only used in QtWidgets for the resizing of docking widgets. The move functionality is unused, and has been removed. Change-Id: Id477f36cb7d449b06e124950fed6f5f182aa5721 Reviewed-by: Richard Moe Gustavsen Reviewed-by: Friedemann Kleint --- src/widgets/widgets/qdockwidget.cpp | 6 +- src/widgets/widgets/qmainwindowlayout.cpp | 3 +- src/widgets/widgets/qwidgetresizehandler.cpp | 106 +++++-------------- src/widgets/widgets/qwidgetresizehandler_p.h | 34 ++---- 4 files changed, 39 insertions(+), 110 deletions(-) diff --git a/src/widgets/widgets/qdockwidget.cpp b/src/widgets/widgets/qdockwidget.cpp index 1d358c493e..cfe17af6e9 100644 --- a/src/widgets/widgets/qdockwidget.cpp +++ b/src/widgets/widgets/qdockwidget.cpp @@ -887,12 +887,10 @@ void QDockWidgetPrivate::endDrag(bool abort) void QDockWidgetPrivate::setResizerActive(bool active) { Q_Q(QDockWidget); - if (active && !resizer) { + if (active && !resizer) resizer = new QWidgetResizeHandler(q); - resizer->setMovingEnabled(false); - } if (resizer) - resizer->setActive(QWidgetResizeHandler::Resize, active); + resizer->setEnabled(active); } bool QDockWidgetPrivate::isAnimating() const diff --git a/src/widgets/widgets/qmainwindowlayout.cpp b/src/widgets/widgets/qmainwindowlayout.cpp index 30562d8270..83697278b8 100644 --- a/src/widgets/widgets/qmainwindowlayout.cpp +++ b/src/widgets/widgets/qmainwindowlayout.cpp @@ -200,7 +200,6 @@ public: QDockWidgetGroupLayout(QDockWidgetGroupWindow* parent) : QLayout(parent) { setSizeConstraint(QLayout::SetMinAndMaxSize); resizer = new QWidgetResizeHandler(parent); - resizer->setMovingEnabled(false); } ~QDockWidgetGroupLayout() { layoutState.deleteAllLayoutItems(); @@ -259,7 +258,7 @@ public: li->apply(false); if (savedState.rect.isValid()) savedState.rect = li->rect; - resizer->setActive(QWidgetResizeHandler::Resize, !nativeWindowDeco()); + resizer->setEnabled(!nativeWindowDeco()); } QDockAreaLayoutInfo *dockAreaLayoutInfo() { return &layoutState; } diff --git a/src/widgets/widgets/qwidgetresizehandler.cpp b/src/widgets/widgets/qwidgetresizehandler.cpp index 9dbba12aba..43aa4c7988 100644 --- a/src/widgets/widgets/qwidgetresizehandler.cpp +++ b/src/widgets/widgets/qwidgetresizehandler.cpp @@ -58,46 +58,37 @@ QT_BEGIN_NAMESPACE static bool resizeHorizontalDirectionFixed = false; static bool resizeVerticalDirectionFixed = false; -// ### fixme: Qt 6: No longer export QWidgetResizeHandler and remove "Move" -// functionality. Currently, only the resize functionality is used by QDockWidget. -// Historically, the class was used in Qt 3's QWorkspace (predecessor to QMdiArea). - QWidgetResizeHandler::QWidgetResizeHandler(QWidget *parent, QWidget *cw) : QObject(parent), widget(parent), childWidget(cw ? cw : parent), - fw(0), extrahei(0), buttonDown(false), moveResizeMode(false), sizeprotect(true), movingEnabled(true) + fw(0), extrahei(0), buttonDown(false), active(false) { mode = Nowhere; widget->setMouseTracking(true); QFrame *frame = qobject_cast(widget); range = frame ? frame->frameWidth() : RANGE; range = qMax(RANGE, range); - activeForMove = activeForResize = true; + enabled = true; widget->installEventFilter(this); } -void QWidgetResizeHandler::setActive(Action ac, bool b) +void QWidgetResizeHandler::setEnabled(bool b) { - if (ac & Move) - activeForMove = b; - if (ac & Resize) - activeForResize = b; + if (b == enabled) + return; - if (!isActive()) + enabled = b; + if (!enabled) setMouseCursor(Nowhere); } -bool QWidgetResizeHandler::isActive(Action ac) const +bool QWidgetResizeHandler::isEnabled() const { - bool b = false; - if (ac & Move) b = activeForMove; - if (ac & Resize) b |= activeForResize; - - return b; + return enabled; } bool QWidgetResizeHandler::eventFilter(QObject *o, QEvent *ee) { - if (!isActive() + if (!isEnabled() || (ee->type() != QEvent::MouseButtonPress && ee->type() != QEvent::MouseButtonRelease && ee->type() != QEvent::MouseMove @@ -126,35 +117,24 @@ bool QWidgetResizeHandler::eventFilter(QObject *o, QEvent *ee) if (e->button() == Qt::LeftButton) { buttonDown = false; emit activate(); - bool me = movingEnabled; - movingEnabled = (me && o == widget); mouseMoveEvent(e); - movingEnabled = me; buttonDown = true; moveOffset = widget->mapFromGlobal(e->globalPos()); invertedMoveOffset = widget->rect().bottomRight() - moveOffset; - if (mode == Center) { - if (movingEnabled) - return true; - } else { + if (mode != Center) return true; - } } } break; case QEvent::MouseButtonRelease: if (w->isMaximized()) break; if (static_cast(ee)->button() == Qt::LeftButton) { - moveResizeMode = false; + active = false; buttonDown = false; widget->releaseMouse(); widget->releaseKeyboard(); - if (mode == Center) { - if (movingEnabled) - return true; - } else { + if (mode != Center) return true; - } } break; case QEvent::MouseMove: { @@ -162,16 +142,9 @@ bool QWidgetResizeHandler::eventFilter(QObject *o, QEvent *ee) break; QMouseEvent *e = static_cast(ee); buttonDown = buttonDown && (e->buttons() & Qt::LeftButton); // safety, state machine broken! - bool me = movingEnabled; - movingEnabled = (me && o == widget && (buttonDown || moveResizeMode)); mouseMoveEvent(e); - movingEnabled = me; - if (mode == Center) { - if (movingEnabled) - return true; - } else { + if (mode != Center) return true; - } } break; case QEvent::KeyPress: keyPressEvent(static_cast(ee)); @@ -193,7 +166,7 @@ bool QWidgetResizeHandler::eventFilter(QObject *o, QEvent *ee) void QWidgetResizeHandler::mouseMoveEvent(QMouseEvent *e) { QPoint pos = widget->mapFromGlobal(e->globalPos()); - if (!moveResizeMode && !buttonDown) { + if (!active && !buttonDown) { if (pos.y() <= range && pos.x() <= range) mode = TopLeft; else if (pos.y() >= widget->height()-range && pos.x() >= widget->width()-range) @@ -215,7 +188,7 @@ void QWidgetResizeHandler::mouseMoveEvent(QMouseEvent *e) else mode = Nowhere; - if (widget->isMinimized() || !isActive(Resize)) + if (widget->isMinimized() || !isEnabled()) mode = Center; #ifndef QT_NO_CURSOR setMouseCursor(mode); @@ -223,7 +196,7 @@ void QWidgetResizeHandler::mouseMoveEvent(QMouseEvent *e) return; } - if (mode == Center && !movingEnabled) + if (mode == Center) return; if (widget->testAttribute(Qt::WA_WState_ConfigPending)) @@ -237,9 +210,9 @@ void QWidgetResizeHandler::mouseMoveEvent(QMouseEvent *e) globalPos.rx() = 0; if (globalPos.y() < 0) globalPos.ry() = 0; - if (sizeprotect && globalPos.x() > widget->parentWidget()->width()) + if (globalPos.x() > widget->parentWidget()->width()) globalPos.rx() = widget->parentWidget()->width(); - if (sizeprotect && globalPos.y() > widget->parentWidget()->height()) + if (globalPos.y() > widget->parentWidget()->height()) globalPos.ry() = widget->parentWidget()->height(); } @@ -300,9 +273,6 @@ void QWidgetResizeHandler::mouseMoveEvent(QMouseEvent *e) case Right: geom = QRect(widget->geometry().topLeft(), QPoint(p.x(), widget->geometry().bottom())) ; break; - case Center: - geom.moveTopLeft(pp); - break; default: break; } @@ -314,10 +284,7 @@ void QWidgetResizeHandler::mouseMoveEvent(QMouseEvent *e) if (geom != widget->geometry() && (widget->isWindow() || widget->parentWidget()->rect().intersects(geom))) { - if (mode == Center) - widget->move(geom.topLeft()); - else - widget->setGeometry(geom); + widget->setGeometry(geom); } } @@ -361,7 +328,7 @@ void QWidgetResizeHandler::setMouseCursor(MousePosition m) void QWidgetResizeHandler::keyPressEvent(QKeyEvent * e) { - if (!isMove() && !isResize()) + if (!isResizing()) return; bool is_control = e->modifiers() & Qt::ControlModifier; int delta = is_control?1:8; @@ -378,7 +345,7 @@ void QWidgetResizeHandler::keyPressEvent(QKeyEvent * e) invertedMoveOffset.rx() -= delta; } } - if (isResize() && !resizeHorizontalDirectionFixed) { + if (isResizing() && !resizeHorizontalDirectionFixed) { resizeHorizontalDirectionFixed = true; if (mode == BottomRight) mode = BottomLeft; @@ -403,7 +370,7 @@ void QWidgetResizeHandler::keyPressEvent(QKeyEvent * e) invertedMoveOffset.rx() -= delta; } } - if (isResize() && !resizeHorizontalDirectionFixed) { + if (isResizing() && !resizeHorizontalDirectionFixed) { resizeHorizontalDirectionFixed = true; if (mode == BottomLeft) mode = BottomRight; @@ -428,7 +395,7 @@ void QWidgetResizeHandler::keyPressEvent(QKeyEvent * e) invertedMoveOffset.ry() -= delta; } } - if (isResize() && !resizeVerticalDirectionFixed) { + if (isResizing() && !resizeVerticalDirectionFixed) { resizeVerticalDirectionFixed = true; if (mode == BottomLeft) mode = TopLeft; @@ -453,7 +420,7 @@ void QWidgetResizeHandler::keyPressEvent(QKeyEvent * e) invertedMoveOffset.ry() -= delta; } } - if (isResize() && !resizeVerticalDirectionFixed) { + if (isResizing() && !resizeVerticalDirectionFixed) { resizeVerticalDirectionFixed = true; if (mode == TopLeft) mode = BottomLeft; @@ -471,7 +438,7 @@ void QWidgetResizeHandler::keyPressEvent(QKeyEvent * e) case Qt::Key_Return: case Qt::Key_Enter: case Qt::Key_Escape: - moveResizeMode = false; + active = false; widget->releaseMouse(); widget->releaseKeyboard(); buttonDown = false; @@ -485,10 +452,10 @@ void QWidgetResizeHandler::keyPressEvent(QKeyEvent * e) void QWidgetResizeHandler::doResize() { - if (!activeForResize) + if (!enabled) return; - moveResizeMode = true; + active = true; moveOffset = widget->mapFromGlobal(QCursor::pos()); if (moveOffset.x() < widget->width()/2) { if (moveOffset.y() < widget->height()/2) @@ -513,23 +480,6 @@ void QWidgetResizeHandler::doResize() resizeVerticalDirectionFixed = false; } -void QWidgetResizeHandler::doMove() -{ - if (!activeForMove) - return; - - mode = Center; - moveResizeMode = true; - moveOffset = widget->mapFromGlobal(QCursor::pos()); - invertedMoveOffset = widget->rect().bottomRight() - moveOffset; -#ifndef QT_NO_CURSOR - widget->grabMouse(Qt::SizeAllCursor); -#else - widget->grabMouse(); -#endif - widget->grabKeyboard(); -} - QT_END_NAMESPACE #include "moc_qwidgetresizehandler_p.cpp" diff --git a/src/widgets/widgets/qwidgetresizehandler_p.h b/src/widgets/widgets/qwidgetresizehandler_p.h index df3ac7cb8a..2390ba074a 100644 --- a/src/widgets/widgets/qwidgetresizehandler_p.h +++ b/src/widgets/widgets/qwidgetresizehandler_p.h @@ -62,34 +62,22 @@ QT_BEGIN_NAMESPACE class QMouseEvent; class QKeyEvent; -class Q_WIDGETS_EXPORT QWidgetResizeHandler : public QObject +class QWidgetResizeHandler : public QObject { Q_OBJECT public: - enum Action { - Move = 0x01, - Resize = 0x02, - Any = Move|Resize - }; - explicit QWidgetResizeHandler(QWidget *parent, QWidget *cw = nullptr); - void setActive(bool b) { setActive(Any, b); } - void setActive(Action ac, bool b); - bool isActive() const { return isActive(Any); } - bool isActive(Action ac) const; - void setMovingEnabled(bool b) { movingEnabled = b; } - bool isMovingEnabled() const { return movingEnabled; } + void setEnabled(bool b); + bool isEnabled() const; bool isButtonDown() const { return buttonDown; } void setExtraHeight(int h) { extrahei = h; } - void setSizeProtection(bool b) { sizeprotect = b; } void setFrameWidth(int w) { fw = w; } void doResize(); - void doMove(); Q_SIGNALS: void activate(); @@ -117,19 +105,13 @@ private: int fw; int extrahei; int range; - uint buttonDown :1; - uint moveResizeMode :1; - uint activeForResize :1; - uint sizeprotect :1; - uint movingEnabled :1; - uint activeForMove :1; + uint buttonDown :1; + uint active :1; + uint enabled :1; void setMouseCursor(MousePosition m); - bool isMove() const { - return moveResizeMode && mode == Center; - } - bool isResize() const { - return moveResizeMode && !isMove(); + bool isResizing() const { + return active && mode != Center; } };