Skip to content

Commit

Permalink
final adjustments
Browse files Browse the repository at this point in the history
  • Loading branch information
VitorVieiraZ committed Jan 18, 2025
1 parent 9370820 commit dc9b17f
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 56 deletions.
15 changes: 15 additions & 0 deletions app/activeproject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -552,3 +552,18 @@ bool ActiveProject::positionTrackingSupported() const

return mQgsProject->readBoolEntry( QStringLiteral( "Mergin" ), QStringLiteral( "PositionTracking/Enabled" ), false );
}

bool ActiveProject::projectHasRecordingLayers() const
{
if ( !mQgsProject )
return false;

const QMap<QString, QgsMapLayer *> layers = mQgsProject->mapLayers();
for ( auto it = layers.constBegin(); it != layers.constEnd(); ++it )
{
if ( InputUtils::recordingAllowed( it.value(), mQgsProject ) )
return true;
}

return false;
}
3 changes: 3 additions & 0 deletions app/activeproject.h
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,9 @@ class ActiveProject: public QObject

bool positionTrackingSupported() const;

//! Returns project has at least one layer that allows recording
Q_INVOKABLE bool projectHasRecordingLayers() const;

signals:
void qgsProjectChanged();
void localProjectChanged( LocalProject project );
Expand Down
41 changes: 41 additions & 0 deletions app/inpututils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2222,3 +2222,44 @@ double InputUtils::pixelDistanceBetween( const QPointF &p1, const QPointF &p2 )
{
return std::hypot( p1.x() - p2.x(), p1.y() - p2.y() );
}

bool InputUtils::layerHasGeometry( const QgsVectorLayer *layer )
{
if ( !layer || !layer->isValid() )
return false;
return layer->wkbType() != Qgis::WkbType::NoGeometry && layer->wkbType() != Qgis::WkbType::Unknown;
}

bool InputUtils::layerVisible( QgsMapLayer *layer )
{
QgsLayerTree *root = QgsProject::instance()->layerTreeRoot();
QgsLayerTreeLayer *layerTree = root->findLayer( layer );

if ( layerTree )
return layerTree->isVisible();

return false;
}

bool InputUtils::isPositionTrackingLayer( QgsMapLayer *layer, QgsProject *project )
{
if ( !layer || !project )
return false;

QString trackingLayerId = project->readEntry( QStringLiteral( "Mergin" ), QStringLiteral( "PositionTracking/TrackingLayer" ), QString() );
return layer->id() == trackingLayerId;
}

bool InputUtils::recordingAllowed( QgsMapLayer *layer, QgsProject *project )
{
if ( !layer || !layer->isValid() || !project )
return false;

QgsVectorLayer *vectorLayer = qobject_cast<QgsVectorLayer *>( layer );

return ( vectorLayer &&
!vectorLayer->readOnly() &&
layerHasGeometry( vectorLayer ) &&
layerVisible( layer ) &&
!isPositionTrackingLayer( layer, project ) );
}
17 changes: 17 additions & 0 deletions app/inpututils.h
Original file line number Diff line number Diff line change
Expand Up @@ -589,6 +589,23 @@ class InputUtils: public QObject
*/
static double pixelDistanceBetween( const QPointF &p1, const QPointF &p2 );

/**
* filters if input layer is visible in current map theme
*/
static bool layerVisible( QgsMapLayer *layer );

/**
* Returns if layer is not NoGeo and not Unknown
*/
static bool layerHasGeometry( const QgsVectorLayer *layer );

/**
* Returns true if the layer is the position tracking layer
*/
Q_INVOKABLE static bool isPositionTrackingLayer( QgsMapLayer *layer, QgsProject *project );

static bool recordingAllowed( QgsMapLayer *layer, QgsProject *project );

public slots:
void onQgsLogMessageReceived( const QString &message, const QString &tag, Qgis::MessageLevel level );

Expand Down
38 changes: 1 addition & 37 deletions app/layersproxymodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,33 +33,6 @@ bool LayersProxyModel::filterAcceptsRow( int source_row, const QModelIndex &sour
return filterFunction( layer );
}

bool layerHasGeometry( const QgsVectorLayer *layer )
{
if ( !layer || !layer->isValid() )
return false;
return layer->wkbType() != Qgis::WkbType::NoGeometry && layer->wkbType() != Qgis::WkbType::Unknown;
}

bool LayersProxyModel::recordingAllowed( QgsMapLayer *layer ) const
{
if ( !layer || !layer->isValid() )
return false;

QgsVectorLayer *vectorLayer = qobject_cast<QgsVectorLayer *>( layer );
return ( vectorLayer && !vectorLayer->readOnly() && layerHasGeometry( vectorLayer ) && layerVisible( layer ) && !isPositionTrackingLayer( layer ) );
}

bool LayersProxyModel::layerVisible( QgsMapLayer *layer ) const
{
QgsLayerTree *root = QgsProject::instance()->layerTreeRoot();
QgsLayerTreeLayer *layerTree = root->findLayer( layer );

if ( layerTree )
return layerTree->isVisible();

return false;
}

QList<QgsMapLayer *> LayersProxyModel::layers() const
{
QList<QgsMapLayer *> filteredLayers;
Expand Down Expand Up @@ -142,15 +115,6 @@ QVariant LayersProxyModel::getData( QModelIndex index, int role ) const
return sourceModel()->data( index, role );
}

bool LayersProxyModel::isPositionTrackingLayer( QgsMapLayer *layer ) const
{
if ( !layer || !mProject )
return false;

QString trackingLayerId = mProject->readEntry( QStringLiteral( "Mergin" ), QStringLiteral( "PositionTracking/TrackingLayer" ), QString() );
return layer->id() == trackingLayerId;
}

QgsProject *LayersProxyModel::qgsProject() const
{
return mProject;
Expand Down Expand Up @@ -204,7 +168,7 @@ void LayersProxyModel::applyFilterFunction()
case ActiveLayerSelection:
filterFunction = [this]( QgsMapLayer * layer )
{
return recordingAllowed( layer );
return InputUtils::recordingAllowed( layer, mProject );
};
break;
default:
Expand Down
10 changes: 0 additions & 10 deletions app/layersproxymodel.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,6 @@ class LayersProxyModel : public QgsMapLayerProxyModel
//! Returns first layer from proxy model's layers list (filtered with filter function)
Q_INVOKABLE QgsMapLayer *firstUsableLayer() const;

//! Returns true if the layer is the position tracking layer
Q_INVOKABLE bool isPositionTrackingLayer( QgsMapLayer *layer ) const;

/**
* @brief layers method return layers from source model filtered with filter function
*/
Expand Down Expand Up @@ -86,13 +83,6 @@ class LayersProxyModel : public QgsMapLayerProxyModel
void refreshData();

private:

//! returns if input layer is capable of recording new features
bool recordingAllowed( QgsMapLayer *layer ) const;

//! filters if input layer is visible in current map theme
bool layerVisible( QgsMapLayer *layer ) const;

LayerModelTypes mModelType;
LayersModel *mModel;

Expand Down
10 changes: 1 addition & 9 deletions app/qml/main.qml
Original file line number Diff line number Diff line change
Expand Up @@ -255,14 +255,6 @@ ApplicationWindow {
accuracy: LocationPermission.Precise
}

MM.LayersProxyModel {
id: recordingLayersModel

modelType: MM.LayersProxyModel.AllLayers
qgsProject: __activeProject.qgsProject
model: MM.LayersModel {}
}

MMToolbar {
id: mapToolbar

Expand All @@ -287,7 +279,7 @@ ApplicationWindow {
text: qsTr("Add")
iconSource: __style.addIcon
onClicked: {
if ( recordingLayersModel.rowCount() > 0 ) {
if ( __activeProject.projectHasRecordingLayers() ) {
stateManager.state = "map"
map.record()
}
Expand Down

0 comments on commit dc9b17f

Please sign in to comment.