From 07c2c16c49cceeb8b31d228b14f93ba5bcb05b46 Mon Sep 17 00:00:00 2001 From: Zhe Zhang Date: Wed, 9 Oct 2024 14:11:48 -0700 Subject: [PATCH] Add the env docs button and window --- src/badger/gui/default/components/env_cbox.py | 3 + .../gui/default/components/routine_page.py | 9 +++ .../gui/default/windows/env_docs_window.py | 61 +++++++++++++++++++ 3 files changed, 73 insertions(+) create mode 100644 src/badger/gui/default/windows/env_docs_window.py diff --git a/src/badger/gui/default/components/env_cbox.py b/src/badger/gui/default/components/env_cbox.py index 11a097dd..15f69207 100644 --- a/src/badger/gui/default/components/env_cbox.py +++ b/src/badger/gui/default/components/env_cbox.py @@ -58,9 +58,12 @@ def init_ui(self): btn_env_play.setFixedSize(128, 24) if not strtobool(read_value('BADGER_ENABLE_ADVANCED')): btn_env_play.hide() + self.btn_docs = btn_docs = QPushButton('Open Docs') + btn_docs.setFixedSize(128, 24) hbox_name.addWidget(lbl) hbox_name.addWidget(cb, 1) hbox_name.addWidget(btn_env_play) + hbox_name.addWidget(btn_docs) vbox.addWidget(name) params = QWidget() diff --git a/src/badger/gui/default/components/routine_page.py b/src/badger/gui/default/components/routine_page.py index 0bb65a19..9fd9f3fe 100644 --- a/src/badger/gui/default/components/routine_page.py +++ b/src/badger/gui/default/components/routine_page.py @@ -31,6 +31,7 @@ from .filter_cbox import BadgerFilterBox from .state_item import state_item from ..windows.docs_window import BadgerDocsWindow +from ..windows.env_docs_window import BadgerEnvDocsWindow from ..windows.edit_script_dialog import BadgerEditScriptDialog from ..windows.lim_vrange_dialog import BadgerLimitVariableRangeDialog from ..windows.review_dialog import BadgerReviewDialog @@ -69,6 +70,7 @@ def __init__(self): self.routine = None self.script = '' self.window_docs = BadgerDocsWindow(self, '') + self.window_env_docs = BadgerEnvDocsWindow(self, '') self.vars_env = None # needed for passing env vars to the var table # Limit variable ranges @@ -185,6 +187,7 @@ def config_logic(self): self.generator_box.btn_edit_script.clicked.connect(self.edit_script) self.env_box.cb.currentIndexChanged.connect(self.select_env) self.env_box.btn_env_play.clicked.connect(self.open_playground) + self.env_box.btn_docs.clicked.connect(self.open_environment_docs) self.env_box.btn_add_var.clicked.connect(self.add_var) self.env_box.btn_lim_vrange.clicked.connect(self.limit_variable_ranges) self.env_box.btn_add_con.clicked.connect(self.add_constraint) @@ -520,6 +523,9 @@ def select_env(self, i): self.env_box.update_stylesheets(env.name) + # Update the docs + self.window_env_docs.update_docs(env.name) + def get_init_table_header(self): table = self.env_box.init_table header_list = [] @@ -636,6 +642,9 @@ def open_playground(self): def open_generator_docs(self): self.window_docs.show() + def open_environment_docs(self): + self.window_env_docs.show() + def add_var(self): # TODO: Use a cached env env_params = load_config(self.env_box.edit.toPlainText()) diff --git a/src/badger/gui/default/windows/env_docs_window.py b/src/badger/gui/default/windows/env_docs_window.py new file mode 100644 index 00000000..ace50100 --- /dev/null +++ b/src/badger/gui/default/windows/env_docs_window.py @@ -0,0 +1,61 @@ +from PyQt5.QtWidgets import QTextEdit, QHBoxLayout, QVBoxLayout, QCheckBox, QWidget, QMainWindow +from ....factory import get_env_docs + + +class BadgerEnvDocsWindow(QMainWindow): + def __init__(self, parent, env_name): + super().__init__(parent=parent) + + self.env_name = env_name + self.render_md = True + self.docs = None + + self.init_ui() + self.config_logic() + self.load_docs() + + def init_ui(self): + self.setWindowTitle(f'Docs for environment {self.env_name}') + self.resize(640, 640) + + doc_panel = QWidget(self) + vbox = QVBoxLayout(doc_panel) + + # Toolbar + toolbar = QWidget() + hbox_tool = QHBoxLayout(toolbar) + hbox_tool.setContentsMargins(0, 0, 0, 0) + self.cb_md = cb_md = QCheckBox('Render as Markdown') + cb_md.setChecked(True) + hbox_tool.addStretch() + hbox_tool.addWidget(cb_md) + vbox.addWidget(toolbar) + + self.markdown_viewer = QTextEdit() + self.markdown_viewer.setReadOnly(True) + vbox.addWidget(self.markdown_viewer) + + self.setCentralWidget(doc_panel) + + def config_logic(self): + self.cb_md.stateChanged.connect(self.switch_render_mode) + + def load_docs(self): + try: + self.docs = docs = get_env_docs(self.env_name) + except Exception as e: + self.docs = docs = str(e) + + if self.render_md: + self.markdown_viewer.setMarkdown(docs) + else: + self.markdown_viewer.setText(docs) + + def update_docs(self, env_name): + self.env_name = env_name + self.setWindowTitle(f'Docs for environment {env_name}') + self.load_docs() + + def switch_render_mode(self): + self.render_md = self.cb_md.isChecked() + self.load_docs()