rock_widget_collection  0.1
Public Member Functions | Protected Member Functions | Protected Attributes | List of all members
QCPPlottableLegendItem Class Reference

A legend item representing a plottable with an icon and the plottable name. More...

#include <qcustomplot.h>

Inherits QCPAbstractLegendItem.

Public Member Functions

 QCPPlottableLegendItem (QCPLegend *parent, const QCPAbstractPlottable *plottable)
 
virtual ~QCPPlottableLegendItem ()
 
const QCPAbstractPlottableplottable () const
 
void setTextWrap (bool wrap)
 
bool textWrap () const
 
- Public Member Functions inherited from QCPAbstractLegendItem
 QCPAbstractLegendItem (QCPLegend *parent)
 
virtual ~QCPAbstractLegendItem ()
 
QFont font () const
 
void setFont (const QFont &font)
 

Protected Member Functions

virtual void draw (QPainter *painter, const QRect &rect) const
 
virtual QSize size (const QSize &targetSize) const
 

Protected Attributes

const QCPAbstractPlottablemPlottable
 
QSize mIconSize
 
QPen mIconBorderPen
 
int mIconTextPadding
 
bool mTextWrap
 
- Protected Attributes inherited from QCPAbstractLegendItem
QCPLegendmParentLegend
 
QFont mFont
 

Detailed Description

A legend item representing a plottable with an icon and the plottable name.

This is the standard legend item for plottables. It displays an icon of the plottable next to the plottable name. The icon is drawn by the respective plottable itself (QCPAbstractPlottable::drawLegendIcon), and tries to give an intuitive symbol for the plottable. For example, the QCPGraph draws a centered horizontal line with a single scatter point in the middle and filling (if enabled) below.

Legend items of this type are always associated with one plottable (retrievable via the plottable() function and settable with the constructor). You may change the font of the plottable name with setFont. If setTextWrap is set to true, the plottable name will wrap at the right legend boundary (see QCPLegend::setMinimumSize). Icon padding and border pen is taken from the parent QCPLegend, see QCPLegend::setIconBorderPen and QCPLegend::setIconTextPadding.

The function QCPAbstractPlottable::addToLegend/QCPAbstractPlottable::removeFromLegend creates/removes legend items of this type in the default implementation. However, these functions may be reimplemented such that a different kind of legend item (e.g a direct subclass of QCPAbstractLegendItem) is used for that plottable.

Definition at line 533 of file qcustomplot.h.

Constructor & Destructor Documentation

◆ QCPPlottableLegendItem()

QCPPlottableLegendItem::QCPPlottableLegendItem ( QCPLegend parent,
const QCPAbstractPlottable plottable 
)

Creates a new legend item associated with plottable.

Once it's created, it can be added to the legend via QCPLegend::addItem.

A more convenient way of adding/removing a plottable to/from the legend is via the functions QCPAbstractPlottable::addToLegend and QCPAbstractPlottable::removeFromLegend.

Definition at line 6418 of file qcustomplot.cc.

6418  :
6419  QCPAbstractLegendItem(parent),
6420  mPlottable(plottable)
6421 {
6422  // take default values from parent legend:
6423  mIconSize = parent->iconSize();
6424  mIconBorderPen = parent->iconBorderPen();
6425  mIconTextPadding = parent->iconTextPadding();
6426  mTextWrap = false;
6427 }
QPen iconBorderPen() const
Definition: qcustomplot.h:598
QCPAbstractLegendItem(QCPLegend *parent)
QSize iconSize() const
Definition: qcustomplot.h:596
int iconTextPadding() const
Definition: qcustomplot.h:597
const QCPAbstractPlottable * mPlottable
Definition: qcustomplot.h:544

◆ ~QCPPlottableLegendItem()

virtual QCPPlottableLegendItem::~QCPPlottableLegendItem ( )
inlinevirtual

Definition at line 537 of file qcustomplot.h.

537 {}

Member Function Documentation

◆ draw()

void QCPPlottableLegendItem::draw ( QPainter *  painter,
const QRect &  rect 
) const
protectedvirtual

Draws this legend item with painter inside the specified rect. The rect typically has the size which was returned from a preceding size call.

Implements QCPAbstractLegendItem.

Definition at line 6451 of file qcustomplot.cc.

6452 {
6453  if (!mPlottable) return;
6454  painter->setFont(mFont);
6455  QRect textRect;
6456  QRect iconRect(rect.topLeft(), mIconSize);
6457  if (mTextWrap)
6458  {
6459  // take width from rect since our text should wrap there (only icon must fit at least):
6460  textRect = painter->fontMetrics().boundingRect(0, 0, rect.width()-mIconTextPadding-mIconSize.width(), rect.height(), Qt::TextDontClip | Qt::TextWordWrap, mPlottable->name());
6461  if (textRect.height() < mIconSize.height()) // text smaller than icon, center text vertically in icon height
6462  {
6463  painter->drawText(rect.x()+mIconSize.width()+mIconTextPadding, rect.y(), rect.width()-mIconTextPadding-mIconSize.width(), mIconSize.height(), Qt::TextDontClip | Qt::TextWordWrap, mPlottable->name());
6464  } else // text bigger than icon, position top of text with top of icon
6465  {
6466  painter->drawText(rect.x()+mIconSize.width()+mIconTextPadding, rect.y(), rect.width()-mIconTextPadding-mIconSize.width(), textRect.height(), Qt::TextDontClip | Qt::TextWordWrap, mPlottable->name());
6467  }
6468  } else
6469  {
6470  // text can't wrap (except with explicit newlines), center at current item size (icon size)
6471  textRect = painter->fontMetrics().boundingRect(0, 0, 0, rect.height(), Qt::TextDontClip, mPlottable->name());
6472  if (textRect.height() < mIconSize.height()) // text smaller than icon, center text vertically in icon height
6473  {
6474  painter->drawText(rect.x()+mIconSize.width()+mIconTextPadding, rect.y(), rect.width(), mIconSize.height(), Qt::TextDontClip, mPlottable->name());
6475  } else // text bigger than icon, position top of text with top of icon
6476  {
6477  painter->drawText(rect.x()+mIconSize.width()+mIconTextPadding, rect.y(), rect.width(), textRect.height(), Qt::TextDontClip, mPlottable->name());
6478  }
6479  }
6480  // draw icon:
6481  painter->save();
6482  painter->setClipRect(iconRect, Qt::IntersectClip);
6483  mPlottable->drawLegendIcon(painter, iconRect);
6484  painter->restore();
6485  // draw icon border:
6486  if (mIconBorderPen.style() != Qt::NoPen)
6487  {
6488  painter->setPen(mIconBorderPen);
6489  painter->setBrush(Qt::NoBrush);
6490  painter->drawRect(iconRect);
6491  }
6492 }
QString name() const
Definition: qcustomplot.h:122
virtual void drawLegendIcon(QPainter *painter, const QRect &rect) const =0
const QCPAbstractPlottable * mPlottable
Definition: qcustomplot.h:544

◆ plottable()

const QCPAbstractPlottable* QCPPlottableLegendItem::plottable ( ) const
inline

Definition at line 539 of file qcustomplot.h.

539 { return mPlottable; }
const QCPAbstractPlottable * mPlottable
Definition: qcustomplot.h:544

◆ setTextWrap()

void QCPPlottableLegendItem::setTextWrap ( bool  wrap)

Sets whether the text of the legend item is wrapped at word boundaries to fit the with of the legend.

To prevent the legend autoSize feature (QCPLegend::setAutoSize) from compressing the text too strong by wrapping it very often, set an appropriate minimum width with QCPLegend::setMinimumSize.

Definition at line 6437 of file qcustomplot.cc.

6438 {
6439  mTextWrap = wrap;
6440 }

◆ size()

QSize QCPPlottableLegendItem::size ( const QSize &  targetSize) const
protectedvirtual

Returns the size this item occupies in the legend. The legend will adapt its layout with the help of this function. If this legend item can have a variable width (e.g. auto-wrapping text), this function tries to find a size with a width close to the width of targetSize. The height of targetSize only may have meaning in specific sublasses. Typically, it's ignored.

Implements QCPAbstractLegendItem.

Definition at line 6505 of file qcustomplot.cc.

6506 {
6507  if (!mPlottable) return QSize();
6508  QSize result(0, 0);
6509  QRect textRect;
6510  QFontMetrics fontMetrics(mFont);
6511  if (mTextWrap)
6512  {
6513  // take width from targetSize since our text can wrap (Only icon must fit at least):
6514  textRect = fontMetrics.boundingRect(0, 0, targetSize.width()-mIconTextPadding-mIconSize.width(), mIconSize.height(), Qt::TextDontClip | Qt::TextWordWrap, mPlottable->name());
6515  } else
6516  {
6517  // text can't wrap (except with explicit newlines), center at current item size (icon size)
6518  textRect = fontMetrics.boundingRect(0, 0, 0, mIconSize.height(), Qt::TextDontClip, mPlottable->name());
6519  }
6520  result.setWidth(mIconSize.width() + mIconTextPadding + textRect.width());
6521  result.setHeight(qMax(textRect.height(), mIconSize.height()));
6522  return result;
6523 }
QString name() const
Definition: qcustomplot.h:122
const QCPAbstractPlottable * mPlottable
Definition: qcustomplot.h:544

◆ textWrap()

bool QCPPlottableLegendItem::textWrap ( ) const
inline

Definition at line 541 of file qcustomplot.h.

541 { return mTextWrap; }

Member Data Documentation

◆ mIconBorderPen

QPen QCPPlottableLegendItem::mIconBorderPen
protected

Definition at line 546 of file qcustomplot.h.

◆ mIconSize

QSize QCPPlottableLegendItem::mIconSize
protected

Definition at line 545 of file qcustomplot.h.

◆ mIconTextPadding

int QCPPlottableLegendItem::mIconTextPadding
protected

Definition at line 547 of file qcustomplot.h.

◆ mPlottable

const QCPAbstractPlottable* QCPPlottableLegendItem::mPlottable
protected

Definition at line 544 of file qcustomplot.h.

◆ mTextWrap

bool QCPPlottableLegendItem::mTextWrap
protected

Definition at line 548 of file qcustomplot.h.


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