! If the tracer is connected with a graph (\ref setGraph), this function updates the tracer's \a position to reside on the graph data, depending on the configured key (\ref setGraphKey). It is called automatically on every redraw and normally doesn't need to be called manually. One exception is when you want to read the tracer coordinates via \a position and are not sure that the grap
| 30693 | If there is no graph set on this tracer, this function does nothing. |
| 30694 | */ |
| 30695 | void QCPItemTracer::updatePosition() |
| 30696 | { |
| 30697 | if (mGraph) |
| 30698 | { |
| 30699 | if (mParentPlot->hasPlottable(mGraph)) |
| 30700 | { |
| 30701 | if (mGraph->data()->size() > 1) |
| 30702 | { |
| 30703 | QCPGraphDataContainer::const_iterator first = mGraph->data()->constBegin(); |
| 30704 | QCPGraphDataContainer::const_iterator last = mGraph->data()->constEnd()-1; |
| 30705 | if (mGraphKey <= first->key) |
| 30706 | position->setCoords(first->key, first->value); |
| 30707 | else if (mGraphKey >= last->key) |
| 30708 | position->setCoords(last->key, last->value); |
| 30709 | else |
| 30710 | { |
| 30711 | QCPGraphDataContainer::const_iterator it = mGraph->data()->findBegin(mGraphKey); |
| 30712 | if (it != mGraph->data()->constEnd()) // mGraphKey is not exactly on last iterator, but somewhere between iterators |
| 30713 | { |
| 30714 | QCPGraphDataContainer::const_iterator prevIt = it; |
| 30715 | ++it; // won't advance to constEnd because we handled that case (mGraphKey >= last->key) before |
| 30716 | if (mInterpolating) |
| 30717 | { |
| 30718 | // interpolate between iterators around mGraphKey: |
| 30719 | double slope = 0; |
| 30720 | if (!qFuzzyCompare(double(it->key), double(prevIt->key))) |
| 30721 | slope = (it->value-prevIt->value)/(it->key-prevIt->key); |
| 30722 | position->setCoords(mGraphKey, (mGraphKey-prevIt->key)*slope+prevIt->value); |
| 30723 | } else |
| 30724 | { |
| 30725 | // find iterator with key closest to mGraphKey: |
| 30726 | if (mGraphKey < (prevIt->key+it->key)*0.5) |
| 30727 | position->setCoords(prevIt->key, prevIt->value); |
| 30728 | else |
| 30729 | position->setCoords(it->key, it->value); |
| 30730 | } |
| 30731 | } else // mGraphKey is exactly on last iterator (should actually be caught when comparing first/last keys, but this is a failsafe for fp uncertainty) |
| 30732 | position->setCoords(it->key, it->value); |
| 30733 | } |
| 30734 | } else if (mGraph->data()->size() == 1) |
| 30735 | { |
| 30736 | QCPGraphDataContainer::const_iterator it = mGraph->data()->constBegin(); |
| 30737 | position->setCoords(it->key, it->value); |
| 30738 | } else |
| 30739 | qDebug() << Q_FUNC_INFO << "graph has no data"; |
| 30740 | } else |
| 30741 | qDebug() << Q_FUNC_INFO << "graph not contained in QCustomPlot instance (anymore)"; |
| 30742 | } |
| 30743 | } |
| 30744 | |
| 30745 | /*! \internal |
| 30746 |
nothing calls this directly
no test coverage detected