def paint(self, painter): widget = self.parent() scale = widget.inverseScale() # metrics if self._rulerObject is not None: line, d, a = self._rulerObject origin = line.p1() cursor = line.p2() sz = 8 * scale color = QColor(140, 193, 255, 170) # line painter.save() painter.setPen(color) drawing.drawLine(painter, origin.x(), origin.y(), cursor.x(), cursor.y(), scale) path = QPainterPath() path.addEllipse(origin.x() - sz / 2, origin.y() - sz / 2, sz, sz) path.addEllipse(cursor.x() - sz / 2, cursor.y() - sz / 2, sz, sz) painter.fillPath(path, color) painter.restore() # text xAlign = yAlign = "center" pos = (origin + cursor) / 2 drawing.drawTextAtPoint(painter, d, pos.x(), pos.y(), scale, xAlign, yAlign) xAlign, yAlign = "left", "top" dx = cursor.x() - origin.x() if dx < 0: xAlign = "right" drawing.drawTextAtPoint(painter, a, cursor.x(), cursor.y(), scale, xAlign, yAlign)
def paint(self, painter, index): widget = self.parent() if index != widget.activeIndex(): return scale = widget.inverseScale() # metrics if self._rulerObject is not None: line, a = self._rulerObject origin = line.p1() cursor = line.p2() size = 8 * scale halfSize = 4 * scale color = QColor(255, 85, 127, 170) # line painter.save() painter.setPen(color) drawing.drawLine(painter, origin.x(), origin.y(), cursor.x(), cursor.y(), scale) # ellipses ellipses = [ (origin.x(), origin.y()), (cursor.x(), cursor.y()), ] path = QPainterPath() path.setFillRule(Qt.WindingFill) for x, y in itertools.chain(self._rulerPts.values(), ellipses): x -= halfSize y -= halfSize path.addEllipse(x, y, size, size) painter.fillPath(path, color) painter.restore() # text line = QLineF(line) xAlign = yAlign = "center" ellipses.pop(0) rp = self._rulerPts # XXX: sort shouldn't be performed in paintEvent for pt in itertools.chain((rp[k] for k in sorted(rp)), ellipses): p = QPointF(*pt) line.setP2(p) if line.length(): d = str(round(line.length(), 1)) pos = (line.p1() + line.p2()) / 2 drawing.drawTextAtPoint(painter, d, pos.x(), pos.y(), scale, xAlign, yAlign) line.setP1(p) xAlign, yAlign = "left", "top" dx = cursor.x() - origin.x() px = size if dx < 0: xAlign = "right" px = -px drawing.drawTextAtPoint(painter, a, cursor.x() + px, cursor.y() + size, scale, xAlign, yAlign)
def paint(self, painter): if self._rulerObject is not None: path, text = self._rulerObject painter.drawPath(path) baseElem = path.elementAt(0) cursorElem = path.elementAt(2) # work out text alignment # we need to find out which quadrant the ruler is operating in xAlign, yAlign = "left", "bottom" dx = cursorElem.x - baseElem.x if dx < 0: xAlign = "right" dy = cursorElem.y - baseElem.y if dy <= 0: yAlign = "top" drawing.drawTextAtPoint( painter, text, cursorElem.x, baseElem.y, self.parent()._inverseScale, xAlign, yAlign)