From: Simon Paul St James Date: Mon, 18 Aug 2008 20:33:21 +0000 (+0000) Subject: Very rough initial attempt at previews-in-tooltip for Konqueror and Dolphin. Still... X-Git-Url: https://cloud.milkyroute.net/gitweb/dolphin.git/commitdiff_plain/cfe28380063eedb2f9b418215acfe369ea9da5d5 Very rough initial attempt at previews-in-tooltip for Konqueror and Dolphin. Still needs extensive work in positioning, scaling/ padding items to fit, and timing things so that the previews are more likely to have been generated when the tooltip is positioned and displayed. CCBUG:161848 svn path=/trunk/KDE/kdebase/apps/; revision=848984 --- diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index f09810b71..96941803b 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -28,6 +28,7 @@ set(dolphinprivate_LIB_SRCS draganddrophelper.cpp dolphinmodel.cpp dolphinsettings.cpp + dolphintooltip.cpp dolphincategorydrawer.cpp dolphinview.cpp dolphinviewactionhandler.cpp diff --git a/src/dolphintooltip.cpp b/src/dolphintooltip.cpp new file mode 100644 index 000000000..67b5fd6f5 --- /dev/null +++ b/src/dolphintooltip.cpp @@ -0,0 +1,61 @@ +#include "dolphintooltip.h" + +#include +#include +#include + +#include + +const int PREVIEW_WIDTH = 256; +const int PREVIEW_HEIGHT = 256; + +DolphinToolTipItem::DolphinToolTipItem(const KFileItem & fileItem) : + KToolTipItem(KIcon(fileItem.iconName()), fileItem.getToolTipText(), UserType) +{ + + //if (icon().actualSize(QSize(256,256)).width() != PREVIEW_WIDTH) + /*{ + QPixmap paddedImage(QSize(PREVIEW_WIDTH, PREVIEW_HEIGHT)); + paddedImage.fill(Qt::transparent); + QPainter painter(&paddedImage); + KIcon kicon(fileItem.iconName()); + painter.drawPixmap((PREVIEW_WIDTH - 128) / 2, (PREVIEW_HEIGHT - 128) / 2, kicon.pixmap(QSize(128,128))); + setData(Qt::DecorationRole, QIcon(paddedImage)); + }*/ + + // Initiate the preview job. + KFileItemList fileItems; + fileItems << fileItem; + KIO::PreviewJob* job = KIO::filePreview(fileItems, PREVIEW_WIDTH, PREVIEW_HEIGHT ); + connect(job, SIGNAL(gotPreview(const KFileItem&, const QPixmap&)), + this, SLOT(setPreview(const KFileItem&, const QPixmap&))); + +} +DolphinToolTipItem::~DolphinToolTipItem() +{ +} +void DolphinToolTipItem::setPreview(const KFileItem& fileItem, const QPixmap& preview) +{ + Q_UNUSED(fileItem); + /* QPixmap paddedImage(QSize(PREVIEW_WIDTH, PREVIEW_HEIGHT)); + paddedImage.fill(Qt::transparent); + QPainter painter(&paddedImage); + KIcon kicon(fileItem.iconName()); + painter.drawPixmap((PREVIEW_WIDTH - preview.width()) / 2, (PREVIEW_HEIGHT - preview.height()) / 2, preview); + setData(Qt::DecorationRole, QIcon(paddedImage));*/ + setData(Qt::DecorationRole, QIcon(preview)); +}; +// Delegate everything to the base class, after re-setting the decorationSize +// to the preview size. +QSize DolphinBalloonTooltipDelegate::sizeHint(const KStyleOptionToolTip *option, const KToolTipItem *item) const +{ + KStyleOptionToolTip updatedStyleOption = *option; + updatedStyleOption.decorationSize = QSize(PREVIEW_WIDTH, PREVIEW_HEIGHT); + return KFormattedBalloonTipDelegate::sizeHint(&updatedStyleOption, item); +} +void DolphinBalloonTooltipDelegate::paint(QPainter *painter, const KStyleOptionToolTip *option, const KToolTipItem *item) const +{ + KStyleOptionToolTip updatedStyleOption = *option; + updatedStyleOption.decorationSize = QSize(PREVIEW_WIDTH, PREVIEW_HEIGHT); + return KFormattedBalloonTipDelegate::paint(painter, &updatedStyleOption, item); +} \ No newline at end of file diff --git a/src/dolphintooltip.h b/src/dolphintooltip.h new file mode 100644 index 000000000..32cc34098 --- /dev/null +++ b/src/dolphintooltip.h @@ -0,0 +1,55 @@ +/*************************************************************************** + * Copyright (C) 2008 by Simon St James * + * * + * 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 * + ***************************************************************************/ + +// NOTE: proper documentation will be added once the code is better developed. + +#ifndef DOLPHINTOOLTIP_H +#define DOLPHINTOOLTIP_H + +#include "ktooltip.h" +#include "kformattedballoontipdelegate.h" + +#include +#include + +class KFileItem; +class QPixmap; + +class DolphinToolTipItem : public QObject, public KToolTipItem +{ + Q_OBJECT +public: + DolphinToolTipItem(const KFileItem & fileItem); + virtual ~DolphinToolTipItem(); +private slots: + void setPreview(const KFileItem& fileItem, const QPixmap& preview); +private: +}; + +class DolphinBalloonTooltipDelegate : public KFormattedBalloonTipDelegate +{ +public: + DolphinBalloonTooltipDelegate() {} + virtual ~DolphinBalloonTooltipDelegate() {} + + virtual QSize sizeHint(const KStyleOptionToolTip *option, const KToolTipItem *item) const; + virtual void paint(QPainter *painter, const KStyleOptionToolTip *option, const KToolTipItem *item) const; +private: +}; +#endif \ No newline at end of file diff --git a/src/tooltipmanager.cpp b/src/tooltipmanager.cpp index 8e4ad0eba..2b41ac48f 100644 --- a/src/tooltipmanager.cpp +++ b/src/tooltipmanager.cpp @@ -19,10 +19,10 @@ #include "tooltipmanager.h" +#include "dolphintooltip.h" #include "dolphinmodel.h" #include "dolphinsortfilterproxymodel.h" -#include #include #include @@ -31,7 +31,7 @@ #include #include -K_GLOBAL_STATIC(KFormattedBalloonTipDelegate, g_delegate) +K_GLOBAL_STATIC(DolphinBalloonTooltipDelegate, g_delegate) ToolTipManager::ToolTipManager(QAbstractItemView* parent, DolphinSortFilterProxyModel* model) : @@ -103,7 +103,10 @@ void ToolTipManager::hideToolTip() void ToolTipManager::showToolTip() { - KToolTipItem* tip = new KToolTipItem(KIcon(m_item.iconName()), m_item.getToolTipText()); + // TODO - create tip during requestTip(...) - this makes it more likely that the previews + // job will have completed by the time the tooltip is shown, resulting in less flicker. + // The memory management will be more intricate, though. + DolphinToolTipItem *tip = new DolphinToolTipItem(m_item); KStyleOptionToolTip option; // TODO: get option content from KToolTip or add KToolTip::sizeHint() method @@ -127,12 +130,22 @@ void ToolTipManager::showToolTip() int x = m_itemRect.right(); int y = m_itemRect.bottom(); if (x + size.width() - 1 > desktop.right()) { - x = m_itemRect.left() - size.width(); + // Any room to the left of the item? + if (m_itemRect.left() - size.width() > desktop.left()) + { + x = m_itemRect.left() - size.width(); + } + else + { + // Move left until we are back onscreen; we'll be horizontally + // overlapping m_itemRect, but hopefully the y value will keep us + // from drawing inside it. + x = desktop.right() - size.width(); + } } if (y + size.height() - 1 > desktop.bottom()) { y = m_itemRect.top() - size.height(); } - KToolTip::showTip(QPoint(x, y), tip); }