382 changes: 293 additions & 89 deletions src/core/composer/qgscomposerlegend.cpp

Large diffs are not rendered by default.

79 changes: 65 additions & 14 deletions src/core/composer/qgscomposerlegend.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#define QGSCOMPOSERLEGEND_H

#include "qgscomposeritem.h"
#include "qgscomposerlegenditem.h"
#include "qgslegendmodel.h"

class QgsSymbol;
Expand All @@ -35,6 +36,7 @@ class CORE_EXPORT QgsComposerLegend : public QgsComposerItem
Q_OBJECT

public:

QgsComposerLegend( QgsComposition* composition );
~QgsComposerLegend();

Expand All @@ -47,6 +49,7 @@ class CORE_EXPORT QgsComposerLegend : public QgsComposerItem
/**Paints the legend and calculates its size. If painter is 0, only size is calculated*/
QSizeF paintAndDetermineSize( QPainter* painter );


/**Sets item box to the whole content*/
void adjustBoxSize();

Expand All @@ -70,31 +73,34 @@ class CORE_EXPORT QgsComposerLegend : public QgsComposerItem
void setItemFont( const QFont& f );

double boxSpace() const {return mBoxSpace;}
void setBoxSpace( double s ) {mBoxSpace = s;}
void setBoxSpace( double s ) {mBoxSpace = s; mColumns.clear();}

double groupSpace() const {return mGroupSpace;}
void setGroupSpace( double s ) {mGroupSpace = s;}
void setGroupSpace( double s ) {mGroupSpace = s;mColumns.clear();}

double layerSpace() const {return mLayerSpace;}
void setLayerSpace( double s ) {mLayerSpace = s;}
void setLayerSpace( double s ) {mLayerSpace = s;mColumns.clear();}

double symbolSpace() const {return mSymbolSpace;}
void setSymbolSpace( double s ) {mSymbolSpace = s;}
void setSymbolSpace( double s ) {mSymbolSpace = s;mColumns.clear();}

double iconLabelSpace() const {return mIconLabelSpace;}
void setIconLabelSpace( double s ) {mIconLabelSpace = s;}
void setIconLabelSpace( double s ) {mIconLabelSpace = s;mColumns.clear();}

double symbolWidth() const {return mSymbolWidth;}
void setSymbolWidth( double w ) {mSymbolWidth = w;}
void setSymbolWidth( double w ) {mSymbolWidth = w;mColumns.clear();}

double symbolHeight() const {return mSymbolHeight;}
void setSymbolHeight( double h ) {mSymbolHeight = h;}
void setSymbolHeight( double h ) {mSymbolHeight = h;mColumns.clear();}

void setWrapChar( const QString& t ) {mWrapChar = t;}
void setWrapChar( const QString& t ) {mWrapChar = t;mColumns.clear();}
QString wrapChar() const {return mWrapChar;}

int columnCount() const { return mColumnCount; }
void setColumnCount( int c ) { mColumnCount = c;mColumns.clear();}

void setComposerMap( const QgsComposerMap* map );
const QgsComposerMap* composerMap() const { return mComposerMap; }
const QgsComposerMap* composerMap() const { return mComposerMap;}

/**Updates the model and all legend entries*/
void updateLegend();
Expand Down Expand Up @@ -145,28 +151,71 @@ class CORE_EXPORT QgsComposerLegend : public QgsComposerItem
/** Spacing between lines when wrapped */
double mlineSpacing;

/** Number of legend columns */
int mColumnCount;
/** Cached division of items to columns */
QMap<QStandardItem *, int> mColumns;

QgsLegendModel mLegendModel;

/**Reference to map (because symbols are sometimes in map units)*/
const QgsComposerMap* mComposerMap;


private:
// Group or layer size
struct Size
{
QSizeF size;
//bool isLayer; // layer or group
QgsComposerLegendItem::ItemType type;
QStandardItem * item;
Size() {}
Size( QSizeF s, QgsComposerLegendItem::ItemType t, QStandardItem * i ): size( s ), type( t ), item( i ) {}
} ;
struct LegendSize
{
QSizeF size; // legend size
QList<Size> sizes; // layer / group sizes
LegendSize() {}
LegendSize( QSizeF s, QList<Size>ls ): size( s ), sizes( ls ) {}
};
class Position
{
public:
QSizeF titleSize; // without spaces around
QPointF point; // current position
double columnTop; // y coord where columns start (mBoxSpace + title height)
int column;
QMap<QStandardItem *, int> columns;
// widths of columns, does not include spaces before/between/after columns
QVector<double> widths;
double maxColumnHeight;
// set max width for current column
void expandWidth( double w );
// set column and x position
void setColumn( QStandardItem *item );
double boxSpace;
double columnSpace; // space between columns
};

QgsComposerLegend(); //forbidden

/**Draws a group item and all subitems*/
void drawGroupItem( QPainter* p, QgsComposerGroupItem* groupItem, double& currentYCoord, double& maxXCoord );
/**Draws a group item and all subitems
* Returns list of sizes of layers and groups including this group.
*/
QList<Size> drawGroupItem( QPainter* p, QgsComposerGroupItem* groupItem, Position& currentPosition );
/**Draws a layer item and all subitems*/
void drawLayerItem( QPainter* p, QgsComposerLayerItem* layerItem, double& currentYCoord, double& maxXCoord );
Size drawLayerItem( QPainter* p, QgsComposerLayerItem* layerItem, Position& currentPosition );

/**Draws child items of a layer item
@param p painter
@param layerItem parent model item (layer)
@param currentYCoord in/out: current y position of legend item
@param currentPosition in/out: current y position of legend item
@param maxXCoord in/out: maximum x-coordinate of the whole legend
@param layerOpacity opacity of the corresponding map layer
*/
void drawLayerChildItems( QPainter* p, QStandardItem* layerItem, double& currentYCoord, double& maxXCoord, int layerOpacity = 255 );
QSizeF drawLayerChildItems( QPainter* p, QStandardItem* layerItem, Position& currentPosition, int layerOpacity = 255 );

/**Draws a symbol at the current y position and returns the new x position. Returns real symbol height, because for points,
it is possible that it differs from mSymbolHeight*/
Expand All @@ -176,6 +225,8 @@ class CORE_EXPORT QgsComposerLegend : public QgsComposerItem
void drawLineSymbol( QPainter*, QgsSymbol* s, double currentYCoord, double& currentXPosition, int opacity = 255 ) const;
void drawPolygonSymbol( QPainter* p, QgsSymbol* s, double currentYCoord, double& currentXPosition, int opacity = 255 ) const;

LegendSize paintAndDetermineSize( QPainter* painter, QMap<QStandardItem *, int> columns );

/**Helper function that lists ids of layers contained in map canvas*/
QStringList layerIdList() const;

Expand Down
1 change: 1 addition & 0 deletions src/core/composer/qgslegendmodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ QStandardItem* QgsLegendModel::addGroup( QString text, int position )
{
invisibleRootItem()->insertRow( position, groupItem );
}
emit layersChanged();
return groupItem;
}

Expand Down
80 changes: 48 additions & 32 deletions src/ui/qgscomposerlegendwidgetbase.ui
Original file line number Diff line number Diff line change
Expand Up @@ -44,25 +44,22 @@
<item row="0" column="0">
<widget class="QToolBox" name="toolBox">
<property name="currentIndex">
<number>1</number>
<number>0</number>
</property>
<widget class="QWidget" name="page">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>370</width>
<height>467</height>
<height>479</height>
</rect>
</property>
<attribute name="label">
<string>General</string>
</attribute>
<layout class="QGridLayout" name="gridLayout">
<item row="2" column="1">
<widget class="QLineEdit" name="mTitleLineEdit"/>
</item>
<item row="3" column="1">
<item row="5" column="1">
<widget class="QPushButton" name="mTitleFontButton">
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Fixed">
Expand All @@ -75,28 +72,28 @@
</property>
</widget>
</item>
<item row="4" column="1">
<item row="6" column="1">
<widget class="QPushButton" name="mGroupFontButton">
<property name="text">
<string>Group Font...</string>
</property>
</widget>
</item>
<item row="5" column="1">
<item row="7" column="1">
<widget class="QPushButton" name="mLayerFontButton">
<property name="text">
<string>Layer Font...</string>
</property>
</widget>
</item>
<item row="6" column="1">
<item row="8" column="1">
<widget class="QPushButton" name="mItemFontButton">
<property name="text">
<string>Item Font...</string>
</property>
</widget>
</item>
<item row="7" column="1">
<item row="9" column="1">
<widget class="QDoubleSpinBox" name="mSymbolWidthSpinBox">
<property name="prefix">
<string>Symbol width </string>
Expand All @@ -106,7 +103,7 @@
</property>
</widget>
</item>
<item row="8" column="1">
<item row="10" column="1">
<widget class="QDoubleSpinBox" name="mSymbolHeightSpinBox">
<property name="prefix">
<string>Symbol height </string>
Expand All @@ -116,7 +113,7 @@
</property>
</widget>
</item>
<item row="10" column="1">
<item row="12" column="1">
<widget class="QDoubleSpinBox" name="mLayerSpaceSpinBox">
<property name="prefix">
<string>Layer space </string>
Expand All @@ -126,7 +123,7 @@
</property>
</widget>
</item>
<item row="12" column="1">
<item row="14" column="1">
<widget class="QDoubleSpinBox" name="mSymbolSpaceSpinBox">
<property name="prefix">
<string>Symbol space </string>
Expand All @@ -136,7 +133,7 @@
</property>
</widget>
</item>
<item row="13" column="1">
<item row="15" column="1">
<widget class="QDoubleSpinBox" name="mIconLabelSpaceSpinBox">
<property name="prefix">
<string>Icon label space </string>
Expand All @@ -146,7 +143,7 @@
</property>
</widget>
</item>
<item row="14" column="1">
<item row="16" column="1">
<widget class="QDoubleSpinBox" name="mBoxSpaceSpinBox">
<property name="prefix">
<string>Box space </string>
Expand All @@ -156,7 +153,7 @@
</property>
</widget>
</item>
<item row="20" column="1">
<item row="22" column="1">
<spacer name="verticalSpacer">
<property name="orientation">
<enum>Qt::Vertical</enum>
Expand All @@ -169,27 +166,17 @@
</property>
</spacer>
</item>
<item row="16" column="1">
<item row="18" column="1">
<widget class="QComboBox" name="mMapComboBox"/>
</item>
<item row="15" column="1">
<item row="17" column="1">
<widget class="QLabel" name="mMapLabel">
<property name="text">
<string>Map</string>
</property>
</widget>
</item>
<item row="1" column="1">
<widget class="QLabel" name="mTitleLabel">
<property name="text">
<string>&amp;Title</string>
</property>
<property name="buddy">
<cstring>mTitleLineEdit</cstring>
</property>
</widget>
</item>
<item row="9" column="1">
<item row="11" column="1">
<widget class="QDoubleSpinBox" name="mGroupSpaceSpinBox">
<property name="prefix">
<string>Group Space </string>
Expand All @@ -199,20 +186,50 @@
</property>
</widget>
</item>
<item row="18" column="1">
<item row="20" column="1">
<widget class="QLabel" name="label">
<property name="text">
<string>Wrap text on</string>
</property>
</widget>
</item>
<item row="19" column="1">
<item row="21" column="1">
<widget class="QLineEdit" name="mWrapCharLineEdit">
<property name="frame">
<bool>true</bool>
</property>
</widget>
</item>
<item row="2" column="1">
<layout class="QHBoxLayout" name="horizontalLayout">
<item>
<widget class="QLabel" name="mTitleLabel">
<property name="text">
<string>&amp;Title</string>
</property>
<property name="buddy">
<cstring>mTitleLineEdit</cstring>
</property>
</widget>
</item>
<item>
<widget class="QLineEdit" name="mTitleLineEdit"/>
</item>
</layout>
</item>
<item row="4" column="1">
<widget class="QSpinBox" name="mColumnCountSpinBox">
<property name="prefix">
<string>Column count </string>
</property>
<property name="minimum">
<number>1</number>
</property>
<property name="maximum">
<number>1000</number>
</property>
</widget>
</item>
</layout>
</widget>
<widget class="QWidget" name="page_2">
Expand Down Expand Up @@ -439,7 +456,6 @@
<layoutdefault spacing="6" margin="11"/>
<tabstops>
<tabstop>scrollArea</tabstop>
<tabstop>mTitleLineEdit</tabstop>
<tabstop>mTitleFontButton</tabstop>
<tabstop>mGroupFontButton</tabstop>
<tabstop>mLayerFontButton</tabstop>
Expand Down