# -*- coding: utf-8 -*- from tools import trace, timer from View.ASubWindow import ASubMainWindow from View.ListedSubWindow import ListedSubWindow from PyQt5.QtCore import ( Qt, QVariant, QAbstractTableModel, QCoreApplication, QModelIndex, pyqtSlot, QRect, ) from PyQt5.QtWidgets import ( QTableView, QAbstractItemView, ) from Model.BoundaryCondition.BoundaryConditionTypes import ( NotDefined, PonctualContribution, TimeOverZ, TimeOverDebit, ZOverDebit ) from View.BoundaryCondition.Edit.UndoCommand import ( SetDataCommand, AddCommand, DelCommand, SortCommand, MoveCommand, PasteCommand, DuplicateCommand, ) from View.BoundaryCondition.Edit.translate import * _translate = QCoreApplication.translate class TableModel(QAbstractTableModel): def __init__(self, data=None, undo=None): super(QAbstractTableModel, self).__init__() self._headers = data.header 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.TextAlignmentRole: return Qt.AlignHCenter | Qt.AlignVCenter if role != Qt.ItemDataRole.DisplayRole: return QVariant() row = index.row() column = index.column() value = QVariant() if 0 <= column < 2: v = self._data.get_i(row)[column] if self._data.get_type_column(column) == float: value = f"{v:.4f}" else: # TODO: Time format value = f"{v}" return value 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() self._undo.push( SetDataCommand( self._data, row, column, value ) ) 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() def sort(self, _reverse, parent=QModelIndex()): self.layoutAboutToBeChanged.emit() self._undo.push( SortCommand( self._data, False ) ) self.layoutAboutToBeChanged.emit() self.layoutChanged.emit() def move_up(self, row, parent=QModelIndex()): if row <= 0: return target = row + 2 self.beginMoveRows(parent, row - 1, row - 1, parent, target) self._undo_stack.push( MoveCommand( self._data, "up", row ) ) self.endMoveRows() self.layoutChanged.emit() def move_down(self, index, parent=QModelIndex()): if row > len(self._data): return target = row self.beginMoveRows(parent, row + 1, row + 1, parent, target) self._undo_stack.push( MoveCommand( self._data, "down", row ) ) self.endMoveRows() self.layoutChanged.emit() def paste(self, row, header, data): if len(data) == 0: return self.layoutAboutToBeChanged.emit() self._undo.push( PasteCommand( self._data, row, list( map( lambda d: self._data.new_from_data(header, d), data ) ) ) ) self.layoutAboutToBeChanged.emit() self.layoutChanged.emit() def undo(self): self._undo.undo() self.layoutChanged.emit() def redo(self): self._undo.redo() self.layoutChanged.emit()