# Table.py -- Pamhyr # Copyright (C) 2023 INRAE # # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program. If not, see . # -*- coding: utf-8 -*- import logging import traceback from tools import trace, timer from PyQt5.QtCore import ( Qt, QVariant, QAbstractTableModel, QCoreApplication, QModelIndex, pyqtSlot, QRect, ) from PyQt5.QtWidgets import ( QDialogButtonBox, QPushButton, QLineEdit, QFileDialog, QTableView, QAbstractItemView, QUndoStack, QShortcut, QAction, QItemDelegate, QComboBox, ) from View.Stricklers.UndoCommand import ( SetNameCommand, SetCommentCommand, SetMinorCommand, SetMediumCommand, AddCommand, DelCommand, SortCommand, ) from View.Stricklers.translate import * logger = logging.getLogger() _translate = QCoreApplication.translate class TableModel(QAbstractTableModel): def __init__(self, data=None, undo=None, tab=""): super(QAbstractTableModel, self).__init__() self._headers = list(table_headers.keys()) self._data = data self._undo = undo def flags(self, index): options = Qt.ItemIsEnabled | Qt.ItemIsSelectable options |= Qt.ItemIsEditable return options def rowCount(self, parent): return len(self._data) def columnCount(self, parent): return len(self._headers) def data(self, index, role): if role != Qt.ItemDataRole.DisplayRole: return QVariant() row = index.row() column = index.column() if self._headers[column] == "name": return self._data.get(row).name elif self._headers[column] == "comment": return self._data.get(row).comment elif self._headers[column] == "minor": return self._data.get(row).minor elif self._headers[column] == "medium": return self._data.get(row).medium return QVariant() def headerData(self, section, orientation, role): if role == Qt.ItemDataRole.DisplayRole and orientation == Qt.Orientation.Horizontal: return table_headers[self._headers[section]] return QVariant() def setData(self, index, value, role=Qt.EditRole): if not index.isValid() or role != Qt.EditRole: return False row = index.row() column = index.column() try: if self._headers[column] == "name": self._undo.push( SetNameCommand( self._data, row, value ) ) elif self._headers[column] == "comment": self._undo.push( SetCommentCommand( self._data, row, value ) ) elif self._headers[column] == "minor": self._undo.push( SetMinorCommand( self._data, row, value ) ) elif self._headers[column] == "medium": self._undo.push( SetMediumCommand( self._data, row, value ) ) except Exception as e: logger.info(e) logger.debug(traceback.format_exc()) self.dataChanged.emit(index, index) return True def add(self, row, parent=QModelIndex()): self.beginInsertRows(parent, row, row - 1) self._undo.push( AddCommand( self._data, row ) ) self.endInsertRows() self.layoutChanged.emit() def delete(self, rows, parent=QModelIndex()): self.beginRemoveRows(parent, rows[0], rows[-1]) self._undo.push( DelCommand( self._data, rows ) ) self.endRemoveRows() self.layoutChanged.emit() def sort(self, _reverse, parent=QModelIndex()): self.layoutAboutToBeChanged.emit() self._undo.push( SortCommand( self._data, False ) ) self.layoutAboutToBeChanged.emit() self.layoutChanged.emit() def undo(self): self._undo.undo() self.layoutChanged.emit() def redo(self): self._undo.redo() self.layoutChanged.emit()