lib.py 2.2 KB

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