def paintEvent(self, event): painter = QtGui.QPainter(self) fm = painter.fontMetrics() # Note: if anti-aliasing is enabled here, then coordinates need to be aligned to half-pixels # to get true pixel-aligned lines. Without anti-aliasing, integer coordinates are enough. # painter.setRenderHint(QtGui.QPainter.Antialiasing) # color bar by = self.spacingBorders()[0] for y in range(0, self.height() - 2 * by): yNorm = float(y) / (self.height() - 2 * by) color = self.colors[int(yNorm * (len(self.colors) - 1))] rect = QtCore.QRect(self.borderOffset, self.height() - by - y, self.colorBarWidth, 1) painter.fillRect(rect, color) # base line xb = self.borderOffset + self.colorBarWidth + self.barSpacing y0 = self.coordinateTransform.toScreen( self.coordinateTransform.coord_min) y1 = self.coordinateTransform.toScreen( self.coordinateTransform.coord_max) painter.drawLine(xb, y0, xb, y1) # tick start xt = xb + self.tickLength xtm = xb + self.tickLength / 2 # label start ls = xt + self.labelSpacing lh = fm.height() # label precision majorTicks = self.scaleDivision.majorTicks() if len(majorTicks) < 2: interval = 0 else: interval = majorTicks[1] - majorTicks[0] precision = numberPrecision(interval) digits = max(0, int(-precision)) for tick in self.scaleDivision.majorTicks(): # for vertical scale we invert the coordinates y = self.height() - self.coordinateTransform.toScreen(tick) painter.drawLine(xt, y, xb, y) tick_string = '{0:.{1}f}'.format(tick, digits) painter.drawText(ls, y + lh / 2 - 2, tick_string) for tick in self.scaleDivision.minorTicks(): # for vertical scale we invert the coordinates y = self.height() - self.coordinateTransform.toScreen(tick) painter.drawLine(xtm, y, xb, y)
def paintEvent(self, event): painter = QtGui.QPainter(self) fm = painter.fontMetrics() # Note: if anti-aliasing is enabled here, then coordinates need to be aligned to half-pixels # to get true pixel-aligned lines. Without anti-aliasing, integer coordinates are enough. # painter.setRenderHint(QtGui.QPainter.Antialiasing) # base line #xb = self.width() - self.borderOffset #y0 = self.coordinateTransform.toScreen(self.coordinateTransform.coord_min) #y1 = self.coordinateTransform.toScreen(self.coordinateTransform.coord_max) #painter.drawLine(1, y0, 1, y1) # tick start xt = self.tickLength xtm = self.tickLength * 2 / 3 # label end #le = xt - self.labelSpacing lh = fm.height() # label precision majorTicks = self.scaleDivision.majorTicks() if len(majorTicks) < 2: interval = 0 else: interval = majorTicks[1] - majorTicks[0] precision = numberPrecision(interval) digits = max(0, int(-precision)) painter.setPen(QtGui.QPen(QtCore.Qt.white, 3)) for tick in self.scaleDivision.majorTicks(): # for vertical scale we invert the coordinates y = self.height() - self.coordinateTransform.toScreen(tick) painter.drawLine(0, y, xt, y) tick_string = self.tickFormatter(tick, digits) #painter.drawText(le - fm.width(tick_string), y + lh / 2 - 2, tick_string) painter.drawText(xt + self.labelSpacing, y + lh / 2 - 2, " " + tick_string) for tick in self.scaleDivision.minorTicks(): # for vertical scale we invert the coordinates y = self.height() - self.coordinateTransform.toScreen(tick) try: if len(tick.Name) > 5: painter.setPen(QtGui.QPen(QtCore.Qt.black, 3)) else: painter.setPen(QtGui.QPen(QtCore.Qt.white, 3)) except: painter.setPen(QtGui.QPen(QtCore.Qt.white, 1)) painter.drawLine(0, y, xtm, y)
def paintEvent(self, event): painter = QtGui.QPainter(self) fm = painter.fontMetrics() # Note: if anti-aliasing is enabled here, then coordinates need to be aligned to half-pixels # to get true pixel-aligned lines. Without anti-aliasing, integer coordinates are enough. # painter.setRenderHint(QtGui.QPainter.Antialiasing) # color bar by = self.spacingBorders()[0] for y in range(0, self.height() - 2 * by): yNorm = float(y) / (self.height() - 2 * by) color = self.colors[int(yNorm * (len(self.colors) - 1))] rect = QtCore.QRect(self.borderOffset, self.height() - by - y, self.colorBarWidth, 1) painter.fillRect(rect, color) # base line xb = self.borderOffset + self.colorBarWidth + self.barSpacing y0 = self.coordinateTransform.toScreen(self.coordinateTransform.coord_min) y1 = self.coordinateTransform.toScreen(self.coordinateTransform.coord_max) painter.drawLine(xb, y0, xb, y1) # tick start xt = xb + self.tickLength xtm = xb + self.tickLength / 2 # label start ls = xt + self.labelSpacing lh = fm.height() # label precision majorTicks = self.scaleDivision.majorTicks() if len(majorTicks) < 2: interval = 0 else: interval = majorTicks[1] - majorTicks[0] precision = numberPrecision(interval) digits = max(0, int(-precision)) for tick in self.scaleDivision.majorTicks(): # for vertical scale we invert the coordinates y = self.height() - self.coordinateTransform.toScreen(tick) painter.drawLine(xt, y, xb, y) tick_string = '{0:.{1}f}'.format(tick, digits) painter.drawText(ls, y + lh / 2 - 2, tick_string) for tick in self.scaleDivision.minorTicks(): # for vertical scale we invert the coordinates y = self.height() - self.coordinateTransform.toScreen(tick) painter.drawLine(xtm, y, xb, y)
def sizeHint(self): # for vertical scale bar majorTicks = self.scaleDivision.majorTicks() fm = QtGui.QFontMetrics(self.font()) # label precision if len(majorTicks) < 2: maxLabelWidth = 0 else: interval = majorTicks[1] - majorTicks[0] prec = numberPrecision(interval) digits = max(0, int(-prec)) maxLabelWidth = max([fm.width(self.tickFormatter(tick, digits)) for tick in majorTicks]) return QtCore.QSize(maxLabelWidth + self.tickLength + self.borderOffset + self.labelSpacing, 10)
def paintEvent(self, event): painter = QtGui.QPainter(self) fm = painter.fontMetrics() # Note: if anti-aliasing is enabled here, then coordinates need to be aligned to half-pixels # to get true pixel-aligned lines. Without anti-aliasing, integer coordinates are enough. # painter.setRenderHint(QtGui.QPainter.Antialiasing) # base line yb = self.borderOffset x0 = self.coordinateTransform.toScreen( self.coordinateTransform.coord_min) x1 = self.coordinateTransform.toScreen( self.coordinateTransform.coord_max) painter.drawLine(x0, yb, x1, yb) # tick start yt = yb + self.tickLength ytm = yb + self.tickLength / 2 # label end le = yt + self.labelSpacing # label precision majorTicks = self.scaleDivision.majorTicks() if len(majorTicks) < 2: interval = 0 else: interval = majorTicks[1] - majorTicks[0] precision = numberPrecision(interval) digits = max(0, int(-precision)) for tick in self.scaleDivision.majorTicks(): # for vertical scale we invert the coordinates x = self.coordinateTransform.toScreen(tick) painter.drawLine(x, yt, x, yb) tick_string = '{0:.{1}f}'.format(tick, digits) painter.drawText(x - fm.width(tick_string) / 2, le + fm.height(), tick_string) for tick in self.scaleDivision.minorTicks(): # for vertical scale we invert the coordinates x = self.coordinateTransform.toScreen(tick) painter.drawLine(x, ytm, x, yb)
def paintEvent(self, event): painter = QtGui.QPainter(self) fm = painter.fontMetrics() # Note: if anti-aliasing is enabled here, then coordinates need to be aligned to half-pixels # to get true pixel-aligned lines. Without anti-aliasing, integer coordinates are enough. # painter.setRenderHint(QtGui.QPainter.Antialiasing) # base line xb = self.width() - self.borderOffset y0 = self.coordinateTransform.toScreen(self.coordinateTransform.coord_min) y1 = self.coordinateTransform.toScreen(self.coordinateTransform.coord_max) painter.drawLine(xb, y0, xb, y1) # tick start xt = xb - self.tickLength xtm = xb - self.tickLength / 2 # label end le = xt - self.labelSpacing lh = fm.height() # label precision majorTicks = self.scaleDivision.majorTicks() if len(majorTicks) < 2: interval = 0 else: interval = majorTicks[1] - majorTicks[0] precision = numberPrecision(interval) digits = max(0, int(-precision)) for tick in self.scaleDivision.majorTicks(): # for vertical scale we invert the coordinates y = self.height() - self.coordinateTransform.toScreen(tick) painter.drawLine(xt, y, xb, y) tick_string = self.tickFormatter(tick, digits) painter.drawText(le - fm.width(tick_string), y + lh / 2 - 2, tick_string) for tick in self.scaleDivision.minorTicks(): # for vertical scale we invert the coordinates y = self.height() - self.coordinateTransform.toScreen(tick) painter.drawLine(xtm, y, xb, y)
def paintEvent(self, event): painter = QtGui.QPainter(self) fm = painter.fontMetrics() # Note: if anti-aliasing is enabled here, then coordinates need to be aligned to half-pixels # to get true pixel-aligned lines. Without anti-aliasing, integer coordinates are enough. # painter.setRenderHint(QtGui.QPainter.Antialiasing) # base line yb = self.borderOffset x0 = self.coordinateTransform.toScreen(self.coordinateTransform.coord_min) x1 = self.coordinateTransform.toScreen(self.coordinateTransform.coord_max) painter.drawLine(x0, yb, x1, yb) # tick start yt = yb + self.tickLength ytm = yb + self.tickLength / 2 # label end le = yt + self.labelSpacing # label precision majorTicks = self.scaleDivision.majorTicks() if len(majorTicks) < 2: interval = 0 else: interval = majorTicks[1] - majorTicks[0] precision = numberPrecision(interval) digits = max(0, int(-precision)) for tick in self.scaleDivision.majorTicks(): # for vertical scale we invert the coordinates x = self.coordinateTransform.toScreen(tick) painter.drawLine(x, yt, x, yb) tick_string = '{0:.{1}f}'.format(tick, digits) painter.drawText(x - fm.width(tick_string) / 2, le + fm.height(), tick_string) for tick in self.scaleDivision.minorTicks(): # for vertical scale we invert the coordinates x = self.coordinateTransform.toScreen(tick) painter.drawLine(x, ytm, x, yb)
def sizeHint(self): # for vertical scale bar majorTicks = self.scaleDivision.majorTicks() fm = QtGui.QFontMetrics(self.font()) # label precision if len(majorTicks) < 2: maxLabelWidth = 0 else: interval = majorTicks[1] - majorTicks[0] prec = numberPrecision(interval) digits = max(0, int(-prec)) maxLabelWidth = max([ fm.width('{0:.{1}f}'.format(tick, digits)) for tick in majorTicks ]) return QtCore.QSize( self.borderOffset + self.colorBarWidth + self.barSpacing + self.tickLength + self.labelSpacing + maxLabelWidth, 10)