1 |
bh |
6 |
# Copyright (C) 2001 by Intevation GmbH |
2 |
|
|
# Authors: |
3 |
|
|
# Bernhard Herzog <[email protected]> |
4 |
|
|
# |
5 |
|
|
# This program is free software under the GPL (>=v2) |
6 |
|
|
# Read the file COPYING coming with Thuban for details. |
7 |
|
|
|
8 |
|
|
""" |
9 |
|
|
Command Objects. |
10 |
|
|
|
11 |
|
|
Command objects represent a command that a user can invoke and act as |
12 |
|
|
mediators between the GUI and the application. |
13 |
|
|
|
14 |
|
|
This module also defines a command registry that maps command names to |
15 |
|
|
command objects. |
16 |
|
|
""" |
17 |
|
|
|
18 |
|
|
__version__ = "$Revision$" |
19 |
|
|
|
20 |
|
|
|
21 |
|
|
from types import TupleType |
22 |
|
|
|
23 |
|
|
|
24 |
|
|
class Command: |
25 |
|
|
|
26 |
|
|
""" |
27 |
|
|
Represent a single command. |
28 |
|
|
|
29 |
|
|
A is identified by a name, it has a title (used in menu items and |
30 |
|
|
buttons, etc) and a callable object that can be invoked with the |
31 |
|
|
context as a single parameter. The context is currently the |
32 |
|
|
mainwindow object. |
33 |
|
|
|
34 |
|
|
Additionally, a command may have functions that can determine |
35 |
|
|
whether the command can be invoked or whether it is checked in case |
36 |
|
|
of a toggled command. These functions are called with just the |
37 |
|
|
context as parameters. |
38 |
|
|
""" |
39 |
|
|
|
40 |
|
|
args = () |
41 |
|
|
kwargs = None |
42 |
|
|
sensitive = None |
43 |
|
|
checked = None |
44 |
|
|
dyntext = None |
45 |
|
|
|
46 |
|
|
def __init__(self, name, title, function, helptext = "", icon = "", |
47 |
|
|
args = (), kwargs = None, |
48 |
|
|
sensitive = None, checked = None, dyntext = None): |
49 |
|
|
self.name = name |
50 |
|
|
self.title = title |
51 |
|
|
self.function = function |
52 |
|
|
self.helptext = helptext |
53 |
|
|
self.icon = icon |
54 |
|
|
if args != (): |
55 |
|
|
if type(args) != TupleType: |
56 |
|
|
args = (args,) |
57 |
|
|
self.args = args |
58 |
|
|
if kwargs is not None: |
59 |
|
|
self.kwargs = kwargs |
60 |
|
|
if sensitive is not None: |
61 |
|
|
self.sensitive = sensitive |
62 |
|
|
if checked is not None: |
63 |
|
|
self.checked = checked |
64 |
|
|
if dyntext is not None: |
65 |
|
|
self.dyntext = dyntext |
66 |
|
|
|
67 |
|
|
def Name(self): |
68 |
|
|
return self.name |
69 |
|
|
|
70 |
|
|
def Title(self): |
71 |
|
|
return self.title |
72 |
|
|
|
73 |
|
|
def HelpText(self): |
74 |
|
|
return self.helptext |
75 |
|
|
|
76 |
|
|
def Icon(self): |
77 |
|
|
return self.icon |
78 |
|
|
|
79 |
|
|
def Sensitive(self, context): |
80 |
|
|
if self.sensitive is not None: |
81 |
|
|
return self.sensitive(context) |
82 |
|
|
return 1 |
83 |
|
|
|
84 |
|
|
def Checked(self, context): |
85 |
|
|
if self.checked is not None: |
86 |
|
|
return self.checked(context) |
87 |
|
|
return 0 # XXX raise an exception? |
88 |
|
|
|
89 |
|
|
def IsCheckCommand(self): |
90 |
|
|
return self.checked is not None |
91 |
|
|
|
92 |
|
|
def DynText(self, context): |
93 |
|
|
if self.dyntext is not None: |
94 |
|
|
return self.dyntext(context) |
95 |
|
|
return self.Title() |
96 |
|
|
|
97 |
|
|
def HasDynText(self): |
98 |
|
|
return self.dyntext is not None |
99 |
|
|
|
100 |
|
|
def IsDynamic(self): |
101 |
|
|
"""Return true if the command is in any way dynamic""" |
102 |
|
|
return (self.sensitive is not None |
103 |
|
|
or self.checked is not None |
104 |
|
|
or self.dyntext is not None) |
105 |
|
|
|
106 |
|
|
def Execute(self, context, args = ()): |
107 |
|
|
kw = self.kwargs |
108 |
|
|
if kw is None: |
109 |
|
|
kw = {} |
110 |
|
|
if type(args) != TupleType: |
111 |
|
|
args = (args,) |
112 |
|
|
#print self.name, self.args, args |
113 |
|
|
apply(self.function, (context,) + self.args + args, kw) |
114 |
|
|
|
115 |
|
|
|
116 |
|
|
class CommandRegistry: |
117 |
|
|
|
118 |
|
|
""" |
119 |
|
|
A CommandRegistry maps command names to command objects |
120 |
|
|
""" |
121 |
|
|
|
122 |
|
|
def __init__(self): |
123 |
|
|
self.registry = {} |
124 |
|
|
|
125 |
|
|
def Add(self, script): |
126 |
|
|
self.registry[script.name] = script |
127 |
|
|
|
128 |
|
|
def AddFunction(self, name, title, function, args = (), sensitive = None): |
129 |
|
|
self.Add(Command(name, title, function, args = args, |
130 |
|
|
sensitive = sensitive)) |
131 |
|
|
|
132 |
|
|
def Command(self, name): |
133 |
|
|
return self.registry.get(name, None) |
134 |
|
|
|
135 |
|
|
|
136 |
|
|
|
137 |
|
|
# The central command registry |
138 |
|
|
registry = CommandRegistry() |