From 0aeb2f2530f5b3eeed8b2657491adad29f595d8e Mon Sep 17 00:00:00 2001 From: Pierre-Antoine Rouby Date: Tue, 4 Jul 2023 15:34:35 +0200 Subject: [PATCH] Debug: Add debug python repl. --- src/View/Configure/Window.py | 1 + src/View/Debug/Window.py | 81 ++++++++++++++++++++++++++++++++++++ src/View/MainWindow.py | 32 +++++++++++++- src/View/ui/DebugRepl.ui | 68 ++++++++++++++++++++++++++++++ 4 files changed, 181 insertions(+), 1 deletion(-) create mode 100644 src/View/Debug/Window.py create mode 100644 src/View/ui/DebugRepl.ui diff --git a/src/View/Configure/Window.py b/src/View/Configure/Window.py index 8d941180..69d64a69 100644 --- a/src/View/Configure/Window.py +++ b/src/View/Configure/Window.py @@ -216,6 +216,7 @@ class ConfigureWindow(ASubWindow, ListedSubWindow): def set_debug(self): self.conf.debug = not self.conf.debug print(f"[DEBUG] Debug mode set : {self.conf.debug}") + self.parent.setup_debug_mode() # Solvers diff --git a/src/View/Debug/Window.py b/src/View/Debug/Window.py new file mode 100644 index 00000000..dd26d91e --- /dev/null +++ b/src/View/Debug/Window.py @@ -0,0 +1,81 @@ +# -*- coding: utf-8 -*- + +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, QRect, QThread, + pyqtSlot, pyqtSignal, +) + +from PyQt5.QtWidgets import ( + QDialogButtonBox, QPushButton, QLineEdit, + QFileDialog, QTableView, QAbstractItemView, + QUndoStack, QShortcut, QAction, QItemDelegate, + QComboBox, QVBoxLayout, QHeaderView, QTabWidget, + QProgressBar, QLabel, QTextEdit, +) + +_translate = QCoreApplication.translate + +class ReplWindow(ASubMainWindow, ListedSubWindow): + def __init__(self, title="Debug REPL", + study=None, config=None, + solver=None, parent=None): + self._title = title + + self._study = study + self._config = config + self._parent = parent + + super(ReplWindow, self).__init__( + name=self._title, ui="DebugRepl", parent=parent + ) + self.ui.setWindowTitle(self._title) + + self.__debug_exec_result__ = None + self._history = [] + self._history_ind = 0 + + self.setup_connections() + + def setup_connections(self): + self._hup_sc = QShortcut(QKeySequence("Up"), self) + self._hdown_sc = QShortcut(QKeySequence("Down"), self) + self._hup_sc.activated.connect(self.history_up) + self._hdown_sc.activated.connect(self.history_down) + + self.find(QPushButton, "pushButton").clicked.connect(self.eval_python) + + def history_up(self): + if self._history_ind < len(self._history): + self._history_ind += 1 + self.set_line_edit_text("lineEdit", self._history[-self._history_ind]) + + def history_down(self): + if self._history_ind > 0: + self._history_ind -= 1 + self.set_line_edit_text("lineEdit", self._history[-self._history_ind]) + + def eval_python(self): + code = self.get_line_edit_text("lineEdit") + self._history.append(code) + self.set_line_edit_text("lineEdit", "") + self._history_ind = 0 + + code = "self.__debug_exec_result__ = " + code + print(f"[DEBUG] ! {code}") + value = exec(code) + value = self.__debug_exec_result__ + + msg = f" # " + code + " #" + self.find(QTextEdit, "textEdit").append(msg) + + self.find(QTextEdit, "textEdit").append(str(value)) diff --git a/src/View/MainWindow.py b/src/View/MainWindow.py index 5150409c..7953f3e3 100644 --- a/src/View/MainWindow.py +++ b/src/View/MainWindow.py @@ -13,7 +13,7 @@ from PyQt5.QtCore import ( ) from PyQt5.QtWidgets import ( QMainWindow, QApplication, QAction, - QFileDialog, QShortcut, + QFileDialog, QShortcut, QMenu, QToolBar, ) from PyQt5.uic import loadUi @@ -34,6 +34,7 @@ from View.Sections.Window import SectionsWindow from View.SolverParameters.Window import SolverParametersWindow from View.RunSolver.Window import SelectSolverWindow, SolverLogWindow from View.CheckList.Window import CheckListWindow +from View.Debug.Window import ReplWindow from Model.Study import Study @@ -91,6 +92,7 @@ class ApplicationWindow(QMainWindow, ListedSubWindow, WindowToolKit): self.setup_sc() self.setup_connection() self.default_style() + self.setup_debug_mode(init = True) self.trans = QTranslator(self) #self.ui.retranslateUi() @@ -183,6 +185,22 @@ class ApplicationWindow(QMainWindow, ListedSubWindow, WindowToolKit): # Maximise window self.showMaximized() + def setup_debug_mode(self, init = False): + menu = self.findChild(QMenu, "menu_help") + + if init: + self.debug_action = QAction("Debug", self) + self.debug_action.setStatusTip("Debug") + self.debug_action.triggered.connect(self.open_debug) + + if self.conf.debug: + menu.addAction(self.debug_action) + else: + if self.conf.debug: + menu.addAction(self.debug_action) + else: + menu.removeAction(self.debug_action) + ######### # MODEL # ######### @@ -423,6 +441,18 @@ class ApplicationWindow(QMainWindow, ListedSubWindow, WindowToolKit): ) sol.show() + ######### + # DEBUG # + ######### + + def open_debug(self): + repl = ReplWindow( + study = self.model, + config = self.conf, + parent = self + ) + repl.show() + # TODO: Delete me ! ############### # DUMMY STUFF # diff --git a/src/View/ui/DebugRepl.ui b/src/View/ui/DebugRepl.ui new file mode 100644 index 00000000..f889061d --- /dev/null +++ b/src/View/ui/DebugRepl.ui @@ -0,0 +1,68 @@ + + + MainWindow + + + + 0 + 0 + 640 + 480 + + + + MainWindow + + + + + + + + + + + + + + + Eval + + + + ressources/player_play.pngressources/player_play.png + + + Ctrl+Return + + + false + + + + + + + + + true + + + + + + + + + 0 + 0 + 640 + 22 + + + + + + + +