Skip to content

Commit 39c436d

Browse files
author
timlinux
committedDec 21, 2010
applied patch from NathanW for ticket #3339
git-svn-id: http://svn.osgeo.org/qgis/trunk@14951 c8812cc2-4d05-0410-92ff-de0c093fc19c
1 parent e0b8b29 commit 39c436d

9 files changed

+299
-21
lines changed
 

‎src/app/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ SET(QGIS_APP_SRCS
103103
composer/qgscomposershapewidget.cpp
104104
composer/qgscomposertablewidget.cpp
105105
composer/qgscomposerlegenditemdialog.cpp
106+
composer/qgscomposerlegendlayersdialog.cpp
106107
composer/qgscomposerlegendwidget.cpp
107108
composer/qgscompositionwidget.cpp
108109
composer/qgsitempositiondialog.cpp
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/***************************************************************************
2+
qgscomposerlegendlayersdialog.cpp
3+
-------------------------------
4+
***************************************************************************/
5+
6+
/***************************************************************************
7+
* *
8+
* This program is free software; you can redistribute it and/or modify *
9+
* it under the terms of the GNU General Public License as published by *
10+
* the Free Software Foundation; either version 2 of the License, or *
11+
* (at your option) any later version. *
12+
* *
13+
***************************************************************************/
14+
15+
#include "qgscomposerlegendlayersdialog.h"
16+
17+
#include <QStandardItem>
18+
19+
QgsComposerLegendLayersDialog::QgsComposerLegendLayersDialog( QList<QgsMapLayer*> layers, QWidget* parent ): QDialog( parent )
20+
{
21+
setupUi( this );
22+
23+
QList<QgsMapLayer*>::iterator layerIt = layers.begin();
24+
for (; layerIt != layers.end(); ++layerIt )
25+
{
26+
QListWidgetItem* item = new QListWidgetItem(( *layerIt )->name(), listMapLayers );
27+
mItemLayerMap.insert( item, *layerIt );
28+
}
29+
}
30+
31+
QgsComposerLegendLayersDialog::QgsComposerLegendLayersDialog(): QDialog( 0 )
32+
{
33+
34+
}
35+
36+
QgsComposerLegendLayersDialog::~QgsComposerLegendLayersDialog()
37+
{
38+
39+
}
40+
41+
QgsMapLayer* QgsComposerLegendLayersDialog::selectedLayer()
42+
{
43+
QListWidgetItem* item = listMapLayers->currentItem();
44+
if ( !item )
45+
{
46+
return 0;
47+
}
48+
49+
QMap<QListWidgetItem*, QgsMapLayer*>::iterator it = mItemLayerMap.find( item );
50+
QgsMapLayer* c = 0;
51+
c = it.value();
52+
return c;
53+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/***************************************************************************
2+
qgscomposerlegenditemdialog.h
3+
-----------------------------
4+
***************************************************************************/
5+
6+
/***************************************************************************
7+
* *
8+
* This program is free software; you can redistribute it and/or modify *
9+
* it under the terms of the GNU General Public License as published by *
10+
* the Free Software Foundation; either version 2 of the License, or *
11+
* (at your option) any later version. *
12+
* *
13+
***************************************************************************/
14+
15+
#ifndef QGSCOMPOSERLEGENDLAYERSDIALOG_H
16+
#define QGSCOMPOSERLEGENDLAYERSDIALOG_H
17+
18+
#include "ui_qgscomposerlegendlayersdialogbase.h"
19+
#include "qgsmaplayer.h"
20+
21+
/** \ingroup MapComposer
22+
* A dialog to add new layers to the legend.
23+
* */
24+
class QgsComposerLegendLayersDialog: private Ui::QgsComposerLegendLayersDialogBase, public QDialog
25+
{
26+
public:
27+
QgsComposerLegendLayersDialog( QList<QgsMapLayer*> layers, QWidget* parent = 0 );
28+
~QgsComposerLegendLayersDialog();
29+
QgsMapLayer* selectedLayer();
30+
31+
private:
32+
QgsComposerLegendLayersDialog(); //forbidden
33+
34+
/**Stores the relation between items and map layer pointers. */
35+
QMap<QListWidgetItem*, QgsMapLayer*> mItemLayerMap;
36+
};
37+
38+
#endif //QGSCOMPOSERLEGENDITEMDIALOG_H

‎src/app/composer/qgscomposerlegendwidget.cpp

Lines changed: 61 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,29 @@
1818
#include "qgscomposerlegendwidget.h"
1919
#include "qgscomposerlegend.h"
2020
#include "qgscomposerlegenditemdialog.h"
21+
#include "qgscomposerlegendlayersdialog.h"
2122
#include "qgscomposeritemwidget.h"
2223
#include <QFontDialog>
2324

2425
#include "qgsapplegendinterface.h"
2526
#include "qgisapp.h"
2627
#include "qgsmapcanvas.h"
2728
#include "qgsmaprenderer.h"
29+
#include "qgsapplication.h"
30+
31+
#include <QMessageBox>
2832

2933
QgsComposerLegendWidget::QgsComposerLegendWidget( QgsComposerLegend* legend ): mLegend( legend )
3034
{
3135
setupUi( this );
3236

37+
// setup icons
38+
mAddToolButton->setIcon( QIcon( QgsApplication::iconPath( "symbologyAdd.png" ) ) );
39+
mEditPushButton->setIcon( QIcon( QgsApplication::iconPath( "symbologyEdit.png" ) ) );
40+
mRemoveToolButton->setIcon( QIcon( QgsApplication::iconPath( "symbologyRemove.png" ) ) );
41+
mMoveUpToolButton->setIcon( QIcon( QgsApplication::iconPath( "symbologyUp.png" ) ) );
42+
mMoveDownToolButton->setIcon( QIcon( QgsApplication::iconPath( "symbologyDown.png" ) ) );
43+
3344
//add widget for item properties
3445
QgsComposerItemWidget* itemPropertiesWidget = new QgsComposerItemWidget( this, legend );
3546
toolBox->addItem( itemPropertiesWidget, tr( "Item Options" ) );
@@ -77,7 +88,6 @@ void QgsComposerLegendWidget::setGuiElements()
7788
blockSignals( false );
7889
}
7990

80-
8191
void QgsComposerLegendWidget::on_mTitleLineEdit_textChanged( const QString& text )
8292
{
8393
if ( mLegend )
@@ -360,6 +370,56 @@ void QgsComposerLegendWidget::on_mMoveUpToolButton_clicked()
360370
mLegend->endCommand();
361371
}
362372

373+
void QgsComposerLegendWidget::on_mCheckBoxAutoUpdate_stateChanged ( int state )
374+
{
375+
if ( state == Qt::Checked )
376+
{
377+
mLegend->model()->setAutoUpdate( true );
378+
}
379+
else
380+
{
381+
mLegend->model()->setAutoUpdate( false );
382+
}
383+
}
384+
385+
void QgsComposerLegendWidget::on_mAddToolButton_clicked()
386+
{
387+
if ( !mLegend )
388+
{
389+
return;
390+
}
391+
392+
QStandardItemModel* itemModel = qobject_cast<QStandardItemModel *>( mItemTreeView->model() );
393+
if ( !itemModel )
394+
{
395+
return;
396+
}
397+
398+
QgisApp* app = QgisApp::instance();
399+
if ( !app )
400+
{
401+
return;
402+
}
403+
404+
QgsMapCanvas* canvas = app->mapCanvas();
405+
if ( canvas )
406+
{
407+
QList<QgsMapLayer*> layers = canvas->layers();
408+
409+
QgsComposerLegendLayersDialog addDialog( layers );
410+
if ( addDialog.exec() == QDialog::Accepted )
411+
{
412+
QgsMapLayer* layer = addDialog.selectedLayer();
413+
if ( layer )
414+
{
415+
mLegend->beginCommand( "Legend item added" );
416+
mLegend->model()->addLayer( layer );
417+
mLegend->endCommand();
418+
}
419+
}
420+
}
421+
}
422+
363423
void QgsComposerLegendWidget::on_mRemoveToolButton_clicked()
364424
{
365425
if ( !mLegend )

‎src/app/composer/qgscomposerlegendwidget.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,13 @@ class QgsComposerLegendWidget: public QWidget, private Ui::QgsComposerLegendWidg
5050
void on_mLayerFontButton_clicked();
5151
void on_mItemFontButton_clicked();
5252
void on_mBoxSpaceSpinBox_valueChanged( double d );
53+
void on_mCheckBoxAutoUpdate_stateChanged (int state );
5354

5455
//item manipulation
5556
void on_mMoveDownToolButton_clicked();
5657
void on_mMoveUpToolButton_clicked();
5758
void on_mRemoveToolButton_clicked();
59+
void on_mAddToolButton_clicked();
5860
void on_mEditPushButton_clicked();
5961
void on_mUpdatePushButton_clicked();
6062
void on_mUpdateAllPushButton_clicked();

‎src/core/composer/qgslegendmodel.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
#include <QDomElement>
3333
#include <QMimeData>
3434
#include <QSettings>
35+
#include <QMessageBox>
3536

3637
QgsLegendModel::QgsLegendModel(): QStandardItemModel()
3738
{
@@ -40,6 +41,7 @@ QgsLegendModel::QgsLegendModel(): QStandardItemModel()
4041
connect( QgsMapLayerRegistry::instance(), SIGNAL( layerWillBeRemoved( QString ) ), this, SLOT( removeLayer( const QString& ) ) );
4142
connect( QgsMapLayerRegistry::instance(), SIGNAL( layerWasAdded( QgsMapLayer* ) ), this, SLOT( addLayer( QgsMapLayer* ) ) );
4243
}
44+
4345
setItemPrototype( new QgsComposerSymbolItem() );
4446

4547
QWidgetList topLevelWidgets = QApplication::topLevelWidgets();
@@ -674,3 +676,24 @@ bool QgsLegendModel::dropMimeData( const QMimeData *data, Qt::DropAction action,
674676
emit layersChanged();
675677
return true;
676678
}
679+
680+
void QgsLegendModel::setAutoUpdate(bool autoUpdate)
681+
{
682+
mAutoUpdate = autoUpdate;
683+
if ( autoUpdate )
684+
{
685+
if ( QgsMapLayerRegistry::instance() )
686+
{
687+
connect( QgsMapLayerRegistry::instance(), SIGNAL( layerWillBeRemoved( QString ) ), this, SLOT( removeLayer( const QString& ) ) );
688+
connect( QgsMapLayerRegistry::instance(), SIGNAL( layerWasAdded( QgsMapLayer* ) ), this, SLOT( addLayer( QgsMapLayer* ) ) );
689+
}
690+
}
691+
else
692+
{
693+
if ( QgsMapLayerRegistry::instance() )
694+
{
695+
disconnect( QgsMapLayerRegistry::instance(), SIGNAL( layerWillBeRemoved( QString ) ), this, SLOT( removeLayer( const QString& ) ) );
696+
disconnect( QgsMapLayerRegistry::instance(), SIGNAL( layerWasAdded( QgsMapLayer* ) ), this, SLOT( addLayer( QgsMapLayer* ) ) );
697+
}
698+
}
699+
}

‎src/core/composer/qgslegendmodel.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,8 @@ class CORE_EXPORT QgsLegendModel: public QStandardItemModel
8484
/**Implements the drop operation*/
8585
bool dropMimeData( const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent );
8686

87+
void setAutoUpdate( bool autoUpdate );
88+
bool AutoUpdaet() { return mAutoUpdate; }
8789

8890
public slots:
8991
void removeLayer( const QString& layerId );
@@ -112,6 +114,9 @@ class CORE_EXPORT QgsLegendModel: public QStandardItemModel
112114
/**True if this application has toplevel windows (normally true). If this is false, this means that the application
113115
might not have a running x-server on unix systems and so QPixmap and QIcon cannot be used*/
114116
bool mHasTopLevelWindow;
117+
118+
/**True if the legend is auto updated when layers are added or removed from the map canvas */
119+
bool mAutoUpdate;
115120
};
116121

117122
#endif
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<ui version="4.0">
3+
<class>QgsComposerLegendLayersDialogBase</class>
4+
<widget class="QDialog" name="QgsComposerLegendLayersDialogBase">
5+
<property name="geometry">
6+
<rect>
7+
<x>0</x>
8+
<y>0</y>
9+
<width>252</width>
10+
<height>194</height>
11+
</rect>
12+
</property>
13+
<property name="windowTitle">
14+
<string>Add layer to legend</string>
15+
</property>
16+
<layout class="QGridLayout" name="gridLayout">
17+
<item row="0" column="0">
18+
<widget class="QListWidget" name="listMapLayers"/>
19+
</item>
20+
<item row="1" column="0">
21+
<widget class="QDialogButtonBox" name="buttonBox">
22+
<property name="orientation">
23+
<enum>Qt::Horizontal</enum>
24+
</property>
25+
<property name="standardButtons">
26+
<set>QDialogButtonBox::Cancel|QDialogButtonBox::Ok</set>
27+
</property>
28+
</widget>
29+
</item>
30+
</layout>
31+
</widget>
32+
<resources/>
33+
<connections>
34+
<connection>
35+
<sender>buttonBox</sender>
36+
<signal>accepted()</signal>
37+
<receiver>QgsComposerLegendLayersDialogBase</receiver>
38+
<slot>accept()</slot>
39+
<hints>
40+
<hint type="sourcelabel">
41+
<x>248</x>
42+
<y>254</y>
43+
</hint>
44+
<hint type="destinationlabel">
45+
<x>157</x>
46+
<y>274</y>
47+
</hint>
48+
</hints>
49+
</connection>
50+
<connection>
51+
<sender>buttonBox</sender>
52+
<signal>rejected()</signal>
53+
<receiver>QgsComposerLegendLayersDialogBase</receiver>
54+
<slot>reject()</slot>
55+
<hints>
56+
<hint type="sourcelabel">
57+
<x>316</x>
58+
<y>260</y>
59+
</hint>
60+
<hint type="destinationlabel">
61+
<x>286</x>
62+
<y>274</y>
63+
</hint>
64+
</hints>
65+
</connection>
66+
</connections>
67+
</ui>

‎src/ui/qgscomposerlegendwidgetbase.ui

Lines changed: 49 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
<rect>
77
<x>0</x>
88
<y>0</y>
9-
<width>369</width>
10-
<height>471</height>
9+
<width>371</width>
10+
<height>476</height>
1111
</rect>
1212
</property>
1313
<property name="sizePolicy">
@@ -33,8 +33,8 @@
3333
<rect>
3434
<x>0</x>
3535
<y>0</y>
36-
<width>367</width>
37-
<height>469</height>
36+
<width>369</width>
37+
<height>474</height>
3838
</rect>
3939
</property>
4040
<layout class="QGridLayout" name="gridLayout_3">
@@ -48,8 +48,8 @@
4848
<rect>
4949
<x>0</x>
5050
<y>0</y>
51-
<width>349</width>
52-
<height>397</height>
51+
<width>338</width>
52+
<height>404</height>
5353
</rect>
5454
</property>
5555
<attribute name="label">
@@ -183,70 +183,99 @@
183183
<rect>
184184
<x>0</x>
185185
<y>0</y>
186-
<width>349</width>
187-
<height>397</height>
186+
<width>351</width>
187+
<height>394</height>
188188
</rect>
189189
</property>
190190
<attribute name="label">
191191
<string>Legend items</string>
192192
</attribute>
193193
<layout class="QGridLayout" name="gridLayout_2">
194-
<item row="0" column="0" colspan="7">
194+
<item row="0" column="0" colspan="4">
195+
<widget class="QCheckBox" name="mCheckBoxAutoUpdate">
196+
<property name="sizePolicy">
197+
<sizepolicy hsizetype="Expanding" vsizetype="Fixed">
198+
<horstretch>0</horstretch>
199+
<verstretch>0</verstretch>
200+
</sizepolicy>
201+
</property>
202+
<property name="text">
203+
<string>Auto Update</string>
204+
</property>
205+
<property name="checked">
206+
<bool>true</bool>
207+
</property>
208+
</widget>
209+
</item>
210+
<item row="1" column="0" colspan="8">
195211
<widget class="QTreeView" name="mItemTreeView">
196212
<property name="sizePolicy">
197213
<sizepolicy hsizetype="Minimum" vsizetype="Expanding">
198214
<horstretch>0</horstretch>
199215
<verstretch>0</verstretch>
200216
</sizepolicy>
201217
</property>
218+
<property name="wordWrap">
219+
<bool>false</bool>
220+
</property>
202221
<property name="headerHidden">
203222
<bool>true</bool>
204223
</property>
224+
<attribute name="headerVisible">
225+
<bool>false</bool>
226+
</attribute>
205227
</widget>
206228
</item>
207-
<item row="1" column="0">
229+
<item row="2" column="0">
208230
<widget class="QToolButton" name="mMoveDownToolButton">
209231
<property name="text">
210-
<string>v</string>
232+
<string/>
211233
</property>
212234
</widget>
213235
</item>
214-
<item row="1" column="1">
236+
<item row="2" column="1">
215237
<widget class="QToolButton" name="mMoveUpToolButton">
216238
<property name="text">
217-
<string>^</string>
239+
<string/>
240+
</property>
241+
</widget>
242+
</item>
243+
<item row="2" column="2">
244+
<widget class="QToolButton" name="mAddToolButton">
245+
<property name="text">
246+
<string/>
218247
</property>
219248
</widget>
220249
</item>
221-
<item row="1" column="2">
250+
<item row="2" column="3">
222251
<widget class="QToolButton" name="mRemoveToolButton">
223252
<property name="text">
224-
<string>X</string>
253+
<string/>
225254
</property>
226255
</widget>
227256
</item>
228-
<item row="1" column="3">
257+
<item row="2" column="4">
229258
<widget class="QToolButton" name="mEditPushButton">
230259
<property name="text">
231-
<string>Edit</string>
260+
<string/>
232261
</property>
233262
</widget>
234263
</item>
235-
<item row="1" column="4">
264+
<item row="2" column="5">
236265
<widget class="QToolButton" name="mUpdatePushButton">
237266
<property name="text">
238267
<string>Update</string>
239268
</property>
240269
</widget>
241270
</item>
242-
<item row="1" column="5">
271+
<item row="2" column="6">
243272
<widget class="QToolButton" name="mUpdateAllPushButton">
244273
<property name="text">
245274
<string>All</string>
246275
</property>
247276
</widget>
248277
</item>
249-
<item row="1" column="6">
278+
<item row="2" column="7">
250279
<widget class="QToolButton" name="mAddGroupButton">
251280
<property name="text">
252281
<string>Add group</string>

0 commit comments

Comments
 (0)
Please sign in to comment.