• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdelibs API Reference
  • KDE Home
  • Contact Us
 

Plasma

  • sources
  • kde-4.12
  • kdelibs
  • plasma
containmentactions.cpp
Go to the documentation of this file.
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::popupPosition(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"
Plasma::Vertical
The applet is constrained horizontally, but can expand vertically.
Definition: plasma.h:77
Plasma::ContainmentActions::pluginName
QString pluginName() const
Returns the plugin name for the containmentactions.
Plasma::ContainmentActions
The base ContainmentActions class.
Definition: containmentactions.h:55
Plasma::ContainmentActions::packageStructure
static PackageStructure::Ptr packageStructure()
Returns the Package specialization for containmentactions.
Definition: containmentactions.cpp:122
Plasma::ContainmentActions::createConfigurationInterface
virtual QWidget * createConfigurationInterface(QWidget *parent)
Returns the widget used in the configuration dialog.
Definition: containmentactions.cpp:187
Plasma::scenePosFromEvent
QPointF scenePosFromEvent(QEvent *event)
Definition: containmentactions.cpp:322
Plasma::ContainmentActions::contextEvent
virtual void contextEvent(QEvent *event)
Implement this to respond to events.
Definition: containmentactions.cpp:198
Plasma::ContainmentActions::listContainmentActionsInfo
static KPluginInfo::List listContainmentActionsInfo()
Returns a list of all known containmentactions plugins.
Definition: containmentactions.cpp:73
Plasma::Applet::formFactor
virtual FormFactor formFactor() const
Returns the current form factor the applet is being displayed in.
Definition: applet.cpp:1479
QWidget
Plasma::ContainmentActions::ContainmentActionsPackage
friend class ContainmentActionsPackage
Definition: containmentactions.h:263
containment.h
Plasma::ContainmentActions::contextualActions
virtual QList< QAction * > contextualActions()
Implement this to provide a list of actions that can be added to another menu for example...
Definition: containmentactions.cpp:203
Plasma::ContainmentActions::configurationRequired
bool configurationRequired() const
Definition: containmentactions.cpp:214
Plasma::ContainmentActions::~ContainmentActions
~ContainmentActions()
Definition: containmentactions.cpp:68
Plasma::ContainmentActions::containment
Containment * containment()
Definition: containmentactions.cpp:131
QObject
Plasma::ContainmentActions::load
static ContainmentActions * load(Containment *parent, const QString &name, const QVariantList &args=QVariantList())
Attempts to load a containmentactions.
Definition: containmentactions.cpp:81
Plasma::Horizontal
The applet is constrained vertically, but can expand horizontally.
Definition: plasma.h:75
Plasma::isNonSceneEvent
bool isNonSceneEvent(QEvent *event)
Definition: containmentactions.cpp:343
Plasma::isPluginVersionCompatible
bool isPluginVersionCompatible(unsigned int version)
Verifies that a plugin is compatible with plasma.
Definition: version.cpp:51
Plasma::Applet
The base Applet class.
Definition: applet.h:77
Plasma::ContainmentActions::event
bool event(QEvent *e)
Definition: containmentactions.cpp:383
Plasma::ContainmentActions::name
QString name() const
Returns the user-visible name for the containmentactions, as specified in the .desktop file...
Plasma::ContainmentActions::dataEngine
Q_INVOKABLE DataEngine * dataEngine(const QString &name) const
Loads the given DataEngine.
Definition: containmentactions.cpp:209
Plasma::ContainmentActions::configurationAccepted
virtual void configurationAccepted()
This method is called when the user's configuration changes are accepted.
Definition: containmentactions.cpp:193
Plasma::ContainmentActions::init
virtual void init(const KConfigGroup &config)
This method is called once the containmentactions is loaded or settings are changed.
Definition: containmentactions.cpp:177
Plasma::Applet::pluginName
QString pluginName
Definition: applet.h:82
Plasma::PackageStructure::Ptr
KSharedPtr< PackageStructure > Ptr
Definition: packagestructure.h:77
Plasma::DataEngine
Data provider for plasmoids (Plasma plugins)
Definition: dataengine.h:58
Plasma::ContainmentActions::save
virtual void save(KConfigGroup &config)
This method is called when settings need to be saved.
Definition: containmentactions.cpp:182
Plasma::ContainmentActions::isInitialized
bool isInitialized() const
Definition: containmentactions.cpp:166
Plasma::ContainmentActions::eventToString
static QString eventToString(QEvent *event)
Turns a mouse or wheel event into a string suitable for a ContainmentActions.
Definition: containmentactions.cpp:225
Plasma::toString
static QScriptValue toString(QScriptContext *ctx, QScriptEngine *eng)
Definition: easingcurve.cpp:57
Plasma::ContainmentActions::popupPosition
QPoint popupPosition(const QSize &s, QEvent *event)
Returns a popup position appropriate to the event and the size.
Definition: containmentactions.cpp:348
containmentactions.h
version.h
Plasma::ContainmentActions::restore
void restore(const KConfigGroup &config)
This method should be called once the plugin is loaded or settings are changed.
Definition: containmentactions.cpp:171
Plasma::ContainmentActions::paste
void paste(QPointF scenePos, QPoint screenPos)
pastes the clipboard at a given location
Definition: containmentactions.cpp:293
Plasma::Applet::popupPosition
QPoint popupPosition(const QSize &s) const
Reccomended position for a popup window like a menu or a tooltip given its size.
Definition: applet.cpp:737
Plasma::ContainmentActions::setContainment
void setContainment(Containment *newContainment)
newContainment the containment the plugin should be associated with.
Definition: containmentactions.cpp:389
Plasma::ContainmentActions::icon
QString icon() const
Returns the icon related to this containmentactions.
Plasma::ContainmentActions::ContainmentActions
ContainmentActions(QObject *parent=0)
Default constructor for an empty or null containmentactions.
Definition: containmentactions.cpp:47
Plasma::ContainmentActions::setConfigurationRequired
void setConfigurationRequired(bool needsConfiguring=true)
When the containmentactions needs to be configured before being usable, this method can be called to ...
Definition: containmentactions.cpp:219
Plasma::Containment
The base class for plugins that provide backgrounds and applet grouping containers.
Definition: containment.h:72
Plasma::screenPosFromEvent
QPoint screenPosFromEvent(QEvent *event)
Definition: containmentactions.cpp:301
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 22:48:33 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

Plasma

Skip menu "Plasma"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Related Pages

kdelibs API Reference

Skip menu "kdelibs API Reference"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDEWebKit
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • kio
  • KIOSlave
  • KJS
  •   KJS-API
  • kjsembed
  •   WTF
  • KNewStuff
  • KParts
  • KPty
  • Kross
  • KUnitConversion
  • KUtils
  • Nepomuk
  • Nepomuk-Core
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal