QGIS API Documentation  2.0.1-Dufour
 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 
64  QgsComposerLegendStyle style( QgsComposerLegendStyle::Style s ) const { return mStyleMap.value( s ); }
65  void setStyle( QgsComposerLegendStyle::Style s, const QgsComposerLegendStyle style ) { mStyleMap[s] = style; }
66 
67  QFont styleFont( QgsComposerLegendStyle::Style s ) const { return style( s ).font(); }
69  void setStyleFont( QgsComposerLegendStyle::Style s, const QFont& f );
70 
72  void setStyleMargin( QgsComposerLegendStyle::Style s, double margin );
73  void setStyleMargin( QgsComposerLegendStyle::Style s, QgsComposerLegendStyle::Side side, double margin );
74 
75  double boxSpace() const {return mBoxSpace;}
76  void setBoxSpace( double s ) {mBoxSpace = s;}
77 
78  double columnSpace() const {return mColumnSpace;}
79  void setColumnSpace( double s ) { mColumnSpace = s;}
80 
81  QColor fontColor() const {return mFontColor;}
82  void setFontColor( const QColor& c ) {mFontColor = c;}
83 
84  double symbolWidth() const {return mSymbolWidth;}
85  void setSymbolWidth( double w ) {mSymbolWidth = w;}
86 
87  double symbolHeight() const {return mSymbolHeight;}
88  void setSymbolHeight( double h ) {mSymbolHeight = h;}
89 
90  void setWrapChar( const QString& t ) {mWrapChar = t;}
91  QString wrapChar() const {return mWrapChar;}
92 
93  int columnCount() const { return mColumnCount; }
94  void setColumnCount( int c ) { mColumnCount = c;}
95 
96  int splitLayer() const { return mSplitLayer; }
97  void setSplitLayer( bool s ) { mSplitLayer = s;}
98 
99  int equalColumnWidth() const { return mEqualColumnWidth; }
100  void setEqualColumnWidth( bool s ) { mEqualColumnWidth = s;}
101 
102  void setComposerMap( const QgsComposerMap* map );
103  const QgsComposerMap* composerMap() const { return mComposerMap;}
104 
106  void updateLegend();
107 
112  bool writeXML( QDomElement& elem, QDomDocument & doc ) const;
113 
118  bool readXML( const QDomElement& itemElem, const QDomDocument& doc );
119 
120  public slots:
122  void synchronizeWithModel();
124  void invalidateCurrentMap();
125 
126  protected:
127  QString mTitle;
128  QString mWrapChar;
129 
130  QColor mFontColor;
131 
133  qreal mBoxSpace;
135  double mColumnSpace;
136 
138  double mSymbolWidth;
141 
143  double mlineSpacing;
144 
147 
149 
152 
155 
158 
159  private:
165  class Nucleon
166  {
167  public:
169  // Symbol size size without any space around for symbol item
170  QSizeF symbolSize;
171  // Label size without any space around for symbol item
172  QSizeF labelSize;
173  QSizeF size;
174  // Offset of symbol label, this offset is the same for all symbol labels
175  // of the same layer in the same column
176  double labelXOffset;
177  };
178 
188  class Atom
189  {
190  public:
191  Atom(): size( QSizeF( 0, 0 ) ), column( 0 ) {}
192  QList<Nucleon> nucleons;
193  // Atom size including nucleons interspaces but without any space around atom.
194  QSizeF size;
195  int column;
196  };
197 
199  QList<Atom> createAtomList( QStandardItem* rootItem, bool splitLayer );
200 
202  void setColumns( QList<Atom>& atomList );
203 
204  QgsComposerLegend(); //forbidden
205 
206  QSizeF drawTitle( QPainter* painter = 0, QPointF point = QPointF(), Qt::AlignmentFlag halignment = Qt::AlignLeft );
207 
211  QSizeF drawGroupItemTitle( QgsComposerGroupItem* groupItem, QPainter* painter = 0, QPointF point = QPointF() );
213  QSizeF drawLayerItemTitle( QgsComposerLayerItem* layerItem, QPainter* painter = 0, QPointF point = QPointF() );
214 
215  Nucleon drawSymbolItem( QgsComposerLegendItem* symbolItem, QPainter* painter = 0, QPointF point = QPointF(), double labelXOffset = 0. );
216 
219  void drawSymbolV2( QPainter* p, QgsSymbolV2* s, double currentYCoord, double& currentXPosition, double& symbolHeight ) const;
220 
224  QSizeF drawAtom( Atom atom, QPainter* painter = 0, QPointF point = QPointF() );
225 
226  double spaceAboveAtom( Atom atom );
227 
229  QStringList layerIdList() const;
230 
233  QStringList splitStringForWrapping( QString stringToSplt );
234 
235  QMap<QgsComposerLegendStyle::Style, QgsComposerLegendStyle> mStyleMap;
236 };
237 
238 #endif