The file ktooltip.h contained the three public classes KToolTip, KToolTipDelegate and KToolTipItem. Use one separate file for each class.

svn path=/trunk/KDE/kdebase/apps/; revision=984646
This commit is contained in:
Peter Penz 2009-06-21 10:20:28 +00:00
parent 1c351eac8f
commit 1e208254c5
10 changed files with 420 additions and 324 deletions

View file

@ -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

View file

@ -19,7 +19,10 @@
*******************************************************************************/
#include "kformattedballoontipdelegate.h"
#include "ktooltipitem.h"
#include "ktooltip.h"
#include <QBitmap>
#include <QIcon>
#include <QLinearGradient>
#include <QTextDocument>
#include <kcolorscheme.h>

View file

@ -21,7 +21,7 @@
#ifndef KFORMATTEDBALLOONTIPDELEGATE_H
#define KFORMATTEDBALLOONTIPDELEGATE_H
#include <tooltips/ktooltip.h>
#include <tooltips/ktooltipdelegate.h>
#include <QPainter>
class KFormattedBalloonTipDelegate : public KToolTipDelegate

View file

@ -19,16 +19,12 @@
#include "ktooltip.h"
#include "ktooltip_p.h"
#include "ktooltipdelegate.h"
#include <QApplication>
#include <QMap>
#include <QPixmap>
#include <QPainter>
#include <QVariant>
#include <QIcon>
#include <QWidget>
#include <QToolTip>
#include <QDebug>
#ifdef Q_WS_X11
# include <QX11Info>
@ -42,173 +38,9 @@ const int ShapeInput = 2;
#endif
class KToolTipItemPrivate
{
public:
QMap<int, QVariant> 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<QIcon>(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)

View file

@ -20,156 +20,11 @@
#ifndef KTOOLTIP_H
#define KTOOLTIP_H
#include <QObject>
#include <QPalette>
#include <QFont>
#include <QRect>
#include <tooltips/ktooltipitem.h>
#include <QStyle>
#include <QFontMetrics>
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

View file

@ -20,6 +20,8 @@
#ifndef KTOOLTIP_P_H
#define KTOOLTIP_P_H
#include <QPoint>
class KTipLabel;
class KStyleOptionToolTip;
class KToolTipDelegate;

View file

@ -0,0 +1,138 @@
/***************************************************************************
* Copyright (C) 2008 by Fredrik Höglund <fredrik@kde.org> *
* *
* 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 <QApplication>
#include <QPainter>
#include <QIcon>
#ifdef Q_WS_X11
# include <QX11Info>
# include <X11/Xlib.h>
# include <X11/extensions/shape.h>
#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
}

View file

@ -0,0 +1,110 @@
/***************************************************************************
* Copyright (C) 2008 by Fredrik Höglund <fredrik@kde.org> *
* *
* 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 <QFontMetrics>
#include <QFont>
#include <QObject>
#include <QPalette>
#include <QRect>
#include <QStyle>
#include <QSize>
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

View file

@ -0,0 +1,76 @@
/***************************************************************************
* Copyright (C) 2008 by Fredrik Höglund <fredrik@kde.org> *
* *
* 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 <QIcon>
class KToolTipItemPrivate
{
public:
QMap<int, QVariant> 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<QIcon>(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();
}

View file

@ -0,0 +1,84 @@
/***************************************************************************
* Copyright (C) 2008 by Fredrik Höglund <fredrik@kde.org> *
* *
* 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 <QVariant>
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