Difference between revisions of "Development/Tutorials/Plasma/JavaScript/CheatSheet"

Jump to: navigation, search
(i18n functions are global and not in plasmoid object.)
m (Text replace - "<code>" to "<syntaxhighlight lang="text">")
Line 21: Line 21:
 
* To connect to a signal use <tt>object.signalName.connect(function() {});</tt>
 
* To connect to a signal use <tt>object.signalName.connect(function() {});</tt>
 
* Enumeration values can be used like
 
* Enumeration values can be used like
<code>
+
<syntaxhighlight lang="text">
 
if (plasmoid.formFactor() == Vertical) {
 
if (plasmoid.formFactor() == Vertical) {
 
     layout.setOrientation(QtVertical);
 
     layout.setOrientation(QtVertical);
Line 72: Line 72:
 
If you want to use the standard notifications in KDE, you can using Services, with the following code:
 
If you want to use the standard notifications in KDE, you can using Services, with the following code:
  
<code>
+
<syntaxhighlight lang="text">
 
engine = dataEngine("notifications");
 
engine = dataEngine("notifications");
 
service = engine.serviceForSource("notification");
 
service = engine.serviceForSource("notification");

Revision as of 21:45, 29 June 2011

JavaScript Plasmoid Cheat Sheet
Tutorial Series   JavaScript Plasmoids
Previous   Now Playing: Advanced DataEngine Usage Example
What's Next   n/a
Further Reading   JavaScript Plasmoid API reference


Basics

  • Everything but the metadata.desktop file goes in a directory named contents
  • X-Plasma-MainScript in the metadata.desktop file should point to the main script, relative to the contents directory
  • X-Plasma-DefaultSize in the metadata.desktop file specifies the default widget size in width,height format (eg: 200,100)
  • plasmoidviewer is an immensely useful tool
  • To connect to a signal use object.signalName.connect(function() {});
  • Enumeration values can be used like
if (plasmoid.formFactor() == Vertical) {
    layout.setOrientation(QtVertical);
} else {
    layout.setOrientation(QtHorizontal);
}
</code>
 
== Global variables and functions ==
 
* The <tt>plasmoid</tt> variable contains all the main plasmoid functionality  and represents the main plasmoid widget, and corresponds to the <tt>Applet</tt> class in C++
* The <tt>startupArguments</tt> variable contains any arguments that were passed to the plasmoid when it was started (such as when the plasmoid is registered as a handler for a mimetype)
* The <tt>loadui</tt> function can be used to load a Qt UI file.  Beware that a relative path is relative to plasma's working directory (in 4.2 at least), and so may not do what you want.
* The <tt>print</tt> function prints debug output
* There are several constructor methods:
** <tt>PlasmaSvg(file, parent)</tt> constructs a <tt>Plasma::Svg</tt>.  The second argument is optional.
** <tt>PlasmaFrameSvg(file, parent)</tt> constructs a <tt>Plasma:FrameSvg</tt>.  The second argument is optional.
** <tt>QPainter()</tt>
** <tt>QGraphicsItem()</tt>
** <tt>QTimer()</tt>
** <tt>QFont()</tt>
** <tt>QRectF()</tt>
** <tt>QSizeF()</tt>
** <tt>QPoint()</tt>
** <tt>LinearLayout()</tt> constructs a <tt>QGraphicsLinearLayout</tt>
** <tt>Url()</tt> constructs a <tt>KUrl</tt>
* Plus constructors for all the Plasma widgets (eg: <tt>IconWidget()</tt>).
 
== Languages ==
 
* The Name and Comment fields in the <tt>metadata.desktop</tt> file can be translated like <tt>Name[nl]=Hallo JavaScript</tt>
* <tt>i18n()</tt> takes a string (in English) as the first argument, and substitutes in the following arguments to replace %1, %2, %3 etc.  Eg: <tt>i18n("The file is called %1", fileName);</tt>
* <tt>i18nc()</tt> works just the same, but has an extra argument at the start that provides some context for translators.  Eg: <tt>i18nc("Player name - score", "%1 - %2", playerName, score);</tt>
* <tt>i18np()</tt> is for cases where plural forms might be important.  Eg: <tt>i18np("One image in album %2", "%1 images in album %2", imageCount, albumName);</tt>
* <tt>i18ncp()</tt> is a combination of the previous two.  Eg: <tt>i18ncp("Personal file", "One file", "%1 files", numFiles);</tt>
 
== DataEngines ==
 
* Add a <tt>dataUpdate</tt> method to <tt>plasmoid</tt> to receive updates: <tt>plasmoid.dataUpdate = function(name, data) { /* ... */ }</tt> - <tt>data</tt> contains an object mapping keys to values
* <tt>plasmoid.dataEngine("engine name").connectSource("source name", plasmoid, 500);</tt> updates every half second
* <tt>plasmoid.dataEngine("engine name").connectSource("source name", plasmoid);</tt> updates when new data is available
* <tt>plasmaengineexplorer</tt> is your friend
 
== Services ==
 
* Prior to KDE 4.4, DO NOT use <tt>plasmoid.dataEngine("engine name").serviceForSource("source name")</tt> - you will get a dummy service back. Instead, use <tt>plasmoid.service("engine name", "source name")</tt>
 
=== Notifications ===
 
If you want to use the standard notifications in KDE, you can using Services, with the following code:
 
<syntaxhighlight lang="text">
engine = dataEngine("notifications");
service = engine.serviceForSource("notification");
op = service.operationDescription("createNotification");
op["appName"] = "foo";
op["appIcon"] = "konqueror";
op["summary"] = "this is a summary";
op["body"] = "body of notification";
op["timeout"] = 2000;
 
service.startOperationCall(op);
</code>

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