1/*
2 * Copyright 2008 by Aaron Seigo <aseigo@kde.org>
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
8 *
9 * This library 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 GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor,
17 * Boston, MA 02110-1301 USA
18 */
19
20#include "tooltipcontent.h"
21
22#include <QGraphicsWidget>
23#include <QHash>
24#include <QTextDocument>
25
26#include <kiconloader.h>
27
28namespace Plasma
29{
30
31struct ToolTipResource
32{
33 ToolTipResource()
34 {
35 }
36
37 ToolTipResource(ToolTipContent::ResourceType t, const QVariant &v)
38 : type(t),
39 data(v)
40 {
41 }
42
43 ToolTipContent::ResourceType type;
44 QVariant data;
45};
46
47const int MAXIMUM_TEXT_LENGTH = 5000;
48
49class ToolTipContentPrivate
50{
51public:
52 ToolTipContentPrivate()
53 : autohide(true),
54 instantPopup(false),
55 clickable(false),
56 highlightWindows(false)
57 {
58 }
59
60 QString mainText;
61 QString subText;
62 QPixmap image;
63 QList<WId> windowsToPreview;
64 QHash<QString, ToolTipResource> resources;
65 QWeakPointer<QGraphicsWidget> graphicsWidget;
66 bool autohide : 1;
67 bool instantPopup : 1;
68 bool clickable : 1;
69 bool highlightWindows : 1;
70};
71
72ToolTipContent::ToolTipContent()
73 : d(new ToolTipContentPrivate)
74{
75}
76
77ToolTipContent::ToolTipContent(const ToolTipContent &other)
78 : d(new ToolTipContentPrivate(*other.d))
79{
80}
81
82ToolTipContent::~ToolTipContent()
83{
84 delete d;
85}
86
87ToolTipContent &ToolTipContent::operator=(const ToolTipContent &other)
88{
89 *d = *other.d;
90 return *this;
91}
92
93ToolTipContent::ToolTipContent(const QString &mainText,
94 const QString &subText,
95 const QPixmap &image)
96 : d(new ToolTipContentPrivate)
97{
98 setMainText(mainText);
99 setSubText(subText);
100 setImage(image);
101}
102
103ToolTipContent::ToolTipContent(const QString &mainText,
104 const QString &subText,
105 const QIcon &icon)
106 : d(new ToolTipContentPrivate)
107{
108 setMainText(mainText);
109 setSubText(subText);
110 setImage(icon);
111}
112
113bool ToolTipContent::isEmpty() const
114{
115 return d->mainText.isEmpty() &&
116 d->subText.isEmpty() &&
117 d->image.isNull() &&
118 (d->windowsToPreview.size() == 0);
119}
120
121void ToolTipContent::setMainText(const QString &text)
122{
123 d->mainText = text.trimmed();
124}
125
126QString ToolTipContent::mainText() const
127{
128 QString text = d->mainText;
129 text.truncate(MAXIMUM_TEXT_LENGTH);
130 return text;
131}
132
133void ToolTipContent::setSubText(const QString &text)
134{
135 d->subText = text.trimmed();
136}
137
138QString ToolTipContent::subText() const
139{
140 QString text = d->subText;
141 text.truncate(MAXIMUM_TEXT_LENGTH);
142 return text;
143}
144
145void ToolTipContent::setImage(const QPixmap &image)
146{
147 d->image = image;
148}
149
150void ToolTipContent::setImage(const QIcon &icon)
151{
152 d->image = icon.pixmap(IconSize(KIconLoader::Desktop));
153}
154
155QPixmap ToolTipContent::image() const
156{
157 return d->image;
158}
159
160void ToolTipContent::setWindowToPreview(WId id)
161{
162 d->windowsToPreview.clear();
163 d->windowsToPreview.append(id);
164}
165
166WId ToolTipContent::windowToPreview() const
167{
168 if (d->windowsToPreview.size() == 1) {
169 return d->windowsToPreview.first();
170 } else {
171 return 0;
172 }
173}
174
175void ToolTipContent::setWindowsToPreview(const QList<WId> & ids)
176{
177 d->windowsToPreview = ids;
178}
179
180QList<WId> ToolTipContent::windowsToPreview() const
181{
182 return d->windowsToPreview;
183}
184
185void ToolTipContent::setHighlightWindows(bool highlight)
186{
187 d->highlightWindows = highlight;
188}
189
190bool ToolTipContent::highlightWindows() const
191{
192 return d->highlightWindows;
193}
194
195void ToolTipContent::setAutohide(bool autohide)
196{
197 d->autohide = autohide;
198}
199
200bool ToolTipContent::autohide() const
201{
202 return d->autohide;
203}
204
205void ToolTipContent::setInstantPopup(bool enabled)
206{
207 d->instantPopup = enabled;
208}
209
210bool ToolTipContent::isInstantPopup() const
211{
212 return d->instantPopup;
213}
214
215void ToolTipContent::addResource(ResourceType type, const QUrl &path, const QVariant &resource)
216{
217 d->resources.insert(path.toString(), ToolTipResource(type, resource));
218}
219
220void ToolTipContent::registerResources(QTextDocument *document) const
221{
222 if (!document) {
223 return;
224 }
225
226 QHashIterator<QString, ToolTipResource> it(d->resources);
227 while (it.hasNext()) {
228 it.next();
229 const ToolTipResource &r = it.value();
230 QTextDocument::ResourceType t = QTextDocument::ImageResource;
231
232 switch (r.type) {
233 case ImageResource:
234 break;
235 case HtmlResource:
236 t = QTextDocument::HtmlResource;
237 break;
238 case CssResource:
239 t = QTextDocument::StyleSheetResource;
240 break;
241 }
242
243 document->addResource(t, it.key(), r.data);
244 }
245}
246
247void ToolTipContent::setClickable(bool clickable)
248{
249 d->clickable = clickable;
250}
251
252bool ToolTipContent::isClickable() const
253{
254 return d->clickable;
255}
256
257void ToolTipContent::setGraphicsWidget(QGraphicsWidget *widget)
258{
259 d->graphicsWidget = widget;
260}
261
262QGraphicsWidget *ToolTipContent::graphicsWidget() const
263{
264 return d->graphicsWidget.data();
265}
266
267} // namespace Plasma
268
269
270