BoundaryConditionWindow.py 3.29 KiB
# -*- coding: utf-8 -*-

from View.ASubWindow import ASubMainWindow
from View.ListedSubWindow import ListedSubWindow

from PyQt5.QtCore import (
    Qt, QVariant, QAbstractTableModel,
    QCoreApplication,
)

from PyQt5.QtWidgets import (
    QDialogButtonBox, QPushButton, QLineEdit,
    QFileDialog, QTableView, QAbstractItemView,
)

from View.BoundaryCondition.EditBoundaryConditionWindow import EditBoundaryConditionWindow

_translate = QCoreApplication.translate

long_types = {
    "PC": _translate("BoundaryCondition", "Ponctual contribution"),
    "TZ": _translate("BoundaryCondition", "Time over Z"),
    "TD": _translate("BoundaryCondition", "Time over Debit"),
    "ZD": _translate("BoundaryCondition", "Z over Debit"),
}

table_headers = {
    "name": _translate("BoundaryCondition", "Name"),
    "type": _translate("BoundaryCondition", "Type"),
    "node": _translate("BoundaryCondition", "Node")
}

class TableModel(QAbstractTableModel):
    def __init__(self, data=None):
        super(QAbstractTableModel, self).__init__()
        self._headers = list(table_headers.keys())
        self._data = data
        self._lst = self._data.boundary_condition

    def flags(self, index):
        options = Qt.ItemIsEnabled | Qt.ItemIsSelectable
        options |= Qt.ItemIsEditable

        return options

    def rowCount(self, parent):
        return len(self._lst)

    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.lst[row].name
        elif self._headers[column] == "type":
            t = self.lst[row].bctype
            return long_types[t]
        elif self._headers[column] == "node":
            n = self.lst[row].node
            if n is None:
                return "-"
            return n.name

        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.lst[row].name = value
        elif self._headers[column] == "type":
            self.lst[row].bctype = value
        elif self._headers[column] == "node":
            self.lst[row].node = value

        self.dataChanged.emit(index, index)
        return True


class BoundaryConditionWindow(ASubMainWindow, ListedSubWindow):
    def __init__(self, title="BoundaryConditions", study=None, parent=None):
        super(BoundaryConditionWindow, self).__init__(
            name=title, ui="BoundaryConditions", parent=parent
        )

        self._study = study

        table = self.find(QTableView, "tableView")
        self._table = TableModel(
            data = self._study.river
        )
        table.setModel(self._table)

        self.ui.setWindowTitle(title)

    def edit(self):
        win = EditBoundaryConditionWindow(data=None, parent=self)
        win.show()