21
21
#include " qgslogger.h"
22
22
23
23
#include < stdlib.h> // for random()
24
+ #include < sys/time.h>
24
25
25
26
QgsVectorGradientColorRampV2::QgsVectorGradientColorRampV2 ( QColor color1, QColor color2 )
26
27
: mColor1( color1 ), mColor2( color2 )
@@ -256,11 +257,25 @@ QgsStringMap QgsVectorColorBrewerColorRampV2::properties() const
256
257
257
258
/*
258
259
TODO
259
- - return a suitable name (scheme_variant)
260
- - use model/view for the treewidget
261
- - fix ocal file parsing
260
+ - return a suitable name (scheme_variant) ??
262
261
- re-organize and rename colorramp classes and widgets
263
262
*/
263
+
264
+ /*
265
+ TODO load schemes
266
+ - don't show empty dirs? (e.g. jjg/hatch)
267
+
268
+ - better grouping:
269
+ - cl/fs2????
270
+ - cw
271
+ - dca
272
+ - dirs with one scheme ( cl/ es/ ma/ occ/ wkp/
273
+ jjg/neo10/env/green-crystal jjg/neo10/face/eyes/blue )
274
+
275
+ - fix rendering:
276
+ - ibcao
277
+
278
+ */
264
279
QString QgsCptCityColorRampV2::mBaseDir ;
265
280
QStringList QgsCptCityColorRampV2::mCollections ;
266
281
QMap< QString, QStringList > QgsCptCityColorRampV2::mSchemeMap ;
@@ -389,7 +404,7 @@ QgsStringMap QgsCptCityColorRampV2::properties() const
389
404
390
405
QStringList QgsCptCityColorRampV2::listSchemeCollections ( QString collectionName, bool recursive )
391
406
{
392
- QDir dir = QDir ( QgsCptCityColorRampV2::getBaseDir () + " /" + collectionName );
407
+ QDir dir = QDir ( QgsCptCityColorRampV2::baseDir () + " /" + collectionName );
393
408
if ( ! dir.exists () )
394
409
return QStringList ();
395
410
@@ -417,7 +432,7 @@ QStringList QgsCptCityColorRampV2::listSchemeCollections( QString collectionName
417
432
418
433
QStringList QgsCptCityColorRampV2::listSchemeNames ( QString collectionName )
419
434
{
420
- QDir dir = QDir ( QgsCptCityColorRampV2::getBaseDir () + " /" + collectionName );
435
+ QDir dir = QDir ( QgsCptCityColorRampV2::baseDir () + " /" + collectionName );
421
436
if ( ! dir.exists () )
422
437
{
423
438
QgsDebugMsg ( " dir " + dir.dirName () + " does not exist" );
@@ -430,7 +445,7 @@ QStringList QgsCptCityColorRampV2::listSchemeNames( QString collectionName )
430
445
return entries;
431
446
}
432
447
433
- QString QgsCptCityColorRampV2::getBaseDir ()
448
+ QString QgsCptCityColorRampV2::baseDir ()
434
449
{
435
450
// if was set with setBaseDir, return that value
436
451
QString baseDir = mBaseDir ;
@@ -451,19 +466,137 @@ QString QgsCptCityColorRampV2::getBaseDir()
451
466
return baseDir;
452
467
}
453
468
454
- QString QgsCptCityColorRampV2::getFilename () const
469
+ QString QgsCptCityColorRampV2::fileName () const
455
470
{
456
471
if ( mSchemeName == " " )
457
472
return QString ();
458
473
else
459
- return QgsCptCityColorRampV2::getBaseDir () + " /" + mSchemeName + mVariantName + " .svg" ;
474
+ return QgsCptCityColorRampV2::baseDir () + " /" + mSchemeName + mVariantName + " .svg" ;
475
+ }
476
+
477
+ QString findFileName ( const QString & target, const QString & startDir, const QString & baseDir )
478
+ {
479
+ if ( startDir == " " || ! startDir.startsWith ( baseDir ) )
480
+ return QString ();
481
+
482
+ QDir dir = QDir ( startDir );
483
+ // todo test when
484
+ while ( ! dir.exists ( target ) && dir.path () != baseDir )
485
+ {
486
+ dir.cdUp ();
487
+ }
488
+ if ( ! dir.exists ( target ) )
489
+ return QString ();
490
+ else
491
+ return dir.path () + QDir::separator () + target;
492
+ }
493
+
494
+ QString QgsCptCityColorRampV2::copyingFileName () const
495
+ {
496
+ return findFileName ( " COPYING.xml" , QFileInfo ( fileName () ).dir ().path (), baseDir () );
497
+ }
498
+
499
+ QString QgsCptCityColorRampV2::descFileName () const
500
+ {
501
+ return findFileName ( " DESC.xml" , QFileInfo ( fileName () ).dir ().path (), baseDir () );
502
+ }
503
+
504
+ QMap< QString, QString > QgsCptCityColorRampV2::copyingInfo ()
505
+ {
506
+ QMap< QString, QString > copyingMap;
507
+
508
+ // import xml file
509
+ QString licenseFileName = copyingFileName ();
510
+ if ( licenseFileName.isNull () )
511
+ return copyingMap;
512
+ QFile f ( licenseFileName );
513
+ if ( !f.open ( QFile::ReadOnly ) )
514
+ {
515
+ QgsDebugMsg ( " Couldn't open xml file: " + licenseFileName );
516
+ return copyingMap;
517
+ }
518
+
519
+ // parse the document
520
+ QDomDocument doc ( " license" );
521
+ if ( !doc.setContent ( &f ) )
522
+ {
523
+ f.close ();
524
+ QgsDebugMsg ( " Couldn't parse xml file: " + licenseFileName );
525
+ return copyingMap;
526
+ }
527
+ f.close ();
528
+
529
+ // get root element
530
+ QDomElement docElem = doc.documentElement ();
531
+ if ( docElem.tagName () != " copying" )
532
+ {
533
+ QgsDebugMsg ( " Incorrect root tag: " + docElem.tagName () );
534
+ return copyingMap;
535
+ }
536
+
537
+ // load author information
538
+ QDomElement authorsElement = docElem.firstChildElement ( " authors" );
539
+ if ( authorsElement.isNull () )
540
+ {
541
+ QgsDebugMsg ( " authors tag missing" );
542
+ }
543
+ else
544
+ {
545
+ QDomElement e = authorsElement.firstChildElement ();
546
+ QStringList authors;
547
+ while ( ! e.isNull () )
548
+ {
549
+ if ( e.tagName () == " author" )
550
+ {
551
+ if ( ! e.firstChildElement ( " name" ).isNull () )
552
+ authors << e.firstChildElement ( " name" ).text ().simplified ();
553
+ // org???
554
+ }
555
+ e = e.nextSiblingElement ();
556
+ }
557
+ copyingMap[ " authors" ] = authors.join ( " , " );
558
+ }
559
+
560
+ // load license information
561
+ QDomElement licenseElement = docElem.firstChildElement ( " license" );
562
+ if ( licenseElement.isNull () )
563
+ {
564
+ QgsDebugMsg ( " license tag missing" );
565
+ }
566
+ else
567
+ {
568
+ QDomElement e = licenseElement.firstChildElement ( " informal" );
569
+ if ( ! e.isNull () )
570
+ copyingMap[ " license/informal" ] = e.text ().simplified ();
571
+ e = licenseElement.firstChildElement ( " year" );
572
+ if ( ! e.isNull () )
573
+ copyingMap[ " license/year" ] = e.text ().simplified ();
574
+ e = licenseElement.firstChildElement ( " text" );
575
+ if ( ! e.isNull () && e.attribute ( " href" ) != QString () )
576
+ copyingMap[ " license/url" ] = e.attribute ( " href" );
577
+ }
578
+
579
+ // load src information
580
+ QDomElement element = docElem.firstChildElement ( " src" );
581
+ if ( element.isNull () )
582
+ {
583
+ QgsDebugMsg ( " src tag missing" );
584
+ }
585
+ else
586
+ {
587
+ QDomElement e = element.firstChildElement ( " link" );
588
+ if ( ! e.isNull () && e.attribute ( " href" ) != QString () )
589
+ copyingMap[ " src/link" ] = e.attribute ( " href" );
590
+ }
591
+
592
+ return copyingMap;
460
593
}
461
594
462
595
bool QgsCptCityColorRampV2::loadFile ( QString filename )
463
596
{
464
597
if ( filename == " " )
465
598
{
466
- filename = getFilename ();
599
+ filename = fileName ();
467
600
if ( filename.isNull () )
468
601
return false ;
469
602
}
@@ -503,9 +636,15 @@ bool QgsCptCityColorRampV2::loadFile( QString filename )
503
636
return false ;
504
637
}
505
638
506
- // load color ramp
639
+ // load color ramp from first linearGradient node
507
640
QDomElement rampsElement = docElem.firstChildElement ( " linearGradient" );
508
641
if ( rampsElement.isNull () )
642
+ {
643
+ QDomNodeList nodeList = docElem.elementsByTagName ( " linearGradient" );
644
+ if ( ! nodeList.isEmpty () )
645
+ rampsElement = nodeList.at ( 0 ).toElement ();
646
+ }
647
+ if ( rampsElement.isNull () )
509
648
{
510
649
mErrorString = " linearGradient tag missing" ;
511
650
QgsDebugMsg ( mErrorString );
@@ -621,9 +760,12 @@ bool QgsCptCityColorRampV2::hasAllSchemes()
621
760
return true ;
622
761
}
623
762
624
- // currently this methos takes some time, so it must be explicitly requested
763
+ // currently this method takes some time, so it must be explicitly requested
625
764
bool QgsCptCityColorRampV2::loadSchemes ( QString rootDir, bool reset )
626
765
{
766
+ struct timeval tv1, tv2;
767
+ gettimeofday ( &tv1, 0 );
768
+
627
769
// TODO should keep the name of the previously loaded, or see if the first element is inside rootDir
628
770
if ( ! reset && ! mCollections .isEmpty () )
629
771
{
@@ -787,7 +929,8 @@ bool QgsCptCityColorRampV2::loadSchemes( QString rootDir, bool reset )
787
929
// populate mCollectionNames
788
930
foreach ( QString path, mCollections )
789
931
{
790
- QString filename = QgsCptCityColorRampV2::getBaseDir () + " /" + path + " /" + " DESC.xml" ;
932
+ // TODO parse DESC.xml and COPYING.xml here, and add to CptCityCollection member
933
+ QString filename = QgsCptCityColorRampV2::baseDir () + " /" + path + " /" + " DESC.xml" ;
791
934
QFile f ( filename );
792
935
if ( ! f.open ( QFile::ReadOnly ) )
793
936
{
@@ -844,6 +987,9 @@ bool QgsCptCityColorRampV2::loadSchemes( QString rootDir, bool reset )
844
987
mCollectionSelections [ viewName ] << curName;
845
988
}
846
989
990
+ gettimeofday ( &tv2, 0 );
991
+ QgsDebugMsg ( QString ( " done in %1.%2 seconds" ).arg ( tv2.tv_sec - tv1.tv_sec
992
+ ).arg (( double )( tv2.tv_usec - tv2.tv_usec ) / 1000000.0 ) );
847
993
return ( ! mCollections .isEmpty () );
848
994
}
849
995
0 commit comments