aboutsummaryrefslogtreecommitdiff
path: root/todo/gui/tags/panel/form/widget.py
blob: 1b1744d7de242c25df8e16bd8c30a221b22b2abb (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
from PyQt5 import QtWidgets, QtCore, QtGui
from typing import Optional, Tuple, List, Any

from todo.model.tag import Tag, ValidTagForm
import todo.gui.icon
import todo.gui.tags.panel.form.state
import todo.gui.color
from todo.util.gui.color_input import ColorInput

def widget(
        parent: QtWidgets.QWidget,
        action_title: str,
        tag: Tag,
        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 = tag.name if tag is not None else ""
    name_input = line_edit(grid, grid_layout, 0, "Nom", init_name)

    init_color = tag.color if tag is not None else "#FFFFFF"
    color_input = color_edit(grid, grid_layout, 1, "Couleur", QtGui.QColor(init_color))

    tag_form_edition = todo.gui.tags.panel.form.state.TagFormEdition(
            init_name,
            name_input.textChanged,
            init_color,
            color_input.textChanged)

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

    layout.addWidget(buttons(
            parent = widget,
            action_title = action_title,
            tag_form_signal = tag_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 color_edit(
        parent,
        layout: QtWidgets.QGridLayout,
        n: int,
        label: str,
        init_color: QtGui.QColor) -> QtWidgets.QLineEdit:

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

    edit = ColorInput(init_color, parent)
    layout.addWidget(edit, n, 1)

    return edit

def buttons(parent, action_title, tag_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(todo.gui.icon.dialog_ok(validate.style()))
    validate.clicked.connect(on_validate);
    layout.addWidget(validate)

    def on_tag_form_signal(form: Optional[ValidTagForm]):
        if form:
            validate.setEnabled(True)
        else:
            validate.setDisabled(True)

    tag_form_signal.connect(on_tag_form_signal)

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

    return widget