diff --git a/src/views/tooltips/tooltipmanager.cpp b/src/views/tooltips/tooltipmanager.cpp index bb2890138..9e79a8f70 100644 --- a/src/views/tooltips/tooltipmanager.cpp +++ b/src/views/tooltips/tooltipmanager.cpp @@ -1,215 +1,214 @@ /******************************************************************************* * Copyright (C) 2008 by Konstantin Heil * * * * This program is free software; you can redistribute it and/or modify * * it under the terms of the GNU General Public License as published by * * the Free Software Foundation; either version 2 of the License, or * * (at your option) any later version. * * * * This program is distributed in the hope that it will be useful, * * but WITHOUT ANY WARRANTY; without even the implied warranty of * * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * * GNU General Public License for more details. * * * * You should have received a copy of the GNU General Public License * * along with this program; if not, write to the * * Free Software Foundation, Inc., * * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA * *******************************************************************************/ #include "tooltipmanager.h" #include "dolphinfilemetadatawidget.h" #include #include #include #include #include #include #include #include #include #include #include ToolTipManager::ToolTipManager(QWidget* parent) : QObject(parent), m_showToolTipTimer(nullptr), m_contentRetrievalTimer(nullptr), m_transientParent(nullptr), m_fileMetaDataWidget(nullptr), m_toolTipRequested(false), m_metaDataRequested(false), m_appliedWaitCursor(false), m_margin(4), m_item(), m_itemRect() { if (parent) { m_margin = qMax(m_margin, parent->style()->pixelMetric(QStyle::PM_ToolTipLabelFrameWidth)); } m_showToolTipTimer = new QTimer(this); m_showToolTipTimer->setSingleShot(true); m_showToolTipTimer->setInterval(500); connect(m_showToolTipTimer, &QTimer::timeout, this, static_cast(&ToolTipManager::showToolTip)); m_contentRetrievalTimer = new QTimer(this); m_contentRetrievalTimer->setSingleShot(true); m_contentRetrievalTimer->setInterval(200); connect(m_contentRetrievalTimer, &QTimer::timeout, this, &ToolTipManager::startContentRetrieval); Q_ASSERT(m_contentRetrievalTimer->interval() < m_showToolTipTimer->interval()); } ToolTipManager::~ToolTipManager() { } void ToolTipManager::showToolTip(const KFileItem& item, const QRectF& itemRect, QWindow *transientParent) { hideToolTip(); m_itemRect = itemRect.toRect(); m_itemRect.adjust(-m_margin, -m_margin, m_margin, m_margin); m_item = item; m_transientParent = transientParent; // Only start the retrieving of the content, when the mouse has been over this // item for 200 milliseconds. This prevents a lot of useless preview jobs and // meta data retrieval, when passing rapidly over a lot of items. - delete m_fileMetaDataWidget; - m_fileMetaDataWidget = new DolphinFileMetaDataWidget(); - connect(m_fileMetaDataWidget, &DolphinFileMetaDataWidget::metaDataRequestFinished, + m_fileMetaDataWidget.reset(new DolphinFileMetaDataWidget()); + connect(m_fileMetaDataWidget.data(), &DolphinFileMetaDataWidget::metaDataRequestFinished, this, &ToolTipManager::slotMetaDataRequestFinished); - connect(m_fileMetaDataWidget, &DolphinFileMetaDataWidget::urlActivated, + connect(m_fileMetaDataWidget.data(), &DolphinFileMetaDataWidget::urlActivated, this, &ToolTipManager::urlActivated); m_contentRetrievalTimer->start(); m_showToolTipTimer->start(); m_toolTipRequested = true; Q_ASSERT(!m_metaDataRequested); } void ToolTipManager::hideToolTip() { if (m_appliedWaitCursor) { QApplication::restoreOverrideCursor(); m_appliedWaitCursor = false; } m_toolTipRequested = false; m_metaDataRequested = false; m_showToolTipTimer->stop(); m_contentRetrievalTimer->stop(); if (m_tooltipWidget) { m_tooltipWidget->hideLater(); } } void ToolTipManager::startContentRetrieval() { if (!m_toolTipRequested) { return; } m_fileMetaDataWidget->setName(m_item.text()); // Request the retrieval of meta-data. The slot // slotMetaDataRequestFinished() is invoked after the // meta-data have been received. m_metaDataRequested = true; m_fileMetaDataWidget->setItems(KFileItemList() << m_item); m_fileMetaDataWidget->adjustSize(); // Request a preview of the item m_fileMetaDataWidget->setPreview(QPixmap()); QStringList plugins = KIO::PreviewJob::availablePlugins(); KIO::PreviewJob* job = new KIO::PreviewJob(KFileItemList() << m_item, QSize(256, 256), &plugins); job->setIgnoreMaximumSize(m_item.isLocalFile()); if (job->uiDelegate()) { KJobWidgets::setWindow(job, qApp->activeWindow()); } connect(job, &KIO::PreviewJob::gotPreview, this, &ToolTipManager::setPreviewPix); connect(job, &KIO::PreviewJob::failed, this, &ToolTipManager::previewFailed); } void ToolTipManager::setPreviewPix(const KFileItem& item, const QPixmap& pixmap) { if (!m_toolTipRequested || (m_item.url() != item.url())) { // No tooltip is requested anymore or an old preview has been received return; } if (pixmap.isNull()) { previewFailed(); } else { m_fileMetaDataWidget->setPreview(pixmap); if (!m_showToolTipTimer->isActive()) { showToolTip(); } } } void ToolTipManager::previewFailed() { if (!m_toolTipRequested) { return; } const QPixmap pixmap = QIcon::fromTheme(m_item.iconName()).pixmap(128, 128); m_fileMetaDataWidget->setPreview(pixmap); if (!m_showToolTipTimer->isActive()) { showToolTip(); } } void ToolTipManager::slotMetaDataRequestFinished() { if (!m_toolTipRequested) { return; } m_metaDataRequested = false; if (!m_showToolTipTimer->isActive()) { showToolTip(); } } void ToolTipManager::showToolTip() { Q_ASSERT(m_toolTipRequested); if (m_appliedWaitCursor) { QApplication::restoreOverrideCursor(); m_appliedWaitCursor = false; } if (m_fileMetaDataWidget->preview().isNull() || m_metaDataRequested) { Q_ASSERT(!m_appliedWaitCursor); QApplication::setOverrideCursor(QCursor(Qt::WaitCursor)); m_appliedWaitCursor = true; return; } // Adjust the size to get a proper sizeHint() m_fileMetaDataWidget->adjustSize(); if (!m_tooltipWidget) { m_tooltipWidget.reset(new KToolTipWidget()); } - m_tooltipWidget->showBelow(m_itemRect, m_fileMetaDataWidget, m_transientParent); + m_tooltipWidget->showBelow(m_itemRect, m_fileMetaDataWidget.data(), m_transientParent); m_toolTipRequested = false; } diff --git a/src/views/tooltips/tooltipmanager.h b/src/views/tooltips/tooltipmanager.h index f6d2b7304..63c723f80 100644 --- a/src/views/tooltips/tooltipmanager.h +++ b/src/views/tooltips/tooltipmanager.h @@ -1,98 +1,98 @@ /******************************************************************************* * Copyright (C) 2008 by Konstantin Heil * * * * This program is free software; you can redistribute it and/or modify * * it under the terms of the GNU General Public License as published by * * the Free Software Foundation; either version 2 of the License, or * * (at your option) any later version. * * * * This program is distributed in the hope that it will be useful, * * but WITHOUT ANY WARRANTY; without even the implied warranty of * * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * * GNU General Public License for more details. * * * * You should have received a copy of the GNU General Public License * * along with this program; if not, write to the * * Free Software Foundation, Inc., * * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA * *******************************************************************************/ #ifndef TOOLTIPMANAGER_H #define TOOLTIPMANAGER_H #include #include #include class DolphinFileMetaDataWidget; class KToolTipWidget; class QTimer; class QWindow; /** * @brief Manages the tooltips for an item view. * * When hovering an item, a tooltip is shown after * a short timeout. The tooltip is hidden again when the * viewport is hovered or the item view has been left. */ class ToolTipManager : public QObject { Q_OBJECT public: explicit ToolTipManager(QWidget* parent); ~ToolTipManager() override; /** * Triggers the showing of the tooltip for the item \p item * where the item has the maximum boundaries of \p itemRect. * The tooltip manager takes care that the tooltip is shown * slightly delayed and with a proper \p transientParent. */ void showToolTip(const KFileItem& item, const QRectF& itemRect, QWindow *transientParent); /** * Hides the currently shown tooltip. */ void hideToolTip(); signals: /** * Is emitted when the user clicks a tag or a link * in the metadata widget. */ void urlActivated(const QUrl& url); private slots: void startContentRetrieval(); void setPreviewPix(const KFileItem& item, const QPixmap& pix); void previewFailed(); void slotMetaDataRequestFinished(); void showToolTip(); private: /// Timeout from requesting a tooltip until the tooltip /// should be shown QTimer* m_showToolTipTimer; /// Timeout from requesting a tooltip until the retrieving of /// the tooltip content like preview and meta data gets started. QTimer* m_contentRetrievalTimer; /// Transient parent of the tooltip, mandatory on Wayland. QWindow* m_transientParent; - DolphinFileMetaDataWidget* m_fileMetaDataWidget; + QScopedPointer m_fileMetaDataWidget; QScopedPointer m_tooltipWidget; bool m_toolTipRequested; bool m_metaDataRequested; bool m_appliedWaitCursor; int m_margin; KFileItem m_item; QRect m_itemRect; }; #endif