Эх сурвалжийг харах

Support PySide2 (PySide is deprecated so not supported)

Kentaro Wada 7 жил өмнө
parent
commit
4518bdb13e

+ 7 - 7
labelme/app.py

@@ -1,17 +1,17 @@
 import argparse
 import argparse
-import collections
 import functools
 import functools
 import os.path
 import os.path
-import re
 import subprocess
 import subprocess
 import sys
 import sys
 
 
-from qtpy import PYQT5
+from qtpy import QT_VERSION
 from qtpy import QtCore
 from qtpy import QtCore
 from qtpy.QtCore import Qt
 from qtpy.QtCore import Qt
 from qtpy import QtGui
 from qtpy import QtGui
 from qtpy import QtWidgets
 from qtpy import QtWidgets
 
 
+QT5 = QT_VERSION[0] == '5'
+
 from labelme.canvas import Canvas
 from labelme.canvas import Canvas
 from labelme.colorDialog import ColorDialog
 from labelme.colorDialog import ColorDialog
 from labelme.labelDialog import LabelDialog
 from labelme.labelDialog import LabelDialog
@@ -85,12 +85,12 @@ class LabelQListWidget(QtWidgets.QListWidget):
 
 
     def get_shape_from_item(self, item):
     def get_shape_from_item(self, item):
         for index, (item_, shape) in enumerate(self.itemsToShapes):
         for index, (item_, shape) in enumerate(self.itemsToShapes):
-            if item_ == item:
+            if item_ is item:
                 return shape
                 return shape
 
 
     def get_item_from_shape(self, shape):
     def get_item_from_shape(self, shape):
         for index, (item, shape_) in enumerate(self.itemsToShapes):
         for index, (item, shape_) in enumerate(self.itemsToShapes):
-            if shape_ == shape:
+            if shape_ is shape:
                 return item
                 return item
 
 
     def clear(self):
     def clear(self):
@@ -972,7 +972,7 @@ class MainWindow(QtWidgets.QMainWindow, WindowMixin):
         filename = QtWidgets.QFileDialog.getOpenFileName(
         filename = QtWidgets.QFileDialog.getOpenFileName(
             self, '%s - Choose Image or Label file' % __appname__,
             self, '%s - Choose Image or Label file' % __appname__,
             path, filters)
             path, filters)
-        if qtpy.PYQT5:
+        if QT5:
             filename, _ = filename
             filename, _ = filename
         filename = str(filename)
         filename = str(filename)
         if filename:
         if filename:
@@ -1008,7 +1008,7 @@ class MainWindow(QtWidgets.QMainWindow, WindowMixin):
         filename = dlg.getSaveFileName(
         filename = dlg.getSaveFileName(
             self, 'Choose File', default_labelfile_name,
             self, 'Choose File', default_labelfile_name,
             'Label files (*%s)' % LabelFile.suffix)
             'Label files (*%s)' % LabelFile.suffix)
-        if qtpy.PYQT5:
+        if QT5:
             filename, _ = filename
             filename, _ = filename
         filename = str(filename)
         filename = str(filename)
         return filename
         return filename

+ 10 - 5
labelme/canvas.py

@@ -2,11 +2,13 @@ from __future__ import print_function
 
 
 import sys
 import sys
 
 
-from qtpy import PYQT5
+from qtpy import QT_VERSION
 from qtpy import QtCore
 from qtpy import QtCore
 from qtpy import QtGui
 from qtpy import QtGui
 from qtpy import QtWidgets
 from qtpy import QtWidgets
 
 
+QT5 = QT_VERSION[0] == '5'
+
 from labelme.lib import distance
 from labelme.lib import distance
 from labelme.shape import Shape
 from labelme.shape import Shape
 
 
@@ -95,7 +97,7 @@ class Canvas(QtWidgets.QWidget):
 
 
     def mouseMoveEvent(self, ev):
     def mouseMoveEvent(self, ev):
         """Update line with last point and current coordinates."""
         """Update line with last point and current coordinates."""
-        if PYQT5:
+        if QT5:
             pos = self.transformPos(ev.pos())
             pos = self.transformPos(ev.pos())
         else:
         else:
             pos = self.transformPos(ev.posF())
             pos = self.transformPos(ev.posF())
@@ -186,7 +188,7 @@ class Canvas(QtWidgets.QWidget):
             self.hVertex, self.hShape = None, None
             self.hVertex, self.hShape = None, None
 
 
     def mousePressEvent(self, ev):
     def mousePressEvent(self, ev):
-        if PYQT5:
+        if QT5:
             pos = self.transformPos(ev.pos())
             pos = self.transformPos(ev.pos())
         else:
         else:
             pos = self.transformPos(ev.posF())
             pos = self.transformPos(ev.posF())
@@ -402,7 +404,10 @@ class Canvas(QtWidgets.QWidget):
         aw, ah = area.width(), area.height()
         aw, ah = area.width(), area.height()
         x = (aw - w) / (2 * s) if aw > w else 0
         x = (aw - w) / (2 * s) if aw > w else 0
         y = (ah - h) / (2 * s) if ah > h else 0
         y = (ah - h) / (2 * s) if ah > h else 0
-        return QtCore.QPointF(x, y)
+        if QT5:
+            return QtCore.QPoint(x, y)
+        else:
+            return QtCore.QPointF(x, y)
 
 
     def outOfPixmap(self, p):
     def outOfPixmap(self, p):
         w, h = self.pixmap.width(), self.pixmap.height()
         w, h = self.pixmap.width(), self.pixmap.height()
@@ -485,7 +490,7 @@ class Canvas(QtWidgets.QWidget):
         return super(Canvas, self).minimumSizeHint()
         return super(Canvas, self).minimumSizeHint()
 
 
     def wheelEvent(self, ev):
     def wheelEvent(self, ev):
-        if PYQT5:
+        if QT5:
             mods = ev.modifiers()
             mods = ev.modifiers()
             delta = ev.angleDelta()
             delta = ev.angleDelta()
             if QtCore.Qt.ControlModifier == int(mods):
             if QtCore.Qt.ControlModifier == int(mods):

+ 5 - 3
labelme/labelDialog.py

@@ -1,8 +1,10 @@
-from qtpy import PYQT5
+from qtpy import QT_VERSION
 from qtpy import QtCore
 from qtpy import QtCore
 from qtpy import QtGui
 from qtpy import QtGui
 from qtpy import QtWidgets
 from qtpy import QtWidgets
 
 
+QT5 = QT_VERSION[0] == '5'
+
 from .lib import labelValidator
 from .lib import labelValidator
 from .lib import newIcon
 from .lib import newIcon
 
 
@@ -76,7 +78,7 @@ class LabelDialog(QtWidgets.QDialog):
         self.edit.setText(item.text())
         self.edit.setText(item.text())
 
 
     def validate(self):
     def validate(self):
-        if PYQT5:
+        if QT5:
             if self.edit.text().strip():
             if self.edit.text().strip():
                 self.accept()
                 self.accept()
         else:
         else:
@@ -84,7 +86,7 @@ class LabelDialog(QtWidgets.QDialog):
                 self.accept()
                 self.accept()
 
 
     def postProcess(self):
     def postProcess(self):
-        if PYQT5:
+        if QT5:
             self.edit.setText(self.edit.text().strip())
             self.edit.setText(self.edit.text().strip())
         else:
         else:
             self.edit.setText(self.edit.text().trimmed())
             self.edit.setText(self.edit.text().trimmed())

+ 2 - 2
labelme/toolBar.py

@@ -32,5 +32,5 @@ class ToolButton(QtWidgets.QToolButton):
         ms = super(ToolButton, self).minimumSizeHint()
         ms = super(ToolButton, self).minimumSizeHint()
         w1, h1 = ms.width(), ms.height()
         w1, h1 = ms.width(), ms.height()
         w2, h2 = self.minSize
         w2, h2 = self.minSize
-        ToolButton.minSize = max(w1, w2), max(h1, h2)
-        return QtCore.QSize(*ToolButton.minSize)
+        self.minSize = max(w1, w2), max(h1, h2)
+        return QtCore.QSize(*self.minSize)

+ 19 - 4
setup.py

@@ -21,24 +21,39 @@ install_requires = [
     'qtpy',
     'qtpy',
 ]
 ]
 
 
+# Find python binding for qt with priority:
+# PyQt5 -> PySide2 -> PyQt4,
+# and PyQt5 is automatically installed on Python3.
+QT_BINDING = None
 
 
 try:
 try:
     import PyQt5  # NOQA
     import PyQt5  # NOQA
-    PYQT_VERSION = 5
+    QT_BINDING = 'pyqt5'
 except ImportError:
 except ImportError:
+    pass
+
+if QT_BINDING is None:
+    try:
+        import PySide2  # NOQA
+        QT_BINDING = 'pyside2'
+    except ImportError:
+        pass
+
+if QT_BINDING is None:
     try:
     try:
         import PyQt4  # NOQA
         import PyQt4  # NOQA
-        PYQT_VERSION = 4
+        QT_BINDING = 'pyqt4'
     except ImportError:
     except ImportError:
         if PY2:
         if PY2:
             sys.stderr.write(
             sys.stderr.write(
-                'Please install PyQt4 or PyQt5 for Python2.\n'
+                'Please install PyQt5, PySide2 or PyQt4 for Python2.\n'
                 'Note that PyQt5 can be installed via pip for Python3.')
                 'Note that PyQt5 can be installed via pip for Python3.')
             sys.exit(1)
             sys.exit(1)
         assert PY3
         assert PY3
         # PyQt5 can be installed via pip for Python3
         # PyQt5 can be installed via pip for Python3
         install_requires.append('pyqt5')
         install_requires.append('pyqt5')
-        PYQT_VERSION = 5
+        QT_BINDING = 'pyqt5'
+del QT_BINDING
 
 
 
 
 if sys.argv[1] == 'release':
 if sys.argv[1] == 'release':