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
|
from PyQt5 import QtWidgets
import db.tasks
from model.task import Task, TaskForm
import gui.icons
import gui.tasks.form.state
def widget(
parent: QtWidgets.QWidget,
action_title: str,
task: Task,
on_validated,
on_cancel):
widget = QtWidgets.QWidget(parent)
layout = QtWidgets.QVBoxLayout(widget)
widget.setLayout(layout)
init_name = task.name if task is not None else ''
(name_labelled_input, name_input) = labelled_input(widget, 'Name', init_name)
layout.addWidget(name_labelled_input)
init_tag = task.tag if task is not None else ''
(tag_labelled_input, tag_input) = labelled_input(widget, 'Tag', init_tag)
layout.addWidget(tag_labelled_input)
task_form_edition = gui.tasks.form.state.TaskFormEdition(
init_name,
name_input.textChanged,
init_tag,
tag_input.textChanged)
layout.addWidget(buttons(
parent = widget,
action_title = action_title,
task_form_signal = task_form_edition.signal(),
on_validate = lambda: validate(task_form_edition.get(), on_validated),
on_cancel = on_cancel))
return widget
# Use grid ?
def labelled_input(parent, label: str, default_value: str):
widget = QtWidgets.QWidget(parent)
layout = QtWidgets.QHBoxLayout(widget)
widget.setLayout(layout)
label = QtWidgets.QLabel(label, widget)
layout.addWidget(label)
line_edit = QtWidgets.QLineEdit(widget)
if default_value != None:
line_edit.setText(default_value)
layout.addWidget(line_edit)
return (widget, line_edit)
def buttons(parent, action_title, task_form_signal, on_validate, on_cancel):
widget = QtWidgets.QWidget(parent)
layout = QtWidgets.QHBoxLayout(widget)
validate = QtWidgets.QPushButton(action_title, widget)
validate.setDisabled(True)
validate.setIcon(gui.icons.dialog_apply(validate.style()))
validate.clicked.connect(on_validate);
layout.addWidget(validate)
def on_task_form_signal(task_form):
if validate_form(task_form):
validate.setEnabled(True)
else:
validate.setDisabled(True)
task_form_signal.connect(on_task_form_signal)
cancel = QtWidgets.QPushButton('cancel', widget)
cancel.setIcon(gui.icons.dialog_cancel(cancel.style()))
cancel.clicked.connect(on_cancel)
layout.addWidget(cancel)
return widget
def validate(task_form: TaskForm, on_validated):
valid_form = validate_form(task_form)
if valid_form:
on_validated(valid_form)
def clean_form(task_form: TaskForm):
return TaskForm(
name = task_form.name.strip(),
tag = task_form.tag.strip())
def validate_form(task_form: TaskForm):
task_form = clean_form(task_form)
if task_form.name:
return task_form
else:
return None
|