mirror of https://gitlab.com/pamhyr/pamhyr2
152 lines
3.7 KiB
Python
152 lines
3.7 KiB
Python
# -*- coding: utf-8 -*-
|
|
|
|
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.SedimentLayers.UndoCommand import *
|
|
from View.SedimentLayers.translate import *
|
|
|
|
_translate = QCoreApplication.translate
|
|
|
|
|
|
class TableModel(QAbstractTableModel):
|
|
def __init__(self, study=None, undo=None):
|
|
super(QAbstractTableModel, self).__init__()
|
|
self._headers = list(table_headers.keys())
|
|
self._study = study
|
|
self._undo = undo
|
|
self._sl = self._study.river.sediment_layers
|
|
|
|
def flags(self, index):
|
|
options = Qt.ItemIsEnabled | Qt.ItemIsSelectable
|
|
options |= Qt.ItemIsEditable
|
|
|
|
return options
|
|
|
|
def rowCount(self, parent):
|
|
return len(self._sl)
|
|
|
|
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._sl.get(row).name
|
|
elif self._headers[column] == "comment":
|
|
return self._sl.get(row).comment
|
|
|
|
return QVariant()
|
|
|
|
def headerData(self, friction, orientation, role):
|
|
if role == Qt.ItemDataRole.DisplayRole and orientation == Qt.Orientation.Horizontal:
|
|
return table_headers[self._headers[friction]]
|
|
|
|
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()
|
|
|
|
if self._headers[column] == "name":
|
|
self._undo.push(
|
|
SetNameCommand(
|
|
self._sl, row, value
|
|
)
|
|
)
|
|
if self._headers[column] == "comment":
|
|
self._undo.push(
|
|
SetCommentCommand(
|
|
self._sl, row, 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._sl, row
|
|
)
|
|
)
|
|
|
|
self.endInsertRows()
|
|
self.layoutChanged.emit()
|
|
|
|
def delete(self, rows, parent=QModelIndex()):
|
|
self.beginRemoveRows(parent, rows[0], rows[-1])
|
|
|
|
self._undo.push(
|
|
DelCommand(
|
|
self._sl, rows
|
|
)
|
|
)
|
|
|
|
self.endRemoveRows()
|
|
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._sl, "up", row
|
|
)
|
|
)
|
|
|
|
self.endMoveRows()
|
|
self.layoutChanged.emit()
|
|
|
|
def move_down(self, index, parent=QModelIndex()):
|
|
if row > len(self._sl):
|
|
return
|
|
|
|
target = row
|
|
|
|
self.beginMoveRows(parent, row + 1, row + 1, parent, target)
|
|
|
|
self._undo_stack.push(
|
|
MoveCommand(
|
|
self._sl, "down", row
|
|
)
|
|
)
|
|
|
|
self.endMoveRows()
|
|
self.layoutChanged.emit()
|
|
|
|
def undo(self):
|
|
self._undo.undo()
|
|
self.layoutChanged.emit()
|
|
|
|
def redo(self):
|
|
self._undo.redo()
|
|
self.layoutChanged.emit()
|