~ubuntu-branches/ubuntu/karmic/tovid/karmic

« back to all changes in this revision

Viewing changes to libtovid/metagui/__init__.py

  • Committer: Bazaar Package Importer
  • Author(s): Matvey Kozhev
  • Date: 2008-01-24 22:04:40 UTC
  • Revision ID: james.westby@ubuntu.com-20080124220440-x7cheljduf1rdgnq
Tags: upstream-0.31
ImportĀ upstreamĀ versionĀ 0.31

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#! /usr/bin/env python
 
2
# __init__.py (metagui)
 
3
 
 
4
"""A simplified GUI-writing module.
 
5
 
 
6
 
 
7
INTRODUCTION
 
8
 
 
9
This module demonstrates a simplified approach to creating GUIs for
 
10
command-line programs. It's designed so _anyone_ can easily write their
 
11
own GUI, without having any programming experience.
 
12
 
 
13
It assumes your GUI is a direct frontend to one or more command-line programs,
 
14
with each command-line option having an associated GUI control widget. Several
 
15
kinds of widget are provided, for setting Filename, Color, Number or Font, or
 
16
for picking a Choice or Flag.
 
17
 
 
18
You probably know of a handful of command-line applications that would be
 
19
much better with a GUI, even a cheesy-looking Tkinter one. This module makes
 
20
it easy to create one.
 
21
 
 
22
 
 
23
DEFINE CONTROLS
 
24
 
 
25
Say, if you have a program that takes input and output filenames:
 
26
 
 
27
    $ tovid -in movie.avi -out movie_encoded
 
28
 
 
29
then you can create GUI widgets for those options like this:
 
30
 
 
31
    Filename('-in', "Input filename")
 
32
    Filename('-out', "Output prefix")
 
33
 
 
34
These have the general format:
 
35
 
 
36
    Control('-option', "Label", ...)
 
37
 
 
38
where:
 
39
 
 
40
    Control   is a Control subclass, such as Filename, Choice, or Number,
 
41
              describing what type of value is being controlled;
 
42
    'option'  is a command-line option (without the leading '-'),
 
43
              whose value is set by the Control; and
 
44
    "Label"   is the text that should appear next to the GUI Control.
 
45
 
 
46
Other parameters include default value, help/tooltip text to show, allowable
 
47
values, and hints about how to draw the GUI control widget; they are specific
 
48
to the flavor of Control being used. Here's a sampling:
 
49
 
 
50
    Choice    Multiple-choice values
 
51
    Color     Color selection button
 
52
    Filename  Type or [Browse] a filename
 
53
    FileList  Add/remove/rearrange filename list
 
54
    Flag      Check box, yes or no
 
55
    Font      Font selection button
 
56
    List      Space-separated list
 
57
    Number    Number between A and B
 
58
    Text      Plain old text
 
59
 
 
60
For a full list of available Control subclasses and how to use them,
 
61
see libtovid/metagui/control.py.
 
62
 
 
63
 
 
64
CREATING THE GUI
 
65
 
 
66
First, give your Controls a place to live, in a Panel:
 
67
 
 
68
    general = Panel("General",
 
69
        Filename('-bgaudio', "Background audio file"),
 
70
        Flag('-submenus', "Create submenus"),
 
71
        Number('-menu-length', "Length of menu (seconds)", 0, 120)
 
72
        )
 
73
 
 
74
This will create three GUI widgets in a Panel labeled "General": one for typing
 
75
or browsing to a filename, one for enabling or disabling submenus, and another 
 
76
for setting menu length to a number between 0 and 120. You can nest panels
 
77
inside one another for grouping; sub-Panels have their own label and list of
 
78
Controls or sub-Panels.
 
79
 
 
80
Once you have a Panel, you can create an Application:
 
81
 
 
82
    app = Application('todisc', [general])
 
83
 
 
84
This says your application will run the 'todisc' command-line program,
 
85
passing options set by the "General" panel. Now, create the GUI:
 
86
 
 
87
    gui = GUI('MyGUI', [app])
 
88
    gui.run()
 
89
 
 
90
This creates the GUI, draws all the widgets, and will run your command-line
 
91
program at the push of a button.
 
92
 
 
93
 
 
94
CREATE A MULTI-PANEL GUI
 
95
 
 
96
If your program has a lot of options, one panel may not be enough to hold them
 
97
all without looking cluttered, so you may break them down into multiple Panels,
 
98
which will be shown in the GUI as tabs that you can switch between. Create
 
99
Panels like this:
 
100
 
 
101
    thumbs = Panel("Thumbnails",
 
102
        Color('-thumb-mist-color', ...),
 
103
        Text('-wave', ...)
 
104
    )
 
105
    text = Panel("Text",
 
106
        Font('-menu-font', ...),
 
107
        Number('-menu-fontsize', ...)
 
108
    )
 
109
 
 
110
then, create the Application and GUI:
 
111
 
 
112
    todisc = Application('todisc', [thumbs, text])
 
113
    gui = GUI('MyGUI', [todisc])
 
114
    gui.run()
 
115
 
 
116
If multiple panels are given to Application, a tabbed interface is created,
 
117
with one tab for each panel.
 
118
 
 
119
 
 
120
CREATE A MULTI-APPLICATION GUI
 
121
 
 
122
If your GUI needs to be able to run several different command-line programs,
 
123
you can create a multi-application GUI. Create panels for each application,
 
124
then create the applications:
 
125
 
 
126
    todisc = Application('todisc', [todisc_panel1, todisc_panel2])
 
127
    tovid = Application('tovid', [tovid_panel1, tovid_panel2])
 
128
 
 
129
and then the GUI:
 
130
 
 
131
    gui = GUI('MultiGUI', [todisc, tovid])
 
132
    gui.run()
 
133
 
 
134
See the apps/ directory for example GUIs.
 
135
"""
 
136
 
 
137
# Export everything from support, control, and gui modules
 
138
# (Open to suggestion about more elegant ways to do this...)
 
139
from support import *
 
140
from control import *
 
141
from gui import *
 
142
from support import __all__ as all_support
 
143
from control import __all__ as all_control
 
144
from gui import __all__ as all_gui
 
145
__all__ = [\
 
146
    'support',
 
147
    'control',
 
148
    'gui',
 
149
    'manpage',
 
150
    'builder',
 
151
    'tooltip',
 
152
    'widget'] + all_support + all_control + all_gui
 
153
 
 
154