Skip to content

Commit

Permalink
Merge pull request #6563 from ales-erjavec/fixes/owpredictions-column…
Browse files Browse the repository at this point in the history
…-size-hints

[FIX] Predictions: Fix column size hinting
  • Loading branch information
janezd committed Sep 8, 2023
2 parents 38ccb8b + 6bac595 commit 0aec15e
Showing 1 changed file with 25 additions and 6 deletions.
31 changes: 25 additions & 6 deletions Orange/widgets/evaluate/owpredictions.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
from contextlib import contextmanager
from functools import partial
from operator import itemgetter
from itertools import chain
from itertools import chain, product
from typing import Set, Sequence, Union, Optional, List, NamedTuple

import numpy
from AnyQt.QtWidgets import (
QTableView, QSplitter, QToolTip, QStyle, QApplication, QSizePolicy,
QPushButton, QStyledItemDelegate)
QPushButton, QStyledItemDelegate, QStyleOptionViewItem)
from AnyQt.QtGui import QPainter, QStandardItem, QPen, QColor, QBrush
from AnyQt.QtCore import (
Qt, QSize, QRect, QRectF, QPoint, QPointF, QLocale,
Expand Down Expand Up @@ -1004,11 +1004,12 @@ def paint(self, painter, option, index):
QStyle.PM_FocusFrameHMargin, option, option.widget) + 1
bottommargin = min(margin, 1)
rect = option.rect.adjusted(margin, margin, -margin, -bottommargin)
option.text = ""

textrect = style.subElementRect(
QStyle.SE_ItemViewItemText, option, option.widget)

# Are the margins included in the subElementRect?? -> No!
textrect = textrect.adjusted(margin, margin, -margin, -bottommargin)
textrect = textrect.adjusted(0, 0, 0, -bottommargin)
spacing = max(metrics.leading(), 1)

distheight = rect.height() - metrics.height() - spacing
Expand All @@ -1025,8 +1026,8 @@ def paint(self, painter, option, index):

textrect = textrect.adjusted(0, 0, 0, -distheight - spacing)
distrect = QRect(
textrect.bottomLeft() + QPoint(0, spacing),
QSize(rect.width(), distheight))
textrect.bottomLeft() + QPoint(margin, spacing),
QSize(textrect.width() - 2 * margin, distheight))
painter.setPen(QPen(Qt.lightGray, 0.3))
self.drawBar(painter, option, index, distrect)
painter.restore()
Expand Down Expand Up @@ -1068,6 +1069,24 @@ def __init__(
else:
self.tooltip = ""

def sizeHint(self, option: QStyleOptionViewItem, index: QModelIndex) -> QSize:
sh = super().sizeHint(option, index)
opt = QStyleOptionViewItem(option)
self.initStyleOption(opt, index)
widget = option.widget
style = widget.style() if widget is not None else QApplication.style()
# standin for {.2f} format to compute max possible text width
pp = [float(f"{x}.{x}{x}") for x in range(10)]
maxwidth = 0
nclass = max((len(self.class_values), *filter(None, self.shown_probabilities or ())))
for pp, cls in product(pp, self.class_values):
dist = [pp] * nclass
opt.text = self.fmt.format(dist=dist, value=cls)
csh = style.sizeFromContents(QStyle.CT_ItemViewItem, opt, QSize(), widget)
maxwidth = max(maxwidth, csh.width())
sh.setWidth(max(maxwidth, sh.width()))
return sh

# pylint: disable=unused-argument
def helpEvent(self, event, view, option, index):
QToolTip.showText(event.globalPos(), self.tooltip, view)
Expand Down

0 comments on commit 0aec15e

Please sign in to comment.