Main Page · Class Overview · Hierarchy · All Classes · Special Pages
Public Types | Public Functions | Signals | Protected Functions
QCPLegend Class Reference

Manages a legend inside a QCustomPlot. More...

Inheritance diagram for QCPLegend:
Inheritance graph

Public Types

enum  SelectablePart
 
- Public Types inherited from QCPLayoutElement
enum  UpdatePhase
 

Public Functions

 QCPLegend ()
 
QPen borderPen () const
 
QBrush brush () const
 
QFont font () const
 
QColor textColor () const
 
QSize iconSize () const
 
int iconTextPadding () const
 
QPen iconBorderPen () const
 
SelectableParts selectableParts () const
 
SelectableParts selectedParts () const
 
QPen selectedBorderPen () const
 
QPen selectedIconBorderPen () const
 
QBrush selectedBrush () const
 
QFont selectedFont () const
 
QColor selectedTextColor () const
 
void setBorderPen (const QPen &pen)
 
void setBrush (const QBrush &brush)
 
void setFont (const QFont &font)
 
void setTextColor (const QColor &color)
 
void setIconSize (const QSize &size)
 
void setIconSize (int width, int height)
 
void setIconTextPadding (int padding)
 
void setIconBorderPen (const QPen &pen)
 
Q_SLOT void setSelectableParts (const SelectableParts &selectableParts)
 
Q_SLOT void setSelectedParts (const SelectableParts &selectedParts)
 
void setSelectedBorderPen (const QPen &pen)
 
void setSelectedIconBorderPen (const QPen &pen)
 
void setSelectedBrush (const QBrush &brush)
 
void setSelectedFont (const QFont &font)
 
void setSelectedTextColor (const QColor &color)
 
virtual double selectTest (const QPointF &pos, bool onlySelectable, QVariant *details=0) const
 
QCPAbstractLegendItemitem (int index) const
 
QCPPlottableLegendItemitemWithPlottable (const QCPAbstractPlottable *plottable) const
 
int itemCount () const
 
bool hasItem (QCPAbstractLegendItem *item) const
 
bool hasItemWithPlottable (const QCPAbstractPlottable *plottable) const
 
bool addItem (QCPAbstractLegendItem *item)
 
bool removeItem (int index)
 
bool removeItem (QCPAbstractLegendItem *item)
 
void clearItems ()
 
QList< QCPAbstractLegendItem * > selectedItems () const
 
- Public Functions inherited from QCPLayoutGrid
 QCPLayoutGrid ()
 
int rowCount () const
 
int columnCount () const
 
QList< double > columnStretchFactors () const
 
QList< double > rowStretchFactors () const
 
int columnSpacing () const
 
int rowSpacing () const
 
void setColumnStretchFactor (int column, double factor)
 
void setColumnStretchFactors (const QList< double > &factors)
 
void setRowStretchFactor (int row, double factor)
 
void setRowStretchFactors (const QList< double > &factors)
 
void setColumnSpacing (int pixels)
 
void setRowSpacing (int pixels)
 
virtual void updateLayout ()
 
virtual int elementCount () const
 
virtual QCPLayoutElementelementAt (int index) const
 
virtual QCPLayoutElementtakeAt (int index)
 
virtual bool take (QCPLayoutElement *element)
 
virtual QList< QCPLayoutElement * > elements (bool recursive) const
 
virtual void simplify ()
 
virtual QSize minimumSizeHint () const
 
virtual QSize maximumSizeHint () const
 
QCPLayoutElementelement (int row, int column) const
 
bool addElement (int row, int column, QCPLayoutElement *element)
 
bool hasElement (int row, int column)
 
void expandTo (int newRowCount, int newColumnCount)
 
void insertRow (int newIndex)
 
void insertColumn (int newIndex)
 
- Public Functions inherited from QCPLayout
 QCPLayout ()
 
virtual void update (UpdatePhase phase)
 
bool removeAt (int index)
 
bool remove (QCPLayoutElement *element)
 
void clear ()
 
- Public Functions inherited from QCPLayoutElement
 QCPLayoutElement (QCustomPlot *parentPlot=0)
 
QCPLayoutlayout () const
 
QRect rect () const
 
QRect outerRect () const
 
QMargins margins () const
 
QMargins minimumMargins () const
 
QCP::MarginSides autoMargins () const
 
QSize minimumSize () const
 
QSize maximumSize () const
 
QCPMarginGroupmarginGroup (QCP::MarginSide side) const
 
QHash< QCP::MarginSide,
QCPMarginGroup * > 
marginGroups () const
 
void setOuterRect (const QRect &rect)
 
void setMargins (const QMargins &margins)
 
void setMinimumMargins (const QMargins &margins)
 
void setAutoMargins (QCP::MarginSides sides)
 
void setMinimumSize (const QSize &size)
 
void setMinimumSize (int width, int height)
 
void setMaximumSize (const QSize &size)
 
void setMaximumSize (int width, int height)
 
void setMarginGroup (QCP::MarginSides sides, QCPMarginGroup *group)
 
- Public Functions inherited from QCPLayerable
 QCPLayerable (QCustomPlot *plot, QString targetLayer=QString(), QCPLayerable *parentLayerable=0)
 
bool visible () const
 
QCustomPlotparentPlot () const
 
QCPLayerableparentLayerable () const
 
QCPLayerlayer () const
 
bool antialiased () const
 
void setVisible (bool on)
 
Q_SLOT bool setLayer (QCPLayer *layer)
 
bool setLayer (const QString &layerName)
 
void setAntialiased (bool enabled)
 
bool realVisibility () const
 

Signals

void selectionChanged (QCPLegend::SelectableParts parts)
 
void selectableChanged (QCPLegend::SelectableParts parts)
 
- Signals inherited from QCPLayerable
void layerChanged (QCPLayer *newLayer)
 

Protected Functions

virtual void parentPlotInitialized (QCustomPlot *parentPlot)
 
virtual QCP::Interaction selectionCategory () const
 
virtual void applyDefaultAntialiasingHint (QCPPainter *painter) const
 
virtual void draw (QCPPainter *painter)
 
virtual void selectEvent (QMouseEvent *event, bool additive, const QVariant &details, bool *selectionStateChanged)
 
virtual void deselectEvent (bool *selectionStateChanged)
 
QPen getBorderPen () const
 
QBrush getBrush () const
 
- Protected Functions inherited from QCPLayoutGrid
void getMinimumRowColSizes (QVector< int > *minColWidths, QVector< int > *minRowHeights) const
 
void getMaximumRowColSizes (QVector< int > *maxColWidths, QVector< int > *maxRowHeights) const
 
- Protected Functions inherited from QCPLayout
void sizeConstraintsChanged () const
 
void adoptElement (QCPLayoutElement *el)
 
void releaseElement (QCPLayoutElement *el)
 
QVector< int > getSectionSizes (QVector< int > maxSizes, QVector< int > minSizes, QVector< double > stretchFactors, int totalSize) const
 
- Protected Functions inherited from QCPLayoutElement
virtual int calculateAutoMargin (QCP::MarginSide side)
 
virtual void mousePressEvent (QMouseEvent *event)
 
virtual void mouseMoveEvent (QMouseEvent *event)
 
virtual void mouseReleaseEvent (QMouseEvent *event)
 
virtual void mouseDoubleClickEvent (QMouseEvent *event)
 
virtual void wheelEvent (QWheelEvent *event)
 
- Protected Functions inherited from QCPLayerable
virtual QRect clipRect () const
 
void initializeParentPlot (QCustomPlot *parentPlot)
 
void setParentLayerable (QCPLayerable *parentLayerable)
 
bool moveToLayer (QCPLayer *layer, bool prepend)
 
void applyAntialiasingHint (QCPPainter *painter, bool localAntialiased, QCP::AntialiasedElement overrideElement) const
 

Detailed Description

Manages a legend inside a QCustomPlot.

A legend is a small box somewhere in the plot which lists plottables with their name and icon.

Normally, the legend is populated by calling QCPAbstractPlottable::addToLegend. The respective legend item can be removed with QCPAbstractPlottable::removeFromLegend. However, QCPLegend also offers an interface to add and manipulate legend items directly: item, itemWithPlottable, itemCount, addItem, removeItem, etc.

The QCPLegend derives from QCPLayoutGrid and as such can be placed in any position a QCPLayoutElement may be positioned. The legend items are themselves QCPLayoutElements which are placed in the grid layout of the legend. QCPLegend only adds an interface specialized for handling child elements of type QCPAbstractLegendItem, as mentioned above. In principle, any other layout elements may also be added to a legend via the normal QCPLayoutGrid interface. However, the QCPAbstractLegendItem-Interface will ignore those elements (e.g. itemCount will only return the number of items with QCPAbstractLegendItems type).

By default, every QCustomPlot has one legend (QCustomPlot::legend) which is placed in the inset layout of the main axis rect (QCPAxisRect::insetLayout). To move the legend to another position inside the axis rect, use the methods of the QCPLayoutInset. To move the legend outside of the axis rect, place it anywhere else with the QCPLayout/QCPLayoutElement interface.

Member Enumeration Documentation

Defines the selectable parts of a legend

See Also
setSelectedParts, setSelectableParts
Enumerator
spNone 

0x000 None

spLegendBox 

0x001 The legend box (frame)

spItems 

0x002 Legend items individually (see selectedItems)

Constructor & Destructor Documentation

QCPLegend::QCPLegend ( )
explicit

Constructs a new QCPLegend instance with parentPlot as the containing plot and default values.

Note that by default, QCustomPlot already contains a legend ready to be used as QCustomPlot::legend

Member Function Documentation

void QCPLegend::setBorderPen ( const QPen &  pen)

Sets the pen, the border of the entire legend is drawn with.

void QCPLegend::setBrush ( const QBrush &  brush)

Sets the brush of the legend background.

void QCPLegend::setFont ( const QFont &  font)

Sets the default font of legend text. Legend items that draw text (e.g. the name of a graph) will use this font by default. However, a different font can be specified on a per-item-basis by accessing the specific legend item.

This function will also set font on all already existing legend items.

See Also
QCPAbstractLegendItem::setFont
void QCPLegend::setTextColor ( const QColor &  color)

Sets the default color of legend text. Legend items that draw text (e.g. the name of a graph) will use this color by default. However, a different colors can be specified on a per-item-basis by accessing the specific legend item.

This function will also set color on all already existing legend items.

See Also
QCPAbstractLegendItem::setTextColor
void QCPLegend::setIconSize ( const QSize &  size)

Sets the size of legend icons. Legend items that draw an icon (e.g. a visual representation of the graph) will use this size by default.

void QCPLegend::setIconSize ( int  width,
int  height 
)

This is an overloaded function.

void QCPLegend::setIconTextPadding ( int  padding)

Sets the horizontal space in pixels between the legend icon and the text next to it. Legend items that draw an icon (e.g. a visual representation of the graph) and text (e.g. the name of the graph) will use this space by default.

void QCPLegend::setIconBorderPen ( const QPen &  pen)

Sets the pen used to draw a border around each legend icon. Legend items that draw an icon (e.g. a visual representation of the graph) will use this pen by default.

If no border is wanted, set this to Qt::NoPen.

void QCPLegend::setSelectableParts ( const SelectableParts &  selectable)

Sets whether the user can (de-)select the parts in selectable by clicking on the QCustomPlot surface. (When QCustomPlot::setInteractions contains QCP::iSelectLegend.)

However, even when selectable is set to a value not allowing the selection of a specific part, it is still possible to set the selection of this part manually, by calling setSelectedParts directly.

See Also
SelectablePart, setSelectedParts
void QCPLegend::setSelectedParts ( const SelectableParts &  selected)

Sets the selected state of the respective legend parts described by SelectablePart. When a part is selected, it uses a different pen/font and brush. If some legend items are selected and selected doesn't contain spItems, those items become deselected.

The entire selection mechanism is handled automatically when QCustomPlot::setInteractions contains iSelectLegend. You only need to call this function when you wish to change the selection state manually.

This function can change the selection state of a part even when setSelectableParts was set to a value that actually excludes the part.

emits the selectionChanged signal when selected is different from the previous selection state.

Note that it doesn't make sense to set the selected state spItems here when it wasn't set before, because there's no way to specify which exact items to newly select. Do this by calling QCPAbstractLegendItem::setSelected directly on the legend item you wish to select.

See Also
SelectablePart, setSelectableParts, selectTest, setSelectedBorderPen, setSelectedIconBorderPen, setSelectedBrush, setSelectedFont
void QCPLegend::setSelectedBorderPen ( const QPen &  pen)

When the legend box is selected, this pen is used to draw the border instead of the normal pen set via setBorderPen.

See Also
setSelectedParts, setSelectableParts, setSelectedBrush
void QCPLegend::setSelectedIconBorderPen ( const QPen &  pen)

Sets the pen legend items will use to draw their icon borders, when they are selected.

See Also
setSelectedParts, setSelectableParts, setSelectedFont
void QCPLegend::setSelectedBrush ( const QBrush &  brush)

When the legend box is selected, this brush is used to draw the legend background instead of the normal brush set via setBrush.

See Also
setSelectedParts, setSelectableParts, setSelectedBorderPen
void QCPLegend::setSelectedFont ( const QFont &  font)

Sets the default font that is used by legend items when they are selected.

This function will also set font on all already existing legend items.

See Also
setFont, QCPAbstractLegendItem::setSelectedFont
void QCPLegend::setSelectedTextColor ( const QColor &  color)

Sets the default text color that is used by legend items when they are selected.

This function will also set color on all already existing legend items.

See Also
setTextColor, QCPAbstractLegendItem::setSelectedTextColor
double QCPLegend::selectTest ( const QPointF &  pos,
bool  onlySelectable,
QVariant *  details = 0 
) const
virtual

Layout elements are sensitive to events inside their outer rect. If pos is within the outer rect, this method returns a value corresponding to 0.99 times the parent plot's selection tolerance. However, layout elements are not selectable by default. So if onlySelectable is true, -1.0 is returned.

See QCPLayerable::selectTest for a general explanation of this virtual method.

QCPLayoutElement subclasses may reimplement this method to provide more specific selection test behaviour.

Reimplemented from QCPLayoutElement.

QCPAbstractLegendItem * QCPLegend::item ( int  index) const

Returns the item with index i.

See Also
itemCount
QCPPlottableLegendItem * QCPLegend::itemWithPlottable ( const QCPAbstractPlottable plottable) const

Returns the QCPPlottableLegendItem which is associated with plottable (e.g. a QCPGraph*). If such an item isn't in the legend, returns 0.

See Also
hasItemWithPlottable
int QCPLegend::itemCount ( ) const

Returns the number of items currently in the legend.

See Also
item
bool QCPLegend::hasItem ( QCPAbstractLegendItem item) const

Returns whether the legend contains itm.

bool QCPLegend::hasItemWithPlottable ( const QCPAbstractPlottable plottable) const

Returns whether the legend contains a QCPPlottableLegendItem which is associated with plottable (e.g. a QCPGraph*). If such an item isn't in the legend, returns false.

See Also
itemWithPlottable
bool QCPLegend::addItem ( QCPAbstractLegendItem item)

Adds item to the legend, if it's not present already.

Returns true on sucess, i.e. if the item wasn't in the list already and has been successfuly added.

The legend takes ownership of the item.

bool QCPLegend::removeItem ( int  index)

Removes the item with index index from the legend.

Returns true, if successful.

See Also
itemCount, clearItems
bool QCPLegend::removeItem ( QCPAbstractLegendItem item)

This is an overloaded function.

Removes item from the legend.

Returns true, if successful.

See Also
clearItems
void QCPLegend::clearItems ( )

Removes all items from the legend.

QList< QCPAbstractLegendItem * > QCPLegend::selectedItems ( ) const

Returns the legend items that are currently selected. If no items are selected, the list is empty.

See Also
QCPAbstractLegendItem::setSelected, setSelectable
void QCPLegend::selectionChanged ( QCPLegend::SelectableParts  selection)
signal

This signal is emitted when the selection state of this legend has changed.

See Also
setSelectedParts, setSelectableParts
void QCPLegend::parentPlotInitialized ( QCustomPlot parentPlot)
protectedvirtual

propagates the parent plot initialization to all child elements, by calling QCPLayerable::initializeParentPlot on them.

Reimplemented from QCPLayoutElement.

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

Returns the selection category this layerable shall belong to. The selection category is used in conjunction with QCustomPlot::setInteractions to control which objects are selectable and which aren't.

Subclasses that don't fit any of the normal QCP::Interaction values can use QCP::iSelectOther. This is what the default implementation returns.

See Also
QCustomPlot::setInteractions

Reimplemented from QCPLayerable.

void QCPLegend::applyDefaultAntialiasingHint ( QCPPainter painter) const
protectedvirtual

A convenience function to easily set the QPainter::Antialiased hint on the provided painter before drawing main legend elements.

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 with QCustomPlot::setAntialiasedElements and QCustomPlot::setNotAntialiasedElements.

See Also
setAntialiased

Reimplemented from QCPLayoutElement.

void QCPLegend::draw ( QCPPainter painter)
protectedvirtual

Draws the legend box with the provided painter. The individual legend items are layerables themselves, thus are drawn independently.

Reimplemented from QCPLayoutElement.

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

This event is called when the layerable shall be selected, as a consequence of a click by the user. Subclasses should react to it by setting their selection state appropriately. The default implementation does nothing.

event is the mouse event that caused the selection. additive indicates, whether the user was holding the multi-select-modifier while performing the selection (see QCustomPlot::setMultiSelectModifier). if additive is true, the selection state must be toggled (i.e. become selected when unselected and unselected when selected).

Every selectEvent is preceded by a call to selectTest, which has returned positively (i.e. returned a value greater than 0 and less than the selection tolerance of the parent QCustomPlot). The details data you output from selectTest is fed back via details here. You may use it to transport any kind of information from the selectTest to the possibly subsequent selectEvent. Usually details is used to transfer which part was clicked, if it is a layerable that has multiple individually selectable parts (like QCPAxis). This way selectEvent doesn't need to do the calculation again to find out which part was actually clicked.

selectionStateChanged is an output parameter. If the pointer is non-null, this function must set the value either to true or false, depending on whether the selection state of this layerable was actually changed. For layerables that only are selectable as a whole and not in parts, this is simple: if additive is true, selectionStateChanged must also be set to true, because the selection toggles. If additive is false, selectionStateChanged is only set to true, if the layerable was previously unselected and now is switched to the selected state.

See Also
selectTest, deselectEvent

Reimplemented from QCPLayerable.

void QCPLegend::deselectEvent ( bool *  selectionStateChanged)
protectedvirtual

This event is called when the layerable shall be deselected, either as consequence of a user interaction or a call to QCustomPlot::deselectAll. Subclasses should react to it by unsetting their selection appropriately.

just as in selectEvent, the output parameter selectionStateChanged (if non-null), must return true or false when the selection state of this layerable has changed or not changed, respectively.

See Also
selectTest, selectEvent

Reimplemented from QCPLayerable.

QPen QCPLegend::getBorderPen ( ) const
protected

Returns the pen used to paint the border of the legend, taking into account the selection state of the legend box.

QBrush QCPLegend::getBrush ( ) const
protected

Returns the brush used to paint the background of the legend, taking into account the selection state of the legend box.


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