.Simulation
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Pages
Signals | Public Member Functions | Protected Member Functions | Protected Attributes | Friends | List of all members
QCPlot::QCPAbstractItem Class Referenceabstract

The abstract base class for all items in a plot. More...

Inheritance diagram for QCPlot::QCPAbstractItem:
QCPlot::QCPLayerable QCPlot::QCPItemBracket QCPlot::QCPItemCurve QCPlot::QCPItemEllipse QCPlot::QCPItemLine QCPlot::QCPItemPixmap QCPlot::QCPItemRect QCPlot::QCPItemStraightLine QCPlot::QCPItemText QCPlot::QCPItemTracer

Signals

void selectionChanged (bool selected)
 This signal is emitted when the selection state of this item has changed, either by user interaction or by a direct call to setSelected.
 

Public Member Functions

 QCPAbstractItem (QCustomPlot *parentPlot)
 Base class constructor which initializes base class members.
 
bool clipToAxisRect () const
 
QCPAxisclipKeyAxis () const
 
QCPAxisclipValueAxis () const
 
bool selectable () const
 
bool selected () const
 
void setClipToAxisRect (bool clip)
 Sets whether the item shall be clipped to the axis rect or whether it shall be visible on the entire QCustomPlot. More...
 
void setClipAxes (QCPAxis *keyAxis, QCPAxis *valueAxis)
 Sets both clip axes. More...
 
void setClipKeyAxis (QCPAxis *axis)
 Sets the clip key axis. More...
 
void setClipValueAxis (QCPAxis *axis)
 Sets the clip value axis. More...
 
void setSelectable (bool selectable)
 Sets whether the user can (de-)select this item by clicking on the QCustomPlot surface. More...
 
void setSelected (bool selected)
 Sets whether this item is selected or not. More...
 
virtual double selectTest (const QPointF &pos) const =0
 This function is used to decide whether a click hits an item or not. More...
 
QList< QCPItemPosition * > positions () const
 Returns all positions of the item in a list. More...
 
QList< QCPItemAnchor * > anchors () const
 Returns all anchors of the item in a list. More...
 
QCPItemPositionposition (const QString &name) const
 Returns the QCPItemPosition with the specified name. More...
 
QCPItemAnchoranchor (const QString &name) const
 Returns the QCPItemAnchor with the specified name. More...
 
bool hasAnchor (const QString &name) const
 Returns whether this item has an anchor with the specified name. More...
 
- Public Member Functions inherited from QCPlot::QCPLayerable
 QCPLayerable (QCustomPlot *parentPlot)
 Creates a new QCPLayerable instance. More...
 
bool visible () const
 
QCustomPlotparentPlot () const
 
QCPLayerlayer () const
 
bool antialiased () const
 
void setVisible (bool on)
 Sets the visibility of this layerable object. More...
 
bool setLayer (QCPLayer *layer)
 Sets the layer of this layerable object. More...
 
bool setLayer (const QString &layerName)
 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts. Sets the layer of this layerable object by name. More...
 
void setAntialiased (bool enabled)
 Sets whether this object will be drawn antialiased or not. More...
 

Protected Member 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
 
QCPItemPositioncreatePosition (const QString &name)
 
QCPItemAnchorcreateAnchor (const QString &name, int anchorId)
 
- Protected Member Functions inherited from QCPlot::QCPLayerable
bool moveToLayer (QCPLayer *layer, bool prepend)
 
void applyAntialiasingHint (QCPPainter *painter, bool localAntialiased, QCP::AntialiasedElement overrideElement) const
 

Protected Attributes

bool mClipToAxisRect
 
QCPAxismClipKeyAxis
 
QCPAxismClipValueAxis
 
bool mSelectable
 
bool mSelected
 
QList< QCPItemPosition * > mPositions
 
QList< QCPItemAnchor * > mAnchors
 
- Protected Attributes inherited from QCPlot::QCPLayerable
bool mVisible
 
QCustomPlotmParentPlot
 
QCPLayermLayer
 
bool mAntialiased
 

Friends

class QCustomPlot
 
class QCPItemAnchor
 

Detailed Description

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:

QCPItemLineA line defined by a start and an end point. May have different ending styles on each side (e.g. arrows).
QCPItemStraightLineA straight line defined by a start and a direction point. Unlike QCPItemLine, the straight line is infinitely long and has no endings.
QCPItemCurveA curve defined by start, end and two intermediate control points. May have different ending styles on each side (e.g. arrows).
QCPItemRectA rectangle
QCPItemEllipseAn ellipse
QCPItemPixmapAn arbitrary pixmap
QCPItemTextA text label
QCPItemBracketA bracket which may be used to reference/highlight certain parts in the plot.
QCPItemTracerAn item that can be attached to a QCPGraph and sticks to its data points, given a key coordinate.

Using items

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:

line->start->setCoords(-0.1, 0.8);
line->end->setCoords(1.1, 0.2);

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:

line->start->setCoords(100, 200);
line->end->setCoords(450, 320);

Creating own items

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.

Allowing the item to be positioned

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
}

The draw function

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.

The selectTest function

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

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.

Member Function Documentation

QCPItemAnchor * QCPlot::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).

See Also
anchors, position
QList< QCPItemAnchor * > QCPlot::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.

See Also
positions, anchor
bool QCPlot::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).

See Also
anchor, position
QCPItemPosition * QCPlot::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).

See Also
positions, anchor
QList< QCPItemPosition * > QCPlot::QCPAbstractItem::positions ( ) const
inline

Returns all positions of the item in a list.

See Also
anchors, position
double QCPlot::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.

See Also
setSelected, QCustomPlot::setInteractions

Implemented in QCPlot::QCPItemTracer, QCPlot::QCPItemBracket, QCPlot::QCPItemCurve, QCPlot::QCPItemText, QCPlot::QCPItemPixmap, QCPlot::QCPItemRect, QCPlot::QCPItemEllipse, QCPlot::QCPItemLine, and QCPlot::QCPItemStraightLine.

void QCPlot::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.

See Also
setClipToAxisRect, setClipKeyAxis, setClipValueAxis
void QCPlot::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.

See Also
setClipToAxisRect, setClipAxes, setClipValueAxis
void QCPlot::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 QCPlot::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.

See Also
setClipToAxisRect, setClipAxes, setClipKeyAxis
void QCPlot::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.

See Also
QCustomPlot::setInteractions, setSelected
void QCPlot::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.

See Also
selectTest

The documentation for this class was generated from the following files: