2626from Orange .widgets .utils import apply_all
2727from Orange .widgets .utils .colorpalettes import DefaultContinuousPalette
2828from Orange .widgets .utils .graphicslayoutitem import SimpleLayoutItem , scaled
29+ from Orange .widgets .utils .graphicsflowlayout import GraphicsFlowLayout
2930from Orange .widgets .utils .graphicspixmapwidget import GraphicsPixmapWidget
3031from Orange .widgets .utils .image import qimage_from_array
3132
@@ -1339,8 +1340,11 @@ def __init__(
13391340 self .__colormap = colormap
13401341 self .__title = title
13411342 self .__names = colormap .names
1342- self .__layout = QGraphicsGridLayout ()
1343- self .__layout .setSpacing (2 )
1343+ self .__layout = QGraphicsLinearLayout (Qt .Vertical )
1344+ self .__flow = GraphicsFlowLayout ()
1345+ self .__layout .addItem (self .__flow )
1346+ self .__flow .setHorizontalSpacing (4 )
1347+ self .__flow .setVerticalSpacing (4 )
13441348 self .__orientation = orientation
13451349 kwargs .setdefault (
13461350 "sizePolicy" , QSizePolicy (QSizePolicy .Maximum , QSizePolicy .Maximum )
@@ -1361,10 +1365,16 @@ def orientation(self):
13611365 return self .__orientation
13621366
13631367 def _clear (self ):
1364- items = reversed (list (layout_items (self .__layout )))
1368+ items = list (layout_items (self .__flow ))
1369+ layout_clear (self .__flow )
13651370 for item in items :
1366- self .__layout .removeItem (item )
1371+ if isinstance (item , SimpleLayoutItem ):
1372+ remove_item (item .item )
1373+ # remove 'title' item if present
1374+ items = [item for item in layout_items (self .__layout )
1375+ if item is not self .__flow ]
13671376 for item in items :
1377+ self .__layout .removeItem (item )
13681378 if isinstance (item , SimpleLayoutItem ):
13691379 remove_item (item .item )
13701380
@@ -1374,53 +1384,39 @@ def _setup(self):
13741384 names = self .__colormap .names
13751385 title = self .__title
13761386 layout = self .__layout
1377- assert layout .count () == 0
1387+ flow = self .__flow
1388+ assert flow .count () == 0
13781389 font = self .font ()
13791390 fm = QFontMetrics (font )
13801391 size = fm .width ("X" )
1381- start = 0
1392+ headeritem = None
13821393 if title :
1383- start = 1
1384- item = QGraphicsSimpleTextItem (title )
1385- item .setFont (font )
13861394 headeritem = QGraphicsSimpleTextItem (title )
13871395 headeritem .setFont (font )
1388- else :
1389- headeritem = None
13901396
1391- items = []
1392- for i , (color , label ) in enumerate (zip (colors , names ), start = start ):
1393- colitem = QGraphicsRectItem (0 , 0 , size , size )
1394- colitem .setBrush (QColor (* color ))
1397+ def centered (item ):
1398+ return SimpleLayoutItem (item , anchor = (0.5 , 0.5 ), anchorItem = (0.5 , 0.5 ))
1399+
1400+ def legend_item_pair (color : QColor , size : float , text : str ):
1401+ coloritem = QGraphicsRectItem (0 , 0 , size , size )
1402+ coloritem .setBrush (color )
13951403 textitem = QGraphicsSimpleTextItem ()
13961404 textitem .setFont (font )
1397- textitem .setText (label )
1398- items .append ((colitem , textitem ))
1405+ textitem .setText (text )
1406+ layout = QGraphicsLinearLayout (Qt .Horizontal )
1407+ layout .setSpacing (2 )
1408+ layout .addItem (centered (coloritem ))
1409+ layout .addItem (SimpleLayoutItem (textitem ))
1410+ return coloritem , textitem , layout
13991411
1400- def centered ( item ):
1401- return SimpleLayoutItem ( item , anchor = ( 0.5 , 0.5 ), anchorItem = ( 0.5 , 0.5 ))
1412+ items = [ legend_item_pair ( QColor ( * color ), size , name )
1413+ for color , name in zip ( colors , names )]
14021414
1403- def addrowspan ( item ) :
1404- layout .addItem (centered ( item ), layout . rowCount (), 0 , 1 , 2 )
1415+ for sym , label , pair_layout in items :
1416+ flow .addItem (pair_layout )
14051417
1406- def addrow (symbol , label ):
1407- row = layout .rowCount ()
1408- layout .addItem (centered (symbol ), row , 0 )
1409- layout .addItem (
1410- SimpleLayoutItem (label ), row , 1 ,
1411- alignment = Qt .AlignVCenter | Qt .AlignLeft
1412- )
1413- if self .__orientation == Qt .Vertical :
1414- if headeritem :
1415- addrowspan (headeritem )
1416- apply_all (items , lambda el : addrow (* el ))
1417- else :
1418- for sym , label in items :
1419- layout .addItem (centered (sym ), 1 , layout .columnCount ())
1420- layout .addItem (SimpleLayoutItem (label ), 1 , layout .columnCount ())
1421- if headeritem :
1422- layout .addItem (
1423- centered (headeritem ), 0 , 0 , 1 , layout .columnCount ())
1418+ if headeritem :
1419+ layout .insertItem (0 , centered (headeritem ))
14241420
14251421 def changeEvent (self , event : QEvent ) -> None :
14261422 if event .type () == QEvent .FontChange :
@@ -1431,7 +1427,7 @@ def _updateFont(self, font):
14311427 w = QFontMetrics (font ).width ("X" )
14321428 for item in filter (
14331429 lambda item : isinstance (item , SimpleLayoutItem ),
1434- layout_items (self .__layout )
1430+ chain ( layout_items (self .__layout ), layout_items ( self . __flow ) )
14351431 ):
14361432 if isinstance (item .item , QGraphicsSimpleTextItem ):
14371433 item .item .setFont (font )
0 commit comments