/
qgsmaplayer.cpp
1798 lines (1523 loc) · 54.4 KB
/
qgsmaplayer.cpp
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
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/***************************************************************************
qgsmaplayer.cpp - description
-------------------
begin : Fri Jun 28 2002
copyright : (C) 2002 by Gary E.Sherman
email : sherman at mrcc.com
***************************************************************************/
/***************************************************************************
* *
* 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 <QDir>
#include <QDomDocument>
#include <QDomElement>
#include <QDomImplementation>
#include <QDomNode>
#include <QFile>
#include <QFileInfo>
#include <QTextStream>
#include <QUrl>
#include <sqlite3.h>
#include "qgssqliteutils.h"
#include "qgssqliteutils.h"
#include "qgs3drendererregistry.h"
#include "qgsabstract3drenderer.h"
#include "qgsapplication.h"
#include "qgscoordinatereferencesystem.h"
#include "qgsdatasourceuri.h"
#include "qgslogger.h"
#include "qgsauthmanager.h"
#include "qgsmaplayer.h"
#include "qgsmaplayerlegend.h"
#include "qgsmaplayerstylemanager.h"
#include "qgsmeshlayer.h"
#include "qgspathresolver.h"
#include "qgsprojectfiletransform.h"
#include "qgsproject.h"
#include "qgsproviderregistry.h"
#include "qgsrasterlayer.h"
#include "qgsreadwritecontext.h"
#include "qgsrectangle.h"
#include "qgsvectorlayer.h"
#include "qgsvectordataprovider.h"
#include "qgsxmlutils.h"
#include "qgsstringutils.h"
QString QgsMapLayer::extensionPropertyType( QgsMapLayer::PropertyType type )
{
switch ( type )
{
case Metadata:
return QStringLiteral( ".qmd" );
case Style:
return QStringLiteral( ".qml" );
}
return QString();
}
QgsMapLayer::QgsMapLayer( QgsMapLayer::LayerType type,
const QString &lyrname,
const QString &source )
: mDataSource( source )
, mLayerName( lyrname )
, mLayerType( type )
, mUndoStack( new QUndoStack( this ) )
, mUndoStackStyles( new QUndoStack( this ) )
, mStyleManager( new QgsMapLayerStyleManager( this ) )
, mRefreshTimer( new QTimer( this ) )
{
//mShortName.replace( QRegExp( "[\\W]" ), "_" );
// Generate the unique ID of this layer
QString uuid = QUuid::createUuid().toString();
// trim { } from uuid
mID = lyrname + '_' + uuid.mid( 1, uuid.length() - 2 );
// Tidy the ID up to avoid characters that may cause problems
// elsewhere (e.g in some parts of XML). Replaces every non-word
// character (word characters are the alphabet, numbers and
// underscore) with an underscore.
// Note that the first backslashe in the regular expression is
// there for the compiler, so the pattern is actually \W
mID.replace( QRegExp( "[\\W]" ), QStringLiteral( "_" ) );
connect( mStyleManager, &QgsMapLayerStyleManager::currentStyleChanged, this, &QgsMapLayer::styleChanged );
connect( mRefreshTimer, &QTimer::timeout, this, [ = ] { triggerRepaint( true ); } );
}
QgsMapLayer::~QgsMapLayer()
{
delete m3DRenderer;
delete mLegend;
delete mStyleManager;
}
void QgsMapLayer::clone( QgsMapLayer *layer ) const
{
layer->setBlendMode( blendMode() );
Q_FOREACH ( const QString &s, styleManager()->styles() )
{
layer->styleManager()->addStyle( s, styleManager()->style( s ) );
}
layer->setName( name() );
layer->setShortName( shortName() );
layer->setExtent( extent() );
layer->setMaximumScale( maximumScale() );
layer->setMinimumScale( minimumScale() );
layer->setScaleBasedVisibility( hasScaleBasedVisibility() );
layer->setTitle( title() );
layer->setAbstract( abstract() );
layer->setKeywordList( keywordList() );
layer->setDataUrl( dataUrl() );
layer->setDataUrlFormat( dataUrlFormat() );
layer->setAttribution( attribution() );
layer->setAttributionUrl( attributionUrl() );
layer->setMetadataUrl( metadataUrl() );
layer->setMetadataUrlType( metadataUrlType() );
layer->setMetadataUrlFormat( metadataUrlFormat() );
layer->setLegendUrl( legendUrl() );
layer->setLegendUrlFormat( legendUrlFormat() );
layer->setDependencies( dependencies() );
layer->setCrs( crs() );
layer->setCustomProperties( mCustomProperties );
}
QgsMapLayer::LayerType QgsMapLayer::type() const
{
return mLayerType;
}
QString QgsMapLayer::id() const
{
return mID;
}
void QgsMapLayer::setName( const QString &name )
{
if ( name == mLayerName )
return;
mLayerName = name;
emit nameChanged();
}
QString QgsMapLayer::name() const
{
QgsDebugMsgLevel( "returning name '" + mLayerName + '\'', 4 );
return mLayerName;
}
QgsDataProvider *QgsMapLayer::dataProvider()
{
return nullptr;
}
const QgsDataProvider *QgsMapLayer::dataProvider() const
{
return nullptr;
}
QString QgsMapLayer::publicSource() const
{
// Redo this every time we're asked for it, as we don't know if
// dataSource has changed.
QString safeName = QgsDataSourceUri::removePassword( mDataSource );
return safeName;
}
QString QgsMapLayer::source() const
{
return mDataSource;
}
QgsRectangle QgsMapLayer::extent() const
{
return mExtent;
}
void QgsMapLayer::setBlendMode( QPainter::CompositionMode blendMode )
{
mBlendMode = blendMode;
emit blendModeChanged( blendMode );
emit styleChanged();
}
QPainter::CompositionMode QgsMapLayer::blendMode() const
{
return mBlendMode;
}
bool QgsMapLayer::readLayerXml( const QDomElement &layerElement, QgsReadWriteContext &context )
{
bool layerError;
QDomNode mnl;
QDomElement mne;
// read provider
QString provider;
mnl = layerElement.namedItem( QStringLiteral( "provider" ) );
mne = mnl.toElement();
provider = mne.text();
// set data source
mnl = layerElement.namedItem( QStringLiteral( "datasource" ) );
mne = mnl.toElement();
mDataSource = mne.text();
// if the layer needs authentication, ensure the master password is set
QRegExp rx( "authcfg=([a-z]|[A-Z]|[0-9]){7}" );
if ( ( rx.indexIn( mDataSource ) != -1 )
&& !QgsApplication::authManager()->setMasterPassword( true ) )
{
return false;
}
mDataSource = decodedSource( mDataSource, provider, context );
// Set the CRS from project file, asking the user if necessary.
// Make it the saved CRS to have WMS layer projected correctly.
// We will still overwrite whatever GDAL etc picks up anyway
// further down this function.
mnl = layerElement.namedItem( QStringLiteral( "layername" ) );
mne = mnl.toElement();
QgsCoordinateReferenceSystem savedCRS;
CUSTOM_CRS_VALIDATION savedValidation;
QDomNode srsNode = layerElement.namedItem( QStringLiteral( "srs" ) );
mCRS.readXml( srsNode );
mCRS.setValidationHint( tr( "Specify CRS for layer %1" ).arg( mne.text() ) );
mCRS.validate();
savedCRS = mCRS;
// Do not validate any projections in children, they will be overwritten anyway.
// No need to ask the user for a projections when it is overwritten, is there?
savedValidation = QgsCoordinateReferenceSystem::customCrsValidation();
QgsCoordinateReferenceSystem::setCustomCrsValidation( nullptr );
// read custom properties before passing reading further to a subclass, so that
// the subclass can also read custom properties
readCustomProperties( layerElement );
QgsReadWriteContextCategoryPopper p = context.enterCategory( tr( "Layer" ), mne.text() );
// now let the children grab what they need from the Dom node.
layerError = !readXml( layerElement, context );
// overwrite CRS with what we read from project file before the raster/vector
// file reading functions changed it. They will if projections is specified in the file.
// FIXME: is this necessary?
QgsCoordinateReferenceSystem::setCustomCrsValidation( savedValidation );
mCRS = savedCRS;
// Abort if any error in layer, such as not found.
if ( layerError )
{
return false;
}
// the internal name is just the data source basename
//QFileInfo dataSourceFileInfo( mDataSource );
//internalName = dataSourceFileInfo.baseName();
// set ID
mnl = layerElement.namedItem( QStringLiteral( "id" ) );
if ( ! mnl.isNull() )
{
mne = mnl.toElement();
if ( ! mne.isNull() && mne.text().length() > 10 ) // should be at least 17 (yyyyMMddhhmmsszzz)
{
mID = mne.text();
}
}
// use scale dependent visibility flag
setScaleBasedVisibility( layerElement.attribute( QStringLiteral( "hasScaleBasedVisibilityFlag" ) ).toInt() == 1 );
if ( layerElement.hasAttribute( QStringLiteral( "minimumScale" ) ) )
{
// older element, when scales were reversed
setMaximumScale( layerElement.attribute( QStringLiteral( "minimumScale" ) ).toDouble() );
setMinimumScale( layerElement.attribute( QStringLiteral( "maximumScale" ) ).toDouble() );
}
else
{
setMaximumScale( layerElement.attribute( QStringLiteral( "maxScale" ) ).toDouble() );
setMinimumScale( layerElement.attribute( QStringLiteral( "minScale" ) ).toDouble() );
}
setAutoRefreshInterval( layerElement.attribute( QStringLiteral( "autoRefreshTime" ), QStringLiteral( "0" ) ).toInt() );
setAutoRefreshEnabled( layerElement.attribute( QStringLiteral( "autoRefreshEnabled" ), QStringLiteral( "0" ) ).toInt() );
setRefreshOnNofifyMessage( layerElement.attribute( QStringLiteral( "refreshOnNotifyMessage" ), QString() ) );
setRefreshOnNotifyEnabled( layerElement.attribute( QStringLiteral( "refreshOnNotifyEnabled" ), QStringLiteral( "0" ) ).toInt() );
// set name
mnl = layerElement.namedItem( QStringLiteral( "layername" ) );
mne = mnl.toElement();
setName( mne.text() );
//short name
QDomElement shortNameElem = layerElement.firstChildElement( QStringLiteral( "shortname" ) );
if ( !shortNameElem.isNull() )
{
mShortName = shortNameElem.text();
}
//title
QDomElement titleElem = layerElement.firstChildElement( QStringLiteral( "title" ) );
if ( !titleElem.isNull() )
{
mTitle = titleElem.text();
}
//abstract
QDomElement abstractElem = layerElement.firstChildElement( QStringLiteral( "abstract" ) );
if ( !abstractElem.isNull() )
{
mAbstract = abstractElem.text();
}
//keywordList
QDomElement keywordListElem = layerElement.firstChildElement( QStringLiteral( "keywordList" ) );
if ( !keywordListElem.isNull() )
{
QStringList kwdList;
for ( QDomNode n = keywordListElem.firstChild(); !n.isNull(); n = n.nextSibling() )
{
kwdList << n.toElement().text();
}
mKeywordList = kwdList.join( QStringLiteral( ", " ) );
}
//metadataUrl
QDomElement dataUrlElem = layerElement.firstChildElement( QStringLiteral( "dataUrl" ) );
if ( !dataUrlElem.isNull() )
{
mDataUrl = dataUrlElem.text();
mDataUrlFormat = dataUrlElem.attribute( QStringLiteral( "format" ), QLatin1String( "" ) );
}
//legendUrl
QDomElement legendUrlElem = layerElement.firstChildElement( QStringLiteral( "legendUrl" ) );
if ( !legendUrlElem.isNull() )
{
mLegendUrl = legendUrlElem.text();
mLegendUrlFormat = legendUrlElem.attribute( QStringLiteral( "format" ), QLatin1String( "" ) );
}
//attribution
QDomElement attribElem = layerElement.firstChildElement( QStringLiteral( "attribution" ) );
if ( !attribElem.isNull() )
{
mAttribution = attribElem.text();
mAttributionUrl = attribElem.attribute( QStringLiteral( "href" ), QLatin1String( "" ) );
}
//metadataUrl
QDomElement metaUrlElem = layerElement.firstChildElement( QStringLiteral( "metadataUrl" ) );
if ( !metaUrlElem.isNull() )
{
mMetadataUrl = metaUrlElem.text();
mMetadataUrlType = metaUrlElem.attribute( QStringLiteral( "type" ), QLatin1String( "" ) );
mMetadataUrlFormat = metaUrlElem.attribute( QStringLiteral( "format" ), QLatin1String( "" ) );
}
// mMetadata.readFromLayer( this );
QDomElement metadataElem = layerElement.firstChildElement( QStringLiteral( "resourceMetadata" ) );
mMetadata.readMetadataXml( metadataElem );
return true;
} // bool QgsMapLayer::readLayerXML
bool QgsMapLayer::readXml( const QDomNode &layer_node, QgsReadWriteContext &context )
{
Q_UNUSED( layer_node );
Q_UNUSED( context );
// NOP by default; children will over-ride with behavior specific to them
return true;
} // void QgsMapLayer::readXml
bool QgsMapLayer::writeLayerXml( QDomElement &layerElement, QDomDocument &document, const QgsReadWriteContext &context ) const
{
// use scale dependent visibility flag
layerElement.setAttribute( QStringLiteral( "hasScaleBasedVisibilityFlag" ), hasScaleBasedVisibility() ? 1 : 0 );
layerElement.setAttribute( QStringLiteral( "maxScale" ), QString::number( maximumScale() ) );
layerElement.setAttribute( QStringLiteral( "minScale" ), QString::number( minimumScale() ) );
if ( !extent().isNull() )
{
layerElement.appendChild( QgsXmlUtils::writeRectangle( mExtent, document ) );
}
layerElement.setAttribute( QStringLiteral( "autoRefreshTime" ), QString::number( mRefreshTimer->interval() ) );
layerElement.setAttribute( QStringLiteral( "autoRefreshEnabled" ), mRefreshTimer->isActive() ? 1 : 0 );
layerElement.setAttribute( QStringLiteral( "refreshOnNotifyEnabled" ), mIsRefreshOnNofifyEnabled ? 1 : 0 );
layerElement.setAttribute( QStringLiteral( "refreshOnNotifyMessage" ), mRefreshOnNofifyMessage );
// ID
QDomElement layerId = document.createElement( QStringLiteral( "id" ) );
QDomText layerIdText = document.createTextNode( id() );
layerId.appendChild( layerIdText );
layerElement.appendChild( layerId );
// data source
QDomElement dataSource = document.createElement( QStringLiteral( "datasource" ) );
QString src = encodedSource( source(), context );
QDomText dataSourceText = document.createTextNode( src );
dataSource.appendChild( dataSourceText );
layerElement.appendChild( dataSource );
// layer name
QDomElement layerName = document.createElement( QStringLiteral( "layername" ) );
QDomText layerNameText = document.createTextNode( name() );
layerName.appendChild( layerNameText );
layerElement.appendChild( layerName );
// layer short name
if ( !mShortName.isEmpty() )
{
QDomElement layerShortName = document.createElement( QStringLiteral( "shortname" ) );
QDomText layerShortNameText = document.createTextNode( mShortName );
layerShortName.appendChild( layerShortNameText );
layerElement.appendChild( layerShortName );
}
// layer title
if ( !mTitle.isEmpty() )
{
QDomElement layerTitle = document.createElement( QStringLiteral( "title" ) );
QDomText layerTitleText = document.createTextNode( mTitle );
layerTitle.appendChild( layerTitleText );
layerElement.appendChild( layerTitle );
}
// layer abstract
if ( !mAbstract.isEmpty() )
{
QDomElement layerAbstract = document.createElement( QStringLiteral( "abstract" ) );
QDomText layerAbstractText = document.createTextNode( mAbstract );
layerAbstract.appendChild( layerAbstractText );
layerElement.appendChild( layerAbstract );
}
// layer keyword list
QStringList keywordStringList = keywordList().split( ',' );
if ( !keywordStringList.isEmpty() )
{
QDomElement layerKeywordList = document.createElement( QStringLiteral( "keywordList" ) );
for ( int i = 0; i < keywordStringList.size(); ++i )
{
QDomElement layerKeywordValue = document.createElement( QStringLiteral( "value" ) );
QDomText layerKeywordText = document.createTextNode( keywordStringList.at( i ).trimmed() );
layerKeywordValue.appendChild( layerKeywordText );
layerKeywordList.appendChild( layerKeywordValue );
}
layerElement.appendChild( layerKeywordList );
}
// layer metadataUrl
QString aDataUrl = dataUrl();
if ( !aDataUrl.isEmpty() )
{
QDomElement layerDataUrl = document.createElement( QStringLiteral( "dataUrl" ) );
QDomText layerDataUrlText = document.createTextNode( aDataUrl );
layerDataUrl.appendChild( layerDataUrlText );
layerDataUrl.setAttribute( QStringLiteral( "format" ), dataUrlFormat() );
layerElement.appendChild( layerDataUrl );
}
// layer legendUrl
QString aLegendUrl = legendUrl();
if ( !aLegendUrl.isEmpty() )
{
QDomElement layerLegendUrl = document.createElement( QStringLiteral( "legendUrl" ) );
QDomText layerLegendUrlText = document.createTextNode( aLegendUrl );
layerLegendUrl.appendChild( layerLegendUrlText );
layerLegendUrl.setAttribute( QStringLiteral( "format" ), legendUrlFormat() );
layerElement.appendChild( layerLegendUrl );
}
// layer attribution
QString aAttribution = attribution();
if ( !aAttribution.isEmpty() )
{
QDomElement layerAttribution = document.createElement( QStringLiteral( "attribution" ) );
QDomText layerAttributionText = document.createTextNode( aAttribution );
layerAttribution.appendChild( layerAttributionText );
layerAttribution.setAttribute( QStringLiteral( "href" ), attributionUrl() );
layerElement.appendChild( layerAttribution );
}
// layer metadataUrl
QString aMetadataUrl = metadataUrl();
if ( !aMetadataUrl.isEmpty() )
{
QDomElement layerMetadataUrl = document.createElement( QStringLiteral( "metadataUrl" ) );
QDomText layerMetadataUrlText = document.createTextNode( aMetadataUrl );
layerMetadataUrl.appendChild( layerMetadataUrlText );
layerMetadataUrl.setAttribute( QStringLiteral( "type" ), metadataUrlType() );
layerMetadataUrl.setAttribute( QStringLiteral( "format" ), metadataUrlFormat() );
layerElement.appendChild( layerMetadataUrl );
}
// timestamp if supported
if ( timestamp() > QDateTime() )
{
QDomElement stamp = document.createElement( QStringLiteral( "timestamp" ) );
QDomText stampText = document.createTextNode( timestamp().toString( Qt::ISODate ) );
stamp.appendChild( stampText );
layerElement.appendChild( stamp );
}
layerElement.appendChild( layerName );
// zorder
// This is no longer stored in the project file. It is superfluous since the layers
// are written and read in the proper order.
// spatial reference system id
QDomElement mySrsElement = document.createElement( QStringLiteral( "srs" ) );
mCRS.writeXml( mySrsElement, document );
layerElement.appendChild( mySrsElement );
// layer metadata
QDomElement myMetadataElem = document.createElement( QStringLiteral( "resourceMetadata" ) );
mMetadata.writeMetadataXml( myMetadataElem, document );
layerElement.appendChild( myMetadataElem );
// now append layer node to map layer node
writeCustomProperties( layerElement, document );
return writeXml( layerElement, document, context );
}
bool QgsMapLayer::writeXml( QDomNode &layer_node, QDomDocument &document, const QgsReadWriteContext &context ) const
{
Q_UNUSED( layer_node );
Q_UNUSED( document );
Q_UNUSED( context );
// NOP by default; children will over-ride with behavior specific to them
return true;
}
QString QgsMapLayer::encodedSource( const QString &source, const QgsReadWriteContext &context ) const
{
Q_UNUSED( context );
return source;
}
QString QgsMapLayer::decodedSource( const QString &source, const QString &dataProvider, const QgsReadWriteContext &context ) const
{
Q_UNUSED( context );
Q_UNUSED( dataProvider );
return source;
}
void QgsMapLayer::resolveReferences( QgsProject *project )
{
if ( m3DRenderer )
m3DRenderer->resolveReferences( *project );
}
void QgsMapLayer::readCustomProperties( const QDomNode &layerNode, const QString &keyStartsWith )
{
mCustomProperties.readXml( layerNode, keyStartsWith );
}
void QgsMapLayer::writeCustomProperties( QDomNode &layerNode, QDomDocument &doc ) const
{
mCustomProperties.writeXml( layerNode, doc );
}
void QgsMapLayer::readStyleManager( const QDomNode &layerNode )
{
QDomElement styleMgrElem = layerNode.firstChildElement( QStringLiteral( "map-layer-style-manager" ) );
if ( !styleMgrElem.isNull() )
mStyleManager->readXml( styleMgrElem );
else
mStyleManager->reset();
}
void QgsMapLayer::writeStyleManager( QDomNode &layerNode, QDomDocument &doc ) const
{
if ( mStyleManager )
{
QDomElement styleMgrElem = doc.createElement( QStringLiteral( "map-layer-style-manager" ) );
mStyleManager->writeXml( styleMgrElem );
layerNode.appendChild( styleMgrElem );
}
}
bool QgsMapLayer::isValid() const
{
return mValid;
}
#if 0
void QgsMapLayer::connectNotify( const char *signal )
{
Q_UNUSED( signal );
QgsDebugMsgLevel( "QgsMapLayer connected to " + QString( signal ), 3 );
} // QgsMapLayer::connectNotify
#endif
bool QgsMapLayer::isInScaleRange( double scale ) const
{
return !mScaleBasedVisibility ||
( ( mMinScale == 0 || mMinScale * Qgis::SCALE_PRECISION < scale )
&& ( mMaxScale == 0 || scale < mMaxScale ) );
}
bool QgsMapLayer::hasScaleBasedVisibility() const
{
return mScaleBasedVisibility;
}
bool QgsMapLayer::hasAutoRefreshEnabled() const
{
return mRefreshTimer->isActive();
}
int QgsMapLayer::autoRefreshInterval() const
{
return mRefreshTimer->interval();
}
void QgsMapLayer::setAutoRefreshInterval( int interval )
{
if ( interval <= 0 )
{
mRefreshTimer->stop();
mRefreshTimer->setInterval( 0 );
}
else
{
mRefreshTimer->setInterval( interval );
}
emit autoRefreshIntervalChanged( mRefreshTimer->isActive() ? mRefreshTimer->interval() : 0 );
}
void QgsMapLayer::setAutoRefreshEnabled( bool enabled )
{
if ( !enabled )
mRefreshTimer->stop();
else if ( mRefreshTimer->interval() > 0 )
mRefreshTimer->start();
emit autoRefreshIntervalChanged( mRefreshTimer->isActive() ? mRefreshTimer->interval() : 0 );
}
const QgsLayerMetadata &QgsMapLayer::metadata() const
{
return mMetadata;
}
void QgsMapLayer::setMaximumScale( double scale )
{
mMinScale = scale;
}
double QgsMapLayer::maximumScale() const
{
return mMinScale;
}
void QgsMapLayer::setMinimumScale( double scale )
{
mMaxScale = scale;
}
void QgsMapLayer::setScaleBasedVisibility( const bool enabled )
{
mScaleBasedVisibility = enabled;
}
double QgsMapLayer::minimumScale() const
{
return mMaxScale;
}
QStringList QgsMapLayer::subLayers() const
{
return QStringList(); // Empty
}
void QgsMapLayer::setLayerOrder( const QStringList &layers )
{
Q_UNUSED( layers );
// NOOP
}
void QgsMapLayer::setSubLayerVisibility( const QString &name, bool vis )
{
Q_UNUSED( name );
Q_UNUSED( vis );
// NOOP
}
QgsCoordinateReferenceSystem QgsMapLayer::crs() const
{
return mCRS;
}
void QgsMapLayer::setCrs( const QgsCoordinateReferenceSystem &srs, bool emitSignal )
{
mCRS = srs;
if ( !mCRS.isValid() )
{
mCRS.setValidationHint( tr( "Specify CRS for layer %1" ).arg( name() ) );
mCRS.validate();
}
if ( emitSignal )
emit crsChanged();
}
QString QgsMapLayer::formatLayerName( const QString &name )
{
QString layerName( name );
layerName.replace( '_', ' ' );
layerName = QgsStringUtils::capitalize( layerName, QgsStringUtils::ForceFirstLetterToCapital );
return layerName;
}
QString QgsMapLayer::baseURI( PropertyType type ) const
{
QString myURI = publicSource();
// if file is using the VSIFILE mechanism, remove the prefix
if ( myURI.startsWith( QLatin1String( "/vsigzip/" ), Qt::CaseInsensitive ) )
{
myURI.remove( 0, 9 );
}
else if ( myURI.startsWith( QLatin1String( "/vsizip/" ), Qt::CaseInsensitive ) &&
myURI.endsWith( QLatin1String( ".zip" ), Qt::CaseInsensitive ) )
{
// ideally we should look for .qml file inside zip file
myURI.remove( 0, 8 );
}
else if ( myURI.startsWith( QLatin1String( "/vsitar/" ), Qt::CaseInsensitive ) &&
( myURI.endsWith( QLatin1String( ".tar" ), Qt::CaseInsensitive ) ||
myURI.endsWith( QLatin1String( ".tar.gz" ), Qt::CaseInsensitive ) ||
myURI.endsWith( QLatin1String( ".tgz" ), Qt::CaseInsensitive ) ) )
{
// ideally we should look for .qml file inside tar file
myURI.remove( 0, 8 );
}
QFileInfo myFileInfo( myURI );
QString key;
if ( myFileInfo.exists() )
{
// if file is using the /vsizip/ or /vsigzip/ mechanism, cleanup the name
if ( myURI.endsWith( QLatin1String( ".gz" ), Qt::CaseInsensitive ) )
myURI.chop( 3 );
else if ( myURI.endsWith( QLatin1String( ".zip" ), Qt::CaseInsensitive ) )
myURI.chop( 4 );
else if ( myURI.endsWith( QLatin1String( ".tar" ), Qt::CaseInsensitive ) )
myURI.chop( 4 );
else if ( myURI.endsWith( QLatin1String( ".tar.gz" ), Qt::CaseInsensitive ) )
myURI.chop( 7 );
else if ( myURI.endsWith( QLatin1String( ".tgz" ), Qt::CaseInsensitive ) )
myURI.chop( 4 );
myFileInfo.setFile( myURI );
// get the file name for our .qml style file
key = myFileInfo.path() + QDir::separator() + myFileInfo.completeBaseName() + QgsMapLayer::extensionPropertyType( type );
}
else
{
key = publicSource();
}
return key;
}
QString QgsMapLayer::metadataUri() const
{
return baseURI( PropertyType::Metadata );
}
QString QgsMapLayer::saveDefaultMetadata( bool &resultFlag )
{
return saveNamedMetadata( metadataUri(), resultFlag );
}
QString QgsMapLayer::loadDefaultMetadata( bool &resultFlag )
{
return loadNamedMetadata( metadataUri(), resultFlag );
}
QString QgsMapLayer::styleURI() const
{
return baseURI( PropertyType::Style );
}
QString QgsMapLayer::loadDefaultStyle( bool &resultFlag )
{
return loadNamedStyle( styleURI(), resultFlag );
}
bool QgsMapLayer::loadNamedMetadataFromDatabase( const QString &db, const QString &uri, QString &qmd )
{
return loadNamedPropertyFromDatabase( db, uri, qmd, PropertyType::Metadata );
}
bool QgsMapLayer::loadNamedStyleFromDatabase( const QString &db, const QString &uri, QString &qml )
{
return loadNamedPropertyFromDatabase( db, uri, qml, PropertyType::Style );
}
bool QgsMapLayer::loadNamedPropertyFromDatabase( const QString &db, const QString &uri, QString &xml, QgsMapLayer::PropertyType type )
{
QgsDebugMsgLevel( QString( "db = %1 uri = %2" ).arg( db, uri ), 4 );
bool resultFlag = false;
// read from database
sqlite3_database_unique_ptr database;
sqlite3_statement_unique_ptr statement;
int myResult;
QgsDebugMsgLevel( QString( "Trying to load style or metadata for \"%1\" from \"%2\"" ).arg( uri, db ), 4 );
if ( db.isEmpty() || !QFile( db ).exists() )
return false;
myResult = database.open_v2( db, SQLITE_OPEN_READONLY, nullptr );
if ( myResult != SQLITE_OK )
{
return false;
}
QString mySql;
switch ( type )
{
case Metadata:
mySql = QStringLiteral( "select qmd from tbl_metadata where metadata=?" );
break;
case Style:
mySql = QStringLiteral( "select qml from tbl_styles where style=?" );
break;
}
statement = database.prepare( mySql, myResult );
if ( myResult == SQLITE_OK )
{
QByteArray param = uri.toUtf8();
if ( sqlite3_bind_text( statement.get(), 1, param.data(), param.length(), SQLITE_STATIC ) == SQLITE_OK &&
sqlite3_step( statement.get() ) == SQLITE_ROW )
{
xml = QString::fromUtf8( reinterpret_cast< const char * >( sqlite3_column_text( statement.get(), 0 ) ) );
resultFlag = true;
}
}
return resultFlag;
}
QString QgsMapLayer::loadNamedStyle( const QString &uri, bool &resultFlag )
{
return loadNamedProperty( uri, PropertyType::Style, resultFlag );
}
QString QgsMapLayer::loadNamedProperty( const QString &uri, QgsMapLayer::PropertyType type, bool &resultFlag )
{
QgsDebugMsgLevel( QString( "uri = %1 myURI = %2" ).arg( uri, publicSource() ), 4 );
resultFlag = false;
QDomDocument myDocument( QStringLiteral( "qgis" ) );
// location of problem associated with errorMsg
int line, column;
QString myErrorMessage;
QFile myFile( uri );
if ( myFile.open( QFile::ReadOnly ) )
{
QgsDebugMsg( QString( "file found %1" ).arg( uri ) );
// read file
resultFlag = myDocument.setContent( &myFile, &myErrorMessage, &line, &column );
if ( !resultFlag )
myErrorMessage = tr( "%1 at line %2 column %3" ).arg( myErrorMessage ).arg( line ).arg( column );
myFile.close();
}
else
{
QFileInfo project( QgsProject::instance()->fileName() );
QgsDebugMsgLevel( QString( "project fileName: %1" ).arg( project.absoluteFilePath() ), 4 );
QString xml;
switch ( type )
{
case QgsMapLayer::Style:
{
if ( loadNamedStyleFromDatabase( QDir( QgsApplication::qgisSettingsDirPath() ).absoluteFilePath( QStringLiteral( "qgis.qmldb" ) ), uri, xml ) ||
( project.exists() && loadNamedStyleFromDatabase( project.absoluteDir().absoluteFilePath( project.baseName() + ".qmldb" ), uri, xml ) ) ||
loadNamedStyleFromDatabase( QDir( QgsApplication::pkgDataPath() ).absoluteFilePath( QStringLiteral( "resources/qgis.qmldb" ) ), uri, xml ) )
{
resultFlag = myDocument.setContent( xml, &myErrorMessage, &line, &column );
if ( !resultFlag )
{
myErrorMessage = tr( "%1 at line %2 column %3" ).arg( myErrorMessage ).arg( line ).arg( column );
}
}
else
{
myErrorMessage = tr( "Style not found in database" );
resultFlag = false;
}
break;
}
case QgsMapLayer::Metadata:
{
if ( loadNamedMetadataFromDatabase( QDir( QgsApplication::qgisSettingsDirPath() ).absoluteFilePath( QStringLiteral( "qgis.qmldb" ) ), uri, xml ) ||
( project.exists() && loadNamedMetadataFromDatabase( project.absoluteDir().absoluteFilePath( project.baseName() + ".qmldb" ), uri, xml ) ) ||
loadNamedMetadataFromDatabase( QDir( QgsApplication::pkgDataPath() ).absoluteFilePath( QStringLiteral( "resources/qgis.qmldb" ) ), uri, xml ) )
{
resultFlag = myDocument.setContent( xml, &myErrorMessage, &line, &column );
if ( !resultFlag )
{
myErrorMessage = tr( "%1 at line %2 column %3" ).arg( myErrorMessage ).arg( line ).arg( column );
}
}
else
{
myErrorMessage = tr( "Metadata not found in database" );
resultFlag = false;
}
break;
}
}
}
if ( !resultFlag )
{
return myErrorMessage;
}
switch ( type )
{
case QgsMapLayer::Style:
resultFlag = importNamedStyle( myDocument, myErrorMessage );
if ( !resultFlag )
myErrorMessage = tr( "Loading style file %1 failed because:\n%2" ).arg( uri, myErrorMessage );
break;
case QgsMapLayer::Metadata:
resultFlag = importNamedMetadata( myDocument, myErrorMessage );
if ( !resultFlag )
myErrorMessage = tr( "Loading metadata file %1 failed because:\n%2" ).arg( uri, myErrorMessage );
break;
}
return myErrorMessage;
}
bool QgsMapLayer::importNamedMetadata( QDomDocument &document, QString &errorMessage )
{
QDomElement myRoot = document.firstChildElement( QStringLiteral( "qgis" ) );
if ( myRoot.isNull() )
{
errorMessage = tr( "Root <qgis> element could not be found" );
return false;
}
return mMetadata.readMetadataXml( myRoot );
}