QGIS API Documentation  2.4.0-Chugiak
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
qgscomposerlegend.h
Go to the documentation of this file.
1 /***************************************************************************
2  qgscomposerlegend.h - description
3  -------------------
4  begin : June 2008
5  copyright : (C) 2008 by Marco Hugentobler
6  email : marco dot hugentobler at karto dot baug dot ethz dot ch
7  ***************************************************************************/
8 
9 /***************************************************************************
10  * *
11  * This program is free software; you can redistribute it and/or modify *
12  * it under the terms of the GNU General Public License as published by *
13  * the Free Software Foundation; either version 2 of the License, or *
14  * (at your option) any later version. *
15  * *
16  ***************************************************************************/
17 
18 #ifndef QGSCOMPOSERLEGEND_H
19 #define QGSCOMPOSERLEGEND_H
20 
21 #include "qgscomposerlegendstyle.h"
22 #include "qgscomposeritem.h"
23 #include "qgscomposerlegenditem.h"
24 #include "qgslegendmodel.h"
25 
26 class QgsSymbolV2;
29 class QgsComposerMap;
30 
34 class CORE_EXPORT QgsComposerLegend : public QgsComposerItem
35 {
36  Q_OBJECT;
37 
38  public:
39  QgsComposerLegend( QgsComposition* composition );
41 
43  virtual int type() const { return ComposerLegend; }
44 
46  void paint( QPainter* painter, const QStyleOptionGraphicsItem* itemStyle, QWidget* pWidget );
47 
49  QSizeF paintAndDetermineSize( QPainter* painter );
50 
52  void adjustBoxSize();
53 
55  QgsLegendModel* model() {return &mLegendModel;}
56 
57  //setters and getters
58  void setTitle( const QString& t ) {mTitle = t;}
59  QString title() const {return mTitle;}
60 
66  Qt::AlignmentFlag titleAlignment() const { return mTitleAlignment; }
72  void setTitleAlignment( Qt::AlignmentFlag alignment ) { mTitleAlignment = alignment; }
73 
77  QgsComposerLegendStyle style( QgsComposerLegendStyle::Style s ) const { return mStyleMap.value( s ); }
78  void setStyle( QgsComposerLegendStyle::Style s, const QgsComposerLegendStyle style ) { mStyleMap[s] = style; }
79 
80  QFont styleFont( QgsComposerLegendStyle::Style s ) const { return style( s ).font(); }
82  void setStyleFont( QgsComposerLegendStyle::Style s, const QFont& f );
83 
85  void setStyleMargin( QgsComposerLegendStyle::Style s, double margin );
86  void setStyleMargin( QgsComposerLegendStyle::Style s, QgsComposerLegendStyle::Side side, double margin );
87 
88  double boxSpace() const {return mBoxSpace;}
89  void setBoxSpace( double s ) {mBoxSpace = s;}
90 
91  double columnSpace() const {return mColumnSpace;}
92  void setColumnSpace( double s ) { mColumnSpace = s;}
93 
94  QColor fontColor() const {return mFontColor;}
95  void setFontColor( const QColor& c ) {mFontColor = c;}
96 
97  double symbolWidth() const {return mSymbolWidth;}
98  void setSymbolWidth( double w ) {mSymbolWidth = w;}
99 
100  double symbolHeight() const {return mSymbolHeight;}
101  void setSymbolHeight( double h ) {mSymbolHeight = h;}
102 
103  double wmsLegendWidth() const {return mWmsLegendWidth;}
104  void setWmsLegendWidth( double w ) {mWmsLegendWidth = w;}
105 
106  double wmsLegendHeight() const {return mWmsLegendHeight;}
107  void setWmsLegendHeight( double h ) {mWmsLegendHeight = h;}
108 
109  void setWrapChar( const QString& t ) {mWrapChar = t;}
110  QString wrapChar() const {return mWrapChar;}
111 
112  int columnCount() const { return mColumnCount; }
113  void setColumnCount( int c ) { mColumnCount = c;}
114 
115  int splitLayer() const { return mSplitLayer; }
116  void setSplitLayer( bool s ) { mSplitLayer = s;}
117 
118  int equalColumnWidth() const { return mEqualColumnWidth; }
119  void setEqualColumnWidth( bool s ) { mEqualColumnWidth = s;}
120 
121  void setComposerMap( const QgsComposerMap* map );
122  const QgsComposerMap* composerMap() const { return mComposerMap;}
123 
125  void updateLegend();
126 
131  bool writeXML( QDomElement& elem, QDomDocument & doc ) const;
132 
137  bool readXML( const QDomElement& itemElem, const QDomDocument& doc );
138 
139  public slots:
141  void synchronizeWithModel();
143  void invalidateCurrentMap();
144 
145  protected:
146  QString mTitle;
147  QString mWrapChar;
148 
149  QColor mFontColor;
150 
152  qreal mBoxSpace;
154  double mColumnSpace;
155 
157  double mSymbolWidth;
160 
165 
167  double mlineSpacing;
168 
170  Qt::AlignmentFlag mTitleAlignment;
171 
174 
176 
179 
182 
185 
186  private:
192  class Nucleon
193  {
194  public:
196  // Symbol size size without any space around for symbol item
197  QSizeF symbolSize;
198  // Label size without any space around for symbol item
199  QSizeF labelSize;
200  QSizeF size;
201  // Offset of symbol label, this offset is the same for all symbol labels
202  // of the same layer in the same column
203  double labelXOffset;
204  };
205 
215  class Atom
216  {
217  public:
218  Atom(): size( QSizeF( 0, 0 ) ), column( 0 ) {}
219  QList<Nucleon> nucleons;
220  // Atom size including nucleons interspaces but without any space around atom.
221  QSizeF size;
222  int column;
223  };
224 
226  QList<Atom> createAtomList( QStandardItem* rootItem, bool splitLayer );
227 
229  void setColumns( QList<Atom>& atomList );
230 
231  QgsComposerLegend(); //forbidden
232 
236  QSizeF drawTitle( QPainter* painter = 0, QPointF point = QPointF(), Qt::AlignmentFlag halignment = Qt::AlignLeft );
237 
241  QSizeF drawGroupItemTitle( QgsComposerGroupItem* groupItem, QPainter* painter = 0, QPointF point = QPointF() );
243  QSizeF drawLayerItemTitle( QgsComposerLayerItem* layerItem, QPainter* painter = 0, QPointF point = QPointF() );
244 
245  Nucleon drawSymbolItem( QgsComposerLegendItem* symbolItem, QPainter* painter = 0, QPointF point = QPointF(), double labelXOffset = 0. );
246 
249  void drawSymbolV2( QPainter* p, QgsSymbolV2* s, double currentYCoord, double& currentXPosition, double& symbolHeight, int opacity = 255 ) const;
250 
254  QSizeF drawAtom( Atom atom, QPainter* painter = 0, QPointF point = QPointF() );
255 
256  double spaceAboveAtom( Atom atom );
257 
259  QStringList layerIdList() const;
260 
263  QStringList splitStringForWrapping( QString stringToSplt );
264 
265  QMap<QgsComposerLegendStyle::Style, QgsComposerLegendStyle> mStyleMap;
266 };
267 
268 #endif
269 
double boxSpace() const
QgsComposerLegendStyle style(QgsComposerLegendStyle::Style s) const
Returns style.
A model that provides group, layer and classification items.
double mWmsLegendWidth
Width of WMS legendGraphic pixmap.
virtual bool readXML(const QDomElement &itemElem, const QDomDocument &doc)=0
sets state from Dom document
double mColumnSpace
Space between columns.
Qt::AlignmentFlag mTitleAlignment
Title alignment, one of Qt::AlignLeft, Qt::AlignHCenter, Qt::AlignRight)
double mWmsLegendHeight
Height of WMS legendGraphic pixmap.
void setSplitLayer(bool s)
Nucleon is either group title, layer title or layer child item.
void setColumnSpace(double s)
A item that forms part of a map composition.
double columnSpace() const
QgsLegendModel mLegendModel
double wmsLegendWidth() const
void setTitle(const QString &t)
int mColumnCount
Number of legend columns.
double mlineSpacing
Spacing between lines when wrapped.
void setWmsLegendHeight(double h)
double mSymbolHeight
Height of symbol icon.
const QgsComposerMap * mComposerMap
Reference to map (because symbols are sometimes in map units)
void setSymbolHeight(double h)
QColor fontColor() const
QString wrapChar() const
Qt::AlignmentFlag titleAlignment() const
Returns the alignment of the legend title.
QString title() const
Graphics scene for map printing.
QFont styleFont(QgsComposerLegendStyle::Style s) const
Abstract base class for the legend item types.
Object representing map window.
void setSymbolWidth(double w)
double symbolWidth() const
QgsComposerLegendStyle & rstyle(QgsComposerLegendStyle::Style s)
Returns reference to modifiable style.
void setWmsLegendWidth(double w)
Composer legend components style.
int columnCount() const
void setWrapChar(const QString &t)
qreal mBoxSpace
Space between item box and contents.
double wmsLegendHeight() const
int equalColumnWidth() const
const QgsComposerMap * composerMap() const
void setEqualColumnWidth(bool s)
void setStyle(QgsComposerLegendStyle::Style s, const QgsComposerLegendStyle style)
void setTitleAlignment(Qt::AlignmentFlag alignment)
Sets the alignment of the legend title.
bool mEqualColumnWidth
Use the same width (maximum) for all columns.
QgsLegendModel * model()
Returns pointer to the legend model.
void setColumnCount(int c)
Atom is indivisible set (indivisible into more columns).
QMap< QgsComposerLegendStyle::Style, QgsComposerLegendStyle > mStyleMap
QgsComposerLegendItem * item
void setFontColor(const QColor &c)
bool mSplitLayer
Allow splitting layers into multiple columns.
double size
Definition: qgssvgcache.cpp:77
A legend that can be placed onto a map composition.
virtual int type() const
return correct graphics item type.
double mSymbolWidth
Width of symbol icon.
void setBoxSpace(double s)
virtual bool writeXML(QDomElement &elem, QDomDocument &doc) const =0
stores state in Dom element
double symbolHeight() const
int splitLayer() const