Skip to content

Commit 98cae5d

Browse files
author
mhugent
committed
Fix patch #14951. Read/Write auto update to xml, initialize mAutoUpdate and guard against multiple connections
git-svn-id: http://svn.osgeo.org/qgis/trunk@14957 c8812cc2-4d05-0410-92ff-de0c093fc19c
1 parent cd2a18c commit 98cae5d

File tree

4 files changed

+43
-21
lines changed

4 files changed

+43
-21
lines changed

src/app/composer/qgscomposerlegendwidget.cpp

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ QgsComposerLegendWidget::QgsComposerLegendWidget( QgsComposerLegend* legend ): m
3434
{
3535
setupUi( this );
3636

37-
// setup icons
37+
// setup icons
3838
mAddToolButton->setIcon( QIcon( QgsApplication::iconPath( "symbologyAdd.png" ) ) );
3939
mEditPushButton->setIcon( QIcon( QgsApplication::iconPath( "symbologyEdit.png" ) ) );
4040
mRemoveToolButton->setIcon( QIcon( QgsApplication::iconPath( "symbologyRemove.png" ) ) );
@@ -76,16 +76,20 @@ void QgsComposerLegendWidget::setGuiElements()
7676
return;
7777
}
7878

79-
blockSignals( true );
79+
blockAllSignals( true );
8080
mTitleLineEdit->setText( mLegend->title() );
8181
mSymbolWidthSpinBox->setValue( mLegend->symbolWidth() );
8282
mSymbolHeightSpinBox->setValue( mLegend->symbolHeight() );
8383
mLayerSpaceSpinBox->setValue( mLegend->layerSpace() );
8484
mSymbolSpaceSpinBox->setValue( mLegend->symbolSpace() );
8585
mIconLabelSpaceSpinBox->setValue( mLegend->iconLabelSpace() );
8686
mBoxSpaceSpinBox->setValue( mLegend->boxSpace() );
87+
if ( mLegend->model() )
88+
{
89+
mCheckBoxAutoUpdate->setChecked( mLegend->model()->autoUpdate() );
90+
}
8791

88-
blockSignals( false );
92+
blockAllSignals( false );
8993
}
9094

9195
void QgsComposerLegendWidget::on_mTitleLineEdit_textChanged( const QString& text )
@@ -370,8 +374,13 @@ void QgsComposerLegendWidget::on_mMoveUpToolButton_clicked()
370374
mLegend->endCommand();
371375
}
372376

373-
void QgsComposerLegendWidget::on_mCheckBoxAutoUpdate_stateChanged ( int state )
377+
void QgsComposerLegendWidget::on_mCheckBoxAutoUpdate_stateChanged( int state )
374378
{
379+
if ( !mLegend->model() )
380+
{
381+
return;
382+
}
383+
375384
if ( state == Qt::Checked )
376385
{
377386
mLegend->model()->setAutoUpdate( true );
@@ -409,13 +418,13 @@ void QgsComposerLegendWidget::on_mAddToolButton_clicked()
409418
QgsComposerLegendLayersDialog addDialog( layers );
410419
if ( addDialog.exec() == QDialog::Accepted )
411420
{
412-
QgsMapLayer* layer = addDialog.selectedLayer();
413-
if ( layer )
414-
{
415-
mLegend->beginCommand( "Legend item added" );
416-
mLegend->model()->addLayer( layer );
417-
mLegend->endCommand();
418-
}
421+
QgsMapLayer* layer = addDialog.selectedLayer();
422+
if ( layer )
423+
{
424+
mLegend->beginCommand( "Legend item added" );
425+
mLegend->model()->addLayer( layer );
426+
mLegend->endCommand();
427+
}
419428
}
420429
}
421430
}
@@ -569,3 +578,9 @@ void QgsComposerLegendWidget::updateLegend()
569578
mLegend->endCommand();
570579
}
571580
}
581+
582+
void QgsComposerLegendWidget::blockAllSignals( bool b )
583+
{
584+
mItemTreeView->blockSignals( b );
585+
mCheckBoxAutoUpdate->blockSignals( b );
586+
}

src/app/composer/qgscomposerlegendwidget.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ 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 );
53+
void on_mCheckBoxAutoUpdate_stateChanged( int state );
5454

5555
//item manipulation
5656
void on_mMoveDownToolButton_clicked();
@@ -68,6 +68,7 @@ class QgsComposerLegendWidget: public QWidget, private Ui::QgsComposerLegendWidg
6868

6969
private:
7070
QgsComposerLegendWidget();
71+
void blockAllSignals( bool b );
7172

7273

7374
QgsComposerLegend* mLegend;

src/core/composer/qgslegendmodel.cpp

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,13 @@
3434
#include <QSettings>
3535
#include <QMessageBox>
3636

37-
QgsLegendModel::QgsLegendModel(): QStandardItemModel()
37+
QgsLegendModel::QgsLegendModel(): QStandardItemModel(), mAutoUpdate( true )
3838
{
3939
if ( QgsMapLayerRegistry::instance() )
4040
{
4141
connect( QgsMapLayerRegistry::instance(), SIGNAL( layerWillBeRemoved( QString ) ), this, SLOT( removeLayer( const QString& ) ) );
4242
connect( QgsMapLayerRegistry::instance(), SIGNAL( layerWasAdded( QgsMapLayer* ) ), this, SLOT( addLayer( QgsMapLayer* ) ) );
4343
}
44-
4544
setItemPrototype( new QgsComposerSymbolItem() );
4645

4746
QWidgetList topLevelWidgets = QApplication::topLevelWidgets();
@@ -318,14 +317,13 @@ void QgsLegendModel::removeLayer( const QString& layerId )
318317
int numRootItems = rowCount();
319318
for ( int i = 0; i < numRootItems ; ++i )
320319
{
321-
currentLayerItem = item( i );
322-
if ( !currentLayerItem )
320+
QgsComposerLayerItem* lItem = dynamic_cast<QgsComposerLayerItem*>( item( i ) );
321+
if ( !lItem )
323322
{
324323
continue;
325324
}
326325

327-
QString currentId = currentLayerItem->data( Qt::UserRole + 2 ).toString();
328-
if ( currentId == layerId )
326+
if ( layerId == lItem->layerID() )
329327
{
330328
removeRow( i ); //todo: also remove the subitems and their symbols...
331329
emit layersChanged();
@@ -461,6 +459,7 @@ bool QgsLegendModel::writeXML( QDomElement& composerLegendElem, QDomDocument& do
461459
}
462460

463461
QDomElement legendModelElem = doc.createElement( "Model" );
462+
legendModelElem.setAttribute( "autoUpdate", mAutoUpdate );
464463
int nTopLevelItems = invisibleRootItem()->rowCount();
465464
QStandardItem* currentItem = 0;
466465
QgsComposerLegendItem* currentLegendItem = 0;
@@ -513,6 +512,8 @@ bool QgsLegendModel::readXML( const QDomElement& legendModelElem, const QDomDocu
513512
currentItem->readXML( currentElem );
514513
appendRow( currentItem );
515514
}
515+
516+
setAutoUpdate( legendModelElem.attribute( "autoUpdate", "1" ).toInt() );
516517
return true;
517518
}
518519

@@ -677,8 +678,13 @@ bool QgsLegendModel::dropMimeData( const QMimeData *data, Qt::DropAction action,
677678
return true;
678679
}
679680

680-
void QgsLegendModel::setAutoUpdate(bool autoUpdate)
681+
void QgsLegendModel::setAutoUpdate( bool autoUpdate )
681682
{
683+
if ( mAutoUpdate == autoUpdate ) //prevent multiple signal/slot connections
684+
{
685+
return;
686+
}
687+
682688
mAutoUpdate = autoUpdate;
683689
if ( autoUpdate )
684690
{
@@ -696,4 +702,4 @@ void QgsLegendModel::setAutoUpdate(bool autoUpdate)
696702
disconnect( QgsMapLayerRegistry::instance(), SIGNAL( layerWasAdded( QgsMapLayer* ) ), this, SLOT( addLayer( QgsMapLayer* ) ) );
697703
}
698704
}
699-
}
705+
}

src/core/composer/qgslegendmodel.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ class CORE_EXPORT QgsLegendModel: public QStandardItemModel
8585
bool dropMimeData( const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent );
8686

8787
void setAutoUpdate( bool autoUpdate );
88-
bool AutoUpdaet() { return mAutoUpdate; }
88+
bool autoUpdate() { return mAutoUpdate; }
8989

9090
public slots:
9191
void removeLayer( const QString& layerId );

0 commit comments

Comments
 (0)