Skip to content

Commit

Permalink
Merge pull request #1058 from vasole/rc
Browse files Browse the repository at this point in the history
[GUI] Do not use rcParams, follow QT_API environment variable
  • Loading branch information
vasole authored Dec 13, 2023
2 parents c8e3b6f + a4fe565 commit e10d252
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 62 deletions.
41 changes: 10 additions & 31 deletions PyMca5/PyMcaGraph/backends/MatplotlibBackend.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,55 +67,33 @@ def nanmin(x):
TK = False
if ("tk" in sys.argv) or ("Tkinter" in sys.modules) or ("tkinter" in sys.modules):
TK = True
if TK and ("PyQt4.QtCore" not in sys.modules) and ("PyQt5.QtCore" not in sys.modules) and\
("PySide.QtCore" not in sys.modules):
if sys.version < '3.0':
import Tkinter as Tk
else:
import tkinter as Tk
elif ('PySide.QtCore' in sys.modules) or ('PySide' in sys.argv):
matplotlib.rcParams['backend'] = 'Qt4Agg'
if 'backend.qt4' in matplotlib.rcParams:
matplotlib.rcParams['backend.qt4'] = 'PySide'
from PySide import QtCore, QtGui
elif ("PyQt4.QtCore" in sys.modules) or ('PyQt4' in sys.argv):
from PyQt4 import QtCore, QtGui
matplotlib.rcParams['backend'] = 'Qt4Agg'
if TK and ("PyQt5.QtCore" not in sys.modules) and ("PyQt6.QtCore" not in sys.modules) and \
("PySide6.QtCore" not in sys.modules) and ("PySide2.QtCore" not in sys.modules):
import tkinter as Tk
elif 'PySide2.QtCore' in sys.modules:
matplotlib.rcParams['backend'] = 'Qt5Agg'
from PySide2 import QtCore, QtGui, QtWidgets
QtGui.QApplication = QtWidgets.QApplication
elif 'PyQt5.QtCore' in sys.modules:
matplotlib.rcParams['backend'] = 'Qt5Agg'
from PyQt5 import QtCore, QtGui, QtWidgets
QtGui.QApplication = QtWidgets.QApplication
elif 'PySide6.QtCore' in sys.modules:
matplotlib.rcParams['backend'] = 'Qt5Agg'
from PySide6 import QtCore, QtGui, QtWidgets
QtGui.QApplication = QtWidgets.QApplication
elif 'PyQt6.QtCore' in sys.modules:
matplotlib.rcParams['backend'] = 'Qt5Agg'
from PyQt6 import QtCore, QtGui, QtWidgets
QtGui.QApplication = QtWidgets.QApplication
else:
try:
from PyQt5 import QtCore, QtGui, QtWidgets
QtGui.QApplication = QtWidgets.QApplication
matplotlib.rcParams['backend'] = 'Qt5Agg'
except ImportError:
try:
from PyQt6 import QtCore, QtGui, QtWidgets
QtGui.QApplication = QtWidgets.QApplication
matplotlib.rcParams['backend'] = 'Qt5Agg'
except ImportError:
from PySide6 import QtCore, QtGui, QtWidgets
QtGui.QApplication = QtWidgets.QApplication
matplotlib.rcParams['backend'] = 'Qt5Agg'
if ("PyQt4.QtCore" in sys.modules) or ("PySide.QtCore" in sys.modules):
from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg as FigureCanvas
TK = False
QT = True
elif ("PyQt5.QtCore" in sys.modules) or ("PySide2.QtCore" in sys.modules):
if ("PyQt5.QtCore" in sys.modules) or ("PySide2.QtCore" in sys.modules):
from ._patch_matplotlib import patch_backend_qt
patch_backend_qt()
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
Expand All @@ -131,6 +109,11 @@ def nanmin(x):
TK = True
QT = False
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg as FigureCanvas
else:
QT = False
TK = False
print("Unknown backend. Defaulting to Agg")
from matplotlib.backends.backend_agg import FigureCanvasAgg as FigureCanvas
try:
from .. import PlotBackend
except ImportError:
Expand Down Expand Up @@ -245,11 +228,7 @@ def twinx(self):
# this respects aspect size
# self.ax = self.fig.add_subplot(111, aspect='equal')
# This should be independent of Qt
if matplotlib.rcParams['backend'] == "Qt4Agg":
FigureCanvas.setSizePolicy(self,
QtGui.QSizePolicy.Expanding,
QtGui.QSizePolicy.Expanding)
elif matplotlib.rcParams['backend'] == "Qt5Agg":
if QT:
FigureCanvas.setSizePolicy(self,
QtWidgets.QSizePolicy.Expanding,
QtWidgets.QSizePolicy.Expanding)
Expand Down
43 changes: 25 additions & 18 deletions PyMca5/PyMcaGui/PyMcaQt.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@
elif hasattr(sys, 'argv') and ('--binding=PySide6' in sys.argv):
# argv might not be defined for embedded python (e.g., in Qt designer)
BINDING = 'PySide6'
else:
BINDING = os.environ.get("QT_API", None)

if BINDING is None: # Try the different bindings
try:
Expand All @@ -87,23 +89,24 @@
if "PySide6" in sys.modules:
del sys.modules["PySide6"]
try:
import PySide2.QtCore
BINDING = "PySide2"
import PyQt6.QtCore
BINDING = "PyQt6"
except ImportError:
if 'PySide2' in sys.modules:
del sys.modules["PySide2"]
if 'PyQt6' in sys.modules:
del sys.modules["PyQt6"]
try:
import PyQt6.QtCore # noqa
BINDING = "PyQt6"
import PySide2.QtCore # noqa
BINDING = "PySide2"
except ImportError:
if 'PyQt6' in sys.modules:
del sys.modules["PyQt6"]
if 'PySide2' in sys.modules:
del sys.modules["PySide2"]
raise ImportError(
'No Qt wrapper found. Install PyQt5, PySide2, PySide6 or PyQt6.')
'No Qt wrapper found. Install PyQt5, PySide6 or PyQt6.')

_logger.info("BINDING set to %s" % BINDING)
_logger.info("BINDING set to %s" % BINDING)

if BINDING == "PyQt5":
if BINDING.lower() == "pyqt5":
BINDING = "PyQt5"
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
Expand All @@ -124,8 +127,8 @@
Signal = pyqtSignal
Slot = pyqtSlot

elif BINDING == "PySide2":
# try PySide2 (experimental)
elif BINDING.lower() == "pyside2":
BINDING = "PySide2"
from PySide2.QtCore import *
from PySide2.QtGui import *
from PySide2.QtWidgets import *
Expand Down Expand Up @@ -191,8 +194,9 @@ class QThread(_ExecMixIn, QThread): pass
_platform_plugin_path
_logger.info("QT_QPA_PLATFORM_PLUGIN_PATH set to <%s>" % \
_platform_plugin_path)
elif BINDING == 'PySide6':
elif BINDING.lower() == 'pyside6':
_logger.debug('Using PySide6 bindings')
BINDING = "PySide6"
import PySide6

from PySide6.QtCore import * # noqa
Expand Down Expand Up @@ -232,8 +236,9 @@ def width(self):
screen = QApplication.instance().primaryScreen()
return screen.availableGeometry().width()

elif BINDING == 'PyQt6':
elif BINDING.lower() == 'pyqt6':
_logger.debug('Using PyQt6 bindings')
BINDING = "PyQt6"
import enum
from PyQt6 import QtCore
if QtCore.PYQT_VERSION < int("0x60300", 16):
Expand Down Expand Up @@ -417,7 +422,9 @@ class _Foo(object): pass
class QObject(QObject, _Foo): pass

else:
raise ImportError('No Qt wrapper found. Install one of PyQt5, PySide2, PySide6, PyQt6')
raise ImportError('No Qt wrapper found. Install one of PyQt5, PySide6, PyQt6')

_logger.info("PyMcaQt.BINDING set to %s" % BINDING)

# provide a exception handler but not implement it by default
def exceptionHandler(type_, value, trace):
Expand Down Expand Up @@ -470,7 +477,7 @@ def __init__(self, *var, **kw):
if (size.width() > 15) and (size.height() > 15):
self.setIconSize(size)
except Exception:
print("unable")
print("unable to setIconSize")
pass

if sys.version_info < (3,):
Expand Down Expand Up @@ -511,7 +518,7 @@ def safe_str(potentialQString):
else:
safe_str = str

if BINDING=="PySide2":
if BINDING.lower()=="pyside2":
_logger = logging.warning("PyMca PySide2 support deprecated and not reliable")

class CLocaleQDoubleValidator(QDoubleValidator):
Expand Down
12 changes: 1 addition & 11 deletions PyMca5/PyMcaGui/misc/QIPythonWidget.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,24 +32,14 @@
__contact__ = "sole@esrf.fr"
__license__ = "MIT"
__copyright__ = "European Synchrotron Radiation Facility, Grenoble, France"
# Set the QT API

import os
import sys

from PyMca5.PyMcaGui.PyMcaQt import QApplication, QWidget, \
QPushButton, QVBoxLayout, QMessageBox, \
BINDING

# this seems to be obsolete stuff
if BINDING == "PySide":
os.environ['QT_API'] = 'pyside'
elif BINDING == "PySide2":
os.environ['QT_API'] = 'pyside2'
elif BINDING == "PyQt4":
os.environ['QT_API'] = 'pyqt'
else:
os.environ['QT_API'] = 'pyqt5'

QTCONSOLE = True
if sys.version_info < (3,):
import IPython
Expand Down
2 changes: 0 additions & 2 deletions PyMca5/PyMcaGui/physics/xrf/QtMcaAdvancedFitReport.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,7 @@
QTVERSION = qt.qVersion()

#this is installation dependent I guess
from matplotlib import rcParams
from matplotlib import __version__ as matplotlib_version
#rcParams['numerix'] = "numeric"
from matplotlib.font_manager import FontProperties
from matplotlib.backends.backend_agg import FigureCanvasAgg as FigureCanvas
from matplotlib.figure import Figure
Expand Down

0 comments on commit e10d252

Please sign in to comment.