Skip to content

Commit

Permalink
Project selection through project menu (#1362)
Browse files Browse the repository at this point in the history
* Added open project menu to project menu

* BW25 decorations

* Added project name to AB title bar

* Documentation

* Quickfix for opening of Wiki
  • Loading branch information
mrvisscher authored Sep 18, 2024
1 parent 1cda4fe commit 0c75d6c
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 8 deletions.
12 changes: 7 additions & 5 deletions activity_browser/layouts/main.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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)
Expand Down
49 changes: 46 additions & 3 deletions activity_browser/ui/menu_bar.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import os
from PySide2 import QtGui, QtWidgets
from PySide2.QtCore import QSize, QUrl, Slot

Expand All @@ -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):
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -143,5 +188,3 @@ def __init__(self, parent=None) -> None:

self.addAction(self.install_migrations_action)



0 comments on commit 0c75d6c

Please sign in to comment.