The abstract base class for all items in a plot. More...
Public Functions | |
QCPAbstractItem (QCustomPlot *parentPlot) | |
bool | clipToAxisRect () const |
QCPAxis * | clipKeyAxis () const |
QCPAxis * | clipValueAxis () const |
bool | selectable () const |
bool | selected () const |
void | setClipToAxisRect (bool clip) |
void | setClipAxes (QCPAxis *keyAxis, QCPAxis *valueAxis) |
void | setClipKeyAxis (QCPAxis *axis) |
void | setClipValueAxis (QCPAxis *axis) |
void | setSelectable (bool selectable) |
void | setSelected (bool selected) |
virtual double | selectTest (const QPointF &pos) const =0 |
QList< QCPItemPosition * > | positions () const |
QList< QCPItemAnchor * > | anchors () const |
QCPItemPosition * | position (const QString &name) const |
QCPItemAnchor * | anchor (const QString &name) const |
bool | hasAnchor (const QString &name) const |
bool | visible () const |
QCustomPlot * | parentPlot () const |
QCPLayer * | layer () const |
bool | antialiased () const |
void | setVisible (bool on) |
bool | setLayer (QCPLayer *layer) |
bool | setLayer (const QString &layerName) |
void | setAntialiased (bool enabled) |
Signals | |
void | selectionChanged (bool selected) |
Protected Functions | |
virtual QRect | clipRect () const |
virtual void | applyDefaultAntialiasingHint (QCPPainter *painter) const |
virtual void | draw (QCPPainter *painter)=0 |
double | distSqrToLine (const QPointF &start, const QPointF &end, const QPointF &point) const |
double | rectSelectTest (const QRectF &rect, const QPointF &pos, bool filledRect) const |
virtual QPointF | anchorPixelPoint (int anchorId) const |
QCPItemPosition * | createPosition (const QString &name) |
QCPItemAnchor * | createAnchor (const QString &name, int anchorId) |
bool | moveToLayer (QCPLayer *layer, bool prepend) |
void | applyAntialiasingHint (QCPPainter *painter, bool localAntialiased, QCP::AntialiasedElement overrideElement) const |
The abstract base class for all items in a plot.
In QCustomPlot, items are supplemental graphical elements that are neither plottables (QCPAbstractPlottable) nor axes (QCPAxis). While plottables are always tied to two axes and thus plot coordinates, items can also be placed in absolute coordinates independent of any axes. Each specific item has at least one QCPItemPosition member which controls the positioning. Some items are defined by more than one coordinate and thus have two or more QCPItemPosition members (For example, QCPItemRect has topLeft and bottomRight).
This abstract base class defines a very basic interface like visibility and clipping. Since this class is abstract, it can't be instantiated. Use one of the subclasses or create a subclass yourself to create new items.
The built-in items are:
QCPItemLine | A line defined by a start and an end point. May have different ending styles on each side (e.g. arrows). |
QCPItemStraightLine | A straight line defined by a start and a direction point. Unlike QCPItemLine, the straight line is infinitely long and has no endings. |
QCPItemCurve | A curve defined by start, end and two intermediate control points. May have different ending styles on each side (e.g. arrows). |
QCPItemRect | A rectangle |
QCPItemEllipse | An ellipse |
QCPItemPixmap | An arbitrary pixmap |
QCPItemText | A text label |
QCPItemBracket | A bracket which may be used to reference/highlight certain parts in the plot. |
QCPItemTracer | An item that can be attached to a QCPGraph and sticks to its data points, given a key coordinate. |
First you instantiate the item you want to use and add it to the plot:
QCPItemLine *line = new QCPItemLine(customPlot); customPlot->addItem(line);
by default, the positions of the item are bound to the x- and y-Axis of the plot. So we can just set the plot coordinates where the line should start/end:
If we wanted the line to be positioned not in plot coordinates but a different coordinate system, e.g. absolute pixel positions on the QCustomPlot surface, we would have changed the position type like this:
line->start->setType(QCPItemPosition::ptAbsolute); line->end->setType(QCPItemPosition::ptAbsolute);
Then we can set the coordinates, this time in pixels:
To create an own item, you implement a subclass of QCPAbstractItem. These are the pure virtual functions, you must implement:
See the documentation of those functions for what they need to do.
As mentioned, item positions are represented by QCPItemPosition members. Let's assume the new item shall have only one coordinate as its position (as opposed to two like a rect or multiple like a polygon). You then add a public member of type QCPItemPosition like so:
QCPItemPosition * const myPosition;
the const makes sure the pointer itself can't be modified from the user of your new item (the QCPItemPosition instance it points to, can be modified, of course). The initialization of this pointer is made easy with the createPosition function. Just assign the return value of this function to each QCPItemPosition in the constructor of your item. createPosition takes a string which is the name of the position, typically this is identical to the variable name. For example, the constructor of QCPItemExample could look like this:
QCPItemExample::QCPItemExample(QCustomPlot *parentPlot) : QCPAbstractItem(parentPlot), myPosition(createPosition("myPosition")) { // other constructor code }
Your implementation of the draw function should check whether the item is visible (mVisible) and then draw the item. You can retrieve its position in pixel coordinates from the position member(s) via QCPItemPosition::pixelPoint.
To optimize performance you should calculate a bounding rect first (don't forget to take the pen width into account), check whether it intersects the clipRect, and only draw the item at all if this is the case.
Your implementation of the selectTest function may use the helpers distSqrToLine and rectSelectTest. With these, the implementation of the selection test becomes significantly simpler for most items.
Providing anchors (QCPItemAnchor) starts off like adding a position. First you create a public member, e.g.
QCPItemAnchor * const bottom;
and create it in the constructor with the createAnchor function, assigning it a name and an anchor id (an integer enumerating all anchors on the item, you may create an own enum for this). Since anchors can be placed anywhere, relative to the item's position(s), your item needs to provide the position of every anchor with the reimplementation of the anchorPixelPoint(int anchorId) function.
In essence the QCPItemAnchor is merely an intermediary that itself asks your item for the pixel position when anything attached to the anchor needs to know the coordinates.
QCPAbstractItem::QCPAbstractItem | ( | QCustomPlot * | parentPlot | ) |
Base class constructor which initializes base class members.
void QCPAbstractItem::setClipToAxisRect | ( | bool | clip | ) |
Sets whether the item shall be clipped to the axis rect or whether it shall be visible on the entire QCustomPlot. The axis rect is defined by the clip axes which can be set via setClipAxes or individually with setClipKeyAxis and setClipValueAxis.
void QCPAbstractItem::setClipAxes | ( | QCPAxis * | keyAxis, |
QCPAxis * | valueAxis | ||
) |
Sets both clip axes. Together they define the axis rect that will be used to clip the item when setClipToAxisRect is set to true.
void QCPAbstractItem::setClipKeyAxis | ( | QCPAxis * | axis | ) |
Sets the clip key axis. Together with the clip value axis it defines the axis rect that will be used to clip the item when setClipToAxisRect is set to true.
void QCPAbstractItem::setClipValueAxis | ( | QCPAxis * | axis | ) |
Sets the clip value axis. Together with the clip key axis it defines the axis rect that will be used to clip the item when setClipToAxisRect is set to true.
void QCPAbstractItem::setSelectable | ( | bool | selectable | ) |
Sets whether the user can (de-)select this item by clicking on the QCustomPlot surface. (When QCustomPlot::setInteractions contains QCustomPlot::iSelectItems.)
However, even when selectable was set to false, it is possible to set the selection manually, by calling setSelected directly.
void QCPAbstractItem::setSelected | ( | bool | selected | ) |
Sets whether this item is selected or not. When selected, it might use a different visual appearance (e.g. pen and brush), this depends on the specific item, though.
The entire selection mechanism for items is handled automatically when QCustomPlot::setInteractions contains QCustomPlot::iSelectItems. 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 setSelectable was set to false.
emits the selectionChanged signal when selected is different from the previous selection state.
double QCPAbstractItem::selectTest | ( | const QPointF & | pos | ) | const [pure virtual] |
This function is used to decide whether a click hits an item or not.
pos is a point in pixel coordinates on the QCustomPlot surface. This function returns the shortest pixel distance of this point to the item. If the item is either invisible or the distance couldn't be determined, -1.0 is returned. setSelectable has no influence on the return value of this function.
If the item is represented not by single lines but by an area like QCPItemRect or QCPItemText, 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.
Implemented in QCPItemTracer, QCPItemBracket, QCPItemCurve, QCPItemText, QCPItemPixmap, QCPItemRect, QCPItemEllipse, QCPItemLine, and QCPItemStraightLine.
QList< QCPItemPosition * > QCPAbstractItem::positions | ( | ) | const [inline] |
QList< QCPItemAnchor * > QCPAbstractItem::anchors | ( | ) | const [inline] |
Returns all anchors of the item in a list. Note that since a position (QCPItemPosition) is always also an anchor, the list will also contain the positions of this item.
QCPItemPosition * QCPAbstractItem::position | ( | const QString & | name | ) | const |
Returns the QCPItemPosition with the specified name. If this item doesn't have a position by that name, returns 0.
This function provides an alternative way to access item positions. Normally, you access positions direcly by their member pointers (which typically have the same variable name as name).
QCPItemAnchor * QCPAbstractItem::anchor | ( | const QString & | name | ) | const |
Returns the QCPItemAnchor with the specified name. If this item doesn't have an anchor by that name, returns 0.
This function provides an alternative way to access item anchors. Normally, you access anchors direcly by their member pointers (which typically have the same variable name as name).
bool QCPAbstractItem::hasAnchor | ( | const QString & | name | ) | const |
Returns whether this item has an anchor with the specified name.
Note that you can check for positions with this function, too, because every position is also an anchor (QCPItemPosition inherits from QCPItemAnchor).
QRect QCPAbstractItem::clipRect | ( | ) | const [protected, virtual] |
Returns the rect the visual representation of this item is clipped to. This depends on the current setting of setClipToAxisRect aswell as the clip axes set with setClipAxes.
If the item is not clipped to an axis rect, the QCustomPlot::viewport rect is returned.
Reimplemented from QCPLayerable.
void QCPAbstractItem::applyDefaultAntialiasingHint | ( | QCPPainter * | painter | ) | const [protected, virtual] |
A convenience function to easily set the QPainter::Antialiased hint on the provided painter before drawing item lines.
This is the antialiasing state the painter passed to the 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 QCustomPlot::setNotAntialiasedElements.
Implements QCPLayerable.
void QCPAbstractItem::draw | ( | QCPPainter * | painter | ) | [protected, pure virtual] |
Draws this item with the provided painter. Called by QCustomPlot::draw on all its visible items.
The cliprect of the provided painter is set to the rect returned by clipRect before this function is called. For items this depends on the clipping settings defined by setClipToAxisRect, setClipKeyAxis and setClipValueAxis.
Implements QCPLayerable.
Implemented in QCPItemTracer, QCPItemBracket, QCPItemCurve, QCPItemText, QCPItemPixmap, QCPItemRect, QCPItemEllipse, QCPItemLine, and QCPItemStraightLine.
double QCPAbstractItem::distSqrToLine | ( | const QPointF & | start, |
const QPointF & | end, | ||
const QPointF & | point | ||
) | const [protected] |
Finds the shortest squared distance of point to the line segment defined by start and end.
This function may be used to help with the implementation of the selectTest function for specific items.
double QCPAbstractItem::rectSelectTest | ( | const QRectF & | rect, |
const QPointF & | pos, | ||
bool | filledRect | ||
) | const [protected] |
A convenience function which returns the selectTest value for a specified rect and a specified click position pos. filledRect defines whether a click inside the rect should also be considered a hit or whether only the rect border is sensitive to hits.
This function may be used to help with the implementation of the selectTest function for specific items.
For example, if your item consists of four rects, call this function four times, once for each rect, in your selectTest reimplementation. Finally, return the minimum of all four returned values which were greater or equal to zero. (Because this function may return -1.0 when pos doesn't hit rect at all). If all calls returned -1.0, return -1.0, too, because your item wasn't hit.
QPointF QCPAbstractItem::anchorPixelPoint | ( | int | anchorId | ) | const [protected, virtual] |
Returns the pixel position of the anchor with Id anchorId. This function must be reimplemented in item subclasses if they want to provide anchors (QCPItemAnchor).
For example, if the item has two anchors with id 0 and 1, this function takes one of these anchor ids and returns the respective pixel points of the specified anchor.
Reimplemented in QCPItemBracket, QCPItemText, QCPItemPixmap, QCPItemRect, and QCPItemEllipse.
QCPItemPosition * QCPAbstractItem::createPosition | ( | const QString & | name | ) | [protected] |
Creates a QCPItemPosition, registers it with this item and returns a pointer to it. The specified name must be a unique string that is usually identical to the variable name of the position member (This is needed to provide the name based position access to positions).
Don't delete positions created by this function manually, as the item will take care of it.
Use this function in the constructor (initialization list) of the specific item subclass to create each position member. Don't create QCPItemPositions with new yourself, because they won't be registered with the item properly.
QCPItemAnchor * QCPAbstractItem::createAnchor | ( | const QString & | name, |
int | anchorId | ||
) | [protected] |
Creates a QCPItemAnchor, registers it with this item and returns a pointer to it. The specified name must be a unique string that is usually identical to the variable name of the anchor member (This is needed to provide the name based anchor access to anchors).
The anchorId must be a number identifying the created anchor. It is recommended to create an enum (e.g. "AnchorIndex") for this on each item that uses anchors. This id is used by the anchor to identify itself when it calls QCPAbstractItem::anchorPixelPoint. That function then returns the correct pixel coordinates for the passed anchor id.
Don't delete anchors created by this function manually, as the item will take care of it.
Use this function in the constructor (initialization list) of the specific item subclass to create each anchor member. Don't create QCPItemAnchors with new yourself, because then they won't be registered with the item properly.
void QCPAbstractItem::selectionChanged | ( | bool | selected | ) | [signal] |
This signal is emitted when the selection state of this item has changed, either by user interaction or by a direct call to setSelected.
void QCPLayerable::setVisible | ( | bool | on | ) | [inherited] |
Sets the visibility of this layerable object. If an object is not visible, it will not be drawn on the QCustomPlot surface, and user interaction with it (e.g. click/selection) is not possible.
bool QCPLayerable::setLayer | ( | QCPLayer * | layer | ) | [inherited] |
Sets the layer of this layerable object. The object will be placed on top of the other objects already on layer.
Returns true on success, i.e. if layer is a valid layer.
bool QCPLayerable::setLayer | ( | const QString & | layerName | ) | [inherited] |
This is an overloaded function.
Sets the layer of this layerable object by name
Returns true on success, i.e. if layerName is a valid layer name.
void QCPLayerable::setAntialiased | ( | bool | enabled | ) | [inherited] |
Sets whether this object will be drawn antialiased or not.
Note that antialiasing settings may be overridden by QCustomPlot::setAntialiasedElements and QCustomPlot::setNotAntialiasedElements.
bool QCPLayerable::moveToLayer | ( | QCPLayer * | layer, |
bool | prepend | ||
) | [protected, inherited] |
Moves this layerable object to layer. If prepend is true, this object will be prepended to the new layer's list, i.e. it will be drawn below the objects already on the layer. If it is false, the object will be appended.
Returns true on success, i.e. if layer is a valid layer.
void QCPLayerable::applyAntialiasingHint | ( | QCPPainter * | painter, |
bool | localAntialiased, | ||
QCP::AntialiasedElement | overrideElement | ||
) | const [protected, inherited] |
Sets the QPainter::Antialiasing render hint on the provided painter, depending on the localAntialiased value as well as the overrides QCustomPlot::setAntialiasedElements and QCustomPlot::setNotAntialiasedElements. Which override enum this function takes into account is controlled via overrideElement.