/[thuban]/branches/WIP-pyshapelib-bramz/Thuban/UI/command.py
ViewVC logotype

Annotation of /branches/WIP-pyshapelib-bramz/Thuban/UI/command.py

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2734 - (hide annotations)
Thu Mar 1 12:42:59 2007 UTC (18 years ago) by bramz
File MIME type: text/x-python
File size: 4081 byte(s)
made a copy
1 bh 183 # Copyright (C) 2001, 2002 by Intevation GmbH
2 bh 6 # 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 bh 183 A command is identified by a name, it has a title (used in menu
30     items and buttons, etc) and a callable object that can be invoked
31 bh 221 with the context as a single parameter. The context is an object
32     with a few public attributes for the application object, the session
33     and the main window.
34 bh 6
35     Additionally, a command may have functions that can determine
36     whether the command can be invoked or whether it is checked in case
37     of a toggled command. These functions are called with just the
38     context as parameters.
39     """
40 dpinte 2700
41 bh 6 args = ()
42     kwargs = None
43     sensitive = None
44     checked = None
45     dyntext = None
46    
47     def __init__(self, name, title, function, helptext = "", icon = "",
48     args = (), kwargs = None,
49     sensitive = None, checked = None, dyntext = None):
50     self.name = name
51     self.title = title
52     self.function = function
53     self.helptext = helptext
54     self.icon = icon
55     if args != ():
56     if type(args) != TupleType:
57     args = (args,)
58     self.args = args
59     if kwargs is not None:
60     self.kwargs = kwargs
61     if sensitive is not None:
62     self.sensitive = sensitive
63     if checked is not None:
64     self.checked = checked
65     if dyntext is not None:
66     self.dyntext = dyntext
67    
68     def Name(self):
69     return self.name
70    
71     def Title(self):
72     return self.title
73    
74     def HelpText(self):
75     return self.helptext
76    
77     def Icon(self):
78     return self.icon
79    
80     def Sensitive(self, context):
81     if self.sensitive is not None:
82     return self.sensitive(context)
83     return 1
84    
85     def Checked(self, context):
86     if self.checked is not None:
87     return self.checked(context)
88     return 0 # XXX raise an exception?
89    
90     def IsCheckCommand(self):
91     return self.checked is not None
92    
93     def DynText(self, context):
94     if self.dyntext is not None:
95     return self.dyntext(context)
96     return self.Title()
97    
98     def HasDynText(self):
99     return self.dyntext is not None
100    
101     def IsDynamic(self):
102     """Return true if the command is in any way dynamic"""
103     return (self.sensitive is not None
104     or self.checked is not None
105     or self.dyntext is not None)
106    
107 bh 355 def IsTool(self):
108     """Return whether the command represents a tool.
109    
110     This default implementation always returns 0.
111     """
112     return 0
113    
114 bh 6 def Execute(self, context, args = ()):
115     kw = self.kwargs
116     if kw is None:
117     kw = {}
118     if type(args) != TupleType:
119     args = (args,)
120     #print self.name, self.args, args
121     apply(self.function, (context,) + self.args + args, kw)
122    
123 bh 355 class ToolCommand(Command):
124 bh 6
125 bh 355 """A command tool activating a tool"""
126    
127     def IsTool(self):
128     """Return whether the command represents a tool, i.e. always 1"""
129     return 1
130    
131    
132 bh 6 class CommandRegistry:
133    
134     """
135     A CommandRegistry maps command names to command objects
136     """
137    
138     def __init__(self):
139     self.registry = {}
140    
141     def Add(self, script):
142     self.registry[script.name] = script
143    
144     def AddFunction(self, name, title, function, args = (), sensitive = None):
145     self.Add(Command(name, title, function, args = args,
146     sensitive = sensitive))
147    
148     def Command(self, name):
149     return self.registry.get(name, None)
150    
151    
152    
153     # The central command registry
154     registry = CommandRegistry()

Properties

Name Value
svn:eol-style native
svn:keywords Author Date Id Revision

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26