Skip to content

Commit

Permalink
GUI PYTHON: (edit_iode_obj.py) implemented
Browse files Browse the repository at this point in the history
            Edit[Comment|Identity|List]Dialog classes
  • Loading branch information
alixdamman committed Nov 4, 2024
1 parent a61b116 commit 5efda0e
Show file tree
Hide file tree
Showing 5 changed files with 145 additions and 18 deletions.
Empty file removed gui/iode_objs/edit/edit_comment.py
Empty file.
Empty file.
135 changes: 135 additions & 0 deletions gui/iode_objs/edit/edit_iode_obj.py
Original file line number Diff line number Diff line change
@@ -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)


Empty file removed gui/iode_objs/edit/edit_list.py
Empty file.
28 changes: 10 additions & 18 deletions gui/iode_objs/views/table_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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()
Expand All @@ -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()
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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()
Expand All @@ -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)
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 5efda0e

Please sign in to comment.