From 6ed4e669ef7cb857c7b0ac774c41e8f9c7758217 Mon Sep 17 00:00:00 2001 From: Joris Date: Fri, 8 May 2020 19:35:08 +0200 Subject: Add duration to tasks --- src/gui/tasks/form/state.py | 46 +++++++++++++++++++++++++++++++------------- src/gui/tasks/form/widget.py | 33 +++++++++++++------------------ 2 files changed, 46 insertions(+), 33 deletions(-) (limited to 'src/gui/tasks/form') diff --git a/src/gui/tasks/form/state.py b/src/gui/tasks/form/state.py index 6aedb95..a47aec7 100644 --- a/src/gui/tasks/form/state.py +++ b/src/gui/tasks/form/state.py @@ -1,39 +1,59 @@ from PyQt5 import QtCore +from typing import NamedTuple, Optional -from model.task import TaskForm +from model.task import ValidTaskForm class TaskFormEdition: - def __init__(self, name, name_signal, tag, tag_signal): + def __init__(self, name, name_signal, duration, duration_signal, tag, tag_signal): self._name = name + self._duration = duration self._tag = tag - self._signal = TaskFormSignal() + self._signal = ValidTaskFormSignal() name_signal.connect(lambda name: self.on_name_signal(name)) + duration_signal.connect(lambda duration: self.on_duration_signal(duration)) tag_signal.connect(lambda tag: self.on_tag_signal(tag)) - def get(self): - return TaskForm( - name = self._name, - tag = self._tag) + def get(self) -> Optional[ValidTaskForm]: + name = self._name.strip() + duration = self._duration.strip() + tag = self._tag.strip() + + if name and (duration == '' or duration.isdigit()): + return ValidTaskForm( + name = name, + duration = 0 if duration == '' else int(duration), + tag = tag) + else: + return None def on_name_signal(self, name: str): self._name = name - self._signal.emit(self.get()) + 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._signal.emit(self.get()) + self.emit() + + def emit(self): + validForm = self.get() + if validForm: + self._signal.emit(validForm) def signal(self): return self._signal -class TaskFormSignal(QtCore.QObject): - _signal = QtCore.pyqtSignal(TaskForm, name = 'taskForm') +class ValidTaskFormSignal(QtCore.QObject): + _signal = QtCore.pyqtSignal(ValidTaskForm, name = 'validTaskForm') def __init__(self): QtCore.QObject.__init__(self) - def emit(self, taskForm): - self._signal.emit(taskForm) + def emit(self, form: Optional[ValidTaskForm]): + self._signal.emit(form) def connect(self, f): self._signal.connect(f) diff --git a/src/gui/tasks/form/widget.py b/src/gui/tasks/form/widget.py index 61fa24d..8063d8d 100644 --- a/src/gui/tasks/form/widget.py +++ b/src/gui/tasks/form/widget.py @@ -1,7 +1,8 @@ from PyQt5 import QtWidgets +from typing import Optional import db.tasks -from model.task import Task, TaskForm +from model.task import Task, ValidTaskForm import gui.icons import gui.tasks.form.state @@ -21,6 +22,10 @@ def widget( (name_labelled_input, name_input) = labelled_input(widget, 'Name', init_name) layout.addWidget(name_labelled_input) + init_duration = str(task.duration) if task is not None else '' + (duration_labelled_input, duration_input) = labelled_input(widget, 'Duration', init_duration) + layout.addWidget(duration_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) @@ -28,6 +33,8 @@ def widget( task_form_edition = gui.tasks.form.state.TaskFormEdition( init_name, name_input.textChanged, + init_duration, + duration_input.textChanged, init_tag, tag_input.textChanged) @@ -40,7 +47,6 @@ def widget( return widget -# Use grid ? def labelled_input(parent, label: str, default_value: str): widget = QtWidgets.QWidget(parent) @@ -67,8 +73,8 @@ def buttons(parent, action_title, task_form_signal, on_validate, on_cancel): validate.clicked.connect(on_validate); layout.addWidget(validate) - def on_task_form_signal(task_form): - if validate_form(task_form): + def on_task_form_signal(form: Optional[ValidTaskForm]): + if form: validate.setEnabled(True) else: validate.setDisabled(True) @@ -82,19 +88,6 @@ def buttons(parent, action_title, task_form_signal, on_validate, on_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 +def validate(form: Optional[ValidTaskForm], on_validated): + if form: + on_validated(form) -- cgit v1.2.3