def set(self, U=None, vmin=None, vmax=None): # normalize U fm = QFontMetrics(self.font()) self.vmin = vmin if vmin is not None else ( np.min(U) if U is not None else 0.) self.vmax = vmax if vmax is not None else ( np.max(U) if U is not None else 1.) difference = abs(self.vmin - self.vmax) if difference == 0: precision = 3 else: precision = m.log( max(abs(self.vmin), abs(self.vmax)) / difference, 10) + 1 precision = int(min(max(precision, 3), 8)) self.vmin_str = format( ('{:.' + str(precision) + '}').format(self.vmin)) self.vmax_str = format( ('{:.' + str(precision) + '}').format(self.vmax)) self.vmin_width = fm.width(self.vmin_str) self.vmax_width = fm.width(self.vmax_str) self.text_height = fm.height() * 1.5 self.text_ascent = fm.ascent() * 1.5 self.text_descent = fm.descent() * 1.5 self.setMinimumSize( max(self.vmin_width, self.vmax_width) + 20, 300) self.update()
def minLabelDist(self, font): """ Determine the minimum distance between two labels, that is necessary that the texts don't overlap. :param QFont font: Font :return: The maximum width of a label .. seealso:: :py:meth:`getBorderDistHint()` """ if not self.hasComponent(QwtAbstractScaleDraw.Labels): return 0 ticks = self.scaleDiv().ticks(QwtScaleDiv.MajorTick) if not ticks: return 0 fm = QFontMetrics(font) vertical = self.orientation() == Qt.Vertical bRect1 = QRectF() bRect2 = self.labelRect(font, ticks[0]) if vertical: bRect2.setRect(-bRect2.bottom(), 0.0, bRect2.height(), bRect2.width()) maxDist = 0.0 for tick in ticks: bRect1 = bRect2 bRect2 = self.labelRect(font, tick) if vertical: bRect2.setRect(-bRect2.bottom(), 0.0, bRect2.height(), bRect2.width()) dist = fm.leading() if bRect1.right() > 0: dist += bRect1.right() if bRect2.left() < 0: dist += -bRect2.left() if dist > maxDist: maxDist = dist angle = qwtRadians(self.labelRotation()) if vertical: angle += math.pi / 2 sinA = math.sin(angle) if qFuzzyCompare(sinA + 1.0, 1.0): return math.ceil(maxDist) fmHeight = fm.ascent() - 2 labelDist = fmHeight / math.sin(angle) * math.cos(angle) if labelDist < 0: labelDist = -labelDist if labelDist > maxDist: labelDist = maxDist if labelDist < fmHeight: labelDist = fmHeight return math.ceil(labelDist)