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: column size hint #1514

Merged
merged 2 commits into from
Aug 26, 2016
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
58 changes: 55 additions & 3 deletions Orange/widgets/evaluate/owpredictions.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,14 +119,14 @@ def __init__(self):
childrenCollapsible=False,
handleWidth=2,
)
self.dataview = QtGui.QTableView(
self.dataview = TableView(
verticalScrollBarPolicy=Qt.ScrollBarAlwaysOn,
horizontalScrollBarPolicy=Qt.ScrollBarAlwaysOn,
horizontalScrollMode=QtGui.QTableView.ScrollPerPixel,
selectionMode=QtGui.QTableView.NoSelection,
focusPolicy=Qt.StrongFocus
)
self.predictionsview = QtGui.QTableView(
self.predictionsview = TableView(
verticalScrollBarPolicy=Qt.ScrollBarAlwaysOff,
horizontalScrollBarPolicy=Qt.ScrollBarAlwaysOn,
horizontalScrollMode=QtGui.QTableView.ScrollPerPixel,
Expand Down Expand Up @@ -286,7 +286,13 @@ def _update_predictions_model(self):
predmodel.setDynamicSortFilter(True)
self.predictionsview.setItemDelegate(PredictionsItemDelegate())
self.predictionsview.setModel(predmodel)
self.predictionsview.horizontalHeader().setSortIndicatorShown(False)
hheader = self.predictionsview.horizontalHeader()
hheader.setSortIndicatorShown(False)
# SortFilterProxyModel is slow due to large abstraction overhead
# (every comparison triggers multiple `model.index(...)`,
# model.rowCount(...), `model.parent`, ... calls)
hheader.setClickable(predmodel.rowCount() < 20000)

predmodel.layoutChanged.connect(self._update_data_sort_order)
self._update_data_sort_order()
self.predictionsview.resizeColumnsToContents()
Expand Down Expand Up @@ -758,6 +764,52 @@ def headerData(self, section, orientation, role=Qt.DisplayRole):
PredictionsModel = _TableModel


class TableView(QtGui.QTableView):
MaxSizeHintSamples = 1000

def sizeHintForColumn(self, column):
"""
Reimplemented from `QTableView.sizeHintForColumn`

Note: This does not match the QTableView's implementation,
in particular size hints from editor/index widgets are not taken
into account.

Parameters
----------
column : int
"""
# This is probably not needed in Qt5?
if self.model() is None:
return -1

self.ensurePolished()
model = self.model()
vheader = self.verticalHeader()
top = vheader.visualIndexAt(0)
bottom = vheader.visualIndexAt(self.viewport().height())
if bottom < 0:
bottom = self.rowCount(column)

options = self.viewOptions()
options.widget = self

width = 0
sample_count = 0

for row in range(top, bottom):
if not vheader.isSectionHidden(vheader.logicalIndex(row)):
index = model.index(row, column)
size = self.itemDelegate(index).sizeHint(options, index)
width = max(size.width(), width)
sample_count += 1

if sample_count >= TableView.MaxSizeHintSamples:
break

return width + 1 if self.showGrid() else width


class TableSortProxyModel(QtGui.QSortFilterProxyModel):
def __init__(self, parent=None):
super().__init__(parent)
Expand Down