Skip to content

Commit

Permalink
Use qtpy to abstract Qt python binding
Browse files Browse the repository at this point in the history
  • Loading branch information
wkentaro committed Apr 15, 2018
1 parent 03fc8e1 commit c59d3f4
Show file tree
Hide file tree
Showing 10 changed files with 34 additions and 76 deletions.
21 changes: 7 additions & 14 deletions labelme/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,11 @@
import subprocess
import sys

try:
from PyQt5 import QtCore
from PyQt5.QtCore import Qt
from PyQt5 import QtGui
from PyQt5 import QtWidgets
PYQT5 = True
except ImportError:
from PyQt4 import QtCore
from PyQt4.QtCore import Qt
from PyQt4 import QtGui
from PyQt4 import QtGui as QtWidgets
PYQT5 = False
from qtpy import PYQT5
from qtpy import QtCore
from qtpy.QtCore import Qt
from qtpy import QtGui
from qtpy import QtWidgets

from labelme.canvas import Canvas
from labelme.colorDialog import ColorDialog
Expand Down Expand Up @@ -979,7 +972,7 @@ def openFile(self, _value=False):
filename = QtWidgets.QFileDialog.getOpenFileName(
self, '%s - Choose Image or Label file' % __appname__,
path, filters)
if PYQT5:
if qtpy.PYQT5:
filename, _ = filename
filename = str(filename)
if filename:
Expand Down Expand Up @@ -1015,7 +1008,7 @@ def saveFileDialog(self):
filename = dlg.getSaveFileName(
self, 'Choose File', default_labelfile_name,
'Label files (*%s)' % LabelFile.suffix)
if PYQT5:
if qtpy.PYQT5:
filename, _ = filename
filename = str(filename)
return filename
Expand Down
26 changes: 10 additions & 16 deletions labelme/canvas.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,10 @@

import sys

try:
from PyQt5 import QtCore
from PyQt5 import QtGui
from PyQt5 import QtWidgets
PYQT5 = True
except ImportError:
from PyQt4 import QtCore
from PyQt4 import QtGui
from PyQt4 import QtGui as QtWidgets
PYQT5 = False
from qtpy import PYQT5
from qtpy import QtCore
from qtpy import QtGui
from qtpy import QtWidgets

from labelme.lib import distance
from labelme.shape import Shape
Expand All @@ -29,12 +23,12 @@


class Canvas(QtWidgets.QWidget):
zoomRequest = QtCore.pyqtSignal(int, QtCore.QPoint)
scrollRequest = QtCore.pyqtSignal(int, int)
newShape = QtCore.pyqtSignal()
selectionChanged = QtCore.pyqtSignal(bool)
shapeMoved = QtCore.pyqtSignal()
drawingPolygon = QtCore.pyqtSignal(bool)
zoomRequest = QtCore.Signal(int, QtCore.QPoint)
scrollRequest = QtCore.Signal(int, int)
newShape = QtCore.Signal()
selectionChanged = QtCore.Signal(bool)
shapeMoved = QtCore.Signal()
drawingPolygon = QtCore.Signal(bool)

CREATE, EDIT = 0, 1

Expand Down
5 changes: 1 addition & 4 deletions labelme/colorDialog.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
try:
from PyQt5 import QtWidgets
except ImportError:
from PyQt4 import QtGui as QtWidgets
from qtpy import QtWidgets


class ColorDialog(QtWidgets.QColorDialog):
Expand Down
14 changes: 4 additions & 10 deletions labelme/labelDialog.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,7 @@
try:
from PyQt5 import QtCore
from PyQt5 import QtGui
from PyQt5 import QtWidgets
PYQT5 = True
except ImportError:
from PyQt4 import QtCore
from PyQt4 import QtGui
from PyQt4 import QtGui as QtWidgets
PYQT5 = False
from qtpy import PYQT5
from qtpy import QtCore
from qtpy import QtGui
from qtpy import QtWidgets

from .lib import labelValidator
from .lib import newIcon
Expand Down
11 changes: 3 additions & 8 deletions labelme/lib.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,9 @@
from math import sqrt
import os.path as osp

try:
from PyQt5 import QtCore
from PyQt5 import QtGui
from PyQt5 import QtWidgets
except ImportError:
from PyQt4 import QtCore
from PyQt4 import QtGui
from PyQt4 import QtGui as QtWidgets
from qtpy import QtCore
from qtpy import QtGui
from qtpy import QtWidgets


here = osp.dirname(osp.abspath(__file__))
Expand Down
5 changes: 1 addition & 4 deletions labelme/shape.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
try:
from PyQt5 import QtGui
except ImportError:
from PyQt4 import QtGui
from qtpy import QtGui

from .lib import distance

Expand Down
8 changes: 2 additions & 6 deletions labelme/toolBar.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
try:
from PyQt5 import QtCore
from PyQt5 import QtWidgets
except ImportError:
from PyQt4 import QtCore
from PyQt4 import QtGui as QtWidgets
from qtpy import QtCore
from qtpy import QtWidgets


class ToolBar(QtWidgets.QToolBar):
Expand Down
11 changes: 3 additions & 8 deletions labelme/zoomWidget.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
try:
from PyQt5 import QtCore
from PyQt5 import QtGui
from PyQt5 import QtWidgets
except ImportError:
from PyQt4 import QtCore
from PyQt4 import QtGui
from PyQt4 import QtGui as QtWidgets
from qtpy import QtCore
from qtpy import QtGui
from qtpy import QtWidgets


class ZoomWidget(QtWidgets.QSpinBox):
Expand Down
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
'numpy',
'Pillow>=2.8.0',
'PyYAML',
'qtpy',
]


Expand Down
8 changes: 2 additions & 6 deletions tests/test_labelDialog.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
try:
from PyQt5 import QtCore
from PyQt5 import QtWidgets
except ImportError:
from PyQt4 import QtCore
from PyQt4 import QtGui as QtWidgets
from qtpy import QtCore
from qtpy import QtWidgets

from labelme import labelDialog

Expand Down

0 comments on commit c59d3f4

Please sign in to comment.