aboutsummaryrefslogtreecommitdiff
path: root/src/gui/tasks/form/widget.py
blob: adaf757a24c60010bbba3a59547327f5edd8a7aa (plain)
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
from PyQt5 import QtWidgets, QtCore
from typing import Optional, Tuple, List, Any

import db.tasks
from model.task import Task, ValidTaskForm
from model import difficulty, priority
import gui.icon
import gui.tasks.form.state
import gui.tasks.duration

class TextEditSignal(QtCore.QObject):
    _signal = QtCore.pyqtSignal(str, name = 'textEdit')

    def __init__(self):
        QtCore.QObject.__init__(self)

    def emit(self, text: str):
        self._signal.emit(text)

    def connect(self, f):
        self._signal.connect(f)

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)

    grid = QtWidgets.QWidget(widget)
    layout.addWidget(grid)
    grid_layout = QtWidgets.QGridLayout(grid)
    grid.setLayout(grid_layout)

    init_name = task.name if task is not None else ''
    name_input = line_edit(grid, grid_layout, 0, 'Name', init_name)

    init_duration = gui.tasks.duration.format(task.duration) if task is not None else ''
    duration_input = line_edit(grid, grid_layout, 1, 'Duration', init_duration)

    init_tag = task.tag if task is not None else ''
    tag_input = line_edit(grid, grid_layout, 2, 'Tag', init_tag)

    init_difficulty = task.difficulty if task is not None else difficulty.Difficulty.NORMAL
    difficulty_input = combo_box(
            grid,
            grid_layout,
            3,
            'Difficulty',
            [difficulty.format(d) for d in difficulty.values],
            int(init_difficulty))

    init_priority = task.priority if task is not None else priority.Priority.MIDDLE
    priority_input = combo_box(
            grid,
            grid_layout,
            4,
            'Priority',
            [priority.format(d) for d in priority.values],
            int(init_priority))

    init_description = task.description if task is not None else ''
    (description_input, description_signal) = text_edit(widget, 'Description', init_description)
    layout.addWidget(description_input)

    task_form_edition = gui.tasks.form.state.TaskFormEdition(
            init_name,
            name_input.textChanged,
            init_duration,
            duration_input.textChanged,
            init_tag,
            tag_input.textChanged,
            init_difficulty,
            difficulty_input.currentIndexChanged,
            init_priority,
            priority_input.currentIndexChanged,
            init_description,
            description_signal)

    def on_validate():
        form = task_form_edition.get()
        if form:
            on_validated(form)

    layout.addWidget(buttons(
            parent = widget,
            action_title = action_title,
            task_form_signal = task_form_edition.signal(),
            on_validate = on_validate,
            on_cancel = on_cancel))

    return widget

def line_edit(
        parent,
        layout: QtWidgets.QGridLayout,
        n: int,
        label: str,
        default_value: str) -> QtWidgets.QLineEdit:

    label = QtWidgets.QLabel(label, parent)
    layout.addWidget(label, n, 0)

    edit = QtWidgets.QLineEdit(parent)
    if default_value != None:
        edit.setText(default_value)
    layout.addWidget(edit, n, 1)

    return edit

def combo_box(
        parent,
        layout: QtWidgets.QGridLayout,
        n: int,
        label: str,
        values: List[str],
        default_value: int) -> QtWidgets.QComboBox:

    label = QtWidgets.QLabel(label, parent)
    layout.addWidget(label, n, 0)

    box = QtWidgets.QComboBox(parent)
    for value in values:
        box.addItem(value)
    if default_value != None:
        box.setCurrentIndex(default_value)
    layout.addWidget(box, n, 1)

    return box

def text_edit(
        parent,
        label: str,
        default_value: str) -> Tuple[QtWidgets.QWidget, TextEditSignal]:

    widget = QtWidgets.QWidget(parent)
    layout = QtWidgets.QVBoxLayout(widget)

    signal = TextEditSignal()

    label = QtWidgets.QLabel(label, parent)
    layout.addWidget(label)

    edit = QtWidgets.QTextEdit(parent)
    if default_value != None:
        edit.insertPlainText(default_value)
    layout.addWidget(edit)

    edit.textChanged.connect(lambda: signal.emit(edit.toPlainText()))

    return (widget, signal)

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.icon.dialog_apply(validate.style()))
    validate.clicked.connect(on_validate);
    layout.addWidget(validate)

    def on_task_form_signal(form: Optional[ValidTaskForm]):
        if form:
            validate.setEnabled(True)
        else:
            validate.setDisabled(True)

    task_form_signal.connect(on_task_form_signal)

    cancel = QtWidgets.QPushButton('cancel', widget)
    cancel.setIcon(gui.icon.dialog_cancel(cancel.style()))
    cancel.clicked.connect(on_cancel)
    layout.addWidget(cancel)

    return widget