Make a branch to make krunner Good Enough For Aaron™.
[kdebase/uwolfer.git] / workspace / libs / plasma / applet.h
blobc304b2b560e805581123e5523bd5f27319df5764
1 /*
2 * Copyright 2006-2007 by Aaron Seigo <aseigo@kde.org>
3 * Copyright 2007 by Riccardo Iaconelli <riccardo@kde.org>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU Library General Public License as
7 * published by the Free Software Foundation; either version 2, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details
15 * You should have received a copy of the GNU Library General Public
16 * License along with this program; if not, write to the
17 * Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21 #ifndef PLASMA_APPLET_H
22 #define PLASMA_APPLET_H
24 #include <QtGui/QGraphicsItem>
25 #include <QtGui/QWidget>
27 #include <KPluginInfo>
28 #include <KSharedConfig>
29 #include <KGenericFactory>
31 #include <plasma/configxml.h>
32 #include <plasma/packagestructure.h>
33 #include <plasma/plasma.h>
34 #include <plasma/widgets/widget.h>
36 namespace Plasma
39 class Containment;
40 class DataEngine;
41 class Package;
43 /**
44 * @short The base Applet class
46 * Applet provides several important roles for add-ons widgets in Plasma.
48 * First, it is the base class for the plugin system and therefore is the
49 * interface to applets for host applications. It also handles the life time
50 * management of data engines (e.g. all data engines accessed via
51 * Applet::dataEngine(const QString&) are properly deref'd on Applet
52 * destruction), background painting (allowing for consistent and complex
53 * look and feel in just one line of code for applets), loading and starting
54 * of scripting support for each applet, providing access to the associated
55 * plasmoid package (if any) and access to configuration data.
57 * See techbase.kde.org for tutorial on writing Applets using this class.
59 class PLASMA_EXPORT Applet : public Widget
61 Q_OBJECT
62 // Q_PROPERTY( QRectF maxSizeHint READ maxSizeHint )
63 Q_PROPERTY(bool hasConfigurationInterface READ hasConfigurationInterface WRITE setHasConfigurationInterface)
64 Q_PROPERTY(QString name READ name)
65 Q_PROPERTY(QString category READ category)
66 Q_PROPERTY(bool immutable READ isImmutable WRITE setImmutable)
67 Q_PROPERTY(bool drawStandardBackground READ drawStandardBackground WRITE setDrawStandardBackground)
68 Q_PROPERTY(bool failedToLaunch READ failedToLaunch WRITE setFailedToLaunch)
69 Q_PROPERTY(bool needsConfiguring READ needsConfiguring WRITE setNeedsConfiguring)
70 Q_PROPERTY(QRectF boundingRect READ boundingRect)
71 Q_PROPERTY(QRectF geometry READ geometry WRITE setGeometry)
72 Q_PROPERTY(bool shouldConserveResources READ shouldConserveResources)
73 Q_PROPERTY(Qt::AspectRatioMode aspectRatioMode READ aspectRatioMode WRITE setAspectRatioMode)
75 public:
76 typedef QList<Applet*> List;
77 typedef QHash<QString, Applet*> Dict;
79 /**
80 * @arg parent the QGraphicsItem this applet is parented to
81 * @arg servideId the name of the .desktop file containing the
82 * information about the widget
83 * @arg appletId a unique id used to differentiate between multiple
84 * instances of the same Applet type
86 explicit Applet(QGraphicsItem* parent = 0,
87 const QString& serviceId = QString(),
88 uint appletId = 0);
90 /**
91 * This constructor is to be used with the plugin loading systems
92 * found in KPluginInfo and KService. The argument list is expected
93 * to have two elements: the KService service ID for the desktop entry
94 * and an applet ID which must be a base 10 number.
96 * @arg parent a QObject parent; you probably want to pass in 0
97 * @arg args a list of strings containing two entries: the service id
98 * and the applet id
100 Applet(QObject* parent, const QVariantList& args);
102 ~Applet();
105 * @return a package structure representing a Theme
107 static PackageStructure::Ptr packageStructure();
110 * This method is called once the applet is loaded and added to a Corona.
111 * If the applet requires a QGraphicsScene or has an particularly intensive
112 * set of initialization routines to go through, consider implementing it
113 * in this method instead of the constructor.
115 virtual void init();
118 * @return the id of this applet
120 uint id() const;
123 * Returns the KConfigGroup to access the applets configuration.
125 * This config object will write to an instance
126 * specific config file named \<appletname\>\<instanceid\>rc
127 * in the Plasma appdata directory.
129 KConfigGroup config() const;
132 * Returns a config group with the name provided. This ensures
133 * that the group name is properly namespaced to avoid collision
134 * with other applets that may be sharing this config file
136 * @param group the name of the group to access
138 KConfigGroup config(const QString &group) const;
141 * Saves state information about this applet.
143 void save(KConfigGroup* group) const;
146 * Restores state information about this applet.
148 void restore(KConfigGroup* group);
151 * Returns a KConfigGroup object to be shared by all applets of this
152 * type.
154 * This config object will write to an applet-specific config object
155 * named plasma_\<appletname\>rc in the local config directory.
157 KConfigGroup globalConfig() const;
160 * Returns the config skeleton object from this applet's package,
161 * if any.
163 * @return config skeleton object, or 0 if none
165 ConfigXml* configXml() const;
168 * Loads the given DataEngine
170 * Tries to load the data engine given by @p name. Each engine is
171 * only loaded once, and that instance is re-used on all subsequent
172 * requests.
174 * If the data engine was not found, an invalid data engine is returned
175 * (see DataEngine::isValid()).
177 * Note that you should <em>not</em> delete the returned engine.
179 * @param name Name of the data engine to load
180 * @return pointer to the data engine if it was loaded,
181 * or an invalid data engine if the requested engine
182 * could not be loaded
184 Q_INVOKABLE DataEngine* dataEngine(const QString& name) const;
187 * Accessor for the associated Package object if any.
188 * Generally, only Plasmoids come in a Package.
190 * @return the Package object, or 0 if none
192 const Package* package() const;
195 * Called when any of the geometry constraints have been updated.
196 * This method calls constraintsUpdated, which may be reimplemented,
197 * once the Applet has been prepared for updating the constraints.
199 * @param constraints the type of constraints that were updated
201 void updateConstraints(Plasma::Constraints constraints = Plasma::AllConstraints);
204 * Called when any of the geometry constraints have been updated.
206 * This is always called prior to painting and should be used as an
207 * opportunity to layout the widget, calculate sizings, etc.
209 * Do not call update() from this method; an update() will be triggered
210 * at the appropriate time for the applet.
212 * @param constraints the type of constraints that were updated
213 * @property constraint
215 virtual void constraintsUpdated(Plasma::Constraints constraints);
218 * Returns the current form factor the applet is being displayed in.
220 * @see Plasma::FormFactor
222 virtual FormFactor formFactor() const;
225 * Returns the location of the scene which is displaying applet.
227 * @see Plasma::Location
229 virtual Location location() const;
232 * Returns the rect that the contents are positioned within in local coordinates
234 QRectF contentRect() const;
237 * Returns the area within which contents can be painted.
239 QSizeF contentSize() const;
242 * Sets the content size.
244 * @note Normally an applet should never
245 * call this directly except in the constructor to provide a default
246 * size
248 * @arg size the new size of the contents area
250 void setContentSize(const QSizeF &size);
253 * Sets the content size.
255 * @note Normally an applet should never
256 * call this directly except in the constructor to provide a default
257 * size
259 * @arg width the new width of the contents area
260 * @arg height the new height of the contents area
262 void setContentSize(int width, int height);
265 * Returns an ideal size for the applet's content.
266 * Applets can re-implement this to provide a suitable size based
267 * on their contents.
269 * Unlike sizeHint() , contentSizeHint() does not include the
270 * size of any borders surrounding the content area.
272 * The default implementation returns the sizeHint() of the applet's
273 * layout if it has one, or a null size otherwise.
275 virtual QSizeF contentSizeHint() const;
278 * Sets the minimum size for the content of this applet
280 void setMinimumContentSize(const QSizeF &minSize);
283 * Sets the minimum size for the content of this applet
285 * @arg minWidth the new minimum width of the contents area
286 * @arg minHeight the new minimum height of the contents area
288 void setMinimumContentSize(int minWidth, int minHeight);
291 * Get the minimum size for the content of this applet
293 QSizeF minimumContentSize() const;
296 * Sets the maximum size for the content of this applet.
298 void setMaximumContentSize(const QSizeF &maxSize);
301 * Sets the maximum size for the content of this applet
303 * @arg maxWidth the new maximum width of the contents area
304 * @arg maxHeight the new maximum height of the contents area
306 void setMaximumContentSize(int maxWidth, int maxHeight);
309 * Get the minimum size for the content of this applet
311 QSizeF maximumContentSize() const;
314 * @return the prefered aspect ratio mode for placement and resizing
316 Qt::AspectRatioMode aspectRatioMode() const;
319 * Sets the prefered aspect ratio mode for placement and resizing
321 void setAspectRatioMode(Qt::AspectRatioMode);
324 * @return whether or not to keep this applet square.
326 bool remainSquare() const;
329 * Sets whether or not this applet should be kept square.
331 * @param square true if the applet should always be square in shape.
333 void setRemainSquare(bool square);
336 * Returns a list of all known applets.
338 * @param category Only applets matchin this category will be returned.
339 * Useful in conjunction with knownCategories.
340 * If "Misc" is passed in, then applets without a
341 * Categories= entry are also returned.
342 * If an empty string is passed in, all applets are
343 * returned.
344 * @param parentApp the application to filter applets on. Uses the
345 * X-KDE-ParentApp entry (if any) in the plugin info.
346 * The default value of QString() will result in a
347 * list containing only applets not specifically
348 * registered to an application.
349 * @return list of applets
351 static KPluginInfo::List knownApplets(const QString &category = QString(),
352 const QString &parentApp = QString());
355 * Returns a list of all known applets associated with a certain mimetype.
357 * @return list of applets
359 static KPluginInfo::List knownAppletsForMimetype(const QString &mimetype);
362 * Returns a list of all the categories used by
363 * installed applets.
365 * @param parentApp the application to filter applets on. Uses the
366 * X-KDE-ParentApp entry (if any) in the plugin info.
367 * The default value of QString() will result in a
368 * list containing only applets not specifically
369 * registered to an application.
370 * @return list of categories
371 * @param visibleOnly true if it should only return applets that are marked as visible
373 static QStringList knownCategories(const QString &parentApp = QString(), bool visibleOnly = true);
376 * Attempts to load an applet
378 * Returns a pointer to the applet if successful.
379 * The caller takes responsibility for the applet, including
380 * deleting it when no longer needed.
382 * @param name the plugin name, as returned by KPluginInfo::pluginName()
383 * @param applet unique ID to assign the applet, or zero to have one
384 * assigned automatically.
385 * @param args to send the applet extra arguments
386 * @return a pointer to the loaded applet, or 0 on load failure
388 static Applet* load(const QString &name, uint appletId = 0,
389 const QVariantList& args = QVariantList());
392 * Attempts to load an applet
394 * Returns a pointer to the applet if successful.
395 * The caller takes responsibility for the applet, including
396 * deleting it when no longer needed.
398 * @param info KPluginInfo object for the desired applet
399 * @param applet unique ID to assign the applet, or zero to have one
400 * assigned automatically.
401 * @return a pointer to the loaded applet, or 0 on load failure
403 static Applet* load(const KPluginInfo& info, uint appletId = 0,
404 const QVariantList& args = QVariantList());
407 * Get the category of the given applet
409 * @param a KPluginInfo object for the applet
411 static QString category(const KPluginInfo& applet);
414 * Get the category of the given applet
416 * @param the name of the applet
418 static QString category(const QString& appletName);
421 * This method is called when the interface should be painted.
423 * @param painter the QPainter to use to do the paintiner
424 * @param option the style options object
425 * @param contentsRect the rect to paint within; automatically adjusted for
426 * the background, if any
428 virtual void paintInterface(QPainter *painter,
429 const QStyleOptionGraphicsItem *option,
430 const QRect& contentsRect);
433 * Returns the user-visible name for the applet, as specified in the
434 * .desktop file.
436 * @return the user-visible name for the applet.
438 QString name() const;
441 * Returns the plugin name for the applet
443 QString pluginName() const;
446 * Whether the applet should conserve resources. If true, try to avoid doing stuff which
447 * is computationally heavy. Try to conserve power and resources.
449 * @return true if it should conserve resources, false if it does not.
451 bool shouldConserveResources() const;
454 * Returns the icon related to this applet
456 QString icon() const;
459 * Returns the category the applet is in, as specified in the
460 * .desktop file.
462 QString category() const;
465 * Returns the color corresponding to the applet's category.
467 QColor color() const;
470 * @return true if this applet is immutable
472 bool isImmutable() const;
475 * @return true if this applet is immutable due to Kiosk settings
477 bool isKioskImmutable() const;
480 * @return returns whether or not the applet is using the standard
481 * background
483 bool drawStandardBackground() const;
486 * Sets whether the applet should automatically draw the standard
487 * background.
489 * Defaults to true
491 void setDrawStandardBackground(bool drawBackground);
494 * If for some reason, the applet fails to get up on its feet (the
495 * library couldn't be loaded, necessary hardware support wasn't found,
496 * etc..) this method returns true
498 bool failedToLaunch() const;
501 * Call this method when the applet fails to launch properly. An
502 * optional reason can be provided.
504 * Not that all children items will be deleted when this method is
505 * called. If you have pointers to these items, you will need to
506 * reset them after calling this method.
508 * @param failed true when the applet failed, false when it succeeded
509 * @param reason an optional reason to show the user why the applet
510 * failed to launch
512 void setFailedToLaunch(bool failed, const QString& reason = QString());
515 * @return true if the applet currently needs to be configured,
516 * otherwise, false
518 bool needsConfiguring() const;
521 * When the applet needs to be configured before being usable, this
522 * method can be called to show a standard interface prompting the user
523 * to configure the applet
525 * Not that all children items will be deleted when this method is
526 * called. If you have pointers to these items, you will need to
527 * reset them after calling this method.
529 * @param needsConfiguring true if the applet needs to be configured,
530 * or false if it doesn't
532 void setNeedsConfiguring(bool needsConfiguring);
535 * @return true if this plasmoid provides a GUI configuration
537 bool hasConfigurationInterface();
540 * Sets whether or not this applet provides a user interface for
541 * configuring the applet.
543 * It defaults to false, and if true is passed in you should
544 * also reimplement showConfigurationInterface()
546 * @arg hasInterface whether or not there is a user interface available
548 void setHasConfigurationInterface(bool hasInterface);
551 * Returns a list of context-related QAction instances.
553 * This is used e.g. within the \a DesktopView to display a
554 * contextmenu.
556 * @return A list of actions. The default implementation returns an
557 * empty list.
559 virtual QList<QAction*> contextActions();
562 * Sets shadow for the given applet.
564 void setShadowShown(bool);
567 * Returns true if the given item has a shadow shown.
569 bool isShadowShown() const;
572 * Sets whether or not this Applet is acting as a Containment
574 void setIsContainment(bool isContainment);
577 * @return true if this Applet is currently being used as a Containment, false otherwise
579 bool isContainment() const;
582 * Sets the geometry of this Plasma::Applet. Should not be used directly by
583 * applet subclasses.
584 * @param geometry the geometry to apply to this Plasma::Applet.
586 void setGeometry(const QRectF &geometry);
589 * Causes this applet to raise above all other applets.
591 void raise();
594 * Reimplemented from LayoutItem
596 QSizeF sizeHint(Qt::SizeHint which, const QSizeF &constraint = QSizeF()) const;
599 * Reimplemented from QGraphicsItem
601 int type() const;
602 enum { Type = Plasma::AppletType };
605 * Reimplemented from QGraphicsItem
607 QRectF boundingRect() const;
610 * Reimplemented from QGraphicsItem
612 QPainterPath shape() const;
615 * @internal
617 void resetConfigurationObject();
620 * @return the Containment, if any, this applet belongs to
622 Containment* containment() const;
624 Q_SIGNALS:
626 * Emitted when the applet needs to take (or lose) keyboard focus.
628 * An applet should emit this signal to ensure that autohiding
629 * elements stay unhidden and other bits of bookkeeping are
630 * performed to ensure proper function.
632 * If you call watchForFocus on your applet, then this is handled for
633 * the applet and it is not necessary to emit the signal directly.
635 * @param focus true if the applet is taking keyboard focus, false if
636 * it is giving it up
638 void requestFocus( bool focus );
641 * Emitted whenever the applet makes a geometry change, so that views
642 * can coordinate themselves with these changes if they desire.
644 void geometryChanged();
647 * Emitted when an applet has changed values in its configuration
648 * and wishes for them to be saved at the next save point. As this implies
649 * disk activity, this signal should be used with care.
651 * @note This does not need to be emitted from saveState by individual
652 * applets.
654 void configNeedsSaving();
656 public Q_SLOTS:
658 * Sets whether or not this applet is immutable or not.
660 * @arg immutable true if this applet should not be changeable
662 void setImmutable(bool immutable);
665 * Destroys the applet; it will be deleted and configurations reset.
667 void destroy();
670 * Reimplement this slot to show a configuration dialog.
672 * Let the user play with the plasmoid options.
673 * Called when the user selects the configure entry
674 * from the context menu.
676 virtual void showConfigurationInterface();
679 * Sends all pending contraints updates to the applet. Will usually
680 * be called automatically, but can also be called manually if needed.
682 void flushUpdatedConstraints();
685 * This method should be invoked whenever an application launch or other window
686 * creation of focussing event was triggered
688 void launchActivated();
691 * called when the Plasma::Theme has changed
693 void themeChanged();
695 protected:
697 * Called when a request to save the state of the applet is made
698 * during runtime
700 virtual void saveState(KConfigGroup* config) const;
703 * Returns the name of the applet.
705 * This will be the same for all instances of this applet.
707 QString globalName() const;
710 * Returns a name unique to the instance of this applet.
712 * Useful for being able to refer directly to a particular
713 * applet. Combines the global name with the applet id
715 QString instanceName() const;
718 * Register widgets that can receive keyboard focus.
720 * Calling this results in an eventFilter being placed on the widget.
722 * @param widget the widget to watch for keyboard focus
723 * @param watch whether to start watching the widget, or to stop doing so
725 void watchForFocus(QObject *widget, bool watch = true);
728 * Register the widgets that manages mouse clicks but you still want
729 * to be able to drag the applet around when holding the mouse pointer
730 * on that widgets.
732 * Calling this results in an eventFilter being places on the widget.
734 * @param widget the widget to watch for mouse move
735 * @param watch whether to start watching the widget, or to stop doing so
737 void watchForMouseMove(QGraphicsItem * watched, bool watch);
740 * Call this whenever focus is needed or not needed.
742 * You do not have to call this method for widgets that have
743 * been registered with watchForFocus
745 * @see watchForFocus
746 * @param focus whether to or not to request focus
748 void needsFocus(bool focus);
751 * @internal event filter; used for focus watching
753 bool eventFilter( QObject *o, QEvent *e );
756 * @internal scene event filter; used to manage applet dragging
758 bool sceneEventFilter ( QGraphicsItem * watched, QEvent * event );
761 * @internal manage the mouse movement to drag the applet around
763 void mouseMoveEvent(QGraphicsSceneMouseEvent *event);
766 * Reimpmlemented from LayoutItem
768 QPointF topLeft() const;
771 * Reimplemented from QGraphicsItem
773 QVariant itemChange(GraphicsItemChange change, const QVariant &value);
775 protected Q_SLOTS:
777 * @internal used to show the configuration of an applet on first show
779 void performSetupConfig();
782 * @internal used to check the immutability of the item in the config file
784 void checkImmutability();
786 private:
787 Q_DISABLE_COPY(Applet)
790 * Reimplemented from QGraphicsItem
792 void paintWidget(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = 0);
794 class Private;
795 Private* const d;
798 } // Plasma namespace
801 * Register an applet when it is contained in a loadable module
803 #define K_EXPORT_PLASMA_APPLET(libname, classname) \
804 K_PLUGIN_FACTORY(factory, registerPlugin<classname>();) \
805 K_EXPORT_PLUGIN(factory("plasma_applet_" #libname))
807 #endif // multiple inclusion guard