MCPcopy Create free account
hub / github.com/CieNTi/serial_port_plotter / updatePosition

Method updatePosition

qcustomplot/qcustomplot.cpp:29903–29951  ·  view source on GitHub ↗

! 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

Source from the content-addressed store, hash-verified

29901 If there is no graph set on this tracer, this function does nothing.
29902*/
29903void QCPItemTracer::updatePosition()
29904{
29905 if (mGraph)
29906 {
29907 if (mParentPlot->hasPlottable(mGraph))
29908 {
29909 if (mGraph->data()->size() > 1)
29910 {
29911 QCPGraphDataContainer::const_iterator first = mGraph->data()->constBegin();
29912 QCPGraphDataContainer::const_iterator last = mGraph->data()->constEnd()-1;
29913 if (mGraphKey <= first->key)
29914 position->setCoords(first->key, first->value);
29915 else if (mGraphKey >= last->key)
29916 position->setCoords(last->key, last->value);
29917 else
29918 {
29919 QCPGraphDataContainer::const_iterator it = mGraph->data()->findBegin(mGraphKey);
29920 if (it != mGraph->data()->constEnd()) // mGraphKey is not exactly on last iterator, but somewhere between iterators
29921 {
29922 QCPGraphDataContainer::const_iterator prevIt = it;
29923 ++it; // won't advance to constEnd because we handled that case (mGraphKey >= last->key) before
29924 if (mInterpolating)
29925 {
29926 // interpolate between iterators around mGraphKey:
29927 double slope = 0;
29928 if (!qFuzzyCompare((double)it->key, (double)prevIt->key))
29929 slope = (it->value-prevIt->value)/(it->key-prevIt->key);
29930 position->setCoords(mGraphKey, (mGraphKey-prevIt->key)*slope+prevIt->value);
29931 } else
29932 {
29933 // find iterator with key closest to mGraphKey:
29934 if (mGraphKey < (prevIt->key+it->key)*0.5)
29935 position->setCoords(prevIt->key, prevIt->value);
29936 else
29937 position->setCoords(it->key, it->value);
29938 }
29939 } else // mGraphKey is exactly on last iterator (should actually be caught when comparing first/last keys, but this is a failsafe for fp uncertainty)
29940 position->setCoords(it->key, it->value);
29941 }
29942 } else if (mGraph->data()->size() == 1)
29943 {
29944 QCPGraphDataContainer::const_iterator it = mGraph->data()->constBegin();
29945 position->setCoords(it->key, it->value);
29946 } else
29947 qDebug() << Q_FUNC_INFO << "graph has no data";
29948 } else
29949 qDebug() << Q_FUNC_INFO << "graph not contained in QCustomPlot instance (anymore)";
29950 }
29951}
29952
29953/*! \internal
29954

Callers

nothing calls this directly

Calls 7

hasPlottableMethod · 0.80
dataMethod · 0.80
constBeginMethod · 0.80
constEndMethod · 0.80
setCoordsMethod · 0.80
sizeMethod · 0.45
findBeginMethod · 0.45

Tested by

no test coverage detected