Skip to content

Commit

Permalink
Prevent undo/redo history when layer is being tracked
Browse files Browse the repository at this point in the history
  • Loading branch information
suricactus committed Dec 17, 2023
1 parent 8f252c9 commit fcf5c47
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 4 deletions.
24 changes: 22 additions & 2 deletions src/core/featurehistory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@
#include <qgsvectorlayer.h>
#include <qgsvectorlayereditbuffer.h>

#include <tracker.h>

FeatureHistory::FeatureHistory( const QgsProject *project )
FeatureHistory::FeatureHistory( const QgsProject *project, TrackingModel *trackingModel )
: mProject( project )
, mTrackingModel( trackingModel )
{
connect( mProject, &QgsProject::homePathChanged, this, &FeatureHistory::onHomePathChanged );
connect( mProject, &QgsProject::layersAdded, this, &FeatureHistory::onLayersAdded );
Expand Down Expand Up @@ -72,6 +74,12 @@ void FeatureHistory::onBeforeCommitChanges()
return;
}

Tracker *layerTracker = mTrackingModel->trackerForLayer( vl );
if ( layerTracker && layerTracker->isActive() )
{
return;
}

QgsVectorLayerEditBuffer *eb = vl->editBuffer();

if ( !eb )
Expand Down Expand Up @@ -117,13 +125,19 @@ void FeatureHistory::onCommittedFeaturesAdded( const QString &localLayerId, cons
return;
}

const QgsVectorLayer *vl = qobject_cast<QgsVectorLayer *>( sender() );
QgsVectorLayer *vl = qobject_cast<QgsVectorLayer *>( sender() );

if ( !vl )
{
return;
}

Tracker *layerTracker = mTrackingModel->trackerForLayer( vl );
if ( layerTracker && layerTracker->isActive() )
{
return;
}

qDebug() << "FeatureHistory::onCommittedFeaturesAdded: adding create committed features";

FeatureModifications modifications = mTempHistoryStep.take( vl->id() );
Expand Down Expand Up @@ -160,6 +174,12 @@ void FeatureHistory::onAfterCommitChanges()
return;
}

Tracker *layerTracker = mTrackingModel->trackerForLayer( vl );
if ( layerTracker && layerTracker->isActive() )
{
return;
}

const QString layerId = vl->id();
FeatureModifications modifications = mTempHistoryStep.take( layerId );
QMap<QgsFeatureId, QgsFeature> modifiedFeaturesOld = mTempModifiedFeaturesByLayerId.take( layerId );
Expand Down
7 changes: 6 additions & 1 deletion src/core/featurehistory.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@
#include <QTimer>
#include <qgsproject.h>

#include <trackingmodel.h>

typedef QPair<QgsFeature, QgsFeature> OldNewFeaturePair;


class FeatureHistory : public QObject
{
Q_OBJECT
Expand All @@ -34,7 +36,7 @@ class FeatureHistory : public QObject
*
* @param project
*/
explicit FeatureHistory( const QgsProject *project );
explicit FeatureHistory( const QgsProject *project, TrackingModel *trackingModel );

//! Perform undo of the most recent modification step
Q_INVOKABLE bool undo();
Expand Down Expand Up @@ -96,6 +98,9 @@ class FeatureHistory : public QObject
//! The current project instance.
const QgsProject *mProject = nullptr;

//! Tracking model. Used to check if the currently modified layer is in tracking, so all changes should be ignored to prevent cluttering the undo history.
TrackingModel *mTrackingModel = nullptr;

//! If currently applying undo or redo feature modifications.
bool mIsApplyingModifications = false;

Expand Down
2 changes: 1 addition & 1 deletion src/core/qgismobileapp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ QgisMobileapp::QgisMobileapp( QgsApplication *app, QObject *parent )
mTrackingModel = new TrackingModel();
mGpkgFlusher = std::make_unique<QgsGpkgFlusher>( mProject );
mLayerObserver = std::make_unique<LayerObserver>( mProject );
mFeatureHistory = std::make_unique<FeatureHistory>( mProject );
mFeatureHistory = std::make_unique<FeatureHistory>( mProject, mTrackingModel );
mFlatLayerTree = new FlatLayerTreeModel( mProject->layerTreeRoot(), mProject, this );
mLegendImageProvider = new LegendImageProvider( mFlatLayerTree->layerTreeModel() );
mLocalFilesImageProvider = new LocalFilesImageProvider();
Expand Down

0 comments on commit fcf5c47

Please sign in to comment.