Cute Chess 0.1
QCPLegend Class Reference

Manages a legend inside a QCustomPlot. More...

Inheritance diagram for QCPLegend:
QCPLayoutGrid QCPLayout QCPLayoutElement QCPLayerable QObject

Public Types

enum  SelectablePart { spNone = 0x000 , spLegendBox = 0x001 , spItems = 0x002 }
Public Types inherited from QCPLayoutGrid
enum  FillOrder { foRowsFirst , foColumnsFirst }
Public Types inherited from QCPLayoutElement
enum  SizeConstraintRect { scrInnerRect , scrOuterRect }
enum  UpdatePhase { upPreparation , upMargins , upLayout }

Signals

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

Public Member Functions

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

Protected Member Functions

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

Protected Attributes

QPen mBorderPen
QBrush mBrush
QFont mFont
QPen mIconBorderPen
QSize mIconSize
int mIconTextPadding
SelectableParts mSelectableParts
QPen mSelectedBorderPen
QBrush mSelectedBrush
QFont mSelectedFont
QPen mSelectedIconBorderPen
SelectableParts mSelectedParts
QColor mSelectedTextColor
QColor mTextColor
Protected Attributes inherited from QCPLayoutGrid
int mColumnSpacing
QList< double > mColumnStretchFactors
QList< QList< QCPLayoutElement * > > mElements
FillOrder mFillOrder
int mRowSpacing
QList< double > mRowStretchFactors
int mWrap
Protected Attributes inherited from QCPLayoutElement
QCP::MarginSides mAutoMargins
QHash< QCP::MarginSide, QCPMarginGroup * > mMarginGroups
QMargins mMargins
QSize mMaximumSize
QMargins mMinimumMargins
QSize mMinimumSize
QRect mOuterRect
QCPLayoutmParentLayout
QRect mRect
SizeConstraintRect mSizeConstraintRect
Protected Attributes inherited from QCPLayerable
bool mAntialiased
QCPLayermLayer
QPointer< QCPLayerablemParentLayerable
QCustomPlotmParentPlot
bool mVisible

Friends

class QCPAbstractLegendItem
class QCustomPlot

Additional Inherited Members

Static Protected Member Functions inherited from QCPLayout
static QSize getFinalMaximumOuterSize (const QCPLayoutElement *el)
static QSize getFinalMinimumOuterSize (const QCPLayoutElement *el)

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.

A legend is populated with legend items by calling QCPAbstractPlottable::addToLegend on the plottable, for which a legend item shall be created. In the case of the main legend (QCustomPlot::legend), simply adding plottables to the plot while QCustomPlot::setAutoAddPlottableToLegend is set to true (the default) creates corresponding legend items. The legend item associated with a certain plottable 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.

Since QCPLegend derives from QCPLayoutGrid, it 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. See the special page about The Layout System for examples on how to add other elements to the legend and move it outside the axis rect.

Use the methods setFillOrder and setWrap inherited from QCPLayoutGrid to control in which order (column first or row first) the legend is filled up when calling addItem, and at which column or row wrapping occurs. The default fill order for legends is foRowsFirst.

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

◆ SelectablePart

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::QCPLegend ( )
explicit

Constructs a new QCPLegend instance with default values.

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

Member Function Documentation

◆ addItem()

bool QCPLegend::addItem ( QCPAbstractLegendItem * item)

Adds item to the legend, if it's not present already. The element is arranged according to the current fill order (setFillOrder) and wrapping (setWrap).

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.

See also
removeItem, item, hasItem

◆ applyDefaultAntialiasingHint()

void QCPLegend::applyDefaultAntialiasingHint ( QCPPainter * painter) const
protectedvirtual

Reimplemented from QCPLayoutElement.

◆ clearItems()

void QCPLegend::clearItems ( )

Removes all items from the legend.

◆ deselectEvent()

void QCPLegend::deselectEvent ( bool * selectionStateChanged)
protectedvirtual

Reimplemented from QCPLayerable.

◆ draw()

void QCPLegend::draw ( QCPPainter * painter)
protectedvirtual

Reimplemented from QCPLayoutElement.

◆ hasItem()

bool QCPLegend::hasItem ( QCPAbstractLegendItem * item) const

Returns whether the legend contains item.

See also
hasItemWithPlottable

◆ hasItemWithPlottable()

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

◆ item()

QCPAbstractLegendItem * QCPLegend::item ( int index) const

Returns the item with index i.

Note that the linear index depends on the current fill order (setFillOrder).

See also
itemCount, addItem, itemWithPlottable

◆ itemCount()

int QCPLegend::itemCount ( ) const

Returns the number of items currently in the legend.

Note that if empty cells are in the legend (e.g. by calling methods of the QCPLayoutGrid base class which allows creating empty cells), they are included in the returned count.

See also
item

◆ itemWithPlottable()

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

◆ parentPlotInitialized()

void QCPLegend::parentPlotInitialized ( QCustomPlot * parentPlot)
protectedvirtual

Reimplemented from QCPLayoutElement.

◆ removeItem() [1/2]

bool QCPLegend::removeItem ( int index)

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Removes the item with the specified index from the legend and deletes it.

After successful removal, the legend is reordered according to the current fill order (setFillOrder) and wrapping (setWrap), so no empty cell remains where the removed item was. If you don't want this, rather use the raw element interface of QCPLayoutGrid.

Returns true, if successful. Unlike QCPLayoutGrid::removeAt, this method only removes elements derived from QCPAbstractLegendItem.

See also
itemCount, clearItems

◆ removeItem() [2/2]

bool QCPLegend::removeItem ( QCPAbstractLegendItem * item)

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Removes item from the legend and deletes it.

After successful removal, the legend is reordered according to the current fill order (setFillOrder) and wrapping (setWrap), so no empty cell remains where the removed item was. If you don't want this, rather use the raw element interface of QCPLayoutGrid.

Returns true, if successful.

See also
clearItems

◆ selectedItems()

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

◆ selectEvent()

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

Reimplemented from QCPLayerable.

◆ selectionCategory()

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

Reimplemented from QCPLayerable.

◆ selectionChanged

void QCPLegend::selectionChanged ( QCPLegend::SelectableParts selection)
signal

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

See also
setSelectedParts, setSelectableParts

◆ selectTest()

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.

◆ setBorderPen()

void QCPLegend::setBorderPen ( const QPen & pen)

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

◆ setBrush()

void QCPLegend::setBrush ( const QBrush & brush)

Sets the brush of the legend background.

◆ setFont()

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

◆ setIconBorderPen()

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.

◆ setIconSize() [1/2]

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.

◆ setIconSize() [2/2]

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

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

◆ setIconTextPadding()

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.

◆ setSelectableParts()

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

◆ setSelectedBorderPen()

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

◆ setSelectedBrush()

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

◆ setSelectedFont()

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

◆ setSelectedIconBorderPen()

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

◆ 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

◆ setSelectedTextColor()

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

◆ setTextColor()

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

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