1+ import os
12from PySide2 import QtGui , QtWidgets
23from PySide2 .QtCore import QSize , QUrl , Slot
34
78from ..info import __version__ as ab_version
89from .icons import qicons
910
11+ AB_BW25 = True if os .environ .get ("AB_BW25" , False ) else False
12+
1013
1114class MenuBar (QtWidgets .QMenuBar ):
1215 def __init__ (self , window ):
@@ -46,6 +49,7 @@ def connect_signals(self):
4649
4750 def setup_file_menu (self ) -> None :
4851 """Build the menu for specific importing/export/updating actions."""
52+ self .file_menu .addMenu (ProjectsMenu (self ))
4953 self .file_menu .addAction (self .new_proj_action )
5054 self .file_menu .addAction (self .dup_proj_action )
5155 self .file_menu .addAction (self .delete_proj_action )
@@ -92,7 +96,7 @@ def setup_help_menu(self) -> None:
9296 "&About Qt" , lambda : QtWidgets .QMessageBox .aboutQt (self .window )
9397 )
9498 self .help_menu .addAction (
95- qicons .question , "&Get help on the wiki" , self .open_wiki ()
99+ qicons .question , "&Get help on the wiki" , self .open_wiki
96100 )
97101 self .help_menu .addAction (
98102 qicons .issue , "&Report an idea/issue on GitHub" , self .raise_issue_github
@@ -134,6 +138,47 @@ def biosphere_exists(self) -> None:
134138 self .import_db_action .setEnabled (exists )
135139
136140
141+ class ProjectsMenu (QtWidgets .QMenu ):
142+ """
143+ Menu that lists all the projects available through bw2data.projects
144+ """
145+ def __init__ (self , parent = None ):
146+ super ().__init__ (parent )
147+ self .setTitle ("Open project" )
148+ self .populate ()
149+
150+ self .aboutToShow .connect (self .populate )
151+ self .triggered .connect (lambda act : bd .projects .set_current (act .text ()))
152+
153+ def populate (self ):
154+ """
155+ Populates the menu with the projects available in the database
156+ """
157+ import bw2data as bd
158+
159+ # clear the menu of any already existing actions
160+ self .clear ()
161+
162+ # sort projects alphabetically
163+ sorted_projects = sorted (list (bd .projects ))
164+
165+ # iterate over the sorted projects and add them as actions to the menu
166+ for i , proj in enumerate (sorted_projects ):
167+ # check whether the project is BW25
168+ bw_25 = (
169+ False if not isinstance (proj .data , dict ) else proj .data .get ("25" , False )
170+ )
171+
172+ # add BW25 decorations if necessary
173+ name = proj .name if not bw_25 or AB_BW25 else "[BW25] " + proj .name
174+
175+ # create the action and disable it if it's BW25 and BW25 is not supported
176+ action = QtWidgets .QAction (name , self )
177+ action .setEnabled (not bw_25 or AB_BW25 )
178+
179+ self .addAction (action )
180+
181+
137182class MigrationsMenu (QtWidgets .QMenu ):
138183 def __init__ (self , parent = None ) -> None :
139184 super ().__init__ (parent )
@@ -143,5 +188,3 @@ def __init__(self, parent=None) -> None:
143188
144189 self .addAction (self .install_migrations_action )
145190
146-
147-
0 commit comments