Index: src/app/CMakeLists.txt =================================================================== --- src/app/CMakeLists.txt (revision 14934) +++ src/app/CMakeLists.txt (working copy) @@ -103,6 +103,7 @@ composer/qgscomposershapewidget.cpp composer/qgscomposertablewidget.cpp composer/qgscomposerlegenditemdialog.cpp + composer/qgscomposerlegendlayersdialog.cpp composer/qgscomposerlegendwidget.cpp composer/qgscompositionwidget.cpp composer/qgsitempositiondialog.cpp Index: src/app/composer/qgscomposerlegendlayersdialog.cpp =================================================================== --- src/app/composer/qgscomposerlegendlayersdialog.cpp (revision 0) +++ src/app/composer/qgscomposerlegendlayersdialog.cpp (revision 0) @@ -0,0 +1,53 @@ +/*************************************************************************** + qgscomposerlegendlayersdialog.cpp + ------------------------------- + ***************************************************************************/ + +/*************************************************************************** + * * + * This program is free software; you can redistribute it and/or modify * + * it under the terms of the GNU General Public License as published by * + * the Free Software Foundation; either version 2 of the License, or * + * (at your option) any later version. * + * * + ***************************************************************************/ + +#include "qgscomposerlegendlayersdialog.h" + +#include + +QgsComposerLegendLayersDialog::QgsComposerLegendLayersDialog( QList layers, QWidget* parent ): QDialog( parent ) +{ + setupUi( this ); + + QList::iterator layerIt = layers.begin(); + for (; layerIt != layers.end(); ++layerIt ) + { + QListWidgetItem* item = new QListWidgetItem(( *layerIt )->name(), listMapLayers ); + mItemLayerMap.insert( item, *layerIt ); + } +} + +QgsComposerLegendLayersDialog::QgsComposerLegendLayersDialog(): QDialog( 0 ) +{ + +} + +QgsComposerLegendLayersDialog::~QgsComposerLegendLayersDialog() +{ + +} + +QgsMapLayer* QgsComposerLegendLayersDialog::selectedLayer() +{ + QListWidgetItem* item = listMapLayers->currentItem(); + if ( !item ) + { + return 0; + } + + QMap::iterator it = mItemLayerMap.find( item ); + QgsMapLayer* c = 0; + c = it.value(); + return c; +} Index: src/app/composer/qgscomposerlegendlayersdialog.h =================================================================== --- src/app/composer/qgscomposerlegendlayersdialog.h (revision 0) +++ src/app/composer/qgscomposerlegendlayersdialog.h (revision 0) @@ -0,0 +1,38 @@ +/*************************************************************************** + qgscomposerlegenditemdialog.h + ----------------------------- + ***************************************************************************/ + +/*************************************************************************** + * * + * This program is free software; you can redistribute it and/or modify * + * it under the terms of the GNU General Public License as published by * + * the Free Software Foundation; either version 2 of the License, or * + * (at your option) any later version. * + * * + ***************************************************************************/ + +#ifndef QGSCOMPOSERLEGENDLAYERSDIALOG_H +#define QGSCOMPOSERLEGENDLAYERSDIALOG_H + +#include "ui_qgscomposerlegendlayersdialogbase.h" +#include "qgsmaplayer.h" + +/** \ingroup MapComposer + * A dialog to add new layers to the legend. + * */ +class QgsComposerLegendLayersDialog: private Ui::QgsComposerLegendLayersDialogBase, public QDialog +{ + public: + QgsComposerLegendLayersDialog( QList layers, QWidget* parent = 0 ); + ~QgsComposerLegendLayersDialog(); + QgsMapLayer* selectedLayer(); + + private: + QgsComposerLegendLayersDialog(); //forbidden + + /**Stores the relation between items and map layer pointers. */ + QMap mItemLayerMap; +}; + +#endif //QGSCOMPOSERLEGENDITEMDIALOG_H Index: src/app/composer/qgscomposerlegendwidget.cpp =================================================================== --- src/app/composer/qgscomposerlegendwidget.cpp (revision 14934) +++ src/app/composer/qgscomposerlegendwidget.cpp (working copy) @@ -18,6 +18,7 @@ #include "qgscomposerlegendwidget.h" #include "qgscomposerlegend.h" #include "qgscomposerlegenditemdialog.h" +#include "qgscomposerlegendlayersdialog.h" #include "qgscomposeritemwidget.h" #include @@ -25,11 +26,21 @@ #include "qgisapp.h" #include "qgsmapcanvas.h" #include "qgsmaprenderer.h" +#include "qgsapplication.h" +#include + QgsComposerLegendWidget::QgsComposerLegendWidget( QgsComposerLegend* legend ): mLegend( legend ) { setupUi( this ); + // setup icons + mAddToolButton->setIcon( QIcon( QgsApplication::iconPath( "symbologyAdd.png" ) ) ); + mEditPushButton->setIcon( QIcon( QgsApplication::iconPath( "symbologyEdit.png" ) ) ); + mRemoveToolButton->setIcon( QIcon( QgsApplication::iconPath( "symbologyRemove.png" ) ) ); + mMoveUpToolButton->setIcon( QIcon( QgsApplication::iconPath( "symbologyUp.png" ) ) ); + mMoveDownToolButton->setIcon( QIcon( QgsApplication::iconPath( "symbologyDown.png" ) ) ); + //add widget for item properties QgsComposerItemWidget* itemPropertiesWidget = new QgsComposerItemWidget( this, legend ); toolBox->addItem( itemPropertiesWidget, tr( "Item Options" ) ); @@ -77,7 +88,6 @@ blockSignals( false ); } - void QgsComposerLegendWidget::on_mTitleLineEdit_textChanged( const QString& text ) { if ( mLegend ) @@ -360,6 +370,56 @@ mLegend->endCommand(); } +void QgsComposerLegendWidget::on_mCheckBoxAutoUpdate_stateChanged ( int state ) +{ + if ( state == Qt::Checked ) + { + mLegend->model()->setAutoUpdate( true ); + } + else + { + mLegend->model()->setAutoUpdate( false ); + } +} + +void QgsComposerLegendWidget::on_mAddToolButton_clicked() +{ + if ( !mLegend ) + { + return; + } + + QStandardItemModel* itemModel = qobject_cast( mItemTreeView->model() ); + if ( !itemModel ) + { + return; + } + + QgisApp* app = QgisApp::instance(); + if ( !app ) + { + return; + } + + QgsMapCanvas* canvas = app->mapCanvas(); + if ( canvas ) + { + QList layers = canvas->layers(); + + QgsComposerLegendLayersDialog addDialog( layers ); + if ( addDialog.exec() == QDialog::Accepted ) + { + QgsMapLayer* layer = addDialog.selectedLayer(); + if ( layer ) + { + mLegend->beginCommand( "Legend item added" ); + mLegend->model()->addLayer( layer ); + mLegend->endCommand(); + } + } + } +} + void QgsComposerLegendWidget::on_mRemoveToolButton_clicked() { if ( !mLegend ) Index: src/app/composer/qgscomposerlegendwidget.h =================================================================== --- src/app/composer/qgscomposerlegendwidget.h (revision 14934) +++ src/app/composer/qgscomposerlegendwidget.h (working copy) @@ -50,11 +50,13 @@ void on_mLayerFontButton_clicked(); void on_mItemFontButton_clicked(); void on_mBoxSpaceSpinBox_valueChanged( double d ); + void on_mCheckBoxAutoUpdate_stateChanged (int state ); //item manipulation void on_mMoveDownToolButton_clicked(); void on_mMoveUpToolButton_clicked(); void on_mRemoveToolButton_clicked(); + void on_mAddToolButton_clicked(); void on_mEditPushButton_clicked(); void on_mUpdatePushButton_clicked(); void on_mUpdateAllPushButton_clicked(); Index: src/core/composer/qgslegendmodel.cpp =================================================================== --- src/core/composer/qgslegendmodel.cpp (revision 14934) +++ src/core/composer/qgslegendmodel.cpp (working copy) @@ -32,6 +32,7 @@ #include #include #include +#include QgsLegendModel::QgsLegendModel(): QStandardItemModel() { @@ -40,6 +41,7 @@ connect( QgsMapLayerRegistry::instance(), SIGNAL( layerWillBeRemoved( QString ) ), this, SLOT( removeLayer( const QString& ) ) ); connect( QgsMapLayerRegistry::instance(), SIGNAL( layerWasAdded( QgsMapLayer* ) ), this, SLOT( addLayer( QgsMapLayer* ) ) ); } + setItemPrototype( new QgsComposerSymbolItem() ); QWidgetList topLevelWidgets = QApplication::topLevelWidgets(); @@ -674,3 +676,24 @@ emit layersChanged(); return true; } + +void QgsLegendModel::setAutoUpdate(bool autoUpdate) +{ + mAutoUpdate = autoUpdate; + if ( autoUpdate ) + { + if ( QgsMapLayerRegistry::instance() ) + { + connect( QgsMapLayerRegistry::instance(), SIGNAL( layerWillBeRemoved( QString ) ), this, SLOT( removeLayer( const QString& ) ) ); + connect( QgsMapLayerRegistry::instance(), SIGNAL( layerWasAdded( QgsMapLayer* ) ), this, SLOT( addLayer( QgsMapLayer* ) ) ); + } + } + else + { + if ( QgsMapLayerRegistry::instance() ) + { + disconnect( QgsMapLayerRegistry::instance(), SIGNAL( layerWillBeRemoved( QString ) ), this, SLOT( removeLayer( const QString& ) ) ); + disconnect( QgsMapLayerRegistry::instance(), SIGNAL( layerWasAdded( QgsMapLayer* ) ), this, SLOT( addLayer( QgsMapLayer* ) ) ); + } + } +} \ No newline at end of file Index: src/core/composer/qgslegendmodel.h =================================================================== --- src/core/composer/qgslegendmodel.h (revision 14934) +++ src/core/composer/qgslegendmodel.h (working copy) @@ -84,6 +84,8 @@ /**Implements the drop operation*/ bool dropMimeData( const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent ); + void setAutoUpdate( bool autoUpdate ); + bool AutoUpdaet() { return mAutoUpdate; } public slots: void removeLayer( const QString& layerId ); @@ -112,6 +114,9 @@ /**True if this application has toplevel windows (normally true). If this is false, this means that the application might not have a running x-server on unix systems and so QPixmap and QIcon cannot be used*/ bool mHasTopLevelWindow; + + /**True if the legend is auto updated when layers are added or removed from the map canvas */ + bool mAutoUpdate; }; #endif Index: src/ui/qgscomposerlegendlayersdialogbase.ui =================================================================== --- src/ui/qgscomposerlegendlayersdialogbase.ui (revision 0) +++ src/ui/qgscomposerlegendlayersdialogbase.ui (revision 0) @@ -0,0 +1,67 @@ + + + QgsComposerLegendLayersDialogBase + + + + 0 + 0 + 252 + 194 + + + + Add layer to legend + + + + + + + + + Qt::Horizontal + + + QDialogButtonBox::Cancel|QDialogButtonBox::Ok + + + + + + + + + buttonBox + accepted() + QgsComposerLegendLayersDialogBase + accept() + + + 248 + 254 + + + 157 + 274 + + + + + buttonBox + rejected() + QgsComposerLegendLayersDialogBase + reject() + + + 316 + 260 + + + 286 + 274 + + + + + Index: src/ui/qgscomposerlegendwidgetbase.ui =================================================================== --- src/ui/qgscomposerlegendwidgetbase.ui (revision 14934) +++ src/ui/qgscomposerlegendwidgetbase.ui (working copy) @@ -6,8 +6,8 @@ 0 0 - 369 - 471 + 371 + 476 @@ -34,7 +34,7 @@ 0 0 367 - 469 + 472 @@ -49,7 +49,7 @@ 0 0 349 - 397 + 398 @@ -184,14 +184,14 @@ 0 0 349 - 397 + 398 Legend items - + @@ -199,60 +199,92 @@ 0 + + false + true + + false + + + false + - + - v + - + - ^ + - + + + + + + + + - X + - + - Edit + - + Update - + All - + Add group + + + + + 0 + 0 + + + + Auto Update + + + true + + +