lib.py 1.8 KB

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