From 982343627333b348d883099e34d4f782b7ab8df6 Mon Sep 17 00:00:00 2001 From: Luke Campagnola Date: Wed, 7 Dec 2016 17:34:12 -0800 Subject: [PATCH] Add spinbox option to limit height based on font size --- pyqtgraph/widgets/SpinBox.py | 25 +++++++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/pyqtgraph/widgets/SpinBox.py b/pyqtgraph/widgets/SpinBox.py index aafdb7d5..df7acfcd 100644 --- a/pyqtgraph/widgets/SpinBox.py +++ b/pyqtgraph/widgets/SpinBox.py @@ -61,7 +61,8 @@ class SpinBox(QtGui.QAbstractSpinBox): self.lastText = '' self.textValid = True ## If false, we draw a red border self.setMinimumWidth(0) - self.setMaximumHeight(20) + self._lastFontHeight = None + self.setSizePolicy(QtGui.QSizePolicy.Expanding, QtGui.QSizePolicy.Preferred) self.errorBox = ErrorBox(self.lineEdit()) @@ -88,7 +89,8 @@ class SpinBox(QtGui.QAbstractSpinBox): 'decimals': 6, 'format': asUnicode("{scaledValue:.{decimals}g}{suffixGap}{siPrefix}{suffix}"), - + + 'compactHeight': True, # manually remove extra margin outside of text } self.decOpts = ['step', 'minStep'] @@ -99,6 +101,7 @@ class SpinBox(QtGui.QAbstractSpinBox): self.setCorrectionMode(self.CorrectToPreviousValue) self.setKeyboardTracking(False) self.setOpts(**kwargs) + self._updateHeight() self.editingFinished.connect(self.editingFinishedEvent) self.proxy = SignalProxy(self.sigValueChanging, slot=self.delayedChange, delay=self.opts['delay']) @@ -149,6 +152,9 @@ class SpinBox(QtGui.QAbstractSpinBox): this feature has been disabled * *suffixGap* - a single space if a suffix is present, or an empty string otherwise. + compactHeight (bool) if True, then set the maximum height of the spinbox based on the + height of its font. This allows more compact packing on platforms with + excessive widget decoration. Default is True. ============== ======================================================================== """ #print opts @@ -518,6 +524,21 @@ class SpinBox(QtGui.QAbstractSpinBox): return self.setValue(val, delaySignal=False) ## allow text update so that values are reformatted pretty-like + def _updateHeight(self): + # SpinBox has very large margins on some platforms; this is a hack to remove those + # margins and allow more compact packing of controls. + if not self.opts['compactHeight']: + self.setMaximumHeight(1e6) + return + h = QtGui.QFontMetrics(self.font()).height() + if self._lastFontHeight != h: + self._lastFontHeight = h + self.setMaximumHeight(h) + + def paintEvent(self, ev): + self._updateHeight() + QtGui.QAbstractSpinBox.paintEvent(self, ev) + class ErrorBox(QtGui.QWidget): """Red outline to draw around lineedit when value is invalid.