#pragma once #include #include #include namespace nmMeasurementLineStyle { // 标定线和测量线的默认画笔,供拖动预览和完成后的图元共用。 inline QPen scalePen() { return QPen(QBrush(QColor(80, 105, 125)), 0.35, Qt::DashLine, Qt::FlatCap); } inline QPen measurePen() { return QPen(QBrush(QColor(196, 106, 30)), 0.35, Qt::SolidLine, Qt::FlatCap); } // 沿端点的法向绘制短刻度,刻度本身始终使用实线。 inline void drawScaleLine(QPainter* painter, const QLineF& line, QPen pen) { const qreal length = line.length(); if (!qIsFinite(length) || length <= 0.0) return; painter->setPen(pen); painter->drawLine(line); const qreal halfTickLength = 0.85; const QPointF tick(-line.dy() / length * halfTickLength, line.dx() / length * halfTickLength); pen.setStyle(Qt::SolidLine); painter->setPen(pen); painter->drawLine(line.p1() - tick, line.p1() + tick); painter->drawLine(line.p2() - tick, line.p2() + tick); } // 线段止于圆边,短线缩小端点圆;单击选取拟合参考点时只画一个圆。 inline void drawMeasureLine(QPainter* painter, const QLineF& line, QPen pen) { const qreal length = line.length(); if (!qIsFinite(length)) return; const qreal endpointRadius = 0.65; qreal radius = endpointRadius; if (length > 0.0) { radius = qMin(endpointRadius, length / 3.0); const QPointF offset(line.dx() / length * radius, line.dy() / length * radius); painter->setPen(pen); painter->drawLine(line.p1() + offset, line.p2() - offset); } pen.setStyle(Qt::SolidLine); painter->setPen(pen); painter->setBrush(Qt::NoBrush); painter->drawEllipse(line.p1(), radius, radius); if (length > 0.0) { painter->drawEllipse(line.p2(), radius, radius); } } } // namespace nmMeasurementLineStyle