Skip to content

Commit 7d33323

Browse files
committed
refactoring LayersProxyModel to RecordingLayersProxyModel
1 parent aedc232 commit 7d33323

15 files changed

Lines changed: 115 additions & 161 deletions

app/CMakeLists.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ set(MM_SRCS
6565
invitationsmodel.cpp
6666
invitationsproxymodel.cpp
6767
layersmodel.cpp
68-
layersproxymodel.cpp
68+
recordinglayersproxymodel.cpp
6969
main.cpp
7070
mapthemesmodel.cpp
7171
notificationmodel.cpp
@@ -149,7 +149,7 @@ set(MM_HDRS
149149
invitationsmodel.h
150150
invitationsproxymodel.h
151151
layersmodel.h
152-
layersproxymodel.h
152+
recordinglayersproxymodel.h
153153
mapthemesmodel.h
154154
notificationmodel.h
155155
projectsmodel.h

app/activeproject.cpp

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -186,8 +186,8 @@ bool ActiveProject::forceLoad( const QString &filePath, bool force )
186186
setProjectRole( role );
187187

188188
updateMapTheme();
189-
updateActiveLayer();
190189
updateMapSettingsLayers();
190+
updateActiveLayer();
191191

192192
emit localProjectChanged( mLocalProject );
193193
emit projectReloaded( mQgsProject );
@@ -465,11 +465,11 @@ void ActiveProject::setMapTheme( const QString &themeName )
465465

466466
void ActiveProject::updateActiveLayer()
467467
{
468-
if ( !InputUtils::layerVisible( mActiveLayer.layer(), mQgsProject ) )
468+
if ( !mMapSettings->layers().contains( mActiveLayer.layer() ) )
469469
{
470470
QgsMapLayer *defaultAppSettingsLayer = InputUtils::mapLayerFromName( mAppSettings.defaultLayer(), mQgsProject );
471471

472-
if ( InputUtils::recordingAllowed( defaultAppSettingsLayer, mQgsProject ) )
472+
if ( recordingAllowed( defaultAppSettingsLayer ) )
473473
{
474474
setActiveLayer( defaultAppSettingsLayer );
475475
return;
@@ -481,7 +481,7 @@ void ActiveProject::updateActiveLayer()
481481
for ( auto it = layers.cbegin(); it != layers.cend(); ++it )
482482
{
483483
QgsMapLayer *layer = it.value();
484-
if ( InputUtils::recordingAllowed( layer, mQgsProject ) )
484+
if ( recordingAllowed( layer ) )
485485
{
486486
defaultLayer = layer;
487487
break;
@@ -519,8 +519,8 @@ void ActiveProject::switchLayerTreeNodeVisibility( QgsLayerTreeNode *node )
519519
node->setItemVisibilityChecked( !node->isVisible() );
520520

521521
updateMapTheme();
522-
updateActiveLayer();
523522
updateMapSettingsLayers();
523+
updateActiveLayer();
524524
}
525525

526526
const QString &ActiveProject::mapTheme() const
@@ -546,7 +546,7 @@ bool ActiveProject::projectHasRecordingLayers() const
546546
const QMap<QString, QgsMapLayer *> layers = mQgsProject->mapLayers();
547547
for ( auto it = layers.constBegin(); it != layers.constEnd(); ++it )
548548
{
549-
if ( InputUtils::recordingAllowed( it.value(), mQgsProject ) )
549+
if ( recordingAllowed( it.value() ) )
550550
return true;
551551
}
552552

@@ -567,3 +567,20 @@ void ActiveProject::setProjectRole( const QString &role )
567567
emit projectRoleChanged();
568568
}
569569
}
570+
571+
bool ActiveProject::recordingAllowed( QgsMapLayer *layer ) const
572+
{
573+
if ( !layer )
574+
return false;
575+
576+
return QgsMapLayerProxyModel::layerMatchesFilters( layer, Qgis::LayerFilter::HasGeometry | Qgis::LayerFilter::WritableLayer ) && layer->id() != positionTrackingLayerId();
577+
}
578+
579+
QString ActiveProject::positionTrackingLayerId() const
580+
{
581+
if ( !mQgsProject )
582+
return QString();
583+
584+
return mQgsProject->readEntry( QStringLiteral( "Mergin" ), QStringLiteral( "PositionTracking/TrackingLayer" ), QString() );
585+
}
586+

app/activeproject.h

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
#include "inputconfig.h"
1919
#include "appsettings.h"
2020
#include "activelayer.h"
21-
#include "layersproxymodel.h"
21+
#include "recordinglayersproxymodel.h"
2222
#include "localprojectsmanager.h"
2323
#include "autosynccontroller.h"
2424
#include "inputmapsettings.h"
@@ -116,12 +116,21 @@ class ActiveProject: public QObject
116116

117117
//! Returns true if the project has at least one layer that allows recording
118118
Q_INVOKABLE bool projectHasRecordingLayers() const;
119+
119120
/**
120121
* Returns role/permission level of current user for this project
121122
*/
122123
Q_INVOKABLE QString projectRole() const;
123124
void setProjectRole( const QString &role );
124125

126+
/**
127+
* Returns true if the layer allows recording:
128+
*/
129+
bool recordingAllowed( QgsMapLayer *layer ) const ;
130+
131+
//! Returns position tracking layer ID if exists
132+
Q_INVOKABLE QString positionTrackingLayerId() const;
133+
125134
signals:
126135
void qgsProjectChanged();
127136
void localProjectChanged( LocalProject project );

app/inpututils.cpp

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2257,20 +2257,6 @@ bool InputUtils::isPositionTrackingLayer( QgsMapLayer *layer, QgsProject *projec
22572257
return layer->id() == trackingLayerId;
22582258
}
22592259

2260-
bool InputUtils::recordingAllowed( QgsMapLayer *layer, QgsProject *project )
2261-
{
2262-
if ( !layer || !layer->isValid() || !project )
2263-
return false;
2264-
2265-
QgsVectorLayer *vectorLayer = qobject_cast<QgsVectorLayer *>( layer );
2266-
2267-
return ( vectorLayer &&
2268-
!vectorLayer->readOnly() &&
2269-
layerHasGeometry( vectorLayer ) &&
2270-
layerVisible( layer, project ) &&
2271-
!isPositionTrackingLayer( layer, project ) );
2272-
}
2273-
22742260
QgsMapLayer *InputUtils::mapLayerFromName( const QString &layerName, QgsProject *project )
22752261
{
22762262
if ( !project || layerName.isEmpty() )

app/inpututils.h

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -604,11 +604,6 @@ class InputUtils: public QObject
604604
*/
605605
Q_INVOKABLE static bool isPositionTrackingLayer( QgsMapLayer *layer, QgsProject *project );
606606

607-
/**
608-
* Returns true if the layer allows recording
609-
*/
610-
static bool recordingAllowed( QgsMapLayer *layer, QgsProject *project );
611-
612607
/**
613608
* Returns QgsMapLayer pointer for given layer name and project.
614609
* If layer with given name does not exist or there is no project, returns nullptr.

app/layersmodel.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ QVariant LayersModel::data( const QModelIndex &index, int role ) const
5454
else return MMStyle::rasterLayerNoColorOverlayIcon();
5555
}
5656
case LayerIdRole: return layer->id();
57+
case LayerVisible: return ( mProject && InputUtils::layerVisible( layer, mProject ) );
5758
}
5859
return QVariant();
5960
}

app/layersmodel.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include <QPointer>
1616

1717
#include "inputconfig.h"
18+
#include "inpututils.h"
1819
#include "qgsmaplayer.h"
1920
#include "qgsmaplayermodel.h"
2021

@@ -37,7 +38,8 @@ class LayersModel : public QgsMapLayerModel
3738
VectorLayerRole,
3839
HasGeometryRole,
3940
IconSourceRole,
40-
LayerIdRole
41+
LayerIdRole,
42+
LayerVisible
4143
};
4244
Q_ENUM( LayerRoles )
4345

app/main.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@
4646
#include "merginsubscriptioninfo.h"
4747
#include "merginsubscriptionstatus.h"
4848
#include "merginprojectstatusmodel.h"
49-
#include "layersproxymodel.h"
49+
#include "recordinglayersproxymodel.h"
5050
#include "layersmodel.h"
5151
#include "activelayer.h"
5252
#include "merginuserauth.h"
@@ -350,7 +350,7 @@ void initDeclarative()
350350
qmlRegisterType< MeasurementMapTool >( "mm", 1, 0, "MeasurementMapTool" );
351351

352352
// layers model
353-
qmlRegisterType<LayersProxyModel>( "mm", 1, 0, "LayersProxyModel" );
353+
qmlRegisterType<RecordingLayersProxyModel>( "mm", 1, 0, "RecordingLayersProxyModel" );
354354
qmlRegisterType<LayersModel>( "mm", 1, 0, "LayersModel" );
355355
}
356356

app/qml/map/MMMapController.qml

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -814,11 +814,10 @@ Item {
814814

815815
onClosed: activeLayerPanelLoader.active = false
816816

817-
list.model: MM.LayersProxyModel {
817+
list.model: MM.RecordingLayersProxyModel {
818818
id: recordingLayersModel
819819

820-
qgsProject: __activeProject.qgsProject
821-
modelType: MM.LayersProxyModel.ActiveLayerSelection
820+
exceptedLayerIds: __activeProject.positionTrackingLayerId()
822821
model: MM.LayersModel {}
823822
}
824823

Lines changed: 16 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -8,21 +8,19 @@
88
***************************************************************************/
99

1010
#include "qgsvectorlayer.h"
11-
#include "layersproxymodel.h"
11+
#include "recordinglayersproxymodel.h"
1212

1313
#include "qgsproject.h"
1414
#include "qgslayertree.h"
1515

16-
LayersProxyModel::LayersProxyModel( QObject *parent ) :
16+
RecordingLayersProxyModel::RecordingLayersProxyModel( QObject *parent ) :
1717
QgsMapLayerProxyModel{ parent }
1818
{
19-
QObject::connect( this, &LayersProxyModel::rowsInserted, this, &LayersProxyModel::countChanged );
20-
QObject::connect( this, &LayersProxyModel::rowsRemoved, this, &LayersProxyModel::countChanged );
21-
22-
updateFilterFunction();
19+
QObject::connect( this, &RecordingLayersProxyModel::rowsInserted, this, &RecordingLayersProxyModel::countChanged );
20+
QObject::connect( this, &RecordingLayersProxyModel::rowsRemoved, this, &RecordingLayersProxyModel::countChanged );
2321
}
2422

25-
bool LayersProxyModel::filterAcceptsRow( int source_row, const QModelIndex &source_parent ) const
23+
bool RecordingLayersProxyModel::filterAcceptsRow( int source_row, const QModelIndex &source_parent ) const
2624
{
2725
if ( !QgsMapLayerProxyModel::filterAcceptsRow( source_row, source_parent ) )
2826
return false;
@@ -31,10 +29,10 @@ bool LayersProxyModel::filterAcceptsRow( int source_row, const QModelIndex &sour
3129
QModelIndex index = mModel->index( source_row, 0, source_parent );
3230
QgsMapLayer *layer = mModel->layerFromIndex( index );
3331

34-
return filterFunction( layer );
32+
return mModel->data( index, LayersModel::LayerVisible ).toBool();
3533
}
3634

37-
QList<QgsMapLayer *> LayersProxyModel::layers() const
35+
QList<QgsMapLayer *> RecordingLayersProxyModel::layers() const
3836
{
3937
QList<QgsMapLayer *> filteredLayers;
4038

@@ -52,12 +50,12 @@ QList<QgsMapLayer *> LayersProxyModel::layers() const
5250
return filteredLayers;
5351
}
5452

55-
void LayersProxyModel::refreshData()
53+
void RecordingLayersProxyModel::refreshData()
5654
{
5755
invalidate();
5856
}
5957

60-
QgsMapLayer *LayersProxyModel::firstUsableLayer() const
58+
QgsMapLayer *RecordingLayersProxyModel::firstUsableLayer() const
6159
{
6260
QList<QgsMapLayer *> filteredLayers = layers();
6361

@@ -69,7 +67,7 @@ QgsMapLayer *LayersProxyModel::firstUsableLayer() const
6967
return nullptr;
7068
}
7169

72-
QModelIndex LayersProxyModel::indexFromLayerId( QString layerId ) const
70+
QModelIndex RecordingLayersProxyModel::indexFromLayerId( QString layerId ) const
7371
{
7472
if ( layerId.isEmpty() )
7573
return QModelIndex();
@@ -79,7 +77,7 @@ QModelIndex LayersProxyModel::indexFromLayerId( QString layerId ) const
7977
return mModel->indexFromLayer( layer ); // return source model index to skip converting indexes in proxy model
8078
}
8179

82-
QgsVectorLayer *LayersProxyModel::layerFromLayerId( QString layerId ) const
80+
QgsVectorLayer *RecordingLayersProxyModel::layerFromLayerId( QString layerId ) const
8381
{
8482
QList<QgsMapLayer *> filteredLayers = layers();
8583

@@ -95,7 +93,7 @@ QgsVectorLayer *LayersProxyModel::layerFromLayerId( QString layerId ) const
9593
return nullptr;
9694
}
9795

98-
QgsVectorLayer *LayersProxyModel::layerFromLayerName( const QString &layerName ) const
96+
QgsVectorLayer *RecordingLayersProxyModel::layerFromLayerName( const QString &layerName ) const
9997
{
10098
QList<QgsMapLayer *> filteredLayers = layers();
10199

@@ -111,76 +109,23 @@ QgsVectorLayer *LayersProxyModel::layerFromLayerName( const QString &layerName )
111109
return nullptr;
112110
}
113111

114-
QVariant LayersProxyModel::getData( QModelIndex index, int role ) const
112+
QVariant RecordingLayersProxyModel::getData( QModelIndex index, int role ) const
115113
{
116114
return sourceModel()->data( index, role );
117115
}
118116

119-
QgsProject *LayersProxyModel::qgsProject() const
120-
{
121-
return mProject;
122-
}
123-
124-
void LayersProxyModel::setQgsProject( QgsProject *project )
125-
{
126-
if ( mProject != project )
127-
{
128-
mProject = project;
129-
emit qgsProjectChanged();
130-
}
131-
}
132-
133-
LayersProxyModel::LayerModelTypes LayersProxyModel::modelType() const
134-
{
135-
return mModelType;
136-
}
137-
138-
void LayersProxyModel::setModelType( LayerModelTypes type )
139-
{
140-
if ( mModelType != type )
141-
{
142-
mModelType = type;
143-
144-
updateFilterFunction();
145-
146-
emit modelTypeChanged();
147-
}
148-
}
149-
150-
LayersModel *LayersProxyModel::model() const
117+
LayersModel *RecordingLayersProxyModel::model() const
151118
{
152119
return mModel;
153120
}
154121

155-
void LayersProxyModel::setModel( LayersModel *model )
122+
void RecordingLayersProxyModel::setModel( LayersModel *model )
156123
{
157124
if ( mModel != model )
158125
{
159126
mModel = model;
160127
setSourceModel( mModel );
128+
setFilters( Qgis::LayerFilter::HasGeometry | Qgis::LayerFilter::WritableLayer );
161129
emit modelChanged();
162130
}
163131
}
164-
165-
void LayersProxyModel::updateFilterFunction()
166-
{
167-
beginResetModel();
168-
169-
switch ( mModelType )
170-
{
171-
case ActiveLayerSelection:
172-
filterFunction = [this]( QgsMapLayer * layer )
173-
{
174-
return InputUtils::recordingAllowed( layer, mProject );
175-
};
176-
break;
177-
default:
178-
filterFunction = []( QgsMapLayer * )
179-
{
180-
return true;
181-
};
182-
break;
183-
}
184-
185-
endResetModel();
186-
}

0 commit comments

Comments
 (0)