Add spinbox option to limit height based on font size

This commit is contained in:
Luke Campagnola 2016-12-07 17:34:12 -08:00
parent 65e9052580
commit 9823436273

View File

@ -61,7 +61,8 @@ class SpinBox(QtGui.QAbstractSpinBox):
self.lastText = '' self.lastText = ''
self.textValid = True ## If false, we draw a red border self.textValid = True ## If false, we draw a red border
self.setMinimumWidth(0) self.setMinimumWidth(0)
self.setMaximumHeight(20) self._lastFontHeight = None
self.setSizePolicy(QtGui.QSizePolicy.Expanding, QtGui.QSizePolicy.Preferred) self.setSizePolicy(QtGui.QSizePolicy.Expanding, QtGui.QSizePolicy.Preferred)
self.errorBox = ErrorBox(self.lineEdit()) self.errorBox = ErrorBox(self.lineEdit())
@ -89,6 +90,7 @@ class SpinBox(QtGui.QAbstractSpinBox):
'format': asUnicode("{scaledValue:.{decimals}g}{suffixGap}{siPrefix}{suffix}"), 'format': asUnicode("{scaledValue:.{decimals}g}{suffixGap}{siPrefix}{suffix}"),
'compactHeight': True, # manually remove extra margin outside of text
} }
self.decOpts = ['step', 'minStep'] self.decOpts = ['step', 'minStep']
@ -99,6 +101,7 @@ class SpinBox(QtGui.QAbstractSpinBox):
self.setCorrectionMode(self.CorrectToPreviousValue) self.setCorrectionMode(self.CorrectToPreviousValue)
self.setKeyboardTracking(False) self.setKeyboardTracking(False)
self.setOpts(**kwargs) self.setOpts(**kwargs)
self._updateHeight()
self.editingFinished.connect(self.editingFinishedEvent) self.editingFinished.connect(self.editingFinishedEvent)
self.proxy = SignalProxy(self.sigValueChanging, slot=self.delayedChange, delay=self.opts['delay']) 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 this feature has been disabled
* *suffixGap* - a single space if a suffix is present, or an empty * *suffixGap* - a single space if a suffix is present, or an empty
string otherwise. 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 #print opts
@ -518,6 +524,21 @@ class SpinBox(QtGui.QAbstractSpinBox):
return return
self.setValue(val, delaySignal=False) ## allow text update so that values are reformatted pretty-like 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): class ErrorBox(QtGui.QWidget):
"""Red outline to draw around lineedit when value is invalid. """Red outline to draw around lineedit when value is invalid.