From 0fbbcfa87059b4ca45b7e9074f57229560604452 Mon Sep 17 00:00:00 2001 From: hjk Date: Thu, 17 Jul 2014 14:26:46 +0200 Subject: [PATCH] FancyMainWindow: Polish dockwidget titlebar display Pop up after 500 ms hovering over an active area of the (approximate) size of the titlebar. Hide the titlebase if the mouse leaves it. Completely hide it (as opposed to the initial 3 px) if it's "inactive". Change-Id: I9e43c7f501d2265466a7575da4e661e25aa6ccf5 Reviewed-by: Alessandro Portale --- src/libs/utils/fancymainwindow.cpp | 81 +++++++++++++++++++++++++----- 1 file changed, 68 insertions(+), 13 deletions(-) diff --git a/src/libs/utils/fancymainwindow.cpp b/src/libs/utils/fancymainwindow.cpp index 46c366df224..44259411c72 100644 --- a/src/libs/utils/fancymainwindow.cpp +++ b/src/libs/utils/fancymainwindow.cpp @@ -31,6 +31,7 @@ #include "qtcassert.h" +#include #include #include #include @@ -42,6 +43,7 @@ #include #include #include +#include #include static const char stateKeyC[] = "State"; @@ -131,7 +133,7 @@ public: const int minWidth = 10; const int maxWidth = 10000; - const int inactiveHeight = 3; + const int inactiveHeight = 0; const int activeHeight = m_closeButton->sizeHint().height() + 2; m_minimumInactiveSize = QSize(minWidth, inactiveHeight); @@ -150,12 +152,6 @@ public: setLayout(layout); } - void enterEvent(QEvent *event) - { - setActive(true); - QWidget::enterEvent(event); - } - void leaveEvent(QEvent *event) { if (!q->isFloating()) @@ -202,27 +198,84 @@ public: class DockWidget : public QDockWidget { + Q_OBJECT + public: DockWidget(QWidget *inner, QWidget *parent) - : QDockWidget(parent) + : QDockWidget(parent), m_inner(inner) { setWidget(inner); setFeatures(QDockWidget::DockWidgetMovable | QDockWidget::DockWidgetClosable | QDockWidget::DockWidgetFloatable); setObjectName(inner->objectName() + QLatin1String("DockWidget")); setWindowTitle(inner->windowTitle()); + setMouseTracking(true); QStyleOptionDockWidget opt; initStyleOption(&opt); - auto titleBar = new TitleBarWidget(this, opt); - titleBar->m_titleLabel->setText(inner->windowTitle()); - setTitleBarWidget(titleBar); + m_titleBar = new TitleBarWidget(this, opt); + m_titleBar->m_titleLabel->setText(inner->windowTitle()); + setTitleBarWidget(m_titleBar); + + m_timer.setSingleShot(true); + m_timer.setInterval(500); + + connect(&m_timer, SIGNAL(timeout()), this, SLOT(handleMouseTimeout())); + + connect(this, SIGNAL(topLevelChanged(bool)), this, SLOT(handleToplevelChanged(bool))); auto origFloatButton = findChild(QLatin1String("qt_dockwidget_floatbutton")); - connect(titleBar->m_floatButton, SIGNAL(clicked()), origFloatButton, SIGNAL(clicked())); + connect(m_titleBar->m_floatButton, SIGNAL(clicked()), origFloatButton, SIGNAL(clicked())); auto origCloseButton = findChild(QLatin1String("qt_dockwidget_closebutton")); - connect(titleBar->m_closeButton, SIGNAL(clicked()), origCloseButton, SIGNAL(clicked())); + connect(m_titleBar->m_closeButton, SIGNAL(clicked()), origCloseButton, SIGNAL(clicked())); } + + bool eventFilter(QObject *, QEvent *event) + { + if (event->type() == QEvent::MouseMove) { + QMouseEvent *me = static_cast(event); + int y = me->pos().y(); + int x = me->pos().x(); + int h = m_titleBar->m_floatButton->height(); + if (!isFloating() && 0 <= x && x < m_inner->width() && 0 <= y && y <= h) { + m_timer.start(); + m_startPos = mapToGlobal(me->pos()); + } + } + return false; + } + + void enterEvent(QEvent *event) + { + QApplication::instance()->installEventFilter(this); + QDockWidget::leaveEvent(event); + } + + void leaveEvent(QEvent *event) + { + QApplication::instance()->removeEventFilter(this); + QDockWidget::leaveEvent(event); + } + + Q_SLOT void handleMouseTimeout() + { + QPoint dist = m_startPos - QCursor::pos(); + if (!isFloating() && dist.manhattanLength() < 4) { + m_titleBar->setActive(true); + } + } + + Q_SLOT void handleToplevelChanged(bool floating) + { + if (!floating) + m_titleBar->setActive(false); + } + +private: + QPoint m_startPos; + QWidget *m_inner; + TitleBarWidget *m_titleBar; + QTimer m_timer; }; /*! \class Utils::FancyMainWindow @@ -444,3 +497,5 @@ void FancyMainWindow::setToolBarDockWidget(QDockWidget *dock) } } // namespace Utils + +#include "fancymainwindow.moc"