Skip to content

Commit

Permalink
new tests and post code adjustments
Browse files Browse the repository at this point in the history
  • Loading branch information
VitorVieiraZ committed Jan 21, 2025
1 parent 96989ce commit 7aa2ce4
Show file tree
Hide file tree
Showing 4 changed files with 193 additions and 30 deletions.
2 changes: 2 additions & 0 deletions app/layersproxymodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -179,4 +179,6 @@ void LayersProxyModel::updateFilterFunction()
};
break;
}

refreshData();
}
74 changes: 44 additions & 30 deletions app/qml/map/MMMapController.qml
Original file line number Diff line number Diff line change
Expand Up @@ -471,7 +471,10 @@ Item {
text: __activeLayer.layerName
leftIconSource: __inputUtils.loadIconFromLayer( __activeLayer.layer )

onClicked: activeLayerPanel.open()
onClicked: {
activeLayerPanelLoader.active = true
activeLayerPanelLoader.item.open()
}
}

Item {
Expand Down Expand Up @@ -795,45 +798,56 @@ Item {
}
}

MMListDrawer {
id: activeLayerPanel

drawerHeader.title: qsTr( "Choose Active Layer" )
Loader {
id: activeLayerPanelLoader
active: false
sourceComponent: activeLayerPanelComponent
}

list.model: MM.LayersProxyModel {
id: recordingLayersModel
Component {
id: activeLayerPanelComponent

qgsProject: __activeProject.qgsProject
modelType: MM.LayersProxyModel.ActiveLayerSelection
model: MM.LayersModel {}
}
MMListDrawer {
id: activeLayerPanel

list.delegate: MMListDelegate {
text: model.layerName
drawerHeader.title: qsTr( "Choose Active Layer" )

// TODO: why we need to set hight here?
height: __style.menuDrawerHeight
list.model: MM.LayersProxyModel {
id: recordingLayersModel

leftContent: MMIcon {
source: model.iconSource
qgsProject: __activeProject.qgsProject
modelType: MM.LayersProxyModel.ActiveLayerSelection
model: MM.LayersModel {}
}

rightContent: MMIcon {
source: __style.doneCircleIcon
visible: __activeLayer.layerId === model.layerId
}
list.delegate: MMListDelegate {
text: model.layerName

// TODO: why we need to set hight here?
height: __style.menuDrawerHeight

onClicked: {
__activeProject.setActiveLayer( recordingLayersModel.layerFromLayerId( model.layerId ) )
activeLayerPanel.close()
leftContent: MMIcon {
source: model.iconSource
}

rightContent: MMIcon {
source: __style.doneCircleIcon
visible: __activeLayer.layerId === model.layerId
}

onClicked: {
__activeProject.setActiveLayer( recordingLayersModel.layerFromLayerId( model.layerId ) )
activeLayerPanel.close()
activeLayerPanelLoader.active = false
}
}
}

emptyStateDelegate: MMMessage {
image: __style.negativeMMSymbolImage
description: qsTr( "Could not find any editable layers in the project." )
linkText: qsTr( "See how to enable digitizing in your project." )
link: __inputHelp.howToEnableDigitizingLink
emptyStateDelegate: MMMessage {
image: __style.negativeMMSymbolImage
description: qsTr( "Could not find any editable layers in the project." )
linkText: qsTr( "See how to enable digitizing in your project." )
link: __inputHelp.howToEnableDigitizingLink
}
}
}

Expand Down
141 changes: 141 additions & 0 deletions app/test/testutils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@
#include "inpututils.h"
#include "merginapi.h"

#include "qgsvectorlayer.h"
#include "qgsproject.h"
#include "qgslayertree.h"
#include "qgslayertreelayer.h"

void TestUtils::merginGetAuthCredentials( MerginApi *api, QString &apiRoot, QString &username, QString &password )
{
Q_ASSERT( api );
Expand Down Expand Up @@ -183,3 +188,139 @@ QgsProject *TestUtils::loadPlanesTestProject()

return project;
}

void TestUtils::testLayerHasGeometry()
{
// null layer => should be false
QCOMPARE( InputUtils::layerHasGeometry( nullptr ), false );

// invalid layer => should be false
QgsVectorLayer *invalidLayer = new QgsVectorLayer( "", "InvalidLayer", "none" );
QVERIFY( invalidLayer->isValid() == false );
QCOMPARE( InputUtils::layerHasGeometry( invalidLayer ), false );
delete invalidLayer;

// valid memory layer with geometry
QgsVectorLayer *pointLayer = new QgsVectorLayer( "Point?crs=EPSG:4326", "ValidPointLayer", "memory" );
QVERIFY( pointLayer->isValid() );
QCOMPARE( InputUtils::layerHasGeometry( pointLayer ), true );

// layer with NoGeo => should be false
QgsVectorLayer *noGeomLayer = new QgsVectorLayer( "None", "NoGeometryLayer", "memory" );
QVERIFY( noGeomLayer->isValid() );
QCOMPARE( InputUtils::layerHasGeometry( noGeomLayer ), false );

delete pointLayer;
delete noGeomLayer;
}

void TestUtils::testLayerVisible()
{
// null layer => should be false
QCOMPARE( InputUtils::layerVisible( nullptr ), false );

QgsProject *project = new QgsProject();
project->clear();

// valid memory layer
QgsVectorLayer *layer = new QgsVectorLayer( "LineString?crs=EPSG:4326", "VisibleLineLayer", "memory" );
QVERIFY( layer->isValid() );

// won't appear in the layer tree => false
QCOMPARE( InputUtils::layerVisible( layer ), false );

// added to project => true
project->addMapLayer( layer );
QCOMPARE( InputUtils::layerVisible( layer ), true );

// hide layer => false
QgsLayerTree *root = project->layerTreeRoot();
QgsLayerTreeLayer *layerTree = root->findLayer( layer );
QVERIFY( layerTree );
layerTree->setItemVisibilityChecked( false );
QCOMPARE( InputUtils::layerVisible( layer ), false );

delete project;
}

void TestUtils::testIsPositionTrackingLayer()
{
QCOMPARE( InputUtils::isPositionTrackingLayer( nullptr, nullptr ), false );

QgsProject *project = new QgsProject();
QgsVectorLayer *layer = new QgsVectorLayer( "Point?crs=EPSG:4326", "TrackingLayer", "memory" );
project->addMapLayer( layer );
QCOMPARE( InputUtils::isPositionTrackingLayer( layer, project ), false );

// tracking layer ID => true
QString layerId = layer->id();
project->writeEntry( QStringLiteral( "Mergin" ), QStringLiteral( "PositionTracking/TrackingLayer" ), layerId );
QCOMPARE( InputUtils::isPositionTrackingLayer( layer, project ), true );

// not tracking layer ID => false
project->writeEntry( QStringLiteral( "Mergin" ), QStringLiteral( "PositionTracking/TrackingLayer" ), QString( "some-other-id" ) );
QCOMPARE( InputUtils::isPositionTrackingLayer( layer, project ), false );

delete project;
}

void TestUtils::testRecordingAllowed()
{
QCOMPARE( InputUtils::recordingAllowed( nullptr, nullptr ), false );

QgsProject *project = new QgsProject();

//valid vector layer => true
QgsVectorLayer *validLayer = new QgsVectorLayer( "Polygon?crs=EPSG:4326", "PolygonLayer", "memory" );
project->addMapLayer( validLayer );
QCOMPARE( InputUtils::recordingAllowed( validLayer, project ), true );

// not visible => false
QgsLayerTreeLayer *layerNode = project->layerTreeRoot()->findLayer( validLayer );
QVERIFY( layerNode );
layerNode->setItemVisibilityChecked( false );
QCOMPARE( InputUtils::recordingAllowed( validLayer, project ), false );
layerNode->setItemVisibilityChecked( true ); // restore

// read-only layer => false
validLayer->setReadOnly( true );
QCOMPARE( InputUtils::recordingAllowed( validLayer, project ), false );
validLayer->setReadOnly( false ); // restore

// noGeo => false
QgsVectorLayer *noGeomLayer = new QgsVectorLayer( "None", "NoGeomLayer", "memory" );
project->addMapLayer( noGeomLayer );
QCOMPARE( InputUtils::recordingAllowed( noGeomLayer, project ), false );

// position tracking layer => false
project->writeEntry( "Mergin", "PositionTracking/TrackingLayer", validLayer->id() );
QCOMPARE( InputUtils::recordingAllowed( validLayer, project ), false );

// restore valid layer => true
project->writeEntry( "Mergin", "PositionTracking/TrackingLayer", QString() );
QCOMPARE( InputUtils::recordingAllowed( validLayer, project ), true );

delete project;
}

void TestUtils::testMapLayerFromName()
{
QCOMPARE( InputUtils::mapLayerFromName( "Anything", nullptr ), static_cast<QgsMapLayer *>( nullptr ) );

// empty layerName => nullptr
QgsProject *project = new QgsProject();
QCOMPARE( InputUtils::mapLayerFromName( "", project ), static_cast<QgsMapLayer *>( nullptr ) );

// ddd a named layer to project and check => should succeed
QgsVectorLayer *layer = new QgsVectorLayer( "Point?crs=EPSG:4326", "MyTestLayer", "memory" );
QVERIFY( layer->isValid() );
project->addMapLayer( layer );
QgsMapLayer *found = InputUtils::mapLayerFromName( "MyTestLayer", project );
QVERIFY( found != nullptr );
QCOMPARE( found->name(), QString( "MyTestLayer" ) );

// non-existing name => nullptr
QCOMPARE( InputUtils::mapLayerFromName( "NoSuchName", project ), static_cast<QgsMapLayer *>( nullptr ) );

delete project;
}
6 changes: 6 additions & 0 deletions app/test/testutils.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,12 @@ namespace TestUtils
* Returns true if files were successfully created
*/
bool generateProjectFolder( const QString &rootPath, const QJsonDocument &structure );

void testLayerHasGeometry();
void testLayerVisible();
void testIsPositionTrackingLayer();
void testRecordingAllowed();
void testMapLayerFromName();
}

#define COMPARENEAR(actual, expected, epsilon) \
Expand Down

0 comments on commit 7aa2ce4

Please sign in to comment.