The ui module

The ui module contains modules pertaining to creating and manipulating user interfaces. This includes tasks such as: getting input from the user, and displaying a graphical user interface to the user.

The input module

The input module contains modules pertaining to retrieving input from the user using various methods.

The evdev module

evdev.py

This is a Python interface to the Linux input system’s event device. Events can be read from an open event file and decoded into spiffy python objects. The Event objects can optionally be fed into a Device object that represents the complete state of the device being monitored.

Copyright (C) 2003-2004 Micah Dowty <micah@navi.cx>

This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA

class pyamp.ui.input.evdev.Event(timestamp=0, type=0, code=0, value=0, unpack=None, readFrom=None)[source]

Represents one linux input system event. It can be encoded and decoded in the ‘struct input_event’ format used by the kernel. Types and codes are automatically encoded and decoded with the #define names used in input.h

pack()[source]

Pack this event into an input_event struct in the local machine’s byte order.

readFrom(stream)[source]

Read the next event from the given file-like object

unpack(s)[source]

Unpack ourselves from the given string,, an input_event struct in the local byte order.

class pyamp.ui.input.evdev.Device(filename)[source]

An abstract input device attached to a Linux evdev device node

poll()[source]

Receive and process all available input events

readMetadata()[source]

Read device identity and capabilities via ioctl()

update_EV_ABS(event)[source]

Scale the absolute axis into the range [-1, 1] using absAxisInfo

The ConsoleInput class

class pyamp.ui.input.ConsoleInput(callback, conditionFn=<function <lambda> at 0xa506df4>, exceptionFn=None, logger=None)

Bases: pyamp.processes.threading.thread.Thread

The ConsoleInput class provides the ability to receive input from a user through the console. This class runs as a separate thread which monitors the console for input and notifies a callback method in the event that keyboard input is received.

The callback method must take one argument which is the character that was read as input. This class also provides the ability to register a callback function to be called in the event that an exception occurs during the processing of the thread.

This class implements the pyamp.processes.Thread, and the amp.logging.Loggable interfaces.

  • callback – The function called when input is received

  • conditionFn – The function used to determine whether we should

    continue waiting for input

  • exceptionFn – The functionc called when an exception is encountered

  • logger – The logger object

classmethod clearScreen()

Clear the console screen.

classmethod getCols()

Get the number of columns for this console screen.

classmethod getRows()

Get the number of rows for this console screen.

classmethod getSize()

Get the size of the current console screen.

onCycle(_increment)

Called during each cycle of the Thread.

  • increment – The current increment number
onException(e, traceback)

This function is called in the event that an exception occurs during the processing of the thread.

  • e – The exception
  • traceback – The traceback
onShutdown()

Called when the ConsoleInput Thread is shutdown.

onStart()

Called when the ConsoleInput thread is started.

The Keys class

class pyamp.ui.input.Keys

The Keys class contains definitions of various keys.

class Control

The Control class contains definitions of various control keys.

The UserInput class

class pyamp.ui.input.UserInput(keyboard, debug=False)

The UserInput class uses the amp.ui.input.evdev module to receive the user’s keyboard presses given the specific Linux keyboard device.

This class provides to ability to add a series of callbacks for specific keyboard presses. These functions will be called in the event that the key presses occur.

  • keyboard – The Linux keyboard device
  • debug – True for debugging mode, False otherwise
addCode(key, code)

Add codes for given key press events. The key can be a tuple where the first index is a list of keys and the second index is a list of modifiers. Or it can be simply a list of keys, or it can be a single key.

addCodes(keys)

Add codes for given key press events. Each key in the keys dictionary can be a tuple where the first index is a list of keys and the second index is a list of modifiers. Or it can be simply a list of keys, or it can be a single key.

addKey(key, callback)

Add a single key to the key press cases. The key can be a tuple where the first argument is the list of keys, or a single key, and the second argument is a list of modifiers. Optionally, is can just be a list of keys, or it can be a single key.

addKeyList(keys, callback)

Add a list of keys mapped to a single function. Keys can be a tuple where the first index the a list of keys, or a single key, and the second index is the list of modifiers. Or it can be a list of keys, and finally it can be just a single key.

addKeys(keys)

Add a key to the key press cases. The keys is a dictionary of keys. Each key in the dictionary is a normal tuple form of a key, and the value of the dictionary key is the callback method.

handleKeyPress()

Handle keys that are pressed.

The qt module

The qt module contains classes pertaining to creating graphical user interfaces using PyQt.

The BaseView class

class pyamp.ui.qt.BaseView(name=None, controller=None, mainWindow=None, logData=None, logColor=0)

Bases: pyamp.logging.loggable.Loggable, pyamp.patterns.interfaces.requiredProperties.RequiredProperties

The BaseView class provides a base framework for creating Qt objects.

It provides several functions that are called in a specific order, and are to be overridden by subclasses in order to provide a specific implementation for the BaseView. These functions pertain to certain aspects of setting up a view and are called in the following order:

  1. configure()
  2. createLayout()
  3. configureLayout()
  4. setupView()
  5. setupConnections()

This class implements the pyamp.logging.Loggable interface and provides the ability to specify the logData object as well as the color to use for logging (the default color is to use a white foreground color).

  • name – The name of the BaseView
  • controller – The controller for the BaseView
  • mainWindow – The main window
  • logData – The LogData object
  • logColor – The color to use for the logger
completeSetup()

Finish setting up the view.

Note

This function should be overridden by subclasses.

configure()

Configure the view.

Note

This function should be overridden by subclasses.

configureLayout()

Configure the layout for this view.

Note

This function should be overridden by subclasses.

createLayout()

Create the main layout.

Note

This function should be overridden by subclasses.

setController(controller)

Set the controller object for this view.

  • controller – The controller
setupConnections()

Set up any connections this view requires.

Note

This function should be overridden by subclasses.

setupView()

Create and set up this view.

Note

This function should be overridden by subclasses.

The BaseWidget class

class pyamp.ui.qt.BaseWidget(name=None, controller=None, mainWindow=None, logData=None, logColor=0)

Bases: PyQt4.QtGui.QWidget, pyamp.ui.qt.baseView.BaseView

The BaseWidget class provides an implementation of the amp.ui.qt.BaseView class designed to set up and create a PyQt4.QtGui.QWidget object.

This class provides serveral functions which can be overridden by subclasses to easily add features to the BaseWidget. These functions are called in the following order during the set up process:

  1. configure()
  2. configureLayout()
  3. createElements()
  4. setupConnections()
  • name – The name of the BaseWidget
  • controller – The controller for the BaseWidget
  • mainWindow – The main window
  • logData – The pyamp.logging.LogData object
  • logColor – The color to use for logging
addElement(element)

Add an element to the main layout.

  • element – The element to add
addElements(elements)

Add a list of elements to the main layout.

  • elements – The list of elements
addItem(item)

Add an item to the main layout.

  • item – The item to add
addWidget(widget)

Add a widget to the main layout.

  • widget – The widget to add
clear()

Remove all of the child widgets and items currently for this BaseWidget.

clearItems()

Clear all the items from this BaseWidget.

clearWidgets()

Clear all the widgets from this BaseWidget.

configure()

Configure the view.

configureLayout()

Configure the layout for this view.

createElements()

Create the elements for this view. Return the list of widgets, or items in the order that they should be added to the layout.

createLayout()

Create the main layout.

getItems()

Return the list of child items for this BaseWidget.

getWidgets()

Return the list of child widgets for this BaseWidget.

layoutType

The layoutType property contains the type of layout to use for this Widget. It should be a value from the LayoutType class.

paintEvent(_event)

Override the paintEvent function to allow StylSheets to work for this QWidget.

Note this function was taken from the documentation of style sheets
for custom QWidget classes.
  • _event – The QPaintEvent
setMargins(left=None, top=None, right=None, bottom=None)

Set the content margins for this widget’s layout.

  • left – The left margin
  • top – The top margin
  • right – The right margin
  • bottom – The bottom margin
setSpacing(spacing)

Set the spacing between components in the layout.

  • spacing – The spacing
setStyleSheet(styleSheet)

Set the style sheet for this widget.

  • styleSheet – The StyleSheet
setupConnections()

Set up any connections this view requires.

setupView()

Create and set up this view.

tabOrder

The tabOrder property allows concrete widgets to set their tab order. Each element in the list will be tab connected to the next element in the list. The list will cycle around to the front so the final element is tab connected to the first element.

The KeyEvents class

class pyamp.ui.qt.KeyEvents

This class provided functions for dealing with PyQt4.QtGui.QKeyEvent objects.

The main function of this class is to provide a method to convert a PyQt4.QtGui.QKeyEvent object into a string representation of the keys that were pressed in the given event.

classmethod parse(event)

Parse the given QKeyEvent into the string representation of the keys pressed.

  • event – The QKeyEvent

The StyleSheet class

class pyamp.ui.qt.StyleSheet(widget=None, **kwargs)

The StyleSheet class provides a wrapper for PyQt4.QtGui.QWidget style sheet strings. StyleSheets can be easily created an exported into a string that is properly formatted for setting the style sheet of a PyQt4.QtGui.QWidget.

Example:

from PyQt4.QtGui import QApplication, QWidget
from pyamp.ui.qt import StyleSheet

# Create the widget, and set its name
app = QApplication([])
widget = QWidget()
widget.setObjectName("ObjectName")

# Create a simple style sheet
style = StyleSheet(border="2px solid black")
normal = "border: 2px solid black"

# These calls are identical
widget.setStyleSheet(str(style))
widget.setStyleSheet(normal)

# Create another style sheet, notice that underscores in the
# StylSheet parameter names are converted to dashes in the
# style sheet string
style = StyleSheet(background_color="red", color="blue",
                   font_weight="bold")
normal = "background-color: red; color: blue; font-weight: bold"

# These calls are identical
widget.setStyleSheet(str(style))
widget.setStyleSheet(normal)

# You can also specify a specific CSS selector string, which can be
# either a specific string, or a widget. If given a widget, the
# selector will be created using the object's class name (e.g.,
# QWidget) and will append an ID selector with the object's name (if
# and only if the widget is named).
style1 = StyleSheet(widget="QWidget#ObjectName",
                    border="1px solid red", margin_top="10")
style2 = StyleSheet(widget, border="1px solid red", margin_top="10")
normal = "QWidget#ObjectName { border: 1px solid red; margin-top: 10;"

# These calls are identical
widget.setStyleSheet(str(style1))
widget.setStyleSheet(str(style2))
widget.setStyleSheet(normal)
  • widget – The widget selector for this style sheet. Can be either a

    string, or an actual widget object

  • kwargs – The keyword arguments

The ResizePolicy class

class pyamp.ui.qt.ResizePolicy

The ResizePolicy encapsulates a specific policy for resizing widgets.

The possible resize policies are:

  • Fixed – The size never changes
  • Expanding – The size expands

For a specific policy, the ResizePolicy class can return the correct size for a widget, given the widget’s current size as well as the size of all of the widget’s child widgets.

For the Fixed resize policy: the size will always remain the same.

For the Expanding resize policy: the size will always expand to fit all of the widget’s children.

Example:

from PyQt4 import QtCore
from pyamp.ui.qt import ResizePolicy

# Create the size of the widget, and the list of sizes for all of
# the widget's children
size = QtCore.QSize(100, 200)
childSizes = [QtCore.QSize(200, 500), QtCore.QSize(300, 400)]

# Get the list of widths and heights for the child widgets
childWidths = map(QtCore.QSize.width, childSizes)
childHeights = map(QtCore.QSize.width, childSizes)

# Both of these return: 100
# Which is the given value of width
ResizePolicy.getSize(ResizePolicy.Fixed, size.width(), childWidths)
ResizePolicy.getWidth(ResizePolicy.Fixed, size, childSizes)

# Both of these return: 900
# Which is the sum of the childHeights list
ResizePolicy.getSize(ResizePolicy.Expanding, size.height(),
                     childHeights)
ResizePolicy.getHeight(ResizePolicy.Fixed, size, childSizes)
classmethod getHeight(policy, size, sizeList)

For the given resize policy, get the correct height for the size of the widget as well as the size of all of the widget’s children.

  • resizePolicy – The resize policy
  • size – The main PyQt4.QtCore.QSize of the widget
  • sizeList – The list of PyQt4.QtCore.QSize for sub widgets
classmethod getSize(policy, size, sizeList)

Get the correct size based on the given resize policy.

  • resizePolicy – The resize policy

  • size – The main single size (width or height) of the widget

  • sizeList – The list of the single dimension size (widths or heights)

    of widgets’s children

classmethod getWidth(policy, size, sizeList)

For the given resize policy, get the correct width for the size of the widget as well as the size of all of the widget’s children.

  • resizePolicy – The resize policy
  • size – The main PyQt4.QtCore.QSize of the widget
  • sizeList – The list of PyQt4.QtCore.QSize for sub widgets

The WidgetList class

class pyamp.ui.qt.WidgetList(name=None, controller=None, mainWindow=None, logData=None, logColor=0)

Bases: pyamp.ui.qt.baseWidget.BaseWidget

The WidgetList class defines a BaseWidget object that has the ability to display a list of widgets vertically one after the other. This class handles resizing the widget appropriately based on the widgets that are in the list.

  • name – The name of the BaseWidget
  • controller – The controller for the BaseWidget
  • mainWindow – The main window
  • logData – The pyamp.logging.LogData object
  • logColor – The color to use for logging
addItem(item)

Add an item to the widget list.

  • item – The item to add
addWidget(widget)

Add a widget to this widget list.

  • widget – The widget to add
clear()

Clear all the widgets from the list.

setHorizontalRezizePolicy(policy)

Set the horizontal resize policy.

  • policy – The new resize policy
setMargin(margin)

Set the margin for this widget list.

  • margin – The margin to use
setVerticalResizePolicy(policy)

Set the vertical resize policy.

  • policy – The new resize policy

The ScrollableList class

class pyamp.ui.qt.ScrollableList(name=None, controller=None, mainWindow=None, logData=None, logColor=0)

Bases: pyamp.ui.qt.baseWidget.BaseWidget

The ScrollableList class creates a BaseWidget that contains a QScrollArea which displays a WidgetList inside of the scroll area. This allows a the user to scroll through the WidgetList to see previously added widgets.

In order to fix the size of the scroll area, subclasses of ScrollableList should set the size class property which is a QSize object.

  • name – The name of the BaseWidget
  • controller – The controller for the BaseWidget
  • mainWindow – The main window
  • logData – The pyamp.logging.LogData object
  • logColor – The color to use for logging
addItem(item)

Add an item to the ScrollabelList widget.

addWidget(widget)

Add a widget to the ScrollableList widget.

clearWidgets()

Clear all of the widgets from the ScrollableList.

createElements()

Override the BaseWidget createElements class.

getScrollArea()

Get the QScrollArea for this widget.

getWidgetList()

Get the WidgetList for this ScrollabeList.

horizontalPolicy

The horizontalPolicy property contains the ScrollBarPolicy for the horizontal scrollbar.

margin

The margin property contains the margin between all items in the list.

scroll(amount)

Scroll the vertical scrollbar by the given amount.

  • amount – The amount to scroll (negative being scrolling up, and

    is positive scrolling down)

scrollTo(y=0)

Scroll to the given position.

  • y – The y position to scroll to
scrollToBottom()

Scroll to the bottom of the list.

scrollToTop()

Scroll to the top of the list.

size

The size property contains the size of the scrollable list.

sizeHint()

Override the QWidget sizeHint function to return the size of this ScrollableList.

verticalPolicy

The verticalPolicy property contains the ScrollBarPolicy for the vertical scrollbar.