aboutsummaryrefslogtreecommitdiff
path: root/src/gui/tasks/dialog.py
blob: 2e0d8d4980d5bfdc6e1888e2eebe7388e351daec (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
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