Moved interesting old vclamp commands over into the curve plugin
[hooke.git] / hooke / interaction.py
1 # Copyright (C) 2010 W. Trevor King <wking@drexel.edu>
2 #
3 # This file is part of Hooke.
4 #
5 # Hooke is free software: you can redistribute it and/or modify it
6 # under the terms of the GNU Lesser General Public License as
7 # published by the Free Software Foundation, either version 3 of the
8 # License, or (at your option) any later version.
9 #
10 # Hooke is distributed in the hope that it will be useful, but WITHOUT
11 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 # or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General
13 # Public License for more details.
14 #
15 # You should have received a copy of the GNU Lesser General Public
16 # License along with Hooke.  If not, see
17 # <http://www.gnu.org/licenses/>.
18
19 """The `interaction` module provides :class:`Request`,
20 :class:`Response`, an related classes for handling user interaction
21 through the
22 :class:`hooke.engine.CommandEngine`/:class:`hooke.ui.UserInterface`
23 connection.
24 """
25
26
27 class InList (list):
28     """:class:`Request` validator class.
29
30     Examples
31     --------
32
33     >>> i = InList(['abc', 'def', 5, True])
34     >>> i('abc')
35     >>> i(5)
36     >>> i(False)
37     Traceback (most recent call last):
38       ...
39     ValueError: False
40     """
41     def __init__(self, *args, **kwargs):
42         list.__init__(self, *args, **kwargs)
43
44     def __call__(self, value):
45         """Raises ValueError if a given `value` is not in our internal
46         list.
47         """
48         if value not in self:
49             raise ValueError(value)
50
51 class Interaction (object):
52     """Mid-command inter-process interaction.
53
54     Stores :attr:`type`, a string representing the interaction type
55     ('boolean', 'string', ...).
56     """
57     def __init__(self, type):
58         self.type = type
59
60 class Request (Interaction):
61     """Command engine requests for information from the UI.
62
63     >>> r = Request('test', 'Does response_class work?')
64     >>> r.response_class()    
65     <class 'hooke.interaction.Response'>
66     """
67     def __init__(self, type, msg, default=None, validator=None):
68         super(Request, self).__init__(type)
69         self.msg = msg
70         self.default = default
71         self.validator = validator
72
73     def response_class(self):
74         class_name = self.__class__.__name__.replace('Request', 'Response')
75         return globals()[class_name]
76
77     def response(self, value):
78         if self.validator != None:
79             self.validator(value)
80         return self.response_class()(value)
81
82 class Response (Interaction):
83     """UI response to a :class:`Request`.
84     """
85     def __init__(self, type, value):
86         super(Response, self).__init__(type)
87         self.value = value
88
89 class BooleanRequest (Request):
90     def __init__(self, msg, default=None):
91         super(BooleanRequest, self).__init__(
92             'boolean', msg, default,
93             validator = InList([True, False, default]))
94
95 class BooleanResponse (Response):
96     def __init__(self, value):
97         super(BooleanResponse, self).__init__('boolean', value)
98
99 class StringRequest (Request):
100     def __init__(self, msg, default=None):
101         super(StringRequest, self).__init__('string', msg, default)
102
103 class StringResponse (Response):
104     def __init__(self, value):
105         super(StringResponse, self).__init__('string', value)
106
107 class FloatRequest (Request):
108     def __init__(self, msg, default=None):
109         super(FloatRequest, self).__init__('float', msg, default)
110
111 class FloatResponse (Response):
112     def __init__(self, value):
113         super(FloatResponse, self).__init__('float', value)
114
115 class SelectionRequest (Request):
116     def __init__(self, msg, default=None, options=[]):
117         super(SelectionRequest, self).__init__('selection', msg, default)
118         self.options = options
119
120 class SelectionResponse (Response):
121     def __init__(self, value):
122         super(SelectionResponse, self).__init__('selection', value)
123
124 class PointRequest (Request):
125     def __init__(self, msg, curve, block=0, default=None):
126         super(PointRequest, self).__init__('point', msg, default)
127         self.curve = curve
128         self.block = block
129
130 class PointResponse (Response):
131     def __init__(self, value):
132         super(PointResponse, self).__init__('point', value)
133
134
135 class Notification (object):
136     def __init__(self, type):
137         self.type = type
138
139 class ReloadUserInterfaceConfig (Notification):
140     def __init__(self, config):
141         super(ReloadUserInterfaceConfig, self).__init__(
142             'reload user interface config')
143         self.config = config