Window.py 4.26 KiB
# Window.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 <https://www.gnu.org/licenses/>.

# -*- coding: utf-8 -*-

import logging

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,
    QWidget, QGridLayout,
)

from View.SolverParameters.UndoCommand import *
from View.SolverParameters.Table import TableModel
from View.SolverParameters import translate as tr

from Solver.Solvers import solver_long_name, solver_type_list

_translate = QCoreApplication.translate

logger = logging.getLogger()

class SolverParametersWindow(ASubMainWindow, ListedSubWindow):
    def __init__(self, title="Solver parameters", study=None, parent=None):
        # Init tanslate dictionary
        tr.init()

        self._title = title + " - " + study.name

        super(SolverParametersWindow, self).__init__(
            name=title, ui="SolverParameters", parent=parent
        )

        self._study = study
        self._params = self._study.river.parameters

        self.setup_sc()
        self.setup_table()
        self.setup_connections()

        self.ui.setWindowTitle(self._title)

    def setup_sc(self):
        self._undo_stack = {}

        for st in solver_type_list:
            self._undo_stack[st] = 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 = {}
        self._tab_widget = self.find(QTabWidget, f"tabWidget")

        for st in solver_type_list:
            # Create widgets
            widget = QWidget()
            grid = QGridLayout()
            table = QTableView()

            widget.setObjectName(f"tab_{st}")
            table.setObjectName(f"tableView_{st}")

            # Create table model
            self._table[st] = TableModel(
                data = self._study.river,
                undo = self._undo_stack[st],
                tab = st,
            )

            table.setModel(self._table[st])
            table.setSelectionBehavior(QAbstractItemView.SelectRows)
            table.horizontalHeader().setSectionResizeMode(QHeaderView.Stretch)
            table.setAlternatingRowColors(True)

            # Add table to tab
            grid.addWidget(table, 0, 0)
            widget.setLayout(grid)
            table.setParent(widget)

            # Create tab
            self._tab_widget.addTab(widget, f"{solver_long_name[st]}")


    def setup_connections(self):
        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 current_tab(self):
        return self.find(QTabWidget, "tabWidget")\
                   .currentWidget()\
                   .objectName()\
                   .replace("tab_", "")

    def undo(self):
        tab = self.current_tab()
        self._table[tab].undo()
        self._set_current_reach()

    def redo(self):
        tab = self.current_tab()
        self._table[tab].redo()
        self._set_current_reach()

    def copy(self):
        logger.info("TODO: copy")

    def paste(self):
        logger.info("TODO: paste")