104 lines
2.6 KiB
Python
104 lines
2.6 KiB
Python
from math import sqrt
|
|
from libs.ustr import ustr
|
|
import hashlib
|
|
import re
|
|
import sys
|
|
|
|
try:
|
|
from PyQt5.QtGui import *
|
|
from PyQt5.QtCore import *
|
|
from PyQt5.QtWidgets import *
|
|
except ImportError:
|
|
from PyQt4.QtGui import *
|
|
from PyQt4.QtCore import *
|
|
|
|
|
|
def newIcon(icon):
|
|
return QIcon(':/' + icon)
|
|
|
|
|
|
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
|
|
|
|
|
|
def newAction(parent, text, slot=None, shortcut=None, icon=None,
|
|
tip=None, checkable=False, enabled=True):
|
|
"""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)
|
|
|
|
|
|
def labelValidator():
|
|
return QRegExpValidator(QRegExp(r'^[^ \t].+'), None)
|
|
|
|
|
|
class struct(object):
|
|
|
|
def __init__(self, **kwargs):
|
|
self.__dict__.update(kwargs)
|
|
|
|
|
|
def distance(p):
|
|
return sqrt(p.x() * p.x() + p.y() * p.y())
|
|
|
|
|
|
def fmtShortcut(text):
|
|
mod, key = text.split('+', 1)
|
|
return '<b>%s</b>+<b>%s</b>' % (mod, key)
|
|
|
|
|
|
def generateColorByText(text):
|
|
s = ustr(text)
|
|
hashCode = int(hashlib.sha256(s.encode('utf-8')).hexdigest(), 16)
|
|
r = int((hashCode / 255) % 255)
|
|
g = int((hashCode / 65025) % 255)
|
|
b = int((hashCode / 16581375) % 255)
|
|
return QColor(r, g, b, 100)
|
|
|
|
def have_qstring():
|
|
'''p3/qt5 get rid of QString wrapper as py3 has native unicode str type'''
|
|
return not (sys.version_info.major >= 3 or QT_VERSION_STR.startswith('5.'))
|
|
|
|
def util_qt_strlistclass():
|
|
return QStringList if have_qstring() else list
|
|
|
|
def natural_sort(list, key=lambda s:s):
|
|
"""
|
|
Sort the list into natural alphanumeric order.
|
|
"""
|
|
def get_alphanum_key_func(key):
|
|
convert = lambda text: int(text) if text.isdigit() else text
|
|
return lambda s: [convert(c) for c in re.split('([0-9]+)', key(s))]
|
|
sort_key = get_alphanum_key_func(key)
|
|
list.sort(key=sort_key)
|