diff --git a/labelImg.py b/labelImg.py index 0115aa3a..dd4856ca 100755 --- a/labelImg.py +++ b/labelImg.py @@ -29,7 +29,7 @@ except ImportError: import resources # Add internal libs from libs.constants import * -from libs.lib import struct, newAction, newIcon, addActions, fmtShortcut, generateColorByText +from libs.utils import * from libs.settings import Settings from libs.shape import Shape, DEFAULT_LINE_COLOR, DEFAULT_FILL_COLOR from libs.stringBundle import StringBundle @@ -49,27 +49,6 @@ from libs.hashableQListWidgetItem import HashableQListWidgetItem __appname__ = 'labelImg' -# Utility functions and classes. - -def have_qstring(): - '''p3/qt5 get rid of QString wrapper as py3 has native unicode str type''' - return not (sys.version_info.major >= 3 or QT_VERSION_STR.startswith('5.')) - -def util_qt_strlistclass(): - return QStringList if have_qstring() else list - - -def natural_sort(list, key=lambda s:s): - """ - Sort the list into natural alphanumeric order. - """ - def get_alphanum_key_func(key): - convert = lambda text: int(text) if text.isdigit() else text - return lambda s: [convert(c) for c in re.split('([0-9]+)', key(s))] - sort_key = get_alphanum_key_func(key) - list.sort(key=sort_key) - - class WindowMixin(object): def menu(self, title, actions=None): diff --git a/libs/canvas.py b/libs/canvas.py index a53414f4..211c390b 100644 --- a/libs/canvas.py +++ b/libs/canvas.py @@ -10,7 +10,7 @@ except ImportError: #from PyQt4.QtOpenGL import * from libs.shape import Shape -from libs.lib import distance +from libs.utils import distance CURSOR_DEFAULT = Qt.ArrowCursor CURSOR_POINT = Qt.PointingHandCursor @@ -42,7 +42,7 @@ class Canvas(QWidget): self.selectedShape = None # save the selected shape here self.selectedShapeCopy = None self.drawingLineColor = QColor(0, 0, 255) - self.drawingRectColor = QColor(0, 0, 255) + self.drawingRectColor = QColor(0, 0, 255) self.line = Shape(line_color=self.drawingLineColor) self.prevPoint = QPointF() self.offsets = QPointF(), QPointF() @@ -662,7 +662,7 @@ class Canvas(QWidget): self.shapes[-1].label = text if line_color: self.shapes[-1].line_color = line_color - + if fill_color: self.shapes[-1].fill_color = fill_color diff --git a/libs/labelDialog.py b/libs/labelDialog.py index 4df80545..b59de084 100644 --- a/libs/labelDialog.py +++ b/libs/labelDialog.py @@ -6,7 +6,7 @@ except ImportError: from PyQt4.QtGui import * from PyQt4.QtCore import * -from libs.lib import newIcon, labelValidator +from libs.utils import newIcon, labelValidator BB = QDialogButtonBox @@ -77,7 +77,7 @@ class LabelDialog(QDialog): # PyQt5: AttributeError: 'str' object has no attribute 'trimmed' text = tQListWidgetItem.text().strip() self.edit.setText(text) - + def listItemDoubleClick(self, tQListWidgetItem): self.listItemClick(tQListWidgetItem) self.validate() diff --git a/libs/shape.py b/libs/shape.py index c934ca09..c72a68f7 100644 --- a/libs/shape.py +++ b/libs/shape.py @@ -9,7 +9,7 @@ except ImportError: from PyQt4.QtGui import * from PyQt4.QtCore import * -from libs.lib import distance +from libs.utils import distance import sys DEFAULT_LINE_COLOR = QColor(0, 255, 0, 128) diff --git a/libs/stringBundle.py b/libs/stringBundle.py index 6cbc3e44..aa99bc99 100644 --- a/libs/stringBundle.py +++ b/libs/stringBundle.py @@ -35,7 +35,8 @@ class StringBundle: locale.getlocale()) > 0 else os.getenv('LANG') except: print('Invalid locale') - locale = 'en' + localeStr = 'en' + return StringBundle(cls.__create_key, localeStr) def getString(self, stringId): diff --git a/libs/lib.py b/libs/utils.py similarity index 75% rename from libs/lib.py rename to libs/utils.py index 2a040976..dacf682a 100644 --- a/libs/lib.py +++ b/libs/utils.py @@ -1,6 +1,9 @@ from math import sqrt from libs.ustr import ustr import hashlib +import re +import sys + try: from PyQt5.QtGui import * from PyQt5.QtCore import * @@ -81,3 +84,20 @@ def generateColorByText(text): g = int((hashCode / 65025) % 255) b = int((hashCode / 16581375) % 255) return QColor(r, g, b, 100) + +def have_qstring(): + '''p3/qt5 get rid of QString wrapper as py3 has native unicode str type''' + return not (sys.version_info.major >= 3 or QT_VERSION_STR.startswith('5.')) + +def util_qt_strlistclass(): + return QStringList if have_qstring() else list + +def natural_sort(list, key=lambda s:s): + """ + Sort the list into natural alphanumeric order. + """ + def get_alphanum_key_func(key): + convert = lambda text: int(text) if text.isdigit() else text + return lambda s: [convert(c) for c in re.split('([0-9]+)', key(s))] + sort_key = get_alphanum_key_func(key) + list.sort(key=sort_key) diff --git a/tests/test_lib.py b/tests/test_lib.py deleted file mode 100644 index 547179b9..00000000 --- a/tests/test_lib.py +++ /dev/null @@ -1,15 +0,0 @@ -import os -import sys -import unittest -from libs.lib import struct, newAction, newIcon, addActions, fmtShortcut, generateColorByText - -class TestLib(unittest.TestCase): - - def test_generateColorByGivingUniceText_noError(self): - res = generateColorByText(u'\u958B\u555F\u76EE\u9304') - self.assertTrue(res.green() >= 0) - self.assertTrue(res.red() >= 0) - self.assertTrue(res.blue() >= 0) - -if __name__ == '__main__': - unittest.main() diff --git a/tests/test_utils.py b/tests/test_utils.py new file mode 100644 index 00000000..1b0a6c45 --- /dev/null +++ b/tests/test_utils.py @@ -0,0 +1,22 @@ +import os +import sys +import unittest +from libs.utils import struct, newAction, newIcon, addActions, fmtShortcut, generateColorByText, natural_sort + +class TestUtils(unittest.TestCase): + + def test_generateColorByGivingUniceText_noError(self): + res = generateColorByText(u'\u958B\u555F\u76EE\u9304') + self.assertTrue(res.green() >= 0) + self.assertTrue(res.red() >= 0) + self.assertTrue(res.blue() >= 0) + + def test_nautalSort_noError(self): + l1 = ['f1', 'f11', 'f3' ] + exptected_l1 = ['f1', 'f3', 'f11'] + natural_sort(l1) + for idx, val in enumerate(l1): + self.assertTrue(val == exptected_l1[idx]) + +if __name__ == '__main__': + unittest.main()