-
Notifications
You must be signed in to change notification settings - Fork 0
/
field_html.py
213 lines (161 loc) · 8.54 KB
/
field_html.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
# -*- coding: utf-8 -*-
# field_tk.py
# Copyright (C) 2007-2008 Jean-Baptiste LAMY -- [email protected]
#
# 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, see <http://www.gnu.org/licenses/>.
from editobj2.field import *
from editobj2.field import _RangeField, _ShortEnumField, _LongEnumField
from editobj2.html_id import *
class HtmlField(MultiGUIField):
def get_edition(self): return self.master.get_edition()
def get_html(self): pass
def set_html_value(self, value):
self.set_value(value)
class HtmlLabelField(HtmlField, LabelField):
def __init__(self, gui, master, o, attr, undo_stack):
super(HtmlLabelField, self).__init__(gui, master, o, attr, undo_stack)
def get_html(self):
return html_escape(unicode(self.get_value()))
class HtmlEntryField(HtmlField, EntryField):
def __init__(self, gui, master, o, attr, undo_stack):
super(HtmlEntryField, self).__init__(gui, master, o, attr, undo_stack)
def get_html(self):
return u"""<input name="%s.%s" type="text" value="%s"/>""" % (self.get_edition().obj2id(self.o), html_escape(self.attr), html_escape_arg(unicode(self.get_value())))
class HtmlFloatField (HtmlEntryField, FloatField):
def set_html_value(self, value):
HtmlField.set_value(self, float(value))
class HtmlIntField (HtmlEntryField, IntField):
def set_html_value(self, value):
HtmlField.set_value(self, int(value))
class HtmlStringField(HtmlEntryField, StringField):
def set_html_value(self, value):
HtmlEntryField.set_value(self, value)
def set_html_value(self, value):
self.set_value(unicode(value, "utf8"))
class HtmlPasswordField(HtmlStringField, PasswordField):
def get_html(self):
return u"""<input name="%s.%s" type="password" value="%s"/>""" % (self.get_edition().obj2id(self.o), html_escape(self.attr), html_escape_arg(self.getvalue()))
class HtmlEditButtonField(HtmlField, EditButtonField):
def __init__(self, gui, master, o, attr, undo_stack):
Tkinter.Button.__init__(self, master, text = editobj2.TRANSLATOR(u"Edit..."), command = self.on_click)
super(HtmlEditButtonField, self).__init__(gui, master, o, attr, undo_stack)
self.update()
def update(self):
self.updating = 1
try:
if self.get_value() is None: self.configure(state = "disabled")
else: self.configure(state = "normal")
finally: self.updating = 0
class HtmlWithButtonStringField(HtmlField, WithButtonStringField):
def __init__(self, gui, master, o, attr, undo_stack):
Tkinter.Frame.__init__(self, master)
super(HtmlWithButtonStringField, self).__init__(gui, master, o, attr, undo_stack)
self.string_field.pack(expand = 1, fill = Htmlinter.BOTH, side = Htmlinter.LEFT)
button = Htmlinter.Button(self, text = editobj2.TRANSLATOR(self.button_text))
button.bind("<ButtonRelease>", self.on_button)
button.pack(expand = 0, fill = Htmlinter.BOTH, side = Htmlinter.RIGHT)
class HtmlFilenameField(HtmlWithButtonStringField, FilenameField):
def on_button(self, *args):
filename = tkFileDialog.askopenfilename()
if filename:
self.string_field.set_value(filename)
self.string_field.update()
class HtmlDirnameField(HtmlWithButtonStringField, DirnameField):
def on_button(self, *args):
folder = tkFileDialog.askdirectory()
if folder:
self.string_field.set_value(folder)
self.string_field.update()
class HtmlURLField(HtmlStringField):
pass
# class HtmlBoolField(HtmlField, BoolField):
# def __init__(self, gui, master, o, attr, undo_stack):
# super(HtmlBoolField, self).__init__(gui, master, o, attr, undo_stack)
# def get_html(self):
# if self.get_value(): yes_checked = u'checked="on"'; no_checked = ""
# else: no_checked = u'checked="on"'; yes_checked = ""
# html = u"""<input name="%s.%s" type="radio" value="1" %s/>%s""" % (self.get_edition().obj2id(self.o), html_escape(self.attr), yes_checked, editobj2.TRANSLATOR("Yes"))
# html += u""" """ * 4
# html += u"""<input name="%s.%s" type="radio" value="0" %s/>%s""" % (self.get_edition().obj2id(self.o), html_escape(self.attr), no_checked , editobj2.TRANSLATOR("No" ))
# return html
# def set_value(self, value):
# if value == "1": HtmlField.set_value(self, True)
# else: HtmlField.set_value(self, False)
class HtmlBoolField(HtmlField, BoolField):
def __init__(self, gui, master, o, attr, undo_stack):
super(HtmlBoolField, self).__init__(gui, master, o, attr, undo_stack)
def get_html(self):
if self.get_value(): checked = 'checked="on"'
else: checked = ""
return u"""
<input name="%s.%s" type="checkbox" %s/>
<input name="%s.%s" type="hidden" value="off"/>
""" % (self.get_edition().obj2id(self.o), html_escape(self.attr), checked, self.get_edition().obj2id(self.o), html_escape(self.attr))
class HtmlProgressBarField(HtmlField, ProgressBarField):
def __init__(self, gui, master, o, attr, undo_stack):
Htmlinter.Label.__init__(self, master)
super(HtmlProgressBarField, self).__init__(gui, master, o, attr, undo_stack)
self.update()
def update(self):
self.updating = 1
try:
v = self.get_value()
if v is introsp.NonConsistent: self["text"] = "???"
else: self["text"] = "%s%%" % int(100 * v)
finally: self.updating = 0
class HtmlTextField(HtmlField, TextField):
def __init__(self, gui, master, o, attr, undo_stack):
super(HtmlTextField, self).__init__(gui, master, o, attr, undo_stack)
def get_html(self):
nb_rows = 2
for ligne in self.get_value().split("\n"):
nb_rows += (len(ligne) // 40) + 1
return u"""<textarea name="%s.%s" rows="%s" cols="40">%s</textarea>""" % (self.get_edition().obj2id(self.o), html_escape(self.attr), nb_rows, html_escape(unicode(self.get_value())))
def set_html_value(self, value):
self.set_value(unicode(value, "utf8"))
class HtmlObjectAttributeField(HtmlField, ObjectAttributeField):
def __init__(self, gui, master, o, attr, undo_stack):
Htmlinter.Frame.__init__(self, master)
super(HtmlObjectAttributeField, self).__init__(gui, master, o, attr, undo_stack)
self.attribute_pane.pack(expand = 1, fill = Htmlinter.BOTH)
self.attribute_pane["relief"] = "sunken"
self.attribute_pane["borderwidth"] = 1
class Html_RangeField(HtmlField, _RangeField):
def __init__(self, gui, master, o, attr, undo_stack, min, max, incr = 1):
super(Html_RangeField, self).__init__(gui, master, o, attr, undo_stack, min, max, incr)
if isinstance(min, int) and isinstance(max, int): self.number_type = int
else: self.number_type = float
def get_html(self):
return u"""<input name="%s.%s" type="text" value="%s"/>""" % (self.get_edition().obj2id(self.o), html_escape(self.attr), html_escape_arg(unicode(self.get_value())))
def set_html_value(self, value):
HtmlField.set_value(self, self.number_type(value))
class Html_ShortEnumField(HtmlField, _ShortEnumField):
def __init__(self, gui, master, o, attr, undo_stack, choices, value_2_enum = None, enum_2_value = None):
super(Html_ShortEnumField, self).__init__(gui, master, o, attr, undo_stack, choices, value_2_enum, enum_2_value)
def get_html(self):
selected_index = self.get_html_value()
html = u"""<select name="%s.%s">\n""" % (self.get_edition().obj2id(self.o), html_escape(self.attr))
items = self.choices.items()
items.sort()
for k, v in items:
index = self.choice_2_index[v]
if index == selected_index: html += u"""<option value="%s" selected="on">%s</option>\n""" % (index, html_escape(unicode(k)))
else: html += u"""<option value="%s">%s</option>\n""" % (index, html_escape(unicode(k)))
html += u"""</select>\n"""
return html
def get_html_value(self):
return self.choice_2_index[self.get_value()]
def set_html_value(self, value):
self.set_value(self.choices[self.choice_keys[int(value)]])
Html_LongEnumField = Html_ShortEnumField