Browse Source

Support PySide2 (PySide is deprecated so not supported)

Kentaro Wada 7 năm trước cách đây
mục cha
commit
4518bdb13e
5 tập tin đã thay đổi với 43 bổ sung21 xóa
  1. 7 7
      labelme/app.py
  2. 10 5
      labelme/canvas.py
  3. 5 3
      labelme/labelDialog.py
  4. 2 2
      labelme/toolBar.py
  5. 19 4
      setup.py

+ 7 - 7
labelme/app.py

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

+ 10 - 5
labelme/canvas.py

@@ -2,11 +2,13 @@ from __future__ import print_function
 
 import sys
 
-from qtpy import PYQT5
+from qtpy import QT_VERSION
 from qtpy import QtCore
 from qtpy import QtGui
 from qtpy import QtWidgets
 
+QT5 = QT_VERSION[0] == '5'
+
 from labelme.lib import distance
 from labelme.shape import Shape
 
@@ -95,7 +97,7 @@ class Canvas(QtWidgets.QWidget):
 
     def mouseMoveEvent(self, ev):
         """Update line with last point and current coordinates."""
-        if PYQT5:
+        if QT5:
             pos = self.transformPos(ev.pos())
         else:
             pos = self.transformPos(ev.posF())
@@ -186,7 +188,7 @@ class Canvas(QtWidgets.QWidget):
             self.hVertex, self.hShape = None, None
 
     def mousePressEvent(self, ev):
-        if PYQT5:
+        if QT5:
             pos = self.transformPos(ev.pos())
         else:
             pos = self.transformPos(ev.posF())
@@ -402,7 +404,10 @@ class Canvas(QtWidgets.QWidget):
         aw, ah = area.width(), area.height()
         x = (aw - w) / (2 * s) if aw > w 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):
         w, h = self.pixmap.width(), self.pixmap.height()
@@ -485,7 +490,7 @@ class Canvas(QtWidgets.QWidget):
         return super(Canvas, self).minimumSizeHint()
 
     def wheelEvent(self, ev):
-        if PYQT5:
+        if QT5:
             mods = ev.modifiers()
             delta = ev.angleDelta()
             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 QtGui
 from qtpy import QtWidgets
 
+QT5 = QT_VERSION[0] == '5'
+
 from .lib import labelValidator
 from .lib import newIcon
 
@@ -76,7 +78,7 @@ class LabelDialog(QtWidgets.QDialog):
         self.edit.setText(item.text())
 
     def validate(self):
-        if PYQT5:
+        if QT5:
             if self.edit.text().strip():
                 self.accept()
         else:
@@ -84,7 +86,7 @@ class LabelDialog(QtWidgets.QDialog):
                 self.accept()
 
     def postProcess(self):
-        if PYQT5:
+        if QT5:
             self.edit.setText(self.edit.text().strip())
         else:
             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()
         w1, h1 = ms.width(), ms.height()
         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',
 ]
 
+# Find python binding for qt with priority:
+# PyQt5 -> PySide2 -> PyQt4,
+# and PyQt5 is automatically installed on Python3.
+QT_BINDING = None
 
 try:
     import PyQt5  # NOQA
-    PYQT_VERSION = 5
+    QT_BINDING = 'pyqt5'
 except ImportError:
+    pass
+
+if QT_BINDING is None:
+    try:
+        import PySide2  # NOQA
+        QT_BINDING = 'pyside2'
+    except ImportError:
+        pass
+
+if QT_BINDING is None:
     try:
         import PyQt4  # NOQA
-        PYQT_VERSION = 4
+        QT_BINDING = 'pyqt4'
     except ImportError:
         if PY2:
             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.')
             sys.exit(1)
         assert PY3
         # PyQt5 can be installed via pip for Python3
         install_requires.append('pyqt5')
-        PYQT_VERSION = 5
+        QT_BINDING = 'pyqt5'
+del QT_BINDING
 
 
 if sys.argv[1] == 'release':