lib.py 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. from math import sqrt
  2. from PyQt4.QtGui import *
  3. from PyQt4.QtCore import *
  4. def newIcon(icon):
  5. return QIcon(':/' + icon)
  6. def newButton(text, icon=None, slot=None):
  7. b = QPushButton(text)
  8. if icon is not None:
  9. b.setIcon(newIcon(icon))
  10. if slot is not None:
  11. b.clicked.connect(slot)
  12. return b
  13. def newAction(parent, text, slot=None, shortcut=None, icon=None,
  14. tip=None, checkable=False, enabled=True):
  15. """Create a new action and assign callbacks, shortcuts, etc."""
  16. a = QAction(text, parent)
  17. if icon is not None:
  18. a.setIcon(newIcon(icon))
  19. if shortcut is not None:
  20. if isinstance(shortcut, (list, tuple)):
  21. a.setShortcuts(shortcut)
  22. else:
  23. a.setShortcut(shortcut)
  24. if tip is not None:
  25. a.setToolTip(tip)
  26. a.setStatusTip(tip)
  27. if slot is not None:
  28. a.triggered.connect(slot)
  29. if checkable:
  30. a.setCheckable(True)
  31. a.setEnabled(enabled)
  32. return a
  33. def addActions(widget, actions):
  34. for action in actions:
  35. if action is None:
  36. widget.addSeparator()
  37. else:
  38. widget.addAction(action)
  39. def labelValidator():
  40. return QRegExpValidator(QRegExp(r'^[^ \t].+'), None)
  41. class struct(object):
  42. def __init__(self, **kwargs):
  43. self.__dict__.update(kwargs)
  44. def distance(p):
  45. return sqrt(p.x() * p.x() + p.y() * p.y())
  46. def fmtShortcut(text):
  47. mod, key = text.split('+', 1)
  48. return '<b>%s</b>+<b>%s</b>' % (mod, key)