| 407 | } |
| 408 | |
| 409 | void PlotViewWidget::drawAxisTickLabels(QPainter & painter, |
| 410 | const PlotViewWidget::AxisProperties & properties, |
| 411 | const QList<PlotViewWidget::TickValue> &ticks, |
| 412 | Range<double> visibleRange) const |
| 413 | { |
| 414 | if (ticks.isEmpty() || this->model == nullptr) |
| 415 | return; |
| 416 | |
| 417 | painter.setPen(QPen(Qt::black, 1)); |
| 418 | QFont displayFont = painter.font(); |
| 419 | QFontMetricsF metrics(displayFont); |
| 420 | |
| 421 | // For drawing the labels we extend the visible range by 1/2 left and right because these might |
| 422 | // still be visible |
| 423 | const auto visibleRangeWidthHalf = (visibleRange.max - visibleRange.min) / 2; |
| 424 | visibleRange.min -= visibleRangeWidthHalf; |
| 425 | visibleRange.max += visibleRangeWidthHalf; |
| 426 | |
| 427 | QList<PlotViewWidget::TickValue> ticksToDrawTextFor; |
| 428 | if (properties.axis == Axis::Y) |
| 429 | { |
| 430 | for (auto tick : ticks) |
| 431 | { |
| 432 | if (tick.value >= visibleRange.min && tick.value <= visibleRange.max) |
| 433 | ticksToDrawTextFor.append(tick); |
| 434 | } |
| 435 | } |
| 436 | else |
| 437 | { |
| 438 | if (ticks.count() < 2) |
| 439 | { |
| 440 | if (ticks[0].value >= visibleRange.min && ticks[0].value <= visibleRange.max) |
| 441 | ticksToDrawTextFor.append(ticks[0]); |
| 442 | } |
| 443 | else |
| 444 | { |
| 445 | const auto maxLabelSize = this->getMaxLabelDrawSize(painter, properties.axis, ticks); |
| 446 | const auto distanceBetweenLabels = 20; |
| 447 | const double minDistanceBetweenTicks = maxLabelSize.width() + distanceBetweenLabels; |
| 448 | const double tickDistanceInValues = ticks[1].value - ticks[0].value; |
| 449 | const auto tickDistanceInPixels = convertPlotPosToPixelPos(QPointF(ticks[1].value, 0)).x() - |
| 450 | convertPlotPosToPixelPos(QPointF(ticks[0].value, 0)).x(); |
| 451 | auto drawEveryNthTick = 1; |
| 452 | while (tickDistanceInPixels * drawEveryNthTick < minDistanceBetweenTicks) |
| 453 | drawEveryNthTick *= 2; |
| 454 | |
| 455 | for (auto tick : ticks) |
| 456 | { |
| 457 | const auto indexOfTickFrom0 = int(std::round(tick.value / tickDistanceInValues)); |
| 458 | const auto isTickVisibleBySubsampling = indexOfTickFrom0 % drawEveryNthTick == 0; |
| 459 | const auto isTickWithinVisibleRange = |
| 460 | (tick.value >= visibleRange.min && tick.value <= visibleRange.max); |
| 461 | if (isTickVisibleBySubsampling && isTickWithinVisibleRange) |
| 462 | ticksToDrawTextFor.append(tick); |
| 463 | } |
| 464 | } |
| 465 | } |
| 466 |
no test coverage detected