| QCustomPlot *\b mParentPlot |
A pointer to the parent QCustomPlot instance. This is adopted from the axes that are passed in the constructor. |
| QString \b mName |
The name of the plottable. |
| bool \b mVisible |
Whether the plot is visible or not. When this is false, you shouldn't draw the data in the \ref draw function (\ref draw is always called, no matter what mVisible is). |
| QPen \b mPen |
The generic pen of the plottable. You should use this pen for the most prominent data representing lines in the plottable (e.g QCPGraph uses this pen for its graph lines and scatters) |
| QPen \b mSelectedPen |
The generic pen that should be used when the plottable is selected (hint: \ref mainPen gives you the right pen, depending on selection state). |
| QBrush \b mBrush |
The generic brush of the plottable. You should use this brush for the most prominent fillable structures in the plottable (e.g. QCPGraph uses this brush to control filling under the graph) |
| QBrush \b mSelectedBrush |
The generic brush that should be used when the plottable is selected (hint: \ref mainBrush gives you the right brush, depending on selection state). |
| QCPAxis *\b mKeyAxis, *\b mValueAxis |
The key and value axes this plottable is attached to. Call their QCPAxis::coordToPixel functions to translate coordinates to pixels in either the key or value dimension. |
| bool \b mSelected |
indicates whether the plottable is selected or not. |
*/
/* start of documentation of pure virtual functions */
/*! \fn void QCPAbstractPlottable::clearData() = 0
Clears all data in the plottable.
*/
/*! \fn double QCPAbstractPlottable::selectTest(const QPointF &pos) const = 0
This function is used to decide whether a click hits a plottable or not.
\a pos is a point in pixel coordinates on the QCustomPlot surface. This function returns the
shortest pixel distance of this point to the plottable (e.g. to the scatters/lines of a graph).
If the plottable is either invisible, contains no data or the distance couldn't be determined,
-1.0 is returned. \ref setSelectable has no influence on the return value of this function.
If the plottable is represented not by single lines but by an area like QCPBars or
QCPStatisticalBox, a click inside the area returns a constant value greater zero (typically 99%
of the selectionTolerance of the parent QCustomPlot). If the click lies outside the area, this
function returns -1.0.
Providing a constant value for area objects allows selecting line objects even when they are
obscured by such area objects, by clicking close to the lines (i.e. closer than
0.99*selectionTolerance).
The actual setting of the selection state is not done by this function. This is handled by the
parent QCustomPlot when the mouseReleaseEvent occurs.
\see setSelected, QCustomPlot::setInteractions
*/
/*! \fn void QCPAbstractPlottable::draw(QCPPainter *painter) = 0
\internal
Draws this plottable with the provided \a painter. Called by \ref QCustomPlot::draw on all its
visible plottables.
The cliprect of the provided painter is set to the axis rect of the key/value axis of this
plottable (what \ref clipRect returns), before this function is called.
*/
/*! \fn void QCPAbstractPlottable::drawLegendIcon(QCPPainter *painter, const QRect &rect) const = 0
\internal
called by QCPLegend::draw (via QCPPlottableLegendItem::draw) to create a graphical representation
of this plottable inside \a rect, next to the plottable name.
*/
/*! \fn QCPRange QCPAbstractPlottable::getKeyRange(bool &validRange, SignDomain inSignDomain) const = 0
\internal
called by rescaleAxes functions to get the full data key bounds. For logarithmic plots, one can
set \a inSignDomain to either \ref sdNegative or \ref sdPositive in order to restrict the
returned range to that sign domain. E.g. when only negative range is wanted, set \a inSignDomain
to \ref sdNegative and all positive points will be ignored for range calculation. For no
restriction, just set \a inSignDomain to \ref sdBoth (default). \a validRange is an output
parameter that indicates whether a proper range could be found or not. If this is false, you
shouldn't use the returned range (e.g. no points in data).
\see rescaleAxes, getValueRange
*/
/*! \fn QCPRange QCPAbstractPlottable::getValueRange(bool &validRange, SignDomain inSignDomain) const = 0
\internal
called by rescaleAxes functions to get the full data value bounds. For logarithmic plots, one can
set \a inSignDomain to either \ref sdNegative or \ref sdPositive in order to restrict the
returned range to that sign domain. E.g. when only negative range is wanted, set \a inSignDomain
to \ref sdNegative and all positive points will be ignored for range calculation. For no
restriction, just set \a inSignDomain to \ref sdBoth (default). \a validRange is an output
parameter that indicates whether a proper range could be found or not. If this is false, you
shouldn't use the returned range (e.g. no points in data).
\see rescaleAxes, getKeyRange
*/
/* end of documentation of pure virtual functions */
/* start of documentation of signals */
/*! \fn void QCPAbstractPlottable::selectionChanged(bool selected)
This signal is emitted when the selection state of this plottable has changed, either by user interaction
or by a direct call to \ref setSelected.
*/
/* end of documentation of signals */
/*!
Constructs an abstract plottable which uses \a keyAxis as its key axis ("x") and \a valueAxis as
its value axis ("y"). \a keyAxis and \a valueAxis must reside in the same QCustomPlot instance
and not have the same orientation. If either of these restrictions is violated, a corresponding
message is printed to the debug output (qDebug), the construction is not aborted, though.
Since QCPAbstractPlottable is an abstract class that defines the basic interface to plottables
(i.e. any form of data representation inside a plot, like graphs, curves etc.), it can't be
directly instantiated.
You probably want one of the subclasses like \ref QCPGraph and \ref QCPCurve instead.
\see setKeyAxis, setValueAxis
*/
QCPAbstractPlottable::QCPAbstractPlottable(QCPAxis *keyAxis, QCPAxis *valueAxis) :
QCPLayerable(keyAxis->parentPlot()),
mName(""),
mAntialiasedFill(true),
mAntialiasedScatters(true),
mAntialiasedErrorBars(false),
mPen(Qt::black),
mSelectedPen(Qt::black),
mBrush(Qt::NoBrush),
mSelectedBrush(Qt::NoBrush),
mKeyAxis(keyAxis),
mValueAxis(valueAxis),
mSelected(false),
mSelectable(true)
{
if (keyAxis->parentPlot() != valueAxis->parentPlot())
qDebug() << Q_FUNC_INFO << "Parent plot of keyAxis is not the same as that of valueAxis.";
if (keyAxis->orientation() == valueAxis->orientation())
qDebug() << Q_FUNC_INFO << "keyAxis and valueAxis must be orthogonal to each other.";
}
/*!
The name is the textual representation of this plottable as it is displayed in the QCPLegend of
the parent QCustomPlot. It may contain any utf-8 characters, including newlines.
*/
void QCPAbstractPlottable::setName(const QString &name)
{
mName = name;
}
/*!
Sets whether fills of this plottable is drawn antialiased or not.
Note that this setting may be overridden by \ref QCustomPlot::setAntialiasedElements and \ref
QCustomPlot::setNotAntialiasedElements.
*/
void QCPAbstractPlottable::setAntialiasedFill(bool enabled)
{
mAntialiasedFill = enabled;
}
/*!
Sets whether the scatter symbols of this plottable are drawn antialiased or not.
Note that this setting may be overridden by \ref QCustomPlot::setAntialiasedElements and \ref
QCustomPlot::setNotAntialiasedElements.
*/
void QCPAbstractPlottable::setAntialiasedScatters(bool enabled)
{
mAntialiasedScatters = enabled;
}
/*!
Sets whether the error bars of this plottable are drawn antialiased or not.
Note that this setting may be overridden by \ref QCustomPlot::setAntialiasedElements and \ref
QCustomPlot::setNotAntialiasedElements.
*/
void QCPAbstractPlottable::setAntialiasedErrorBars(bool enabled)
{
mAntialiasedErrorBars = enabled;
}
/*!
The pen is used to draw basic lines that make up the plottable representation in the
plot.
For example, the \ref QCPGraph subclass draws its graph lines and scatter points
with this pen.
\see setBrush
*/
void QCPAbstractPlottable::setPen(const QPen &pen)
{
mPen = pen;
}
/*!
When the plottable is selected, this pen is used to draw basic lines instead of the normal
pen set via \ref setPen.
\see setSelected, setSelectable, setSelectedBrush, selectTest
*/
void QCPAbstractPlottable::setSelectedPen(const QPen &pen)
{
mSelectedPen = pen;
}
/*!
The brush is used to draw basic fills of the plottable representation in the
plot. The Fill can be a color, gradient or texture, see the usage of QBrush.
For example, the \ref QCPGraph subclass draws the fill under the graph with this brush, when
it's not set to Qt::NoBrush.
\see setPen
*/
void QCPAbstractPlottable::setBrush(const QBrush &brush)
{
mBrush = brush;
}
/*!
When the plottable is selected, this brush is used to draw fills instead of the normal
brush set via \ref setBrush.
\see setSelected, setSelectable, setSelectedPen, selectTest
*/
void QCPAbstractPlottable::setSelectedBrush(const QBrush &brush)
{
mSelectedBrush = brush;
}
/*!
The key axis of a plottable can be set to any axis of a QCustomPlot, as long as it is orthogonal
to the plottable's value axis. This function performs no checks to make sure this is the case.
The typical mathematical choice is to use the x-axis (QCustomPlot::xAxis) as key axis and the
y-axis (QCustomPlot::yAxis) as value axis.
Normally, the key and value axes are set in the constructor of the plottable (or \ref
QCustomPlot::addGraph when working with QCPGraphs through the dedicated graph interface).
\see setValueAxis
*/
void QCPAbstractPlottable::setKeyAxis(QCPAxis *axis)
{
mKeyAxis = axis;
}
/*!
The value axis of a plottable can be set to any axis of a QCustomPlot, as long as it is
orthogonal to the plottable's key axis. This function performs no checks to make sure this is the
case. The typical mathematical choice is to use the x-axis (QCustomPlot::xAxis) as key axis and
the y-axis (QCustomPlot::yAxis) as value axis.
Normally, the key and value axes are set in the constructor of the plottable (or \ref
QCustomPlot::addGraph when working with QCPGraphs through the dedicated graph interface).
\see setKeyAxis
*/
void QCPAbstractPlottable::setValueAxis(QCPAxis *axis)
{
mValueAxis = axis;
}
/*!
Sets whether the user can (de-)select this plottable by clicking on the QCustomPlot surface.
(When \ref QCustomPlot::setInteractions contains iSelectPlottables.)
However, even when \a selectable was set to false, it is possible to set the selection manually,
by calling \ref setSelected directly.
\see setSelected
*/
void QCPAbstractPlottable::setSelectable(bool selectable)
{
mSelectable = selectable;
}
/*!
Sets whether this plottable is selected or not. When selected, it uses a different pen and brush
to draw its lines and fills, see \ref setSelectedPen and \ref setSelectedBrush.
The entire selection mechanism for plottables is handled automatically when \ref
QCustomPlot::setInteractions contains iSelectPlottables. You only need to call this function when
you wish to change the selection state manually.
This function can change the selection state even when \ref setSelectable was set to false.
emits the \ref selectionChanged signal when \a selected is different from the previous selection state.
\see selectTest
*/
void QCPAbstractPlottable::setSelected(bool selected)
{
if (mSelected != selected)
{
mSelected = selected;
emit selectionChanged(mSelected);
}
}
/*!
Rescales the key and value axes associated with this plottable to contain all displayed data, so
the whole plottable is visible. If the scaling of an axis is logarithmic, rescaleAxes will make
sure not to rescale to an illegal range i.e. a range containing different signs and/or zero.
Instead it will stay in the current sign domain and ignore all parts of the plottable that lie
outside of that domain.
\a onlyEnlarge makes sure the ranges are only expanded, never reduced. So it's possible to show
multiple plottables in their entirety by multiple calls to rescaleAxes where the first call has
\a onlyEnlarge set to false (the default), and all subsequent set to true.
*/
void QCPAbstractPlottable::rescaleAxes(bool onlyEnlarge) const
{
rescaleKeyAxis(onlyEnlarge);
rescaleValueAxis(onlyEnlarge);
}
/*!
Rescales the key axis of the plottable so the whole plottable is visible.
See \ref rescaleAxes for detailed behaviour.
*/
void QCPAbstractPlottable::rescaleKeyAxis(bool onlyEnlarge) const
{
SignDomain signDomain = sdBoth;
if (mKeyAxis->scaleType() == QCPAxis::stLogarithmic)
signDomain = (mKeyAxis->range().upper < 0 ? sdNegative : sdPositive);
bool validRange;
QCPRange newRange = getKeyRange(validRange, signDomain);
if (validRange)
{
if (onlyEnlarge)
{
if (mKeyAxis->range().lower < newRange.lower)
newRange.lower = mKeyAxis->range().lower;
if (mKeyAxis->range().upper > newRange.upper)
newRange.upper = mKeyAxis->range().upper;
}
mKeyAxis->setRange(newRange);
}
}
/*!
Rescales the value axis of the plottable so the whole plottable is visible.
See \ref rescaleAxes for detailed behaviour.
*/
void QCPAbstractPlottable::rescaleValueAxis(bool onlyEnlarge) const
{
SignDomain signDomain = sdBoth;
if (mValueAxis->scaleType() == QCPAxis::stLogarithmic)
signDomain = (mValueAxis->range().upper < 0 ? sdNegative : sdPositive);
bool validRange;
QCPRange newRange = getValueRange(validRange, signDomain);
if (validRange)
{
if (onlyEnlarge)
{
if (mValueAxis->range().lower < newRange.lower)
newRange.lower = mValueAxis->range().lower;
if (mValueAxis->range().upper > newRange.upper)
newRange.upper = mValueAxis->range().upper;
}
mValueAxis->setRange(newRange);
}
}
/*!
Adds this plottable to the legend of the parent QCustomPlot.
Normally, a QCPPlottableLegendItem is created and inserted into the legend. If the plottable
needs a more specialized representation in the plot, this function will take this into account
and instead create the specialized subclass of QCPAbstractLegendItem.
Returns true on success, i.e. when a legend item associated with this plottable isn't already in
the legend.
\see removeFromLegend, QCPLegend::addItem
*/
bool QCPAbstractPlottable::addToLegend()
{
if (!mParentPlot->legend->hasItemWithPlottable(this))
{
mParentPlot->legend->addItem(new QCPPlottableLegendItem(mParentPlot->legend, this));
return true;
} else
return false;
}
/*!
Removes the plottable from the legend of the parent QCustomPlot. This means the
QCPAbstractLegendItem (usually a QCPPlottableLegendItem) that is associated with this plottable
is removed.
Returns true on success, i.e. if a legend item associated with this plottable was found and
removed from the legend.
\see addToLegend, QCPLegend::removeItem
*/
bool QCPAbstractPlottable::removeFromLegend() const
{
if (QCPPlottableLegendItem *lip = mParentPlot->legend->itemWithPlottable(this))
return mParentPlot->legend->removeItem(lip);
else
return false;
}
/* inherits documentation from base class */
QRect QCPAbstractPlottable::clipRect() const
{
return mKeyAxis->axisRect() | mValueAxis->axisRect();
}
/*! \internal
Convenience function for transforming a key/value pair to pixels on the QCustomPlot surface,
taking the orientations of the axes associated with this plottable into account (e.g. whether key
represents x or y).
\a key and \a value are transformed to the coodinates in pixels and are written to \a x and \a y.
\see pixelsToCoords, QCPAxis::coordToPixel
*/
void QCPAbstractPlottable::coordsToPixels(double key, double value, double &x, double &y) const
{
if (mKeyAxis->orientation() == Qt::Horizontal)
{
x = mKeyAxis->coordToPixel(key);
y = mValueAxis->coordToPixel(value);
} else
{
y = mKeyAxis->coordToPixel(key);
x = mValueAxis->coordToPixel(value);
}
}
/*! \internal
\overload
Returns the input as pixel coordinates in a QPointF.
*/
const QPointF QCPAbstractPlottable::coordsToPixels(double key, double value) const
{
if (mKeyAxis->orientation() == Qt::Horizontal)
return QPointF(mKeyAxis->coordToPixel(key), mValueAxis->coordToPixel(value));
else
return QPointF(mValueAxis->coordToPixel(value), mKeyAxis->coordToPixel(key));
}
/*! \internal
Convenience function for transforming a x/y pixel pair on the QCustomPlot surface to plot coordinates,
taking the orientations of the axes associated with this plottable into account (e.g. whether key
represents x or y).
\a x and \a y are transformed to the plot coodinates and are written to \a key and \a value.
\see coordsToPixels, QCPAxis::coordToPixel
*/
void QCPAbstractPlottable::pixelsToCoords(double x, double y, double &key, double &value) const
{
if (mKeyAxis->orientation() == Qt::Horizontal)
{
key = mKeyAxis->pixelToCoord(x);
value = mValueAxis->pixelToCoord(y);
} else
{
key = mKeyAxis->pixelToCoord(y);
value = mValueAxis->pixelToCoord(x);
}
}
/*! \internal
\overload
Returns the pixel input \a pixelPos as plot coordinates \a key and \a value.
*/
void QCPAbstractPlottable::pixelsToCoords(const QPointF &pixelPos, double &key, double &value) const
{
pixelsToCoords(pixelPos.x(), pixelPos.y(), key, value);
}
/*! \internal
Returns the pen that should be used for drawing lines of the plottable. Returns mPen when the
graph is not selected and mSelectedPen when it is.
*/
QPen QCPAbstractPlottable::mainPen() const
{
return mSelected ? mSelectedPen : mPen;
}
/*! \internal
Returns the brush that should be used for drawing fills of the plottable. Returns mBrush when the
graph is not selected and mSelectedBrush when it is.
*/
QBrush QCPAbstractPlottable::mainBrush() const
{
return mSelected ? mSelectedBrush : mBrush;
}
/*! \internal
A convenience function to easily set the QPainter::Antialiased hint on the provided \a painter
before drawing plottable lines.
This is the antialiasing state the painter passed to the \ref draw method is in by default.
This function takes into account the local setting of the antialiasing flag as well as
the overrides set e.g. with \ref QCustomPlot::setNotAntialiasedElements.
\see setAntialiased, applyFillAntialiasingHint, applyScattersAntialiasingHint, applyErrorBarsAntialiasingHint
*/
void QCPAbstractPlottable::applyDefaultAntialiasingHint(QCPPainter *painter) const
{
applyAntialiasingHint(painter, mAntialiased, QCP::aePlottables);
}
/*! \internal
A convenience function to easily set the QPainter::Antialiased hint on the provided \a painter
before drawing plottable fills.
This function takes into account the local setting of the fill antialiasing flag as well as
the overrides set e.g. with \ref QCustomPlot::setNotAntialiasedElements.
\see setAntialiased, applyDefaultAntialiasingHint, applyScattersAntialiasingHint, applyErrorBarsAntialiasingHint
*/
void QCPAbstractPlottable::applyFillAntialiasingHint(QCPPainter *painter) const
{
applyAntialiasingHint(painter, mAntialiasedFill, QCP::aeFills);
}
/*! \internal
A convenience function to easily set the QPainter::Antialiased hint on the provided \a painter
before drawing plottable scatter points.
This function takes into account the local setting of the scatters antialiasing flag as well as
the overrides set e.g. with \ref QCustomPlot::setNotAntialiasedElements.
\see setAntialiased, applyFillAntialiasingHint, applyDefaultAntialiasingHint, applyErrorBarsAntialiasingHint
*/
void QCPAbstractPlottable::applyScattersAntialiasingHint(QCPPainter *painter) const
{
applyAntialiasingHint(painter, mAntialiasedScatters, QCP::aeScatters);
}
/*! \internal
A convenience function to easily set the QPainter::Antialiased hint on the provided \a painter
before drawing plottable error bars.
This function takes into account the local setting of the error bars antialiasing flag as well as
the overrides set e.g. with \ref QCustomPlot::setNotAntialiasedElements.
\see setAntialiased, applyFillAntialiasingHint, applyScattersAntialiasingHint, applyDefaultAntialiasingHint
*/
void QCPAbstractPlottable::applyErrorBarsAntialiasingHint(QCPPainter *painter) const
{
applyAntialiasingHint(painter, mAntialiasedErrorBars, QCP::aeErrorBars);
}
/*! \internal
Finds the shortest squared distance of \a point to the line segment defined by \a start and \a
end.
This function may be used to help with the implementation of the \ref selectTest function for
specific plottables.
\note This function is identical to QCPAbstractItem::distSqrToLine
*/
double QCPAbstractPlottable::distSqrToLine(const QPointF &start, const QPointF &end, const QPointF &point) const
{
QVector2D a(start);
QVector2D b(end);
QVector2D p(point);
QVector2D v(b-a);
double vLengthSqr = v.lengthSquared();
if (!qFuzzyIsNull(vLengthSqr))
{
double mu = QVector2D::dotProduct(p-a, v)/vLengthSqr;
if (mu < 0)
return (a-p).lengthSquared();
else if (mu > 1)
return (b-p).lengthSquared();
else
return ((a + mu*v)-p).lengthSquared();
} else
return (a-p).lengthSquared();
}
// ================================================================================
// =================== QCPAbstractLegendItem
// ================================================================================
/*! \class QCPAbstractLegendItem
\brief The abstract base class for all items in a QCPLegend.
It defines a very basic interface to items in a QCPLegend. For representing plottables in the
legend, the subclass QCPPlottableLegendItem is more suitable.
Only derive directly from this class when you need absolute freedom (i.e. a legend item that's
not associated with a plottable).
You must implement the following pure virtual functions:
\li \ref draw
\li \ref size
You inherit the following members you may use: