A widget consists in at least two python files stored in a directory.
The directory should be in a path searched by AUIMD. The default is "widgets" in the auimd package, other additional paths can be defined by the user in the settings menu. For instance the helloworld example can be found in the "example" subdirectory :
examples/ helloword/ __init__.py helloword.py
In order to load widgets defined in the examples directory, start AUIMD, then go in Menu -> Settings -> Widgets, click on "Add..." and select the "example" directory. Finally select "Save", then "Close". The "Hello World !" widget presented below will appear in the "Other" category, under the name "Untitled".
So lets start with the "Hello World !" example. This is the most simple widget we can define. It looks like :
The content of "helloworld.py" is :
from PyQt4 import QtCore, QtGui from core.auimd_widget import * class HelloWorld(QtGui.QLabel, AuimdWidget): def __init__(self, parent, *args): QtGui.QLabel.__init__(self, parent) AuimdWidget.__init__(self, parent) self.setAlignment(QtCore.Qt.AlignHCenter | QtCore.Qt.AlignVCenter) self.setText("Hello World !")
Some explanations :
from PyQt4 import QtCore, QtGui from core.auimd_widget import *
These classic lines import Qt modules and AUIMD modules.
class HelloWorld(QtGui.QLabel, AuimdWidget):
An AUIMD widget must at least inherit from a subclass of QWidget (user interface), and from AuimdWidget (widget structure). In this case our user interface is limited to a simple text label.
def __init__(self, parent, *args): QtGui.QLabel.__init__(self, parent) AuimdWidget.__init__(self, parent)
Initialise our base classes, a widget receives as constructor arguments its parent (the desktop), and optional arguments (some sort of command line parameters).
self.setAlignment(QtCore.Qt.AlignHCenter | QtCore.Qt.AlignVCenter) self.setText("Hello World !")
At last but not least, display some centered text.
Last step, create the __init__.py file :
import helloworld AuimdWidgets=(helloworld.HelloWorld,)
This file must contains a AuimdWidgets declaration, listing available widgets classes.
The widget will appear in the main menu, under the "Other" category, as "Untitled" with a default icon. In order to learn how to provide a better name, category and icon, read the next section.
The complete source code can be found at : http://svn.pierrox.net/auimd/trunk/examples/helloworld/
A widget will look better with some custom name, icon and category. Each class of widget determines these informations through the following static methods :
# set the widget icon, it appears in the main menu and in the widgets menu @staticmethod def defaultWidgetIcon(): return QtGui.QIcon(__path__+"/knewstuff.png") # set the widget title, it appears in the main menu and in the widgets menu # this is the title, for the class, each instance can later override/complement this title @staticmethod def defaultWidgetTitle(): return "Tutorial 1" # set the category in which the widget appears in the main menu @staticmethod def getCategory(): return "Other"
The complete source code can be found at : http://svn.pierrox.net/auimd/trunk/examples/tutorial/tut1/
We will add to this widget the ability to close itself when the close button is pressed.
Each widget has two buttons, located at the bottom of the screen, named "self.right_button" and "self.left_button". They are instances of QPushButton and can be configured as needed. They are automatically connected on slots named "rightButtonAction" and "leftButtonAction".
In the widget __init__ function, configure the button label with :
And react to the button click with this function :
def rightButtonAction(self): self.desktop.closeWidget(self)
The complete source code can be found at : http://svn.pierrox.net/auimd/trunk/examples/tutorial/tut2/