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 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766
|
import itertools
from math import log10, floor, ceil
from typing import Union, Optional, Callable
import numpy as np
from AnyQt.QtCore import (
QRectF, QLineF, QObject, QEvent, Qt, pyqtSignal as Signal
)
from AnyQt.QtGui import QTransform, QFontMetrics, QStaticText, QBrush, QPen, \
QFont, QPalette
from AnyQt.QtWidgets import (
QGraphicsLineItem, QGraphicsSceneMouseEvent, QPinchGesture,
QGraphicsItemGroup, QWidget, QGraphicsWidget
)
import pyqtgraph as pg
import pyqtgraph.functions as fn
from pyqtgraph.graphicsItems.LegendItem import ItemSample
from pyqtgraph.graphicsItems.ScatterPlotItem import drawSymbol
from Orange.widgets.utils.plot import SELECT, PANNING, ZOOMING
class TextItem(pg.TextItem):
if not hasattr(pg.TextItem, "setAnchor"):
# Compatibility with pyqtgraph <= 0.9.10; in (as of yet unreleased)
# 0.9.11 the TextItem has a `setAnchor`, but not `updateText`
def setAnchor(self, anchor):
self.anchor = pg.Point(anchor)
self.updateText()
def get_xy(self):
point = self.pos()
return point.x(), point.y()
class AnchorItem(pg.GraphicsWidget):
def __init__(self, parent=None, line=QLineF(), text="", **kwargs):
super().__init__(parent, **kwargs)
self._text = text
self.setFlag(QGraphicsWidget.ItemSendsScenePositionChanges)
self.setFlag(QGraphicsWidget.ItemHasNoContents)
self._spine = QGraphicsLineItem(line, self)
angle = line.angle()
self._arrow = pg.ArrowItem(parent=self, angle=0)
self._arrow.setPos(self._spine.line().p2())
self._arrow.setRotation(angle)
self._arrow.setStyle(headLen=10)
self._label = TextItem(text=text, color=(10, 10, 10))
self._label.setParentItem(self)
self._label.setPos(*self.get_xy())
self._label.setColor(self.palette().color(QPalette.Text))
if parent is not None:
self.setParentItem(parent)
self.__updateLayout()
def get_xy(self):
point = self._spine.line().p2()
return point.x(), point.y()
def setFont(self, font):
self._label.setFont(font)
def setText(self, text):
if text != self._text:
self._text = text
self._label.setText(text)
self._label.setVisible(bool(text))
def text(self):
return self._text
def setLine(self, *line):
line = QLineF(*line)
if line != self._spine.line():
self._spine.setLine(line)
self.__updateLayout()
def line(self):
return self._spine.line()
def setPen(self, pen):
self._spine.setPen(pen)
def setArrowVisible(self, visible):
self._arrow.setVisible(visible)
def paint(self, painter, option, widget):
pass
def boundingRect(self):
return QRectF()
def viewTransformChanged(self):
self.__updateLayout()
def __updateLayout(self):
T = self.sceneTransform()
if T is None:
T = QTransform()
# map the axis spine to scene coord. system.
viewbox_line = T.map(self._spine.line())
angle = viewbox_line.angle()
assert not np.isnan(angle)
# note in Qt the y axis is inverted (90 degree angle 'points' down)
left_quad = 270 < angle <= 360 or -0.0 <= angle < 90
# position the text label along the viewbox_line
label_pos = self._spine.line().pointAt(0.90)
if left_quad:
# Anchor the text under the axis spine
anchor = (0.5, -0.1)
else:
# Anchor the text over the axis spine
anchor = (0.5, 1.1)
self._label.setPos(label_pos)
self._label.setAnchor(pg.Point(*anchor))
self._label.setRotation(-angle if left_quad else 180 - angle)
self._arrow.setPos(self._spine.line().p2())
self._arrow.setRotation(180 - angle)
def changeEvent(self, event):
if event.type() == QEvent.PaletteChange:
self._label.setColor(self.palette().color(QPalette.Text))
super().changeEvent(event)
def itemChange(self, change, value):
if change in (
QGraphicsWidget.ItemParentHasChanged,
QGraphicsWidget.ItemSceneHasChanged,
# ItemScenePositionHasChanged seems to trigger for any scene
# transform change (even if the pos has not actually changed).
# Das ist gut.
QGraphicsWidget.ItemScenePositionHasChanged,
):
self.__updateLayout()
return super().itemChange(change, value)
class HelpEventDelegate(QObject):
def __init__(self, delegate, parent=None):
super().__init__(parent)
self.delegate = delegate
def eventFilter(self, _, event):
if event.type() == QEvent.GraphicsSceneHelp:
return self.delegate(event)
else:
return False
class MouseEventDelegate(HelpEventDelegate):
def __init__(self, delegate, delegate2, parent=None):
self.delegate2 = delegate2
super().__init__(delegate, parent=parent)
def eventFilter(self, obj, event):
if isinstance(event, QGraphicsSceneMouseEvent):
self.delegate2(event)
return super().eventFilter(obj, event)
class InteractiveViewBox(pg.ViewBox):
def __init__(self, graph, enable_menu=False):
self.init_history()
pg.ViewBox.__init__(self, enableMenu=enable_menu)
self.graph = graph
self.setMouseMode(self.PanMode)
self.grabGesture(Qt.PinchGesture)
@staticmethod
def _dragtip_pos():
return 10, 10
def setDragTooltip(self, tooltip):
scene = self.scene()
scene.addItem(tooltip)
tooltip.setPos(*self._dragtip_pos())
tooltip.hide()
scene.drag_tooltip = tooltip
def updateScaleBox(self, p1, p2):
"""
Overload to use ViewBox.mapToView instead of mapRectFromParent
mapRectFromParent (from Qt) uses QTransform.invert() which has
floating-point issues and can't invert the matrix with large
coefficients. ViewBox.mapToView uses invertQTransform from pyqtgraph.
This code, except for first three lines, are copied from the overloaded
method.
"""
p1 = self.mapToView(p1)
p2 = self.mapToView(p2)
r = QRectF(p1, p2)
self.rbScaleBox.setPos(r.topLeft())
tr = QTransform.fromScale(r.width(), r.height())
self.rbScaleBox.setTransform(tr)
self.rbScaleBox.show()
def safe_update_scale_box(self, buttonDownPos, currentPos):
x, y = currentPos
if buttonDownPos[0] == x:
x += 1
if buttonDownPos[1] == y:
y += 1
self.updateScaleBox(buttonDownPos, pg.Point(x, y))
def _updateDragtipShown(self, enabled):
scene = self.scene()
dragtip = scene.drag_tooltip
if enabled != dragtip.isVisible():
dragtip.setVisible(enabled)
# noinspection PyPep8Naming,PyMethodOverriding
def mouseDragEvent(self, ev, axis=None):
def get_mapped_rect():
p1, p2 = ev.buttonDownPos(ev.button()), ev.pos()
p1 = self.mapToView(p1)
p2 = self.mapToView(p2)
return QRectF(p1, p2)
def select():
ev.accept()
if ev.button() == Qt.LeftButton:
self.safe_update_scale_box(ev.buttonDownPos(), ev.pos())
if ev.isFinish():
self._updateDragtipShown(False)
if hasattr(self.graph, "unsuspend_jittering"):
self.graph.unsuspend_jittering()
self.rbScaleBox.hide()
value_rect = get_mapped_rect()
self.graph.select_by_rectangle(value_rect)
else:
self._updateDragtipShown(True)
if hasattr(self.graph, "suspend_jittering"):
self.graph.suspend_jittering()
self.safe_update_scale_box(ev.buttonDownPos(), ev.pos())
def zoom():
# A fixed version of the code from the inherited mouseDragEvent
# Use mapToView instead of mapRectFromParent
ev.accept()
self.rbScaleBox.hide()
ax = get_mapped_rect()
self.showAxRect(ax)
self.axHistoryPointer += 1
self.axHistory = self.axHistory[:self.axHistoryPointer] + [ax]
if self.graph.state == SELECT and axis is None:
select()
elif self.graph.state == ZOOMING or self.graph.state == PANNING:
# Inherited mouseDragEvent doesn't work for large zooms because it
# uses mapRectFromParent. We don't want to copy the parts of the
# method that work, hence we only use our code under the following
# conditions.
if ev.button() & (Qt.LeftButton | Qt.MiddleButton) \
and self.state['mouseMode'] == pg.ViewBox.RectMode \
and ev.isFinish():
zoom()
else:
super().mouseDragEvent(ev, axis=axis)
else:
ev.ignore()
def updateAutoRange(self):
# indirectly called by the autorange button on the graph
super().updateAutoRange()
self.tag_history()
def tag_history(self):
#add current view to history if it differs from the last view
if self.axHistory:
currentview = self.viewRect()
lastview = self.axHistory[self.axHistoryPointer]
inters = currentview & lastview
united = currentview.united(lastview)
# multiplication instead of division to avoid occasional
# division by zero in tests on github
if inters.width() * inters.height() \
> 0.95 * united.width() * united.height():
return
self.axHistoryPointer += 1
self.axHistory = self.axHistory[:self.axHistoryPointer] + \
[self.viewRect()]
def init_history(self):
self.axHistory = []
self.axHistoryPointer = -1
def autoRange(self, padding=None, items=None, item=None):
super().autoRange(padding=padding, items=items, item=item)
self.tag_history()
def suggestPadding(self, _): # no padding so that undo works correcty
return 0.
def scaleHistory(self, d):
self.tag_history()
super().scaleHistory(d)
def mouseClickEvent(self, ev):
if ev.button() == Qt.RightButton: # undo zoom
self.scaleHistory(-1)
elif ev.modifiers() == Qt.NoModifier:
ev.accept()
self.graph.unselect_all()
def sceneEvent(self, event):
if event.type() == QEvent.Gesture:
return self.gestureEvent(event)
return super().sceneEvent(event)
def gestureEvent(self, event):
gesture = event.gesture(Qt.PinchGesture)
if gesture.state() == Qt.GestureStarted:
event.accept(gesture)
elif gesture.changeFlags() & QPinchGesture.ScaleFactorChanged:
center = self.mapSceneToView(gesture.centerPoint())
scale_prev = gesture.lastScaleFactor()
scale = gesture.scaleFactor()
if scale_prev != 0:
scale = scale / scale_prev
if scale > 0:
self.scaleBy((1 / scale, 1 / scale), center)
elif gesture.state() == Qt.GestureFinished:
self.tag_history()
return True
class DraggableItemsViewBox(InteractiveViewBox):
"""
A viewbox with draggable items
Graph that uses it must provide two methods:
- `closest_draggable_item(pos)` returns an int representing the id of the
draggable item that is closest (and close enough) to `QPoint` pos, or
`None`;
- `show_indicator(item_id)` shows or updates an indicator for moving
the item with the given `item_id`.
Viewbox emits three signals:
- `started = Signal(item_id)`
- `moved = Signal(item_id, x, y)`
- `finished = Signal(item_id, x, y)`
"""
started = Signal(int)
moved = Signal(int, float, float)
finished = Signal(int, float, float)
def __init__(self, graph, enable_menu=False):
self.mouse_state = 0
self.item_id = None
super().__init__(graph, enable_menu)
def mousePressEvent(self, ev):
super().mousePressEvent(ev)
pos = self.childGroup.mapFromParent(ev.pos())
if self.graph.closest_draggable_item(pos) is not None:
self.setCursor(Qt.ClosedHandCursor)
def mouseDragEvent(self, ev, axis=None):
pos = self.childGroup.mapFromParent(ev.pos())
item_id = self.graph.closest_draggable_item(pos)
if ev.button() != Qt.LeftButton or (ev.start and item_id is None):
self.mouse_state = 2
if self.mouse_state == 2:
if ev.finish:
self.mouse_state = 0
super().mouseDragEvent(ev, axis)
return
ev.accept()
if ev.start:
self.setCursor(Qt.ClosedHandCursor)
self.mouse_state = 1
self.item_id = item_id
self.started.emit(self.item_id)
if self.mouse_state == 1:
if ev.finish:
self.mouse_state = 0
self.finished.emit(self.item_id, pos.x(), pos.y())
if self.graph.closest_draggable_item(pos) is not None:
self.setCursor(Qt.OpenHandCursor)
else:
self.setCursor(Qt.ArrowCursor)
self.item_id = None
else:
self.moved.emit(self.item_id, pos.x(), pos.y())
self.graph.show_indicator(self.item_id)
def wrap_legend_items(items, max_width, hspacing, vspacing):
def line_width(line):
return sum(item.boundingRect().width() for item in line) \
+ hspacing * (len(line) - 1)
def create_line(line, yi, fixed_width=None):
x = 0
for item in line:
item.setPos(x, yi * vspacing)
paragraph.addToGroup(item)
if fixed_width:
x += fixed_width
else:
x += item.boundingRect().width() + hspacing
max_item = max(item.boundingRect().width() + hspacing for item in items)
in_line = int(max_width // max_item)
if line_width(items) < max_width: # single line
lines = [items]
fixed_width = None
elif in_line < 2:
lines = [[]]
for i, item in enumerate(items): # just a single column - free wrap
lines[-1].append(item)
if line_width(lines[-1]) > max_width and len(lines[-1]) > 1:
lines.append([lines[-1].pop()])
fixed_width = None
else: # arrange into grid
lines = [items[i:i + in_line]
for i in range(0, len(items) + in_line - 1, in_line)]
fixed_width = max_item
paragraph = QGraphicsItemGroup()
for yi, line in enumerate(lines):
create_line(line, yi, fixed_width=fixed_width)
return paragraph
class ElidedLabelsAxis(pg.AxisItem):
"""
Horizontal axis that elides long text labels
The class assumes that ticks with labels are distributed equally, and that
standard `QWidget.font()` is used for printing them.
"""
def generateDrawSpecs(self, p):
axis_spec, tick_specs, text_specs = super().generateDrawSpecs(p)
bounds = self.mapRectFromParent(self.geometry())
max_width = int(0.9 * bounds.width() / (len(text_specs) or 1))
elide = QFontMetrics(QWidget().font()).elidedText
text_specs = [(rect, flags, elide(text, Qt.ElideRight, max_width))
for rect, flags, text in text_specs]
return axis_spec, tick_specs, text_specs
class DiscretizedScale:
"""
Compute suitable bins for continuous value from its minimal and
maximal value.
The width of the bin is a power of 10 (including negative powers).
The minimal value is rounded up and the maximal is rounded down. If this
gives less than 3 bins, the width is divided by four; if it gives
less than 6, it is halved.
.. attribute:: offset
The start of the first bin.
.. attribute:: width
The width of the bins
.. attribute:: bins
The number of bins
.. attribute:: decimals
The number of decimals used for printing out the boundaries
"""
def __init__(self, min_v, max_v):
"""
:param min_v: Minimal value
:type min_v: float
:param max_v: Maximal value
:type max_v: float
"""
super().__init__()
dif = max_v - min_v if max_v != min_v else 1
if np.isnan(dif):
min_v = 0
dif = decimals = 1
else:
decimals = -floor(log10(dif))
resolution = 10 ** -decimals
bins = ceil(dif / resolution)
if bins < 6:
decimals += 1
if bins < 3:
resolution /= 4
else:
resolution /= 2
bins = ceil(dif / resolution)
self.offset: Union[int, float] = resolution * floor(min_v // resolution)
self.bins = bins
self.decimals = max(decimals, 0)
self.width: Union[int, float] = resolution
def get_bins(self):
# if width is a very large int, dtype of width * np.arange is object
# hence we cast it to float
return self.offset + float(self.width) * np.arange(self.bins + 1)
class PaletteItemSample(ItemSample):
"""A color strip to insert into legends for discretized continuous values"""
def __init__(self, palette, scale,
label_formatter: Optional[Callable[[float], str]] = None):
"""
:param palette: palette used for showing continuous values
:type palette: BinnedContinuousPalette
:param scale: an instance of DiscretizedScale that defines the
conversion of values into bins
:type scale: DiscretizedScale
"""
super().__init__(None)
self.palette = palette
self.scale = scale
if label_formatter is None:
label_formatter = "{{:.{}f}}".format(scale.decimals).format
# offset and width can be in, but label_formatter expects float
# (because it can be ContinuousVariable.str_val), hence cast to float
cuts = [label_formatter(float(scale.offset + i * scale.width))
for i in range(scale.bins + 1)]
self.labels = [QStaticText("{} - {}".format(fr, to))
for fr, to in zip(cuts, cuts[1:])]
self.font = self.font()
self.font.setPointSize(11)
@property
def bin_height(self):
return self.font.pointSize() + 4
@property
def text_width(self):
for label in self.labels:
label.prepare(font=self.font)
return max(label.size().width() for label in self.labels)
def set_font(self, font: QFont):
self.font = font
self.update()
def boundingRect(self):
return QRectF(0, 0,
25 + self.text_width + self.bin_height,
20 + self.scale.bins * self.bin_height)
def paint(self, p, *args):
p.setRenderHint(p.Antialiasing)
p.translate(5, 5)
p.setFont(self.font)
colors = self.palette.qcolors
foreground = super().palette().color(QPalette.Text)
h = self.bin_height
for i, color, label in zip(itertools.count(), colors, self.labels):
p.setPen(Qt.NoPen)
p.setBrush(QBrush(color))
p.drawRect(0, i * h, h, h)
p.setPen(QPen(foreground))
p.drawStaticText(h + 5, i * h + 1, label)
class SymbolItemSample(ItemSample):
"""Adjust position for symbols"""
def __init__(self, pen, brush, size, symbol):
super().__init__(None)
self.__pen = fn.mkPen(pen)
self.__brush = fn.mkBrush(brush)
self.__size = size
self.__symbol = symbol
def paint(self, p, *args):
p.translate(8, 12)
drawSymbol(p, self.__symbol, self.__size, self.__pen, self.__brush)
class StyledAxisItem(pg.AxisItem):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.label.setDefaultTextColor(self.palette().color(QPalette.Text))
def changeEvent(self, event: QEvent) -> None:
if event.type() == QEvent.FontChange:
self.picture = None
self.update()
elif event.type() == QEvent.PaletteChange:
self.picture = None
self.label.setDefaultTextColor(self.palette().color(QPalette.Text))
self.update()
super().changeEvent(event)
__hasTextPen = False
def setTextPen(self, *args, **kwargs):
self.__hasTextPen = args or kwargs
super().setTextPen(*args, **kwargs)
if not self.__hasTextPen:
self.__clear_labelStyle_color()
def textPen(self):
if self.__hasTextPen:
return super().textPen()
else: # bypass pg.AxisItem
return QPen(self.palette().brush(QPalette.Text), 1)
__hasPen = False
def setPen(self, *args, **kwargs):
self.__hasPen = bool(args or kwargs)
super().setPen(*args, **kwargs)
if not self.__hasPen:
self.__clear_labelStyle_color()
def pen(self):
if self.__hasPen:
return super().pen()
else: # bypass pg.AxisItem
return QPen(self.palette().brush(QPalette.Text), 1)
def __clear_labelStyle_color(self):
try:
self.labelStyle.pop("color")
except AttributeError:
pass
class AxisItem(StyledAxisItem):
def __init__(self, orientation, rotate_ticks=False, **kwargs):
super().__init__(orientation, **kwargs)
self.style["rotateTicks"] = rotate_ticks
def setRotateTicks(self, rotate):
self.style["rotateTicks"] = rotate
self.picture = None # pylint: disable=attribute-defined-outside-init
self.prepareGeometryChange()
self.update()
def drawPicture(self, p, axisSpec, tickSpecs, textSpecs):
if self.orientation in ["bottom", "top"] and self.style["rotateTicks"]:
p.setRenderHint(p.Antialiasing, False)
p.setRenderHint(p.TextAntialiasing, True)
# draw long line along axis
pen, p1, p2 = axisSpec
p.setPen(pen)
p.drawLine(p1, p2)
p.translate(0.5, 0) # resolves some damn pixel ambiguity
# draw ticks
for pen, p1, p2 in tickSpecs:
p.setPen(pen)
p.drawLine(p1, p2)
# draw all text
if self.style['tickFont'] is not None:
p.setFont(self.style['tickFont'])
p.setPen(self.pen())
offset = self.style["tickTextOffset"][0]
max_text_size = 0
for rect, flags, text in textSpecs:
p.save()
p.translate(rect.x() + rect.width() / 2
- rect.y() - rect.height() / 2,
rect.x() + rect.width() + offset)
p.rotate(-90)
p.drawText(rect, flags, text)
p.restore()
max_text_size = max(max_text_size, rect.width())
self._updateMaxTextSize(max_text_size + offset)
else:
super().drawPicture(p, axisSpec, tickSpecs, textSpecs)
class PlotWidget(pg.PlotWidget):
"""
A pyqtgraph.PlotWidget with better QPalette integration.
A default constructed plot will respect and adapt to the current palette
"""
def __init__(self, *args, background=None, **kwargs):
axisItems = kwargs.pop("axisItems", None)
if axisItems is None: # Use palette aware AxisItems
axisItems = {"left": AxisItem("left"), "bottom": AxisItem("bottom")}
super().__init__(*args, background=background, axisItems=axisItems,
**kwargs)
if background is None:
# Revert the pg.PlotWidget's modifications, use default
# for QGraphicsView background role
self.setBackgroundRole(QPalette.Base)
# Reset changes to the palette (undo changes from pg.GraphicsView)
self.setPalette(QPalette())
self.__updateScenePalette()
def setScene(self, scene):
super().setScene(scene)
self.__updateScenePalette()
def changeEvent(self, event):
if event.type() == QEvent.PaletteChange:
self.__updateScenePalette()
self.resetCachedContent()
super().changeEvent(event)
def __updateScenePalette(self):
scene = self.scene()
if scene is not None:
scene.setPalette(self.palette())
class GraphicsView(pg.GraphicsView):
"""
A pyqtgraph.GraphicsView with better QPalette integration.
A default constructed plot will respect and adapt to the current palette
"""
def __init__(self, *args, background=None, **kwargs):
super().__init__(*args, background=background, **kwargs)
if background is None:
# Revert the pg.PlotWidget's modifications, use default
# for QGraphicsView
self.setBackgroundRole(QPalette.Base)
# Reset changes to the palette (undo changes from pg.GraphicsView)
self.setPalette(QPalette())
self.__updateScenePalette()
def setScene(self, scene):
super().setScene(scene)
self.__updateScenePalette()
def changeEvent(self, event):
if event.type() == QEvent.PaletteChange:
self.__updateScenePalette()
self.resetCachedContent()
super().changeEvent(event)
def __updateScenePalette(self):
scene = self.scene()
if scene is not None:
scene.setPalette(self.palette())
class PlotItem(pg.PlotItem):
"""
A pyqtgraph.PlotItem with better QPalette integration.
A default constructed plot will respect and adapt to the current palette
"""
def __init__(self, *args, **kwargs):
axisItems = kwargs.pop("axisItems", None)
if axisItems is None:
axisItems = {"left": AxisItem("left"), "bottom": AxisItem("bottom")}
super().__init__(*args, axisItems=axisItems, **kwargs)
|