lib.py 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. #
  2. # Copyright (C) 2011 Michael Pitidis, Hussein Abdulwahid.
  3. #
  4. # This file is part of Labelme.
  5. #
  6. # Labelme is free software: you can redistribute it and/or modify
  7. # it under the terms of the GNU General Public License as published by
  8. # the Free Software Foundation, either version 3 of the License, or
  9. # (at your option) any later version.
  10. #
  11. # Labelme is distributed in the hope that it will be useful,
  12. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. # GNU General Public License for more details.
  15. #
  16. # You should have received a copy of the GNU General Public License
  17. # along with Labelme. If not, see <http://www.gnu.org/licenses/>.
  18. #
  19. from math import sqrt
  20. try:
  21. from PyQt5 import QtCore
  22. from PyQt5 import QtGui
  23. from PyQt5 import QtWidgets
  24. except ImportError:
  25. from PyQt4 import QtCore
  26. from PyQt4 import QtGui
  27. from PyQt4 import QtGui as QtWidgets
  28. def newIcon(icon):
  29. return QtGui.QIcon(':/' + icon)
  30. def newButton(text, icon=None, slot=None):
  31. b = QtWidgets.QPushButton(text)
  32. if icon is not None:
  33. b.setIcon(newIcon(icon))
  34. if slot is not None:
  35. b.clicked.connect(slot)
  36. return b
  37. def newAction(parent, text, slot=None, shortcut=None, icon=None,
  38. tip=None, checkable=False, enabled=True):
  39. """Create a new action and assign callbacks, shortcuts, etc."""
  40. a = QtGui.QAction(text, parent)
  41. if icon is not None:
  42. a.setIcon(newIcon(icon))
  43. if shortcut is not None:
  44. if isinstance(shortcut, (list, tuple)):
  45. a.setShortcuts(shortcut)
  46. else:
  47. a.setShortcut(shortcut)
  48. if tip is not None:
  49. a.setToolTip(tip)
  50. a.setStatusTip(tip)
  51. if slot is not None:
  52. a.triggered.connect(slot)
  53. if checkable:
  54. a.setCheckable(True)
  55. a.setEnabled(enabled)
  56. return a
  57. def addActions(widget, actions):
  58. for action in actions:
  59. if action is None:
  60. widget.addSeparator()
  61. elif isinstance(action, QtGui.QMenu):
  62. widget.addMenu(action)
  63. else:
  64. widget.addAction(action)
  65. def labelValidator():
  66. return QtGui.QRegExpValidator(QtCore.QRegExp(r'^[^ \t].+'), None)
  67. class struct(object):
  68. def __init__(self, **kwargs):
  69. self.__dict__.update(kwargs)
  70. def distance(p):
  71. return sqrt(p.x() * p.x() + p.y() * p.y())
  72. def fmtShortcut(text):
  73. mod, key = text.split('+', 1)
  74. return '<b>%s</b>+<b>%s</b>' % (mod, key)