1 | /* |
2 | * Copyright (c) 2009 Chani Armitage <chani@kde.org> |
3 | * |
4 | * This program is free software; you can redistribute it and/or modify |
5 | * it under the terms of the GNU Library General Public License as |
6 | * published by the Free Software Foundation; either version 2, or |
7 | * (at your option) any later version. |
8 | * |
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 |
13 | * |
14 | * You should have received a copy of the GNU Library General Public |
15 | * License 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 | */ |
19 | |
20 | #include "containmentactions.h" |
21 | #include "containment.h" |
22 | |
23 | #include "private/dataengineconsumer_p.h" |
24 | #include "private/packages_p.h" |
25 | #include "private/containmentactions_p.h" |
26 | #include "private/containment_p.h" |
27 | |
28 | #include <QMetaEnum> |
29 | #include <QMouseEvent> |
30 | #include <QWheelEvent> |
31 | #include <QGraphicsSceneContextMenuEvent> |
32 | #include <QGraphicsSceneMouseEvent> |
33 | #include <QGraphicsSceneWheelEvent> |
34 | |
35 | #include <kdebug.h> |
36 | #include <kglobal.h> |
37 | #include <kservicetypetrader.h> |
38 | #include <kstandarddirs.h> |
39 | |
40 | #include <version.h> |
41 | |
42 | namespace Plasma |
43 | { |
44 | |
45 | PackageStructure::Ptr ContainmentActionsPrivate::s_packageStructure(0); |
46 | |
47 | ContainmentActions::ContainmentActions(QObject * parentObject) |
48 | : d(new ContainmentActionsPrivate(KService::serviceByStorageId(QString()), this)) |
49 | { |
50 | setParent(parentObject); |
51 | } |
52 | |
53 | ContainmentActions::ContainmentActions(QObject *parentObject, const QVariantList &args) |
54 | : d(new ContainmentActionsPrivate(KService::serviceByStorageId(args.count() > 0 ? |
55 | args[0].toString() : QString()), this)) |
56 | { |
57 | // now remove first item since those are managed by Wallpaper and subclasses shouldn't |
58 | // need to worry about them. yes, it violates the constness of this var, but it lets us add |
59 | // or remove items later while applets can just pretend that their args always start at 0 |
60 | QVariantList &mutableArgs = const_cast<QVariantList &>(args); |
61 | if (!mutableArgs.isEmpty()) { |
62 | mutableArgs.removeFirst(); |
63 | } |
64 | |
65 | setParent(parentObject); |
66 | } |
67 | |
68 | ContainmentActions::~ContainmentActions() |
69 | { |
70 | delete d; |
71 | } |
72 | |
73 | KPluginInfo::List ContainmentActions::listContainmentActionsInfo() |
74 | { |
75 | QString constraint; |
76 | |
77 | KService::List offers = KServiceTypeTrader::self()->query("Plasma/ContainmentActions" , constraint); |
78 | return KPluginInfo::fromServices(offers); |
79 | } |
80 | |
81 | ContainmentActions *ContainmentActions::load(Containment *parent, const QString &containmentActionsName, const QVariantList &args) |
82 | { |
83 | if (containmentActionsName.isEmpty()) { |
84 | return 0; |
85 | } |
86 | |
87 | QString constraint = QString("[X-KDE-PluginInfo-Name] == '%1'" ).arg(containmentActionsName); |
88 | KService::List offers = KServiceTypeTrader::self()->query("Plasma/ContainmentActions" , constraint); |
89 | |
90 | if (offers.isEmpty()) { |
91 | kDebug() << "offers is empty for " << containmentActionsName; |
92 | return 0; |
93 | } |
94 | |
95 | KService::Ptr offer = offers.first(); |
96 | KPluginLoader plugin(*offer); |
97 | |
98 | if (!Plasma::isPluginVersionCompatible(plugin.pluginVersion())) { |
99 | return 0; |
100 | } |
101 | |
102 | QVariantList allArgs; |
103 | allArgs << offer->storageId() << args; |
104 | QString error; |
105 | ContainmentActions *containmentActions = offer->createInstance<Plasma::ContainmentActions>(parent, allArgs, &error); |
106 | |
107 | if (!containmentActions) { |
108 | kDebug() << "Couldn't load containmentActions \"" << containmentActionsName << "\"! reason given: " << error; |
109 | } |
110 | |
111 | return containmentActions; |
112 | } |
113 | |
114 | ContainmentActions *ContainmentActions::load(Containment *parent, const KPluginInfo &info, const QVariantList &args) |
115 | { |
116 | if (!info.isValid()) { |
117 | return 0; |
118 | } |
119 | return load(parent, info.pluginName(), args); |
120 | } |
121 | |
122 | PackageStructure::Ptr ContainmentActions::packageStructure() |
123 | { |
124 | if (!ContainmentActionsPrivate::s_packageStructure) { |
125 | ContainmentActionsPrivate::s_packageStructure = new ContainmentActionsPackage(); |
126 | } |
127 | |
128 | return ContainmentActionsPrivate::s_packageStructure; |
129 | } |
130 | |
131 | Containment *ContainmentActions::containment() |
132 | { |
133 | if (d->containment) { |
134 | return d->containment; |
135 | } |
136 | return qobject_cast<Containment*>(parent()); |
137 | } |
138 | |
139 | QString ContainmentActions::name() const |
140 | { |
141 | if (!d->containmentActionsDescription.isValid()) { |
142 | return i18n("Unknown ContainmentActions" ); |
143 | } |
144 | |
145 | return d->containmentActionsDescription.name(); |
146 | } |
147 | |
148 | QString ContainmentActions::icon() const |
149 | { |
150 | if (!d->containmentActionsDescription.isValid()) { |
151 | return QString(); |
152 | } |
153 | |
154 | return d->containmentActionsDescription.icon(); |
155 | } |
156 | |
157 | QString ContainmentActions::pluginName() const |
158 | { |
159 | if (!d->containmentActionsDescription.isValid()) { |
160 | return QString(); |
161 | } |
162 | |
163 | return d->containmentActionsDescription.pluginName(); |
164 | } |
165 | |
166 | bool ContainmentActions::isInitialized() const |
167 | { |
168 | return d->initialized; |
169 | } |
170 | |
171 | void ContainmentActions::restore(const KConfigGroup &config) |
172 | { |
173 | init(config); |
174 | d->initialized = true; |
175 | } |
176 | |
177 | void ContainmentActions::init(const KConfigGroup &config) |
178 | { |
179 | Q_UNUSED(config); |
180 | } |
181 | |
182 | void ContainmentActions::save(KConfigGroup &config) |
183 | { |
184 | Q_UNUSED(config); |
185 | } |
186 | |
187 | QWidget *ContainmentActions::createConfigurationInterface(QWidget *parent) |
188 | { |
189 | Q_UNUSED(parent); |
190 | return 0; |
191 | } |
192 | |
193 | void ContainmentActions::configurationAccepted() |
194 | { |
195 | //do nothing by default |
196 | } |
197 | |
198 | void ContainmentActions::contextEvent(QEvent *event) |
199 | { |
200 | Q_UNUSED(event) |
201 | } |
202 | |
203 | QList<QAction*> ContainmentActions::contextualActions() |
204 | { |
205 | //empty list |
206 | return QList<QAction*>(); |
207 | } |
208 | |
209 | DataEngine *ContainmentActions::dataEngine(const QString &name) const |
210 | { |
211 | return d->dataEngine(name); |
212 | } |
213 | |
214 | bool ContainmentActions::configurationRequired() const |
215 | { |
216 | return d->needsConfig; |
217 | } |
218 | |
219 | void ContainmentActions::setConfigurationRequired(bool needsConfig) |
220 | { |
221 | //TODO: reason? |
222 | d->needsConfig = needsConfig; |
223 | } |
224 | |
225 | QString ContainmentActions::eventToString(QEvent *event) |
226 | { |
227 | QString trigger; |
228 | Qt::KeyboardModifiers modifiers; |
229 | |
230 | switch (event->type()) { |
231 | case QEvent::MouseButtonPress: |
232 | case QEvent::MouseButtonRelease: |
233 | { |
234 | QMouseEvent *e = static_cast<QMouseEvent*>(event); |
235 | int m = QObject::staticQtMetaObject.indexOfEnumerator("MouseButtons" ); |
236 | QMetaEnum mouse = QObject::staticQtMetaObject.enumerator(m); |
237 | trigger += mouse.valueToKey(e->button()); |
238 | modifiers = e->modifiers(); |
239 | break; |
240 | } |
241 | case QEvent::GraphicsSceneMousePress: |
242 | case QEvent::GraphicsSceneMouseRelease: |
243 | case QEvent::GraphicsSceneMouseDoubleClick: |
244 | { |
245 | QGraphicsSceneMouseEvent *e = static_cast<QGraphicsSceneMouseEvent*>(event); |
246 | int m = QObject::staticQtMetaObject.indexOfEnumerator("MouseButtons" ); |
247 | QMetaEnum mouse = QObject::staticQtMetaObject.enumerator(m); |
248 | trigger += mouse.valueToKey(e->button()); |
249 | modifiers = e->modifiers(); |
250 | break; |
251 | } |
252 | case QEvent::Wheel: |
253 | { |
254 | QWheelEvent *e = static_cast<QWheelEvent*>(event); |
255 | int o = QObject::staticQtMetaObject.indexOfEnumerator("Orientations" ); |
256 | QMetaEnum orient = QObject::staticQtMetaObject.enumerator(o); |
257 | trigger = "wheel:" ; |
258 | trigger += orient.valueToKey(e->orientation()); |
259 | modifiers = e->modifiers(); |
260 | break; |
261 | } |
262 | case QEvent::GraphicsSceneWheel: |
263 | { |
264 | QGraphicsSceneWheelEvent *e = static_cast<QGraphicsSceneWheelEvent*>(event); |
265 | int o = QObject::staticQtMetaObject.indexOfEnumerator("Orientations" ); |
266 | QMetaEnum orient = QObject::staticQtMetaObject.enumerator(o); |
267 | trigger = "wheel:" ; |
268 | trigger += orient.valueToKey(e->orientation()); |
269 | modifiers = e->modifiers(); |
270 | break; |
271 | } |
272 | case QEvent::GraphicsSceneContextMenu: |
273 | case QEvent::ContextMenu: |
274 | { |
275 | int m = QObject::staticQtMetaObject.indexOfEnumerator("MouseButtons" ); |
276 | QMetaEnum mouse = QObject::staticQtMetaObject.enumerator(m); |
277 | trigger = mouse.valueToKey(Qt::RightButton); |
278 | modifiers = Qt::NoModifier; |
279 | break; |
280 | } |
281 | default: |
282 | return QString(); |
283 | } |
284 | |
285 | int k = QObject::staticQtMetaObject.indexOfEnumerator("KeyboardModifiers" ); |
286 | QMetaEnum kbd = QObject::staticQtMetaObject.enumerator(k); |
287 | trigger += ';'; |
288 | trigger += kbd.valueToKeys(modifiers); |
289 | |
290 | return trigger; |
291 | } |
292 | |
293 | void ContainmentActions::paste(QPointF scenePos, QPoint screenPos) |
294 | { |
295 | Containment *c = containment(); |
296 | if (c) { |
297 | c->d->dropData(scenePos, screenPos); |
298 | } |
299 | } |
300 | |
301 | QPoint screenPosFromEvent(QEvent *event) |
302 | { |
303 | switch (event->type()) { |
304 | case QEvent::GraphicsSceneMousePress: |
305 | case QEvent::GraphicsSceneMouseRelease: |
306 | case QEvent::GraphicsSceneMouseDoubleClick: |
307 | return static_cast<QGraphicsSceneMouseEvent*>(event)->screenPos(); |
308 | break; |
309 | case QEvent::GraphicsSceneWheel: |
310 | return static_cast<QGraphicsSceneWheelEvent*>(event)->screenPos(); |
311 | break; |
312 | case QEvent::GraphicsSceneContextMenu: |
313 | return static_cast<QGraphicsSceneContextMenuEvent*>(event)->screenPos(); |
314 | break; |
315 | default: |
316 | break; |
317 | } |
318 | |
319 | return QPoint(); |
320 | } |
321 | |
322 | QPointF scenePosFromEvent(QEvent *event) |
323 | { |
324 | switch (event->type()) { |
325 | case QEvent::GraphicsSceneMousePress: |
326 | case QEvent::GraphicsSceneMouseRelease: |
327 | case QEvent::GraphicsSceneMouseDoubleClick: |
328 | return static_cast<QGraphicsSceneMouseEvent*>(event)->scenePos(); |
329 | break; |
330 | case QEvent::GraphicsSceneWheel: |
331 | return static_cast<QGraphicsSceneWheelEvent*>(event)->scenePos(); |
332 | break; |
333 | case QEvent::GraphicsSceneContextMenu: |
334 | return static_cast<QGraphicsSceneContextMenuEvent*>(event)->scenePos(); |
335 | break; |
336 | default: |
337 | break; |
338 | } |
339 | |
340 | return QPoint(); |
341 | } |
342 | |
343 | bool isNonSceneEvent(QEvent *event) |
344 | { |
345 | return dynamic_cast<QGraphicsSceneEvent *>(event) == 0; |
346 | } |
347 | |
348 | QPoint ContainmentActions::(const QSize &s, QEvent *event) |
349 | { |
350 | if (isNonSceneEvent(event)) { |
351 | return screenPosFromEvent(event); |
352 | } |
353 | |
354 | Containment *c = containment(); |
355 | if (!c) { |
356 | return screenPosFromEvent(event); |
357 | } |
358 | |
359 | Applet *applet = c->d->appletAt(scenePosFromEvent(event)); |
360 | QPoint screenPos = screenPosFromEvent(event); |
361 | QPoint pos = screenPos; |
362 | if (applet && containment()->d->isPanelContainment()) { |
363 | pos = applet->popupPosition(s); |
364 | if (event->type() != QEvent::GraphicsSceneContextMenu || |
365 | static_cast<QGraphicsSceneContextMenuEvent *>(event)->reason() == QGraphicsSceneContextMenuEvent::Mouse) { |
366 | // if the menu pops up way away from the mouse press, then move it |
367 | // to the mouse press |
368 | if (c->formFactor() == Vertical) { |
369 | if (pos.y() + s.height() < screenPos.y()) { |
370 | pos.setY(screenPos.y()); |
371 | } |
372 | } else if (c->formFactor() == Horizontal) { |
373 | if (pos.x() + s.width() < screenPos.x()) { |
374 | pos.setX(screenPos.x()); |
375 | } |
376 | } |
377 | } |
378 | } |
379 | |
380 | return pos; |
381 | } |
382 | |
383 | bool ContainmentActions::event(QEvent *) |
384 | { |
385 | //no longer needed |
386 | return false; |
387 | } |
388 | |
389 | void ContainmentActions::setContainment(Containment *newContainment) { |
390 | d->containment = newContainment; |
391 | } |
392 | |
393 | } // Plasma namespace |
394 | |
395 | #include "containmentactions.moc" |
396 | |