diff --git a/activity_browser/layouts/main.py b/activity_browser/layouts/main.py index 35f11323f..3fb6e80c8 100644 --- a/activity_browser/layouts/main.py +++ b/activity_browser/layouts/main.py @@ -1,10 +1,7 @@ # -*- coding: utf-8 -*- -import importlib.util -import shutil -import sys -import traceback +from PySide2 import QtCore, QtWidgets -from PySide2 import QtCore, QtGui, QtWidgets +import activity_browser.mod.bw2data as bd from ..signals import signals from ..ui.icons import qicons @@ -77,6 +74,11 @@ def connect_signals(self): # Keyboard shortcuts signals.restore_cursor.connect(self.restore_user_control) + bd.projects.current_changed.connect(self.set_titlebar) + + def set_titlebar(self): + self.setWindowTitle(f"Activity Browser - {bd.projects.current}") + def add_tab_to_panel(self, obj, label, side): panel = self.left_panel if side == "left" else self.right_panel panel.add_tab(obj, label) diff --git a/activity_browser/ui/menu_bar.py b/activity_browser/ui/menu_bar.py index 18977b1a8..0f07970b2 100644 --- a/activity_browser/ui/menu_bar.py +++ b/activity_browser/ui/menu_bar.py @@ -1,3 +1,4 @@ +import os from PySide2 import QtGui, QtWidgets from PySide2.QtCore import QSize, QUrl, Slot @@ -7,6 +8,8 @@ from ..info import __version__ as ab_version from .icons import qicons +AB_BW25 = True if os.environ.get("AB_BW25", False) else False + class MenuBar(QtWidgets.QMenuBar): def __init__(self, window): @@ -46,6 +49,7 @@ def connect_signals(self): def setup_file_menu(self) -> None: """Build the menu for specific importing/export/updating actions.""" + self.file_menu.addMenu(ProjectsMenu(self)) self.file_menu.addAction(self.new_proj_action) self.file_menu.addAction(self.dup_proj_action) self.file_menu.addAction(self.delete_proj_action) @@ -92,7 +96,7 @@ def setup_help_menu(self) -> None: "&About Qt", lambda: QtWidgets.QMessageBox.aboutQt(self.window) ) self.help_menu.addAction( - qicons.question, "&Get help on the wiki", self.open_wiki() + qicons.question, "&Get help on the wiki", self.open_wiki ) self.help_menu.addAction( qicons.issue, "&Report an idea/issue on GitHub", self.raise_issue_github @@ -134,6 +138,47 @@ def biosphere_exists(self) -> None: self.import_db_action.setEnabled(exists) +class ProjectsMenu(QtWidgets.QMenu): + """ + Menu that lists all the projects available through bw2data.projects + """ + def __init__(self, parent=None): + super().__init__(parent) + self.setTitle("Open project") + self.populate() + + self.aboutToShow.connect(self.populate) + self.triggered.connect(lambda act: bd.projects.set_current(act.text())) + + def populate(self): + """ + Populates the menu with the projects available in the database + """ + import bw2data as bd + + # clear the menu of any already existing actions + self.clear() + + # sort projects alphabetically + sorted_projects = sorted(list(bd.projects)) + + # iterate over the sorted projects and add them as actions to the menu + for i, proj in enumerate(sorted_projects): + # check whether the project is BW25 + bw_25 = ( + False if not isinstance(proj.data, dict) else proj.data.get("25", False) + ) + + # add BW25 decorations if necessary + name = proj.name if not bw_25 or AB_BW25 else "[BW25] " + proj.name + + # create the action and disable it if it's BW25 and BW25 is not supported + action = QtWidgets.QAction(name, self) + action.setEnabled(not bw_25 or AB_BW25) + + self.addAction(action) + + class MigrationsMenu(QtWidgets.QMenu): def __init__(self, parent=None) -> None: super().__init__(parent) @@ -143,5 +188,3 @@ def __init__(self, parent=None) -> None: self.addAction(self.install_migrations_action) - -