# 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 -*- from PyQt5.QtGui import ( QKeySequence, ) from PyQt5.QtCore import ( Qt, QRect, QVariant, QAbstractTableModel, pyqtSlot, pyqtSignal, QEvent, ) from PyQt5.QtWidgets import ( QTableView, QItemDelegate, QComboBox, QLineEdit, QHBoxLayout, QSlider, QPushButton, QCheckBox, QStyledItemDelegate, QStyleOptionButton, QStyle, QApplication, QToolBar, QAction, QHeaderView, QAbstractItemView, QUndoStack, QShortcut, ) from Model.River import RiverNode, RiverReach, River from View.ASubWindow import ASubMainWindow from View.Network.GraphWidget import GraphWidget from View.Network.UndoCommand import * from View.Network.Table import ( GraphTableModel, ComboBoxDelegate, TrueFalseComboBoxDelegate, ) class NetworkWindow(ASubMainWindow): def __init__(self, model=None, title="River network", parent=None): self._title = title self._model = model self._graph = self._model.river self.setup_title() super(NetworkWindow, self).__init__( name=title, ui="Network", parent=parent ) self.ui.setWindowTitle(self._title) self.setup_sc() self.setup_graph() self.setup_table() self.setup_connections() def setup_title(self): self._title = self._title + " - " + self._model.name def setup_sc(self): self._undo_stack = QUndoStack() self.undo_sc = QShortcut(QKeySequence.Undo, self) self.redo_sc = QShortcut(QKeySequence.Redo, self) def setup_table(self): # Nodes table self._nodes_model = GraphTableModel( headers = ["name", "type"], graph = self._graph, rows_type = "nodes", undo = self._undo_stack, ) table = self.find(QTableView, "tableView_nodes") table.setModel(self._nodes_model) #table.resizeColumnsToContents() table.setSelectionBehavior(QAbstractItemView.SelectRows) table.horizontalHeader().setSectionResizeMode(QHeaderView.Stretch) # Edges table self._reachs_model = GraphTableModel( headers = ["name", # "enable", "node1", "node2"], graph = self._graph, rows_type = "edges", undo = self._undo_stack, ) self.delegate_combobox = ComboBoxDelegate( graph = self._graph, parent = self, ) self.delegate_true_false_combobox = TrueFalseComboBoxDelegate( parent = self, ) table = self.find(QTableView, "tableView_reachs") table.setModel(self._reachs_model) # table.setItemDelegateForColumn(1, self.delegate_true_false_combobox) table.setItemDelegateForColumn(1, self.delegate_combobox) table.setItemDelegateForColumn(2, self.delegate_combobox) table.setSelectionBehavior(QAbstractItemView.SelectRows) table.horizontalHeader().setSectionResizeMode(QHeaderView.Stretch) #table.resizeColumnsToContents() def setup_graph(self): self._graph_widget = GraphWidget( self._graph, parent=self, undo = self._undo_stack ) self._graph_layout = self.find(QHBoxLayout, "horizontalLayout_graph") self._graph_layout.addWidget(self._graph_widget) def setup_connections(self): self._nodes_model.dataChanged.connect(self._reachs_model.update) self._nodes_model.dataChanged.connect(self._graph_widget.rename_nodes) self._reachs_model.dataChanged.connect(self._graph_widget.display_update) self._reachs_model.dataChanged.connect(self._nodes_model.update) self._graph_widget.changeEdge.connect(self._reachs_model.update) self._graph_widget.changeNode.connect(self._nodes_model.update) self.find(QAction, "action_toolBar_add").setCheckable(True) self.find(QAction, "action_toolBar_add").triggered.connect( self.clicked_add ) self.find(QAction, "action_toolBar_del").setCheckable(True) self.find(QAction, "action_toolBar_del").triggered.connect( self.clicked_del ) self.undo_sc.activated.connect(self.undo) self.redo_sc.activated.connect(self.redo) def clicked_add(self): if self.get_action_checkable("action_toolBar_add"): self.set_action_checkable("action_toolBar_del", False) self._graph_widget.state("add") else: self._graph_widget.state("move") def clicked_del(self): if self.get_action_checkable("action_toolBar_del"): self.set_action_checkable("action_toolBar_add", False) self._graph_widget.state("del") else: self._graph_widget.state("move") def keyPressEvent(self, event): key = event.key() if key == Qt.Key_Escape: self._graph_widget.reset_selection def undo(self): self._undo_stack.undo() self._reachs_model.update() self._nodes_model.update() self._graph_widget.display_update() def redo(self): self._undo_stack.redo() self._reachs_model.update() self._nodes_model.update() self._graph_widget.display_update()