From: Peter Penz Date: Tue, 3 Nov 2009 21:48:27 +0000 (+0000) Subject: Since Qt 4.5 it is possible to have translucient windows. The tooltip approach having... X-Git-Url: https://cloud.milkyroute.net/gitweb/dolphin.git/commitdiff_plain/74c1d61aa3547c80e1c7ee05397c76ccd6260fa7 Since Qt 4.5 it is possible to have translucient windows. The tooltip approach having a delegate and custom drawing + animation code for transparent tooltips has now been replaced by a translucient window where any kind of (animated) widget can be embedded. Still a lot of finetuning has to be done, but maybe this code can be part of kdelibs in KDE 4.5 (systemsettings currently used the Dolphin tooltips code). Additionally the tooltip uses now KMetaDataWidget instead of using KMetaInfo. This assures that no blocking of the application will occur even if there is a buggy Strigi analyzer that ignores the maximum size-setting and might block the retrieving of the meta data. BUG: 182941 BUG: 195878 svn path=/trunk/KDE/kdebase/apps/; revision=1044485 --- diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index f34367854..2c08a1ef9 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -41,11 +41,8 @@ set(dolphinprivate_LIB_SRCS settings/dolphinsettings.cpp settings/viewpropertiesdialog.cpp settings/viewpropsprogressinfo.cpp - tooltips/dolphintooltip.cpp tooltips/ktooltip.cpp - tooltips/ktooltipdelegate.cpp - tooltips/ktooltipitem.cpp - tooltips/kformattedballoontipdelegate.cpp + tooltips/ktooltipwindow.cpp tooltips/tooltipmanager.cpp versioncontrolobserver.cpp viewextensionsfactory.cpp diff --git a/src/pixmapviewer.h b/src/pixmapviewer.h index 26e520aab..c15750bb7 100644 --- a/src/pixmapviewer.h +++ b/src/pixmapviewer.h @@ -64,7 +64,7 @@ public: virtual ~PixmapViewer(); void setPixmap(const QPixmap& pixmap); - const QPixmap& pixmap() const; + QPixmap pixmap() const; /** * Sets the size hint to \a size and triggers a relayout @@ -89,7 +89,7 @@ private: QSize m_sizeHint; }; -inline const QPixmap& PixmapViewer::pixmap() const +inline QPixmap PixmapViewer::pixmap() const { return m_pixmap; } diff --git a/src/tooltips/dolphintooltip.cpp b/src/tooltips/dolphintooltip.cpp deleted file mode 100644 index 63e1c4041..000000000 --- a/src/tooltips/dolphintooltip.cpp +++ /dev/null @@ -1,52 +0,0 @@ -/*************************************************************************** - * 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 * - ***************************************************************************/ - -#include "dolphintooltip.h" - -#include -#include -#include - -#include - -DolphinBalloonTooltipDelegate::DolphinBalloonTooltipDelegate() -{ -} - -DolphinBalloonTooltipDelegate::~DolphinBalloonTooltipDelegate() -{ -} - -// 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); -} diff --git a/src/tooltips/kformattedballoontipdelegate.cpp b/src/tooltips/kformattedballoontipdelegate.cpp deleted file mode 100644 index 643068cbb..000000000 --- a/src/tooltips/kformattedballoontipdelegate.cpp +++ /dev/null @@ -1,134 +0,0 @@ -/******************************************************************************* - * Copyright (C) 2008 by Fredrik Höglund * - * 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 "kformattedballoontipdelegate.h" -#include "ktooltipitem.h" -#include "ktooltip.h" -#include -#include -#include -#include -#include - -KFormattedBalloonTipDelegate::KFormattedBalloonTipDelegate() -{ -} - -KFormattedBalloonTipDelegate::~KFormattedBalloonTipDelegate() -{ -} - -QSize KFormattedBalloonTipDelegate::sizeHint(const KStyleOptionToolTip &option, const KToolTipItem &item) const -{ - QTextDocument doc; - doc.setHtml(item.text()); - const QIcon icon = item.icon(); - - const QSize iconSize = icon.isNull() ? QSize(0, 0) : icon.actualSize(option.decorationSize); - const QSize docSize = doc.size().toSize(); - QSize contentSize = iconSize + docSize; - - // assure that the content height is large enough for the icon and the document - contentSize.setHeight(iconSize.height() > doc.size().height() ? iconSize.height() : doc.size().height()); - return contentSize + QSize(Border * 3, Border * 2); -} - -void KFormattedBalloonTipDelegate::paint(QPainter *painter, - const KStyleOptionToolTip &option, - const KToolTipItem &item) const -{ - QColor toColor = option.palette.brush(QPalette::ToolTipBase).color(); - QColor fromColor = KColorScheme::shade(toColor, KColorScheme::LightShade, 0.2); - - QPainterPath path = createPath(option); - if (haveAlphaChannel()) { - painter->setRenderHint(QPainter::Antialiasing); - painter->translate(.5, .5); - toColor.setAlpha(220); - fromColor.setAlpha(220); - } - - QLinearGradient gradient(option.rect.topLeft(), option.rect.bottomLeft()); - gradient.setColorAt(0.0, fromColor); - gradient.setColorAt(1.0, toColor); - painter->setPen(Qt::NoPen); - painter->setBrush(gradient); - - painter->drawPath(path); - - const QIcon icon = item.icon(); - int x = Border; - const int y = Border; - if (!icon.isNull()) { - const QSize iconSize = icon.actualSize(option.decorationSize); - const QPoint pos(x + option.rect.x(), y + option.rect.y()); - painter->drawPixmap(pos, icon.pixmap(iconSize)); - x += iconSize.width() + Border; - } - - QTextDocument doc; - doc.setHtml(item.text()); - QPixmap bitmap(doc.size().toSize()); - bitmap.fill(Qt::transparent); - QPainter p(&bitmap); - doc.drawContents(&p); - - const QRect docRect(QPoint(x, y), doc.size().toSize()); - painter->drawPixmap(docRect, bitmap); -} - -QRegion KFormattedBalloonTipDelegate::inputShape(const KStyleOptionToolTip &option) const -{ - QBitmap bitmap(option.rect.size()); - bitmap.fill(Qt::color0); - - QPainter p(&bitmap); - p.setPen(QPen(Qt::color1, 1)); - p.setBrush(Qt::color1); - p.drawPath(createPath(option)); - - return QRegion(bitmap); -} - -QRegion KFormattedBalloonTipDelegate::shapeMask(const KStyleOptionToolTip &option) const -{ - return inputShape(option); -} - -static inline void arc(QPainterPath &path, qreal cx, qreal cy, qreal radius, qreal angle, qreal sweeplength) -{ - path.arcTo(cx-radius, cy-radius, radius * 2, radius * 2, angle, sweeplength); -} - -QPainterPath KFormattedBalloonTipDelegate::createPath(const KStyleOptionToolTip& option) const -{ - const QRect rect = option.rect; - const qreal radius = 5; - - QPainterPath path; - path.moveTo(rect.left(), rect.top() + radius); - arc(path, rect.left() + radius, rect.top() + radius, radius, 180, -90); - arc(path, rect.right() - radius, rect.top() + radius, radius, 90, -90); - arc(path, rect.right() - radius, rect.bottom() - radius, radius, 0, -90); - arc(path, rect.left() + radius, rect.bottom() - radius, radius, 270, -90); - path.closeSubpath(); - - return path; -} diff --git a/src/tooltips/kformattedballoontipdelegate.h b/src/tooltips/kformattedballoontipdelegate.h deleted file mode 100644 index bc8ab23f9..000000000 --- a/src/tooltips/kformattedballoontipdelegate.h +++ /dev/null @@ -1,45 +0,0 @@ -/******************************************************************************* - * Copyright (C) 2008 by Fredrik Höglund * - * 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 KFORMATTEDBALLOONTIPDELEGATE_H -#define KFORMATTEDBALLOONTIPDELEGATE_H - -#include -#include - -class KFormattedBalloonTipDelegate : public KToolTipDelegate -{ -public: - KFormattedBalloonTipDelegate(); - virtual ~KFormattedBalloonTipDelegate(); - - virtual QSize sizeHint(const KStyleOptionToolTip &option, const KToolTipItem &item) const; - virtual void paint(QPainter *painter, const KStyleOptionToolTip &option, const KToolTipItem &item) const; - virtual QRegion inputShape(const KStyleOptionToolTip &option) const; - virtual QRegion shapeMask(const KStyleOptionToolTip &option) const; - -private: - QPainterPath createPath(const KStyleOptionToolTip& option) const; - -private: - enum { Border = 8 }; -}; - -#endif diff --git a/src/tooltips/ktooltip.cpp b/src/tooltips/ktooltip.cpp index 52a4d4a5f..c476fda1b 100644 --- a/src/tooltips/ktooltip.cpp +++ b/src/tooltips/ktooltip.cpp @@ -18,210 +18,84 @@ ***************************************************************************/ #include "ktooltip.h" -#include "ktooltip_p.h" -#include "ktooltipdelegate.h" - -#include -#include +#include "ktooltipwindow_p.h" +#include +#include #include -#include - -#ifdef Q_WS_X11 -# include -# include -# include -#endif - -// compile with XShape older than 1.0 -#ifndef ShapeInput -const int ShapeInput = 2; -#endif - -// ---------------------------------------------------------------------------- - - -class KTipLabel : public QWidget +class KToolTipManager { public: - KTipLabel(); - void showTip(const QPoint &pos, const KToolTipItem *item); - void moveTip(const QPoint &pos); + ~KToolTipManager(); + + static KToolTipManager* instance(); + + void showTip(const QPoint& pos, QWidget* content); void hideTip(); private: - void paintEvent(QPaintEvent*); - QSize sizeHint() const; - KStyleOptionToolTip styleOption() const; - KToolTipDelegate *delegate() const; + KToolTipManager(); -private: - const KToolTipItem *m_currentItem; + KToolTipWindow* m_window; + static KToolTipManager *s_instance; }; -KTipLabel::KTipLabel() : QWidget(0, Qt::ToolTip) -{ -#ifdef Q_WS_X11 - if (QX11Info::isCompositingManagerRunning()) { - setAttribute(Qt::WA_TranslucentBackground); - } -#endif -} - -void KTipLabel::showTip(const QPoint &pos, const KToolTipItem *item) -{ - m_currentItem = item; - move(pos); - show(); -} - -void KTipLabel::hideTip() -{ - hide(); - m_currentItem = 0; -} - -void KTipLabel::moveTip(const QPoint &pos) -{ - move(pos); -} - -void KTipLabel::paintEvent(QPaintEvent*) -{ - if (!m_currentItem) - return; - - KStyleOptionToolTip option = styleOption(); - option.rect = rect(); - -#ifdef Q_WS_X11 - if (QX11Info::isCompositingManagerRunning()) - XShapeCombineRegion(x11Info().display(), winId(), ShapeInput, 0, 0, - delegate()->inputShape(option).handle(), ShapeSet); - else -#endif - setMask(delegate()->shapeMask(option)); - - QPainter p(this); - p.setFont(option.font); - p.setPen(QPen(option.palette.brush(QPalette::Text), 0)); - delegate()->paint(&p, option, *m_currentItem); -} - -QSize KTipLabel::sizeHint() const -{ - if (!m_currentItem) - return QSize(); - - KStyleOptionToolTip option = styleOption(); - return delegate()->sizeHint(option, *m_currentItem); -} - -KStyleOptionToolTip KTipLabel::styleOption() const -{ - KStyleOptionToolTip option; - KToolTipManager::instance()->initStyleOption(&option); - return option; -} - -KToolTipDelegate *KTipLabel::delegate() const -{ - return KToolTipManager::instance()->delegate(); -} - - -// ---------------------------------------------------------------------------- - - KToolTipManager *KToolTipManager::s_instance = 0; -KToolTipManager::KToolTipManager() - : m_label(new KTipLabel), m_currentItem(0), m_delegate(0) +KToolTipManager::KToolTipManager() : + m_window(0) { } KToolTipManager::~KToolTipManager() { - delete m_label; - delete m_currentItem; -} - -void KToolTipManager::showTip(const QPoint &pos, KToolTipItem *item) -{ - hideTip(); - m_label->showTip(pos, item); - m_currentItem = item; - m_tooltipPos = pos; -} - -void KToolTipManager::hideTip() -{ - m_label->hideTip(); - delete m_currentItem; - m_currentItem = 0; + delete m_window; + m_window = 0; } -void KToolTipManager::initStyleOption(KStyleOptionToolTip *option) const +KToolTipManager* KToolTipManager::instance() { - option->direction = QApplication::layoutDirection(); - option->fontMetrics = QFontMetrics(QToolTip::font()); - option->activeCorner = KStyleOptionToolTip::TopLeftCorner; - option->palette = QToolTip::palette(); - option->font = QToolTip::font(); - option->rect = QRect(); - option->state = QStyle::State_None; - option->decorationSize = QSize(32, 32); -} + if (s_instance == 0) { + s_instance = new KToolTipManager(); + } -void KToolTipManager::setDelegate(KToolTipDelegate *delegate) -{ - m_delegate = delegate; + return s_instance; } -void KToolTipManager::update() +void KToolTipManager::showTip(const QPoint& pos, QWidget* content) { - if (m_currentItem == 0) - return; - m_label->showTip(m_tooltipPos, m_currentItem); + hideTip(); + Q_ASSERT(m_window == 0); + m_window = new KToolTipWindow(content); + m_window->move(pos); + m_window->show(); } -KToolTipDelegate *KToolTipManager::delegate() const +void KToolTipManager::hideTip() { - return m_delegate; + if (m_window != 0) { + m_window->hide(); + delete m_window; + m_window = 0; + } } - -// ---------------------------------------------------------------------------- - - namespace KToolTip { - void showText(const QPoint &pos, const QString &text, QWidget *widget, const QRect &rect) - { - Q_UNUSED(widget) - Q_UNUSED(rect) - KToolTipItem *item = new KToolTipItem(text); - KToolTipManager::instance()->showTip(pos, item); - } - - void showText(const QPoint &pos, const QString &text, QWidget *widget) + void showText(const QPoint& pos, const QString& text) { - showText(pos, text, widget, QRect()); + QLabel* label = new QLabel(text); + showTip(pos, label); } - void showTip(const QPoint &pos, KToolTipItem *item) + void showTip(const QPoint& pos, QWidget* content) { - KToolTipManager::instance()->showTip(pos, item); + KToolTipManager::instance()->showTip(pos, content); } void hideTip() { KToolTipManager::instance()->hideTip(); } - - void setToolTipDelegate(KToolTipDelegate *delegate) - { - KToolTipManager::instance()->setDelegate(delegate); - } } diff --git a/src/tooltips/ktooltip.h b/src/tooltips/ktooltip.h index 6de08315c..bd539b834 100644 --- a/src/tooltips/ktooltip.h +++ b/src/tooltips/ktooltip.h @@ -20,35 +20,27 @@ #ifndef KTOOLTIP_H #define KTOOLTIP_H -#include - -#include -#include -class KToolTipDelegate; +class QPoint; +class QString; +class QWidget; /** - * KToolTip provides customizable tooltips that can have animations as well as an alpha - * channel, allowing for dynamic transparency effects. - * - * ARGB tooltips work on X11 even when the application isn't using the ARGB visual. + * Allows to show tooltips having a widget as content. */ namespace KToolTip { - void showText(const QPoint &pos, const QString &text, QWidget *widget, const QRect &rect); - void showText(const QPoint &pos, const QString &text, QWidget *widget = 0); - + void showText(const QPoint& pos, const QString& text); + /** - * Shows the tip @p item at the global position indicated by @p pos. + * Shows the tip @p content at the global position indicated by @p pos. * - * Ownership of the item is transferred to KToolTip. The item will be deleted + * Ownership of the content widget is transferred to KToolTip. The widget will be deleted * automatically when it is hidden. * * The tip is shown immediately when this function is called. */ - void showTip(const QPoint &pos, KToolTipItem *item); + void showTip(const QPoint& pos, QWidget* content); void hideTip(); - - void setToolTipDelegate(KToolTipDelegate *delegate); } #endif diff --git a/src/tooltips/ktooltip_p.h b/src/tooltips/ktooltip_p.h index 5bc37236d..f58622973 100644 --- a/src/tooltips/ktooltip_p.h +++ b/src/tooltips/ktooltip_p.h @@ -31,30 +31,15 @@ class KToolTipManager public: ~KToolTipManager(); - static KToolTipManager *instance() { - if (!s_instance) - s_instance = new KToolTipManager(); + static KToolTipManager* instance(); - return s_instance; - } - - void showTip(const QPoint &pos, KToolTipItem *item); + void showTip(const QPoint &pos, QWidget *tip); void hideTip(); - void initStyleOption(KStyleOptionToolTip *option) const; - - void setDelegate(KToolTipDelegate *delegate); - KToolTipDelegate *delegate() const; - - void update(); - private: KToolTipManager(); - KTipLabel *m_label; - KToolTipItem *m_currentItem; - KToolTipDelegate *m_delegate; - + QWidget *m_widget; QPoint m_tooltipPos; static KToolTipManager *s_instance; diff --git a/src/tooltips/ktooltipdelegate.cpp b/src/tooltips/ktooltipdelegate.cpp deleted file mode 100644 index 0141db143..000000000 --- a/src/tooltips/ktooltipdelegate.cpp +++ /dev/null @@ -1,138 +0,0 @@ -/*************************************************************************** - * Copyright (C) 2008 by Fredrik Höglund * - * * - * 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 "ktooltipdelegate.h" - -#include "ktooltip.h" - -#include -#include -#include - -#ifdef Q_WS_X11 -# include -# include -# include -#endif - - -// ---------------------------------------------------------------------------- - - -KStyleOptionToolTip::KStyleOptionToolTip() - : fontMetrics(QApplication::font()) -{ -} - - -// ---------------------------------------------------------------------------- - - -KToolTipDelegate::KToolTipDelegate() : QObject() -{ -} - -KToolTipDelegate::~KToolTipDelegate() -{ -} - -QSize KToolTipDelegate::sizeHint(const KStyleOptionToolTip &option, const KToolTipItem &item) const -{ - QSize size; - size.rwidth() = option.fontMetrics.width(item.text()); - size.rheight() = option.fontMetrics.lineSpacing(); - - QIcon icon = item.icon(); - if (!icon.isNull()) { - const QSize iconSize = icon.actualSize(option.decorationSize); - size.rwidth() += iconSize.width() + 4; - size.rheight() = qMax(size.height(), iconSize.height()); - } - - return size + QSize(20, 20); - -} - -void KToolTipDelegate::paint(QPainter *painter, - const KStyleOptionToolTip &option, - const KToolTipItem &item) const -{ - bool haveAlpha = haveAlphaChannel(); - painter->setRenderHint(QPainter::Antialiasing); - - QPainterPath path; - if (haveAlpha) - path.addRoundRect(option.rect.adjusted(0, 0, -1, -1), 25); - else - path.addRect(option.rect.adjusted(0, 0, -1, -1)); - - QColor color = option.palette.color(QPalette::ToolTipBase); - QColor from = color.lighter(105); - QColor to = color.darker(120); - - QLinearGradient gradient(0, 0, 0, 1); - gradient.setCoordinateMode(QGradient::ObjectBoundingMode); - gradient.setColorAt(0, from); - gradient.setColorAt(1, to); - - painter->translate(.5, .5); - painter->setPen(QPen(Qt::black, 1)); - painter->setBrush(gradient); - painter->drawPath(path); - painter->translate(-.5, -.5); - - if (haveAlpha) { - QLinearGradient mask(0, 0, 0, 1); - gradient.setCoordinateMode(QGradient::ObjectBoundingMode); - gradient.setColorAt(0, QColor(0, 0, 0, 192)); - gradient.setColorAt(1, QColor(0, 0, 0, 72)); - painter->setCompositionMode(QPainter::CompositionMode_DestinationIn); - painter->fillRect(option.rect, gradient); - painter->setCompositionMode(QPainter::CompositionMode_SourceOver); - } - - QRect textRect = option.rect.adjusted(10, 10, -10, -10); - - QIcon icon = item.icon(); - if (!icon.isNull()) { - const QSize iconSize = icon.actualSize(option.decorationSize); - painter->drawPixmap(textRect.topLeft(), icon.pixmap(iconSize)); - textRect.adjust(iconSize.width() + 4, 0, 0, 0); - } - painter->drawText(textRect, Qt::AlignLeft | Qt::AlignVCenter, item.text()); -} - -QRegion KToolTipDelegate::inputShape(const KStyleOptionToolTip &option) const -{ - return QRegion(option.rect); -} - -QRegion KToolTipDelegate::shapeMask(const KStyleOptionToolTip &option) const -{ - return QRegion(option.rect); -} - -bool KToolTipDelegate::haveAlphaChannel() const -{ -#ifdef Q_WS_X11 - return QX11Info::isCompositingManagerRunning(); -#else - return false; -#endif -} diff --git a/src/tooltips/ktooltipdelegate.h b/src/tooltips/ktooltipdelegate.h deleted file mode 100644 index a219ec5bf..000000000 --- a/src/tooltips/ktooltipdelegate.h +++ /dev/null @@ -1,110 +0,0 @@ -/*************************************************************************** - * Copyright (C) 2008 by Fredrik Höglund * - * * - * 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 KTOOLTIPDELEGATE_H -#define KTOOLTIPDELEGATE_H - -#include -#include -#include -#include -#include -#include -#include - -class KStyleOptionToolTip; -class KToolTipItem; -class QPainter; -class QRegion; - -class KStyleOptionToolTip -{ -public: - KStyleOptionToolTip(); - enum Corner { TopLeftCorner, TopRightCorner, BottomLeftCorner, BottomRightCorner, NoCorner }; - - Qt::LayoutDirection direction; - QFontMetrics fontMetrics; - QPalette palette; - QRect rect; - QStyle::State state; - QFont font; - QSize decorationSize; - Corner activeCorner; -}; - -/** - * KToolTipDelegate is responsible for providing the size hint and - * painting the tooltips. - */ -class KToolTipDelegate : public QObject -{ - Q_OBJECT -public: - KToolTipDelegate(); - virtual ~KToolTipDelegate(); - - virtual QSize sizeHint(const KStyleOptionToolTip &option, const KToolTipItem &item) const; - - /** - * If haveAlphaChannel() returns true, the paint device will be filled with - * Qt::transparent when this function is called, otherwise the content is - * undefined. - */ - virtual void paint(QPainter *painter, - const KStyleOptionToolTip &option, - const KToolTipItem &item) const; - - /** - * Reimplement this function to specify the region of the tooltip - * that accepts input. Any mouse events that occur outside this - * region will be sent to the widget below the tooltip. - * - * The default implementation returns a region containing the - * bounding rect of the tooltip. - * - * This function will only be called if haveAlphaChannel() - * returns true. - */ - virtual QRegion inputShape(const KStyleOptionToolTip &option) const; - - /** - * Reimplement this function to specify a shape mask for the tooltip. - * - * The default implementation returns a region containing the - * bounding rect of the tooltip. - * - * This function will only be called if haveAlphaChannel() - * returns false. - */ - virtual QRegion shapeMask(const KStyleOptionToolTip &option) const; - -protected: - /** - * Returns true if the tooltip has an alpha channel, and false - * otherwise. - * - * Implementors should assume that this condition may change at - * any time during the runtime of the application, as compositing - * can be enabled or disabled in the window manager. - */ - bool haveAlphaChannel() const; -}; - -#endif diff --git a/src/tooltips/ktooltipitem.cpp b/src/tooltips/ktooltipitem.cpp deleted file mode 100644 index 78f778a1c..000000000 --- a/src/tooltips/ktooltipitem.cpp +++ /dev/null @@ -1,76 +0,0 @@ -/*************************************************************************** - * Copyright (C) 2008 by Fredrik Höglund * - * * - * 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 "ktooltipitem.h" -#include "ktooltip_p.h" - -#include - -class KToolTipItemPrivate -{ -public: - QMap map; - int type; -}; - -KToolTipItem::KToolTipItem(const QString &text, int type) - : d(new KToolTipItemPrivate) -{ - d->map[Qt::DisplayRole] = text; - d->type = type; -} - -KToolTipItem::KToolTipItem(const QIcon &icon, const QString &text, int type) - : d(new KToolTipItemPrivate) -{ - d->map[Qt::DecorationRole] = icon; - d->map[Qt::DisplayRole] = text; - d->type = type; -} - -KToolTipItem::~KToolTipItem() -{ - delete d; -} - -int KToolTipItem::type() const -{ - return d->type; -} - -QString KToolTipItem::text() const -{ - return data(Qt::DisplayRole).toString(); -} - -QIcon KToolTipItem::icon() const -{ - return qvariant_cast(data(Qt::DecorationRole)); -} - -QVariant KToolTipItem::data(int role) const -{ - return d->map.value(role); -} - -void KToolTipItem::setData(int role, const QVariant &data) -{ - d->map[role] = data; - KToolTipManager::instance()->update(); -} diff --git a/src/tooltips/ktooltipitem.h b/src/tooltips/ktooltipitem.h deleted file mode 100644 index 7142e5d23..000000000 --- a/src/tooltips/ktooltipitem.h +++ /dev/null @@ -1,81 +0,0 @@ -/*************************************************************************** - * Copyright (C) 2008 by Fredrik Höglund * - * * - * 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 KTOOLTIPITEM_H -#define KTOOLTIPITEM_H - -#include - -class QString; -class QIcon; - -class KToolTipItemPrivate; - -/** - * KToolTipItem contains the data to be displayed in a tooltip. - * - * Custom data can be stored as QVariants in the object by calling - * setData() with a custom item role, and retrieved and displayed - * by a tooltip delegate by calling data(). - * - * The default tooltip delegate uses Qt::DecorationRole and - * Qt::DisplayRole. - * - * To display the tooltip, call KToolTip::showTip() with a pointer - * to the KToolTipItem. - * - * You can reimplement the setData() and/or data() methods in this - * class to implement on-demand loading of data. - */ -class KToolTipItem -{ -public: - enum ItemType { DefaultType, UserType = 1000 }; - - /** - * Creates a KToolTipItem with @p text and no icon. - */ - explicit KToolTipItem(const QString &text, int type = DefaultType); - - /** - * Creates a KToolTipItem with an @p icon and @p text. - */ - KToolTipItem(const QIcon &icon, const QString &text, int type = DefaultType); - - /** - * Destroys the KToolTipItem. - */ - virtual ~KToolTipItem(); - - /** - * Returns the item type. - */ - int type() const; - - QString text() const; - QIcon icon() const; - - virtual QVariant data(int role) const; - virtual void setData(int role, const QVariant &data); - -private: - KToolTipItemPrivate * const d; -}; - -#endif diff --git a/src/tooltips/ktooltipwindow.cpp b/src/tooltips/ktooltipwindow.cpp new file mode 100644 index 000000000..9a1cb45eb --- /dev/null +++ b/src/tooltips/ktooltipwindow.cpp @@ -0,0 +1,93 @@ +/******************************************************************************* + * Copyright (C) 2008 by Fredrik Höglund * + * Copyright (C) 2008 by Konstantin Heil * + * Copyright (C) 2009 by Peter Penz * + * * + * 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 "ktooltipwindow_p.h" + +#include + +#include +#include + +#ifdef Q_WS_X11 + #include +#endif + +KToolTipWindow::KToolTipWindow(QWidget* content) : + QWidget(0) +{ + setAttribute(Qt::WA_TranslucentBackground); + setWindowFlags(Qt::ToolTip | Qt::FramelessWindowHint); + + QVBoxLayout* layout = new QVBoxLayout(this); + layout->addWidget(content); +} + +KToolTipWindow::~KToolTipWindow() +{ +} + +void KToolTipWindow::paintEvent(QPaintEvent* event) +{ + Q_UNUSED(event); + + QPainter painter(this); + + QColor toColor = palette().brush(QPalette::ToolTipBase).color(); + QColor fromColor = KColorScheme::shade(toColor, KColorScheme::LightShade, 0.2); + +#ifdef Q_WS_X11 + const bool haveAlphaChannel = QX11Info::isCompositingManagerRunning(); +#else + const bool haveAlphaChannel = false; +#endif + if (haveAlphaChannel) { + painter.setRenderHint(QPainter::Antialiasing); + painter.translate(0.5, 0.5); + toColor.setAlpha(220); + fromColor.setAlpha(220); + } + + QLinearGradient gradient(QPointF(0.0, 0.0), QPointF(width(), height())); + gradient.setColorAt(0.0, fromColor); + gradient.setColorAt(1.0, toColor); + painter.setPen(Qt::NoPen); + painter.setBrush(gradient); + + const QRect rect(0, 0, width(), height()); + const qreal radius = 5; + + QPainterPath path; + path.moveTo(rect.left(), rect.top() + radius); + arc(path, rect.left() + radius, rect.top() + radius, radius, 180, -90); + arc(path, rect.right() - radius, rect.top() + radius, radius, 90, -90); + arc(path, rect.right() - radius, rect.bottom() - radius, radius, 0, -90); + arc(path, rect.left() + radius, rect.bottom() - radius, radius, 270, -90); + path.closeSubpath(); + + painter.drawPath(path); +} + +void KToolTipWindow::arc(QPainterPath& path, qreal cx, qreal cy, qreal radius, qreal angle, qreal sweeplength) +{ + path.arcTo(cx-radius, cy-radius, radius * 2, radius * 2, angle, sweeplength); +} + +#include "ktooltipwindow_p.moc" diff --git a/src/tooltips/dolphintooltip.h b/src/tooltips/ktooltipwindow_p.h similarity index 63% rename from src/tooltips/dolphintooltip.h rename to src/tooltips/ktooltipwindow_p.h index 89bbd74d6..a9ac30f82 100644 --- a/src/tooltips/dolphintooltip.h +++ b/src/tooltips/ktooltipwindow_p.h @@ -1,5 +1,5 @@ /*************************************************************************** - * Copyright (C) 2008 by Simon St James * + * Copyright (C) 2009 by Peter Penz * * * * 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 * @@ -17,29 +17,29 @@ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA * ***************************************************************************/ -// NOTE: proper documentation will be added once the code is better developed. +#ifndef KTOOLTIPWINDOW_H +#define KTOOLTIPWINDOW_H -#ifndef DOLPHINTOOLTIP_H -#define DOLPHINTOOLTIP_H +#include +class QPaintEvent; -#include -#include - -#include -#include - - -const int PREVIEW_WIDTH = 256; -const int PREVIEW_HEIGHT = 256; - - -class DolphinBalloonTooltipDelegate : public KFormattedBalloonTipDelegate +class KToolTipWindow : public QWidget { -public: - DolphinBalloonTooltipDelegate(); - virtual ~DolphinBalloonTooltipDelegate(); + Q_OBJECT - virtual QSize sizeHint(const KStyleOptionToolTip& option, const KToolTipItem& item) const; - virtual void paint(QPainter* painter, const KStyleOptionToolTip& option, const KToolTipItem& item) const; +public: + explicit KToolTipWindow(QWidget* content); + virtual ~KToolTipWindow(); + +protected: + virtual void paintEvent(QPaintEvent* event); + +private: + /** + * Helper method for KToolTipWindow::paintEvent() to adjust the painter path \p path + * by rounded corners. + */ + static void arc(QPainterPath& path, qreal cx, qreal cy, qreal radius, qreal angle, qreal sweeplength); }; + #endif diff --git a/src/tooltips/tooltipmanager.cpp b/src/tooltips/tooltipmanager.cpp index fbb36aaf5..9851ece6b 100644 --- a/src/tooltips/tooltipmanager.cpp +++ b/src/tooltips/tooltipmanager.cpp @@ -19,25 +19,22 @@ #include "tooltipmanager.h" -#include "dolphintooltip.h" #include "dolphinmodel.h" #include "dolphinsortfilterproxymodel.h" #include -#include #include +#include "panels/information/kmetadatawidget.h" +#include "tooltips/ktooltip.h" + #include #include +#include +#include +#include #include #include -#include - -const int ICON_WIDTH = 128; -const int ICON_HEIGHT = 128; -const int PREVIEW_DELAY = 250; - -K_GLOBAL_STATIC(DolphinBalloonTooltipDelegate, g_delegate) ToolTipManager::ToolTipManager(QAbstractItemView* parent, DolphinSortFilterProxyModel* model) : @@ -54,8 +51,6 @@ ToolTipManager::ToolTipManager(QAbstractItemView* parent, m_hasDefaultIcon(false), m_previewPixmap() { - KToolTip::setToolTipDelegate(g_delegate); - m_dolphinModel = static_cast(m_proxyModel->sourceModel()); connect(parent, SIGNAL(entered(const QModelIndex&)), this, SLOT(requestToolTip(const QModelIndex&))); @@ -157,36 +152,55 @@ void ToolTipManager::prepareToolTip() m_waitOnPreviewTimer->start(250); } - const QString text = m_item.getToolTipText(); if (!m_previewPixmap.isNull()) { - showToolTip(KIcon(m_previewPixmap), text); + showToolTip(m_previewPixmap); } else if (!m_hasDefaultIcon) { - const QPixmap image(KIcon(m_item.iconName()).pixmap(ICON_WIDTH, ICON_HEIGHT)); - showToolTip(image, text); + const QPixmap image(KIcon(m_item.iconName()).pixmap(128, 128)); + showToolTip(image); m_hasDefaultIcon = true; } } -void ToolTipManager::showToolTip(const QIcon& icon, const QString& text) +void ToolTipManager::startPreviewJob() +{ + m_generatingPreview = true; + KIO::PreviewJob* job = KIO::filePreview(KFileItemList() << m_item, 256, 256); + + connect(job, SIGNAL(gotPreview(const KFileItem&, const QPixmap&)), + this, SLOT(setPreviewPix(const KFileItem&, const QPixmap&))); + connect(job, SIGNAL(failed(const KFileItem&)), + this, SLOT(previewFailed())); +} + + +void ToolTipManager::setPreviewPix(const KFileItem& item, + const QPixmap& pixmap) +{ + if ((m_item.url() != item.url()) || pixmap.isNull()) { + // an old preview or an invalid preview has been received + previewFailed(); + } else { + m_previewPixmap = pixmap; + m_generatingPreview = false; + } +} + +void ToolTipManager::previewFailed() +{ + m_generatingPreview = false; +} + + +void ToolTipManager::showToolTip(const QPixmap& pixmap) { if (QApplication::mouseButtons() & Qt::LeftButton) { return; } - KToolTipItem* tip = new KToolTipItem(icon, text); + QWidget* tip = createTipContent(pixmap); - KStyleOptionToolTip option; - // TODO: get option content from KToolTip or add KToolTip::sizeHint() method - option.direction = QApplication::layoutDirection(); - option.fontMetrics = QFontMetrics(QToolTip::font()); - option.activeCorner = KStyleOptionToolTip::TopLeftCorner; - option.palette = QToolTip::palette(); - option.font = QToolTip::font(); - option.rect = QRect(); - option.state = QStyle::State_None; - option.decorationSize = QSize(32, 32); - - const QSize size = g_delegate->sizeHint(option, *tip); + // calculate the x- and y-position of the tooltip + const QSize size = tip->sizeHint(); const QRect desktop = QApplication::desktop()->screenGeometry(m_itemRect.bottomRight()); // m_itemRect defines the area of the item, where the tooltip should be @@ -225,37 +239,24 @@ void ToolTipManager::showToolTip(const QIcon& icon, const QString& text) KToolTip::showTip(QPoint(x, y), tip); } - - -void ToolTipManager::startPreviewJob() +QWidget* ToolTipManager::createTipContent(const QPixmap& pixmap) const { - m_generatingPreview = true; - KIO::PreviewJob* job = KIO::filePreview(KFileItemList() << m_item, - PREVIEW_WIDTH, - PREVIEW_HEIGHT); + QWidget* tipContent = new QWidget(); - connect(job, SIGNAL(gotPreview(const KFileItem&, const QPixmap&)), - this, SLOT(setPreviewPix(const KFileItem&, const QPixmap&))); - connect(job, SIGNAL(failed(const KFileItem&)), - this, SLOT(previewFailed())); -} + QLabel* pixmapLabel = new QLabel(tipContent); + pixmapLabel->setPixmap(pixmap); + pixmapLabel->setFixedSize(pixmap.size()); + KMetaDataWidget* metaDataWidget = new KMetaDataWidget(tipContent); + metaDataWidget->setItem(m_item); + metaDataWidget->setFixedSize(metaDataWidget->sizeHint()); -void ToolTipManager::setPreviewPix(const KFileItem& item, - const QPixmap& pixmap) -{ - if ((m_item.url() != item.url()) || pixmap.isNull()) { - // an old preview or an invalid preview has been received - previewFailed(); - } else { - m_previewPixmap = pixmap; - m_generatingPreview = false; - } -} + QHBoxLayout* tipLayout = new QHBoxLayout(tipContent); + tipLayout->setMargin(0); + tipLayout->addWidget(pixmapLabel); + tipLayout->addWidget(metaDataWidget); -void ToolTipManager::previewFailed() -{ - m_generatingPreview = false; + return tipContent; } #include "tooltipmanager.moc" diff --git a/src/tooltips/tooltipmanager.h b/src/tooltips/tooltipmanager.h index b9af69445..e1f9770c3 100644 --- a/src/tooltips/tooltipmanager.h +++ b/src/tooltips/tooltipmanager.h @@ -67,7 +67,13 @@ private slots: void previewFailed(); private: - void showToolTip(const QIcon& icon, const QString& text); + void showToolTip(const QPixmap& pixmap); + + /** + * Creates widget that represents the tip content having + * an icon and the meta data information. + */ + QWidget* createTipContent(const QPixmap& pixmap) const; QAbstractItemView* m_view; DolphinModel* m_dolphinModel;