mirror of
https://gitlab.com/pamhyr/pamhyr2.git
synced 2026-10-30 03:20:41 +01:00
Stricklers: Add base of code.
This commit is contained in:
1 parent
1179dd0b92
commit
113fafac50
11 files changed
+669
-12
No files matched your search
@@ -0,0 +1,149 @@
|
||||
# -*- 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.Stricklers.UndoCommand import (
|
||||
SetNameCommand, SetCommentCommand,
|
||||
SetMinorCommand, SetMediumCommand,
|
||||
AddCommand, DelCommand, SortCommand,
|
||||
)
|
||||
|
||||
from View.Stricklers.translate import *
|
||||
|
||||
_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()
|
||||
|
||||
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
|
||||
)
|
||||
)
|
||||
|
||||
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()
|
||||
@@ -0,0 +1,156 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
from copy import deepcopy
|
||||
from tools import trace, timer
|
||||
|
||||
from PyQt5.QtWidgets import (
|
||||
QMessageBox, QUndoCommand, QUndoStack,
|
||||
)
|
||||
|
||||
from Model.LateralContribution.LateralContribution import LateralContribution
|
||||
from Model.LateralContribution.LateralContributionList import LateralContributionList
|
||||
|
||||
class SetNameCommand(QUndoCommand):
|
||||
def __init__(self, data, index, new_value):
|
||||
QUndoCommand.__init__(self)
|
||||
|
||||
self._data = data
|
||||
self._index = index
|
||||
self._old = self._data.get(self._index).name
|
||||
self._new = new_value
|
||||
|
||||
def undo(self):
|
||||
self._data.get(self._index).name = self._old
|
||||
|
||||
def redo(self):
|
||||
self._data.get(self._index).name = self._new
|
||||
|
||||
class SetCommentCommand(QUndoCommand):
|
||||
def __init__(self, data, index, new_value):
|
||||
QUndoCommand.__init__(self)
|
||||
|
||||
self._data = data
|
||||
self._index = index
|
||||
self._old = self._data.get(self._index).comment
|
||||
self._new = new_value
|
||||
|
||||
def undo(self):
|
||||
self._data.get(self._index).comment = self._old
|
||||
|
||||
def redo(self):
|
||||
self._data.get(self._index).comment = self._new
|
||||
|
||||
class SetMinorCommand(QUndoCommand):
|
||||
def __init__(self, data, index, new_value):
|
||||
QUndoCommand.__init__(self)
|
||||
|
||||
self._data = data
|
||||
self._index = index
|
||||
self._old = self._data.get(self._index).minor
|
||||
self._new = new_value
|
||||
|
||||
def undo(self):
|
||||
self._data.get(self._index).minor = self._old
|
||||
|
||||
def redo(self):
|
||||
self._data.get(self._index).minor = self._new
|
||||
|
||||
class SetMediumCommand(QUndoCommand):
|
||||
def __init__(self, data, index, new_value):
|
||||
QUndoCommand.__init__(self)
|
||||
|
||||
self._data = data
|
||||
self._index = index
|
||||
self._old = self._data.get(self._index).medium
|
||||
self._new = new_value
|
||||
|
||||
def undo(self):
|
||||
self._data.get(self._index).medium = self._old
|
||||
|
||||
def redo(self):
|
||||
self._data.get(self._index).medium = self._new
|
||||
|
||||
|
||||
class AddCommand(QUndoCommand):
|
||||
def __init__(self, data, index):
|
||||
QUndoCommand.__init__(self)
|
||||
|
||||
self._data = data
|
||||
self._index = index
|
||||
self._new = None
|
||||
|
||||
def undo(self):
|
||||
self._data.delete_i([self._index])
|
||||
|
||||
def redo(self):
|
||||
if self._new is None:
|
||||
self._new = self._data.add(self._index)
|
||||
else:
|
||||
self._data.insert(self._index, self._new)
|
||||
|
||||
class DelCommand(QUndoCommand):
|
||||
def __init__(self, data, rows):
|
||||
QUndoCommand.__init__(self)
|
||||
|
||||
self._data = data
|
||||
self._rows = rows
|
||||
|
||||
self._el = []
|
||||
for row in rows:
|
||||
self._el.append((row, self._data.get(row)))
|
||||
self._el.sort()
|
||||
|
||||
def undo(self):
|
||||
for row, el in self._el:
|
||||
self._data.insert(row, el)
|
||||
|
||||
def redo(self):
|
||||
self._data.delete_i(self._rows)
|
||||
|
||||
class SortCommand(QUndoCommand):
|
||||
def __init__(self, data, _reverse):
|
||||
QUndoCommand.__init__(self)
|
||||
|
||||
self._data = data
|
||||
self._reverse = _reverse
|
||||
|
||||
self._old = self._data.get_tab(self._tab)
|
||||
self._indexes = None
|
||||
|
||||
def undo(self):
|
||||
ll = self._data.get_tab(self._tab)
|
||||
self._data.sort(
|
||||
self._tab,
|
||||
key=lambda x: self._indexes[ll.index(x)]
|
||||
)
|
||||
|
||||
def redo(self):
|
||||
self._data.sort(
|
||||
self._tab,
|
||||
reverse=self._reverse,
|
||||
key=lambda x: x.name
|
||||
)
|
||||
if self._indexes is None:
|
||||
self._indexes = list(
|
||||
map(
|
||||
lambda p: self._old.index(p),
|
||||
self._data.get_tab(self._tab)
|
||||
)
|
||||
)
|
||||
self._old = None
|
||||
|
||||
class PasteCommand(QUndoCommand):
|
||||
def __init__(self, data, row, el):
|
||||
QUndoCommand.__init__(self)
|
||||
|
||||
self._data = data
|
||||
self._row = row
|
||||
self._el = deepcopy(el)
|
||||
self._el.reverse()
|
||||
|
||||
def undo(self):
|
||||
self._data.delete(self._el)
|
||||
|
||||
def redo(self):
|
||||
for el in self._el:
|
||||
self._data.insert(self._row, el)
|
||||
@@ -0,0 +1,120 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
from tools import trace, timer
|
||||
|
||||
from View.ASubWindow import ASubMainWindow
|
||||
from View.ListedSubWindow import ListedSubWindow
|
||||
|
||||
from PyQt5.QtGui import (
|
||||
QKeySequence,
|
||||
)
|
||||
|
||||
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, QVBoxLayout, QHeaderView, QTabWidget,
|
||||
)
|
||||
|
||||
from Model.Stricklers.Stricklers import Stricklers
|
||||
|
||||
from View.Stricklers.UndoCommand import PasteCommand
|
||||
from View.Stricklers.Table import TableModel
|
||||
|
||||
_translate = QCoreApplication.translate
|
||||
|
||||
|
||||
class StricklersWindow(ASubMainWindow, ListedSubWindow):
|
||||
def __init__(self, title="Stricklers", study=None, parent=None):
|
||||
title = title + " - " + study.name
|
||||
|
||||
super(StricklersWindow, self).__init__(
|
||||
name=title, ui="Stricklers", parent=parent
|
||||
)
|
||||
|
||||
self._study = study
|
||||
|
||||
self.setup_sc()
|
||||
self.setup_table()
|
||||
self.setup_connections()
|
||||
|
||||
self.ui.setWindowTitle(title)
|
||||
|
||||
def setup_sc(self):
|
||||
self._undo_stack = QUndoStack()
|
||||
|
||||
self.undo_sc = QShortcut(QKeySequence.Undo, self)
|
||||
self.redo_sc = QShortcut(QKeySequence.Redo, self)
|
||||
self.copy_sc = QShortcut(QKeySequence.Copy, self)
|
||||
self.paste_sc = QShortcut(QKeySequence.Paste, self)
|
||||
|
||||
def setup_table(self):
|
||||
self._table = {}
|
||||
|
||||
for t in ["app", "study"]:
|
||||
table = self.find(QTableView, f"tableView_{t}")
|
||||
self._table[t] = TableModel(
|
||||
data = self._study.river.striklers,
|
||||
undo = self._undo_stack,
|
||||
)
|
||||
table.setModel(self._table[t])
|
||||
table.setSelectionBehavior(QAbstractItemView.SelectRows)
|
||||
table.horizontalHeader().setSectionResizeMode(QHeaderView.Stretch)
|
||||
table.setAlternatingRowColors(True)
|
||||
|
||||
def setup_connections(self):
|
||||
self.find(QAction, "action_add").triggered.connect(self.add)
|
||||
self.find(QAction, "action_del").triggered.connect(self.delete)
|
||||
self.find(QAction, "action_sort").triggered.connect(self.sort)
|
||||
|
||||
self.undo_sc.activated.connect(self.undo)
|
||||
self.redo_sc.activated.connect(self.redo)
|
||||
self.copy_sc.activated.connect(self.copy)
|
||||
self.paste_sc.activated.connect(self.paste)
|
||||
|
||||
def index_selected_rows(self):
|
||||
table = self.find(QTableView, f"tableView_study")
|
||||
return list(
|
||||
set(
|
||||
map(
|
||||
lambda i: i.row(),
|
||||
table.selectedIndexes()
|
||||
)
|
||||
)
|
||||
)
|
||||
|
||||
|
||||
def add(self):
|
||||
rows = self.index_selected_rows()
|
||||
if len(rows) == 0:
|
||||
self._table['study'].add(0)
|
||||
else:
|
||||
self._table['study'].add(rows[0])
|
||||
|
||||
def delete(self):
|
||||
rows = self.index_selected_rows()
|
||||
if len(rows) == 0:
|
||||
return
|
||||
|
||||
self._table['study'].delete(rows)
|
||||
|
||||
def sort(self):
|
||||
self._table['study'].sort()
|
||||
|
||||
def copy(self):
|
||||
print("TODO")
|
||||
|
||||
def paste(self):
|
||||
print("TODO")
|
||||
|
||||
def undo(self):
|
||||
self._table['study'].undo()
|
||||
|
||||
def redo(self):
|
||||
self._table['study'].redo()
|
||||
@@ -0,0 +1,16 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
from PyQt5.QtCore import QCoreApplication
|
||||
|
||||
from Model.LateralContribution.LateralContributionTypes import (
|
||||
NotDefined, LateralContrib, Rain, Evaporation,
|
||||
)
|
||||
|
||||
_translate = QCoreApplication.translate
|
||||
|
||||
table_headers = {
|
||||
"name": _translate("LateralContribution", "Name"),
|
||||
"minor": _translate("LateralContribution", "Minor bed"),
|
||||
"medium": _translate("LateralContribution", "Medium bed"),
|
||||
"comment": _translate("LateralContribution", "Comment"),
|
||||
}
|
||||
Reference in new issue
Block a user