Skip to content

Commit

Permalink
[dualview] Fix crash when removing features
Browse files Browse the repository at this point in the history
  • Loading branch information
m-kuhn committed Sep 4, 2013
1 parent f28462a commit c0659b9
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 2 deletions.
28 changes: 26 additions & 2 deletions src/gui/attributetable/qgsdualview.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include "qgsexpressionbuilderdialog.h"
#include "qgsattributeaction.h"
#include "qgsvectordataprovider.h"
#include "qgsmessagelog.h"

#include <QDialog>
#include <QMenu>
Expand Down Expand Up @@ -229,6 +230,7 @@ void QgsDualView::initLayerCache( QgsVectorLayer* layer )
connect( layer, SIGNAL( editingStopped() ), this, SLOT( editingToggled() ) );
connect( layer, SIGNAL( attributeAdded( int ) ), this, SLOT( attributeAdded( int ) ) );
connect( layer, SIGNAL( attributeDeleted( int ) ), this, SLOT( attributeDeleted( int ) ) );
connect( layer, SIGNAL( featureDeleted( QgsFeatureId ) ), this, SLOT( featureDeleted( QgsFeatureId ) ) );
}

void QgsDualView::initModels( QgsMapCanvas* mapCanvas )
Expand All @@ -255,7 +257,7 @@ void QgsDualView::on_mFeatureList_currentEditSelectionChanged( const QgsFeature
// Backup old dialog and delete only after creating the new dialog, so we can "hot-swap" the contained QgsFeature
QgsAttributeDialog* oldDialog = mAttributeDialog;

if ( mAttributeDialog->dialog() )
if ( mAttributeDialog && mAttributeDialog->dialog() )
{
saveEditChanges();
mAttributeEditorLayout->removeWidget( mAttributeDialog->dialog() );
Expand Down Expand Up @@ -290,6 +292,12 @@ void QgsDualView::saveEditChanges()
// Get the edited feature
const QgsAttributes &dst = mAttributeDialog->feature()->attributes();

if ( src.count() != dst.count() )
{
// bail out
return;
}

mLayerCache->layer()->beginEditCommand( tr( "Attributes changed" ) );

for ( int i = 0; i < dst.count(); ++i )
Expand Down Expand Up @@ -348,7 +356,7 @@ void QgsDualView::previewColumnChanged( QObject* action )
void QgsDualView::editingToggled()
{
// Reload the attribute dialog widget and commit changes if any
if ( mAttributeDialog->dialog() && mAttributeDialog->feature() )
if ( mAttributeDialog && mAttributeDialog->dialog() && mAttributeDialog->feature() )
{
on_mFeatureList_currentEditSelectionChanged( *mAttributeDialog->feature() );
}
Expand Down Expand Up @@ -457,6 +465,22 @@ void QgsDualView::attributeAdded( int attribute )
}
}

void QgsDualView::featureDeleted( QgsFeatureId fid )
{
if ( mAttributeDialog && mAttributeDialog->dialog() )
{
QgsFeature* feat = mAttributeDialog->feature();
if ( feat )
{
if ( fid == feat->id() )
{
delete( mAttributeDialog );
mAttributeDialog = NULL;
}
}
}
}

void QgsDualView::reloadAttribute( const int& idx )
{
if ( mAttributeDialog && mAttributeDialog->dialog() )
Expand Down
8 changes: 8 additions & 0 deletions src/gui/attributetable/qgsdualview.h
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,14 @@ class GUI_EXPORT QgsDualView : public QStackedWidget, private Ui::QgsDualViewBas
*/
void attributeAdded( int attribute );

/**
* Gets called when a feature is deleted.
* So it can be removed from the feature form if required.
*
* @param fid The feature being deleted
*/
void featureDeleted( QgsFeatureId fid );

/**
* Will be called periodically, when loading layers from slow data providers.
*
Expand Down

0 comments on commit c0659b9

Please sign in to comment.