10#include "qwt_plot_legenditem.h"
11#include "qwt_dyngrid_layout.h"
13#include "qwt_graphic.h"
14#include "qwt_legend_data.h"
17#include <qlayoutitem.h>
24 class LayoutItem QWT_FINAL :
public QLayoutItem
28 virtual ~LayoutItem();
35 virtual Qt::Orientations expandingDirections() const QWT_OVERRIDE;
36 virtual QRect geometry() const QWT_OVERRIDE;
37 virtual
bool hasHeightForWidth() const QWT_OVERRIDE;
38 virtual
int heightForWidth(
int ) const QWT_OVERRIDE;
39 virtual
bool isEmpty() const QWT_OVERRIDE;
40 virtual QSize maximumSize() const QWT_OVERRIDE;
41 virtual
int minimumHeightForWidth(
int ) const QWT_OVERRIDE;
42 virtual QSize minimumSize() const QWT_OVERRIDE;
43 virtual
void setGeometry( const QRect& ) QWT_OVERRIDE;
44 virtual QSize sizeHint() const QWT_OVERRIDE;
55 LayoutItem::LayoutItem(
57 : m_legendItem( legendItem )
58 , m_plotItem( plotItem)
62 LayoutItem::~LayoutItem()
81 Qt::Orientations LayoutItem::expandingDirections()
const
83 return Qt::Horizontal;
86 bool LayoutItem::hasHeightForWidth()
const
91 int LayoutItem::minimumHeightForWidth(
int w )
const
93 return m_legendItem->heightForWidth( m_data, w );
96 int LayoutItem::heightForWidth(
int w )
const
98 return m_legendItem->heightForWidth( m_data, w );
101 bool LayoutItem::isEmpty()
const
106 QSize LayoutItem::maximumSize()
const
108 return QSize( QLAYOUTSIZE_MAX, QLAYOUTSIZE_MAX );
111 QSize LayoutItem::minimumSize()
const
113 return m_legendItem->minimumSize( m_data );
116 QSize LayoutItem::sizeHint()
const
118 return minimumSize();
121 void LayoutItem::setGeometry(
const QRect& rect )
126 QRect LayoutItem::geometry()
const
132class QwtPlotLegendItem::PrivateData
138 , borderRadius( 0.0 )
139 , borderPen( Qt::NoPen )
140 , backgroundBrush( Qt::NoBrush )
142 , canvasAlignment( Qt::AlignRight | Qt::AlignBottom )
144 canvasOffset[ 0 ] = canvasOffset[1] = 10;
148 layout->setSpacing( 0 );
149 layout->setContentsMargins( 0, 0, 0, 0 );
164 QBrush backgroundBrush;
168 Qt::Alignment canvasAlignment;
178 m_data =
new PrivateData;
212 if ( m_data->canvasAlignment != alignment )
214 m_data->canvasAlignment = alignment;
225 return m_data->canvasAlignment;
269 m_data->layout->setContentsMargins(
283 m_data->layout->getContentsMargins( &left, NULL, NULL, NULL );
297 if (
spacing != m_data->layout->spacing() )
299 m_data->layout->setSpacing(
spacing );
310 return m_data->layout->spacing();
322 if (
margin != m_data->itemMargin )
324 m_data->itemMargin =
margin;
337 return m_data->itemMargin;
349 if (
spacing != m_data->itemSpacing )
365 return m_data->itemSpacing;
376 if (
font != m_data->font )
406 Qt::Orientations orientations,
int numPixels )
411 bool isChanged =
false;
413 int* offset = m_data->canvasOffset;
415 if ( orientations & Qt::Horizontal )
417 if ( numPixels != offset[0] )
419 offset[0] = numPixels;
424 if ( orientations & Qt::Vertical )
426 if ( numPixels != offset[1] )
428 offset[1] = numPixels;
445 Qt::Orientation orientation )
const
447 const int index = ( orientation == Qt::Vertical ) ? 1 : 0;
448 return m_data->canvasOffset[index];
459 radius = qwtMaxF( 0.0, radius );
461 if ( radius != m_data->borderRadius )
463 m_data->borderRadius = radius;
474 return m_data->borderRadius;
485 if ( m_data->borderPen != pen )
487 m_data->borderPen = pen;
498 return m_data->borderPen;
511 if ( m_data->backgroundBrush != brush )
513 m_data->backgroundBrush = brush;
524 return m_data->backgroundBrush;
539 if ( mode != m_data->backgroundMode )
541 m_data->backgroundMode = mode;
552 return m_data->backgroundMode;
563 if ( m_data->textPen != pen )
565 m_data->textPen = pen;
576 return m_data->textPen;
589 const QRectF& canvasRect )
const
595 if ( m_data->layout->geometry().
isEmpty() )
604 for (
int i = 0; i < m_data->layout->
count(); i++ )
606 const LayoutItem* layoutItem =
607 static_cast< LayoutItem*
>( m_data->layout->
itemAt( i ) );
615 layoutItem->data(), layoutItem->geometry() );
631 QPainter* painter,
const QRectF& rect )
const
635 painter->setPen( m_data->borderPen );
636 painter->setBrush( m_data->backgroundBrush );
638 const double radius = m_data->borderRadius;
639 painter->drawRoundedRect( rect, radius, radius );
653 rect.setSize( m_data->layout->
sizeHint() );
655 if ( m_data->canvasAlignment & Qt::AlignHCenter )
657 int x = qRound( canvasRect.center().x() );
658 rect.moveCenter( QPoint( x, rect.center().y() ) );
660 else if ( m_data->canvasAlignment & Qt::AlignRight )
663 rect.moveRight( qwtFloor( canvasRect.right() - offset ) );
668 rect.moveLeft( qwtCeil( canvasRect.left() + offset ) );
671 if ( m_data->canvasAlignment & Qt::AlignVCenter )
673 int y = qRound( canvasRect.center().y() );
674 rect.moveCenter( QPoint( rect.center().x(), y ) );
676 else if ( m_data->canvasAlignment & Qt::AlignBottom )
679 rect.moveBottom( qwtFloor( canvasRect.bottom() - offset ) );
684 rect.moveTop( qwtCeil( canvasRect.top() + offset ) );
700 if ( plotItem == NULL )
706 m_data->map.constFind( plotItem );
707 if ( it != m_data->map.constEnd() )
708 layoutItems = it.value();
710 bool changed =
false;
712 if ( data.size() != layoutItems.size() )
716 for (
int i = 0; i < layoutItems.size(); i++ )
718 m_data->layout->removeItem( layoutItems[i] );
719 delete layoutItems[i];
723 if ( it != m_data->map.constEnd() )
724 m_data->map.remove( plotItem );
726 if ( !data.isEmpty() )
728 layoutItems.reserve( data.size() );
730 for (
int i = 0; i < data.size(); i++ )
732 LayoutItem* layoutItem =
733 new LayoutItem(
this, plotItem );
734 m_data->layout->
addItem( layoutItem );
735 layoutItems += layoutItem;
738 m_data->map.insert( plotItem, layoutItems );
742 for (
int i = 0; i < data.size(); i++ )
744 if ( layoutItems[i]->data().values() != data[i].values() )
746 layoutItems[i]->setData( data[i] );
761 if ( !m_data->map.isEmpty() )
765 for (
int i = m_data->layout->
count() - 1; i >= 0; i-- )
766 delete m_data->layout->
takeAt( i );
782 const QRectF& rect )
const
784 Q_UNUSED( plotItem );
786 const int m = m_data->itemMargin;
787 const QRectF r = rect.toRect().adjusted( m, m, -m, -m );
789 painter->setClipRect( r, Qt::IntersectClip );
796 QRectF iconRect( r.topLeft(), graphic.
defaultSize() );
799 QPoint( iconRect.center().x(), rect.center().y() ) );
801 graphic.
render( painter, iconRect, Qt::KeepAspectRatio );
803 titleOff += iconRect.width() + m_data->itemSpacing;
812 const QRectF textRect = r.adjusted( titleOff, 0, 0, 0 );
813 text.
draw( painter, textRect );
825 QSize size( 2 * m_data->itemMargin, 2 * m_data->itemMargin );
839 h = graphic.height();
846 w += qwtCeil( sz.width() );
847 h = qMax( h, qwtCeil( sz.height() ) );
850 if ( graphic.width() > 0 && !text.
isEmpty() )
851 w += m_data->itemSpacing;
853 size += QSize( w, h );
865 width -= 2 * m_data->itemMargin;
871 return graphic.height();
873 if ( graphic.width() > 0 )
874 width -= graphic.width() + m_data->itemSpacing;
877 h += 2 * m_data->itemMargin;
879 return qMax( graphic.height(), h );
888 return m_data->map.keys();
901 m_data->map.constFind( plotItem );
902 if ( it != m_data->map.constEnd() )
903 layoutItems = it.value();
906 geometries.reserve(layoutItems.size() );
908 for (
int i = 0; i < layoutItems.size(); i++ )
909 geometries += layoutItems[i]->
geometry();
The QwtDynGridLayout class lays out widgets in a grid, adjusting the number of columns and rows to th...
virtual QLayoutItem * itemAt(int index) const override
virtual bool isEmpty() const override
virtual QLayoutItem * takeAt(int index) override
virtual QSize sizeHint() const override
uint maxColumns() const
Return the upper limit for the number of columns.
void setMaxColumns(uint maxColumns)
virtual void addItem(QLayoutItem *) override
Add an item to the next free position.
virtual void setGeometry(const QRect &) override
virtual int count() const override
virtual void invalidate() override
Invalidate all internal caches.
A paint device for scalable graphics.
QSizeF defaultSize() const
Default size.
void render(QPainter *) const
Replay all recorded painter commands.
Attributes of an entry on a legend.
Base class for items on the plot canvas.
void setZ(double z)
Set the z value.
@ Rtti_PlotLegend
For QwtPlotLegendItem.
void setItemInterest(ItemInterest, bool on=true)
virtual void itemChanged()
A class which draws a legend inside the plot canvas.
void setOffsetInCanvas(Qt::Orientations, int numPixels)
Set the distance between the legend and the canvas border.
BackgroundMode
Background mode.
@ LegendBackground
The legend has a background.
@ ItemBackground
Each item has a background.
int offsetInCanvas(Qt::Orientation) const
virtual QRect geometry(const QRectF &canvasRect) const
BackgroundMode backgroundMode() const
void setBackgroundMode(BackgroundMode)
Set the background mode.
virtual void draw(QPainter *, const QwtScaleMap &xMap, const QwtScaleMap &yMap, const QRectF &canvasRect) const override
virtual void updateLegend(const QwtPlotItem *, const QList< QwtLegendData > &) override
QBrush backgroundBrush() const
void setAlignmentInCanvas(Qt::Alignment)
Set the alignmnet.
void setMargin(int)
Set the margin around legend items.
QwtPlotLegendItem()
Constructor.
Qt::Alignment alignmentInCanvas() const
virtual void drawLegendData(QPainter *, const QwtPlotItem *, const QwtLegendData &, const QRectF &) const
QList< QRect > legendGeometries(const QwtPlotItem *) const
void setBackgroundBrush(const QBrush &)
Set the background brush.
QList< const QwtPlotItem * > plotItems() const
void setMaxColumns(uint)
Limit the number of columns.
void setBorderPen(const QPen &)
void setSpacing(int)
Set the spacing between the legend items.
virtual int heightForWidth(const QwtLegendData &, int width) const
void setFont(const QFont &)
virtual QSize minimumSize(const QwtLegendData &) const
virtual void drawBackground(QPainter *, const QRectF &rect) const
virtual int rtti() const override
void clearLegend()
Remove all items from the legend.
virtual ~QwtPlotLegendItem()
Destructor.
void setTextPen(const QPen &)
Set the pen for drawing text labels.
double borderRadius() const
void setBorderRadius(double)
A class representing a text.
void setFont(const QFont &)
void draw(QPainter *painter, const QRectF &rect) const
double heightForWidth(double width) const