From f955ee200da38d80be4833d8b421221874896ca1 Mon Sep 17 00:00:00 2001 From: Peter Penz Date: Fri, 8 Oct 2010 19:10:53 +0000 Subject: [PATCH] Move the changing of the cursor-shape from the extensions-factory and the selection-toggle to the selection-manager. Beside simplifying the code this also solves some corner-cases where the shape has not been restored correctly. svn path=/trunk/KDE/kdebase/apps/; revision=1183934 --- src/views/selectionmanager.cpp | 68 +++++++++++++++++++++++++---- src/views/selectionmanager.h | 3 ++ src/views/selectiontoggle.cpp | 58 ++++-------------------- src/views/selectiontoggle.h | 4 -- src/views/viewextensionsfactory.cpp | 38 +--------------- src/views/viewextensionsfactory.h | 3 -- 6 files changed, 73 insertions(+), 101 deletions(-) diff --git a/src/views/selectionmanager.cpp b/src/views/selectionmanager.cpp index 7353e5a39..c06d827bd 100644 --- a/src/views/selectionmanager.cpp +++ b/src/views/selectionmanager.cpp @@ -38,7 +38,8 @@ SelectionManager::SelectionManager(QAbstractItemView* parent) : QObject(parent), m_view(parent), m_toggle(0), - m_connected(false) + m_connected(false), + m_appliedPointingHandCursor(false) { connect(parent, SIGNAL(entered(const QModelIndex&)), this, SLOT(slotEntered(const QModelIndex&))); @@ -49,6 +50,7 @@ SelectionManager::SelectionManager(QAbstractItemView* parent) : m_toggle->hide(); connect(m_toggle, SIGNAL(clicked(bool)), this, SLOT(setItemSelected(bool))); + m_toggle->installEventFilter(this); m_view->viewport()->installEventFilter(this); } @@ -59,14 +61,45 @@ SelectionManager::~SelectionManager() bool SelectionManager::eventFilter(QObject* watched, QEvent* event) { - Q_ASSERT(watched == m_view->viewport()); - if (event->type() == QEvent::MouseButtonPress) { - // Set the toggle invisible, if a mouse button has been pressed - // outside the toggle boundaries. This e.g. assures, that the toggle - // gets invisible during dragging items. - const QRect toggleBounds(m_toggle->mapToGlobal(QPoint(0, 0)), m_toggle->size()); - m_toggle->setVisible(toggleBounds.contains(QCursor::pos())); + if (watched == m_view->viewport()) { + switch (event->type()) { + case QEvent::Leave: + m_toggle->hide(); + break; + + case QEvent::MouseButtonPress: { + // Set the toggle invisible, if a mouse button has been pressed + // outside the toggle boundaries. This e.g. assures, that the toggle + // gets invisible during dragging items. + const QRect toggleBounds(m_toggle->mapToGlobal(QPoint(0, 0)), m_toggle->size()); + m_toggle->setVisible(toggleBounds.contains(QCursor::pos())); + break; + } + + default: + break; + } + } else if (watched == m_toggle) { + switch (event->type()) { + case QEvent::Hide: + // If the toggle button gets hidden, the cursor is not above the item + // anymore and the shape must get restored + restoreCursor(); + break; + + case QEvent::Enter: + QApplication::changeOverrideCursor(Qt::ArrowCursor); + break; + + case QEvent::Leave: + QApplication::changeOverrideCursor(Qt::PointingHandCursor); + break; + + default: + break; + } } + return QObject::eventFilter(watched, event); } @@ -82,6 +115,8 @@ void SelectionManager::slotEntered(const QModelIndex& index) (index.column() == DolphinModel::Name) && (QApplication::mouseButtons() == Qt::NoButton); if (showToggle) { + applyPointingHandCursor(); + m_toggle->setUrl(urlForIndex(index)); if (!m_connected) { @@ -198,4 +233,21 @@ const QModelIndex SelectionManager::indexForUrl(const KUrl& url) const return proxyModel->mapFromSource(dirIndex); } + +void SelectionManager::applyPointingHandCursor() +{ + if (!m_appliedPointingHandCursor) { + QApplication::setOverrideCursor(QCursor(Qt::PointingHandCursor)); + m_appliedPointingHandCursor = true; + } +} + +void SelectionManager::restoreCursor() +{ + if (m_appliedPointingHandCursor) { + QApplication::restoreOverrideCursor(); + m_appliedPointingHandCursor = false; + } +} + #include "selectionmanager.moc" diff --git a/src/views/selectionmanager.h b/src/views/selectionmanager.h index 85145a50b..9cab4f3a9 100644 --- a/src/views/selectionmanager.h +++ b/src/views/selectionmanager.h @@ -65,11 +65,14 @@ private slots: private: KUrl urlForIndex(const QModelIndex& index) const; const QModelIndex indexForUrl(const KUrl& url) const; + void applyPointingHandCursor(); + void restoreCursor(); private: QAbstractItemView* m_view; SelectionToggle* m_toggle; bool m_connected; + bool m_appliedPointingHandCursor; }; #endif diff --git a/src/views/selectiontoggle.cpp b/src/views/selectiontoggle.cpp index cbd273b11..c1272fa0f 100644 --- a/src/views/selectiontoggle.cpp +++ b/src/views/selectiontoggle.cpp @@ -32,11 +32,12 @@ #include #include +#include + SelectionToggle::SelectionToggle(QWidget* parent) : QAbstractButton(parent), m_isHovered(false), m_leftMouseButtonPressed(false), - m_appliedArrowCursor(false), m_fadingValue(0), m_margin(0), m_icon(), @@ -106,25 +107,12 @@ void SelectionToggle::setVisible(bool visible) bool SelectionToggle::eventFilter(QObject* obj, QEvent* event) { - if (obj == parent()) { - switch (event->type()) { - case QEvent::Leave: - hide(); - break; - - case QEvent::MouseMove: - if (m_leftMouseButtonPressed) { - // Don't forward mouse move events to the viewport, - // otherwise a rubberband selection will be shown when - // clicking on the selection toggle and moving the mouse - // above the viewport. - return true; - } - break; - - default: - break; - } + if ((obj == parent()) && (event->type() == QEvent::MouseMove) && m_leftMouseButtonPressed) { + // Don't forward mouse move events to the viewport, + // otherwise a rubberband selection will be shown when + // clicking on the selection toggle and moving the mouse + // above the viewport. + return true; } return QAbstractButton::eventFilter(obj, event); @@ -134,19 +122,6 @@ void SelectionToggle::enterEvent(QEvent* event) { QAbstractButton::enterEvent(event); - if (!m_appliedArrowCursor) { - m_appliedArrowCursor = true; - // Apply the arrow asynchronously. This is required for - // the following usecase: - // 1. Cursor is above the viewport left beside an item - // 2. Cursor is moved above the item, so that the selection-toggle - // and the item are entered equally. - // In this situation it is not defined who gets the enter-event first. - // As the selection-toggle is above the item, it should overwrite possible - // cursor changes done by the item. - QTimer::singleShot(0, this, SLOT(applyArrowCursor())); - } - // if the mouse cursor is above the selection toggle, display // it immediately without fading timer m_isHovered = true; @@ -163,13 +138,6 @@ void SelectionToggle::leaveEvent(QEvent* event) { QAbstractButton::leaveEvent(event); - if (m_appliedArrowCursor) { - // Reset the cursor asynchronously. See SelectionToggle::enterEvent() - // for a more information. - m_appliedArrowCursor = false; - QTimer::singleShot(0, this, SLOT(restoreCursor())); - } - m_isHovered = false; update(); } @@ -245,16 +213,6 @@ void SelectionToggle::refreshIcon() setIconOverlay(isChecked()); } -void SelectionToggle::applyArrowCursor() -{ - QApplication::setOverrideCursor(QCursor(Qt::ArrowCursor)); -} - -void SelectionToggle::restoreCursor() -{ - QApplication::restoreOverrideCursor(); -} - void SelectionToggle::startFading() { Q_ASSERT(m_fadingTimeLine == 0); diff --git a/src/views/selectiontoggle.h b/src/views/selectiontoggle.h index 26aa30050..210f1a3aa 100644 --- a/src/views/selectiontoggle.h +++ b/src/views/selectiontoggle.h @@ -82,9 +82,6 @@ private slots: void setIconOverlay(bool checked); void refreshIcon(); - void applyArrowCursor(); - void restoreCursor(); - private: void startFading(); void stopFading(); @@ -92,7 +89,6 @@ private: private: bool m_isHovered; bool m_leftMouseButtonPressed; - bool m_appliedArrowCursor; int m_fadingValue; int m_margin; QPixmap m_icon; diff --git a/src/views/viewextensionsfactory.cpp b/src/views/viewextensionsfactory.cpp index 152396253..5dabd66cc 100644 --- a/src/views/viewextensionsfactory.cpp +++ b/src/views/viewextensionsfactory.cpp @@ -43,7 +43,6 @@ ViewExtensionsFactory::ViewExtensionsFactory(QAbstractItemView* view, DolphinViewController* dolphinViewController, const ViewModeController* viewModeController) : QObject(view), - m_appliedPointingHandCursor(false), m_view(view), m_dolphinViewController(dolphinViewController), m_toolTipManager(0), @@ -129,11 +128,6 @@ ViewExtensionsFactory::ViewExtensionsFactory(QAbstractItemView* view, this, SLOT(slotNameFilterChanged(const QString&))); view->viewport()->installEventFilter(this); - - // Apply a pointing-hand cursor when hovering files - connect(view, SIGNAL(entered(const QModelIndex&)), SLOT(applyPointingHandCursor())); - connect(view, SIGNAL(viewportEntered()), SLOT(restoreCursor())); - connect(viewModeController, SIGNAL(urlChanged(const KUrl&)), SLOT(restoreCursor())); } ViewExtensionsFactory::~ViewExtensionsFactory() @@ -163,21 +157,9 @@ bool ViewExtensionsFactory::autoFolderExpandingEnabled() const bool ViewExtensionsFactory::eventFilter(QObject* watched, QEvent* event) { Q_UNUSED(watched); - - switch (event->type()) { - case QEvent::Wheel: - if (m_selectionManager != 0) { - m_selectionManager->reset(); - } - break; - - case QEvent::Leave: - restoreCursor(); - break; - - default: break; + if ((event->type() == QEvent::Wheel) && (m_selectionManager != 0)) { + m_selectionManager->reset(); } - return false; } @@ -254,22 +236,6 @@ void ViewExtensionsFactory::requestActivation() m_dolphinViewController->requestActivation(); } -void ViewExtensionsFactory::applyPointingHandCursor() -{ - if (!m_appliedPointingHandCursor && !(QApplication::mouseButtons() & Qt::LeftButton)) { - QApplication::setOverrideCursor(QCursor(Qt::PointingHandCursor)); - m_appliedPointingHandCursor = true; - } -} - -void ViewExtensionsFactory::restoreCursor() -{ - if (m_appliedPointingHandCursor) { - QApplication::restoreOverrideCursor(); - m_appliedPointingHandCursor = false; - } -} - DolphinSortFilterProxyModel* ViewExtensionsFactory::proxyModel() const { return static_cast(m_view->model()); diff --git a/src/views/viewextensionsfactory.h b/src/views/viewextensionsfactory.h index 0ab3d06d0..9324932ac 100644 --- a/src/views/viewextensionsfactory.h +++ b/src/views/viewextensionsfactory.h @@ -85,14 +85,11 @@ private slots: void slotNameFilterChanged(const QString& nameFilter); void slotRequestVersionControlActions(const KFileItemList& items); void requestActivation(); - void applyPointingHandCursor(); - void restoreCursor(); private: DolphinSortFilterProxyModel* proxyModel() const; private: - bool m_appliedPointingHandCursor; QAbstractItemView* m_view; DolphinViewController* m_dolphinViewController; ToolTipManager* m_toolTipManager; -- 2.47.3