From b5658771fd9b1a50e10a0004dd1934c746505446 Mon Sep 17 00:00:00 2001 From: Joris Date: Sat, 9 May 2020 15:04:24 +0200 Subject: Enable task modification and deletion with keyboard and mouse --- src/gui/tasks/table/menu.py | 24 ++----------- src/gui/tasks/table/widget.py | 79 ++++++++++++++++++++++++++++--------------- 2 files changed, 55 insertions(+), 48 deletions(-) (limited to 'src/gui/tasks/table') diff --git a/src/gui/tasks/table/menu.py b/src/gui/tasks/table/menu.py index f89ec92..51f7330 100644 --- a/src/gui/tasks/table/menu.py +++ b/src/gui/tasks/table/menu.py @@ -1,7 +1,7 @@ from PyQt5 import QtWidgets import db.tasks -import gui.tasks.modal +import gui.tasks.dialog from model.task import Task, ValidTaskForm def open(database, table, update_task_signal, position): @@ -20,24 +20,6 @@ def open(database, table, update_task_signal, position): if action == modify_action and len(rows) == 1: row = list(rows)[0] task = table.model().get_at(row) - show_update_dialog(database, table, update_task_signal, row, task) + gui.tasks.dialog.update(database, table, update_task_signal, row, task).exec_() elif action == delete_action: - 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 show_update_dialog(database, parent_widget, update_task_signal, row, task): - dialog = gui.tasks.modal.dialog( - parent_widget, - 'Modify a task', - 'modify', - task, - lambda form: on_update(database, update_task_signal, row, task, form)) - dialog.exec_() - -def on_update(database, update_task_signal, row, task: Task, form: ValidTaskForm): - task = db.tasks.update(database.cursor(), task, form) - update_task_signal.emit(row, task) - database.commit() + gui.tasks.dialog.show_delete(database, table, rows) diff --git a/src/gui/tasks/table/widget.py b/src/gui/tasks/table/widget.py index a990c0e..95ebe44 100644 --- a/src/gui/tasks/table/widget.py +++ b/src/gui/tasks/table/widget.py @@ -5,42 +5,67 @@ import db.tasks import gui.tasks.signal import gui.tasks.table.menu import gui.tasks.table.model +import gui.tasks.dialog +from model.task import Task, ValidTaskForm -def widget(database, parent, add_task_signal): - table = QtWidgets.QTableView(parent) +class Widget(QtWidgets.QTableView): - tasks = db.tasks.get(database.cursor()) - table_model = gui.tasks.table.model.TableModel(tasks) + def __init__(self, database, parent, add_task_signal): + super().__init__(parent) - table.setModel(table_model) - table.sortByColumn( + self._database = database + self._update_task_signal = gui.tasks.signal.UpdateTask() + + tasks = db.tasks.get(self._database.cursor()) + table_model = gui.tasks.table.model.TableModel(tasks) + + self.setModel(table_model) + self.sortByColumn( gui.tasks.table.model.default_sort[0], gui.tasks.table.model.default_sort[1]) - table.setSortingEnabled(True) - table.setSelectionBehavior(QtWidgets.QTableView.SelectRows) - table.horizontalHeader().setStretchLastSection(True) - resizeColumns(table) + self.setSortingEnabled(True) + self.setSelectionBehavior(QtWidgets.QTableView.SelectRows) + self.horizontalHeader().setStretchLastSection(True) + self.resizeColumns() + + self.doubleClicked.connect(lambda index: self.on_double_click(index.row())) + + # Menu + self.setContextMenuPolicy(Qt.CustomContextMenu) + self.customContextMenuRequested.connect(lambda position: gui.tasks.table.menu.open(self._database, self, self._update_task_signal, position)) - update_task_signal = gui.tasks.signal.UpdateTask() + add_task_signal.get().connect(lambda task: self.insert(task)) + self._update_task_signal.get().connect(lambda row, task: self.update(row, task)) - # Menu - table.setContextMenuPolicy(Qt.CustomContextMenu) - table.customContextMenuRequested.connect(lambda position: gui.tasks.table.menu.open(database, table, update_task_signal, position)) + def insert(self, task): + self.model().insert_task(self.horizontalHeader(), task) + self.resizeColumns() - add_task_signal.get().connect(lambda task: insert(table, task)) - update_task_signal.get().connect(lambda row, task: update(table, row, task)) + def update(self, row, task): + row = self.model().update_task(self.horizontalHeader(), row, task) + self.selectRow(row) + self.resizeColumns() - return table + def resizeColumns(self): + for column in range(gui.tasks.table.model.columns): + self.resizeColumnToContents(column) -def insert(table, task): - table.model().insert_task(table.horizontalHeader(), task) - resizeColumns(table) + def keyPressEvent(self, event): + super().keyPressEvent(event) + if event.key() in (Qt.Key_Return, Qt.Key_Enter): + rows = self.get_selected_rows() + if len(rows) == 1: + row = rows[0] + task = self.model().get_at(row) + gui.tasks.dialog.update( + self._database, self, self._update_task_signal, row, task).exec_() + elif event.key() == Qt.Key_Delete: + rows = self.get_selected_rows() + gui.tasks.dialog.show_delete(self._database, self, rows) -def update(table, row, task): - row = table.model().update_task(table.horizontalHeader(), row, task) - table.selectRow(row) - resizeColumns(table) + def get_selected_rows(self): + return list(set([index.row() for index in self.selectedIndexes()])) -def resizeColumns(table): - for column in range(gui.tasks.table.model.columns): - table.resizeColumnToContents(column) + def on_double_click(self, row: int): + task = self.model().get_at(row) + gui.tasks.dialog.update(self._database, self, self._update_task_signal, row, task).exec_() -- cgit v1.2.3