Skip to content

Commit

Permalink
add a save button for subform (1-n relations)
Browse files Browse the repository at this point in the history
  • Loading branch information
Gillian Milani authored and m-kuhn committed Nov 24, 2014
1 parent 6b9f85a commit 6f4809d
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 0 deletions.
7 changes: 7 additions & 0 deletions python/gui/qgsvectorlayertools.sip
Expand Up @@ -37,4 +37,11 @@ class QgsVectorLayerTools
*/
virtual bool stopEditing( QgsVectorLayer* layer, bool allowCancel = true ) const = 0;

/**
* Should be called, when the features should be commited but the editing session is not ended.
*
* @param layer The layer to commit
* @return True if successful
*/
virtual bool saveEdits( QgsVectorLayer* layer) const = 0;
};
26 changes: 26 additions & 0 deletions src/app/qgsguivectorlayertools.cpp
Expand Up @@ -65,6 +65,32 @@ bool QgsGuiVectorLayerTools::startEditing( QgsVectorLayer* layer ) const
return res;
}

bool QgsGuiVectorLayerTools::saveEdits( QgsVectorLayer* layer) const
{
bool res = true;

if ( layer->isModified() )
{
if ( !layer->commitChanges() )
{
commitError( layer );
// Leave the in-memory editing state alone,
// to give the user a chance to enter different values
// and try the commit again later
res = false;
}
layer->triggerRepaint();
layer->startEditing();
}
else //layer not modified
{
res = true;
layer->triggerRepaint();
}
return res;
}


bool QgsGuiVectorLayerTools::stopEditing( QgsVectorLayer* layer, bool allowCancel ) const
{
bool res = true;
Expand Down
8 changes: 8 additions & 0 deletions src/app/qgsguivectorlayertools.h
Expand Up @@ -61,6 +61,14 @@ class QgsGuiVectorLayerTools : public QgsVectorLayerTools, public QObject
*/
bool stopEditing( QgsVectorLayer* layer, bool allowCancel = true ) const;

/**
* Should be called, when the features should be commited but the editing session is not ended.
*
* @param layer The layer to commit
* @return True if successful
*/
bool saveEdits( QgsVectorLayer* layer) const;

private:
void commitError( QgsVectorLayer* vlayer ) const;
};
Expand Down
13 changes: 13 additions & 0 deletions src/gui/qgsrelationeditorwidget.cpp
Expand Up @@ -50,6 +50,12 @@ QgsRelationEditorWidget::QgsRelationEditorWidget( QWidget* parent )
mToggleEditingButton->setEnabled( false );
mToggleEditingButton->setCheckable( true );
buttonLayout->addWidget( mToggleEditingButton );
// save Edits
mSaveEditsButton = new QToolButton( this );
mSaveEditsButton->setIcon( QgsApplication::getThemeIcon( "/mActionSaveEdits.svg" ) );
mSaveEditsButton->setText( tr( "Save layer edits" ) );
mSaveEditsButton->setEnabled( true );
buttonLayout->addWidget( mSaveEditsButton );
// add feature
mAddFeatureButton = new QToolButton( this );
mAddFeatureButton->setIcon( QgsApplication::getThemeIcon( "/mActionAdd.svg" ) );
Expand Down Expand Up @@ -111,6 +117,7 @@ QgsRelationEditorWidget::QgsRelationEditorWidget( QWidget* parent )
connect( this, SIGNAL( collapsedStateChanged( bool ) ), this, SLOT( onCollapsedStateChanged( bool ) ) );
connect( mViewModeButtonGroup, SIGNAL( buttonClicked( int ) ), this, SLOT( setViewMode( int ) ) );
connect( mToggleEditingButton, SIGNAL( clicked( bool ) ), this, SLOT( toggleEditing( bool ) ) );
connect( mSaveEditsButton, SIGNAL( clicked() ), this, SLOT( saveEdits() ) );
connect( mAddFeatureButton, SIGNAL( clicked() ), this, SLOT( addFeature() ) );
connect( mDeleteFeatureButton, SIGNAL( clicked() ), this, SLOT( deleteFeature() ) );
connect( mLinkFeatureButton, SIGNAL( clicked() ), this, SLOT( linkFeature() ) );
Expand Down Expand Up @@ -174,6 +181,7 @@ void QgsRelationEditorWidget::referencingLayerEditingToggled()
mDeleteFeatureButton->setEnabled( editable );
mUnlinkFeatureButton->setEnabled( editable );
mToggleEditingButton->setChecked( editable );
mSaveEditsButton->setEnabled( editable );
}

void QgsRelationEditorWidget::addFeature()
Expand Down Expand Up @@ -257,6 +265,11 @@ void QgsRelationEditorWidget::toggleEditing( bool state )
}
}

void QgsRelationEditorWidget::saveEdits()
{
mEditorContext.vectorLayerTools()->saveEdits(mRelation.referencingLayer() );
}

void QgsRelationEditorWidget::onCollapsedStateChanged( bool collapsed )
{
if ( !mInitialized && !collapsed && mRelation.isValid() )
Expand Down
2 changes: 2 additions & 0 deletions src/gui/qgsrelationeditorwidget.h
Expand Up @@ -64,6 +64,7 @@ class GUI_EXPORT QgsRelationEditorWidget : public QgsCollapsibleGroupBox
void linkFeature();
void deleteFeature();
void unlinkFeature();
void saveEdits();
void toggleEditing( bool state );
void onCollapsedStateChanged( bool collapsed );

Expand All @@ -77,6 +78,7 @@ class GUI_EXPORT QgsRelationEditorWidget : public QgsCollapsibleGroupBox
QgsFeature mFeature;

QToolButton* mToggleEditingButton;
QToolButton* mSaveEditsButton;
QToolButton* mAddFeatureButton;
QToolButton* mDeleteFeatureButton;
QToolButton* mLinkFeatureButton;
Expand Down
8 changes: 8 additions & 0 deletions src/gui/qgsvectorlayertools.h
Expand Up @@ -71,6 +71,14 @@ class GUI_EXPORT QgsVectorLayerTools
*/
virtual bool stopEditing( QgsVectorLayer* layer, bool allowCancel = true ) const = 0;

/**
* Should be called, when the features should be commited but the editing session is not ended.
*
* @param layer The layer to commit
* @return True if successful
*/
virtual bool saveEdits( QgsVectorLayer* layer ) const = 0;

};

#endif // QGSVECTORLAYERTOOLS_H

0 comments on commit 6f4809d

Please sign in to comment.