Skip to content

Commit

Permalink
heatmap: Adapt to palette change
Browse files Browse the repository at this point in the history
  • Loading branch information
ales-erjavec committed Jan 10, 2022
1 parent d6a0c77 commit 3eeba5e
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 14 deletions.
41 changes: 28 additions & 13 deletions Orange/widgets/visualize/utils/heatmap.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@
QGraphicsLayoutItem
)

import pyqtgraph as pg

from Orange.clustering import hierarchical
from Orange.clustering.hierarchical import Tree
from Orange.widgets.utils import apply_all
Expand All @@ -32,6 +30,7 @@

from Orange.widgets.utils.graphicstextlist import TextListWidget
from Orange.widgets.utils.dendrogram import DendrogramWidget
from Orange.widgets.visualize.utils.plotutils import AxisItem


def leaf_indices(tree: Tree) -> Sequence[int]:
Expand Down Expand Up @@ -936,6 +935,11 @@ def setShowAverages(self, visible):
item.setVisible(visible)
item.setPreferredWidth(0 if not visible else 10)

def changeEvent(self, event):
if event.type() == QEvent.PaletteChange:
self.__update_palette()
super().changeEvent(event)

def event(self, event):
# type: (QEvent) -> bool
rval = super().event(event)
Expand Down Expand Up @@ -992,6 +996,12 @@ def __select_by_cluster(self, item, dendrogramindex):
node.value.first, node.value.last - 1, hm,
clear=clear, remove=remove, append=append)

def __update_palette(self):
for item in layout_items_recursive(self.layout()):
if isinstance(item, SimpleLayoutItem) \
and isinstance(item.item, QGraphicsSimpleTextItem):
item.item.setBrush(self.palette().text())

def heatmapAtPos(self, pos: QPointF) -> Optional['GraphicsHeatmapWidget']:
for hw in chain.from_iterable(self.heatmap_widget_grid):
if hw.contains(hw.mapFromItem(self, pos)):
Expand Down Expand Up @@ -1234,7 +1244,7 @@ def remove_item(item: QGraphicsItem) -> None:
item.setParentItem(None)


class _GradientLegendAxisItem(pg.AxisItem):
class _GradientLegendAxisItem(AxisItem):
def boundingRect(self):
br = super().boundingRect()
if self.orientation in ["top", "bottom"]:
Expand Down Expand Up @@ -1335,12 +1345,6 @@ def __update(self):

self.updateGeometry()

def changeEvent(self, event: QEvent) -> None:
if event.type() == QEvent.PaletteChange:
pen = QPen(self.palette().color(QPalette.Text))
self.__axis.setPen(pen)
super().changeEvent(event)


class CategoricalColorLegend(QGraphicsWidget):
def __init__(
Expand Down Expand Up @@ -1431,20 +1435,31 @@ def legend_item_pair(color: QColor, size: float, text: str):
def changeEvent(self, event: QEvent) -> None:
if event.type() == QEvent.FontChange:
self._updateFont(self.font())
elif event.type() == QEvent.PaletteChange:
self._updatePalette()
super().changeEvent(event)

def _updateFont(self, font):
w = QFontMetrics(font).horizontalAdvance("X")
for item in filter(
lambda item: isinstance(item, SimpleLayoutItem),
layout_items_recursive(self.__layout)
):
for item in self.__layoutItems():
if isinstance(item.item, QGraphicsSimpleTextItem):
item.item.setFont(font)
elif isinstance(item.item, QGraphicsRectItem):
item.item.setRect(QRectF(0, 0, w, w))
item.updateGeometry()

def _updatePalette(self):
palette = self.palette()
for item in self.__layoutItems():
if isinstance(item.item, QGraphicsSimpleTextItem):
item.item.setBrush(palette.brush(QPalette.Text))

def __layoutItems(self):
return filter(
lambda item: isinstance(item, SimpleLayoutItem),
layout_items_recursive(self.__layout)
)


def layout_items(layout: QGraphicsLayout) -> Iterable[QGraphicsLayoutItem]:
for item in map(layout.itemAt, range(layout.count())):
Expand Down
10 changes: 9 additions & 1 deletion Orange/widgets/visualize/utils/tests/test_heatmap.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import numpy as np

from AnyQt.QtCore import Qt, QPoint
from AnyQt.QtGui import QFont
from AnyQt.QtGui import QFont, QPalette
from AnyQt.QtTest import QTest, QSignalSpy
from AnyQt.QtWidgets import QGraphicsScene, QGraphicsView

Expand Down Expand Up @@ -93,6 +93,14 @@ def test_widget(self):
w.headerGeometry()
w.footerGeometry()

# Trigger the change events.
p = QPalette()
p.setColor(QPalette.All, QPalette.Text, Qt.red)
w.setPalette(p)
f = QFont()
f.setPointSizeF(19.5)
w.setFont(f)

def test_widget_annotations(self):
w = HeatmapGridWidget()
self.scene.addItem(w)
Expand Down

0 comments on commit 3eeba5e

Please sign in to comment.