Difference between revisions of "Paths/Developer/Creating plasmoids with scripts"

Jump to: navigation, search
(fix html in fixed script)
m
 
(One intermediate revision by one user not shown)
Line 10: Line 10:
  
 
==Layout==
 
==Layout==
Each scripted plasmoid shares the same layout.  
+
Each scripted Plasmoid shares the same folder layout.  
The base folder name is, your applet's name, like "hello world"
+
The base folder name is, your applet's name. As this is a "Hello World" example let's call it "hello world".
 
+
This basic layout will look like this:
  Base
+
<pre>
 +
  hello-world
 
   metadata.desktop
 
   metadata.desktop
Contents
+
  contents
  Code
+
    code
  Ui
+
      main.py
 +
    ui
 +
    locale
 +
</pre>
  
 
==metadata.desktop==
 
==metadata.desktop==
 
The base folder contains a metadata.desktop, which should look a little something like:
 
The base folder contains a metadata.desktop, which should look a little something like:
 
+
<pre>
 
  [Desktop Entry]
 
  [Desktop Entry]
 
  Encoding=UTF-8
 
  Encoding=UTF-8
 
  Name=Hello World
 
  Name=Hello World
 +
Comment=This Plasmoid just says Hello to the World
 
  Type=Service
 
  Type=Service
 
  ServiceTypes=Plasma/Applet
 
  ServiceTypes=Plasma/Applet
Line 41: Line 46:
 
  X-Ubuntu-Gettext-Domain=desktop_kdebase-workspace
 
  X-Ubuntu-Gettext-Domain=desktop_kdebase-workspace
 
  X-KDE-PluginInfo-EnabledByDefault=true
 
  X-KDE-PluginInfo-EnabledByDefault=true
 
+
</pre>
 
Notice the X-Plasma-API string. from there you can set it to ruby, python, whatever.  
 
Notice the X-Plasma-API string. from there you can set it to ruby, python, whatever.  
 
Ruby is: ruby-script
 
Ruby is: ruby-script
Line 54: Line 59:
 
(I have tested this script, it works in 4.2. Note: In Pre4.2 it was include plasma, then plasma.Applet, in 4.2+ it's from PyKDE4 import plasmascript then plasmascript.Applet for the equiv.)
 
(I have tested this script, it works in 4.2. Note: In Pre4.2 it was include plasma, then plasma.Applet, in 4.2+ it's from PyKDE4 import plasmascript then plasmascript.Applet for the equiv.)
  
 
+
<pre>
 
  # Import all the headers for Qt and KDE
 
  # Import all the headers for Qt and KDE
 
  from PyQt4.QtCore import *
 
  from PyQt4.QtCore import *
Line 67: Line 72:
 
     plasmascript.Applet.__init__(self, parent)
 
     plasmascript.Applet.__init__(self, parent)
 
     self.top = parent
 
     self.top = parent
  def max(s, o, t):
 
        if o > t:
 
              return o
 
        return t
 
 
   def init(self):
 
   def init(self):
 
     self.text = "Hello World"
 
     self.text = "Hello World"
Line 83: Line 84:
 
       pen = Plasma.Theme.defaultTheme().color(Plasma.Theme.TextColor)
 
       pen = Plasma.Theme.defaultTheme().color(Plasma.Theme.TextColor)
 
       plainFont = KGlobalSettings.generalFont()
 
       plainFont = KGlobalSettings.generalFont()
       plainFont.setPointSizeF( self.max(rect.height(), KGlobalSettings.smallestReadableFont().pointSize()))
+
       plainFont.setPointSizeF( max(rect.height(), KGlobalSettings.smallestReadableFont().pointSize()))
 
       p = self.preparePainter(p, rect, plainFont, self.text)
 
       p = self.preparePainter(p, rect, plainFont, self.text)
 
       p.drawText(QRectF(rect), self.text, QTextOption(Qt.AlignCenter))
 
       p.drawText(QRectF(rect), self.text, QTextOption(Qt.AlignCenter))
Line 91: Line 92:
 
   def setFont(self, p, rect, font, text, tmpRect, tmpFont):
 
   def setFont(self, p, rect, font, text, tmpRect, tmpFont):
 
             p.setFont(tmpFont)
 
             p.setFont(tmpFont)
             tmpFont.setPointSize( self.max(KGlobalSettings.smallestReadableFont().pointSize(),tmpFont.pointSize() - 1) )
+
             tmpFont.setPointSize( max(KGlobalSettings.smallestReadableFont().pointSize(),tmpFont.pointSize() - 1) )
 
             tmpRect = p.boundingRect(rect, Qt.TextWordWrap, text)
 
             tmpRect = p.boundingRect(rect, Qt.TextWordWrap, text)
 
             return tmpRect
 
             return tmpRect
Line 105: Line 106:
 
  def CreateApplet(parent):
 
  def CreateApplet(parent):
 
   return HelloWorld(parent)
 
   return HelloWorld(parent)
 
+
</pre>
==Explaination==
+
==Explanation==
 
The SVG, The Font, and the Applet have to be made. The SVG is basically the Plasma Theme system (I think) but can be used to draw images. The font is resized to fit inside the bounding rect of the applet (Which can change when the user resizes it), Then the text is drawn to the screen using the resized font. Fairly simple.
 
The SVG, The Font, and the Applet have to be made. The SVG is basically the Plasma Theme system (I think) but can be used to draw images. The font is resized to fit inside the bounding rect of the applet (Which can change when the user resizes it), Then the text is drawn to the screen using the resized font. Fairly simple.
  

Latest revision as of 06:53, 10 February 2010

Plasma Chani's advantures in PlasmaLand: http://chani.wordpress.com/2008/12/29/adventures-in-plasmaland-part-1/

Contents

[edit] Introduction

The Python Hello World. Note: This may need work. I haven't tested the script and wrote/translated some code on the fly for this tutorial. There may very well be typos. Sorry.


[edit] Layout

Each scripted Plasmoid shares the same folder layout. The base folder name is, your applet's name. As this is a "Hello World" example let's call it "hello world". This basic layout will look like this:

 hello-world
   metadata.desktop
   contents
     code
       main.py
     ui
     locale

[edit] metadata.desktop

The base folder contains a metadata.desktop, which should look a little something like:

 [Desktop Entry]
 Encoding=UTF-8
 Name=Hello World
 Comment=This Plasmoid just says Hello to the World
 Type=Service
 ServiceTypes=Plasma/Applet

 X-Plasma-API=python
 X-Plasma-MainScript=code/main.py
 X-KDE-PluginInfo-Author=Matthew Adams
 X-KDE-PluginInfo-Email=
 X-KDE-PluginInfo-Name=myHelloWorld
 X-KDE-PluginInfo-Version=1.0
 X-KDE-PluginInfo-Website=http://plasma.kde.org/
 X-KDE-PluginInfo-Category=Examples
 X-KDE-PluginInfo-Depends=
 X-KDE-PluginInfo-License=GPL
 X-KDE-PluginInfo-EnabledByDefault=true
 X-Ubuntu-Gettext-Domain=desktop_kdebase-workspace
 X-KDE-PluginInfo-EnabledByDefault=true

Notice the X-Plasma-API string. from there you can set it to ruby, python, whatever. Ruby is: ruby-script JavaScript: C#: Others:

[edit] The Script in Python

In Base/contents/code you will have your main script file, named "main.py" This is your main script file. The main.py should have a function called "def CreateApplet(parent):" that returns an object that inherits plasma.Applet. in order to be able to inherit plasma.Applet you will need to include a couple specific headers. Here's the helloworld script:

(I have tested this script, it works in 4.2. Note: In Pre4.2 it was include plasma, then plasma.Applet, in 4.2+ it's from PyKDE4 import plasmascript then plasmascript.Applet for the equiv.)

 # Import all the headers for Qt and KDE
 from PyQt4.QtCore import *
 from PyQt4.QtGui import *
 from PyKDE4.kdecore import *
 from PyKDE4.kdeui import *
 from PyKDE4.plasma import Plasma
 from PyKDE4 import plasmascript
 from PyQt4 import QtCore, QtGui
 class HelloWorld(plasmascript.Applet):
   def __init__(self, parent, args=None):
     plasmascript.Applet.__init__(self, parent)
     self.top = parent
   def init(self):
     self.text = "Hello World"
     self.svg = Plasma.Svg(self)
     self.svg.imagePath = 'widgets/background'
   # Yes the code for drawing the text directly on to the applet is fairly large
   # and ugly. It can be done other ways however, add a layout to the applet,
   # then a text label to the layout, then draw the text onto the text label 
   # Although I've never tried that method. This entire method was borrowed
   # From one of the clock applets. 
   def paintInterface(self,p , options, rect):
      pen = Plasma.Theme.defaultTheme().color(Plasma.Theme.TextColor)
      plainFont = KGlobalSettings.generalFont()
      plainFont.setPointSizeF( max(rect.height(), KGlobalSettings.smallestReadableFont().pointSize()))
      p = self.preparePainter(p, rect, plainFont, self.text)
      p.drawText(QRectF(rect), self.text, QTextOption(Qt.AlignCenter))
      self.svg.resize(rect.left()+rect.right()+1, rect.top()+rect.bottom()+1)
      self.svg.paint(p, QRectF(rect), "widgets/background")
      self.resize(rect.left()+rect.right()+1, rect.top()+rect.bottom()+1)
   def setFont(self, p, rect, font, text, tmpRect, tmpFont):
            p.setFont(tmpFont)
            tmpFont.setPointSize( max(KGlobalSettings.smallestReadableFont().pointSize(),tmpFont.pointSize() - 1) )
            tmpRect = p.boundingRect(rect, Qt.TextWordWrap, text)
            return tmpRect
   def preparePainter(self, p, rect, font, text):
          tmpRect = QRect()
          tmpFont = font
          # Starting with the given font, decrease its size until it'll fit in  the
          # given rect allowing wrapping where possible. Thx clock applet!
          tmpRect = self.setFont(p, rect, font, text, tmpRect, tmpFont)
          while tmpFont.pointSize() >  KGlobalSettings.smallestReadableFont().pointSize() and (tmpRect.width() >  rect.width() or tmpRect.height() > rect.height()):
               tmpRect = self.setFont(p, rect, font, text, tmpRect, tmpFont)
          return p
 def CreateApplet(parent):
   return HelloWorld(parent)

[edit] Explanation

The SVG, The Font, and the Applet have to be made. The SVG is basically the Plasma Theme system (I think) but can be used to draw images. The font is resized to fit inside the bounding rect of the applet (Which can change when the user resizes it), Then the text is drawn to the screen using the resized font. Fairly simple.

CreateApplet gets called at startup, and returns the applet we made. It's paintinterface function is called every so often to have the interface painted (text drawn to the screen), first __init__() gets called, but then the library calls "init(self)" on the object, which is where all the real setup happens.


[edit] Installation

 Once you have it all setup, simply run:
 cd /path/to/BASE/
 zip -r ../HelloWorld.plasmoid *
 plasmapkg -i ../HelloWorld.plasmoid

[edit] Testing and running

Once you have installed you can test with

 plasmoidviewer myHelloWorld

Note the name, myHelloWorld. That's defined in the metadata.desktop as the Name= value.

[edit] Removal

 On old kde 4.1.X plasmapkg didn't work with plasmapkg -r ../HelloWorld.plasmoid
 It does in 4.2X - use plasmapkg -r ../HelloWorld.plasmoid in 4.2X
 In 4.1.X do rm -r ~/.kde/share/apps/plasma/plasmoids/HelloWorld to remove

[edit] More Information

There should be example plasmoids for each scripting language available. You can find them from http://websvn.kde.org/trunk/KDE/kdebindings/ (Example: http://websvn.kde.org/trunk/KDE/kdebindings/csharp/plasma/examples/ )

[edit] End Notes

Once you complete a useful or fun plasmoids, you can put it on KDE-Look.org under the scripted plasmoids section, and have it show up in Get Hot New Stuff on peoples computers without them ever opening a web browser.

The differences between Plasma, KDE, and Qt are all very subtle. It is possible and advised to use Qt Designer for the creation of your gui's, and tying them in to your Plasmoid. You can use the output from Qt Designer directly after passing it through pyuic4 without any changes to the output from the .ui file - which means you can design applets so that the gui can be redone and replaced in Qt Designer without touching a single line of code. How is that bad for artists? That's a lesson for another day however. Peace.


This page was last modified on 10 February 2010, at 06:53. This page has been accessed 4,266 times. Content is available under Creative Commons License SA 3.0 as well as the GNU Free Documentation License 1.2.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V.Legal