Skip to content

Commit 7aa2ce4

Browse files
committed
new tests and post code adjustments
1 parent 96989ce commit 7aa2ce4

File tree

4 files changed

+193
-30
lines changed

4 files changed

+193
-30
lines changed

app/layersproxymodel.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,4 +179,6 @@ void LayersProxyModel::updateFilterFunction()
179179
};
180180
break;
181181
}
182+
183+
refreshData();
182184
}

app/qml/map/MMMapController.qml

Lines changed: 44 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -471,7 +471,10 @@ Item {
471471
text: __activeLayer.layerName
472472
leftIconSource: __inputUtils.loadIconFromLayer( __activeLayer.layer )
473473

474-
onClicked: activeLayerPanel.open()
474+
onClicked: {
475+
activeLayerPanelLoader.active = true
476+
activeLayerPanelLoader.item.open()
477+
}
475478
}
476479

477480
Item {
@@ -795,45 +798,56 @@ Item {
795798
}
796799
}
797800

798-
MMListDrawer {
799-
id: activeLayerPanel
800-
801-
drawerHeader.title: qsTr( "Choose Active Layer" )
801+
Loader {
802+
id: activeLayerPanelLoader
803+
active: false
804+
sourceComponent: activeLayerPanelComponent
805+
}
802806

803-
list.model: MM.LayersProxyModel {
804-
id: recordingLayersModel
807+
Component {
808+
id: activeLayerPanelComponent
805809

806-
qgsProject: __activeProject.qgsProject
807-
modelType: MM.LayersProxyModel.ActiveLayerSelection
808-
model: MM.LayersModel {}
809-
}
810+
MMListDrawer {
811+
id: activeLayerPanel
810812

811-
list.delegate: MMListDelegate {
812-
text: model.layerName
813+
drawerHeader.title: qsTr( "Choose Active Layer" )
813814

814-
// TODO: why we need to set hight here?
815-
height: __style.menuDrawerHeight
815+
list.model: MM.LayersProxyModel {
816+
id: recordingLayersModel
816817

817-
leftContent: MMIcon {
818-
source: model.iconSource
818+
qgsProject: __activeProject.qgsProject
819+
modelType: MM.LayersProxyModel.ActiveLayerSelection
820+
model: MM.LayersModel {}
819821
}
820822

821-
rightContent: MMIcon {
822-
source: __style.doneCircleIcon
823-
visible: __activeLayer.layerId === model.layerId
824-
}
823+
list.delegate: MMListDelegate {
824+
text: model.layerName
825+
826+
// TODO: why we need to set hight here?
827+
height: __style.menuDrawerHeight
825828

826-
onClicked: {
827-
__activeProject.setActiveLayer( recordingLayersModel.layerFromLayerId( model.layerId ) )
828-
activeLayerPanel.close()
829+
leftContent: MMIcon {
830+
source: model.iconSource
831+
}
832+
833+
rightContent: MMIcon {
834+
source: __style.doneCircleIcon
835+
visible: __activeLayer.layerId === model.layerId
836+
}
837+
838+
onClicked: {
839+
__activeProject.setActiveLayer( recordingLayersModel.layerFromLayerId( model.layerId ) )
840+
activeLayerPanel.close()
841+
activeLayerPanelLoader.active = false
842+
}
829843
}
830-
}
831844

832-
emptyStateDelegate: MMMessage {
833-
image: __style.negativeMMSymbolImage
834-
description: qsTr( "Could not find any editable layers in the project." )
835-
linkText: qsTr( "See how to enable digitizing in your project." )
836-
link: __inputHelp.howToEnableDigitizingLink
845+
emptyStateDelegate: MMMessage {
846+
image: __style.negativeMMSymbolImage
847+
description: qsTr( "Could not find any editable layers in the project." )
848+
linkText: qsTr( "See how to enable digitizing in your project." )
849+
link: __inputHelp.howToEnableDigitizingLink
850+
}
837851
}
838852
}
839853

app/test/testutils.cpp

Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,11 @@
1717
#include "inpututils.h"
1818
#include "merginapi.h"
1919

20+
#include "qgsvectorlayer.h"
21+
#include "qgsproject.h"
22+
#include "qgslayertree.h"
23+
#include "qgslayertreelayer.h"
24+
2025
void TestUtils::merginGetAuthCredentials( MerginApi *api, QString &apiRoot, QString &username, QString &password )
2126
{
2227
Q_ASSERT( api );
@@ -183,3 +188,139 @@ QgsProject *TestUtils::loadPlanesTestProject()
183188

184189
return project;
185190
}
191+
192+
void TestUtils::testLayerHasGeometry()
193+
{
194+
// null layer => should be false
195+
QCOMPARE( InputUtils::layerHasGeometry( nullptr ), false );
196+
197+
// invalid layer => should be false
198+
QgsVectorLayer *invalidLayer = new QgsVectorLayer( "", "InvalidLayer", "none" );
199+
QVERIFY( invalidLayer->isValid() == false );
200+
QCOMPARE( InputUtils::layerHasGeometry( invalidLayer ), false );
201+
delete invalidLayer;
202+
203+
// valid memory layer with geometry
204+
QgsVectorLayer *pointLayer = new QgsVectorLayer( "Point?crs=EPSG:4326", "ValidPointLayer", "memory" );
205+
QVERIFY( pointLayer->isValid() );
206+
QCOMPARE( InputUtils::layerHasGeometry( pointLayer ), true );
207+
208+
// layer with NoGeo => should be false
209+
QgsVectorLayer *noGeomLayer = new QgsVectorLayer( "None", "NoGeometryLayer", "memory" );
210+
QVERIFY( noGeomLayer->isValid() );
211+
QCOMPARE( InputUtils::layerHasGeometry( noGeomLayer ), false );
212+
213+
delete pointLayer;
214+
delete noGeomLayer;
215+
}
216+
217+
void TestUtils::testLayerVisible()
218+
{
219+
// null layer => should be false
220+
QCOMPARE( InputUtils::layerVisible( nullptr ), false );
221+
222+
QgsProject *project = new QgsProject();
223+
project->clear();
224+
225+
// valid memory layer
226+
QgsVectorLayer *layer = new QgsVectorLayer( "LineString?crs=EPSG:4326", "VisibleLineLayer", "memory" );
227+
QVERIFY( layer->isValid() );
228+
229+
// won't appear in the layer tree => false
230+
QCOMPARE( InputUtils::layerVisible( layer ), false );
231+
232+
// added to project => true
233+
project->addMapLayer( layer );
234+
QCOMPARE( InputUtils::layerVisible( layer ), true );
235+
236+
// hide layer => false
237+
QgsLayerTree *root = project->layerTreeRoot();
238+
QgsLayerTreeLayer *layerTree = root->findLayer( layer );
239+
QVERIFY( layerTree );
240+
layerTree->setItemVisibilityChecked( false );
241+
QCOMPARE( InputUtils::layerVisible( layer ), false );
242+
243+
delete project;
244+
}
245+
246+
void TestUtils::testIsPositionTrackingLayer()
247+
{
248+
QCOMPARE( InputUtils::isPositionTrackingLayer( nullptr, nullptr ), false );
249+
250+
QgsProject *project = new QgsProject();
251+
QgsVectorLayer *layer = new QgsVectorLayer( "Point?crs=EPSG:4326", "TrackingLayer", "memory" );
252+
project->addMapLayer( layer );
253+
QCOMPARE( InputUtils::isPositionTrackingLayer( layer, project ), false );
254+
255+
// tracking layer ID => true
256+
QString layerId = layer->id();
257+
project->writeEntry( QStringLiteral( "Mergin" ), QStringLiteral( "PositionTracking/TrackingLayer" ), layerId );
258+
QCOMPARE( InputUtils::isPositionTrackingLayer( layer, project ), true );
259+
260+
// not tracking layer ID => false
261+
project->writeEntry( QStringLiteral( "Mergin" ), QStringLiteral( "PositionTracking/TrackingLayer" ), QString( "some-other-id" ) );
262+
QCOMPARE( InputUtils::isPositionTrackingLayer( layer, project ), false );
263+
264+
delete project;
265+
}
266+
267+
void TestUtils::testRecordingAllowed()
268+
{
269+
QCOMPARE( InputUtils::recordingAllowed( nullptr, nullptr ), false );
270+
271+
QgsProject *project = new QgsProject();
272+
273+
//valid vector layer => true
274+
QgsVectorLayer *validLayer = new QgsVectorLayer( "Polygon?crs=EPSG:4326", "PolygonLayer", "memory" );
275+
project->addMapLayer( validLayer );
276+
QCOMPARE( InputUtils::recordingAllowed( validLayer, project ), true );
277+
278+
// not visible => false
279+
QgsLayerTreeLayer *layerNode = project->layerTreeRoot()->findLayer( validLayer );
280+
QVERIFY( layerNode );
281+
layerNode->setItemVisibilityChecked( false );
282+
QCOMPARE( InputUtils::recordingAllowed( validLayer, project ), false );
283+
layerNode->setItemVisibilityChecked( true ); // restore
284+
285+
// read-only layer => false
286+
validLayer->setReadOnly( true );
287+
QCOMPARE( InputUtils::recordingAllowed( validLayer, project ), false );
288+
validLayer->setReadOnly( false ); // restore
289+
290+
// noGeo => false
291+
QgsVectorLayer *noGeomLayer = new QgsVectorLayer( "None", "NoGeomLayer", "memory" );
292+
project->addMapLayer( noGeomLayer );
293+
QCOMPARE( InputUtils::recordingAllowed( noGeomLayer, project ), false );
294+
295+
// position tracking layer => false
296+
project->writeEntry( "Mergin", "PositionTracking/TrackingLayer", validLayer->id() );
297+
QCOMPARE( InputUtils::recordingAllowed( validLayer, project ), false );
298+
299+
// restore valid layer => true
300+
project->writeEntry( "Mergin", "PositionTracking/TrackingLayer", QString() );
301+
QCOMPARE( InputUtils::recordingAllowed( validLayer, project ), true );
302+
303+
delete project;
304+
}
305+
306+
void TestUtils::testMapLayerFromName()
307+
{
308+
QCOMPARE( InputUtils::mapLayerFromName( "Anything", nullptr ), static_cast<QgsMapLayer *>( nullptr ) );
309+
310+
// empty layerName => nullptr
311+
QgsProject *project = new QgsProject();
312+
QCOMPARE( InputUtils::mapLayerFromName( "", project ), static_cast<QgsMapLayer *>( nullptr ) );
313+
314+
// ddd a named layer to project and check => should succeed
315+
QgsVectorLayer *layer = new QgsVectorLayer( "Point?crs=EPSG:4326", "MyTestLayer", "memory" );
316+
QVERIFY( layer->isValid() );
317+
project->addMapLayer( layer );
318+
QgsMapLayer *found = InputUtils::mapLayerFromName( "MyTestLayer", project );
319+
QVERIFY( found != nullptr );
320+
QCOMPARE( found->name(), QString( "MyTestLayer" ) );
321+
322+
// non-existing name => nullptr
323+
QCOMPARE( InputUtils::mapLayerFromName( "NoSuchName", project ), static_cast<QgsMapLayer *>( nullptr ) );
324+
325+
delete project;
326+
}

app/test/testutils.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,12 @@ namespace TestUtils
5656
* Returns true if files were successfully created
5757
*/
5858
bool generateProjectFolder( const QString &rootPath, const QJsonDocument &structure );
59+
60+
void testLayerHasGeometry();
61+
void testLayerVisible();
62+
void testIsPositionTrackingLayer();
63+
void testRecordingAllowed();
64+
void testMapLayerFromName();
5965
}
6066

6167
#define COMPARENEAR(actual, expected, epsilon) \

0 commit comments

Comments
 (0)