lib.py 1.4 KB

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