Skip to content

Commit

Permalink
Add the env docs button and window
Browse files Browse the repository at this point in the history
  • Loading branch information
zhe-slac committed Oct 9, 2024
1 parent c79f172 commit 07c2c16
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/badger/gui/default/components/env_cbox.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
9 changes: 9 additions & 0 deletions src/badger/gui/default/components/routine_page.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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 = []
Expand Down Expand Up @@ -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())
Expand Down
61 changes: 61 additions & 0 deletions src/badger/gui/default/windows/env_docs_window.py
Original file line number Diff line number Diff line change
@@ -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()

0 comments on commit 07c2c16

Please sign in to comment.