(→Services) |
m (Fix syntax highlighting.) |
||
| (6 intermediate revisions by 5 users not shown) | |||
| Line 1: | Line 1: | ||
| + | {{TutorialBrowser| | ||
| + | |||
| + | series=JavaScript Plasmoids| | ||
| + | |||
| + | name=Getting Data| | ||
| + | |||
| + | name=JavaScript Plasmoid Cheat Sheet| | ||
| + | |||
| + | pre=[[../DataEngine|Now Playing: Advanced DataEngine Usage Example]]| | ||
| + | |||
| + | reading=[[../API|JavaScript Plasmoid API reference]] | ||
| + | }} | ||
| + | |||
| + | |||
== Basics == | == Basics == | ||
| Line 7: | 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 | ||
| − | < | + | <syntaxhighlight lang="javascript"> |
if (plasmoid.formFactor() == Vertical) { | if (plasmoid.formFactor() == Vertical) { | ||
layout.setOrientation(QtVertical); | layout.setOrientation(QtVertical); | ||
| Line 13: | Line 27: | ||
layout.setOrientation(QtHorizontal); | layout.setOrientation(QtHorizontal); | ||
} | } | ||
| − | </ | + | </syntaxhighlight> |
== Global variables and functions == | == Global variables and functions == | ||
| Line 21: | Line 35: | ||
* 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>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 | * The <tt>print</tt> function prints debug output | ||
| − | * There are several | + | * There are several constructor methods: |
** <tt>PlasmaSvg(file, parent)</tt> constructs a <tt>Plasma::Svg</tt>. The second argument is optional. | ** <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>PlasmaFrameSvg(file, parent)</tt> constructs a <tt>Plasma:FrameSvg</tt>. The second argument is optional. | ||
| Line 38: | Line 52: | ||
* The Name and Comment fields in the <tt>metadata.desktop</tt> file can be translated like <tt>Name[nl]=Hallo JavaScript</tt> | * The Name and Comment fields in the <tt>metadata.desktop</tt> file can be translated like <tt>Name[nl]=Hallo JavaScript</tt> | ||
| − | * <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> | + | * <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> | + | * <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> | + | * <tt>i18ncp()</tt> is a combination of the previous two. Eg: <tt>i18ncp("Personal file", "One file", "%1 files", numFiles);</tt> |
== DataEngines == | == DataEngines == | ||
| Line 53: | Line 67: | ||
* 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> | * 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="javascript"> | ||
| + | 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); | ||
| + | </syntaxhighlight> | ||
| Tutorial Series | JavaScript Plasmoids |
| Previous | Now Playing: Advanced DataEngine Usage Example |
| What's Next | n/a |
| Further Reading | JavaScript Plasmoid API reference |
Contents |
if (plasmoid.formFactor() == Vertical) { layout.setOrientation(QtVertical); } else { layout.setOrientation(QtHorizontal); }
If you want to use the standard notifications in KDE, you can using Services, with the following code:
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);