PythonPlasmoids
Revision 3 as of 2009-08-31 20:23:51
Clear message
Dev Week -- Fun with Python Plasmoids -- agateau and Riddell -- Mon Aug 31st, 2009
UTC
(04:02:23 PM) agateau: Shall we start now? (04:02:25 PM) jawnsy: *round of applause for Riddell and agateau* :-) (04:02:43 PM) agateau: thanks jawnsy :) (04:03:12 PM) agateau: Riddell and myself are now going to introduce you to plasmoid developments in Python (04:03:37 PM) Riddell: and we want you to follow along at home! (04:03:37 PM) agateau: I'll do a short intro of Plasma and Python, then Riddell will take you through your first plasmoid (04:04:04 PM) agateau: and I'll come back with more widgetry for your plasmoids (04:04:14 PM) agateau: First things first, (04:04:22 PM) agateau: What is Plasma? (04:04:36 PM) agateau: It's the new implementation of the desktop (04:04:39 PM) agateau: in KDE4 (04:05:20 PM) agateau: Riddell reminds me I should tell you what packages you need to install while I talk: (04:05:36 PM) agateau: apt-get install kdebase-workspace-bin plasma-scriptengine-python (04:05:41 PM) agateau: and you should be all set (04:06:06 PM) agateau: so, Plasma is based on Qt Graphics View framework, which is, quoting Qt doc: (04:06:13 PM) agateau: "Graphics View provides a surface for managing and interacting with a large (04:06:13 PM) agateau: number of custom-made 2D graphical items, and a view widget for visualizing the (04:06:13 PM) agateau: items, with support for zooming and rotation." (04:06:29 PM) agateau: it can use hardward acceleration, and be themed with SVG files (04:06:40 PM) agateau: what are plasmoid? (04:07:00 PM) agateau: plasmoids are little gadgets you can put on your desktop (04:07:08 PM) agateau: the whole KDE4 desktop is made of plasmoids (04:07:17 PM) agateau: (taskbar, pager, K menu, clock, systray...) (04:07:32 PM) agateau: some examples: (04:07:38 PM) agateau: http://kde.org/announcements/4.2/screenshots/plasma-other-widgets.png (04:07:45 PM) agateau: http://kde.org/announcements/4.3/screenshots/desktop.png (04:08:04 PM) agateau: Plasmoids can be developed in C++, JavaScript, Ruby... (04:08:06 PM) agateau: and Python (04:08:12 PM) agateau: our beloved language (04:08:44 PM) agateau: Python is an interpreted, dynamic programming language (04:08:54 PM) agateau: it's simple yet powerful, (04:08:59 PM) agateau: and very versatile (04:09:13 PM) agateau: it can be used for throw away scripts, desktop applications, web servers... (04:09:18 PM) agateau: and plasmoids (04:09:28 PM) agateau: as Riddell is now going to show you... (04:09:35 PM) Riddell: we had some questions first (04:09:42 PM) Riddell: 21:05 < wizz_> is plasmoid available for ubuntu 9.04 and python2.6? (04:09:55 PM) Riddell: yes, you need to install python-plasma in jaunty (04:10:06 PM) Riddell: as well as kdebase-workspace-bin (04:10:17 PM) Riddell: 21:06 < msp301> will plasma work under gnome?? (04:10:23 PM) Riddell: yes, you can use the plasmoidviewer app (04:10:37 PM) Riddell: or you can try running plasma-desktop on top of gnome, goodness knows how that will end up (04:11:05 PM) Riddell: so let's get coding! (04:11:23 PM) Riddell: a basic plasmoid is made up of a metadata file (04:11:38 PM) Riddell: which tells plasma the name and other vital information about the plasmoid (04:11:42 PM) Riddell: and some code (04:11:54 PM) Riddell: that all gets zipped up (04:12:10 PM) Riddell: and finally you install the zip file so you can run the plasmoid (04:12:23 PM) Riddell: so start off in a new directory (04:12:43 PM) Riddell: and make the directories needed for our "hello-python" plasmoid (04:12:52 PM) Riddell: mkdir -p hello-python/contents/code (04:13:15 PM) Riddell: cd hello-python (04:13:25 PM) Riddell: here we'll put our metadata which is in .desktop format (04:13:44 PM) Riddell: [Desktop Entry] (04:13:44 PM) Riddell: Encoding=UTF-8 (04:13:44 PM) Riddell: Name=Hello Python (04:13:44 PM) Riddell: Type=Service (04:13:44 PM) Riddell: ServiceTypes=Plasma/Applet (04:13:46 PM) Riddell: Icon=chronometer (04:14:08 PM) Riddell: is the top, that gives it a name and tells plasma that it's an applet, also gives it an icon to use for the Add Applet dialogue (04:14:38 PM) Riddell: next some vital plasma info lines (04:14:39 PM) Riddell: X-Plasma-API=python (04:14:40 PM) Riddell: X-Plasma-MainScript=code/main.py (04:14:58 PM) Riddell: so plasma knows it's looking for Python and it knows what code it's looking for (04:15:23 PM) Riddell: finally some plugin info lines (04:15:25 PM) Riddell: X-KDE-PluginInfo-Author=Simon Edwards (04:15:26 PM) Riddell: X-KDE-PluginInfo-Email=simon@simonzone.com (04:15:26 PM) Riddell: X-KDE-PluginInfo-Name=hello-python (04:15:26 PM) Riddell: X-KDE-PluginInfo-Version=1.0 (04:15:26 PM) Riddell: X-KDE-PluginInfo-Website=http://plasma.kde.org/ (04:15:28 PM) Riddell: X-KDE-PluginInfo-Category=Examples (04:15:31 PM) Riddell: X-KDE-PluginInfo-Depends= (04:15:33 PM) Riddell: X-KDE-PluginInfo-License=GPL (04:15:36 PM) Riddell: X-KDE-PluginInfo-EnabledByDefault=true (04:15:38 PM) Riddell: 21:15 < keffie_jayx> Riddell: what is the file name .Desktop? (04:15:48 PM) Riddell: this all goes in a file called "metadata.desktop" (04:15:57 PM) Riddell: and here's the full thing (04:15:58 PM) Riddell: http://people.canonical.com/~jriddell/plasma-python/hello-python/metadata.desktop (04:16:32 PM) Riddell: so if you were following closely, you'll have worked out that code/main.py will be where the real code is (04:17:05 PM) Riddell: cd contents/code and emacs code.py (04:17:13 PM) Riddell: we all use emacs don't we? :) (04:17:36 PM) Riddell: sorry emacs main.py (04:18:03 PM) Riddell: python always starts with importing the relevant libraries (04:18:07 PM) Riddell: in this case it's PyQt and PyKDE (04:18:10 PM) Riddell: from PyQt4.QtCore import * (04:18:10 PM) Riddell: from PyQt4.QtGui import * (04:18:10 PM) Riddell: from PyKDE4.plasma import Plasma (04:18:10 PM) Riddell: from PyKDE4 import plasmascript (04:18:56 PM) Riddell: we want to make a class inheriting from the plasma Applet base class (04:19:28 PM) Riddell: if you know object orientated programming, python is very simple (04:19:30 PM) Riddell: class HelloPython(plasmascript.Applet): (04:19:30 PM) Riddell: def __init__(self,parent,args=None): (04:19:30 PM) Riddell: plasmascript.Applet.__init__(self,parent) (04:19:59 PM) Riddell: that's the class header and the constructor, which just calls the parent constructor (04:20:32 PM) Riddell: we want an init() method to do some basic setup (04:20:32 PM) Riddell: def init(self): (04:20:32 PM) Riddell: self.setHasConfigurationInterface(False) (04:20:32 PM) Riddell: self.resize(125, 125) (04:20:32 PM) Riddell: self.setAspectRatioMode(Plasma.Square) (04:21:03 PM) Riddell: Plasma prefers we don't do the basic setup in the constructor so it gives us this separate init() method instead (04:21:26 PM) Riddell: the code should be pretty self readable because KDE APIs are like that, and Python is clean as programming languages come (04:21:51 PM) Riddell: the main body we're interested in is the paint method which will paint our hello message (04:21:55 PM) Riddell: def paintInterface(self, painter, option, rect): (04:21:55 PM) Riddell: painter.save() (04:21:55 PM) Riddell: painter.setPen(Qt.white) (04:21:55 PM) Riddell: painter.drawText(rect, Qt.AlignVCenter | Qt.AlignHCenter, "Hello Kubuntu!") (04:21:58 PM) Riddell: painter.restore() (04:22:40 PM) Riddell: which is also pretty self explanatory, it uses the painting object to put some text on the screen (04:23:11 PM) Riddell: finally plasma needs us to create the applet object from our class (04:23:12 PM) Riddell: def CreateApplet(parent): return HelloPython(parent) ...