/
qgsproject.cpp
1629 lines (1203 loc) · 42.1 KB
/
qgsproject.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
/***************************************************************************
qgsproject.cpp - description
-------------------
begin : July 23, 2004
copyright : (C) 2004 by Mark Coletti
email : mcoletti at gmail.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 "qgsproject.h"
#include <memory>
#include <cassert>
#include <iostream>
using namespace std;
#include "qgslegend.h"
#include "qgsrect.h"
#include "qgsvectorlayer.h"
#include "qgsrasterlayer.h"
#include "qgsmaplayerregistry.h"
#include "qgsexception.h"
#include "qgsprojectproperty.h"
#include "qgsmapcanvas.h"
#include <QApplication>
#include <QFileInfo>
#include <QDomNode>
#include <QWidget>
#include <QObject>
#include <QTextStream>
#include <QToolBox>
#include <QSplitter>
static const char *const ident_ = "$Id$";
// / canonical project instance
QgsProject * QgsProject::theProject_;
/**
Take the given scope and key and convert them to a string list of key
tokens that will be used to navigate through a Property hierarchy
E.g., scope "someplugin" and key "/foo/bar/baz" will become a string list
of { "properties", "someplugin", "foo", "bar", "baz" }. "properties" is
always first because that's the permanent ``root'' Property node.
*/
static
QStringList makeKeyTokens_(QString const &scope, QString const &key)
{
// XXX - debugger probes
// const char * scope_str = scope.toLocal8Bit().data();
// const char * key_str = key.toLocal8Bit().data();
QStringList keyTokens = QStringList(scope);
keyTokens += QStringList::split('/', key);
// be sure to include the canonical root node
keyTokens.push_front( "properties" );
return keyTokens;
} // makeKeyTokens_
/**
return the property that matches the given key sequence, if any
@param sequence is a tokenized key sequence
@param p is likely to be the top level QgsPropertyKey in QgsProject:e:Imp.
@return null if not found, otherwise located Property
*/
static
QgsProperty * findKey_( QString const & scope,
QString const & key,
QgsPropertyKey & rootProperty )
{
QgsPropertyKey * currentProperty = &rootProperty;
QgsProperty * nextProperty; // link to next property down hiearchy
QStringList keySequence = makeKeyTokens_( scope, key );
while ( ! keySequence.isEmpty() )
{
// if the current head of the sequence list matches the property name,
// then traverse down the property hierarchy
if ( keySequence.first() == currentProperty->name() )
{
// remove front key since we're traversing down a level
keySequence.pop_front();
// if we have only one key name left, then return the key found
if ( 1 == keySequence.count() )
{
return currentProperty->find( keySequence.front() );
}
// if we're out of keys then the current property is the one we
// want; i.e., we're in the rate case of being at the top-most
// property node
else if ( keySequence.isEmpty() )
{
return currentProperty;
}
else if (( nextProperty = currentProperty->find( keySequence.first() ) ))
{
if ( nextProperty->isKey() )
{
currentProperty = dynamic_cast<QgsPropertyKey*>(nextProperty);
}
// it may be that this may be one of several property value
// nodes keyed by QDict string; if this is the last remaining
// key token and the next property is a value node, then
// that's the situation, so return the currentProperty
else if ( nextProperty->isValue() && (1 == keySequence.count()) )
{
return currentProperty;
}
else // QgsPropertyValue not Key, so return null
{
return 0x0;
}
}
else // if the next key down isn't found
{ // then the overall key sequence doesn't exist
return 0x0;
}
}
else
{
return 0x0;
}
}
return 0x0;
} // findKey_
/** add the given key and value
@param sequence is list of keys
@param rootProperty is the property from which to start adding
@param value the value associated with the key
*/
static
QgsProperty * addKey_( QString const & scope,
QString const & key,
QgsPropertyKey * rootProperty,
QVariant value )
{
QStringList keySequence = makeKeyTokens_( scope, key );
// cursor through property key/value hierarchy
QgsPropertyKey * currentProperty = rootProperty;
QgsProperty * newProperty; // link to next property down hiearchy
while ( ! keySequence.isEmpty() )
{
// if the current head of the sequence list matches the property name,
// then traverse down the property hierarchy
if ( keySequence.first() == currentProperty->name() )
{
// remove front key since we're traversing down a level
keySequence.pop_front();
// if key sequence has one last element, then we use that as the
// name to store the value
if ( 1 == keySequence.count() )
{
currentProperty->setValue( keySequence.front(), value );
return currentProperty;
}
// we're at the top element if popping the keySequence element
// will leave it empty; in that case, just add the key
else if ( keySequence.isEmpty() )
{
currentProperty->setValue( value );
return currentProperty;
}
else if (( newProperty = currentProperty->find( keySequence.first() ) ))
{
currentProperty = dynamic_cast<QgsPropertyKey*>(newProperty);
if ( currentProperty )
{
continue;
}
else // QgsPropertyValue not Key, so return null
{
return 0x0;
}
}
else // the next subkey doesn't exist, so add it
{
newProperty = currentProperty->addKey( keySequence.first() );
if ( newProperty )
{
currentProperty = dynamic_cast<QgsPropertyKey*>(newProperty);
}
continue;
}
}
else
{
return 0x0;
}
}
return 0x0;
} // addKey_
static
void removeKey_(QString const & scope,
QString const & key,
QgsPropertyKey & rootProperty)
{
QgsPropertyKey * currentProperty = &rootProperty;
QgsProperty * nextProperty; // link to next property down hiearchy
QgsPropertyKey * previousQgsPropertyKey; // link to previous property up hiearchy
QStringList keySequence = makeKeyTokens_( scope, key );
while ( ! keySequence.isEmpty() )
{
// if the current head of the sequence list matches the property name,
// then traverse down the property hierarchy
if ( keySequence.first() == currentProperty->name() )
{
// remove front key since we're traversing down a level
keySequence.pop_front();
// if we have only one key name left, then try to remove the key
// with that name
if ( 1 == keySequence.count() )
{
currentProperty->removeKey( keySequence.front() );
}
// if we're out of keys then the current property is the one we
// want to remove, but we can't delete it directly; we need to
// delete it from the parent property key container
else if ( keySequence.isEmpty() )
{
previousQgsPropertyKey->removeKey( currentProperty->name() );
}
else if (( nextProperty = currentProperty->find( keySequence.first() ) ))
{
previousQgsPropertyKey = currentProperty;
currentProperty = dynamic_cast<QgsPropertyKey*>(nextProperty);
if ( currentProperty )
{
continue;
}
else // QgsPropertyValue not Key, so return null
{
return;
}
}
else // if the next key down isn't found
{ // then the overall key sequence doesn't exist
return;
}
}
else
{
return;
}
}
} // void removeKey_
struct QgsProject::Imp
{
/// current physical project file
QFile file;
/// property hierarchy
QgsPropertyKey properties_;
/// project title
QString title;
/// true if project has been modified since it has been read or saved
bool dirty;
/// map units for current project
QGis::units mapUnits;
Imp()
: title(""),
dirty(false),
mapUnits(QGis::METERS)
{ // top property node is the root
// "properties" that contains all plug-in
// and extra property keys and values
properties_.name() = "properties"; // root property node always this value
}
/** clear project properties when a new project is started
*/
void clear()
{
QgsDebug( "Clearing project properties Impl->clear();" );
properties_.clearKeys();
mapUnits = QGis::METERS;
title = "";
// reset some default project properties
// XXX THESE SHOULD BE MOVED TO STATUSBAR RELATED SOURCE
QgsProject::instance()->writeEntry("PositionPrecision", "/Automatic", true);
QgsProject::instance()->writeEntry("PositionPrecision", "/DecimalPlaces", 2);
}
}; // struct QgsProject::Imp
QgsProject::QgsProject()
: imp_(new QgsProject::Imp)
{
// Set some default project properties
// XXX THESE SHOULD BE MOVED TO STATUSBAR RELATED SOURCE
writeEntry("PositionPrecision", "/Automatic", true);
writeEntry("PositionPrecision", "/DecimalPlaces", 2);
// XXX writeEntry() makes the project dirty, but it doesn't make sense
// for a new project to be dirty, so let's clean it up
dirty(false);
} // QgsProject ctor
QgsProject::~QgsProject()
{
// note that std::auto_ptr automatically deletes imp_ when it's destroyed
} // QgsProject dtor
QgsProject * QgsProject::instance()
{
if (!QgsProject::theProject_)
{
QgsProject::theProject_ = new QgsProject;
}
return QgsProject::theProject_;
} // QgsProject * instance()
void QgsProject::title(QString const &title)
{
imp_->title = title;
dirty(true);
} // void QgsProject::title
QString const & QgsProject::title() const
{
return imp_->title;
} // QgsProject::title() const
QGis::units QgsProject::mapUnits() const
{
return imp_->mapUnits;
} // QGis::units QgsProject::mapUnits() const
void QgsProject::mapUnits(QGis::units u)
{
imp_->mapUnits = u;
dirty(true);
} // void QgsProject::mapUnits(QGis::units u)
bool QgsProject::dirty() const
{
return imp_->dirty;
} // bool QgsProject::dirty()
void QgsProject::dirty(bool b)
{
imp_->dirty = b;
} // bool QgsProject::dirty()
void QgsProject::filename(QString const &name)
{
imp_->file.setName(name);
dirty(true);
} // void QgsProject::filename( QString const & name )
QString QgsProject::filename() const
{
return imp_->file.name();
} // QString QgsProject::filename() const
/// basically a debugging tool to dump property list values
static void dump_( QgsPropertyKey const & topQgsPropertyKey )
{
qDebug("current properties:");
topQgsPropertyKey.dump();
} // dump_
/**
Fetches extents, or area of interest, saved in project.
@note extent XML of form:
<extent>
<xmin>[number]</xmin>
<ymin>[number]</ymin>
<xmax>[number]</xmax>
<ymax>[number]</ymax>
</extent>
*/
static bool _getExtents(QDomDocument const &doc, QgsRect & aoi)
{
QDomNodeList extents = doc.elementsByTagName("extent");
if (extents.count() > 1)
{
qDebug
("there appears to be more than one extent in the project\nusing first one");
} else if (extents.count() < 1) // no extents found, so bail
{
return false;
}
QDomNode extentNode = extents.item(0);
QDomNode xminNode = extentNode.namedItem("xmin");
QDomNode yminNode = extentNode.namedItem("ymin");
QDomNode xmaxNode = extentNode.namedItem("xmax");
QDomNode ymaxNode = extentNode.namedItem("ymax");
QDomElement exElement = xminNode.toElement();
double xmin = exElement.text().toDouble();
aoi.setXmin(xmin);
exElement = yminNode.toElement();
double ymin = exElement.text().toDouble();
aoi.setYmin(ymin);
exElement = xmaxNode.toElement();
double xmax = exElement.text().toDouble();
aoi.setXmax(xmax);
exElement = ymaxNode.toElement();
double ymax = exElement.text().toDouble();
aoi.setYmax(ymax);
return true;
} // _getExtents
/**
Restore any optional properties found in "doc" to "properties".
<properties> tags for all optional properties. Within that there will be
scope tags. In the following example there exist one property in the
"fsplugin" scope. "layers" is a list containing three string values.
<properties>
<fsplugin>
<foo type="int" >42</foo>
<baz type="int" >1</baz>
<layers type="QStringList" >
<value>railroad</value>
<value>airport</value>
</layers>
<xyqzzy type="int" >1</xyqzzy>
<bar type="double" >123.456</bar>
<feature_types type="QStringList" >
<value>type</value>
</feature_types>
</fsplugin>
</properties>
@param project_properties should be the top QgsPropertyKey node.
*/
static
void
_getProperties(QDomDocument const &doc, QgsPropertyKey & project_properties)
{
QDomNodeList properties = doc.elementsByTagName("properties");
if (properties.count() > 1)
{
qDebug("there appears to be more than one ``properties'' XML tag ... bailing");
return;
} else if (properties.count() < 1) // no properties found, so we're done
{
return;
}
// item(0) because there should only be ONE
// "properties" node
QDomNodeList scopes = properties.item(0).childNodes();
if (scopes.count() < 1)
{
qDebug("empty ``properties'' XML tag ... bailing");
return;
}
QDomNode propertyNode = properties.item(0);
if ( ! project_properties.readXML( propertyNode ) )
{
qDebug("Project_properties.readXML() failed");
}
// DEPRECATED as functionality has been shoved down to QgsProperyKey::readXML()
// size_t i = 0;
// while (i < scopes.count())
// {
// QDomNode curr_scope_node = scopes.item(i);
// qDebug("found %d property node(s) for scope %s",
// curr_scope_node.childNodes().count(),
// (const char *) curr_scope_node.nodeName().utf8());
// QString key(curr_scope_node.nodeName());
// QgsPropertyKey * currentKey =
// dynamic_cast<QgsPropertyKey*>(project_properties.find( key ));
// if ( ! currentKey )
// {
// // if the property key doesn't yet exist, create an empty instance
// // of that key
// currentKey = project_properties.addKey( key );
// if ( ! currentKey )
// {
// qDebug( "%s:%d unable to add key", __FILE__, __LINE__ );
// }
// }
// if (! currentKey->readXML(curr_scope_node))
// {
// qDebug("%s:%d unable to read XML for property %s", __FILE__, __LINE__,
// (const char *) curr_scope_node.nodeName().utf8());
// }
// ++i;
// }
} // _getProperties
/**
Get the project map units
XML in file has this form:
<units>feet</units>
*/
static bool _getMapUnits(QDomDocument const &doc)
{
QDomNodeList nl = doc.elementsByTagName("units");
// since "units" is a new project file type, legacy project files will not
// have this element. If we do have such a legacy project file missing
// "units", then just return;
if (!nl.count())
{
return false;
}
QDomNode node = nl.item(0); // there should only be one, so zeroth element ok
QDomElement element = node.toElement();
if ("meters" == element.text())
{
QgsProject::instance()->mapUnits(QGis::METERS);
} else if ("feet" == element.text())
{
QgsProject::instance()->mapUnits(QGis::FEET);
} else if ("degrees" == element.text())
{
QgsProject::instance()->mapUnits(QGis::DEGREES);
} else if ("unknown" == element.text())
{
QgsProject::instance()->mapUnits(QGis::UNKNOWN);
} else
{
std::
cerr << __FILE__ << ":" << __LINE__ << " unknown map unit type " <<
element.text().toLocal8Bit().data() << "\n";
false;
}
return true;
} // _getMapUnits
/**
Get the project title
XML in file has this form:
<qgis projectname="default project">
<title>a project title</title>
@todo XXX we should go with the attribute xor <title>, not both.
*/
static void _getTitle(QDomDocument const &doc, QString & title)
{
QDomNodeList nl = doc.elementsByTagName("title");
title = ""; // by default the title will be empty
if (!nl.count())
{
qDebug("%s : %d %s", __FILE__, __LINE__, " unable to find title element\n");
return;
}
QDomNode titleNode = nl.item(0); // there should only be one, so zeroth element ok
if (!titleNode.hasChildNodes()) // if not, then there's no actual text
{
qDebug("%s : %d %s", __FILE__, __LINE__, " unable to find title element\n");
return;
}
QDomNode titleTextNode = titleNode.firstChild(); // should only have one child
if (!titleTextNode.isText())
{
qDebug("%s : %d %s", __FILE__, __LINE__, " unable to find title element\n");
return;
}
QDomText titleText = titleTextNode.toText();
title = titleText.data();
} // _getTitle
/** return the version string found in the given DOM document
@returns the version string or an empty string if none found
*/
static QString _getVersion(QDomDocument const &doc)
{
QDomNodeList nl = doc.elementsByTagName("qgis");
if (!nl.count())
{
QgsDebug(" unable to find qgis element in project file");
return "";
}
QDomNode qgisNode = nl.item(0); // there should only be one, so zeroth element ok
QDomElement qgisElement = qgisNode.toElement(); // qgis node should be element
return qgisElement.attribute("version");
} // _getVersion
/**
locate a qgsMapCanvas object
*/
static QgsMapCanvas * _findMapCanvas()
{
QgsMapCanvas * theMapCanvas = 0x0;
QString canonicalMapCanvasName = "theMapCanvas";
QWidgetList wlist = QApplication::topLevelWidgets();
foreach (QWidget *widget, QApplication::topLevelWidgets())
{
theMapCanvas = dynamic_cast <QgsMapCanvas *>(widget->child(canonicalMapCanvasName));
if (theMapCanvas)
{
break;
}
}
if (theMapCanvas)
{
return theMapCanvas;
}
else
{
qDebug("Unable to find the map canvas widget with name " + canonicalMapCanvasName);
return 0x0; // XXX some sort of error value? Exception?
}
} // _findMapCanvas
static QgsLegend * _findLegend()
{
QString canonicalLegendName = "theMapLegend";
QgsLegend* theLegend = 0;
QWidgetList wlist = QApplication::topLevelWidgets();
foreach (QWidget *widget, QApplication::topLevelWidgets())
{
theLegend = dynamic_cast <QgsLegend *>(widget->child(canonicalLegendName.toLocal8Bit().data(), 0, true));
if(theLegend)
{
break;
}
}
return theLegend;
} // _findLegend
/**
Read map layers from project file
@returns pair of < bool, list<QDomNode> >; bool is true if function worked;
else is false. list contains nodes corresponding to layers that couldn't
be loaded
@note XML of form:
<maplayer type="vector" visible="1" showInOverviewFlag="0">
<layername>Hydrop</layername>
<datasource>/data/usgs/city_shp/hydrop.shp</datasource>
<zorder>0</zorder>
<provider>ogr</provider>
<singlesymbol>
<renderitem>
<value>blabla</value>
<symbol>
<outlinecolor red="85" green="0" blue="255" />
<outlinestyle>SolidLine</outlinestyle>
<outlinewidth>1</outlinewidth>
<fillcolor red="0" green="170" blue="255" />
<fillpattern>SolidPattern</fillpattern>
</symbol>
<label>blabla</label>
</renderitem>
</singlesymbol>
<label>0</label>
<labelattributes>
<label text="Label" field="" />
<family name="Sans Serif" field="" />
<size value="12" units="pt" field="" />
<bold on="0" field="" />
<italic on="0" field="" />
<underline on="0" field="" />
<color red="0" green="0" blue="0" field="" />
<x field="" />
<y field="" />
<offset units="pt" x="0" xfield="" y="0" yfield="" />
<angle value="0" field="" />
<alignment value="center" field="" />
</labelattributes>
</maplayer>
*/
static pair< bool, list<QDomNode> > _getMapLayers(QDomDocument const &doc)
{
// Layer order is implicit in the order they are stored in the project file
QDomNodeList nl = doc.elementsByTagName("maplayer");
// XXX what is this used for? QString layerCount( QString::number(nl.count()) );
QString wk;
list<QDomNode> brokenNodes; // a list of DOM nodes corresponding to layers
// that we were unable to load; this could be
// because the layers were removed or
// re-located after the project was last saved
// process the map layer nodes
if (0 == nl.count()) // if we have no layers to process, bail
{
return make_pair(true, brokenNodes); // Decided to return "true" since it's
// possible for there to be a project with no
// layers; but also, more imporantly, this
// would cause the tests/qgsproject to fail
// since the test suite doesn't currently
// support test layers
}
bool returnStatus = true;
for (int i = 0; i < nl.count(); i++)
{
QDomNode node = nl.item(i);
QDomElement element = node.toElement();
QString type = element.attribute("type");
QgsMapLayer *mapLayer;
#ifdef QGISDEBUG
std::cerr << "type is " << type.toLocal8Bit().data() << std::endl;
#endif
if (type == "vector")
{
mapLayer = new QgsVectorLayer;
} else if (type == "raster")
{
mapLayer = new QgsRasterLayer;
}
Q_CHECK_PTR(mapLayer);
if (!mapLayer)
{
#ifdef QGISDEBUG
std::cerr << __FILE__ << " : " << __LINE__ << " unable to create layer\n";
#endif
return make_pair(false, brokenNodes);
}
// have the layer restore state that is stored in DOM node
if ( mapLayer->readXML(node) )
{
mapLayer = QgsMapLayerRegistry::instance()->addMapLayer(mapLayer);
mapLayer->refreshLegend();
}
else
{
delete mapLayer;
qDebug( "%s:%d unable to load %s layer", __FILE__, __LINE__, (const char *)type.toLocal8Bit().data() );
returnStatus = false; // flag that we had problems loading layers
brokenNodes.push_back( node );
}
}
return make_pair(returnStatus, brokenNodes);
} // _getMapLayers
/**
Sets the given canvas' extents
*/
static void _setCanvasExtent(QgsRect const &newExtent)
{
// first find the canonical map canvas
QgsMapCanvas *theMapCanvas = _findMapCanvas();
if (!theMapCanvas)
{
// Don't produce an error message here because _findMapCanvas
// does one already.
return; // XXX some sort of error value? Exception?
}
theMapCanvas->setExtent(newExtent);
// XXX sometimes the canvases are frozen here, sometimes not; this is a
// XXX worrisome inconsistency; regardless, unfreeze the canvases to ensure
// XXX a redraw
theMapCanvas->freeze(false);
theMapCanvas->update();
} // _setCanvasExtent()
/**
Get the full extent for the given canvas.
This is used to get the full extent of the main map canvas so that we can
set the overview canvas to that instead of stupidly setting the overview
canvas to the *same* extent that's in the main map canvas.
*/
static QgsRect _getFullExtent()
{
// XXX since this is a cut-n-paste from above, maybe generalize to a
// XXX separate function?
// first find the canonical map canvas
QgsMapCanvas *theMapCanvas = _findMapCanvas();
if (!theMapCanvas)
{
// _findMapCanvas() will produce an error if the map canvas wasn't found
return QgsRect(); // XXX some sort of error value? Exception?
}
return theMapCanvas->fullExtent();
} // _getFullExtent( )
/**
Get the extent for the given canvas.
This is used to get the extent of the main map canvas so that we can
set the overview canvas to that instead of stupidly setting the overview
canvas to the *same* extent that's in the main map canvas.
*/
static QgsRect _getExtent()
{
// XXX since this is a cut-n-paste from above, maybe generalize to a
// XXX separate function?
// first find the canonical map canvas
QgsMapCanvas *theMapCanvas = _findMapCanvas();
if (!theMapCanvas)
{
// _findMapCanvas will produce an error if the map canvas wasn't found
return QgsRect(); // XXX some sort of error value? Exception?
}
return theMapCanvas->extent();
} // _getExtent( )
/**
Please note that most of the contents were copied from qgsproject
*/
bool QgsProject::read(QFileInfo const &file)
{
imp_->file.setName(file.filePath());
return read();
} // QgsProject::read( QFile & file )