1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204
|
import enum
import inspect
import pkgutil
import sys
import warnings
from operator import attrgetter
from typing import Union, Iterator, Optional
from AnyQt.QtCore import QObject, QRect, QSize, QPoint, QTextBoundaryFinder
from AnyQt.QtGui import QIcon
from orangecanvas.gui.svgiconengine import StyledSvgIconEngine
def progress_bar_milestones(count, iterations=100):
return set([int(i*count/float(iterations)) for i in range(iterations)])
_NOTSET = object()
def deepgetattr(obj, attr, default=_NOTSET):
"""Works exactly like getattr(), except that attr can be a nested attribute
(e.g. "attr1.attr2.attr3").
"""
try:
return attrgetter(attr)(obj)
except AttributeError:
if default is _NOTSET:
raise
return default
def getdeepattr(obj, attr, *arg, **kwarg):
if isinstance(obj, dict):
return obj.get(attr)
return deepgetattr(obj, attr, *arg, **kwarg)
def to_html(str):
return str.replace("<=", "≤").replace(">=", "≥").\
replace("<", "<").replace(">", ">").replace("=\\=", "≠")
getHtmlCompatibleString = to_html
def dumpObjectTree(obj, _indent=0):
"""
Dumps Qt QObject tree. Aids in debugging internals.
See also: QObject.dumpObjectTree()
"""
assert isinstance(obj, QObject)
print('{indent}{type} "{name}"'.format(indent=' ' * (_indent * 4),
type=type(obj).__name__,
name=obj.objectName()),
file=sys.stderr)
for child in obj.children():
dumpObjectTree(child, _indent + 1)
def getmembers(obj, predicate=None):
"""Return all the members of an object in a list of (name, value) pairs sorted by name.
Behaves like inspect.getmembers. If a type object is passed as a predicate,
only members of that type are returned.
"""
if isinstance(predicate, type):
def mypredicate(x):
return isinstance(x, predicate)
else:
mypredicate = predicate
return inspect.getmembers(obj, mypredicate)
class DeprecatedSignal:
def __init__(self, actual_signal, *args,
warning_text='Deprecated', emit_callback=None, **kwargs):
self.signal = actual_signal
self.warning_text = warning_text
self.emit_callback = emit_callback
def emit(self, *args, **kwargs):
warnings.warn(
self.warning_text,
DeprecationWarning, stacklevel=2
)
if self.emit_callback:
self.emit_callback(*args, **kwargs)
return self.signal.emit(*args, **kwargs)
def __getattr__(self, item):
return self.__signal.item
def enum_as_int(value: Union[int, enum.Enum]) -> int:
"""
Return a `enum.Enum` value as an `int.
This is function intended for extracting underlying Qt5/6 enum
values specifically with PyQt6 where most Qt enums are represented
with `enum.Enum` and lose their numerical value.
>>> from PyQt6.QtCore import Qt
>>> enum_as_int(Qt.Alignment.AlignLeft)
1
"""
if isinstance(value, enum.Enum):
return int(value.value)
else:
return int(value)
def dropdown_popup_geometry(
size: QSize, origin: QRect, screen: QRect, preferred_direction="down"
) -> QRect:
"""
Move/constrain the geometry for a drop down popup.
Parameters
----------
size : QSize
The base popup size if not constrained.
origin : QRect
The origin rect from which the popup extends (in screen coords.).
screen : QRect
The available screen geometry into which the popup must fit.
preferred_direction : str
'up' or 'down'
Returns
-------
geometry: QRect
Constrained drop down list geometry to fit into screen
"""
if preferred_direction == "down":
# if the popup geometry extends bellow the screen and there is more
# room above the popup origin ...
geometry = QRect(origin.bottomLeft() + QPoint(0, 1), size)
if geometry.bottom() > screen.bottom() \
and origin.center().y() > screen.center().y():
# ...flip the rect about the origin so it extends upwards
geometry.moveBottom(origin.top() - 1)
elif preferred_direction == "up":
geometry = QRect(origin.topLeft() - QPoint(0, 1 + size.height()), size)
if geometry.top() < screen.top() \
and origin.center().y() < screen.center().y():
# ... flip, extend down
geometry.moveTop(origin.bottom() - 1)
else:
raise ValueError(f"Invalid 'preferred_direction' ({preferred_direction})")
# fixup horizontal position if it extends outside the screen
if geometry.left() < screen.left():
geometry.moveLeft(screen.left())
if geometry.right() > screen.right():
geometry.moveRight(screen.right())
# bounded by screen geometry
return geometry.intersected(screen)
def graphemes(text: str) -> Iterator[str]:
"""
Return an iterator over grapheme clusters of text
"""
# match internal QString encoding
text_encoded = text.encode("utf-16-le")
finder = QTextBoundaryFinder(QTextBoundaryFinder.Grapheme, text)
start = 0
while True:
pos = finder.toNextBoundary()
if pos == -1:
return
yield text_encoded[start*2: pos*2].decode("utf-16-le")
start = pos
def grapheme_slice(text: str, start: int = 0, end: int = None) -> str:
"""
Return a substring of text counting grapheme clusters not codepoints.
"""
if start < 0 or (end is not None and end < 0):
raise ValueError("negative start or end")
s = 0
slice_start: Optional[int] = None
slice_end: Optional[int] = None
for i, g in enumerate(graphemes(text)):
if i == start:
slice_start = s
if i + 1 == end:
slice_end = s + len(g)
break
s += len(g)
if slice_start is None:
return ""
if slice_end is None:
slice_end = len(text)
return text[slice_start: slice_end]
def load_styled_icon(package: str, path: str) -> QIcon:
return QIcon(StyledSvgIconEngine(pkgutil.get_data(package, path)))
|