2017-02-13 01:21:26 +00:00
|
|
|
# Copyright 2017 Canonical, Ltd.
|
|
|
|
#
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU Affero General Public License as
|
|
|
|
# published by the Free Software Foundation, either version 3 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 Affero General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU Affero General Public License
|
|
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
2017-09-05 02:25:21 +00:00
|
|
|
import logging
|
|
|
|
|
2017-02-13 01:21:26 +00:00
|
|
|
from urwid import (
|
|
|
|
AttrMap,
|
2017-10-05 03:19:27 +00:00
|
|
|
connect_signal,
|
2017-02-13 01:21:26 +00:00
|
|
|
delegate_to_widget_mixin,
|
|
|
|
emit_signal,
|
|
|
|
MetaSignals,
|
|
|
|
Text,
|
|
|
|
WidgetDecoration,
|
|
|
|
WidgetDisable,
|
|
|
|
WidgetWrap,
|
|
|
|
)
|
|
|
|
|
2017-10-09 17:28:21 +00:00
|
|
|
from subiquitycore.i18n import *
|
2017-02-13 01:21:26 +00:00
|
|
|
from subiquitycore.ui.buttons import cancel_btn, done_btn
|
|
|
|
from subiquitycore.ui.container import Columns, Pile
|
2017-03-21 01:39:23 +00:00
|
|
|
from subiquitycore.ui.interactive import (
|
|
|
|
PasswordEditor,
|
|
|
|
IntegerEditor,
|
|
|
|
StringEditor,
|
|
|
|
)
|
2017-09-20 03:20:32 +00:00
|
|
|
from subiquitycore.ui.utils import button_pile, Color
|
2017-02-13 01:21:26 +00:00
|
|
|
|
2017-09-05 02:25:21 +00:00
|
|
|
log = logging.getLogger("subiquitycore.ui.form")
|
|
|
|
|
2017-09-14 23:04:38 +00:00
|
|
|
|
2017-02-13 01:21:26 +00:00
|
|
|
class Toggleable(delegate_to_widget_mixin('_original_widget'), WidgetDecoration):
|
|
|
|
|
2017-09-14 23:04:38 +00:00
|
|
|
def __init__(self, original):
|
|
|
|
if not isinstance(original, AttrMap):
|
|
|
|
raise RuntimeError("Toggleable must be passed an AttrMap, not %s", original)
|
2017-02-13 01:21:26 +00:00
|
|
|
self.original = original
|
|
|
|
self.enabled = False
|
|
|
|
self.enable()
|
|
|
|
|
|
|
|
def enable(self):
|
|
|
|
if not self.enabled:
|
2017-09-14 23:04:38 +00:00
|
|
|
self.original_widget = self.original
|
2017-02-13 01:21:26 +00:00
|
|
|
self.enabled = True
|
|
|
|
|
|
|
|
def disable(self):
|
|
|
|
if self.enabled:
|
2017-09-14 23:04:38 +00:00
|
|
|
self.original_widget = WidgetDisable(Color.info_minor(self.original.original_widget))
|
2017-02-13 01:21:26 +00:00
|
|
|
self.enabled = False
|
|
|
|
|
2017-09-14 23:04:38 +00:00
|
|
|
|
2017-02-13 01:21:26 +00:00
|
|
|
class _Validator(WidgetWrap):
|
|
|
|
|
|
|
|
def __init__(self, field, w):
|
|
|
|
self.field = field
|
|
|
|
super().__init__(w)
|
|
|
|
|
|
|
|
def lost_focus(self):
|
2017-10-05 03:16:31 +00:00
|
|
|
self.field.showing_extra = False
|
|
|
|
lf = getattr(self._w, 'lost_focus', None)
|
|
|
|
if lf is not None:
|
|
|
|
lf()
|
2017-02-13 01:21:26 +00:00
|
|
|
self.field.validate()
|
|
|
|
|
|
|
|
|
|
|
|
class FormField(object):
|
|
|
|
|
|
|
|
next_index = 0
|
|
|
|
|
2017-10-05 02:45:16 +00:00
|
|
|
def __init__(self, caption=None, help=None):
|
2017-02-13 01:21:26 +00:00
|
|
|
self.caption = caption
|
|
|
|
self.help = help
|
|
|
|
self.index = FormField.next_index
|
|
|
|
FormField.next_index += 1
|
|
|
|
|
2017-02-14 02:16:12 +00:00
|
|
|
def _make_widget(self, form):
|
|
|
|
raise NotImplementedError(self._make_widget)
|
|
|
|
|
2017-02-13 01:21:26 +00:00
|
|
|
def bind(self, form):
|
2017-02-14 02:16:12 +00:00
|
|
|
widget = self._make_widget(form)
|
|
|
|
return BoundFormField(self, form, widget)
|
2017-02-13 01:21:26 +00:00
|
|
|
|
|
|
|
|
2017-10-06 02:39:14 +00:00
|
|
|
class WantsToKnowFormField(object):
|
2017-10-05 09:14:44 +00:00
|
|
|
"""A marker class."""
|
2017-10-06 02:39:14 +00:00
|
|
|
def set_bound_form_field(self, bff):
|
|
|
|
self.bff = bff
|
2017-10-05 09:14:44 +00:00
|
|
|
|
2017-02-13 01:21:26 +00:00
|
|
|
class BoundFormField(object):
|
|
|
|
|
2017-02-14 02:16:12 +00:00
|
|
|
def __init__(self, field, form, widget):
|
2017-02-13 01:21:26 +00:00
|
|
|
self.field = field
|
|
|
|
self.form = form
|
|
|
|
self.in_error = False
|
|
|
|
self._help = None
|
2017-11-07 22:59:13 +00:00
|
|
|
self.help_text = Text("", align="center")
|
2017-02-13 01:48:53 +00:00
|
|
|
self._caption = None
|
2017-02-13 01:59:25 +00:00
|
|
|
self.pile = None
|
2017-02-13 02:12:04 +00:00
|
|
|
self._enabled = True
|
2017-02-13 02:50:39 +00:00
|
|
|
self.showing_extra = False
|
2017-02-14 02:16:12 +00:00
|
|
|
self.widget = widget
|
2017-10-05 03:19:27 +00:00
|
|
|
if 'change' in getattr(widget, 'signals', []):
|
|
|
|
connect_signal(widget, 'change', self._change)
|
2017-10-06 02:39:14 +00:00
|
|
|
if isinstance(widget, WantsToKnowFormField):
|
|
|
|
widget.set_bound_form_field(self)
|
2017-02-13 01:21:26 +00:00
|
|
|
|
|
|
|
def clean(self, value):
|
|
|
|
cleaner = getattr(self.form, "clean_" + self.field.name, None)
|
|
|
|
if cleaner is not None:
|
|
|
|
value = cleaner(value)
|
|
|
|
return value
|
|
|
|
|
2017-10-05 03:19:27 +00:00
|
|
|
def _change(self, sender, new_val):
|
|
|
|
if self.in_error:
|
|
|
|
self.showing_extra = False
|
|
|
|
# the validator will likely inspect self.value to decide
|
|
|
|
# if the new input is valid. So self.value had better
|
|
|
|
# return the new value and we stuff it into tmpval to do
|
|
|
|
# this. It's a bit of a hack but oh well...
|
|
|
|
self.tmpval = new_val
|
|
|
|
r = self._validate()
|
|
|
|
del self.tmpval
|
|
|
|
if r is not None:
|
|
|
|
return
|
|
|
|
self.in_error = False
|
|
|
|
if not self.showing_extra:
|
|
|
|
self.help_text.set_text(self.help)
|
|
|
|
self.form.validated()
|
|
|
|
|
2017-02-13 01:21:26 +00:00
|
|
|
def _validate(self):
|
2017-02-13 02:12:04 +00:00
|
|
|
if not self._enabled:
|
2017-02-13 01:59:25 +00:00
|
|
|
return
|
2017-02-13 01:21:26 +00:00
|
|
|
try:
|
2017-10-05 02:45:16 +00:00
|
|
|
self.value
|
2017-02-13 01:21:26 +00:00
|
|
|
except ValueError as e:
|
|
|
|
return str(e)
|
|
|
|
validator = getattr(self.form, "validate_" + self.field.name, None)
|
|
|
|
if validator is not None:
|
|
|
|
return validator()
|
|
|
|
|
|
|
|
def validate(self):
|
2017-10-05 02:05:45 +00:00
|
|
|
# cleaning/validation can call show_extra to add an
|
|
|
|
# informative message. We record this by having show_extra to
|
|
|
|
# set showing_extra so we don't immediately replace this
|
|
|
|
# message with the widget's help in the case that validation
|
|
|
|
# succeeds.
|
2017-02-13 01:21:26 +00:00
|
|
|
r = self._validate()
|
|
|
|
if r is None:
|
|
|
|
self.in_error = False
|
2017-10-05 02:05:45 +00:00
|
|
|
if not self.showing_extra:
|
|
|
|
self.help_text.set_text(self.help)
|
2017-02-13 01:21:26 +00:00
|
|
|
else:
|
|
|
|
self.in_error = True
|
2017-10-05 02:05:45 +00:00
|
|
|
self.show_extra(('info_error', r))
|
2017-02-13 01:21:26 +00:00
|
|
|
self.form.validated()
|
|
|
|
|
2017-10-05 02:05:45 +00:00
|
|
|
def show_extra(self, extra_markup):
|
2017-02-13 02:50:39 +00:00
|
|
|
self.showing_extra = True
|
2017-10-05 02:05:45 +00:00
|
|
|
self.help_text.set_text(extra_markup)
|
2017-02-13 01:21:26 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def value(self):
|
2017-10-05 03:19:27 +00:00
|
|
|
return self.clean(getattr(self, 'tmpval', self.widget.value))
|
2017-02-13 01:21:26 +00:00
|
|
|
|
|
|
|
@value.setter
|
|
|
|
def value(self, val):
|
|
|
|
self.widget.value = val
|
|
|
|
|
|
|
|
@property
|
|
|
|
def help(self):
|
|
|
|
if self._help is not None:
|
|
|
|
return self._help
|
2017-10-05 02:05:45 +00:00
|
|
|
elif self.field.help is not None:
|
2017-02-13 01:21:26 +00:00
|
|
|
return self.field.help
|
2017-10-05 02:05:45 +00:00
|
|
|
else:
|
|
|
|
return ""
|
2017-02-13 01:21:26 +00:00
|
|
|
|
|
|
|
@help.setter
|
|
|
|
def help(self, val):
|
|
|
|
self._help = val
|
2017-10-05 02:05:45 +00:00
|
|
|
if self.pile is not None:
|
2017-10-06 02:39:14 +00:00
|
|
|
self.pile[1][1].set_text(val)
|
2017-02-13 01:21:26 +00:00
|
|
|
|
2017-02-13 01:48:53 +00:00
|
|
|
@property
|
|
|
|
def caption(self):
|
|
|
|
if self._caption is not None:
|
|
|
|
return self._caption
|
|
|
|
else:
|
|
|
|
return self.field.caption
|
|
|
|
|
|
|
|
@caption.setter
|
|
|
|
def caption(self, val):
|
|
|
|
self._caption = val
|
2018-03-14 01:07:03 +00:00
|
|
|
if self.pile:
|
|
|
|
self.pile[0][0].set_text(val)
|
2017-02-13 01:48:53 +00:00
|
|
|
|
2017-02-13 02:50:39 +00:00
|
|
|
def _cols(self):
|
2017-02-13 01:48:53 +00:00
|
|
|
text = Text(self.caption, align="right")
|
2017-02-13 02:12:04 +00:00
|
|
|
if self._enabled:
|
2017-02-13 01:59:25 +00:00
|
|
|
input = Color.string_input(_Validator(self, self.widget))
|
|
|
|
else:
|
|
|
|
input = self.widget
|
2017-02-13 01:21:26 +00:00
|
|
|
cols = [
|
2017-03-12 22:52:19 +00:00
|
|
|
(self._longest_caption, text),
|
2017-10-05 02:05:45 +00:00
|
|
|
input
|
2017-02-13 01:21:26 +00:00
|
|
|
]
|
2017-03-12 22:52:19 +00:00
|
|
|
cols = Columns(cols, dividechars=2)
|
2017-02-13 02:12:04 +00:00
|
|
|
if self._enabled:
|
2017-02-13 01:59:25 +00:00
|
|
|
return cols
|
|
|
|
else:
|
|
|
|
return WidgetDisable(Color.info_minor(cols))
|
|
|
|
|
2017-03-12 22:52:19 +00:00
|
|
|
def as_row(self, view, longest_caption):
|
2017-02-13 01:59:25 +00:00
|
|
|
if self.pile is not None:
|
|
|
|
raise RuntimeError("do not call as_row more than once!")
|
2017-02-12 23:18:48 +00:00
|
|
|
self.parent_view = view
|
2017-03-12 22:52:19 +00:00
|
|
|
self._longest_caption = longest_caption
|
2017-11-07 22:59:13 +00:00
|
|
|
self.help_text.set_text(self.help)
|
2017-10-05 02:05:45 +00:00
|
|
|
cols = [
|
|
|
|
(self._longest_caption, Text("")),
|
|
|
|
self.help_text,
|
|
|
|
]
|
|
|
|
self.pile = Pile([self._cols(), Columns(cols, dividechars=2)])
|
2017-02-13 01:21:26 +00:00
|
|
|
return self.pile
|
|
|
|
|
2017-02-13 02:12:04 +00:00
|
|
|
@property
|
|
|
|
def enabled(self):
|
|
|
|
return self._enabled
|
|
|
|
|
|
|
|
@enabled.setter
|
|
|
|
def enabled(self, val):
|
|
|
|
if val != self._enabled:
|
|
|
|
self._enabled = val
|
2017-03-10 02:46:21 +00:00
|
|
|
if self.pile is not None:
|
|
|
|
self.pile.contents[0] = (self._cols(), self.pile.contents[0][1])
|
2017-02-13 01:59:25 +00:00
|
|
|
|
2017-02-13 01:21:26 +00:00
|
|
|
|
2017-02-13 02:37:29 +00:00
|
|
|
def simple_field(widget_maker):
|
|
|
|
class Field(FormField):
|
2017-02-14 02:16:12 +00:00
|
|
|
def _make_widget(self, form):
|
|
|
|
return widget_maker()
|
2017-02-13 02:37:29 +00:00
|
|
|
return Field
|
2017-02-13 01:48:53 +00:00
|
|
|
|
|
|
|
|
2017-02-14 02:16:12 +00:00
|
|
|
StringField = simple_field(StringEditor)
|
2017-03-21 01:39:23 +00:00
|
|
|
PasswordField = simple_field(PasswordEditor)
|
2017-02-14 02:16:12 +00:00
|
|
|
IntegerField = simple_field(IntegerEditor)
|
2017-02-13 01:48:53 +00:00
|
|
|
|
2017-02-13 01:21:26 +00:00
|
|
|
class MetaForm(MetaSignals):
|
|
|
|
|
|
|
|
def __init__(self, name, bases, attrs):
|
|
|
|
super().__init__(name, bases, attrs)
|
|
|
|
_unbound_fields = []
|
|
|
|
for k, v in attrs.items():
|
|
|
|
if isinstance(v, FormField):
|
|
|
|
v.name = k
|
|
|
|
if v.caption is None:
|
|
|
|
v.caption = k + ":"
|
|
|
|
_unbound_fields.append(v)
|
|
|
|
_unbound_fields.sort(key=lambda f:f.index)
|
|
|
|
self._unbound_fields = _unbound_fields
|
|
|
|
|
|
|
|
|
|
|
|
class Form(object, metaclass=MetaForm):
|
|
|
|
|
|
|
|
signals = ['submit', 'cancel']
|
|
|
|
|
2017-09-15 00:18:50 +00:00
|
|
|
ok_label = _("Done")
|
2018-01-11 23:56:24 +00:00
|
|
|
cancel_label = _("Cancel")
|
2017-02-13 01:21:26 +00:00
|
|
|
|
2017-09-04 23:58:49 +00:00
|
|
|
def __init__(self, initial={}):
|
2017-11-22 00:26:55 +00:00
|
|
|
self.done_btn = Toggleable(done_btn(self.ok_label, on_press=self._click_done))
|
2018-01-11 23:56:24 +00:00
|
|
|
self.cancel_btn = Toggleable(cancel_btn(self.cancel_label, on_press=self._click_cancel))
|
2017-09-20 03:20:32 +00:00
|
|
|
self.buttons = button_pile([self.done_btn, self.cancel_btn])
|
2017-02-13 01:21:26 +00:00
|
|
|
self._fields = []
|
|
|
|
for field in self._unbound_fields:
|
|
|
|
bf = field.bind(self)
|
|
|
|
setattr(self, bf.field.name, bf)
|
|
|
|
self._fields.append(bf)
|
2017-09-04 23:58:49 +00:00
|
|
|
if field.name in initial:
|
|
|
|
bf.value = initial[field.name]
|
2017-11-07 22:59:13 +00:00
|
|
|
for bf in self._fields:
|
|
|
|
bf.validate()
|
|
|
|
self.validated()
|
2017-02-13 01:21:26 +00:00
|
|
|
|
|
|
|
def _click_done(self, sender):
|
|
|
|
emit_signal(self, 'submit', self)
|
|
|
|
|
|
|
|
def _click_cancel(self, sender):
|
|
|
|
emit_signal(self, 'cancel', self)
|
|
|
|
|
2017-03-10 02:46:21 +00:00
|
|
|
def remove_field(self, field_name):
|
|
|
|
new_fields = []
|
|
|
|
for bf in self._fields:
|
|
|
|
if bf.field.name != field_name:
|
|
|
|
new_fields.append(bf)
|
|
|
|
self._fields[:] = new_fields
|
|
|
|
|
2017-03-21 01:39:23 +00:00
|
|
|
@property
|
|
|
|
def longest_caption(self):
|
2017-03-12 22:52:19 +00:00
|
|
|
longest_caption = 0
|
|
|
|
for field in self._fields:
|
|
|
|
longest_caption = max(longest_caption, len(field.caption))
|
2017-03-21 01:39:23 +00:00
|
|
|
return longest_caption
|
|
|
|
|
|
|
|
def as_rows(self, view):
|
|
|
|
longest_caption = self.longest_caption
|
2017-02-13 01:21:26 +00:00
|
|
|
rows = []
|
|
|
|
for field in self._fields:
|
2017-03-12 22:52:19 +00:00
|
|
|
rows.append(field.as_row(view, longest_caption))
|
2017-02-13 01:21:26 +00:00
|
|
|
return Pile(rows)
|
|
|
|
|
|
|
|
def validated(self):
|
|
|
|
in_error = False
|
|
|
|
for f in self._fields:
|
|
|
|
if f.in_error:
|
|
|
|
in_error = True
|
|
|
|
break
|
|
|
|
if in_error:
|
2017-09-20 03:20:32 +00:00
|
|
|
self.buttons.base_widget.contents[0][0].disable()
|
|
|
|
self.buttons.base_widget.focus_position = 1
|
2017-02-13 01:21:26 +00:00
|
|
|
else:
|
2017-09-20 03:20:32 +00:00
|
|
|
self.buttons.base_widget.contents[0][0].enable()
|
2017-09-04 23:58:49 +00:00
|
|
|
|
|
|
|
def as_data(self):
|
|
|
|
data = {}
|
|
|
|
for field in self._fields:
|
|
|
|
data[field.field.name] = field.value
|
|
|
|
return data
|