diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 083b31e21f..c959899a24 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -43,6 +43,8 @@ set(dolphinprivate_LIB_SRCS settings/viewpropsprogressinfo.cpp tooltips/dolphintooltip.cpp tooltips/ktooltip.cpp + tooltips/ktooltipdelegate.cpp + tooltips/ktooltipitem.cpp tooltips/kformattedballoontipdelegate.cpp tooltips/tooltipmanager.cpp viewproperties.cpp diff --git a/src/tooltips/kformattedballoontipdelegate.cpp b/src/tooltips/kformattedballoontipdelegate.cpp index 2659ccd976..643068cbb8 100644 --- a/src/tooltips/kformattedballoontipdelegate.cpp +++ b/src/tooltips/kformattedballoontipdelegate.cpp @@ -19,7 +19,10 @@ *******************************************************************************/ #include "kformattedballoontipdelegate.h" +#include "ktooltipitem.h" +#include "ktooltip.h" #include +#include #include #include #include diff --git a/src/tooltips/kformattedballoontipdelegate.h b/src/tooltips/kformattedballoontipdelegate.h index 4dc9f8bbbe..bc8ab23f98 100644 --- a/src/tooltips/kformattedballoontipdelegate.h +++ b/src/tooltips/kformattedballoontipdelegate.h @@ -21,7 +21,7 @@ #ifndef KFORMATTEDBALLOONTIPDELEGATE_H #define KFORMATTEDBALLOONTIPDELEGATE_H -#include +#include #include class KFormattedBalloonTipDelegate : public KToolTipDelegate diff --git a/src/tooltips/ktooltip.cpp b/src/tooltips/ktooltip.cpp index e33cd197c9..114271661f 100644 --- a/src/tooltips/ktooltip.cpp +++ b/src/tooltips/ktooltip.cpp @@ -19,16 +19,12 @@ #include "ktooltip.h" #include "ktooltip_p.h" +#include "ktooltipdelegate.h" #include -#include -#include #include -#include -#include #include #include -#include #ifdef Q_WS_X11 # include @@ -42,173 +38,9 @@ const int ShapeInput = 2; #endif -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(); -} - - - // ---------------------------------------------------------------------------- -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 -} - - - -// ---------------------------------------------------------------------------- - - - class KTipLabel : public QWidget { public: @@ -295,13 +127,9 @@ KToolTipDelegate *KTipLabel::delegate() const } - - // ---------------------------------------------------------------------------- - - KToolTipManager *KToolTipManager::s_instance = 0; KToolTipManager::KToolTipManager() @@ -360,11 +188,9 @@ KToolTipDelegate *KToolTipManager::delegate() const } - // ---------------------------------------------------------------------------- - namespace KToolTip { void showText(const QPoint &pos, const QString &text, QWidget *widget, const QRect &rect) diff --git a/src/tooltips/ktooltip.h b/src/tooltips/ktooltip.h index 31589c6904..6de08315c0 100644 --- a/src/tooltips/ktooltip.h +++ b/src/tooltips/ktooltip.h @@ -20,156 +20,11 @@ #ifndef KTOOLTIP_H #define KTOOLTIP_H -#include -#include -#include -#include +#include + #include #include - -class QString; -class QIcon; -class QSize; -class QPainter; -class QRegion; - -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; -}; - - -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; - -#if 0 -private Q_SLOTS: - /** - * Schedules a repaint of the tooltip item. - * This slot can be connected to a timer to animate the tooltip. - */ - void update(const KToolTipItem *item); -#endif -}; - +class KToolTipDelegate; /** * KToolTip provides customizable tooltips that can have animations as well as an alpha diff --git a/src/tooltips/ktooltip_p.h b/src/tooltips/ktooltip_p.h index 5aa5326cfa..5bc37236de 100644 --- a/src/tooltips/ktooltip_p.h +++ b/src/tooltips/ktooltip_p.h @@ -20,6 +20,8 @@ #ifndef KTOOLTIP_P_H #define KTOOLTIP_P_H +#include + class KTipLabel; class KStyleOptionToolTip; class KToolTipDelegate; diff --git a/src/tooltips/ktooltipdelegate.cpp b/src/tooltips/ktooltipdelegate.cpp new file mode 100644 index 0000000000..0141db1437 --- /dev/null +++ b/src/tooltips/ktooltipdelegate.cpp @@ -0,0 +1,138 @@ +/*************************************************************************** + * 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 new file mode 100644 index 0000000000..a219ec5bff --- /dev/null +++ b/src/tooltips/ktooltipdelegate.h @@ -0,0 +1,110 @@ +/*************************************************************************** + * 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 new file mode 100644 index 0000000000..78f778a1c1 --- /dev/null +++ b/src/tooltips/ktooltipitem.cpp @@ -0,0 +1,76 @@ +/*************************************************************************** + * 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 new file mode 100644 index 0000000000..1d628d8747 --- /dev/null +++ b/src/tooltips/ktooltipitem.h @@ -0,0 +1,84 @@ +/*************************************************************************** + * 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 QSize; +class QPainter; +class QRegion; + +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