Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FIX] Predictions: Fix column size hinting #6563

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading