From 6b1524e4ff115f9cbee93c3c14c09fb347885d56 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?M=C3=A9ven=20Car?= Date: Sat, 7 Nov 2020 19:45:56 +0100 Subject: [PATCH] Places panel: show a tooltip after 500 ms BUG: 426455 FIXED-IN: 20.12 --- src/panels/places/placespanel.cpp | 32 ++++++++++++++++++++++++++++++- src/panels/places/placespanel.h | 7 +++++++ 2 files changed, 38 insertions(+), 1 deletion(-) diff --git a/src/panels/places/placespanel.cpp b/src/panels/places/placespanel.cpp index 336b9deb83..66770ee865 100644 --- a/src/panels/places/placespanel.cpp +++ b/src/panels/places/placespanel.cpp @@ -39,6 +39,8 @@ #include #include #include +#include +#include PlacesPanel::PlacesPanel(QWidget* parent) : Panel(parent), @@ -49,8 +51,12 @@ PlacesPanel::PlacesPanel(QWidget* parent) : m_triggerStorageSetupButton(), m_itemDropEventIndex(-1), m_itemDropEventMimeData(nullptr), - m_itemDropEvent(nullptr) + m_itemDropEvent(nullptr), + m_tooltipTimer() { + m_tooltipTimer.setInterval(500); + m_tooltipTimer.setSingleShot(true); + connect(&m_tooltipTimer, &QTimer::timeout, this, &PlacesPanel::slotShowTooltip); } PlacesPanel::~PlacesPanel() @@ -111,6 +117,8 @@ void PlacesPanel::showEvent(QShowEvent* event) m_view->setWidgetCreator(new KItemListWidgetCreator()); m_view->setGroupHeaderCreator(new KItemListGroupHeaderCreator()); + installEventFilter(this); + m_controller = new KItemListController(m_model, m_view, this); m_controller->setSelectionBehavior(KItemListController::SingleSelection); m_controller->setSingleClickActivationEnforced(true); @@ -137,6 +145,21 @@ void PlacesPanel::showEvent(QShowEvent* event) Panel::showEvent(event); } +bool PlacesPanel::eventFilter(QObject * /* obj */, QEvent *event) +{ + if (event->type() == QEvent::ToolTip) { + + QHelpEvent *hoverEvent = reinterpret_cast(event); + + m_hoveredIndex = m_view->itemAt(hoverEvent->pos()); + m_hoverPos = mapToGlobal(hoverEvent->pos()); + + m_tooltipTimer.start(); + return true; + } + return false; +} + void PlacesPanel::slotItemActivated(int index) { triggerItem(index, Qt::LeftButton); @@ -460,6 +483,13 @@ void PlacesPanel::slotStorageSetupDone(int index, bool success) } } +void PlacesPanel::slotShowTooltip() +{ + const QUrl url = m_model->data(m_hoveredIndex).value("url").value(); + const QString text = url.isLocalFile() ? url.path() : url.toString(); + QToolTip::showText(m_hoverPos, text); +} + void PlacesPanel::addEntry() { const int index = m_controller->selectionManager()->currentItem(); diff --git a/src/panels/places/placespanel.h b/src/panels/places/placespanel.h index 38bfa4c532..fe4f46ba6c 100644 --- a/src/panels/places/placespanel.h +++ b/src/panels/places/placespanel.h @@ -11,6 +11,7 @@ #include "panels/panel.h" #include +#include class KItemListController; class PlacesItemModel; @@ -30,6 +31,8 @@ public: ~PlacesPanel() override; void proceedWithTearDown(); + bool eventFilter(QObject *obj, QEvent *event) override; + signals: void placeActivated(const QUrl& url); void placeMiddleClicked(const QUrl& url); @@ -58,6 +61,7 @@ private slots: void slotAboveItemDropEvent(int index, QGraphicsSceneDragDropEvent* event); void slotUrlsDropped(const QUrl& dest, QDropEvent* event, QWidget* parent); void slotStorageSetupDone(int index, bool success); + void slotShowTooltip(); private: void addEntry(); @@ -84,6 +88,9 @@ private: int m_itemDropEventIndex; QMimeData* m_itemDropEventMimeData; QDropEvent* m_itemDropEvent; + QTimer m_tooltipTimer; + int m_hoveredIndex; + QPoint m_hoverPos; }; #endif // PLACESPANEL_H