iCub-main
Loading...
Searching...
No Matches
Signals | Public Member Functions | Protected Member Functions | Protected Attributes | Friends | List of all members
QCPAbstractItem Class Referenceabstract

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

#include <qcustomplot.h>

+ Inheritance diagram for QCPAbstractItem:

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.
 
void selectableChanged (bool selectable)
 
- Signals inherited from QCPLayerable
void layerChanged (QCPLayer *newLayer)
 This signal is emitted when the layer of this layerable changes, i.e.
 

Public Member Functions

 QCPAbstractItem (QCustomPlot *parentPlot)
 Base class constructor which initializes base class members.
 
virtual ~QCPAbstractItem ()
 
bool clipToAxisRect () const
 
QCPAxisRectclipAxisRect () const
 
bool selectable () const
 
bool selected () const
 
void setClipToAxisRect (bool clip)
 Sets whether the item shall be clipped to an axis rect or whether it shall be visible on the entire QCustomPlot.
 
void setClipAxisRect (QCPAxisRect *rect)
 Sets the clip axis rect.
 
Q_SLOT void setSelectable (bool selectable)
 Sets whether the user can (de-)select this item by clicking on the QCustomPlot surface.
 
Q_SLOT void setSelected (bool selected)
 Sets whether this item is selected or not.
 
virtual double selectTest (const QPointF &pos, bool onlySelectable, QVariant *details=0) const =0
 This function is used to decide whether a click hits a layerable object or not.
 
QList< QCPItemPosition * > positions () const
 Returns all positions of the item in a list.
 
QList< QCPItemAnchor * > anchors () const
 Returns all anchors of the item in a list.
 
QCPItemPositionposition (const QString &name) const
 Returns the QCPItemPosition with the specified name.
 
QCPItemAnchoranchor (const QString &name) const
 Returns the QCPItemAnchor with the specified name.
 
bool hasAnchor (const QString &name) const
 Returns whether this item has an anchor with the specified name.
 
- Public Member Functions inherited from QCPLayerable
 QCPLayerable (QCustomPlot *plot, QString targetLayer="", QCPLayerable *parentLayerable=0)
 Creates a new QCPLayerable instance.
 
 ~QCPLayerable ()
 
bool visible () const
 
QCustomPlotparentPlot () const
 
QCPLayerableparentLayerable () const
 Returns the parent layerable of this layerable.
 
QCPLayerlayer () const
 
bool antialiased () const
 
void setVisible (bool on)
 Sets the visibility of this layerable object.
 
Q_SLOT bool setLayer (QCPLayer *layer)
 Sets the layer of this layerable object.
 
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.
 
void setAntialiased (bool enabled)
 Sets whether this object will be drawn antialiased or not.
 
bool realVisibility () const
 Returns whether this layerable is visible, taking the visibility of the layerable parent and the visibility of the layer this layerable is on into account.
 

Protected Member Functions

virtual QCP::Interaction selectionCategory () const
 
virtual QRect clipRect () const
 
virtual void applyDefaultAntialiasingHint (QCPPainter *painter) const
 
virtual void draw (QCPPainter *painter)=0
 
virtual void selectEvent (QMouseEvent *event, bool additive, const QVariant &details, bool *selectionStateChanged)
 
virtual void deselectEvent (bool *selectionStateChanged)
 
virtual QPointF anchorPixelPoint (int anchorId) const
 
double distSqrToLine (const QPointF &start, const QPointF &end, const QPointF &point) const
 
double rectSelectTest (const QRectF &rect, const QPointF &pos, bool filledRect) const
 
QCPItemPositioncreatePosition (const QString &name)
 
QCPItemAnchorcreateAnchor (const QString &name, int anchorId)
 
- Protected Member Functions inherited from QCPLayerable
virtual void parentPlotInitialized (QCustomPlot *parentPlot)
 
void initializeParentPlot (QCustomPlot *parentPlot)
 
void setParentLayerable (QCPLayerable *parentLayerable)
 
bool moveToLayer (QCPLayer *layer, bool prepend)
 
void applyAntialiasingHint (QCPPainter *painter, bool localAntialiased, QCP::AntialiasedElement overrideElement) const
 

Protected Attributes

bool mClipToAxisRect
 
QPointer< QCPAxisRectmClipAxisRect
 
QList< QCPItemPosition * > mPositions
 
QList< QCPItemAnchor * > mAnchors
 
bool mSelectable
 
bool mSelected
 
- Protected Attributes inherited from QCPLayerable
bool mVisible
 
QCustomPlotmParentPlot
 
QPointer< QCPLayerablemParentLayerable
 
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.

Items are by default clipped to the main axis rect. To make an item visible outside that axis rect, disable clipping via setClipToAxisRect.

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);
A line from one point to another.

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);
QCPItemPosition *const end
QCPItemPosition *const start
void setCoords(double key, double value)
Sets the coordinates of this QCPItemPosition.

If we don't want the line to be positioned in plot coordinates but a different coordinate system, e.g. absolute pixel positions on the QCustomPlot surface, we need to change the position type like this:

void setType(PositionType type)
Sets the type of the position.
@ ptAbsolute
Static positioning in pixels, starting from the top left corner of the viewport/widget.

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 point 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;
Manages the position of an item.

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) :
myPosition(createPosition("myPosition"))
{
// other constructor code
}
The abstract base class for all items in a plot.
QCPItemPosition * createPosition(const QString &name)
QCustomPlot * parentPlot() const
The central class of the library.

The draw function

To give your item a visual representation, reimplement the draw function and use the passed QCPPainter to draw the item. You can retrieve the item 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. See the documentation of selectTest for what the function parameters mean and what the function should return.

Providing anchors

Providing anchors (QCPItemAnchor) starts off like adding a position. First you create a public member, e.g.

QCPItemAnchor * const bottom;
An anchor of an item to which positions can be attached to.

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.

Definition at line 1583 of file qcustomplot.h.

Constructor & Destructor Documentation

◆ QCPAbstractItem()

QCPAbstractItem::QCPAbstractItem ( QCustomPlot parentPlot)

Base class constructor which initializes base class members.

Definition at line 8204 of file qcustomplot.cpp.

◆ ~QCPAbstractItem()

QCPAbstractItem::~QCPAbstractItem ( )
virtual

Definition at line 8218 of file qcustomplot.cpp.

Member Function Documentation

◆ anchor()

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).

See also
anchors, position

Definition at line 8328 of file qcustomplot.cpp.

◆ anchorPixelPoint()

QPointF QCPAbstractItem::anchorPixelPoint ( int  anchorId) const
protectedvirtual

Reimplemented in QCPItemRect, QCPItemText, QCPItemEllipse, QCPItemPixmap, and QCPItemBracket.

Definition at line 8478 of file qcustomplot.cpp.

◆ anchors()

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.

See also
positions, anchor

Definition at line 1613 of file qcustomplot.h.

◆ applyDefaultAntialiasingHint()

void QCPAbstractItem::applyDefaultAntialiasingHint ( QCPPainter painter) const
protectedvirtual

Implements QCPLayerable.

Definition at line 8387 of file qcustomplot.cpp.

◆ clipAxisRect()

QCPAxisRect * QCPAbstractItem::clipAxisRect ( ) const

Definition at line 8225 of file qcustomplot.cpp.

◆ clipRect()

QRect QCPAbstractItem::clipRect ( ) const
protectedvirtual

Reimplemented from QCPLayerable.

Definition at line 8366 of file qcustomplot.cpp.

◆ clipToAxisRect()

bool QCPAbstractItem::clipToAxisRect ( ) const
inline

Definition at line 1597 of file qcustomplot.h.

◆ createAnchor()

QCPItemAnchor * QCPAbstractItem::createAnchor ( const QString &  name,
int  anchorId 
)
protected

Definition at line 8532 of file qcustomplot.cpp.

◆ createPosition()

QCPItemPosition * QCPAbstractItem::createPosition ( const QString &  name)
protected

Definition at line 8498 of file qcustomplot.cpp.

◆ deselectEvent()

void QCPAbstractItem::deselectEvent ( bool *  selectionStateChanged)
protectedvirtual

Reimplemented from QCPLayerable.

Definition at line 8556 of file qcustomplot.cpp.

◆ distSqrToLine()

double QCPAbstractItem::distSqrToLine ( const QPointF &  start,
const QPointF &  end,
const QPointF &  point 
) const
protected

Definition at line 8404 of file qcustomplot.cpp.

◆ draw()

void QCPAbstractItem::draw ( QCPPainter painter)
protectedpure virtual

◆ hasAnchor()

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. This is because every position is also an anchor (QCPItemPosition inherits from QCPItemAnchor).

See also
anchor, position

Definition at line 8347 of file qcustomplot.cpp.

◆ position()

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).

See also
positions, anchor

Definition at line 8307 of file qcustomplot.cpp.

◆ positions()

QList< QCPItemPosition * > QCPAbstractItem::positions ( ) const
inline

Returns all positions of the item in a list.

See also
anchors, position

Definition at line 1612 of file qcustomplot.h.

◆ rectSelectTest()

double QCPAbstractItem::rectSelectTest ( const QRectF &  rect,
const QPointF &  pos,
bool  filledRect 
) const
protected

Definition at line 8442 of file qcustomplot.cpp.

◆ selectable()

bool QCPAbstractItem::selectable ( ) const
inline

Definition at line 1599 of file qcustomplot.h.

◆ selectableChanged

void QCPAbstractItem::selectableChanged ( bool  selectable)
signal

◆ selected()

bool QCPAbstractItem::selected ( ) const
inline

Definition at line 1600 of file qcustomplot.h.

◆ selectEvent()

void QCPAbstractItem::selectEvent ( QMouseEvent *  event,
bool  additive,
const QVariant &  details,
bool *  selectionStateChanged 
)
protectedvirtual

Reimplemented from QCPLayerable.

Definition at line 8542 of file qcustomplot.cpp.

◆ selectionCategory()

QCP::Interaction QCPAbstractItem::selectionCategory ( ) const
protectedvirtual

Reimplemented from QCPLayerable.

Definition at line 8568 of file qcustomplot.cpp.

◆ selectionChanged

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.

◆ selectTest()

virtual double QCPAbstractItem::selectTest ( const QPointF &  pos,
bool  onlySelectable,
QVariant *  details = 0 
) const
pure virtual

This function is used to decide whether a click hits a layerable object 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 object. If the object is either invisible or the distance couldn't be determined, -1.0 is returned. Further, if onlySelectable is true and the object is not selectable, -1.0 is returned, too.

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 the selectionTolerance of the parent QCustomPlot multiplied by 0.99). 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, and the finally selected object is notified via the selectEvent/deselectEvent methods.

details is an optional output parameter. Every layerable subclass may place any information in details. This information will be passed to selectEvent when the parent QCustomPlot decides on the basis of this selectTest call, that the object was successfully selected. The subsequent call to selectEvent will carry the details. This is useful for multi-part objects (like QCPAxis). This way, a possibly complex calculation to decide which part was clicked is only done once in selectTest. The result (i.e. the actually clicked part) can then be placed in details. So in the subsequent selectEvent, the decision which part was selected doesn't have to be done a second time for a single selection operation.

You may pass 0 as details to indicate that you are not interested in those selection details.

See also
selectEvent, deselectEvent, QCustomPlot::setInteractions

Reimplemented from QCPLayerable.

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

◆ setClipAxisRect()

void QCPAbstractItem::setClipAxisRect ( QCPAxisRect rect)

Sets the clip axis rect.

It defines the rect that will be used to clip the item when setClipToAxisRect is set to true.

See also
setClipToAxisRect

Definition at line 8249 of file qcustomplot.cpp.

◆ setClipToAxisRect()

void QCPAbstractItem::setClipToAxisRect ( bool  clip)

Sets whether the item shall be clipped to an axis rect or whether it shall be visible on the entire QCustomPlot.

The axis rect can be set with setClipAxisRect.

See also
setClipAxisRect

Definition at line 8236 of file qcustomplot.cpp.

◆ setSelectable()

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.

See also
QCustomPlot::setInteractions, setSelected

Definition at line 8265 of file qcustomplot.cpp.

◆ setSelected()

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.

See also
setSelectable, selectTest

Definition at line 8288 of file qcustomplot.cpp.

Friends And Related Symbol Documentation

◆ QCPItemAnchor

friend class QCPItemAnchor
friend

Definition at line 1652 of file qcustomplot.h.

◆ QCustomPlot

friend class QCustomPlot
friend

Definition at line 1651 of file qcustomplot.h.

Member Data Documentation

◆ mAnchors

QList<QCPItemAnchor*> QCPAbstractItem::mAnchors
protected

Definition at line 1627 of file qcustomplot.h.

◆ mClipAxisRect

QPointer<QCPAxisRect> QCPAbstractItem::mClipAxisRect
protected

Definition at line 1625 of file qcustomplot.h.

◆ mClipToAxisRect

bool QCPAbstractItem::mClipToAxisRect
protected

Definition at line 1624 of file qcustomplot.h.

◆ mPositions

QList<QCPItemPosition*> QCPAbstractItem::mPositions
protected

Definition at line 1626 of file qcustomplot.h.

◆ mSelectable

bool QCPAbstractItem::mSelectable
protected

Definition at line 1628 of file qcustomplot.h.

◆ mSelected

bool QCPAbstractItem::mSelected
protected

Definition at line 1628 of file qcustomplot.h.


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