Skip to content

Commit

Permalink
Refactor top classes (#487)
Browse files Browse the repository at this point in the history
* Add Qt parent pass-through in init

* Have StatusBar class inherit from QStatusBar

* Add slot decorators to methods

* Have MenuBar class inherit from QMenuBar

* Store QMenu objects in init, use methods to build actions

* Pass QMainWindow instance into Updater and Wizard classes

* Added TODO: Move updater and wizard calls into a controller call through signals.

* Remove json import

* Set MenuBar and StatusBar within MainWindow class
  • Loading branch information
dgdekoning authored Jan 19, 2021
1 parent dd67b1e commit e00ad4c
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 72 deletions.
5 changes: 3 additions & 2 deletions activity_browser/app/ui/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
from .panels import LeftPanel, RightPanel
from .statusbar import Statusbar
from .utils import StdRedirector
from ..signals import signals


class MainWindow(QtWidgets.QMainWindow):
Expand Down Expand Up @@ -83,7 +82,9 @@ def __init__(self):

# Layout: extra items outside main layout
self.menu_bar = MenuBar(self)
self.statusbar = Statusbar(self)
self.setMenuBar(self.menu_bar)
self.status_bar = Statusbar(self)
self.setStatusBar(self.status_bar)

self.connect_signals()

Expand Down
100 changes: 47 additions & 53 deletions activity_browser/app/ui/menu_bar.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
# -*- coding: utf-8 -*-
import json

import brightway2 as bw
from PySide2 import QtCore, QtWidgets, QtGui

Expand All @@ -11,82 +9,81 @@
from .wizards.settings_wizard import SettingsWizard
from .wizards.db_export_wizard import DatabaseExportWizard

# TODO: Revise this entire class by subclassing QMenuBar


class MenuBar(object):
class MenuBar(QtWidgets.QMenuBar):
def __init__(self, window):
super().__init__(parent=window)
self.window = window
self.settings_wizard = None
self.export_wizard = None
self.biosphere_updater = None
self.file_menu = QtWidgets.QMenu('&File', self.window)
self.view_menu = QtWidgets.QMenu('&View', self.window)
self.windows_menu = QtWidgets.QMenu('&Windows', self.window)
self.help_menu = QtWidgets.QMenu('&Help', self.window)

self.update_biosphere_action = QtWidgets.QAction(
window.style().standardIcon(QtWidgets.QStyle.SP_BrowserReload),
"&Update biosphere...", None
)
self.biosphere_updater = None
self.export_db_action = QtWidgets.QAction(
self.window.style().standardIcon(QtWidgets.QStyle.SP_DriveHDIcon),
"&Export database...", None
)
self.import_db_action = QtWidgets.QAction(
qicons.import_db, '&Import database...', None
)

self.menubar = QtWidgets.QMenuBar()
self.menubar.addMenu(self.setup_file_menu())
# self.menubar.addMenu(self.setup_tools_menu())
# self.menubar.addMenu(self.setup_extensions_menu())
self.menubar.addMenu(self.setup_view_menu())
self.menubar.addMenu(self.setup_windows_menu())
self.menubar.addMenu(self.setup_help_menu())
window.setMenuBar(self.menubar)
self.addMenu(self.file_menu)
self.addMenu(self.view_menu)
self.addMenu(self.windows_menu)
self.addMenu(self.help_menu)

self.setup_file_menu()
self.setup_view_menu()
self.update_windows_menu()
self.setup_help_menu()
self.connect_signals()

def connect_signals(self):
signals.update_windows.connect(self.update_windows_menu)
signals.project_selected.connect(self.biosphere_exists)
signals.databases_changed.connect(self.biosphere_exists)
self.update_biosphere_action.triggered.connect(self.update_biosphere)
self.export_db_action.triggered.connect(self.transfer_database_wizard)
self.import_db_action.triggered.connect(lambda: signals.import_database.emit(self.window))

# FILE
def setup_file_menu(self):
menu = QtWidgets.QMenu('&File', self.window)
menu.addAction(self.import_db_action)
menu.addAction(
self.window.style().standardIcon(QtWidgets.QStyle.SP_DriveHDIcon),
"&Export database...",
self.transfer_database_wizard
)
menu.addAction(self.update_biosphere_action)
menu.addAction(
def setup_file_menu(self) -> None:
"""Build the menu for specific importing/export/updating actions."""
self.file_menu.addAction(self.import_db_action)
self.file_menu.addAction(self.export_db_action)
self.file_menu.addAction(self.update_biosphere_action)
self.file_menu.addAction(
qicons.settings,
'&Settings...',
self.open_settings_wizard
)
return menu

# VIEW
def setup_view_menu(self):
view_menu = QtWidgets.QMenu('&View', self.window)
view_menu.addAction(
def setup_view_menu(self) -> None:
"""Build the menu for viewing or hiding specific tabs"""
self.view_menu.addAction(
qicons.graph_explorer,
'&Graph Explorer',
lambda x="Graph Explorer": signals.toggle_show_or_hide_tab.emit(x)
)
view_menu.addAction(
self.view_menu.addAction(
qicons.history,
'&Activity History',
lambda x="History": signals.toggle_show_or_hide_tab.emit(x)
)
view_menu.addAction(
self.view_menu.addAction(
qicons.welcome,
'&Welcome screen',
lambda x="Welcome": signals.toggle_show_or_hide_tab.emit(x)
)
return view_menu

# WINDOWS
def setup_windows_menu(self):
self.windows_menu = QtWidgets.QMenu('&Windows', self.window)
self.update_windows_menu()
return self.windows_menu

def update_windows_menu(self):
"""Clear and rebuild the menu for switching between tabs."""
self.windows_menu.clear()
for index in range(self.window.stacked.count()): # iterate over widgets in QStackedWidget
widget = self.window.stacked.widget(index)
Expand All @@ -96,25 +93,22 @@ def update_windows_menu(self):
lambda widget=widget: self.window.stacked.setCurrentWidget(widget),
)

# HELP
def setup_help_menu(self):
help_menu = QtWidgets.QMenu('&Help', self.window)
help_menu.addAction(
def setup_help_menu(self) -> None:
"""Build the help menu for the menubar."""
self.help_menu.addAction(
self.window.icon,
'&About Activity Browser',
self.about)
help_menu.addAction(
self.help_menu.addAction(
'&About Qt',
lambda: QtWidgets.QMessageBox.aboutQt(self.window)
)
help_menu.addAction(
self.help_menu.addAction(
qicons.issue,
'&Report an idea/issue on GitHub',
self.raise_issue_github
)

return help_menu

def about(self):
text = """
Activity Browser - a graphical interface for Brightway2.<br><br>
Expand Down Expand Up @@ -144,13 +138,13 @@ def about(self):

def raise_issue_github(self):
url = QtCore.QUrl('https://github.com/LCA-ActivityBrowser/activity-browser/issues/new')
QtGui.QDesktopServices.openUrl(url)
QtGui.QDesktopServices.openUrl(url=url)

def open_settings_wizard(self):
self.settings_wizard = SettingsWizard()
self.settings_wizard = SettingsWizard(self.window)

def transfer_database_wizard(self) -> None:
wizard = DatabaseExportWizard()
self.export_wizard = DatabaseExportWizard(self.window)

def biosphere_exists(self) -> None:
""" Test if the default biosphere exists as a database in the project
Expand All @@ -166,8 +160,8 @@ def update_biosphere(self):
ok = QtWidgets.QMessageBox.question(
self.window, "Update biosphere3?",
"Updating the biosphere3 database cannot be undone!",
buttons=QtWidgets.QMessageBox.Ok | QtWidgets.QMessageBox.Abort,
defaultButton=QtWidgets.QMessageBox.Abort
QtWidgets.QMessageBox.Ok | QtWidgets.QMessageBox.Abort,
QtWidgets.QMessageBox.Abort
)
if ok == QtWidgets.QMessageBox.Ok:
self.biosphere_updater = BiosphereUpdater()
self.biosphere_updater = BiosphereUpdater(self.window)
33 changes: 18 additions & 15 deletions activity_browser/app/ui/statusbar.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,22 @@
# -*- coding: utf-8 -*-
from PySide2 import QtWidgets
from PySide2.QtCore import Slot
from PySide2.QtWidgets import QLabel, QStatusBar

from brightway2 import projects
import brightway2 as bw

from ..signals import signals


class Statusbar(object):
class Statusbar(QStatusBar):
def __init__(self, window):
self.window = window
self.statusbar = QtWidgets.QStatusBar()
self.window.setStatusBar(self.statusbar)
super().__init__(parent=window)
self.status_message_left = QLabel('Welcome')
self.status_message_right = QLabel('Database')
self.status_message_center = QLabel('Project')

self.status_message_left = QtWidgets.QLabel('Welcome')
self.status_message_right = QtWidgets.QLabel('Database')
self.status_message_center = QtWidgets.QLabel('Project')

self.statusbar.addWidget(self.status_message_left, 1)
self.statusbar.addWidget(self.status_message_center, 2)
self.statusbar.addWidget(self.status_message_right, 0)
self.addWidget(self.status_message_left, 1)
self.addWidget(self.status_message_center, 2)
self.addWidget(self.status_message_right, 0)

self.connect_signals()

Expand All @@ -27,21 +25,26 @@ def connect_signals(self):
signals.project_selected.connect(self.update_project)
signals.database_selected.connect(self.set_database)

def left(self, message):
@Slot(str, name="statusLeft")
def left(self, message: str) -> None:
print(message) # for console output
if isinstance(message, str):
self.status_message_left.setText(message)

@Slot(str, name="statusCenter")
def center(self, message):
self.status_message_center.setText(message)

@Slot(str, name="statusRight")
def right(self, message):
self.status_message_right.setText(message)

@Slot(name="updateProjectStatus")
def update_project(self):
name = projects.current
name = bw.projects.current
self.center("Project: {}".format(name))
self.right("Database: None")

@Slot(str, name="setDatabaseName")
def set_database(self, name):
self.right("Database: {}".format(name))
4 changes: 2 additions & 2 deletions activity_browser/app/ui/widgets/biosphere_update.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@


class BiosphereUpdater(QtWidgets.QProgressDialog):
def __init__(self):
super().__init__()
def __init__(self, parent=None):
super().__init__(parent=parent)
self.setWindowTitle("Updating '{}' database".format(bw.config.biosphere))
self.setLabelText("Adding new flows to biosphere database")
self.setRange(0, 0)
Expand Down

0 comments on commit e00ad4c

Please sign in to comment.