Skip to content

Commit 9d4a1c4

Browse files
authored
Merge pull request #1367 from mrvisscher/menu-bar-refactor
Menu bar refactor
2 parents 1075bcd + 6ef655a commit 9d4a1c4

1 file changed

Lines changed: 115 additions & 76 deletions

File tree

activity_browser/ui/menu_bar.py

Lines changed: 115 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -1,144 +1,181 @@
11
import os
2+
from importlib.metadata import version
3+
24
from PySide2 import QtGui, QtWidgets
35
from PySide2.QtCore import QSize, QUrl, Slot
46

5-
from activity_browser import actions, signals
7+
from activity_browser import actions, signals, application, info
68
from activity_browser.mod import bw2data as bd
79

8-
from ..info import __version__ as ab_version
910
from .icons import qicons
1011

1112
AB_BW25 = True if os.environ.get("AB_BW25", False) else False
1213

1314

1415
class MenuBar(QtWidgets.QMenuBar):
16+
"""
17+
Main menu bar at the top of the Activity Browser window. Contains submenus for different user interaction categories
18+
"""
1519
def __init__(self, window):
1620
super().__init__(parent=window)
17-
self.window = window
18-
self.file_menu = QtWidgets.QMenu("&Project", self.window)
19-
self.view_menu = QtWidgets.QMenu("&View", self.window)
20-
self.windows_menu = QtWidgets.QMenu("&Windows", self.window)
21-
self.tools_menu = QtWidgets.QMenu("&Tools", self.window)
22-
self.help_menu = QtWidgets.QMenu("&Help", self.window)
21+
22+
self.addMenu(ProjectMenu(self))
23+
self.addMenu(ViewMenu(self))
24+
self.addMenu(ToolsMenu(self))
25+
self.addMenu(HelpMenu(self))
26+
27+
28+
class ProjectMenu(QtWidgets.QMenu):
29+
"""
30+
Project menu: contains actions related to managing the project, such as project duplication, database importing etc.
31+
"""
32+
33+
def __init__(self, parent=None) -> None:
34+
super().__init__(parent)
35+
36+
self.setTitle("&Project")
2337

2438
self.new_proj_action = actions.ProjectNew.get_QAction()
2539
self.dup_proj_action = actions.ProjectDuplicate.get_QAction()
2640
self.delete_proj_action = actions.ProjectDelete.get_QAction()
41+
2742
self.import_proj_action = actions.ProjectImport.get_QAction()
2843
self.export_proj_action = actions.ProjectExport.get_QAction()
44+
2945
self.import_db_action = actions.DatabaseImport.get_QAction()
3046
self.export_db_action = actions.DatabaseExport.get_QAction()
3147
self.update_biosphere_action = actions.BiosphereUpdate.get_QAction()
32-
self.manage_settings_action = actions.SettingsWizardOpen.get_QAction()
33-
self.manage_plugins_action = actions.PluginWizardOpen.get_QAction()
3448

35-
self.addMenu(self.file_menu)
36-
self.addMenu(self.view_menu)
37-
self.addMenu(self.tools_menu)
38-
self.addMenu(self.help_menu)
49+
self.manage_settings_action = actions.SettingsWizardOpen.get_QAction()
3950

40-
self.setup_file_menu()
41-
self.setup_view_menu()
42-
self.setup_tools_menu()
43-
self.setup_help_menu()
44-
self.connect_signals()
51+
self.addMenu(ProjectSelectionMenu(self))
52+
self.addAction(self.new_proj_action)
53+
self.addAction(self.dup_proj_action)
54+
self.addAction(self.delete_proj_action)
55+
self.addSeparator()
56+
self.addAction(self.import_proj_action)
57+
self.addAction(self.export_proj_action)
58+
self.addSeparator()
59+
self.addAction(self.import_db_action)
60+
self.addAction(self.export_db_action)
61+
self.addAction(self.update_biosphere_action)
62+
self.addSeparator()
63+
self.addMenu(MigrationsMenu(self))
64+
self.addSeparator()
65+
self.addAction(self.manage_settings_action)
4566

46-
def connect_signals(self):
4767
bd.projects.current_changed.connect(self.biosphere_exists)
4868
bd.databases.metadata_changed.connect(self.biosphere_exists)
4969

50-
def setup_file_menu(self) -> None:
51-
"""Build the menu for specific importing/export/updating actions."""
52-
self.file_menu.addMenu(ProjectsMenu(self))
53-
self.file_menu.addAction(self.new_proj_action)
54-
self.file_menu.addAction(self.dup_proj_action)
55-
self.file_menu.addAction(self.delete_proj_action)
56-
self.file_menu.addSeparator()
57-
self.file_menu.addAction(self.import_proj_action)
58-
self.file_menu.addAction(self.export_proj_action)
59-
self.file_menu.addSeparator()
60-
self.file_menu.addAction(self.import_db_action)
61-
self.file_menu.addAction(self.export_db_action)
62-
self.file_menu.addAction(self.update_biosphere_action)
63-
self.file_menu.addSeparator()
64-
self.file_menu.addMenu(MigrationsMenu(self))
65-
self.file_menu.addSeparator()
66-
self.file_menu.addAction(self.manage_settings_action)
67-
68-
def setup_view_menu(self) -> None:
69-
"""Build the menu for viewing or hiding specific tabs"""
70-
self.view_menu.addAction(
70+
def biosphere_exists(self) -> None:
71+
"""Test if the default biosphere exists as a database in the project"""
72+
exists = True if bd.config.biosphere in bd.databases else False
73+
self.update_biosphere_action.setEnabled(exists)
74+
self.import_db_action.setEnabled(exists)
75+
76+
77+
class ViewMenu(QtWidgets.QMenu):
78+
"""
79+
View menu: contains actions in regard to hiding and showing specific UI elements.
80+
"""
81+
82+
def __init__(self, parent=None) -> None:
83+
super().__init__(parent)
84+
85+
self.setTitle("&View")
86+
87+
self.addAction(
7188
qicons.graph_explorer,
7289
"&Graph Explorer",
7390
lambda: signals.toggle_show_or_hide_tab.emit("Graph Explorer"),
7491
)
75-
self.view_menu.addAction(
92+
self.addAction(
7693
qicons.history,
7794
"&Activity History",
7895
lambda: signals.toggle_show_or_hide_tab.emit("History"),
7996
)
80-
self.view_menu.addAction(
97+
self.addAction(
8198
qicons.welcome,
8299
"&Welcome screen",
83100
lambda: signals.toggle_show_or_hide_tab.emit("Welcome"),
84101
)
85102

86-
def setup_tools_menu(self) -> None:
87-
"""Build the tools menu for the menubar."""
88-
self.tools_menu.addAction(self.manage_plugins_action)
89103

90-
def setup_help_menu(self) -> None:
91-
"""Build the help menu for the menubar."""
92-
self.help_menu.addAction(
93-
self.window.icon, "&About Activity Browser", self.about
104+
class ToolsMenu(QtWidgets.QMenu):
105+
"""
106+
Tools Menu: contains actions in regard to special tooling aspects of the AB
107+
"""
108+
109+
def __init__(self, parent=None) -> None:
110+
super().__init__(parent)
111+
self.setTitle("&Tools")
112+
113+
self.manage_plugins_action = actions.PluginWizardOpen.get_QAction()
114+
115+
self.addAction(self.manage_plugins_action)
116+
117+
118+
class HelpMenu(QtWidgets.QMenu):
119+
"""
120+
Help Menu: contains actions that show info to the user or redirect them to online resources
121+
"""
122+
123+
def __init__(self, parent=None) -> None:
124+
super().__init__(parent)
125+
self.setTitle("&Help")
126+
127+
self.addAction(
128+
qicons.ab, "&About Activity Browser", self.about
94129
)
95-
self.help_menu.addAction(
96-
"&About Qt", lambda: QtWidgets.QMessageBox.aboutQt(self.window)
130+
self.addAction(
131+
"&About Qt", lambda: QtWidgets.QMessageBox.aboutQt(application.main_window)
97132
)
98-
self.help_menu.addAction(
133+
self.addAction(
99134
qicons.question, "&Get help on the wiki", self.open_wiki
100135
)
101-
self.help_menu.addAction(
136+
self.addAction(
102137
qicons.issue, "&Report an idea/issue on GitHub", self.raise_issue_github
103138
)
104139

105140
def about(self):
106-
text = """
107-
Activity Browser - a graphical interface for Brightway2.<br><br>
108-
Application version: <b>{}</b><br><br>
109-
All development happens on <a href="https://github.com/LCA-ActivityBrowser/activity-browser">github</a>.<br><br>
110-
For copyright information please see the copyright on <a href="https://github.com/LCA-ActivityBrowser/activity-browser/tree/main#copyright">this page</a>.<br><br>
111-
For license information please see the copyright on <a href="https://github.com/LCA-ActivityBrowser/activity-browser/blob/main/LICENSE.txt">this page</a>.<br><br>
112-
"""
113-
msgBox = QtWidgets.QMessageBox(parent=self.window)
114-
msgBox.setWindowTitle("About the Activity Browser")
115-
pixmap = self.window.icon.pixmap(QSize(150, 150))
116-
msgBox.setIconPixmap(pixmap)
117-
msgBox.setWindowIcon(self.window.icon)
118-
msgBox.setText(text.format(ab_version))
119-
msgBox.exec_()
141+
"""Displays an 'about' window to the user containing e.g. the version of the AB and copyright info"""
142+
# set the window text in html format
143+
text = f"""
144+
Activity Browser - a graphical interface for Brightway2.<br><br>
145+
Application version: <b>{version("activity_browser")}</b><br>
146+
bw2data version: <b>{version("bw2data")}</b><br>
147+
bw2io version: <b>{version("bw2calc")}</b><br>
148+
bw2calc version: <b>{version("bw2io")}</b><br><br>
149+
All development happens on <a href="https://github.com/LCA-ActivityBrowser/activity-browser">github</a>.<br><br>
150+
For copyright information please see the copyright on <a href="https://github.com/LCA-ActivityBrowser/activity-browser/tree/main#copyright">this page</a>.<br><br>
151+
For license information please see the copyright on <a href="https://github.com/LCA-ActivityBrowser/activity-browser/blob/main/LICENSE.txt">this page</a>.<br><br>
152+
"""
153+
154+
# set up the window
155+
about_window = QtWidgets.QMessageBox(parent=application.main_window)
156+
about_window.setWindowTitle("About the Activity Browser")
157+
about_window.setIconPixmap(qicons.ab.pixmap(QSize(150, 150)))
158+
about_window.setText(text)
159+
160+
# execute
161+
about_window.exec_()
120162

121163
def open_wiki(self):
164+
"""Opens the AB github wiki in the users default browser"""
122165
url = QUrl(
123166
"https://github.com/LCA-ActivityBrowser/activity-browser/wiki"
124167
)
125168
QtGui.QDesktopServices.openUrl(url)
126169

127170
def raise_issue_github(self):
171+
"""Opens the github create issue page in the users default browser"""
128172
url = QUrl(
129173
"https://github.com/LCA-ActivityBrowser/activity-browser/issues/new/choose"
130174
)
131175
QtGui.QDesktopServices.openUrl(url)
132176

133-
@Slot(name="testBiosphereExists")
134-
def biosphere_exists(self) -> None:
135-
"""Test if the default biosphere exists as a database in the project"""
136-
exists = True if bd.config.biosphere in bd.databases else False
137-
self.update_biosphere_action.setEnabled(exists)
138-
self.import_db_action.setEnabled(exists)
139-
140177

141-
class ProjectsMenu(QtWidgets.QMenu):
178+
class ProjectSelectionMenu(QtWidgets.QMenu):
142179
"""
143180
Menu that lists all the projects available through bw2data.projects
144181
"""
@@ -180,6 +217,8 @@ def populate(self):
180217

181218

182219
class MigrationsMenu(QtWidgets.QMenu):
220+
"""Menu that shows actions that regard to brightway migrations"""
221+
183222
def __init__(self, parent=None) -> None:
184223
super().__init__(parent)
185224

0 commit comments

Comments
 (0)