diff options
Diffstat (limited to 'src/gui/tasks/table/widget.py')
-rw-r--r-- | src/gui/tasks/table/widget.py | 46 |
1 files changed, 46 insertions, 0 deletions
diff --git a/src/gui/tasks/table/widget.py b/src/gui/tasks/table/widget.py new file mode 100644 index 0000000..a990c0e --- /dev/null +++ b/src/gui/tasks/table/widget.py @@ -0,0 +1,46 @@ +from PyQt5 import QtWidgets +from PyQt5.QtCore import Qt + +import db.tasks +import gui.tasks.signal +import gui.tasks.table.menu +import gui.tasks.table.model + +def widget(database, parent, add_task_signal): + table = QtWidgets.QTableView(parent) + + tasks = db.tasks.get(database.cursor()) + table_model = gui.tasks.table.model.TableModel(tasks) + + table.setModel(table_model) + table.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) + + update_task_signal = gui.tasks.signal.UpdateTask() + + # Menu + table.setContextMenuPolicy(Qt.CustomContextMenu) + table.customContextMenuRequested.connect(lambda position: gui.tasks.table.menu.open(database, table, update_task_signal, position)) + + add_task_signal.get().connect(lambda task: insert(table, task)) + update_task_signal.get().connect(lambda row, task: update(table, row, task)) + + return table + +def insert(table, task): + table.model().insert_task(table.horizontalHeader(), task) + resizeColumns(table) + +def update(table, row, task): + row = table.model().update_task(table.horizontalHeader(), row, task) + table.selectRow(row) + resizeColumns(table) + +def resizeColumns(table): + for column in range(gui.tasks.table.model.columns): + table.resizeColumnToContents(column) |