diff options
Diffstat (limited to 'src/gui/tasks/dialog.py')
-rw-r--r-- | src/gui/tasks/dialog.py | 64 |
1 files changed, 64 insertions, 0 deletions
diff --git a/src/gui/tasks/dialog.py b/src/gui/tasks/dialog.py new file mode 100644 index 0000000..2e0d8d4 --- /dev/null +++ b/src/gui/tasks/dialog.py @@ -0,0 +1,64 @@ +from PyQt5 import QtCore, QtWidgets + +from model.task import Task, ValidTaskForm + +import db.tasks +import gui.tasks.form.widget + +def add(database, parent_widget, add_task_signal): + + def on_add(form: ValidTaskForm): + task = db.tasks.insert(database.cursor(), form) + database.commit() + add_task_signal.emit(task) + + return widget(parent_widget, 'Add a task', 'add', None, on_add) + +def update(database, parent_widget, update_task_signal, row, task): + + def on_update(form: ValidTaskForm): + updated_task = db.tasks.update(database.cursor(), task, form) + update_task_signal.emit(row, updated_task) + database.commit() + + return widget(parent_widget, 'Modify a task', 'modify', task, on_update) + +def show_delete(database, table, rows): + confirm = QtWidgets.QMessageBox.question( + table, + 'Task deletion', + 'Do you really want to delete the selected tasks ?', + QtWidgets.QMessageBox.No | QtWidgets.QMessageBox.Yes, + QtWidgets.QMessageBox.Yes) + + if confirm == QtWidgets.QMessageBox.Yes: + db.tasks.delete(database.cursor(), table.model().row_ids(rows)) + database.commit() + table.model().delete_tasks(rows) + +def widget( + parent: QtWidgets.QWidget, + title: str, + action_title: str, + task: Task, + on_validated): + + dialog = QtWidgets.QDialog(parent) + dialog.setWindowTitle(title) + dialog.setMinimumSize(QtCore.QSize(320, 240)) + + layout = QtWidgets.QVBoxLayout(dialog) + dialog.setLayout(layout) + + def on_dialog_validated(form): + dialog.accept() + on_validated(form) + + layout.addWidget(gui.tasks.form.widget.widget( + parent = dialog, + action_title = action_title, + task = task, + on_validated = on_dialog_validated, + on_cancel = lambda: dialog.reject())) + + return dialog |