lib.py 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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.QtGui import *
  22. from PyQt5.QtCore import *
  23. from PyQt5.QtWidgets import *
  24. except ImportError:
  25. from PyQt4.QtGui import *
  26. from PyQt4.QtCore import *
  27. def newIcon(icon):
  28. return QIcon(':/' + icon)
  29. def newButton(text, icon=None, slot=None):
  30. b = QPushButton(text)
  31. if icon is not None:
  32. b.setIcon(newIcon(icon))
  33. if slot is not None:
  34. b.clicked.connect(slot)
  35. return b
  36. def newAction(parent, text, slot=None, shortcut=None, icon=None,
  37. tip=None, checkable=False, enabled=True):
  38. """Create a new action and assign callbacks, shortcuts, etc."""
  39. a = QAction(text, parent)
  40. if icon is not None:
  41. a.setIcon(newIcon(icon))
  42. if shortcut is not None:
  43. if isinstance(shortcut, (list, tuple)):
  44. a.setShortcuts(shortcut)
  45. else:
  46. a.setShortcut(shortcut)
  47. if tip is not None:
  48. a.setToolTip(tip)
  49. a.setStatusTip(tip)
  50. if slot is not None:
  51. a.triggered.connect(slot)
  52. if checkable:
  53. a.setCheckable(True)
  54. a.setEnabled(enabled)
  55. return a
  56. def addActions(widget, actions):
  57. for action in actions:
  58. if action is None:
  59. widget.addSeparator()
  60. elif isinstance(action, QMenu):
  61. widget.addMenu(action)
  62. else:
  63. widget.addAction(action)
  64. def labelValidator():
  65. return QRegExpValidator(QRegExp(r'^[^ \t].+'), None)
  66. class struct(object):
  67. def __init__(self, **kwargs):
  68. self.__dict__.update(kwargs)
  69. def distance(p):
  70. return sqrt(p.x() * p.x() + p.y() * p.y())
  71. def fmtShortcut(text):
  72. mod, key = text.split('+', 1)
  73. return '<b>%s</b>+<b>%s</b>' % (mod, key)