QGIS API Documentation  2.2.0-Valmiera
 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  double wmsLegendWidth() const {return mWmsLegendWidth;}
91  void setWmsLegendWidth( double w ) {mWmsLegendWidth = w;}
92 
93  double wmsLegendHeight() const {return mWmsLegendHeight;}
94  void setWmsLegendHeight( double h ) {mWmsLegendHeight = h;}
95 
96  void setWrapChar( const QString& t ) {mWrapChar = t;}
97  QString wrapChar() const {return mWrapChar;}
98 
99  int columnCount() const { return mColumnCount; }
100  void setColumnCount( int c ) { mColumnCount = c;}
101 
102  int splitLayer() const { return mSplitLayer; }
103  void setSplitLayer( bool s ) { mSplitLayer = s;}
104 
105  int equalColumnWidth() const { return mEqualColumnWidth; }
106  void setEqualColumnWidth( bool s ) { mEqualColumnWidth = s;}
107 
108  void setComposerMap( const QgsComposerMap* map );
109  const QgsComposerMap* composerMap() const { return mComposerMap;}
110 
112  void updateLegend();
113 
118  bool writeXML( QDomElement& elem, QDomDocument & doc ) const;
119 
124  bool readXML( const QDomElement& itemElem, const QDomDocument& doc );
125 
126  public slots:
128  void synchronizeWithModel();
130  void invalidateCurrentMap();
131 
132  protected:
133  QString mTitle;
134  QString mWrapChar;
135 
136  QColor mFontColor;
137 
139  qreal mBoxSpace;
141  double mColumnSpace;
142 
144  double mSymbolWidth;
147 
152 
154  double mlineSpacing;
155 
158 
160 
163 
166 
169 
170  private:
176  class Nucleon
177  {
178  public:
180  // Symbol size size without any space around for symbol item
181  QSizeF symbolSize;
182  // Label size without any space around for symbol item
183  QSizeF labelSize;
184  QSizeF size;
185  // Offset of symbol label, this offset is the same for all symbol labels
186  // of the same layer in the same column
187  double labelXOffset;
188  };
189 
199  class Atom
200  {
201  public:
202  Atom(): size( QSizeF( 0, 0 ) ), column( 0 ) {}
203  QList<Nucleon> nucleons;
204  // Atom size including nucleons interspaces but without any space around atom.
205  QSizeF size;
206  int column;
207  };
208 
210  QList<Atom> createAtomList( QStandardItem* rootItem, bool splitLayer );
211 
213  void setColumns( QList<Atom>& atomList );
214 
215  QgsComposerLegend(); //forbidden
216 
217  QSizeF drawTitle( QPainter* painter = 0, QPointF point = QPointF(), Qt::AlignmentFlag halignment = Qt::AlignLeft );
218 
222  QSizeF drawGroupItemTitle( QgsComposerGroupItem* groupItem, QPainter* painter = 0, QPointF point = QPointF() );
224  QSizeF drawLayerItemTitle( QgsComposerLayerItem* layerItem, QPainter* painter = 0, QPointF point = QPointF() );
225 
226  Nucleon drawSymbolItem( QgsComposerLegendItem* symbolItem, QPainter* painter = 0, QPointF point = QPointF(), double labelXOffset = 0. );
227 
230  void drawSymbolV2( QPainter* p, QgsSymbolV2* s, double currentYCoord, double& currentXPosition, double& symbolHeight ) const;
231 
235  QSizeF drawAtom( Atom atom, QPainter* painter = 0, QPointF point = QPointF() );
236 
237  double spaceAboveAtom( Atom atom );
238 
240  QStringList layerIdList() const;
241 
244  QStringList splitStringForWrapping( QString stringToSplt );
245 
246  QMap<QgsComposerLegendStyle::Style, QgsComposerLegendStyle> mStyleMap;
247 };
248 
249 #endif
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.
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
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)
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