aboutsummaryrefslogtreecommitdiff
path: root/src/gui/tasks/form/state.py
blob: 727bedd68aa0db51a180712d7ab9ee81d1753f2b (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
import re
from PyQt5 import QtCore
from typing import NamedTuple, Optional

from model.task import ValidTaskForm
from model.difficulty import Difficulty
from model.priority import Priority
import gui.tasks.duration

class TaskFormEdition:
    def __init__(
            self,
            name,
            name_signal,
            duration,
            duration_signal,
            tag,
            tag_signal,
            difficulty,
            difficulty_signal,
            priority,
            priority_signal,
            description,
            description_signal):

        self._name = name
        self._duration = duration
        self._tag = tag
        self._difficulty = difficulty
        self._priority = priority
        self._description = description
        self._signal = ValidTaskFormSignal()

        name_signal.connect(lambda n: self.on_name_signal(n))
        duration_signal.connect(lambda d: self.on_duration_signal(d))
        tag_signal.connect(lambda t: self.on_tag_signal(t))
        difficulty_signal.connect(lambda d: self.on_difficulty_signal(d))
        priority_signal.connect(lambda p: self.on_priority_signal(p))
        description_signal.connect(lambda d: self.on_description_signal(d))

    def get(self) -> Optional[ValidTaskForm]:
        name = self._name.strip()
        duration = gui.tasks.duration.parse(self._duration)
        tag = self._tag.strip()
        difficulty = self._difficulty
        priority = self._priority
        description = self._description.strip()

        if name and duration != None:
            return ValidTaskForm(
                name = name,
                duration = duration,
                tag = tag,
                difficulty = difficulty,
                priority = priority,
                description = description)
        else:
            return None

    def on_name_signal(self, name: str):
        self._name = name
        self.emit()

    def on_duration_signal(self, duration: str):
        self._duration = duration
        self.emit()

    def on_tag_signal(self, tag: str):
        self._tag = tag
        self.emit()

    def on_difficulty_signal(self, index: int):
        self._difficulty = Difficulty(index)
        self.emit()

    def on_priority_signal(self, index: int):
        self._priority = Priority(index)
        self.emit()

    def on_description_signal(self, description: str):
        self._description = description
        self.emit()

    def emit(self):
        validForm = self.get()
        if validForm:
            self._signal.emit(validForm)

    def signal(self):
        return self._signal

class ValidTaskFormSignal(QtCore.QObject):
    _signal = QtCore.pyqtSignal(ValidTaskForm, name = 'validTaskForm')

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

    def emit(self, form: Optional[ValidTaskForm]):
        self._signal.emit(form)

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