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 | |
28 | namespace Plasma |
29 | { |
30 | |
31 | struct 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 | |
47 | const int MAXIMUM_TEXT_LENGTH = 5000; |
48 | |
49 | class ToolTipContentPrivate |
50 | { |
51 | public: |
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 : 1; |
68 | bool clickable : 1; |
69 | bool highlightWindows : 1; |
70 | }; |
71 | |
72 | ToolTipContent::ToolTipContent() |
73 | : d(new ToolTipContentPrivate) |
74 | { |
75 | } |
76 | |
77 | ToolTipContent::ToolTipContent(const ToolTipContent &other) |
78 | : d(new ToolTipContentPrivate(*other.d)) |
79 | { |
80 | } |
81 | |
82 | ToolTipContent::~ToolTipContent() |
83 | { |
84 | delete d; |
85 | } |
86 | |
87 | ToolTipContent &ToolTipContent::operator=(const ToolTipContent &other) |
88 | { |
89 | *d = *other.d; |
90 | return *this; |
91 | } |
92 | |
93 | ToolTipContent::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 | |
103 | ToolTipContent::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 | |
113 | bool ToolTipContent::isEmpty() const |
114 | { |
115 | return d->mainText.isEmpty() && |
116 | d->subText.isEmpty() && |
117 | d->image.isNull() && |
118 | (d->windowsToPreview.size() == 0); |
119 | } |
120 | |
121 | void ToolTipContent::setMainText(const QString &text) |
122 | { |
123 | d->mainText = text.trimmed(); |
124 | } |
125 | |
126 | QString ToolTipContent::mainText() const |
127 | { |
128 | QString text = d->mainText; |
129 | text.truncate(MAXIMUM_TEXT_LENGTH); |
130 | return text; |
131 | } |
132 | |
133 | void ToolTipContent::setSubText(const QString &text) |
134 | { |
135 | d->subText = text.trimmed(); |
136 | } |
137 | |
138 | QString ToolTipContent::subText() const |
139 | { |
140 | QString text = d->subText; |
141 | text.truncate(MAXIMUM_TEXT_LENGTH); |
142 | return text; |
143 | } |
144 | |
145 | void ToolTipContent::setImage(const QPixmap &image) |
146 | { |
147 | d->image = image; |
148 | } |
149 | |
150 | void ToolTipContent::setImage(const QIcon &icon) |
151 | { |
152 | d->image = icon.pixmap(IconSize(KIconLoader::Desktop)); |
153 | } |
154 | |
155 | QPixmap ToolTipContent::image() const |
156 | { |
157 | return d->image; |
158 | } |
159 | |
160 | void ToolTipContent::setWindowToPreview(WId id) |
161 | { |
162 | d->windowsToPreview.clear(); |
163 | d->windowsToPreview.append(id); |
164 | } |
165 | |
166 | WId ToolTipContent::windowToPreview() const |
167 | { |
168 | if (d->windowsToPreview.size() == 1) { |
169 | return d->windowsToPreview.first(); |
170 | } else { |
171 | return 0; |
172 | } |
173 | } |
174 | |
175 | void ToolTipContent::setWindowsToPreview(const QList<WId> & ids) |
176 | { |
177 | d->windowsToPreview = ids; |
178 | } |
179 | |
180 | QList<WId> ToolTipContent::windowsToPreview() const |
181 | { |
182 | return d->windowsToPreview; |
183 | } |
184 | |
185 | void ToolTipContent::setHighlightWindows(bool highlight) |
186 | { |
187 | d->highlightWindows = highlight; |
188 | } |
189 | |
190 | bool ToolTipContent::highlightWindows() const |
191 | { |
192 | return d->highlightWindows; |
193 | } |
194 | |
195 | void ToolTipContent::setAutohide(bool autohide) |
196 | { |
197 | d->autohide = autohide; |
198 | } |
199 | |
200 | bool ToolTipContent::autohide() const |
201 | { |
202 | return d->autohide; |
203 | } |
204 | |
205 | void ToolTipContent::(bool enabled) |
206 | { |
207 | d->instantPopup = enabled; |
208 | } |
209 | |
210 | bool ToolTipContent::() const |
211 | { |
212 | return d->instantPopup; |
213 | } |
214 | |
215 | void ToolTipContent::addResource(ResourceType type, const QUrl &path, const QVariant &resource) |
216 | { |
217 | d->resources.insert(path.toString(), ToolTipResource(type, resource)); |
218 | } |
219 | |
220 | void 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 | |
247 | void ToolTipContent::setClickable(bool clickable) |
248 | { |
249 | d->clickable = clickable; |
250 | } |
251 | |
252 | bool ToolTipContent::isClickable() const |
253 | { |
254 | return d->clickable; |
255 | } |
256 | |
257 | void ToolTipContent::setGraphicsWidget(QGraphicsWidget *widget) |
258 | { |
259 | d->graphicsWidget = widget; |
260 | } |
261 | |
262 | QGraphicsWidget *ToolTipContent::graphicsWidget() const |
263 | { |
264 | return d->graphicsWidget.data(); |
265 | } |
266 | |
267 | } // namespace Plasma |
268 | |
269 | |
270 | |