From 3c946e02e59a05ea0b04aa6c95ce38d9d1f8ae30 Mon Sep 17 00:00:00 2001 From: Joris Date: Sat, 9 May 2020 13:37:08 +0200 Subject: Use duration format in form --- src/gui/tasks/duration.py | 31 +++++++++++++++++++++++++++++++ src/gui/tasks/form/state.py | 8 +++++--- src/gui/tasks/form/widget.py | 3 ++- src/gui/tasks/table/model.py | 11 ++--------- 4 files changed, 40 insertions(+), 13 deletions(-) create mode 100644 src/gui/tasks/duration.py (limited to 'src') diff --git a/src/gui/tasks/duration.py b/src/gui/tasks/duration.py new file mode 100644 index 0000000..d1b7106 --- /dev/null +++ b/src/gui/tasks/duration.py @@ -0,0 +1,31 @@ +import re +from typing import Optional +import math + +def format(minutes: int): + if minutes >= 60 * 24: + return '' + str(math.floor(minutes / 60 / 24)) + 'd' + elif minutes >= 60: + return '' + str(math.floor(minutes / 60)) + 'h' + elif minutes > 0: + return '' + str(minutes) + 'm' + else: + return '' + +def parse(duration: str) -> Optional[int]: + duration = duration.strip() + if duration: + result = re.match('^(\d+)([mhd])$', duration.strip()) + if result: + n = int(result.group(1)) + unit = result.group(2) + if unit == 'm': + return n + elif unit == 'h': + return n * 60 + elif unit == 'd': + return n * 60 * 24 + else: + return None + else: + return 0 diff --git a/src/gui/tasks/form/state.py b/src/gui/tasks/form/state.py index a47aec7..12ffe36 100644 --- a/src/gui/tasks/form/state.py +++ b/src/gui/tasks/form/state.py @@ -1,7 +1,9 @@ +import re from PyQt5 import QtCore from typing import NamedTuple, Optional from model.task import ValidTaskForm +import gui.tasks.duration class TaskFormEdition: def __init__(self, name, name_signal, duration, duration_signal, tag, tag_signal): @@ -15,13 +17,13 @@ class TaskFormEdition: def get(self) -> Optional[ValidTaskForm]: name = self._name.strip() - duration = self._duration.strip() + duration = gui.tasks.duration.parse(self._duration) tag = self._tag.strip() - if name and (duration == '' or duration.isdigit()): + if name and duration != None: return ValidTaskForm( name = name, - duration = 0 if duration == '' else int(duration), + duration = duration, tag = tag) else: return None diff --git a/src/gui/tasks/form/widget.py b/src/gui/tasks/form/widget.py index 8063d8d..6421747 100644 --- a/src/gui/tasks/form/widget.py +++ b/src/gui/tasks/form/widget.py @@ -6,6 +6,7 @@ from model.task import Task, ValidTaskForm import gui.icons import gui.tasks.form.state +import gui.tasks.duration def widget( parent: QtWidgets.QWidget, @@ -22,7 +23,7 @@ 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 '' + init_duration = gui.tasks.duration.format(task.duration) if task is not None else '' (duration_labelled_input, duration_input) = labelled_input(widget, 'Duration', init_duration) layout.addWidget(duration_labelled_input) diff --git a/src/gui/tasks/table/model.py b/src/gui/tasks/table/model.py index 6b8133d..cb25b22 100644 --- a/src/gui/tasks/table/model.py +++ b/src/gui/tasks/table/model.py @@ -6,6 +6,7 @@ import time import math import util.array import util.range +import gui.tasks.duration columns = 4 @@ -34,7 +35,7 @@ class TableModel(QtCore.QAbstractTableModel): elif index.column() == 1: return task.name elif index.column() == 2: - return pp_duration(task.duration) + return gui.tasks.duration.format(task.duration) elif index.column() == 3: return task.tag else: @@ -103,14 +104,6 @@ def age_since(timestamp): else: return '1m' -def pp_duration(minutes: int): - if minutes >= 60 * 24: - return '' + str(math.floor(minutes / 60 / 24)) + 'd' - elif minutes >= 60: - return '' + str(math.floor(minutes / 60)) + 'h' - else: - return '' + str(minutes) + 'm' - def sort_key(task: Task, row: int): if row == 0: return task.created_at -- cgit v1.2.3