X-Git-Url: https://cloud.milkyroute.net/gitweb/dolphin.git/blobdiff_plain/41253c0c81bafb54a0b6c82e5282e3add85e8a50..8fda69892284144a8aea3845d2aad844021c99d2:/src/views/tooltips/tooltipmanager.cpp diff --git a/src/views/tooltips/tooltipmanager.cpp b/src/views/tooltips/tooltipmanager.cpp index 4a9f91359..637261702 100644 --- a/src/views/tooltips/tooltipmanager.cpp +++ b/src/views/tooltips/tooltipmanager.cpp @@ -1,45 +1,43 @@ -/******************************************************************************* - * 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 * - *******************************************************************************/ +/* + * SPDX-FileCopyrightText: 2008 Konstantin Heil + * + * SPDX-License-Identifier: GPL-2.0-or-later + */ #include "tooltipmanager.h" #include "dolphinfilemetadatawidget.h" -#include + #include #include +#include #include +#include #include +#include #include #include +#include #include #include #include #include +class IconLoaderSingleton { +public: + IconLoaderSingleton() = default; + + KIconLoader self; +}; + +Q_GLOBAL_STATIC(IconLoaderSingleton, iconLoader) + ToolTipManager::ToolTipManager(QWidget* parent) : QObject(parent), - m_showToolTipTimer(0), - m_contentRetrievalTimer(0), - m_transientParent(0), - m_fileMetaDataWidget(0), - m_tooltipWidget(new KToolTipWidget()), + m_showToolTipTimer(nullptr), + m_contentRetrievalTimer(nullptr), + m_transientParent(nullptr), m_toolTipRequested(false), m_metaDataRequested(false), m_appliedWaitCursor(false), @@ -54,7 +52,7 @@ ToolTipManager::ToolTipManager(QWidget* parent) : m_showToolTipTimer = new QTimer(this); m_showToolTipTimer->setSingleShot(true); m_showToolTipTimer->setInterval(500); - connect(m_showToolTipTimer, &QTimer::timeout, this, static_cast(&ToolTipManager::showToolTip)); + connect(m_showToolTipTimer, &QTimer::timeout, this, QOverload<>::of(&ToolTipManager::showToolTip)); m_contentRetrievalTimer = new QTimer(this); m_contentRetrievalTimer->setSingleShot(true); @@ -66,11 +64,14 @@ ToolTipManager::ToolTipManager(QWidget* parent) : ToolTipManager::~ToolTipManager() { + if (!m_fileMetaDatWidgetOwnershipTransferred) { + delete m_fileMetaDataWidget; + } } void ToolTipManager::showToolTip(const KFileItem& item, const QRectF& itemRect, QWindow *transientParent) { - hideToolTip(); + hideToolTip(HideBehavior::Instantly); m_itemRect = itemRect.toRect(); @@ -82,10 +83,11 @@ void ToolTipManager::showToolTip(const KFileItem& item, const QRectF& itemRect, // 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, - this, &ToolTipManager::slotMetaDataRequestFinished); + if (!m_fileMetaDataWidget) { + m_fileMetaDataWidget = new DolphinFileMetaDataWidget(); + connect(m_fileMetaDataWidget, &DolphinFileMetaDataWidget::metaDataRequestFinished, this, &ToolTipManager::slotMetaDataRequestFinished); + connect(m_fileMetaDataWidget, &DolphinFileMetaDataWidget::urlActivated, this, &ToolTipManager::urlActivated); + } m_contentRetrievalTimer->start(); m_showToolTipTimer->start(); @@ -93,7 +95,7 @@ void ToolTipManager::showToolTip(const KFileItem& item, const QRectF& itemRect, Q_ASSERT(!m_metaDataRequested); } -void ToolTipManager::hideToolTip() +void ToolTipManager::hideToolTip(const HideBehavior behavior) { if (m_appliedWaitCursor) { QApplication::restoreOverrideCursor(); @@ -104,7 +106,16 @@ void ToolTipManager::hideToolTip() m_metaDataRequested = false; m_showToolTipTimer->stop(); m_contentRetrievalTimer->stop(); - m_tooltipWidget->hideLater(); + if (m_tooltipWidget) { + switch (behavior) { + case HideBehavior::Instantly: + m_tooltipWidget->hide(); + break; + case HideBehavior::Later: + m_tooltipWidget->hideLater(); + break; + } + } } void ToolTipManager::startContentRetrieval() @@ -125,8 +136,12 @@ void ToolTipManager::startContentRetrieval() // Request a preview of the item m_fileMetaDataWidget->setPreview(QPixmap()); - KIO::PreviewJob* job = new KIO::PreviewJob(KFileItemList() << m_item, QSize(256, 256)); - job->setIgnoreMaximumSize(m_item.isLocalFile()); + const KConfigGroup globalConfig(KSharedConfig::openConfig(), "PreviewSettings"); + const QStringList plugins = globalConfig.readEntry("Plugins", KIO::PreviewJob::defaultPlugins()); + KIO::PreviewJob* job = new KIO::PreviewJob(KFileItemList() << m_item, + QSize(256, 256), + &plugins); + job->setIgnoreMaximumSize(m_item.isLocalFile() && !m_item.isSlow()); if (job->uiDelegate()) { KJobWidgets::setWindow(job, qApp->activeWindow()); } @@ -161,8 +176,13 @@ void ToolTipManager::previewFailed() if (!m_toolTipRequested) { return; } - - const QPixmap pixmap = QIcon::fromTheme(m_item.iconName()).pixmap(128, 128); + QPalette pal; + for (auto state : { QPalette::Active, QPalette::Inactive, QPalette::Disabled }) { + pal.setBrush(state, QPalette::WindowText, pal.toolTipText()); + pal.setBrush(state, QPalette::Window, pal.toolTipBase()); + } + iconLoader->self.setCustomPalette(pal); + const QPixmap pixmap = KDE::icon(m_item.iconName(), &iconLoader->self).pixmap(128, 128); m_fileMetaDataWidget->setPreview(pixmap); if (!m_showToolTipTimer->isActive()) { showToolTip(); @@ -199,7 +219,13 @@ void ToolTipManager::showToolTip() // 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); + // At this point KToolTipWidget adopted our parent-less metadata widget. + m_fileMetaDatWidgetOwnershipTransferred = true; + m_toolTipRequested = false; }