| 41 | } |
| 42 | |
| 43 | void MeasurementQtAnnotation::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, |
| 44 | QWidget *widget) { |
| 45 | if (_annotation) { |
| 46 | QColor lineColor = this->getDrawingColor(); |
| 47 | _currentLoD = option->levelOfDetailFromTransform(painter->worldTransform()); |
| 48 | std::vector<Point> coords = _annotation->getCoordinates(); |
| 49 | if (coords.size() > 1) { |
| 50 | if (isSelected()) { |
| 51 | painter->setPen(QPen(QBrush(lineColor.lighter(150)), _lineAnnotationSelectedThickness / _currentLoD)); |
| 52 | } |
| 53 | else { |
| 54 | painter->setPen(QPen(QBrush(lineColor), _lineThickness / _currentLoD)); |
| 55 | } |
| 56 | painter->drawLine(QPointF(0, 0), this->mapFromScene(coords[coords.size() - 1].getX()*_scale, coords[coords.size() - 1].getY()*_scale)); |
| 57 | Point firstPoint = coords[0]; |
| 58 | Point lastPoint = coords[coords.size() - 1]; |
| 59 | QPointF centerPoint = this->mapFromScene(lastPoint.getX()*_scale, lastPoint.getY()*_scale) / 2; |
| 60 | float vecLength = sqrt(QPointF::dotProduct(centerPoint, centerPoint)); |
| 61 | QPointF unitVec = QPointF(centerPoint.y(), -centerPoint.x()) / vecLength; |
| 62 | QPointF textPos = centerPoint.y() < 0 ? (centerPoint - (15 / _currentLoD) * unitVec) * _currentLoD : (centerPoint + (15 / _currentLoD) * unitVec) * _currentLoD;; |
| 63 | QPainterPath textPath; |
| 64 | QFont ft = QFont("Arial"); |
| 65 | float sizeInPixels = sqrt(pow(firstPoint.getX() - lastPoint.getX(), 2) + pow(firstPoint.getY() - lastPoint.getY(), 2)); |
| 66 | if (_spacing.size() > 0) { |
| 67 | float sizeInUnits = sizeInPixels * _spacing[0]; |
| 68 | QString unit = " um"; |
| 69 | if (sizeInUnits > 1000) { |
| 70 | sizeInUnits /= 1000; |
| 71 | unit = " mm"; |
| 72 | } |
| 73 | textPath.addText(textPos, ft, QString::number(sizeInUnits, 'g', 4) + unit); |
| 74 | } |
| 75 | else { |
| 76 | textPath.addText(textPos, ft, QString::number(sizeInPixels) + QString(" pixels")); |
| 77 | } |
| 78 | painter->scale(1.5 / _currentLoD, 1.5 / _currentLoD); |
| 79 | painter->setPen(Qt::NoPen); |
| 80 | painter->setBrush(QBrush(QColor(0,0,0,75))); |
| 81 | painter->drawRect(textPath.boundingRect().adjusted(-5,-5,5,5)); |
| 82 | painter->setBrush(QBrush(Qt::white)); |
| 83 | painter->drawPath(textPath); |
| 84 | } |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | bool MeasurementQtAnnotation::collidesWithPath(const QPainterPath & path, Qt::ItemSelectionMode mode) const { |
| 89 | return contains(path.currentPosition()); |
nothing calls this directly
no test coverage detected