1 /***************************************************************************
2 * Copyright (C) 2008 by Fredrik Höglund <fredrik@kde.org> *
4 * This program is free software; you can redistribute it and/or modify *
5 * it under the terms of the GNU General Public License as published by *
6 * the Free Software Foundation; either version 2 of the License, or *
7 * (at your option) any later version. *
9 * This program is distributed in the hope that it will be useful, *
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
12 * GNU General Public License for more details. *
14 * You should have received a copy of the GNU General Public License *
15 * along with this program; if not, write to the *
16 * Free Software Foundation, Inc., *
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA *
18 ***************************************************************************/
28 #include <QFontMetrics>
36 class KToolTipItemPrivate
;
39 * KToolTipItem contains the data to be displayed in a tooltip.
41 * Custom data can be stored as QVariants in the object by calling
42 * setData() with a custom item role, and retrieved and displayed
43 * by a tooltip delegate by calling data().
45 * The default tooltip delegate uses Qt::DecorationRole and
48 * To display the tooltip, call KToolTip::showTip() with a pointer
49 * to the KToolTipItem.
51 * You can reimplement the setData() and/or data() methods in this
52 * class to implement on-demand loading of data.
57 enum ItemType
{ DefaultType
, UserType
= 1000 };
60 * Creates a KToolTipItem with @p text and no icon.
62 explicit KToolTipItem(const QString
&text
, int type
= DefaultType
);
65 * Creates a KToolTipItem with an @p icon and @p text.
67 KToolTipItem(const QIcon
&icon
, const QString
&text
, int type
= DefaultType
);
70 * Destroys the KToolTipItem.
72 virtual ~KToolTipItem();
75 * Returns the item type.
82 virtual QVariant
data(int role
) const;
83 virtual void setData(int role
, const QVariant
&data
);
86 KToolTipItemPrivate
* const d
;
90 class KStyleOptionToolTip
93 KStyleOptionToolTip();
94 enum Corner
{ TopLeftCorner
, TopRightCorner
, BottomLeftCorner
, BottomRightCorner
, NoCorner
};
96 Qt::LayoutDirection direction
;
97 QFontMetrics fontMetrics
;
102 QSize decorationSize
;
107 * KToolTipDelegate is responsible for providing the size hint and
108 * painting the tooltips.
110 class KToolTipDelegate
: public QObject
115 virtual ~KToolTipDelegate();
117 virtual QSize
sizeHint(const KStyleOptionToolTip
*option
, const KToolTipItem
*item
) const;
120 * If haveAlphaChannel() returns true, the paint device will be filled with
121 * Qt::transparent when this function is called, otherwise the content is
124 virtual void paint(QPainter
*painter
, const KStyleOptionToolTip
*option
,
125 const KToolTipItem
*item
) const;
128 * Reimplement this function to specify the region of the tooltip
129 * that accepts input. Any mouse events that occur outside this
130 * region will be sent to the widget below the tooltip.
132 * The default implementation returns a region containing the
133 * bounding rect of the tooltip.
135 * This function will only be called if haveAlphaChannel()
138 virtual QRegion
inputShape(const KStyleOptionToolTip
*option
) const;
141 * Reimplement this function to specify a shape mask for the tooltip.
143 * The default implementation returns a region containing the
144 * bounding rect of the tooltip.
146 * This function will only be called if haveAlphaChannel()
149 virtual QRegion
shapeMask(const KStyleOptionToolTip
*option
) const;
153 * Returns true if the tooltip has an alpha channel, and false
156 * Implementors should assume that this condition may change at
157 * any time during the runtime of the application, as compositing
158 * can be enabled or disabled in the window manager.
160 bool haveAlphaChannel() const;
165 * Schedules a repaint of the tooltip item.
166 * This slot can be connected to a timer to animate the tooltip.
168 void update(const KToolTipItem
*item
);
174 * KToolTip provides customizable tooltips that can have animations as well as an alpha
175 * channel, allowing for dynamic transparency effects.
177 * ARGB tooltips work on X11 even when the application isn't using the ARGB visual.
181 void showText(const QPoint
&pos
, const QString
&text
, QWidget
*widget
, const QRect
&rect
);
182 void showText(const QPoint
&pos
, const QString
&text
, QWidget
*widget
= 0);
185 * Shows the tip @p item at the global position indicated by @p pos.
187 * Ownership of the item is transferred to KToolTip. The item will be deleted
188 * automatically when it is hidden.
190 * The tip is shown immediately when this function is called.
192 void showTip(const QPoint
&pos
, KToolTipItem
*item
);
195 void setToolTipDelegate(KToolTipDelegate
*delegate
);