55from xml .sax .saxutils import escape
66from types import SimpleNamespace as namespace
77
8- if sys .version_info > (3 , 5 ):
9- from typing import Optional
8+ from typing import Optional
109
1110import numpy as np
1211import sklearn .metrics
1312
1413from AnyQt .QtWidgets import (
1514 QGraphicsScene , QGraphicsView , QGraphicsWidget , QGraphicsGridLayout ,
16- QGraphicsProxyWidget , QGraphicsItemGroup , QGraphicsSimpleTextItem ,
17- QGraphicsRectItem , QFrame , QSizePolicy
15+ QGraphicsItemGroup , QGraphicsSimpleTextItem , QGraphicsRectItem ,
16+ QSizePolicy , QStyleOptionGraphicsItem , QWidget , QWIDGETSIZE_MAX
1817)
19- from AnyQt .QtGui import QColor , QPen , QBrush , QPainter , QFontMetrics
18+ from AnyQt .QtGui import QColor , QPen , QBrush , QPainter , QFontMetrics , QPalette
2019from AnyQt .QtCore import Qt , QEvent , QRectF , QSizeF , QSize , QPointF
2120from AnyQt .QtCore import pyqtSignal as Signal
2221
@@ -327,20 +326,14 @@ def _replot(self):
327326
328327 self .scene .addItem (silplot )
329328 self ._update_annotations ()
330-
331- silplot .resize (silplot .effectiveSizeHint (Qt .PreferredSize ))
332329 silplot .selectionChanged .connect (self .commit )
333-
334- self .scene .setSceneRect (
335- QRectF (QPointF (0 , 0 ),
336- self ._silplot .effectiveSizeHint (Qt .PreferredSize )))
330+ silplot .layout ().activate ()
331+ self ._update_scene_rect ()
332+ silplot .geometryChanged .connect (self ._update_scene_rect )
337333
338334 def _update_bar_size (self ):
339335 if self ._silplot is not None :
340336 self ._set_bar_height ()
341- self .scene .setSceneRect (
342- QRectF (QPointF (0 , 0 ),
343- self ._silplot .effectiveSizeHint (Qt .PreferredSize )))
344337
345338 def _update_annotations (self ):
346339 if 0 < self .annotation_var_idx < len (self .annotation_var_model ):
@@ -361,6 +354,9 @@ def _update_annotations(self):
361354 else :
362355 self ._silplot .setRowNames (None )
363356
357+ def _update_scene_rect (self ):
358+ self .scene .setSceneRect (self ._silplot .geometry ())
359+
364360 def commit (self ):
365361 """
366362 Commit/send the current selection to the output.
@@ -542,6 +538,7 @@ def setRowNamesVisible(self, visible):
542538 self .__rowNamesVisible = visible
543539 for item in self .__textItems ():
544540 item .setVisible (visible )
541+ self .updateGeometry ()
545542
546543 def rowNamesVisible (self ):
547544 return self .__rowNamesVisible
@@ -608,10 +605,7 @@ def __setup(self):
608605 self .layout ().addItem (silhouettegroup , i + 1 , 2 )
609606
610607 if group .label :
611- line = QFrame (frameShape = QFrame .VLine )
612- proxy = QGraphicsProxyWidget (self )
613- proxy .setWidget (line )
614- self .layout ().addItem (proxy , i + 1 , 1 )
608+ self .layout ().addItem (Line (orientation = Qt .Vertical ), i + 1 , 1 )
615609 label = QGraphicsSimpleTextItem (self )
616610 label .setText ("{} ({})" .format (escape (group .label ),
617611 len (group .scores )))
@@ -893,6 +887,75 @@ def selection(self):
893887 return np .asarray (self .__selection , dtype = int )
894888
895889
890+ class Line (QGraphicsWidget ):
891+ """
892+ A line separator graphics widget
893+ """
894+ def __init__ (self , parent = None , orientation = Qt .Horizontal , ** kwargs ):
895+ sizePolicy = kwargs .pop ("sizePolicy" , None )
896+ super ().__init__ (None , ** kwargs )
897+ self .__orientation = Qt .Horizontal
898+ if sizePolicy is None :
899+ sizePolicy = QSizePolicy (QSizePolicy .Expanding , QSizePolicy .Fixed )
900+ sizePolicy .setControlType (QSizePolicy .Frame )
901+ self .setSizePolicy (sizePolicy )
902+ else :
903+ self .setSizePolicy (sizePolicy )
904+
905+ self .setOrientation (orientation )
906+
907+ if parent is not None :
908+ self .setParentItem (parent )
909+
910+ def setOrientation (self , orientation ):
911+ if self .__orientation != orientation :
912+ self .__orientation = orientation
913+ sp = self .sizePolicy ()
914+ if orientation == Qt .Vertical :
915+ sp .setVerticalPolicy (QSizePolicy .Expanding )
916+ sp .setHorizontalPolicy (QSizePolicy .Fixed )
917+ else :
918+ sp .setVerticalPolicy (QSizePolicy .Fixed )
919+ sp .setHorizontalPolicy (QSizePolicy .Expanding )
920+ self .setSizePolicy (sp )
921+ self .updateGeometry ()
922+
923+ def sizeHint (self , which , constraint = QRectF ()):
924+ # type: (Qt.SizeHint, QSizeF) -> QSizeF
925+ pw = 1.
926+ sh = QSizeF ()
927+ if which == Qt .MinimumSize :
928+ sh = QSizeF (pw , pw )
929+ elif which == Qt .PreferredSize :
930+ sh = QSizeF (pw , 30. )
931+ elif which == Qt .MaximumSize :
932+ sh = QSizeF (pw , QWIDGETSIZE_MAX )
933+
934+ if self .__orientation == Qt .Horizontal :
935+ sh = sh .transposed ()
936+ return sh
937+
938+ def paint (self , painter , option , widget = None ):
939+ # type: (QPainter, QStyleOptionGraphicsItem, Optional[QWidget]) -> None
940+ palette = option .palette # type: QPalette
941+ role = QPalette .WindowText
942+ if widget is not None :
943+ role = widget .foregroundRole ()
944+ color = palette .color (role )
945+ painter .setPen (QPen (color , 1 ))
946+ rect = self .contentsRect ()
947+ center = rect .center ()
948+ if self .__orientation == Qt .Vertical :
949+ p1 = QPointF (center .x (), rect .top ())
950+ p2 = QPointF (center .x (), rect .bottom ())
951+ elif self .__orientation == Qt .Horizontal :
952+ p1 = QPointF (rect .left (), center .y ())
953+ p2 = QPointF (rect .right (), center .y ())
954+ else :
955+ assert False
956+ painter .drawLine (p1 , p2 )
957+
958+
896959class BarPlotItem (QGraphicsWidget ):
897960 def __init__ (self , parent = None , ** kwargs ):
898961 super ().__init__ (parent , ** kwargs )
0 commit comments