Exemple #1
0
    def drawMarkings(self, painter):
        painter.save()
        painter.translate(self.width() / 2, self.height() / 2)
        scale = min((self.width() - self._margins) / 120.0,
                    (self.height() - self._margins) / 120.0)
        painter.scale(scale, scale)

        font = QFont(self.font())
        font.setPixelSize(10)
        metrics = QFontMetricsF(font)

        painter.setFont(font)
        painter.setPen(self.palette().color(QPalette.Shadow))

        i = 0
        while i < 360:
            if i % 45 == 0:
                painter.drawLine(0, -40, 0, -50)
                painter.drawText(-metrics.width(self._pointText[i]) / 2.0, -52,
                                 self._pointText[i])
            else:
                painter.drawLine(0, -45, 0, -50)
            painter.rotate(15)
            i += 15
        painter.restore()
	def __init__(self, leftFlow = 0, rightFlow = 0, maxFlow = 100, parent = None):
		super(YPipeWidget, self).__init__(parent)

		self.leftSpinBox = QSpinBox(self)
		self.leftSpinBox.setRange(0, maxFlow)
		self.leftSpinBox.setValue(leftFlow)
		self.leftSpinBox.setSuffix(" l/s")
		self.leftSpinBox.setAlignment(Qt.AlignRight|Qt.AlignVCenter)
		self.connect(self.leftSpinBox, SIGNAL("valueChanged(int)"), self.valueChanged)

		self.rightSpinBox = QSpinBox(self)
		self.rightSpinBox.setRange(0, maxFlow)
		self.rightSpinBox.setValue(rightFlow)
		self.rightSpinBox.setSuffix(" l/s")
		self.rightSpinBox.setAlignment(Qt.AlignRight|Qt.AlignVCenter)
		self.connect(self.rightSpinBox, SIGNAL("valueChanged(int)"), self.valueChanged)

		self.label = QLabel(self)
		self.label.setFrameStyle(QFrame.StyledPanel | QFrame.Sunken)
		self.label.setAlignment(Qt.AlignCenter)
		fm = QFontMetricsF(self.font())
		self.label.setMinimumWidth(fm.width(" 999 l/s "))

		self.setSizePolicy(QSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding))

		self.setMinimumSize(self.minimumSizeHint())
		self.valueChanged()
Exemple #3
0
    def plot(self, under, over):
    
        if not under:
            return
        
        ctx = PaintGL.instance().currentContext
        painter = ctx.painter
        plotm = PlotModule.instance()
        plot_width, plot_height = plotm.getPlotWindow()
        
        metrics = QFontMetricsF(QFont())

        painter.save()
        painter.setPen(QColor(0, 0, 160))
        painter.setBrush(QColor(255, 255, 255, 192))
        painter.setCompositionMode(painter.CompositionMode_SourceOver)

        for point, label in self.locations:
            ok, x, y = plotm.GeoToPhys(point.x(), point.y())
            y = plot_height - y
            if ok:
                rect = QRectF(x, y, metrics.width(label) + 8, metrics.height() + 8)
                painter.drawRect(rect)
                painter.drawText(rect, Qt.AlignCenter, label)

        painter.restore()
 def minimumSizeHint(self):
     font = QFont(self.font())
     font.setPointSize(font.pointSize() - 1)
     fm = QFontMetricsF(font)
     return QSize(fm.width(FractionSlider.WSTRING) *
                  self.__denominator,
                  (fm.height() * 4) + FractionSlider.YMARGIN)
Exemple #5
0
	def minimumSizeHint(self):
		"Pour être sûr que le texte du chrono ne soit pas tronqué"
		font = QFont(self.font())
		font.setPointSize(font.pointSize() - 1)
		fm = QFontMetricsF(font)
		l = fm.width(TracerChrono.TAILLE*2) # + 10.
		L = fm.height() + 2.
		return QSize(l, L)
 def _find_font(self):
     font = QFont()
     wanted_size = self._point_size
     font.setStyleStrategy(QFont.StyleStrategy(QFont.OpenGLCompatible | QFont.PreferAntialias))
     fm = QFontMetricsF(font)
     width = fm.width("888")
     ratio = 1.8*wanted_size/max(width, fm.ascent())
     self._font = font
     self._font_zoom = ratio
Exemple #7
0
 def resizeEvent(self, event=None):
     fm = QFontMetricsF(self.font())
     x = (self.width() - self.label.width()) / 2
     y = self.height() - (fm.height() * 1.5)
     self.label.move(x, y)
     y = self.height() / 60.0
     x = (self.width() / 4.0) - self.leftSpinBox.width()
     self.leftSpinBox.move(x, y)
     x = self.width() - (self.width() / 4.0)
     self.rightSpinBox.move(x, y)
 def paintEvent(self, event=None):
     font = QFont(self.font())
     font.setPointSize(font.pointSize() - 1)
     fm = QFontMetricsF(font)
     fracWidth = fm.width(FractionSlider.WSTRING)
     indent = fm.boundingRect("9").width() / 2.0
     if not X11:
         fracWidth *= 1.5
     span = self.width() - (FractionSlider.XMARGIN * 2)
     value = self.__numerator / float(self.__denominator)
     painter = QPainter(self)
     painter.setRenderHint(QPainter.Antialiasing)
     painter.setRenderHint(QPainter.TextAntialiasing)
     painter.setPen(self.palette().color(QPalette.Mid))
     painter.setBrush(self.palette().brush(
             QPalette.AlternateBase))
     painter.drawRect(self.rect())
     segColor = QColor(Qt.green).dark(120)
     segLineColor = segColor.dark()
     painter.setPen(segLineColor)
     painter.setBrush(segColor)
     painter.drawRect(FractionSlider.XMARGIN,
                      FractionSlider.YMARGIN, span, fm.height())
     textColor = self.palette().color(QPalette.Text)
     segWidth = span / self.__denominator
     segHeight = fm.height() * 2
     nRect = fm.boundingRect(FractionSlider.WSTRING)
     x = FractionSlider.XMARGIN
     yOffset = segHeight + fm.height()
     for i in range(self.__denominator + 1):
         painter.setPen(segLineColor)
         painter.drawLine(x, FractionSlider.YMARGIN, x, segHeight)
         painter.setPen(textColor)
         y = segHeight
         rect = QRectF(nRect)
         rect.moveCenter(QPointF(x, y + fm.height() / 2.0))
         painter.drawText(rect, Qt.AlignCenter,
                          QString.number(i))
         y = yOffset
         rect.moveCenter(QPointF(x, y + fm.height() / 2.0))
         painter.drawText(rect, Qt.AlignCenter,
                          QString.number(self.__denominator))
         painter.drawLine(QPointF(rect.left() + indent, y),
                          QPointF(rect.right() - indent, y))
         x += segWidth
     span = int(span)
     y = FractionSlider.YMARGIN - 0.5
     triangle = [QPointF(value * span, y),
                 QPointF((value * span) +
                         (2 * FractionSlider.XMARGIN), y),
                 QPointF((value * span) +
                         FractionSlider.XMARGIN, fm.height())]
     painter.setPen(Qt.yellow)
     painter.setBrush(Qt.darkYellow)
     painter.drawPolygon(QPolygonF(triangle))
Exemple #9
0
 def selectValues(self, length, is_vertical, painter = None):
     if painter is not None:
         metric = QFontMetricsF(self.font, painter.device())
     else:
         metric = QFontMetricsF(self.font)
     min_dist = 1
     if not is_vertical: # Find the maximum size of 2 figures
         test = [ str(i)*2 for i in range(10) ]
         for t in test:
             min_dist = max(min_dist, metric.boundingRect(t).width())
     return self._selectValues_direct(length, is_vertical, metric, min_dist)
Exemple #10
0
 def _update_margin_line(self, font):
     # Fix for older version of Qt which doens't has ForceIntegerMetrics
     if "ForceIntegerMetrics" in dir(QFont):
         self.document().defaultFont().setStyleStrategy(
             QFont.ForceIntegerMetrics)
     font_metrics = QFontMetricsF(self.document().defaultFont())
     if (font_metrics.width("#") * settings.MARGIN_LINE) == \
        (font_metrics.width(" ") * settings.MARGIN_LINE):
         self.pos_margin = font_metrics.width('#') * settings.MARGIN_LINE
     else:
         char_width = font_metrics.averageCharWidth()
         self.pos_margin = char_width * settings.MARGIN_LINE
Exemple #11
0
 def getLinkPointForParameter(self, paramIndex):
     offsetX = 25
     if isinstance(self.element, Algorithm) and self.element.paramsFolded:
         paramIndex = -1
         offsetX = 17
     font = QFont('Verdana', 8)
     fm = QFontMetricsF(font)
     if isinstance(self.element, Algorithm):
         h = -(fm.height() * 1.2) * (paramIndex + 2) - fm.height() / 2.0 + 8
         h = h - ModelerGraphicItem.BOX_HEIGHT / 2.0
     else:
         h = 0
     return QPointF(-ModelerGraphicItem.BOX_WIDTH / 2 + offsetX, h)
Exemple #12
0
    def getAdjustedText(self, text):
        font = QFont('Verdana', 8)
        fm = QFontMetricsF(font)
        w = fm.width(text)
        if w < self.BOX_WIDTH - 25 - FlatButtonGraphicItem.WIDTH:
            return text

        text = text[0:-3] + '...'
        w = fm.width(text)
        while w > self.BOX_WIDTH - 25 - FlatButtonGraphicItem.WIDTH:
            text = text[0:-4] + '...'
            w = fm.width(text)
        return text
 def getLinkPointForOutput(self, outputIndex):
     if isinstance(self.element, Algorithm):
         outputIndex = outputIndex if not self.element.outputsFolded else -1
         text = self.getAdjustedText(self.element.algorithm.outputs[outputIndex].description)
         font = QFont("Verdana", 8)
         fm = QFontMetricsF(font)
         w = fm.width(text)
         h = fm.height() * 1.2 * (outputIndex + 1) + fm.height() / 2.0
         y = h + ModelerGraphicItem.BOX_HEIGHT / 2.0 + 5
         x = -ModelerGraphicItem.BOX_WIDTH / 2 + 33 + w + 5 if not self.element.outputsFolded else 10
         return QPointF(x, y)
     else:
         return QPointF(0, 0)
Exemple #14
0
 def set_font(self, family=settings.FONT_FAMILY, size=settings.FONT_SIZE):
     font = QFont(family, size)
     self.document().setDefaultFont(font)
     # Fix for older version of Qt which doens't has ForceIntegerMetrics
     if "ForceIntegerMetrics" in dir(QFont):
         self.document().defaultFont().setStyleStrategy(
             QFont.ForceIntegerMetrics)
     font_metrics = QFontMetricsF(self.document().defaultFont())
     if (font_metrics.width("#") * settings.MARGIN_LINE) == \
        (font_metrics.width(" ") * settings.MARGIN_LINE):
         self.pos_margin = font_metrics.width('#') * settings.MARGIN_LINE
     else:
         char_width = font_metrics.averageCharWidth()
         self.pos_margin = char_width * settings.MARGIN_LINE
Exemple #15
0
    def boundingRect(self):
        font = QFont('Verdana', 8)
        fm = QFontMetricsF(font)
        unfolded = isinstance(self.element, Algorithm) and not self.element.paramsFolded
        numParams = len(self.element.algorithm.parameters) if unfolded else 0
        unfolded = isinstance(self.element, Algorithm) and not self.element.outputsFolded
        numOutputs = len(self.element.algorithm.outputs) if unfolded else 0

        hUp = fm.height() * 1.2 * (numParams + 2)
        hDown = fm.height() * 1.2 * (numOutputs + 2)
        rect = QRectF(-(ModelerGraphicItem.BOX_WIDTH + 2) / 2,
                      -(ModelerGraphicItem.BOX_HEIGHT + 2) / 2 - hUp,
                      ModelerGraphicItem.BOX_WIDTH + 2,
                      ModelerGraphicItem.BOX_HEIGHT + hDown + hUp)
        return rect
 def polygon(self):
     font = QFont("Verdana", 8)
     fm = QFontMetricsF(font)
     hUp = fm.height() * 1.2 * (len(self.element.parameters) + 2)
     hDown = fm.height() * 1.2 * (len(self.element.outputs) + 2)
     pol = QPolygonF(
         [
             QPointF(-(ModelerGraphicItem.BOX_WIDTH + 2) / 2, -(ModelerGraphicItem.BOX_HEIGHT + 2) / 2 - hUp),
             QPointF(-(ModelerGraphicItem.BOX_WIDTH + 2) / 2, (ModelerGraphicItem.BOX_HEIGHT + 2) / 2 + hDown),
             QPointF((ModelerGraphicItem.BOX_WIDTH + 2) / 2, (ModelerGraphicItem.BOX_HEIGHT + 2) / 2 + hDown),
             QPointF((ModelerGraphicItem.BOX_WIDTH + 2) / 2, -(ModelerGraphicItem.BOX_HEIGHT + 2) / 2 - hUp),
             QPointF(-(ModelerGraphicItem.BOX_WIDTH + 2) / 2, -(ModelerGraphicItem.BOX_HEIGHT + 2) / 2 - hUp),
         ]
     )
     return pol
Exemple #17
0
    def _update_sidebar(self):
        # Margin 0 is used for line numbers
        fontmetrics = QFontMetricsF(self.__font)
        maxLine = math.ceil(math.log10(self.lines()))
        self.setMarginWidth(0, fontmetrics.width('0' * int(maxLine)) + 10)

        # Fold
        self.foldable_lines = []
        lines = self.lines()
        for line in range(lines):
            text = self.text(line)
            if self.patFold.match(text):
                self.foldable_lines.append(line)
                if line in self.contractedFolds():
                    self.markerDelete(line, self._fold_collapsed_marker)
                    self.markerDelete(line, self._fold_expanded_marker)
                    self.markerAdd(line, self._fold_collapsed_marker)
                else:
                    self.markerDelete(line, self._fold_collapsed_marker)
                    self.markerDelete(line, self._fold_expanded_marker)
                    self.markerAdd(line, self._fold_expanded_marker)
Exemple #18
0
 def paint(self, painter, option, widget=None):
     rect = QRectF(-(ModelerGraphicItem.BOX_WIDTH + 2) / 2.0,
                   -(ModelerGraphicItem.BOX_HEIGHT + 2) / 2.0,
                   ModelerGraphicItem.BOX_WIDTH + 2,
                   ModelerGraphicItem.BOX_HEIGHT + 2)
     painter.setPen(QPen(Qt.gray, 1))
     color = QColor(125, 232, 232)
     if isinstance(self.element, ModelerParameter):
         color = QColor(179, 179, 255)
     elif isinstance(self.element, Algorithm):
         color = Qt.white
     painter.setBrush(QBrush(color, Qt.SolidPattern))
     painter.drawRect(rect)
     font = QFont('Verdana', 8)
     painter.setFont(font)
     painter.setPen(QPen(Qt.black))
     text = self.getAdjustedText(self.text)
     if isinstance(self.element, Algorithm) and not self.element.active:
         painter.setPen(QPen(Qt.gray))
         text = text + "\n(deactivated)"
     elif self.isSelected():
         painter.setPen(QPen(Qt.blue))
     fm = QFontMetricsF(font)
     text = self.getAdjustedText(self.text)
     h = fm.height()
     pt = QPointF(-ModelerGraphicItem.BOX_WIDTH / 2 + 25, h / 2.0)
     painter.drawText(pt, text)
     painter.setPen(QPen(Qt.black))
     if isinstance(self.element, Algorithm):
         h = -(fm.height() * 1.2)
         h = h - ModelerGraphicItem.BOX_HEIGHT / 2.0 + 5
         pt = QPointF(-ModelerGraphicItem.BOX_WIDTH / 2 + 25, h)
         painter.drawText(pt, 'In')
         i = 1
         if not self.element.paramsFolded:
             for param in self.element.algorithm.parameters:
                 if not param.hidden:
                     text = self.getAdjustedText(param.description)
                     h = -(fm.height() * 1.2) * (i + 1)
                     h = h - ModelerGraphicItem.BOX_HEIGHT / 2.0 + 5
                     pt = QPointF(-ModelerGraphicItem.BOX_WIDTH / 2
                                  + 33, h)
                     painter.drawText(pt, text)
                     i += 1
         h = fm.height() * 1.2
         h = h + ModelerGraphicItem.BOX_HEIGHT / 2.0
         pt = QPointF(-ModelerGraphicItem.BOX_WIDTH / 2 + 25, h)
         painter.drawText(pt, 'Out')
         if not self.element.outputsFolded:
             for i, out in enumerate(self.element.algorithm.outputs):
                 text = self.getAdjustedText(out.description)
                 h = fm.height() * 1.2 * (i + 2)
                 h = h + ModelerGraphicItem.BOX_HEIGHT / 2.0
                 pt = QPointF(-ModelerGraphicItem.BOX_WIDTH / 2
                              + 33, h)
                 painter.drawText(pt, text)
     if self.pixmap:
         painter.drawPixmap(-(ModelerGraphicItem.BOX_WIDTH / 2.0) + 3, -8,
                            self.pixmap)
Exemple #19
0
    def _width_with_metrics(self, text):
    
        # Return the width of this piece of text when rendered using this
        # font.
        
        fontName = self.font["BaseFont"].name
        at = fontName.find("+")
        if at != -1:
            fontName = fontName[at+1:]

        if "-" in fontName:
            family, style = fontName.split("-")[:2]
        elif " " in fontName:
            family, style = fontName.split(" ")[:2]
        elif "," in fontName:
            family, style = fontName.split(",")[:2]
        else:
            family = fontName
            style = ""
        
        font = QFontDatabase().font(family, style, self.size)
        font.setPointSizeF(self.size)
        fm = QFontMetricsF(font)
        return fm.width(text)
	def paintEvent(self, event = None):
		
		LogicalSize = 100.0

		def logicalFromPhysical(length, side):
			return (length / side) * LogicalSize

		fm = QFontMetricsF(self.font())
		ymargin = ((LogicalSize / 30.0) + logicalFromPhysical(self.leftSpinBox.height(), self.height()))
		ymax = (LogicalSize - logicalFromPhysical(fm.height() * 2, self.height()))
		width = LogicalSize / 4.0
		
		cx, cy = LogicalSize / 2.0, LogicalSize / 3.0
		ax, ay = cx - (2 * width), ymargin
		bx, by = cx - width, ay
		dx, dy = cx + width, ay
		ex, ey = cx + (2 * width), ymargin
		fx, fy = cx + (width / 2), cx + (LogicalSize / 24.0)
		gx, gy = fx, ymax
		hx, hy = cx - (width / 2), ymax
		ix, iy = hx, fy

		painter = QPainter(self)
		painter.setRenderHint(QPainter.Antialiasing)
		side = min(self.width(), self.height())
		painter.setViewport((self.width() - side) / 2, (self.height() - side) / 2, side, side)
		painter.setWindow(0, 0, LogicalSize, LogicalSize)

		painter.setPen(Qt.NoPen)

		gradient = QLinearGradient(QPointF(0, 0), QPointF(0, 100))
		gradient.setColorAt(0, Qt.white)
		a = self.leftSpinBox.value()
		gradient.setColorAt(1, (Qt.red if a != 0 else Qt.white))
		painter.setBrush(QBrush(gradient))
		painter.drawPolygon(QPolygon([ax, ay, bx, by, cx, cy, ix, iy]))

		gradient = QLinearGradient(QPointF(0, 0), QPointF(0, 100))
		gradient.setColorAt(0, Qt.white)
		b = self.rightSpinBox.value()
		gradient.setColorAt(1, (Qt.blue if b != 0 else Qt.white))
		painter.setBrush(QBrush(gradient))
		painter.drawPolygon(QPolygon([cx, cy, dx, dy, ex, ey, fx, fy]))

		if (a + b) == 0:
			color = QColor(Qt.white)
		else:
			ashare = (a / (a + b)) * 255.0
			bshare = 255.0 - ashare
			color = QColor(ashare, 0, bshare)

		gradient = QLinearGradient(QPointF(0, 0), QPointF(0, 100))
		gradient.setColorAt(0, Qt.white)
		gradient.setColorAt(1, color)
		painter.setBrush(QBrush(gradient))
		painter.drawPolygon(QPolygon([cx, cy, fx, fy, gx, gy, hx, hy, ix, iy]))

		painter.setPen(Qt.black)
		painter.drawPolyline(QPolygon([ax, ay, ix, iy, hx, hy]))
		painter.drawPolyline(QPolygon([gx, gy, fx, fy, ex, ey]))
		painter.drawPolyline(QPolygon([bx, by, cx, cy, dx, dy]))
Exemple #21
0
    def draw(self, painter, size = None):
        """
        :Arguments:
            painter : QPainter
                Opened painter on which to draw
        """
        bounding_rect = QRectF()
        position = self.position
        transfer_function = self.transfer_function
        font = QFont(self.font)
        text_color = self.text_color
        line_color = self.line_color
        line_thickness = self.line_thickness
        value_range = self.value_range
        if size is None:
            viewport = painter.viewport() # viewport rectangle
            mat, ok = painter.worldMatrix().inverted()
            if not ok:
                raise ValueError("Transformation matrix of painter is singular.")
            viewport = mat.mapRect(viewport)
        else:
            viewport = size
# First, prepare the gradient
        w = viewport.width()
        h = viewport.height()
        #print("Size of viewport: {0}x{1}".format(w, h))
        gr = QLinearGradient()
        nb_values = ceil(w/5.0)
        brush_color = QColor()
        for i in range(int(nb_values)):
            brush_color.setRgbF(*transfer_function.rgba(i/nb_values))
            gr.setColorAt(i/nb_values, brush_color)
# Second, find its position
        metric = QFontMetricsF(font, painter.device())
        font_test = [ str(i)*5 for i in range(10) ]
        lim_width = 0
        lim_height = 0
        for t in font_test:
            rect = metric.boundingRect(t)
            lim_width  = max(lim_width,  rect.width())
            lim_height = max(lim_height, rect.height())
        lim_height *= 3
        length = self.scale_length
        shift_length = (1-length)/2
        width = self.scale_width
        shift_width = self.scale_shift_width
        delta_value = value_range[1]-value_range[0]
        if position == "Top":
            scale_rect = QRectF(shift_length*w, shift_width*h, length*w, width*h)
            limit_rect(scale_rect, viewport, lim_width, lim_height)
            gr.setStart(scale_rect.left(), scale_rect.center().y())
            gr.setFinalStop(scale_rect.right(), scale_rect.center().y())
            start_pos = scale_rect.bottomLeft()
            end_pos = scale_rect.bottomRight()
        elif position == "Right":
            scale_rect = QRectF((1-shift_width-width)*w, shift_length*h, width*w, length*h)
            limit_rect(scale_rect, viewport, lim_width, lim_height)
            gr.setStart(scale_rect.center().x(), scale_rect.bottom())
            gr.setFinalStop(scale_rect.center().x(), scale_rect.top())
            start_pos = scale_rect.bottomLeft()
            end_pos = scale_rect.topLeft()
        elif position == "Bottom":
            scale_rect = QRectF(shift_length*w, (1-shift_width-width)*h, length*w, width*h)
            limit_rect(scale_rect, viewport, lim_width, lim_height)
            gr.setStart(scale_rect.left(), scale_rect.center().y())
            gr.setFinalStop(scale_rect.right(), scale_rect.center().y())
            start_pos = scale_rect.topLeft()
            end_pos = scale_rect.topRight()
        elif position == "Left":
            scale_rect = QRectF(shift_width*w, shift_length*h, width*w, length*h)
            limit_rect(scale_rect, viewport, lim_width, lim_height)
            gr.setStart(scale_rect.center().x(), scale_rect.bottom())
            gr.setFinalStop(scale_rect.center().x(), scale_rect.top())
            start_pos = scale_rect.bottomRight()
            end_pos = scale_rect.topRight()
        else:
            raise ValueError("Invalid scale position: %s" % position)
        shift_pos = (end_pos-start_pos)/delta_value
        if position in ["Left", "Right"]:
            is_vertical = True
            length = scale_rect.height()
        else:
            is_vertical = False
            length = scale_rect.width()
# Get the ticks
        ticks = self.selectValues(length, is_vertical, painter)
        if len(ticks) == 0:
            return
        ticks_str, ticks_extra = self._tick2str(ticks)
# Figure the shifts
        dist_to_bar = self.text_to_bar
        max_width = 0
        max_height = 0
        for t in ticks_str:
            rect = metric.boundingRect(t)
            max_width = max(rect.width(), max_width)
            max_height = max(rect.height(), max_height)
        if position == "Left":
            shift_left = dist_to_bar
            shift_top = None
        elif position == "Right":
            shift_left = -dist_to_bar-max_width
            shift_top = None
        elif position == "Top":
            shift_left = None
            shift_top = dist_to_bar
        else:
            shift_left = None
            shift_top = -dist_to_bar-max_height
        painter.save()
        painter.translate(viewport.topLeft())
        #print("viewport.topLeft() = {0}x{1}".format(viewport.left(), viewport.top()))
        painter.setBrush(gr)
        line_pen = QPen(line_color)
        line_pen.setWidth(line_thickness)
        painter.setPen(line_pen)
        painter.drawRect(scale_rect)
        bounding_rect |= scale_rect
        #print("Scale rect: +{0}+{1}x{2}x{3}".format(scale_rect.left(),
            #scale_rect.top(), scale_rect.width(), scale_rect.height()))
        painter.setFont(font)
        painter.setPen(text_color)
        for ts,t in zip(ticks_str, ticks):
            r = metric.boundingRect(ts)
            pos = start_pos+shift_pos*(t-value_range[0])
            if shift_left is None:
                pos.setX( pos.x() - r.width()/2 )
            else:
                pos.setX( pos.x() + shift_left )
            if shift_top is None:
                pos.setY( pos.y() - r.height()/2)
            else:
                pos.setY( pos.y() + shift_top )
            r.moveTo(pos)
            real_rect = painter.drawText(r, Qt.TextDontClip | Qt.AlignVCenter | Qt.AlignHCenter, ts)
            bounding_rect |= real_rect
        if ticks_extra is not None or self.unit:
            unit = self.unit
            exp_width = width = space_width = 0
            exp_txt = ""
            r = exp_r = unit_r = QRectF()
            exp_font = None
            if ticks_extra is not None:
                exp_txt = u"×10"
                r = metric.boundingRect(exp_txt)
                exp_font = QFont(font)
                exp_size = self.exp_size
                if exp_font.pixelSize() != -1:
                    exp_font.setPixelSize(exp_size*exp_font.pixelSize())
                else:
                    exp_font.setPointSizeF(exp_size*exp_font.pointSizeF())
                exp_metric = QFontMetricsF(exp_font, painter.device())
                exp_r = exp_metric.boundingRect(ticks_extra)
            if unit:
                unit_r = metric.boundingRect(unit)
            total_width = r.width()+exp_r.width()+unit_r.width()
            total_height = max(r.height(),unit_r.height())+exp_r.height()/2
            pos = scale_rect.topRight()
            log_debug("top right of scale bar = (%g,%g)" % (pos.x(), pos.y()))
            log_debug("Size of image = (%d,%d)" % (w,h))
            log_debug("Size of text = (%g,%g)" % (total_width, total_height))
            if position == "Bottom":
                pos.setY(pos.y() + scale_rect.height() + dist_to_bar)
                pos.setX(pos.x() - total_width)
            elif position == "Top":
                pos.setY(pos.y() - dist_to_bar - total_height)
                pos.setX(pos.x() - total_width)
            else: # position == "left" or "right"
                pos.setX(pos.x() - (scale_rect.width() + total_width)/2)
                if pos.x() < 0:
                    pos.setX(dist_to_bar)
                elif pos.x()+total_width+dist_to_bar > w:
                    pos.setX(w - total_width - dist_to_bar)
                pos.setY(pos.y() - dist_to_bar - total_height)
            log_debug("Display unit at position: (%g,%g)" % (pos.x(), pos.y()))

            if ticks_extra is not None:
                r.moveTo(pos)
                real_rect = painter.drawText(r, Qt.TextDontClip | Qt.AlignVCenter | Qt.AlignHCenter, exp_txt)
                bounding_rect |= real_rect
                pos.setX( pos.x() + r.width() )
                pos.setY( pos.y() - metric.ascent()/2 )
                exp_r.moveTo(pos)
                painter.setFont(exp_font)
                real_rect = painter.drawText(exp_r, Qt.TextDontClip | Qt.AlignVCenter | Qt.AlignHCenter, ticks_extra)
                bounding_rect |= real_rect
                pos.setY(pos.y() + metric.ascent()/2)
            if unit:
                pos.setX(pos.x() + space_width + exp_r.width())
                unit_r.moveTo(pos)
                painter.setFont(font)
                real_rect = painter.drawText(unit_r, Qt.TextDontClip | Qt.AlignVCenter | Qt.AlignHCenter, unit)
                bounding_rect |= real_rect
        # Draw the ticks now
        painter.setPen(line_pen)
        tick_size = self.tick_size
        if is_vertical:
            width = scale_rect.width()*tick_size
        else:
            width = scale_rect.height()*tick_size
        pen_width = painter.pen().widthF()
        if pen_width == 0:
            pen_width = 1.0
        for t in ticks:
            pos1 = start_pos + shift_pos*(t-value_range[0])
            pos2 = QPointF(pos1)
            if is_vertical:
                pos1.setX(scale_rect.left() + pen_width)
                pos2.setX(pos1.x() + width - pen_width)
                painter.drawLine(pos1, pos2)
                pos1.setX(scale_rect.right() - pen_width)
                pos2.setX(pos1.x() - width + pen_width)
                painter.drawLine(pos1, pos2)
            else:
                pos1.setY(scale_rect.top() + pen_width)
                pos2.setY(pos1.y() + width - pen_width)
                painter.drawLine(pos1, pos2)
                pos1.setY(scale_rect.bottom() - pen_width)
                pos2.setY(pos1.y() - width + pen_width)
                painter.drawLine(pos1, pos2)
        painter.restore()
        bounding_rect = bounding_rect.adjusted(-pen_width, -pen_width, pen_width, pen_width)
        return bounding_rect