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] Hierarchical Clustering: Fix size constraints #2796

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
71 changes: 40 additions & 31 deletions Orange/widgets/unsupervised/owhierarchicalclustering.py
Original file line number Diff line number Diff line change
Expand Up @@ -674,11 +674,11 @@ def sizeHint(self, which, constraint=QSizeF()):
if self._root and which == Qt.PreferredSize:
nleaves = len([node for node in self._items.keys()
if not node.branches])

base = max(10, min(spacing * 16, 250))
if self.orientation in [self.Left, self.Right]:
return QSizeF(250, spacing * nleaves + mleft + mright)
return QSizeF(base, spacing * nleaves + mleft + mright)
else:
return QSizeF(spacing * nleaves + mtop + mbottom, 250)
return QSizeF(spacing * nleaves + mtop + mbottom, base)

elif which == Qt.MinimumSize:
return QSizeF(mleft + mright + 10, mtop + mbottom + 10)
Expand Down Expand Up @@ -885,7 +885,7 @@ def __init__(self):
self.zoom_slider = gui.hSlider(
self.controlArea, self, "zoom_factor", box="Zoom",
minValue=-6, maxValue=3, step=1, ticks=True, createLabel=False,
callback=self.__zoom_factor_changed)
callback=self.__update_font_scale)

zoom_in = QAction(
"Zoom in", self, shortcut=QKeySequence.ZoomIn,
Expand Down Expand Up @@ -976,7 +976,7 @@ def axis_view(orientation):
self.dendrogram.selectionEdited.connect(self._selection_edited)

self.labels = GraphicsSimpleTextList()
self.labels.setSizePolicy(QSizePolicy.Preferred, QSizePolicy.Preferred)
self.labels.setSizePolicy(QSizePolicy.Minimum, QSizePolicy.Preferred)
self.labels.setAlignment(Qt.AlignLeft)
self.labels.setMaximumWidth(200)
self.labels.layout().setSpacing(0)
Expand All @@ -1003,6 +1003,7 @@ def axis_view(orientation):
self.top_axis.line.valueChanged.connect(self._axis_slider_changed)
self.dendrogram.geometryChanged.connect(self._dendrogram_geom_changed)
self._set_cut_line_visible(self.selection_method == 1)
self.__update_font_scale()

@Inputs.distances
def set_distances(self, matrix):
Expand Down Expand Up @@ -1254,10 +1255,21 @@ def sizeHint(self):

def eventFilter(self, obj, event):
if obj is self.view.viewport() and event.type() == QEvent.Resize:
width = self.view.viewport().width() - 2
self._main_graphics.setMaximumWidth(width)
self._main_graphics.setMinimumWidth(width)
self._main_graphics.layout().activate()
# NOTE: not using viewport.width(), due to 'transient' scroll bars
# (macOS). Viewport covers the whole view, but QGraphicsView still
# scrolls left, right with scroll bar extent (other
# QAbstractScrollArea widgets behave as expected).
w_frame = self.view.frameWidth()
margin = self.view.viewportMargins()
w_scroll = self.view.verticalScrollBar().width()
width = (self.view.width() - w_frame * 2 -
margin.left() - margin.right() - w_scroll)
# layout with new width constraint
self.__layout_main_graphics(width=width)
elif obj is self._main_graphics and \
event.type() == QEvent.LayoutRequest:
# layout preserving the width (vertical re layout)
self.__layout_main_graphics()
elif event.type() == QEvent.MouseButtonPress and \
(obj is self.top_axis_view.viewport() or
obj is self.bottom_axis_view.viewport()):
Expand All @@ -1268,10 +1280,6 @@ def eventFilter(self, obj, event):
self.top_axis.line.setValue(cut.x())
# update the line visibility, output, ...
self._selection_method_changed()
elif obj is self._main_graphics and \
event.type() == QEvent.LayoutRequest:
self.__update_size_constraints()

return super().eventFilter(obj, event)

def onDeleteWidget(self):
Expand Down Expand Up @@ -1319,16 +1327,16 @@ def _dendrogram_slider_changed(self, value):

def _set_slider_value(self, value, span):
with blocked(self.cut_line):
self.cut_line.setValue(value)
self.cut_line.setRange(0, span)
self.cut_line.setValue(value)

with blocked(self.top_axis.line):
self.top_axis.line.setValue(value)
self.top_axis.line.setRange(0, span)
self.top_axis.line.setValue(value)

with blocked(self.bottom_axis.line):
self.bottom_axis.line.setValue(value)
self.bottom_axis.line.setRange(0, span)
self.bottom_axis.line.setValue(value)

def set_cutoff_height(self, height):
self.cutoff_height = height
Expand Down Expand Up @@ -1385,34 +1393,35 @@ def clip(minval, maxval, val):
self.zoom_factor = clip(self.zoom_slider.minimum(),
self.zoom_slider.maximum(),
self.zoom_factor + 1)
self.__zoom_factor_changed()
self.__update_font_scale()

def __zoom_out(self):
def clip(minval, maxval, val):
return min(max(val, minval), maxval)
self.zoom_factor = clip(self.zoom_slider.minimum(),
self.zoom_slider.maximum(),
self.zoom_factor - 1)
self.__zoom_factor_changed()
self.__update_font_scale()

def __zoom_reset(self):
self.zoom_factor = 0
self.__zoom_factor_changed()

def __update_size_constraints(self):
size = self._main_graphics.size()
preferred = self._main_graphics.sizeHint(
Qt.PreferredSize, constraint=QSizeF(size.width(), -1))
self._main_graphics.resize(QSizeF(size.width(), preferred.height()))
self._main_graphics.layout().activate()

def __zoom_factor_changed(self):
self.__update_font_scale()

def __layout_main_graphics(self, width=-1):
if width < 0:
# Preserve current width.
width = self._main_graphics.size().width()
preferred = self._main_graphics.effectiveSizeHint(
Qt.PreferredSize, constraint=QSizeF(width, -1))
self._main_graphics.resize(QSizeF(width, preferred.height()))
mw = self._main_graphics.minimumWidth() + 4
self.view.setMinimumWidth(mw + self.view.verticalScrollBar().width())

def __update_font_scale(self):
font = self.scene.font()
factor = (1.25 ** self.zoom_factor)
font = qfont_scaled(font, factor)
self.labels.setFont(font)
self.dendrogram.setFont(font)
self.__update_size_constraints()
self._main_graphics.setFont(font)

def send_report(self):
annot = self.label_cb.currentText()
Expand Down