Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move to QtPy compatibility #1404

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
Open
4 changes: 4 additions & 0 deletions activity_browser/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
import sys
from logging import getLogger

try:
import PySide6
except ImportError:
import PySide2

from .logger import log_file_location, setup_ab_logging
from .mod import bw2data
Expand Down
1 change: 1 addition & 0 deletions activity_browser/actions/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,4 @@
from .plugin_wizard_open import PluginWizardOpen
from .settings_wizard_open import SettingsWizardOpen
from .migrations_install import MigrationsInstall
from .pyside_upgrade import PysideUpgrade
2 changes: 1 addition & 1 deletion activity_browser/actions/activity/activity_delete.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from typing import List

from PySide2 import QtWidgets
from qtpy import QtWidgets

from activity_browser import application
from activity_browser.actions.base import ABAction, exception_dialogs
Expand Down
2 changes: 1 addition & 1 deletion activity_browser/actions/activity/activity_duplicate.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from typing import Callable, List, Union

from PySide2 import QtCore
from qtpy import QtCore

from activity_browser.actions.base import ABAction, exception_dialogs
from activity_browser.bwutils import commontasks
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from typing import List

from PySide2 import QtWidgets
from qtpy import QtWidgets

from activity_browser import application, project_settings
from activity_browser.actions.base import ABAction, exception_dialogs
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from typing import Callable, Optional, Union

import pandas as pd
from PySide2 import QtCore
from qtpy import QtCore

from activity_browser import application, signals
from activity_browser.actions.base import ABAction, exception_dialogs
Expand Down
2 changes: 1 addition & 1 deletion activity_browser/actions/activity/activity_modify.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from typing import Callable, Union

from PySide2 import QtCore
from qtpy import QtCore

from activity_browser.actions.base import ABAction, exception_dialogs
from activity_browser.mod.bw2data import get_activity
Expand Down
2 changes: 1 addition & 1 deletion activity_browser/actions/activity/activity_new.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from uuid import uuid4

from PySide2 import QtWidgets
from qtpy import QtWidgets

from activity_browser import application
from activity_browser.actions.base import ABAction, exception_dialogs
Expand Down
2 changes: 1 addition & 1 deletion activity_browser/actions/activity/activity_relink.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from typing import List

from PySide2 import QtCore, QtWidgets
from qtpy import QtCore, QtWidgets

from activity_browser import application
from activity_browser.actions.base import ABAction, exception_dialogs
Expand Down
2 changes: 1 addition & 1 deletion activity_browser/actions/base.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from PySide2 import QtCore, QtGui, QtWidgets
from qtpy import QtCore, QtGui, QtWidgets

from activity_browser import application

Expand Down
2 changes: 1 addition & 1 deletion activity_browser/actions/biosphere_update.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from PySide2 import QtWidgets
from qtpy import QtWidgets

from activity_browser import application
from activity_browser.actions.base import ABAction, exception_dialogs
Expand Down
2 changes: 1 addition & 1 deletion activity_browser/actions/calculation_setup/cs_delete.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from logging import getLogger

from PySide2 import QtWidgets
from qtpy import QtWidgets

from activity_browser import application, signals
from activity_browser.actions.base import ABAction, exception_dialogs
Expand Down
2 changes: 1 addition & 1 deletion activity_browser/actions/calculation_setup/cs_duplicate.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from logging import getLogger

from PySide2 import QtWidgets
from qtpy import QtWidgets

from activity_browser import application, signals
from activity_browser.actions.base import ABAction, exception_dialogs
Expand Down
2 changes: 1 addition & 1 deletion activity_browser/actions/calculation_setup/cs_new.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from logging import getLogger

from PySide2 import QtWidgets
from qtpy import QtWidgets

from activity_browser import application, signals
from activity_browser.actions.base import ABAction, exception_dialogs
Expand Down
2 changes: 1 addition & 1 deletion activity_browser/actions/calculation_setup/cs_rename.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from logging import getLogger

from PySide2 import QtWidgets
from qtpy import QtWidgets

from activity_browser import application, signals
from activity_browser.actions.base import ABAction, exception_dialogs
Expand Down
2 changes: 1 addition & 1 deletion activity_browser/actions/database/database_delete.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from bw2data.parameters import Group
from PySide2 import QtCore, QtWidgets
from qtpy import QtCore, QtWidgets

from activity_browser import application, project_settings
from activity_browser.actions.base import ABAction, exception_dialogs
Expand Down
2 changes: 1 addition & 1 deletion activity_browser/actions/database/database_duplicate.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from PySide2 import QtWidgets
from qtpy import QtWidgets

from activity_browser import application
from activity_browser.actions.base import ABAction, exception_dialogs
Expand Down
2 changes: 1 addition & 1 deletion activity_browser/actions/database/database_export.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from PySide2 import QtWidgets
from qtpy import QtWidgets

from activity_browser import application
from activity_browser.actions.base import ABAction, exception_dialogs
Expand Down
2 changes: 1 addition & 1 deletion activity_browser/actions/database/database_new.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from PySide2 import QtWidgets
from qtpy import QtWidgets

from activity_browser import application, project_settings, signals
from activity_browser.actions.base import ABAction, exception_dialogs
Expand Down
2 changes: 1 addition & 1 deletion activity_browser/actions/database/database_relink.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from PySide2 import QtCore, QtWidgets
from qtpy import QtCore, QtWidgets

from activity_browser import application
from activity_browser.actions.base import ABAction, exception_dialogs
Expand Down
2 changes: 1 addition & 1 deletion activity_browser/actions/method/cf_new.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from typing import List

from PySide2 import QtWidgets
from qtpy import QtWidgets

from activity_browser import application
from activity_browser.actions.base import ABAction, exception_dialogs
Expand Down
2 changes: 1 addition & 1 deletion activity_browser/actions/method/cf_remove.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from typing import List

from PySide2 import QtWidgets
from qtpy import QtWidgets

from activity_browser import application
from activity_browser.actions.base import ABAction, exception_dialogs
Expand Down
2 changes: 1 addition & 1 deletion activity_browser/actions/method/method_delete.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from typing import List
from logging import getLogger

from PySide2 import QtWidgets
from qtpy import QtWidgets

from activity_browser import application
from activity_browser.actions.base import ABAction, exception_dialogs
Expand Down
2 changes: 1 addition & 1 deletion activity_browser/actions/method/method_open.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from typing import List

from PySide2 import QtWidgets, QtCore
from qtpy import QtWidgets, QtCore

from activity_browser import signals
from activity_browser.actions.base import ABAction, exception_dialogs
Expand Down
2 changes: 1 addition & 1 deletion activity_browser/actions/migrations_install.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from PySide2 import QtWidgets
from qtpy import QtWidgets

from activity_browser import application
from activity_browser.actions.base import ABAction, exception_dialogs
Expand Down
2 changes: 1 addition & 1 deletion activity_browser/actions/parameter/parameter_new.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from typing import Tuple

from PySide2 import QtCore, QtGui, QtWidgets
from qtpy import QtCore, QtGui, QtWidgets

from activity_browser import actions, application
from activity_browser.actions.base import ABAction, exception_dialogs
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from typing import List, Tuple

from peewee import IntegrityError
from PySide2 import QtWidgets
from qtpy import QtWidgets

from activity_browser import application
from activity_browser.actions.base import ABAction, exception_dialogs
Expand Down
2 changes: 1 addition & 1 deletion activity_browser/actions/parameter/parameter_rename.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from typing import Any

from PySide2 import QtWidgets
from qtpy import QtWidgets

from activity_browser import application
from activity_browser.actions.base import ABAction, exception_dialogs
Expand Down
2 changes: 1 addition & 1 deletion activity_browser/actions/project/project_delete.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from PySide2 import QtWidgets
from qtpy import QtWidgets

from activity_browser import ab_settings, application
from activity_browser.actions.base import ABAction, exception_dialogs
Expand Down
2 changes: 1 addition & 1 deletion activity_browser/actions/project/project_duplicate.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from PySide2 import QtWidgets
from qtpy import QtWidgets

from activity_browser import application
from activity_browser.actions.base import ABAction, exception_dialogs
Expand Down
2 changes: 1 addition & 1 deletion activity_browser/actions/project/project_export.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import tarfile
from logging import getLogger

from PySide2 import QtWidgets, QtCore
from qtpy import QtWidgets, QtCore

from activity_browser import application
from activity_browser.mod import bw2data as bd
Expand Down
2 changes: 1 addition & 1 deletion activity_browser/actions/project/project_import.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import tarfile
from logging import getLogger

from PySide2 import QtWidgets, QtCore
from qtpy import QtWidgets, QtCore
from bw2io import backup

from activity_browser import application
Expand Down
2 changes: 1 addition & 1 deletion activity_browser/actions/project/project_new.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from PySide2 import QtWidgets
from qtpy import QtWidgets

from activity_browser import application
from activity_browser.actions.base import ABAction, exception_dialogs
Expand Down
100 changes: 100 additions & 0 deletions activity_browser/actions/pyside_upgrade.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
import qtpy
import os
import sys
import subprocess
import time

from activity_browser import application
from activity_browser.actions.base import ABAction, exception_dialogs
from activity_browser.ui import threading, icons

from qtpy import QtWidgets
from qtpy.QtCore import Signal, SignalInstance


class PysideUpgrade(ABAction):
"""
ABAction to install PySide6 through PyPI/pip. Installs PySide6, sets the environment variable for QtPy to use
PySide6 and then restarts the Activity Browser through a subprocess.
"""

icon = icons.qicons.forward
text = "Upgrade installation to PySide6"

@classmethod
@exception_dialogs
def run(cls):

# slot definition to update the progress dialog with thread updates
def update_dialog_slot(progress: int, label: str):
dialog.setValue(progress)
dialog.setLabelText(label)

assert not qtpy.PYSIDE6, "Already running PySide6"
assert cls.in_conda(), "Not inside a Conda environment"

# setup a progress dialog to show the user we're doing something
dialog = QtWidgets.QProgressDialog(application.main_window)
dialog.setWindowTitle("Upgrading GUI back-end")
dialog.setMaximum(0)
dialog.setCancelButton(None)

# messages can get quite long, so enable word-wrapping
lbl = dialog.findChild(QtWidgets.QLabel)
lbl.setWordWrap(True)

# initialize thread and connect signals
thread = PySideUpgradeThread(application)
thread.status.connect(update_dialog_slot)
thread.exit.connect(sys.exit)

thread.start()
dialog.exec_()

@staticmethod
def in_conda() -> bool:
"""Returns true when the current shell is in a Conda environment."""
return bool(os.environ.get("CONDA_DEFAULT_ENV", False))


class PySideUpgradeThread(threading.ABThread):
exit: SignalInstance = Signal()

def run_safely(self):
self.pip_installation()
self.restart()

def pip_installation(self):
"""
Install PySide6 from PyPI using a subprocess.Popen call
"""
self.status.emit(0, "Installing PySide6 through pip")

# open subprocess that installs PySide6
process = subprocess.Popen(["pip", "install", "pyside6"], stdout=subprocess.PIPE)

while process.poll() is None: # block until the subprocess is finished
# format stdout
line = process.stdout.readline().decode().strip()
if not line:
continue

# redirect stdout to both console and progress dialog
print(line)
self.status.emit(0, line)

assert process.returncode == 0, "Failed to install PySide6"

def restart(self):
"""
Restarts the Activity Browser through a subprocess. Sleeps 5 seconds to allow the user to register
the restart.
"""
self.status.emit(0, "Restarting the Activity Browser")
subprocess.Popen(["python", "-c", "import activity_browser; activity_browser.run_activity_browser()"])
time.sleep(5)

# signal restart through the exit signal as sys.exit needs to be called in the main thread.
self.exit.emit()


8 changes: 6 additions & 2 deletions activity_browser/application.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@
import os
from logging import getLogger

from PySide2.QtCore import QCoreApplication, QObject, QSysInfo, Qt
from PySide2.QtWidgets import QApplication
import qtpy
from qtpy.QtCore import QCoreApplication, QObject, QSysInfo, Qt
from qtpy.QtWidgets import QApplication, QStyleFactory

log = getLogger(__name__)

Expand Down Expand Up @@ -55,3 +56,6 @@ def deleteLater(self):
QCoreApplication.setAttribute(Qt.AA_ShareOpenGLContexts, True)

application = ABApplication()

if qtpy.PYSIDE6:
application.setStyle(QStyleFactory().create("fusion"))
2 changes: 1 addition & 1 deletion activity_browser/bwutils/calculations.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from logging import getLogger

from bw2calc.errors import BW2CalcError
from PySide2.QtWidgets import QApplication
from qtpy.QtWidgets import QApplication

from ..bwutils import (MLCA, Contributions, MonteCarloLCA,
SuperstructureContributions, SuperstructureMLCA)
Expand Down
2 changes: 1 addition & 1 deletion activity_browser/bwutils/multilca.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import bw2calc as bc
import numpy as np
import pandas as pd
from PySide2.QtWidgets import QApplication, QMessageBox
from qtpy.QtWidgets import QApplication, QMessageBox

from activity_browser.mod import bw2data as bd

Expand Down
4 changes: 2 additions & 2 deletions activity_browser/bwutils/superstructure/dataframe.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@

import numpy as np
import pandas as pd
from PySide2.QtCore import Qt
from PySide2.QtWidgets import QApplication, QPushButton
from qtpy.QtCore import Qt
from qtpy.QtWidgets import QApplication, QPushButton

from ..errors import ScenarioDatabaseNotFoundError
from ..metadata import AB_metadata
Expand Down
Loading
Loading