def labelTransformation(self, pos, size): transform = QTransform() transform.translate(pos.x(), pos.y()) transform.rotate(self.labelRotation()) flags = self.labelAlignment() if flags == 0: flags = self.Flags[self.alignment()] if flags & Qt.AlignLeft: x = -size.width() elif flags & Qt.AlignRight: x = 0. else: x = -(.5*size.width()) if flags & Qt.AlignTop: y = -size.height() elif flags & Qt.AlignBottom: y = 0 else: y = -(.5*size.height()) transform.translate(x, y) return transform
def labelTransformation(self, pos, size): transform = QTransform() transform.translate(pos.x(), pos.y()) transform.rotate(self.labelRotation()) flags = self.labelAlignment() if flags == 0: flags = self.Flags[self.alignment()] if flags & Qt.AlignLeft: x = -size.width() elif flags & Qt.AlignRight: x = 0. else: x = -(.5 * size.width()) if flags & Qt.AlignTop: y = -size.height() elif flags & Qt.AlignBottom: y = 0 else: y = -(.5 * size.height()) transform.translate(x, y) return transform
def qwtDrawGraphicSymbols(painter, points, numPoint, graphic, symbol): pointRect = QRectF(graphic.controlPointRect()) if pointRect.isEmpty(): return sx = 1. sy = 1. sz = symbol.size() if sz.isValid(): sx = sz.width()/pointRect.width() sy = sz.height()/pointRect.height() pinPoint = QPointF(pointRect.center()) if symbol.isPinPointEnabled(): pinPoint = symbol.pinPoint() transform = QTransform(painter.transform()) for pos in points: tr = QTransform(transform) tr.translate(pos.x(), pos.y()) tr.scale(sx, sy) tr.translate(-pinPoint.x(), -pinPoint.y()) painter.setTransform(tr) graphic.render(painter) painter.setTransform(transform)
def qwtDrawGraphicSymbols(painter, points, numPoint, graphic, symbol): pointRect = QRectF(graphic.controlPointRect()) if pointRect.isEmpty(): return sx = 1. sy = 1. sz = symbol.size() if sz.isValid(): sx = sz.width() / pointRect.width() sy = sz.height() / pointRect.height() pinPoint = QPointF(pointRect.center()) if symbol.isPinPointEnabled(): pinPoint = symbol.pinPoint() transform = QTransform(painter.transform()) for pos in points: tr = QTransform(transform) tr.translate(pos.x(), pos.y()) tr.scale(sx, sy) tr.translate(-pinPoint.x(), -pinPoint.y()) painter.setTransform(tr) graphic.render(painter) painter.setTransform(transform)
def labelTransformation(self, pos, size): """ Calculate the transformation that is needed to paint a label depending on its alignment and rotation. :param QPointF pos: Position where to paint the label :param QSizeF size: Size of the label :return: Transformation matrix .. seealso:: :py:meth:`setLabelAlignment()`, :py:meth:`setLabelRotation()` """ transform = QTransform() transform.translate(pos.x(), pos.y()) transform.rotate(self.labelRotation()) flags = self.labelAlignment() if flags == 0: flags = self.Flags[self.alignment()] if flags & Qt.AlignLeft: x = -size.width() elif flags & Qt.AlignRight: x = 0. else: x = -(.5*size.width()) if flags & Qt.AlignTop: y = -size.height() elif flags & Qt.AlignBottom: y = 0 else: y = -(.5*size.height()) transform.translate(x, y) return transform
def render(self, *args): """ .. py:method:: render(painter) Replay all recorded painter commands :param QPainter painter: Qt painter .. py:method:: render(painter, size, aspectRatioMode) Replay all recorded painter commands The graphic is scaled to fit into the rectangle of the given size starting at ( 0, 0 ). :param QPainter painter: Qt painter :param QSizeF size: Size for the scaled graphic :param Qt.AspectRatioMode aspectRatioMode: Mode how to scale .. py:method:: render(painter, rect, aspectRatioMode) Replay all recorded painter commands The graphic is scaled to fit into the given rectangle :param QPainter painter: Qt painter :param QRectF rect: Rectangle for the scaled graphic :param Qt.AspectRatioMode aspectRatioMode: Mode how to scale .. py:method:: render(painter, pos, aspectRatioMode) Replay all recorded painter commands The graphic is scaled to the :py:meth:`defaultSize()` and aligned to a position. :param QPainter painter: Qt painter :param QPointF pos: Reference point, where to render :param Qt.AspectRatioMode aspectRatioMode: Mode how to scale """ if len(args) == 1: painter, = args if self.isNull(): return transform = painter.transform() painter.save() for command in self.__data.commands: qwtExecCommand(painter, command, self.__data.renderHints, transform, self.__data.initialTransform) painter.restore() elif len(args) in (2, 3) and isinstance(args[1], QSizeF): painter, size = args[:2] aspectRatioMode = Qt.IgnoreAspectRatio if len(args) == 3: aspectRatioMode = args[-1] r = QRectF(0., 0., size.width(), size.height()) self.render(painter, r, aspectRatioMode) elif len(args) in (2, 3) and isinstance(args[1], QRectF): painter, rect = args[:2] aspectRatioMode = Qt.IgnoreAspectRatio if len(args) == 3: aspectRatioMode = args[-1] if self.isEmpty() or rect.isEmpty(): return sx = 1. sy = 1. if self.__data.pointRect.width() > 0.: sx = rect.width()/self.__data.pointRect.width() if self.__data.pointRect.height() > 0.: sy = rect.height()/self.__data.pointRect.height() scalePens = not bool(self.__data.renderHints & self.RenderPensUnscaled) for info in self.__data.pathInfos: ssx = info.scaleFactorX(self.__data.pointRect, rect, scalePens) if ssx > 0.: sx = min([sx, ssx]) ssy = info.scaleFactorY(self.__data.pointRect, rect, scalePens) if ssy > 0.: sy = min([sy, ssy]) if aspectRatioMode == Qt.KeepAspectRatio: s = min([sx, sy]) sx = s sy = s elif aspectRatioMode == Qt.KeepAspectRatioByExpanding: s = max([sx, sy]) sx = s sy = s tr = QTransform() tr.translate(rect.center().x()-.5*sx*self.__data.pointRect.width(), rect.center().y()-.5*sy*self.__data.pointRect.height()) tr.scale(sx, sy) tr.translate(-self.__data.pointRect.x(), -self.__data.pointRect.y()) transform = painter.transform() if not scalePens and transform.isScaling(): # we don't want to scale pens according to sx/sy, # but we want to apply the scaling from the # painter transformation later self.__data.initialTransform = QTransform() self.__data.initialTransform.scale(transform.m11(), transform.m22()) painter.setTransform(tr, True) self.render(painter) painter.setTransform(transform) self.__data.initialTransform = None elif len(args) in (2, 3) and isinstance(args[1], QPointF): painter, pos = args[:2] alignment = Qt.AlignTop|Qt.AlignLeft if len(args) == 3: alignment = args[-1] r = QRectF(pos, self.defaultSize()) if alignment & Qt.AlignLeft: r.moveLeft(pos.x()) elif alignment & Qt.AlignHCenter: r.moveCenter(QPointF(pos.x(), r.center().y())) elif alignment & Qt.AlignRight: r.moveRight(pos.x()) if alignment & Qt.AlignTop: r.moveTop(pos.y()) elif alignment & Qt.AlignVCenter: r.moveCenter(QPointF(r.center().x(), pos.y())) elif alignment & Qt.AlignBottom: r.moveBottom(pos.y()) self.render(painter, r) else: raise TypeError("%s().render() takes 1, 2 or 3 argument(s) (%s "\ "given)" % (self.__class__.__name__, len(args)))
def render(self, *args): """ .. py:method:: render(painter) Replay all recorded painter commands :param QPainter painter: Qt painter .. py:method:: render(painter, size, aspectRatioMode) Replay all recorded painter commands The graphic is scaled to fit into the rectangle of the given size starting at ( 0, 0 ). :param QPainter painter: Qt painter :param QSizeF size: Size for the scaled graphic :param Qt.AspectRatioMode aspectRatioMode: Mode how to scale .. py:method:: render(painter, rect, aspectRatioMode) Replay all recorded painter commands The graphic is scaled to fit into the given rectangle :param QPainter painter: Qt painter :param QRectF rect: Rectangle for the scaled graphic :param Qt.AspectRatioMode aspectRatioMode: Mode how to scale .. py:method:: render(painter, pos, aspectRatioMode) Replay all recorded painter commands The graphic is scaled to the :py:meth:`defaultSize()` and aligned to a position. :param QPainter painter: Qt painter :param QPointF pos: Reference point, where to render :param Qt.AspectRatioMode aspectRatioMode: Mode how to scale """ if len(args) == 1: painter, = args if self.isNull(): return transform = painter.transform() painter.save() for command in self.__data.commands: qwtExecCommand(painter, command, self.__data.renderHints, transform, self.__data.initialTransform) painter.restore() elif len(args) in (2, 3) and isinstance(args[1], QSizeF): painter, size = args[:2] aspectRatioMode = Qt.IgnoreAspectRatio if len(args) == 3: aspectRatioMode = args[-1] r = QRectF(0., 0., size.width(), size.height()) self.render(painter, r, aspectRatioMode) elif len(args) in (2, 3) and isinstance(args[1], QRectF): painter, rect = args[:2] aspectRatioMode = Qt.IgnoreAspectRatio if len(args) == 3: aspectRatioMode = args[-1] if self.isEmpty() or rect.isEmpty(): return sx = 1. sy = 1. if self.__data.pointRect.width() > 0.: sx = rect.width() / self.__data.pointRect.width() if self.__data.pointRect.height() > 0.: sy = rect.height() / self.__data.pointRect.height() scalePens = not bool(self.__data.renderHints & self.RenderPensUnscaled) for info in self.__data.pathInfos: ssx = info.scaleFactorX(self.__data.pointRect, rect, scalePens) if ssx > 0.: sx = min([sx, ssx]) ssy = info.scaleFactorY(self.__data.pointRect, rect, scalePens) if ssy > 0.: sy = min([sy, ssy]) if aspectRatioMode == Qt.KeepAspectRatio: s = min([sx, sy]) sx = s sy = s elif aspectRatioMode == Qt.KeepAspectRatioByExpanding: s = max([sx, sy]) sx = s sy = s tr = QTransform() tr.translate( rect.center().x() - .5 * sx * self.__data.pointRect.width(), rect.center().y() - .5 * sy * self.__data.pointRect.height()) tr.scale(sx, sy) tr.translate(-self.__data.pointRect.x(), -self.__data.pointRect.y()) transform = painter.transform() if not scalePens and transform.isScaling(): # we don't want to scale pens according to sx/sy, # but we want to apply the scaling from the # painter transformation later self.__data.initialTransform = QTransform() self.__data.initialTransform.scale(transform.m11(), transform.m22()) painter.setTransform(tr, True) self.render(painter) painter.setTransform(transform) self.__data.initialTransform = None elif len(args) in (2, 3) and isinstance(args[1], QPointF): painter, pos = args[:2] alignment = Qt.AlignTop | Qt.AlignLeft if len(args) == 3: alignment = args[-1] r = QRectF(pos, self.defaultSize()) if alignment & Qt.AlignLeft: r.moveLeft(pos.x()) elif alignment & Qt.AlignHCenter: r.moveCenter(QPointF(pos.x(), r.center().y())) elif alignment & Qt.AlignRight: r.moveRight(pos.x()) if alignment & Qt.AlignTop: r.moveTop(pos.y()) elif alignment & Qt.AlignVCenter: r.moveCenter(QPointF(r.center().x(), pos.y())) elif alignment & Qt.AlignBottom: r.moveBottom(pos.y()) self.render(painter, r) else: raise TypeError("%s().render() takes 1, 2 or 3 argument(s) (%s "\ "given)" % (self.__class__.__name__, len(args)))