Skip to content

Commit 1b437d3

Browse files
committedNov 13, 2012
multicolumn composer legend
1 parent d8d9766 commit 1b437d3

File tree

7 files changed

+423
-136
lines changed

7 files changed

+423
-136
lines changed
 

‎src/app/composer/qgscomposerlegendwidget.cpp

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ void QgsComposerLegendWidget::setGuiElements()
8787

8888
blockAllSignals( true );
8989
mTitleLineEdit->setText( mLegend->title() );
90+
mColumnCountSpinBox->setValue( mLegend->columnCount() );
9091
mSymbolWidthSpinBox->setValue( mLegend->symbolWidth() );
9192
mSymbolHeightSpinBox->setValue( mLegend->symbolHeight() );
9293
mGroupSpaceSpinBox->setValue( mLegend->groupSpace() );
@@ -109,7 +110,6 @@ void QgsComposerLegendWidget::setGuiElements()
109110
{
110111
mMapComboBox->setCurrentIndex( mMapComboBox->findData( -1 ) );
111112
}
112-
113113
blockAllSignals( false );
114114
}
115115

@@ -137,6 +137,18 @@ void QgsComposerLegendWidget::on_mTitleLineEdit_textChanged( const QString& text
137137
}
138138
}
139139

140+
void QgsComposerLegendWidget::on_mColumnCountSpinBox_valueChanged( int c )
141+
{
142+
if ( mLegend )
143+
{
144+
mLegend->beginCommand( tr( "Legend column count" ), QgsComposerMergeCommand::LegendColumnCount );
145+
mLegend->setColumnCount( c );
146+
mLegend->adjustBoxSize();
147+
mLegend->update();
148+
mLegend->endCommand();
149+
}
150+
}
151+
140152
void QgsComposerLegendWidget::on_mSymbolWidthSpinBox_valueChanged( double d )
141153
{
142154
if ( mLegend )
@@ -718,6 +730,7 @@ void QgsComposerLegendWidget::blockAllSignals( bool b )
718730
mItemTreeView->blockSignals( b );
719731
mCheckBoxAutoUpdate->blockSignals( b );
720732
mMapComboBox->blockSignals( b );
733+
mColumnCountSpinBox->blockSignals( b );
721734
mSymbolWidthSpinBox->blockSignals( b );
722735
mSymbolHeightSpinBox->blockSignals( b );
723736
mGroupSpaceSpinBox->blockSignals( b );

‎src/app/composer/qgscomposerlegendwidget.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ class QgsComposerLegendWidget: public QWidget, private Ui::QgsComposerLegendWidg
4141

4242
void on_mWrapCharLineEdit_textChanged( const QString& text );
4343
void on_mTitleLineEdit_textChanged( const QString& text );
44+
void on_mColumnCountSpinBox_valueChanged( int c );
4445
void on_mSymbolWidthSpinBox_valueChanged( double d );
4546
void on_mSymbolHeightSpinBox_valueChanged( double d );
4647
void on_mGroupSpaceSpinBox_valueChanged( double d );

‎src/core/composer/qgscomposeritemcommand.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ class CORE_EXPORT QgsComposerMergeCommand: public QgsComposerItemCommand
8080
ComposerMapAnnotationDistance,
8181
//composer legend
8282
ComposerLegendText,
83+
LegendColumnCount,
8384
LegendSymbolWidth,
8485
LegendSymbolHeight,
8586
LegendGroupSpace,

‎src/core/composer/qgscomposerlegend.cpp

Lines changed: 293 additions & 89 deletions
Large diffs are not rendered by default.

‎src/core/composer/qgscomposerlegend.h

Lines changed: 65 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#define QGSCOMPOSERLEGEND_H
2020

2121
#include "qgscomposeritem.h"
22+
#include "qgscomposerlegenditem.h"
2223
#include "qgslegendmodel.h"
2324

2425
class QgsSymbol;
@@ -35,6 +36,7 @@ class CORE_EXPORT QgsComposerLegend : public QgsComposerItem
3536
Q_OBJECT
3637

3738
public:
39+
3840
QgsComposerLegend( QgsComposition* composition );
3941
~QgsComposerLegend();
4042

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

52+
5053
/**Sets item box to the whole content*/
5154
void adjustBoxSize();
5255

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

7275
double boxSpace() const {return mBoxSpace;}
73-
void setBoxSpace( double s ) {mBoxSpace = s;}
76+
void setBoxSpace( double s ) {mBoxSpace = s; mColumns.clear();}
7477

7578
double groupSpace() const {return mGroupSpace;}
76-
void setGroupSpace( double s ) {mGroupSpace = s;}
79+
void setGroupSpace( double s ) {mGroupSpace = s;mColumns.clear();}
7780

7881
double layerSpace() const {return mLayerSpace;}
79-
void setLayerSpace( double s ) {mLayerSpace = s;}
82+
void setLayerSpace( double s ) {mLayerSpace = s;mColumns.clear();}
8083

8184
double symbolSpace() const {return mSymbolSpace;}
82-
void setSymbolSpace( double s ) {mSymbolSpace = s;}
85+
void setSymbolSpace( double s ) {mSymbolSpace = s;mColumns.clear();}
8386

8487
double iconLabelSpace() const {return mIconLabelSpace;}
85-
void setIconLabelSpace( double s ) {mIconLabelSpace = s;}
88+
void setIconLabelSpace( double s ) {mIconLabelSpace = s;mColumns.clear();}
8689

8790
double symbolWidth() const {return mSymbolWidth;}
88-
void setSymbolWidth( double w ) {mSymbolWidth = w;}
91+
void setSymbolWidth( double w ) {mSymbolWidth = w;mColumns.clear();}
8992

9093
double symbolHeight() const {return mSymbolHeight;}
91-
void setSymbolHeight( double h ) {mSymbolHeight = h;}
94+
void setSymbolHeight( double h ) {mSymbolHeight = h;mColumns.clear();}
9295

93-
void setWrapChar( const QString& t ) {mWrapChar = t;}
96+
void setWrapChar( const QString& t ) {mWrapChar = t;mColumns.clear();}
9497
QString wrapChar() const {return mWrapChar;}
9598

99+
int columnCount() const { return mColumnCount; }
100+
void setColumnCount( int c ) { mColumnCount = c;mColumns.clear();}
101+
96102
void setComposerMap( const QgsComposerMap* map );
97-
const QgsComposerMap* composerMap() const { return mComposerMap; }
103+
const QgsComposerMap* composerMap() const { return mComposerMap;}
98104

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

154+
/** Number of legend columns */
155+
int mColumnCount;
156+
/** Cached division of items to columns */
157+
QMap<QStandardItem *, int> mColumns;
158+
148159
QgsLegendModel mLegendModel;
149160

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

153164

154165
private:
166+
// Group or layer size
167+
struct Size
168+
{
169+
QSizeF size;
170+
//bool isLayer; // layer or group
171+
QgsComposerLegendItem::ItemType type;
172+
QStandardItem * item;
173+
Size() {}
174+
Size( QSizeF s, QgsComposerLegendItem::ItemType t, QStandardItem * i ): size( s ), type( t ), item( i ) {}
175+
} ;
176+
struct LegendSize
177+
{
178+
QSizeF size; // legend size
179+
QList<Size> sizes; // layer / group sizes
180+
LegendSize() {}
181+
LegendSize( QSizeF s, QList<Size>ls ): size( s ), sizes( ls ) {}
182+
};
183+
class Position
184+
{
185+
public:
186+
QSizeF titleSize; // without spaces around
187+
QPointF point; // current position
188+
double columnTop; // y coord where columns start (mBoxSpace + title height)
189+
int column;
190+
QMap<QStandardItem *, int> columns;
191+
// widths of columns, does not include spaces before/between/after columns
192+
QVector<double> widths;
193+
double maxColumnHeight;
194+
// set max width for current column
195+
void expandWidth( double w );
196+
// set column and x position
197+
void setColumn( QStandardItem *item );
198+
double boxSpace;
199+
double columnSpace; // space between columns
200+
};
201+
155202
QgsComposerLegend(); //forbidden
156203

157-
/**Draws a group item and all subitems*/
158-
void drawGroupItem( QPainter* p, QgsComposerGroupItem* groupItem, double& currentYCoord, double& maxXCoord );
204+
/**Draws a group item and all subitems
205+
* Returns list of sizes of layers and groups including this group.
206+
*/
207+
QList<Size> drawGroupItem( QPainter* p, QgsComposerGroupItem* groupItem, Position& currentPosition );
159208
/**Draws a layer item and all subitems*/
160-
void drawLayerItem( QPainter* p, QgsComposerLayerItem* layerItem, double& currentYCoord, double& maxXCoord );
209+
Size drawLayerItem( QPainter* p, QgsComposerLayerItem* layerItem, Position& currentPosition );
161210

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

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

228+
LegendSize paintAndDetermineSize( QPainter* painter, QMap<QStandardItem *, int> columns );
229+
179230
/**Helper function that lists ids of layers contained in map canvas*/
180231
QStringList layerIdList() const;
181232

‎src/core/composer/qgslegendmodel.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@ QStandardItem* QgsLegendModel::addGroup( QString text, int position )
131131
{
132132
invisibleRootItem()->insertRow( position, groupItem );
133133
}
134+
emit layersChanged();
134135
return groupItem;
135136
}
136137

‎src/ui/qgscomposerlegendwidgetbase.ui

Lines changed: 48 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -44,25 +44,22 @@
4444
<item row="0" column="0">
4545
<widget class="QToolBox" name="toolBox">
4646
<property name="currentIndex">
47-
<number>1</number>
47+
<number>0</number>
4848
</property>
4949
<widget class="QWidget" name="page">
5050
<property name="geometry">
5151
<rect>
5252
<x>0</x>
5353
<y>0</y>
5454
<width>370</width>
55-
<height>467</height>
55+
<height>479</height>
5656
</rect>
5757
</property>
5858
<attribute name="label">
5959
<string>General</string>
6060
</attribute>
6161
<layout class="QGridLayout" name="gridLayout">
62-
<item row="2" column="1">
63-
<widget class="QLineEdit" name="mTitleLineEdit"/>
64-
</item>
65-
<item row="3" column="1">
62+
<item row="5" column="1">
6663
<widget class="QPushButton" name="mTitleFontButton">
6764
<property name="sizePolicy">
6865
<sizepolicy hsizetype="Preferred" vsizetype="Fixed">
@@ -75,28 +72,28 @@
7572
</property>
7673
</widget>
7774
</item>
78-
<item row="4" column="1">
75+
<item row="6" column="1">
7976
<widget class="QPushButton" name="mGroupFontButton">
8077
<property name="text">
8178
<string>Group Font...</string>
8279
</property>
8380
</widget>
8481
</item>
85-
<item row="5" column="1">
82+
<item row="7" column="1">
8683
<widget class="QPushButton" name="mLayerFontButton">
8784
<property name="text">
8885
<string>Layer Font...</string>
8986
</property>
9087
</widget>
9188
</item>
92-
<item row="6" column="1">
89+
<item row="8" column="1">
9390
<widget class="QPushButton" name="mItemFontButton">
9491
<property name="text">
9592
<string>Item Font...</string>
9693
</property>
9794
</widget>
9895
</item>
99-
<item row="7" column="1">
96+
<item row="9" column="1">
10097
<widget class="QDoubleSpinBox" name="mSymbolWidthSpinBox">
10198
<property name="prefix">
10299
<string>Symbol width </string>
@@ -106,7 +103,7 @@
106103
</property>
107104
</widget>
108105
</item>
109-
<item row="8" column="1">
106+
<item row="10" column="1">
110107
<widget class="QDoubleSpinBox" name="mSymbolHeightSpinBox">
111108
<property name="prefix">
112109
<string>Symbol height </string>
@@ -116,7 +113,7 @@
116113
</property>
117114
</widget>
118115
</item>
119-
<item row="10" column="1">
116+
<item row="12" column="1">
120117
<widget class="QDoubleSpinBox" name="mLayerSpaceSpinBox">
121118
<property name="prefix">
122119
<string>Layer space </string>
@@ -126,7 +123,7 @@
126123
</property>
127124
</widget>
128125
</item>
129-
<item row="12" column="1">
126+
<item row="14" column="1">
130127
<widget class="QDoubleSpinBox" name="mSymbolSpaceSpinBox">
131128
<property name="prefix">
132129
<string>Symbol space </string>
@@ -136,7 +133,7 @@
136133
</property>
137134
</widget>
138135
</item>
139-
<item row="13" column="1">
136+
<item row="15" column="1">
140137
<widget class="QDoubleSpinBox" name="mIconLabelSpaceSpinBox">
141138
<property name="prefix">
142139
<string>Icon label space </string>
@@ -146,7 +143,7 @@
146143
</property>
147144
</widget>
148145
</item>
149-
<item row="14" column="1">
146+
<item row="16" column="1">
150147
<widget class="QDoubleSpinBox" name="mBoxSpaceSpinBox">
151148
<property name="prefix">
152149
<string>Box space </string>
@@ -156,7 +153,7 @@
156153
</property>
157154
</widget>
158155
</item>
159-
<item row="20" column="1">
156+
<item row="22" column="1">
160157
<spacer name="verticalSpacer">
161158
<property name="orientation">
162159
<enum>Qt::Vertical</enum>
@@ -169,27 +166,17 @@
169166
</property>
170167
</spacer>
171168
</item>
172-
<item row="16" column="1">
169+
<item row="18" column="1">
173170
<widget class="QComboBox" name="mMapComboBox"/>
174171
</item>
175-
<item row="15" column="1">
172+
<item row="17" column="1">
176173
<widget class="QLabel" name="mMapLabel">
177174
<property name="text">
178175
<string>Map</string>
179176
</property>
180177
</widget>
181178
</item>
182-
<item row="1" column="1">
183-
<widget class="QLabel" name="mTitleLabel">
184-
<property name="text">
185-
<string>&amp;Title</string>
186-
</property>
187-
<property name="buddy">
188-
<cstring>mTitleLineEdit</cstring>
189-
</property>
190-
</widget>
191-
</item>
192-
<item row="9" column="1">
179+
<item row="11" column="1">
193180
<widget class="QDoubleSpinBox" name="mGroupSpaceSpinBox">
194181
<property name="prefix">
195182
<string>Group Space </string>
@@ -199,20 +186,50 @@
199186
</property>
200187
</widget>
201188
</item>
202-
<item row="18" column="1">
189+
<item row="20" column="1">
203190
<widget class="QLabel" name="label">
204191
<property name="text">
205192
<string>Wrap text on</string>
206193
</property>
207194
</widget>
208195
</item>
209-
<item row="19" column="1">
196+
<item row="21" column="1">
210197
<widget class="QLineEdit" name="mWrapCharLineEdit">
211198
<property name="frame">
212199
<bool>true</bool>
213200
</property>
214201
</widget>
215202
</item>
203+
<item row="2" column="1">
204+
<layout class="QHBoxLayout" name="horizontalLayout">
205+
<item>
206+
<widget class="QLabel" name="mTitleLabel">
207+
<property name="text">
208+
<string>&amp;Title</string>
209+
</property>
210+
<property name="buddy">
211+
<cstring>mTitleLineEdit</cstring>
212+
</property>
213+
</widget>
214+
</item>
215+
<item>
216+
<widget class="QLineEdit" name="mTitleLineEdit"/>
217+
</item>
218+
</layout>
219+
</item>
220+
<item row="4" column="1">
221+
<widget class="QSpinBox" name="mColumnCountSpinBox">
222+
<property name="prefix">
223+
<string>Column count </string>
224+
</property>
225+
<property name="minimum">
226+
<number>1</number>
227+
</property>
228+
<property name="maximum">
229+
<number>1000</number>
230+
</property>
231+
</widget>
232+
</item>
216233
</layout>
217234
</widget>
218235
<widget class="QWidget" name="page_2">
@@ -439,7 +456,6 @@
439456
<layoutdefault spacing="6" margin="11"/>
440457
<tabstops>
441458
<tabstop>scrollArea</tabstop>
442-
<tabstop>mTitleLineEdit</tabstop>
443459
<tabstop>mTitleFontButton</tabstop>
444460
<tabstop>mGroupFontButton</tabstop>
445461
<tabstop>mLayerFontButton</tabstop>

0 commit comments

Comments
 (0)
Please sign in to comment.