lib.py 1.7 KB

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