-
Notifications
You must be signed in to change notification settings - Fork 79
Expand file tree
/
Copy pathmain.cpp
More file actions
892 lines (772 loc) · 35.2 KB
/
main.cpp
File metadata and controls
892 lines (772 loc) · 35.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#include "inputconfig.h"
#include <QFontDatabase>
#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QQmlComponent>
#include <QtDebug>
#include <QQmlError>
#include <QScreen>
#include <QWindow>
#include <QtGlobal>
#include <QQmlContext>
#include <QQuickWindow>
#include <QLocale>
#include <QImageReader>
#include <QStandardPaths>
#ifdef INPUT_TEST
#include "test/inputtests.h"
#endif
#include <qqml.h>
#include <qgsmessagelog.h>
#include "qgsconfig.h"
#include "qgsproviderregistry.h"
#include "qgsmaplayerproxymodel.h"
#include "qgsnetworkaccessmanager.h"
#include "geodiffutils.h"
#include "merginerrortypes.h"
#include "androidutils.h"
#include "ios/iosutils.h"
#include "inpututils.h"
#include "coreutils.h"
#include "position/positiondirection.h"
#include "mapthemesmodel.h"
#include "merginapi.h"
#include "merginapistatus.h"
#include "merginservertype.h"
#include "merginsubscriptioninfo.h"
#include "merginsubscriptionstatus.h"
#include "merginprojectstatusmodel.h"
#include "recordinglayersproxymodel.h"
#include "layersmodel.h"
#include "activelayer.h"
#include "merginuserauth.h"
#include "merginuserinfo.h"
#include "variablesmanager.h"
#include "inputhelp.h"
#include "inputprojutils.h"
#include "fieldsmodel.h"
#include "projectwizard.h"
#include "qrcodedecoder.h"
#include "inputexpressionfunctions.h"
#include "compass.h"
#include "attributepreviewcontroller.h"
#include "qgsfeature.h"
#include "qgslogger.h"
#include "qgsmaplayer.h"
#include "qgsmessagelog.h"
#include "qgspointxy.h"
#include "qgsproject.h"
#include "qgsrelationmanager.h"
#include "qgscoordinatetransformcontext.h"
#include "qgsvectorlayer.h"
#include "qgsunittypes.h"
#include "mmstyle.h"
#include "notificationmodel.h"
#include "rememberattributescontroller.h"
#include "attributecontroller.h"
#include "attributedata.h"
#include "attributeformmodel.h"
#include "attributeformproxymodel.h"
#include "attributetabmodel.h"
#include "attributetabproxymodel.h"
#include "inputcoordinatetransformer.h"
#include "identifykit.h"
#include "featurelayerpair.h"
#include "inputmapcanvasmap.h"
#include "inputmapsettings.h"
#include "inputmaptransform.h"
#include "position/positionkit.h"
#include "scalebarkit.h"
#include "featuresmodel.h"
#include "staticfeaturesmodel.h"
#include "layerfeaturesmodel.h"
#include "relationfeaturesmodel.h"
#include "relationreferencefeaturesmodel.h"
#include "fieldvalidator.h"
#include "valuerelationfeaturesmodel.h"
#include "snaputils.h"
#include "guidelinecontroller.h"
#include "multieditmanager.h"
#include "mixedattributevalue.h"
#include "mapsketchingcontroller.h"
#include "projectsmodel.h"
#include "projectsproxymodel.h"
#include "project.h"
#include "qgsproject.h"
#include "bluetoothdiscoverymodel.h"
#include "position/mapposition.h"
#include "position/providers/positionprovidersmodel.h"
#include "position/providers/abstractpositionprovider.h"
#include "position/tracking/positiontrackingmanager.h"
#include "position/tracking/positiontrackinghighlight.h"
#include "synchronizationmanager.h"
#include "synchronizationerror.h"
#include "maptools/abstractmaptool.h"
#include "maptools/recordingmaptool.h"
#include "maptools/splittingmaptool.h"
#include "maptools/measurementmaptool.h"
#include "layer/layertreemodel.h"
#include "layer/layertreemodelpixmapprovider.h"
#include "layer/layertreesortfiltermodel.h"
#include "layer/layertreeflatmodel.h"
#include "layer/layertreeflatmodelpixmapprovider.h"
#include "layer/layertreeflatsortfiltermodel.h"
#include "layer/layerdetaildata.h"
#include "layer/layerdetaillegendimageprovider.h"
#include "workspacesmodel.h"
#include "workspacesproxymodel.h"
#include "invitationsmodel.h"
#include "invitationsproxymodel.h"
#include "changelogmodel.h"
#include "streamingintervaltype.h"
#include <QQuickStyle>
#ifdef MOBILE_OS
#include <QFile>
#include <QDir>
#include <QStandardPaths>
#endif
#ifdef DESKTOP_OS
#include <QCommandLineParser>
#include <qgis.h>
#endif
#include "qgsapplication.h"
#include "activeproject.h"
#include "appsettings.h"
static QString getDataDir()
{
#ifdef QGIS_QUICK_DATA_PATH
QString dataPathRaw( STR( QGIS_QUICK_DATA_PATH ) );
#ifdef ANDROID
dataPathRaw = AndroidUtils::externalStorageAppFolder();
#endif
#ifdef Q_OS_IOS
QString docsLocation = QStandardPaths::standardLocations( QStandardPaths::DocumentsLocation ).value( 0 );
QDir myDir( docsLocation );
if ( !myDir.exists() )
{
myDir.mkpath( docsLocation );
}
dataPathRaw = docsLocation + "/" + dataPathRaw;
#endif
#ifdef Q_OS_WIN32
QString appLocation = QStandardPaths::writableLocation( QStandardPaths::AppDataLocation );
QDir myDir( appLocation );
if ( !myDir.exists() )
{
myDir.mkpath( appLocation );
}
dataPathRaw = appLocation + "/" + dataPathRaw;
#endif
qputenv( "QGIS_QUICK_DATA_PATH", dataPathRaw.toUtf8().constData() );
#else
qDebug( "== Must set QGIS_QUICK_DATA_PATH in order to get QGIS Quick running! ==" );
#endif
QString dataDir = QString::fromLocal8Bit( qgetenv( "QGIS_QUICK_DATA_PATH" ) ) ;
qDebug() << "QGIS_QUICK_DATA_PATH: " << dataDir;
return dataDir;
}
static void setEnvironmentQgisPrefixPath()
{
#ifdef DESKTOP_OS
#ifdef QGIS_PREFIX_PATH
qputenv( "QGIS_PREFIX_PATH", STR( QGIS_PREFIX_PATH ) );
#endif
if ( QString::fromLocal8Bit( qgetenv( "QGIS_PREFIX_PATH" ) ).isEmpty() )
{
// if not on Android, QGIS_PREFIX_PATH env variable should have been set already or defined as C++ define
qDebug( "== Must set QGIS_PREFIX_PATH in order to get QGIS Quick module running! ==" );
}
#endif
#if defined (ANDROID) || defined (Q_OS_IOS)
QDir myDir( QDir::homePath() );
myDir.cdUp();
QString prefixPath = myDir.absolutePath(); // something like: /data/data/org.qgis.quick
qputenv( "QGIS_PREFIX_PATH", prefixPath.toUtf8().constData() );
#elif defined (Q_OS_WIN32)
QString prefixPath = QCoreApplication::applicationDirPath();
qputenv( "QGIS_PREFIX_PATH", prefixPath.toUtf8().constData() );
#endif
qDebug() << "QGIS_PREFIX_PATH: " << QString::fromLocal8Bit( qgetenv( "QGIS_PREFIX_PATH" ) );
}
static void init_qgis( const QString &pkgPath )
{
QgsApplication::init();
#ifdef MOBILE_OS
// QGIS plugins on Android are in the same path as other libraries
QgsApplication::setPluginPath( QApplication::applicationDirPath() );
QgsApplication::setPkgDataPath( pkgPath );
#else
Q_UNUSED( pkgPath )
#endif
QgsApplication::initQgis();
// make sure the DB exists - otherwise custom projections will be failing
if ( !QgsApplication::createDatabase() )
qDebug( "Can't create qgis user DB!!!" );
qDebug( "qgis providers:\n%s", QgsProviderRegistry::instance()->pluginList().toLatin1().data() );
}
static void init_pg( const QString &dataDir )
{
QFileInfo pgFile( QStringLiteral( "%1/pg_service.conf" ).arg( dataDir ) );
if ( pgFile.exists() && pgFile.isReadable() )
{
qputenv( "PGSYSCONFDIR", dataDir.toUtf8() );
CoreUtils::log( QStringLiteral( "PostgreSQL" ), QStringLiteral( "found pg_service.conf, setting PGSYSCONFDIR" ) );
}
}
void initDeclarative()
{
qmlRegisterUncreatableType<MerginUserAuth>( "mm", 1, 0, "MerginUserAuth", "" );
qmlRegisterUncreatableType<MerginUserInfo>( "mm", 1, 0, "MerginUserInfo", "" );
qmlRegisterUncreatableType<MerginSubscriptionInfo>( "mm", 1, 0, "MerginSubscriptionInfo", "" );
qmlRegisterUncreatableType<ActiveProject>( "mm", 1, 0, "ActiveProject", "" );
qmlRegisterUncreatableType<SynchronizationManager>( "mm", 1, 0, "SynchronizationManager", "" );
qmlRegisterUncreatableType<SynchronizationError>( "mm", 1, 0, "SyncError", "SyncError Enum" );
qmlRegisterUncreatableType<AppSettings>( "mm", 1, 0, "AppSettings", "" );
qmlRegisterUncreatableType<MerginApiStatus>( "mm", 1, 0, "MerginApiStatus", "MerginApiStatus Enum" );
qmlRegisterUncreatableType<MerginServerType>( "mm", 1, 0, "MerginServerType", "MerginServerType Enum" );
qmlRegisterUncreatableType<MerginSubscriptionStatus>( "mm", 1, 0, "MerginSubscriptionStatus", "MerginSubscriptionStatus Enum" );
qmlRegisterUncreatableType<MerginProjectStatusModel>( "mm", 1, 0, "MerginProjectStatusModel", "Enum" );
qmlRegisterUncreatableType<ActiveLayer>( "mm", 1, 0, "ActiveLayer", "" );
qmlRegisterUncreatableType<StreamingIntervalType>( "mm", 1, 0, "StreamingIntervalType", "StreamingIntervalType Enum" );
qmlRegisterUncreatableType<RegistrationError>( "mm", 1, 0, "RegistrationError", "RegistrationError Enum" );
qmlRegisterType<PositionDirection>( "mm", 1, 0, "PositionDirection" );
qmlRegisterType<Compass>( "mm", 1, 0, "Compass" );
qmlRegisterType<FieldsModel>( "mm", 1, 0, "FieldsModel" );
qmlRegisterType<QrCodeDecoder>( "mm", 1, 0, "QrCodeDecoder" );
qmlRegisterType<ProjectsModel>( "mm", 1, 0, "ProjectsModel" );
qmlRegisterType<ProjectsProxyModel>( "mm", 1, 0, "ProjectsProxyModel" );
qmlRegisterType<AttributePreviewController>( "mm", 1, 0, "AttributePreviewController" );
qmlRegisterType<WorkspacesModel>( "mm", 1, 0, "WorkspacesModel" );
qmlRegisterType<WorkspacesProxyModel>( "mm", 1, 0, "WorkspacesProxyModel" );
qmlRegisterType<MerginInvitation>( "mm", 1, 0, "MerginInvitation" );
qmlRegisterType<InvitationsModel>( "mm", 1, 0, "InvitationsModel" );
qmlRegisterType<InvitationsProxyModel>( "mm", 1, 0, "InvitationsProxyModel" );
qmlRegisterType<ChangelogModel>( "mm", 1, 0, "ChangelogModel" );
qmlRegisterUncreatableType<AttributePreviewModel>( "mm", 1, 0, "AttributePreviewModel", "" );
qmlRegisterUncreatableMetaObject( ProjectStatus::staticMetaObject, "mm", 1, 0, "ProjectStatus", "ProjectStatus Enum" );
qRegisterMetaType< FeatureLayerPair >( "FeatureLayerPair" );
qRegisterMetaType< FeatureLayerPair * >( "FeatureLayerPair*" );
qRegisterMetaType< AttributeController * >( "AttributeController*" );
qmlRegisterUncreatableType<NotificationType>( "mm", 1, 0, "NotificationType", "NotificationType Enum" );
qRegisterMetaType< QList<QgsMapLayer *> >( "QList<QgsMapLayer*>" );
qRegisterMetaType< QgsAttributes > ( "QgsAttributes" );
qRegisterMetaType< QgsCoordinateReferenceSystem >( "QgsCoordinateReferenceSystem" );
qRegisterMetaType< QgsCoordinateTransformContext >( "QgsCoordinateTransformContext" );
qRegisterMetaType< QgsFeature > ( "QgsFeature" );
qRegisterMetaType< QgsFeatureId > ( "QgsFeatureId" );
qRegisterMetaType< QgsPoint >( "QgsPoint" );
qRegisterMetaType< QgsLayerTreeNode * >( "QgsLayerTreeNode*" );
qRegisterMetaType< QgsPointXY >( "QgsPointXY" );
qRegisterMetaType< QgsRelation >( "QgsRelation" );
qRegisterMetaType< QgsPolymorphicRelation >( "QgsPolymorphicRelation" );
qRegisterMetaType< Qgis::SystemOfMeasurement >( "Qgis::SystemOfMeasurement" );
qRegisterMetaType< Qgis::DistanceUnit >( "Qgis::DistanceUnit" );
qRegisterMetaType< QgsCoordinateFormatter::FormatFlags >( "QgsCoordinateFormatter::FormatFlags" );
qRegisterMetaType< QgsCoordinateFormatter::Format >( "QgsCoordinateFormatter::Format" );
qRegisterMetaType< QVariant::Type >( "QVariant::Type" );
qRegisterMetaType< QgsVertexId >( "QgsVertexId" );
qmlRegisterAnonymousType<QAbstractItemModel>( "mm", 1 );
qRegisterMetaType< Vertex >( "Vertex" );
qmlRegisterUncreatableType< FormItem >( "mm", 1, 0, "FormItem", "Only enums from FormItem can be used" );
qmlRegisterUncreatableType< AttributeFormModel >( "mm", 1, 0, "AttributeFormModel", "Created by AttributeController" );
qmlRegisterUncreatableType< AttributeFormProxyModel >( "mm", 1, 0, "AttributeFormProxyModel", "Created by AttributeController" );
qmlRegisterUncreatableType< AttributeTabModel >( "mm", 1, 0, "AttributeTabModel", "Created by AttributeController" );
qmlRegisterUncreatableType< AttributeTabProxyModel >( "mm", 1, 0, "AttributeTabProxyModel", "Created by AttributeController" );
qmlRegisterUncreatableType< FieldValidator >( "mm", 1, 0, "FieldValidator", "Only enums from FieldValidator can be used" );
qmlRegisterType< AttributeController >( "mm", 1, 0, "AttributeController" );
qmlRegisterType< RememberAttributesController >( "mm", 1, 0, "RememberAttributesController" );
qmlRegisterType< IdentifyKit >( "mm", 1, 0, "IdentifyKit" );
qmlRegisterType< PositionKit >( "mm", 1, 0, "PositionKit" );
qmlRegisterType< MapPosition >( "mm", 1, 0, "MapPosition" );
qmlRegisterType< ScaleBarKit >( "mm", 1, 0, "ScaleBarKit" );
qmlRegisterType< SnapUtils >( "mm", 1, 0, "SnapUtils" );
qmlRegisterType< LayerTreeModel >( "mm", 1, 0, "LayerTreeModel" );
qmlRegisterType< LayerTreeSortFilterModel >( "mm", 1, 0, "LayerTreeSortFilterModel" );
qmlRegisterType< LayerTreeFlatModel >( "mm", 1, 0, "LayerTreeFlatModel" );
qmlRegisterType< LayerTreeFlatSortFilterModel >( "mm", 1, 0, "LayerTreeFlatSortFilterModel" );
qmlRegisterType< LayerDetailData >( "mm", 1, 0, "LayerDetailData" );
qmlRegisterType< MapThemesModel >( "mm", 1, 0, "MapThemesModel" );
qmlRegisterType< GuidelineController >( "mm", 1, 0, "GuidelineController" );
qmlRegisterType< FeaturesModel >( "mm", 1, 0, "FeaturesModel" );
qmlRegisterType< StaticFeaturesModel >( "mm", 1, 0, "StaticFeaturesModel" );
qmlRegisterType< LayerFeaturesModel >( "mm", 1, 0, "LayerFeaturesModel" );
qmlRegisterType< RelationFeaturesModel >( "mm", 1, 0, "RelationFeaturesModel" );
qmlRegisterType< ValueRelationFeaturesModel >( "mm", 1, 0, "ValueRelationFeaturesModel" );
qmlRegisterType< RelationReferenceFeaturesModel >( "mm", 1, 0, "RelationReferenceFeaturesModel" );
qmlRegisterType< BluetoothDiscoveryModel >( "mm", 1, 0, "BluetoothDiscoveryModel" );
qmlRegisterType< PositionProvidersModel >( "mm", 1, 0, "PositionProvidersModel" );
qmlRegisterType< PositionTrackingManager >( "mm", 1, 0, "PositionTrackingManager" );
qmlRegisterType< PositionTrackingHighlight >( "mm", 1, 0, "PositionTrackingHighlight" );
qmlRegisterType< MultiEditManager >( "mm", 1, 0, "MultiEditManager" );
qmlRegisterType< MapSketchingController >( "mm", 1, 0, "MapSketchingController" );
qmlRegisterUncreatableType< QgsUnitTypes >( "qgs", 1, 0, "QgsUnitTypes", "Only enums from QgsUnitTypes can be used" );
qmlRegisterType< QgsVectorLayer >( "qgs", 1, 0, "VectorLayer" );
qmlRegisterType< QgsProject >( "qgs", 1, 0, "Project" );
qmlRegisterType< InputMapCanvasMap >( "mm", 1, 0, "MapCanvasMap" );
qmlRegisterType< InputMapSettings >( "mm", 1, 0, "MapSettings" );
qmlRegisterType< InputMapTransform >( "mm", 1, 0, "MapTransform" );
qmlRegisterType< InputCoordinateTransformer >( "mm", 1, 0, "CoordinateTransformer" );
qmlRegisterUncreatableType< AbstractPositionProvider >( "mm", 1, 0, "PositionProvider", "Must be instantiated via its construct method" );
// map tools
qmlRegisterUncreatableType< AbstractMapTool >( "mm", 1, 0, "AbstractMapTool", "Instantiate one of child map tools instead" );
qmlRegisterType< RecordingMapTool >( "mm", 1, 0, "RecordingMapTool" );
qmlRegisterType< SplittingMapTool >( "mm", 1, 0, "SplittingMapTool" );
qmlRegisterType< MeasurementMapTool >( "mm", 1, 0, "MeasurementMapTool" );
// layers model
qmlRegisterType<RecordingLayersProxyModel>( "mm", 1, 0, "RecordingLayersProxyModel" );
qmlRegisterType<LayersModel>( "mm", 1, 0, "LayersModel" );
QMetaType::registerConverter( &MixedAttributeValue::toString );
}
void addQmlImportPath( QQmlEngine &engine )
{
// QML resources
engine.addImportPath( ":/com.merginmaps/imports:" );
// This adds a runtime qml directory containing Input plugin
// when Input is installed (e.g. Android/Win32)
engine.addImportPath( QgsApplication::qmlImportPath() );
qDebug() << "adding QML import Path: " << QgsApplication::qmlImportPath();
#ifdef QML_BUILD_IMPORT_DIR
// Adds a runtime qml directory containing Input plugin
// if we are using the developer mode (not installed Input)
// e.g. Linux/MacOS
QString qmlBuildImportPath( STR( QML_BUILD_IMPORT_DIR ) );
engine.addImportPath( qmlBuildImportPath );
qDebug() << "adding QML import Path: " << qmlBuildImportPath;
#endif
#ifdef Q_OS_IOS
// REQUIRED FOR IOS - to load Input/*.qml files defined in qmldir
engine.addImportPath( "qrc:///" );
qDebug() << "adding QML import Path: " << "qrc:///";
#endif
}
int main( int argc, char *argv[] )
{
QgsApplication app( argc, argv, true );
const QString version = CoreUtils::appVersion();
// Set up the QSettings environment must be done after qapp is created
QCoreApplication::setOrganizationName( "Lutra Consulting" );
QCoreApplication::setOrganizationDomain( "lutraconsulting.co.uk" );
QCoreApplication::setApplicationName( "Input" ); // used by QSettings
QCoreApplication::setApplicationVersion( version );
#ifdef ANDROID
// Fix rendering problems on some Android devices - see https://bugreports.qt.io/browse/QTBUG-134089
QCoreApplication::setAttribute( Qt::AA_DisableShaderDiskCache );
#endif
#ifdef INPUT_TEST
InputTests tests;
tests.parseArgs( argc, argv );
#endif
qDebug() << "Mergin Maps Input App" << version << InputUtils::appPlatform() << "(" << CoreUtils::appVersionCode() << ")";
qDebug() << "Built with QGIS " << VERSION_INT << " and QT " << qVersion();
qDebug() << "Device uuid " << CoreUtils::deviceUuid();
// Set/Get enviroment
QString dataDir = getDataDir();
QString projectDir = dataDir + "/projects";
#ifdef INPUT_TEST
if ( tests.testingRequested() )
{
projectDir = tests.initTestingDir();
}
#endif
QDir projectsDirectory( projectDir );
if ( !projectsDirectory.exists() )
{
projectsDirectory.mkpath( projectDir );
}
CoreUtils::setLogFilename( projectDir + "/.logs" );
CoreUtils::log( QStringLiteral( "AppState" ), QStringLiteral( "Application has started: %1 (%2)" ).arg( version ).arg( CoreUtils::appVersionCode() ) );
setEnvironmentQgisPrefixPath();
// Initialize translations
QLocale locale;
QTranslator inputTranslator;
if ( inputTranslator.load( locale, "input", "_", ":/" ) )
{
app.installTranslator( &inputTranslator );
qDebug() << "Loaded input translation" << app.locale() << "for" << locale;
}
else
{
qDebug() << "Error in loading input translation for " << locale;
}
QString appBundleDir;
#ifdef ANDROID
appBundleDir = dataDir + "/qgis-data";
#endif
#ifdef Q_OS_IOS
appBundleDir = QCoreApplication::applicationDirPath() + "/qgis-data";
#endif
#ifdef Q_OS_WIN32
appBundleDir = QCoreApplication::applicationDirPath() + "\\qgis-data";
#endif
#ifdef Q_OS_LINUX
appBundleDir = dataDir;
#endif
#ifdef Q_OS_MACOS
appBundleDir = dataDir;
#endif
InputProjUtils inputProjUtils;
inputProjUtils.initProjLib( appBundleDir, dataDir, projectDir );
init_pg( dataDir );
init_qgis( appBundleDir );
#ifdef ANDROID
// See issue #3431 -> disable Android accessibility features to prevent ANRs
qputenv( "QT_ANDROID_DISABLE_ACCESSIBILITY", "1" );
#endif
#ifdef Q_OS_IOS
// See issue #3561 -> WFS layers causing stress on iOS
qputenv( "QGIS_USE_SHARED_MEMORY_KEEP_ALIVE", "1" );
qDebug() << "Setting QGIS_USE_SHARED_MEMORY_KEEP_ALIVE environment variable TRUE";
#endif
// AppSettings has to be initialized after QGIS app init (because of correct reading/writing QSettings).
AppSettings as;
// there seem to be issues with HTTP/2 server support (QTBUG-111417)
// so let's stick to HTTP/1 for the time being (Qt5 has HTTP/2 disabled by default)
QgsNetworkAccessManager::instance()->setRequestPreprocessor( []( QNetworkRequest * r )
{
r->setAttribute( QNetworkRequest::Http2AllowedAttribute, false );
} );
// Create Input classes
GeodiffUtils::init();
AndroidUtils androidUtils;
IosUtils iosUtils;
LocalProjectsManager localProjectsManager( projectDir );
std::unique_ptr<MerginApi> ma = std::unique_ptr<MerginApi>( new MerginApi( localProjectsManager ) );
InputUtils iu( &androidUtils );
MerginProjectStatusModel mpsm( localProjectsManager );
InputHelp help( ma.get() );
ProjectWizard pw( projectDir );
NotificationModel notificationModel;
ActiveLayer al;
ActiveProject activeProject( as, al, localProjectsManager );
std::unique_ptr<VariablesManager> vm( new VariablesManager( ma.get() ) );
vm->registerInputExpressionFunctions();
SynchronizationManager syncManager( ma.get() );
LayerTreeModelPixmapProvider *layerTreeModelPixmapProvider( new LayerTreeModelPixmapProvider );
LayerTreeFlatModelPixmapProvider *layerTreeFlatModelPixmapProvider( new LayerTreeFlatModelPixmapProvider );
LayerDetailLegendImageProvider *layerDetailLegendImageProvider( new LayerDetailLegendImageProvider );
// build position kit, save active provider to QSettings and load previously active provider
PositionKit pk;
QObject::connect( &pk, &PositionKit::positionProviderChanged, &as, [&as]( AbstractPositionProvider * provider )
{
as.setActivePositionProviderId( provider ? provider->id() : QLatin1String() );
} );
pk.setPositionProvider( pk.constructActiveProvider( &as ) );
pk.setAppSettings( &as );
// Lambda context object can be used in all lambda functions defined here,
// it secures lambdas, so that they are destroyed when this object is destroyed to avoid crashes.
QObject lambdaContext;
QObject::connect( &app, &QGuiApplication::applicationStateChanged, &lambdaContext, []( Qt::ApplicationState state )
{
QString msg;
// Instatiate QDebug with QString to redirect output to string
// It is used to convert enum to string
QDebug logHelper( &msg );
logHelper << QStringLiteral( "Application changed state to:" ) << state;
CoreUtils::log( QStringLiteral( "AppState" ), msg );
} );
QObject::connect( &app, &QCoreApplication::aboutToQuit, &lambdaContext, []()
{
CoreUtils::log( QStringLiteral( "AppState" ), QStringLiteral( "Application has quit" ) );
} );
QObject::connect( &help, &InputHelp::submitReportSuccessful, &lambdaContext, [¬ificationModel]()
{
notificationModel.addSuccess( QObject::tr( "Report submitted. Please contact us on %1" ).arg( InputHelp::helpdeskMail() ) );
} );
QObject::connect( &help, &InputHelp::submitReportFailed, &lambdaContext, [¬ificationModel]()
{
notificationModel.addError( QObject::tr( "Failed to submit report. Please check your internet connection." ) );
} );
QObject::connect( &pw, &ProjectWizard::notifySuccess, &lambdaContext, [¬ificationModel]( const QString & message )
{
notificationModel.addSuccess( message );
} );
QObject::connect( &iosUtils, &IosUtils::notifyError, &lambdaContext, [¬ificationModel]( const QString & message )
{
notificationModel.addError( message );
} );
QObject::connect( &androidUtils, &AndroidUtils::notifyInfo, &lambdaContext, [¬ificationModel]( const QString & message )
{
notificationModel.addInfo( message );
} );
QObject::connect( &androidUtils, &AndroidUtils::notifyError, &lambdaContext, [¬ificationModel]( const QString & message )
{
notificationModel.addError( message );
} );
QObject::connect( &activeProject, &ActiveProject::syncActiveProject, &syncManager, [&syncManager]( const LocalProject & project )
{
syncManager.syncProject( project, SyncOptions::Authorized, SyncOptions::Retry );
} );
QObject::connect( &activeProject, &ActiveProject::projectReloaded, &lambdaContext, [merginApi = ma.get(), &activeProject]()
{
merginApi->reloadProjectRole( activeProject.projectId() );
} );
QObject::connect( ma.get(), &MerginApi::authChanged, &lambdaContext, [merginApi = ma.get(), &activeProject]()
{
if ( activeProject.isProjectLoaded() )
{
merginApi->reloadProjectRole( activeProject.projectId() );
}
} );
QObject::connect( ma.get(), &MerginApi::projectRoleUpdated, &activeProject, [&activeProject]( const QString & projectId, const QString & role )
{
if ( projectId == activeProject.projectId() )
{
activeProject.setProjectRole( role );
}
} );
QObject::connect( ma.get(), &MerginApi::notifyInfo, &lambdaContext, [¬ificationModel]( const QString & message )
{
notificationModel.addInfo( message );
} );
QObject::connect( ma.get(), &MerginApi::notifySuccess, &lambdaContext, [¬ificationModel]( const QString & message )
{
notificationModel.addSuccess( message );
} );
QObject::connect( ma.get(), &MerginApi::notifyError, &lambdaContext, [¬ificationModel]( const QString & message )
{
notificationModel.addError( message );
} );
// Direct connections
QObject::connect( &app, &QGuiApplication::applicationStateChanged, &pk, &PositionKit::appStateChanged );
QObject::connect( &pw, &ProjectWizard::projectCreated, &localProjectsManager, &LocalProjectsManager::addLocalProject );
QObject::connect( &activeProject, &ActiveProject::projectReloaded, vm.get(), &VariablesManager::merginProjectChanged );
QObject::connect( &activeProject, &ActiveProject::projectWillBeReloaded, &inputProjUtils, &InputProjUtils::resetHandlers );
QObject::connect( &syncManager, &SynchronizationManager::syncFinished, &activeProject, [&activeProject]( const QString & projectId, const bool successfully, const int version, const bool reloadNeeded )
{
Q_UNUSED( successfully );
Q_UNUSED( version );
if ( reloadNeeded && activeProject.projectId() == projectId )
{
activeProject.reloadProject( activeProject.qgsProject()->homePath() );
}
} );
QObject::connect( QgsApplication::messageLog(),
static_cast<void ( QgsMessageLog::* )( const QString &message, const QString &tag, Qgis::MessageLevel level )>( &QgsMessageLog::messageReceived ),
&iu,
&InputUtils::onQgsLogMessageReceived );
QFile projectLoadingFile( ActiveProject::LOADING_FLAG_FILE_PATH );
if ( projectLoadingFile.exists() )
{
// Cleaning default project due to a project loading has crashed during the last run.
as.setDefaultProject( QString() );
projectLoadingFile.remove();
CoreUtils::log( QStringLiteral( "Loading project error" ), QStringLiteral( "Application has been unexpectedly finished during the last run." ) );
}
#ifdef INPUT_TEST
if ( tests.testingRequested() )
{
tests.initTestDeclarative();
tests.init( ma.get(), &iu, vm.get(), &pk, &as );
return tests.runTest();
}
#endif
// we ship our fonts because they do not need to be installed on the target platform
QStringList fonts;
fonts << ":/Inter-Regular.ttf"
<< ":/Inter-SemiBold.ttf";
for ( QString font : fonts )
{
if ( QFontDatabase::addApplicationFont( font ) == -1 )
qDebug() << "!! Failed to load font" << font;
else
qDebug() << "Loaded font" << font;
}
app.setFont( QFont( "Inter" ) );
QQuickStyle::setStyle( "Basic" );
QQmlEngine engine;
addQmlImportPath( engine );
initDeclarative();
// QGIS environment variables to set
// OGR_SQLITE_JOURNAL is set to DELETE to avoid working with WAL files
// and properly close connection after writting changes to gpkg.
qputenv( "OGR_SQLITE_JOURNAL", "DELETE" );
// TODO: rework to singletons instead (check Qt docs why)
// Register to QQmlEngine
engine.rootContext()->setContextProperty( "__notificationModel", ¬ificationModel );
engine.rootContext()->setContextProperty( "__androidUtils", &androidUtils );
engine.rootContext()->setContextProperty( "__iosUtils", &iosUtils );
engine.rootContext()->setContextProperty( "__inputUtils", &iu );
engine.rootContext()->setContextProperty( "__inputProjUtils", &inputProjUtils );
engine.rootContext()->setContextProperty( "__inputHelp", &help );
engine.rootContext()->setContextProperty( "__activeProject", &activeProject );
engine.rootContext()->setContextProperty( "__syncManager", &syncManager );
engine.rootContext()->setContextProperty( "__appSettings", &as );
engine.rootContext()->setContextProperty( "__merginApi", ma.get() );
engine.rootContext()->setContextProperty( "__merginProjectStatusModel", &mpsm );
engine.rootContext()->setContextProperty( "__activeLayer", &al );
engine.rootContext()->setContextProperty( "__projectWizard", &pw );
engine.rootContext()->setContextProperty( "__localProjectsManager", &localProjectsManager );
engine.rootContext()->setContextProperty( "__variablesManager", vm.get() );
engine.rootContext()->setContextProperty( "__positionKit", &pk );
// add image provider to pass QIcons/QImages from C++ to QML
engine.rootContext()->setContextProperty( "__layerTreeModelPixmapProvider", layerTreeModelPixmapProvider );
engine.addImageProvider( QLatin1String( "LayerTreeModelPixmapProvider" ), layerTreeModelPixmapProvider );
engine.rootContext()->setContextProperty( "__layerTreeFlatModelPixmapProvider", layerTreeFlatModelPixmapProvider );
engine.addImageProvider( QLatin1String( "LayerTreeFlatModelPixmapProvider" ), layerTreeFlatModelPixmapProvider );
engine.rootContext()->setContextProperty( "__layerDetailLegendImageProvider", layerDetailLegendImageProvider );
engine.addImageProvider( QLatin1String( "LayerDetailLegendImageProvider" ), layerDetailLegendImageProvider );
#ifdef HAVE_BLUETOOTH
engine.rootContext()->setContextProperty( "__haveBluetooth", true );
#else
engine.rootContext()->setContextProperty( "__haveBluetooth", false );
#endif
#ifdef MOBILE_OS
engine.rootContext()->setContextProperty( "__appwindowvisibility", QWindow::Maximized );
engine.rootContext()->setContextProperty( "__appwindowwidth", QVariant( 0 ) );
engine.rootContext()->setContextProperty( "__appwindowheight", QVariant( 0 ) );
#else
engine.rootContext()->setContextProperty( "__appwindowvisibility", QWindow::Windowed );
engine.rootContext()->setContextProperty( "__appwindowwidth", 640 );
engine.rootContext()->setContextProperty( "__appwindowheight", 1136 );
#endif
engine.rootContext()->setContextProperty( "__version", version );
// Even though enabling QT's HighDPI scaling removes the need to multiply pixel values with dp,
// there are screens that need a "little help", because system DPR has different value than the
// one we calculated. In these scenarios we use a ratio between real (our) DPR and DPR reported by QT.
// Use `value * __dp` for each pixel value in QML
qreal dp = InputUtils::calculateDpRatio();
engine.rootContext()->setContextProperty( "__dp", dp );
MMStyle *style = new MMStyle( &engine, dp );
engine.rootContext()->setContextProperty( "__style", style );
// Set safe areas for mobile devices
#ifdef ANDROID
auto safeAreaInsets = androidUtils.getSafeArea();
if ( safeAreaInsets.length() == 4 )
{
// Values from Android API must be scaled with dpr
qreal dpr = QGuiApplication::primaryScreen()->devicePixelRatio();
style->setSafeAreaTop( safeAreaInsets[0] / dpr );
style->setSafeAreaRight( safeAreaInsets[1] / dpr );
style->setSafeAreaBottom( safeAreaInsets[2] / dpr );
style->setSafeAreaLeft( safeAreaInsets[3] / dpr );
}
#elif defined( Q_OS_IOS )
//
// After migration to Qt 6.8.3, we can no longer reliably read the safe area on app startup (on iOS).
// It appears the UIWindow is not fully initialized, returning zero safe area insets.
// However, the window is correctly initialized once the event loop begins processing events.
// Therefore, we delay the safe area retrieval until after the event loop starts.
// This is a temporary workaround and might be replaced in the future by
// the more robust approach described in https://www.qt.io/blog/expanded-client-areas-and-safe-areas-in-qt-6.9.
//
const int SAFE_AREA_REFRESH_DELAY_MS = 10;
QTimer::singleShot( SAFE_AREA_REFRESH_DELAY_MS, &lambdaContext, [&iosUtils, &style]()
{
auto safeAreaInsets = iosUtils.getSafeArea();
if ( safeAreaInsets.length() == 4 )
{
style->setSafeAreaTop( safeAreaInsets[0] );
style->setSafeAreaRight( safeAreaInsets[1] );
style->setSafeAreaBottom( safeAreaInsets[2] );
style->setSafeAreaLeft( safeAreaInsets[3] );
}
} );
//
// Workaround for Qt bug <link> on iOS.
// ApplicationWindow's height and width are not properly updated (updated without signals),
// causing visual layout issues throughout the app.
// Forcing the screen to portrait mode triggers a recalculation and repaint of visual items,
// resolving the layout problems.
//
// Therefore, as a temporary fix, we rotate the screen to portrait mode on iOS when the app
// starts in landscape mode. It's crucial to execute this code after a short delay using
// QTimer, ensuring that the UIScene has been properly initialized.
//
const int SHORT_STARTUP_DELAY = 1;
QTimer::singleShot( SHORT_STARTUP_DELAY, &lambdaContext, [&iosUtils]()
{
iosUtils.rotateScreenToPortrait();
} );
#endif
// Set simulated position for desktop builds
#ifdef DESKTOP_OS
bool use_simulated_position = true;
#else
bool use_simulated_position = false;
#endif
engine.rootContext()->setContextProperty( "__use_simulated_position", use_simulated_position );
// show "new look and feel" welcome dialog on start?
QString lastAppVersion = as.appVersion();
bool showWelcomeToNewDesignDialog = false;
if ( !lastAppVersion.isEmpty() ) // this is not a first run?
{
int dotPos = lastAppVersion.indexOf( '.' );
if ( dotPos >= 0 && lastAppVersion.left( dotPos ).toInt() < 2024 )
{
// only show if previously we were on 2.x version, and now we're on 2024.x.y with the new design
showWelcomeToNewDesignDialog = true;
}
}
engine.rootContext()->setContextProperty( "__showWelcomeToNewDesignDialog", showWelcomeToNewDesignDialog );
QQmlComponent component( &engine, QUrl( "qrc:/com.merginmaps/imports/MMInput/main.qml" ) );
QObject *object = component.create();
if ( !component.errors().isEmpty() )
{
qDebug( "%s", QgsApplication::showSettings().toLocal8Bit().data() );
qDebug() << "****************************************";
qDebug() << "***** QML errors: *****";
qDebug() << "****************************************";
for ( const QQmlError &error : component.errors() )
{
qDebug() << " " << error;
}
qDebug() << "****************************************";
qDebug() << "****************************************";
}
if ( object == nullptr )
{
qDebug() << "FATAL ERROR: unable to create main.qml";
return EXIT_FAILURE;
}
#ifdef Q_OS_IOS
QString logoUrl = "qrc:logo.png";
#else
QString logoUrl = ":/logo.png";
#endif
if ( QQuickWindow *quickWindow = qobject_cast<QQuickWindow *>( object ) )
{
quickWindow->setIcon( QIcon( logoUrl ) );
}
#ifdef DESKTOP_OS
QCommandLineParser parser;
parser.addVersionOption();
parser.process( app );
#endif
// Add some data for debugging
qDebug() << iu.dumpScreenInfo();
qDebug() << "data directory: " << dataDir;
qDebug() << "All up and running";
#ifdef ANDROID
androidUtils.hideSplashScreen();
#endif
// save app version to settings
as.setAppVersion( version );
// Photos bigger that 512 MB (when uncompressed) will not load
QImageReader::setAllocationLimit( 512 );
int ret = EXIT_FAILURE;
try
{
ret = app.exec();
}
catch ( QgsException &e )
{
CoreUtils::log( "Main", QStringLiteral( "Caught unhandled QgsException %1" ).arg( e.what() ) );
}
catch ( std::exception &e )
{
CoreUtils::log( "Main", QStringLiteral( "Caught unhandled std::exception %1" ).arg( e.what() ) );
}
catch ( ... )
{
CoreUtils::log( "Main", QStringLiteral( "Caught unhandled unknown exception" ) );
}
return ret;
}