From 5efda0e66f7430427be7b79e0166b1b1e1d169ee Mon Sep 17 00:00:00 2001 From: alixdamman Date: Mon, 4 Nov 2024 16:25:35 +0100 Subject: [PATCH] GUI PYTHON: (edit_iode_obj.py) implemented Edit[Comment|Identity|List]Dialog classes --- gui/iode_objs/edit/edit_comment.py | 0 gui/iode_objs/edit/edit_identity.py | 0 gui/iode_objs/edit/edit_iode_obj.py | 135 ++++++++++++++++++++++++++++ gui/iode_objs/edit/edit_list.py | 0 gui/iode_objs/views/table_view.py | 28 +++--- 5 files changed, 145 insertions(+), 18 deletions(-) delete mode 100644 gui/iode_objs/edit/edit_comment.py delete mode 100644 gui/iode_objs/edit/edit_identity.py delete mode 100644 gui/iode_objs/edit/edit_list.py diff --git a/gui/iode_objs/edit/edit_comment.py b/gui/iode_objs/edit/edit_comment.py deleted file mode 100644 index e69de29bb..000000000 diff --git a/gui/iode_objs/edit/edit_identity.py b/gui/iode_objs/edit/edit_identity.py deleted file mode 100644 index e69de29bb..000000000 diff --git a/gui/iode_objs/edit/edit_iode_obj.py b/gui/iode_objs/edit/edit_iode_obj.py index e69de29bb..00b2906d7 100644 --- a/gui/iode_objs/edit/edit_iode_obj.py +++ b/gui/iode_objs/edit/edit_iode_obj.py @@ -0,0 +1,135 @@ +from PySide6.QtCore import Qt, Signal, Slot, QModelIndex +from PySide6.QtWidgets import QDialog, QPlainTextEdit, QMessageBox +from PySide6.QtGui import QDesktopServices, QShortcut, QKeySequence + + +from utils import URL_MANUAL +from abstract_main_window import AbstractMainWindow +from text_edit.completer import IodeCompleter +from .ui_edit_iode_obj import Ui_EditObjectDialog + +from typing import Any +from enum import Enum +from iode import (IodeTypes, IODE_DATABASE_TYPE_NAMES, comments, identities, lists, + scalars, tables, variables, Table, TableLineType) + + +class AbstractEditObjDialog(QDialog): + database_modified = Signal() + + def __init__(self, database, global_database, name: str, parent=None): + super().__init__(parent) + self.previous_name = name + self._database = database + self._global_database = global_database + self.previous_value = None + + iode_type = database.iode_type + self.setWindowTitle("Editing " + IODE_DATABASE_TYPE_NAMES[int(iode_type)] + " " + name) + + self.full_screen_shortcut = QShortcut(QKeySequence(Qt.Modifier.CTRL | Qt.Key.Key_X), self) + self.full_screen_shortcut.setContext(Qt.ShortcutContext.WidgetWithChildrenShortcut) + self.full_screen_shortcut.activated.connect(self.showMaximized) + + def _edit(self, name: str, value: Any): + try: + # user changed nothing + if name == self.previous_name and value == self.previous_value: + self.accept() + return + + # user changed the name when editing but an IODE object with the same name already exist + # in the global database + if name != self.previous_name and name in self._global_database: + res = QMessageBox.question(None, "WARNING", f"{name} already exists. Replace it ?", + QMessageBox.StandardButton.Yes | QMessageBox.StandardButton.No) + if res != QMessageBox.StandardButton.Yes: + self.reject() + return + + self._database[name] = value + self.database_modified.emit() + self.accept() + except Exception as e: + QMessageBox.warning(None, "WARNING", str(e)) + + @Slot() + def edit(self): + raise NotImplementedError() + + @Slot() + def help(self): + """ + Opens the manual in a web browser. + """ + QDesktopServices.openUrl(URL_MANUAL) + + +class EditCommentDialog(AbstractEditObjDialog): + def __init__(self, database, name: str, parent=None): + super().__init__(database, comments, name, parent) + self.ui = Ui_EditObjectDialog() + self.ui.setupUi(self) + + self.ui.lineEdit_name.setText(name) + self.ui.lineEdit_name.setReadOnly(True) + + self.previous_value = database[name] + self.ui.textEdit_content.setLineWrapMode(QPlainTextEdit.LineWrapMode.WidgetWidth) + self.ui.textEdit_content.setPlainText(self.previous_value) + + @Slot() + def edit(self): + name = self.ui.lineEdit_name.text() + value = self.ui.textEdit_content.toPlainText() + self._edit(name, value) + + +class EditIdentityDialog(AbstractEditObjDialog): + def __init__(self, database, name: str, parent=None): + super().__init__(database, identities, name, parent) + self.ui = Ui_EditObjectDialog() + self.ui.setupUi(self) + + self.ui.lineEdit_name.setText(name) + self.ui.lineEdit_name.setReadOnly(True) + + self.previous_value = str(database[name]) + self.ui.textEdit_content.setLineWrapMode(QPlainTextEdit.LineWrapMode.WidgetWidth) + self.ui.textEdit_content.setPlainText(self.previous_value) + + completer = IodeCompleter(report_commands=False, lec_functions=False, + iode_types=[IodeTypes.SCALARS, IodeTypes.VARIABLES]) + self.ui.textEdit_content.completer = completer + + @Slot() + def edit(self): + name = self.ui.lineEdit_name.text() + value = self.ui.textEdit_content.toPlainText() + self._edit(name, value) + + +class EditListDialog(AbstractEditObjDialog): + def __init__(self, database, name: str, parent=None): + super().__init__(database, lists, name, parent) + self.ui = Ui_EditObjectDialog() + self.ui.setupUi(self) + + self.ui.lineEdit_name.setText(name) + self.ui.lineEdit_name.setReadOnly(True) + + self.previous_value = ", ".join(database[name]) + self.ui.textEdit_content.setLineWrapMode(QPlainTextEdit.LineWrapMode.WidgetWidth) + self.ui.textEdit_content.setPlainText(self.previous_value) + + completer = IodeCompleter(report_commands=False, lec_functions=False, + iode_types=[IodeTypes.SCALARS, IodeTypes.VARIABLES]) + self.ui.textEdit_content.completer = completer + + @Slot() + def edit(self): + name = self.ui.lineEdit_name.text() + value = self.ui.textEdit_content.toPlainText() + self._edit(name, value) + + diff --git a/gui/iode_objs/edit/edit_list.py b/gui/iode_objs/edit/edit_list.py deleted file mode 100644 index e69de29bb..000000000 diff --git a/gui/iode_objs/views/table_view.py b/gui/iode_objs/views/table_view.py index 6b056b327..3cb17d04c 100644 --- a/gui/iode_objs/views/table_view.py +++ b/gui/iode_objs/views/table_view.py @@ -16,6 +16,8 @@ from iode_objs.new.add_object import (AddCommentDialog, AddIdentityDialog, AddListDialog, AddScalarDialog, AddTableDialog, AddVariableDialog) +from iode_objs.edit.edit_iode_obj import (EditCommentDialog, EditIdentityDialog, + EditListDialog) from iode_objs.models.abstract_table_model import IodeAbstractTableModel from iode_objs.models.table_model import IdentitiesModel @@ -38,8 +40,8 @@ def __init__(self, parent=None): def _open_edit_dialog(self, obj_name: str): model: IodeAbstractTableModel = self.model() - # self.edit_dialog = EditCommentDialog(obj_name, model.displayed_database, self) - # self.edit_dialog.database_modified.connect(self.database_modified) + self.edit_dialog = EditCommentDialog(obj_name, model.displayed_database, self) + self.edit_dialog.database_modified.connect(self.database_modified) def _open_add_dialog(self) -> QDialog: model: IodeAbstractTableModel = self.model() @@ -65,8 +67,8 @@ def _open_add_dialog(self) -> QDialog: model: IodeAbstractTableModel = self.model() # dialog: EditEquationDialog = EditEquationDialog("", model.displayed_database, self) # dialog.new_object_inserted.connect(self.new_object_inserted) - # dialog.database_modified.connect(self.database_modified) - # dialog.database_modified.connect(self.update_filter) + # dialog.database_modified.connect(self.database_modified) + # dialog.database_modified.connect(self.update_filter) # return dialog @Slot() @@ -99,8 +101,8 @@ def setup(self, main_window: AbstractMainWindow): def _open_edit_dialog(self, obj_name: str): model: IodeAbstractTableModel = self.model() - # self.edit_dialog = EditIdentityDialog(obj_name, model.displayed_database, self) - # self.edit_dialog.database_modified.connect(self.database_modified) + self.edit_dialog = EditIdentityDialog(obj_name, model.displayed_database, self) + self.edit_dialog.database_modified.connect(self.database_modified) def _open_add_dialog(self) -> QDialog: model: IodeAbstractTableModel = self.model() @@ -135,8 +137,8 @@ def __init__(self, parent=None): def _open_edit_dialog(self, obj_name: str): model: IodeAbstractTableModel = self.model() - # self.edit_dialog = EditListDialog(obj_name, model.displayed_database, self) - # self.edit_dialog.database_modified.connect(self.database_modified) + self.edit_dialog = EditListDialog(obj_name, model.displayed_database, self) + self.edit_dialog.database_modified.connect(self.database_modified) def _open_add_dialog(self) -> QDialog: model: IodeAbstractTableModel = self.model() @@ -153,11 +155,6 @@ def __init__(self, parent=None): self.horizontalHeader().setStretchLastSection(False) self.verticalHeader().setStretchLastSection(False) - def _open_edit_dialog(self, obj_name: str): - model: IodeAbstractTableModel = self.model() - # self.edit_dialog = EditScalarDialog(obj_name, model.displayed_database, self) - # self.edit_dialog.database_modified.connect(self.database_modified) - def _open_add_dialog(self) -> QDialog: model: IodeAbstractTableModel = self.model() return AddScalarDialog(model.displayed_database, self) @@ -312,11 +309,6 @@ def setup_context_menu(self): action.triggered.connect(self.open_graphs_dialog) self.context_menu.addAction(action) - def _open_edit_dialog(self, obj_name: str): - model: IodeAbstractTableModel = self.model() - # self.edit_dialog = EditVariableDialog(obj_name, model.displayed_database, self) - # self.edit_dialog.database_modified.connect(self.database_modified) - def _open_add_dialog(self) -> QDialog: if not self.check_global_sample(): return None