greenhouse/libs/lib.py

74 lines
1.7 KiB
Python
Raw Normal View History

2015-09-17 10:37:20 +08:00
from math import sqrt
2017-01-02 20:50:02 -05:00
try:
from PyQt5.QtGui import *
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
except ImportError:
from PyQt4.QtGui import *
from PyQt4.QtCore import *
2015-09-17 10:37:20 +08:00
def newIcon(icon):
return QIcon(':/' + icon)
2015-09-17 10:37:20 +08:00
def newButton(text, icon=None, slot=None):
b = QPushButton(text)
if icon is not None:
b.setIcon(newIcon(icon))
if slot is not None:
b.clicked.connect(slot)
return b
2015-09-17 10:37:20 +08:00
def newAction(parent, text, slot=None, shortcut=None, icon=None,
tip=None, checkable=False, enabled=True):
2015-09-17 10:37:20 +08:00
"""Create a new action and assign callbacks, shortcuts, etc."""
a = QAction(text, parent)
if icon is not None:
a.setIcon(newIcon(icon))
if shortcut is not None:
if isinstance(shortcut, (list, tuple)):
a.setShortcuts(shortcut)
else:
a.setShortcut(shortcut)
if tip is not None:
a.setToolTip(tip)
a.setStatusTip(tip)
if slot is not None:
a.triggered.connect(slot)
if checkable:
a.setCheckable(True)
a.setEnabled(enabled)
return a
def addActions(widget, actions):
for action in actions:
if action is None:
widget.addSeparator()
elif isinstance(action, QMenu):
widget.addMenu(action)
else:
widget.addAction(action)
2015-09-17 10:37:20 +08:00
def labelValidator():
return QRegExpValidator(QRegExp(r'^[^ \t].+'), None)
class struct(object):
2015-09-17 10:37:20 +08:00
def __init__(self, **kwargs):
self.__dict__.update(kwargs)
2015-09-17 10:37:20 +08:00
def distance(p):
return sqrt(p.x() * p.x() + p.y() * p.y())
2015-09-17 10:37:20 +08:00
def fmtShortcut(text):
mod, key = text.split('+', 1)
return '<b>%s</b>+<b>%s</b>' % (mod, key)