Skip to content

Commit 7fef9e5

Browse files
committed
FEATURE: Automatically open documentation in browser
1 parent 8b21cf5 commit 7fef9e5

File tree

2 files changed

+62
-6
lines changed

2 files changed

+62
-6
lines changed

MethodicConfigurator/backend_filesystem_program_settings.py

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,18 @@
2121
from re import escape as re_escape
2222
from re import match as re_match
2323
from re import sub as re_sub
24-
from typing import Any
24+
from typing import Any, Union
2525

2626
from platformdirs import site_config_dir, user_config_dir
2727

2828
from MethodicConfigurator import _
2929

30+
SETTINGS_DEFAULTS: dict[str, Union[int, bool]] = {
31+
"Format version": 1,
32+
"auto_open_doc_in_browser": True,
33+
"annotate_docs_into_param_files": False,
34+
}
35+
3036

3137
class ProgramSettings:
3238
"""
@@ -125,7 +131,7 @@ def __get_settings_as_dict() -> dict[str, Any]:
125131
pass
126132

127133
if "Format version" not in settings:
128-
settings["Format version"] = 1
134+
settings["Format version"] = SETTINGS_DEFAULTS["Format version"]
129135

130136
if "directory_selection" not in settings:
131137
settings["directory_selection"] = {}
@@ -137,6 +143,11 @@ def __get_settings_as_dict() -> dict[str, Any]:
137143
if "parameter_editor" not in settings["display_usage_popup"]:
138144
settings["display_usage_popup"]["parameter_editor"] = True
139145

146+
if "auto_open_doc_in_browser" not in settings:
147+
settings["auto_open_doc_in_browser"] = SETTINGS_DEFAULTS["auto_open_doc_in_browser"]
148+
if "annotate_docs_into_param_files" not in settings:
149+
settings["annotate_docs_into_param_files"] = SETTINGS_DEFAULTS["annotate_docs_into_param_files"]
150+
140151
return settings
141152

142153
@staticmethod
@@ -235,3 +246,16 @@ def set_display_usage_popup(ptype: str, value: bool) -> None:
235246
settings, _, _ = ProgramSettings.__get_settings_config()
236247
settings["display_usage_popup"][ptype] = value
237248
ProgramSettings.__set_settings_from_dict(settings)
249+
250+
@staticmethod
251+
def get_setting(setting: str) -> Union[int, bool]:
252+
if setting in SETTINGS_DEFAULTS:
253+
return ProgramSettings.__get_settings_as_dict().get(setting, SETTINGS_DEFAULTS[setting])
254+
return False
255+
256+
@staticmethod
257+
def set_setting(setting: str, value: Union[int, bool]) -> None:
258+
if setting in SETTINGS_DEFAULTS:
259+
settings, _, _ = ProgramSettings.__get_settings_config()
260+
settings[setting] = value
261+
ProgramSettings.__set_settings_from_dict(settings)

MethodicConfigurator/frontend_tkinter_parameter_editor.py

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,12 +52,13 @@ class DocumentationFrame: # pylint: disable=too-few-public-methods
5252
functionality to open these links in a web browser.
5353
"""
5454

55-
def __init__(self, root: tk.Widget, local_filesystem, current_file: str) -> None:
55+
def __init__(self, root: tk.Widget, local_filesystem: LocalFilesystem, current_file: str) -> None:
5656
self.root = root
5757
self.local_filesystem = local_filesystem
5858
self.current_file = current_file
5959
self.documentation_frame: ttk.LabelFrame
6060
self.documentation_labels: dict[str, ttk.Label] = {}
61+
self.auto_open_var = tk.BooleanVar(value=bool(ProgramSettings.get_setting("auto_open_doc_in_browser")))
6162
self.__create_documentation_frame()
6263

6364
def __create_documentation_frame(self) -> None:
@@ -84,9 +85,32 @@ def __create_documentation_frame(self) -> None:
8485
label.grid(row=i, column=0, sticky="w")
8586
show_tooltip(label, descriptive_tooltips[i])
8687

87-
# Create labels for the second column with the documentation links
88-
self.documentation_labels[text] = ttk.Label(documentation_grid)
89-
self.documentation_labels[text].grid(row=i, column=1, sticky="w")
88+
if i == 3:
89+
bottom_frame = ttk.Frame(documentation_grid)
90+
bottom_frame.grid(row=i, column=1, sticky="ew") # ew to stretch horizontally
91+
92+
self.documentation_labels[text] = ttk.Label(bottom_frame)
93+
self.documentation_labels[text].pack(side=tk.LEFT, fill="x", expand=True)
94+
auto_open_checkbox = ttk.Checkbutton(
95+
bottom_frame,
96+
text=_("Automatically open documentation links in browser"),
97+
variable=self.auto_open_var,
98+
command=lambda: ProgramSettings.set_setting("auto_open_doc_in_browser", self.auto_open_var.get()),
99+
)
100+
show_tooltip(
101+
auto_open_checkbox,
102+
_(
103+
"Automatically open all the above documentation links in a browser\n"
104+
"whenever the current intermediate parameter file changes"
105+
),
106+
)
107+
auto_open_checkbox.pack(side=tk.LEFT, expand=False)
108+
else:
109+
# Create labels for the second column with the documentation links
110+
self.documentation_labels[text] = ttk.Label(documentation_grid)
111+
self.documentation_labels[text].grid(row=i, column=1, sticky="ew")
112+
documentation_grid.columnconfigure(0, weight=0)
113+
documentation_grid.columnconfigure(1, weight=1)
90114

91115
# Dynamically update the documentation text and URL links
92116
self.update_documentation_labels(self.current_file)
@@ -111,6 +135,14 @@ def update_documentation_labels(self, current_file: str) -> None:
111135
mandatory_text, mandatory_url = self.local_filesystem.get_documentation_text_and_url(current_file, "mandatory")
112136
self.__update_documentation_label(_("Mandatory:"), mandatory_text, mandatory_url, False)
113137

138+
if self.auto_open_var.get():
139+
if wiki_url:
140+
webbrowser_open(url=wiki_url, new=0, autoraise=False)
141+
if external_tool_url:
142+
webbrowser_open(url=external_tool_url, new=0, autoraise=False)
143+
if blog_url:
144+
webbrowser_open(url=blog_url, new=0, autoraise=True)
145+
114146
def __update_documentation_label(self, label_key, text, url, url_expected=True) -> None:
115147
label = self.documentation_labels[label_key]
116148
if url:

0 commit comments

Comments
 (0)