|
1 | 1 | import os |
| 2 | +from importlib.metadata import version |
| 3 | + |
2 | 4 | from PySide2 import QtGui, QtWidgets |
3 | 5 | from PySide2.QtCore import QSize, QUrl, Slot |
4 | 6 |
|
5 | | -from activity_browser import actions, signals |
| 7 | +from activity_browser import actions, signals, application, info |
6 | 8 | from activity_browser.mod import bw2data as bd |
7 | 9 |
|
8 | | -from ..info import __version__ as ab_version |
9 | 10 | from .icons import qicons |
10 | 11 |
|
11 | 12 | AB_BW25 = True if os.environ.get("AB_BW25", False) else False |
12 | 13 |
|
13 | 14 |
|
14 | 15 | 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 | + """ |
15 | 19 | def __init__(self, window): |
16 | 20 | 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") |
23 | 37 |
|
24 | 38 | self.new_proj_action = actions.ProjectNew.get_QAction() |
25 | 39 | self.dup_proj_action = actions.ProjectDuplicate.get_QAction() |
26 | 40 | self.delete_proj_action = actions.ProjectDelete.get_QAction() |
| 41 | + |
27 | 42 | self.import_proj_action = actions.ProjectImport.get_QAction() |
28 | 43 | self.export_proj_action = actions.ProjectExport.get_QAction() |
| 44 | + |
29 | 45 | self.import_db_action = actions.DatabaseImport.get_QAction() |
30 | 46 | self.export_db_action = actions.DatabaseExport.get_QAction() |
31 | 47 | 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() |
34 | 48 |
|
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() |
39 | 50 |
|
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) |
45 | 66 |
|
46 | | - def connect_signals(self): |
47 | 67 | bd.projects.current_changed.connect(self.biosphere_exists) |
48 | 68 | bd.databases.metadata_changed.connect(self.biosphere_exists) |
49 | 69 |
|
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( |
71 | 88 | qicons.graph_explorer, |
72 | 89 | "&Graph Explorer", |
73 | 90 | lambda: signals.toggle_show_or_hide_tab.emit("Graph Explorer"), |
74 | 91 | ) |
75 | | - self.view_menu.addAction( |
| 92 | + self.addAction( |
76 | 93 | qicons.history, |
77 | 94 | "&Activity History", |
78 | 95 | lambda: signals.toggle_show_or_hide_tab.emit("History"), |
79 | 96 | ) |
80 | | - self.view_menu.addAction( |
| 97 | + self.addAction( |
81 | 98 | qicons.welcome, |
82 | 99 | "&Welcome screen", |
83 | 100 | lambda: signals.toggle_show_or_hide_tab.emit("Welcome"), |
84 | 101 | ) |
85 | 102 |
|
86 | | - def setup_tools_menu(self) -> None: |
87 | | - """Build the tools menu for the menubar.""" |
88 | | - self.tools_menu.addAction(self.manage_plugins_action) |
89 | 103 |
|
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 |
94 | 129 | ) |
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) |
97 | 132 | ) |
98 | | - self.help_menu.addAction( |
| 133 | + self.addAction( |
99 | 134 | qicons.question, "&Get help on the wiki", self.open_wiki |
100 | 135 | ) |
101 | | - self.help_menu.addAction( |
| 136 | + self.addAction( |
102 | 137 | qicons.issue, "&Report an idea/issue on GitHub", self.raise_issue_github |
103 | 138 | ) |
104 | 139 |
|
105 | 140 | 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_() |
120 | 162 |
|
121 | 163 | def open_wiki(self): |
| 164 | + """Opens the AB github wiki in the users default browser""" |
122 | 165 | url = QUrl( |
123 | 166 | "https://github.com/LCA-ActivityBrowser/activity-browser/wiki" |
124 | 167 | ) |
125 | 168 | QtGui.QDesktopServices.openUrl(url) |
126 | 169 |
|
127 | 170 | def raise_issue_github(self): |
| 171 | + """Opens the github create issue page in the users default browser""" |
128 | 172 | url = QUrl( |
129 | 173 | "https://github.com/LCA-ActivityBrowser/activity-browser/issues/new/choose" |
130 | 174 | ) |
131 | 175 | QtGui.QDesktopServices.openUrl(url) |
132 | 176 |
|
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 | | - |
140 | 177 |
|
141 | | -class ProjectsMenu(QtWidgets.QMenu): |
| 178 | +class ProjectSelectionMenu(QtWidgets.QMenu): |
142 | 179 | """ |
143 | 180 | Menu that lists all the projects available through bw2data.projects |
144 | 181 | """ |
@@ -180,6 +217,8 @@ def populate(self): |
180 | 217 |
|
181 | 218 |
|
182 | 219 | class MigrationsMenu(QtWidgets.QMenu): |
| 220 | + """Menu that shows actions that regard to brightway migrations""" |
| 221 | + |
183 | 222 | def __init__(self, parent=None) -> None: |
184 | 223 | super().__init__(parent) |
185 | 224 |
|
|
0 commit comments