4
4
begin : August 2012
5
5
copyright : (C) 2009 by Martin Dobias
6
6
copyright : (C) 2011 Radim Blazek
7
- copyright : (C) 2012 by Etienne Tourigny
7
+ copyright : (C) 2012 by Etienne Tourigny
8
8
email : etourigny.dev at gmail.com
9
9
***************************************************************************
10
10
* *
36
36
#include " qgsmimedatautils.h"
37
37
38
38
39
- QgsCptCityDataItem::QgsCptCityDataItem ( QgsCptCityDataItem::Type type, QgsCptCityDataItem* parent,
39
+ QgsCptCityDataItem::QgsCptCityDataItem ( QgsCptCityDataItem::Type type, QgsCptCityDataItem* parent,
40
40
QString name, QString path, QString info )
41
41
// Do not pass parent to QObject, Qt would delete this when parent is deleted
42
- : QObject(), mType( type ), mParent( parent ), mPopulated( false ),
42
+ : QObject(), mType( type ), mParent( parent ), mPopulated( false ),
43
43
mName( name ), mPath( path ), mInfo( info ), mValid( true )
44
44
{
45
45
}
@@ -234,16 +234,16 @@ bool QgsCptCityDataItem::equal( const QgsCptCityDataItem *other )
234
234
235
235
// ---------------------------------------------------------------------
236
236
237
- QgsCptCityColorRampItem::QgsCptCityColorRampItem ( QgsCptCityDataItem* parent,
238
- QString name, QString path,
239
- QString info, QString variantName )
240
- : QgsCptCityDataItem( ColorRamp, parent, name, path, info ),
237
+ QgsCptCityColorRampItem::QgsCptCityColorRampItem ( QgsCptCityDataItem* parent,
238
+ QString name, QString path,
239
+ QString info, QString variantName )
240
+ : QgsCptCityDataItem( ColorRamp, parent, name, path, info ),
241
241
mRamp( path, variantName )
242
242
{
243
243
mPopulated = true ;
244
-
244
+
245
245
// TODO how to get iconSize from treeView?
246
- QSize iconSize ( 100 , 15 );
246
+ QSize iconSize ( 100 , 15 );
247
247
248
248
// make preview from variant if exists
249
249
QStringList variantList = mRamp .variantList ();
@@ -301,17 +301,15 @@ bool QgsCptCityColorRampItem::equal( const QgsCptCityDataItem *other )
301
301
}
302
302
// const QgsCptCityColorRampItem *o = qobject_cast<const QgsCptCityColorRampItem *> ( other );
303
303
const QgsCptCityColorRampItem *o = dynamic_cast <const QgsCptCityColorRampItem *>( other );
304
- return ( mPath == o->mPath && mName == o->mName &&
304
+ return ( mPath == o->mPath && mName == o->mName &&
305
305
ramp ().variantName () == o->ramp ().variantName () );
306
306
}
307
307
308
308
// ---------------------------------------------------------------------
309
- QgsCptCityCollectionItem::QgsCptCityCollectionItem ( QgsCptCityDataItem* parent,
310
- QString name, QString path,
311
- QString info, QString collectionName )
312
- : QgsCptCityDataItem( Collection, parent, name, path, info ),
313
- mCollectionName( collectionName )
314
- {
309
+ QgsCptCityCollectionItem::QgsCptCityCollectionItem ( QgsCptCityDataItem* parent,
310
+ QString name, QString path, QString info )
311
+ : QgsCptCityDataItem( Collection, parent, name, path, info )
312
+ {
315
313
316
314
}
317
315
@@ -326,15 +324,14 @@ QgsCptCityCollectionItem::~QgsCptCityCollectionItem()
326
324
}
327
325
328
326
// -----------------------------------------------------------------------
329
- QgsCptCityDirectoryItem::QgsCptCityDirectoryItem ( QgsCptCityDataItem* parent,
330
- QString name, QString path,
331
- QString info, QString collectionName )
332
- : QgsCptCityCollectionItem( parent, name, path, info, collectionName )
327
+ QgsCptCityDirectoryItem::QgsCptCityDirectoryItem ( QgsCptCityDataItem* parent,
328
+ QString name, QString path, QString info )
329
+ : QgsCptCityCollectionItem( parent, name, path, info )
333
330
{
334
331
mType = Directory;
335
- mValid = QDir ( QgsCptCityCollection::baseDir ( mCollectionName ) + " / " + mPath ).exists ();
332
+ mValid = QDir ( QgsCptCityCollection::defaultBaseDir () ).exists ();
336
333
if ( ! mValid )
337
- QgsDebugMsg ( " created invalid dir item, path = " + QgsCptCityCollection::baseDir ( mCollectionName ) + " /" + mPath );
334
+ QgsDebugMsg ( " created invalid dir item, path = " + QgsCptCityCollection::defaultBaseDir ( ) + " /" + mPath );
338
335
// populate();
339
336
}
340
337
@@ -344,22 +341,21 @@ QgsCptCityDirectoryItem::~QgsCptCityDirectoryItem()
344
341
345
342
QVector<QgsCptCityDataItem*> QgsCptCityDirectoryItem::createChildren ( )
346
343
{
347
- QgsCptCityCollection* collection = QgsCptCityCollection::collectionRegistry (). value ( mCollectionName );
344
+ QgsCptCityCollection* collection = QgsCptCityCollection::defaultCollection ( );
348
345
QgsCptCityDataItem* item = 0 ;
349
346
QVector<QgsCptCityDataItem*> children;
350
347
351
- if ( ! mValid )
348
+ if ( ! mValid || ! collection )
352
349
return children;
353
350
354
351
QgsDebugMsg ( " name= " + mName + " path= " + mPath );
355
352
356
353
// add children dirs
357
- foreach ( QString childPath, collection->listSchemeCollections ( mPath ) )
354
+ foreach ( QString childPath, collection->listDirNames ( mPath ) )
358
355
{
359
356
QgsDebugMsg ( " childPath = " + childPath + " name= " + QFileInfo ( childPath ).baseName () );
360
- item = new QgsCptCityDirectoryItem ( this , QFileInfo ( childPath ).baseName (), childPath,
361
- collection->collectionNames ().value ( childPath ),
362
- mCollectionName );
357
+ item = new QgsCptCityDirectoryItem ( this , QFileInfo ( childPath ).baseName (), childPath,
358
+ collection->dirNamesMap ().value ( childPath ) );
363
359
if ( item->isValid () )
364
360
children << item;
365
361
else
@@ -392,10 +388,9 @@ bool QgsCptCityDirectoryItem::equal( const QgsCptCityDataItem *other )
392
388
393
389
394
390
// -----------------------------------------------------------------------
395
- QgsCptCityCategoryItem::QgsCptCityCategoryItem ( QgsCptCityDataItem* parent,
396
- QString name, QString path,
397
- QString info, QString collectionName )
398
- : QgsCptCityCollectionItem( parent, name, path, info, collectionName )
391
+ QgsCptCityCategoryItem::QgsCptCityCategoryItem ( QgsCptCityDataItem* parent,
392
+ QString name, QString path, QString info )
393
+ : QgsCptCityCollectionItem( parent, name, path, info )
399
394
{
400
395
mType = Category;
401
396
}
@@ -406,22 +401,24 @@ QgsCptCityCategoryItem::~QgsCptCityCategoryItem()
406
401
407
402
QVector<QgsCptCityDataItem*> QgsCptCityCategoryItem::createChildren ( )
408
403
{
409
- QgsCptCityCollection* collection = QgsCptCityCollection::collectionRegistry (). value ( mCollectionName );
404
+ QgsCptCityCollection* collection = QgsCptCityCollection::defaultCollection ( );
410
405
QgsCptCityDataItem* item = 0 ;
411
406
QVector<QgsCptCityDataItem*> children;
412
-
407
+
408
+ if ( ! mValid || ! collection )
409
+ return children;
410
+
413
411
QgsDebugMsg ( " name= " + mName + " path= " + mPath );
414
-
415
- // add children collections
416
- foreach ( QString childPath, collection->collectionSelections ().value ( mPath ) )
412
+
413
+ // add children collections
414
+ foreach ( QString childPath, collection->selectionsMap ().value ( mPath ) )
417
415
{
418
416
QgsDebugMsg ( " childPath = " + childPath + " name= " + QFileInfo ( childPath ).baseName () );
419
417
if ( childPath.endsWith ( " /" ) )
420
418
{
421
419
childPath.chop ( 1 );
422
- item = new QgsCptCityDirectoryItem ( this , childPath, childPath,
423
- collection->collectionNames ().value ( childPath ),
424
- mCollectionName );
420
+ item = new QgsCptCityDirectoryItem ( this , childPath, childPath,
421
+ collection->dirNamesMap ().value ( childPath ) );
425
422
if ( item->isValid () )
426
423
children << item;
427
424
else
@@ -438,7 +435,7 @@ QVector<QgsCptCityDataItem*> QgsCptCityCategoryItem::createChildren( )
438
435
}
439
436
440
437
QgsDebugMsg ( QString ( " path= %1 inserted %2 children" ).arg ( mPath ).arg ( children.count () ) );
441
-
438
+
442
439
return children;
443
440
}
444
441
@@ -454,10 +451,15 @@ bool QgsCptCityCategoryItem::equal( const QgsCptCityDataItem *other )
454
451
455
452
// -----------------------------------------------------------------------
456
453
457
- QgsCptCityBrowserModel::QgsCptCityBrowserModel ( QObject *parent, QString collectionName , QString viewName )
458
- : QAbstractItemModel( parent ), mCollectionName( collectionName ), mViewName( viewName )
454
+ QgsCptCityBrowserModel::QgsCptCityBrowserModel ( QObject *parent, QgsCptCityCollection* collection , QString viewName )
455
+ : QAbstractItemModel( parent ), mCollection( collection ), mViewName( viewName )
459
456
{
460
- QgsDebugMsg (" collectionName = " +collectionName+" viewName=" +viewName);
457
+ if ( mCollection == NULL )
458
+ {
459
+ QgsDebugMsg ( " Error: collection invalid" );
460
+ Q_ASSERT ( mCollection != NULL );
461
+ }
462
+ QgsDebugMsg ( " collectionName = " + collection->collectionName () + " viewName=" + viewName );
461
463
addRootItems ();
462
464
}
463
465
@@ -468,47 +470,34 @@ QgsCptCityBrowserModel::~QgsCptCityBrowserModel()
468
470
469
471
void QgsCptCityBrowserModel::addRootItems ( )
470
472
{
471
- QgsDebugMsg ( " mCollectionName = " + mCollectionName + " mViewName = " + mViewName );
472
-
473
- QgsCptCityCollection* collection =
474
- QgsCptCityCollection::collectionRegistry ().value ( mCollectionName );
475
-
476
- if ( ! collection )
477
- {
478
- QgsDebugMsg ( " Error: collection " + mCollectionName +
479
- " not found in the cpt-city collection registry" );
480
- return ;
481
- }
482
-
483
473
if ( mViewName == " authors" )
484
474
{
485
475
QgsCptCityDirectoryItem* item = 0 ;
486
- foreach ( QString path, collection-> listSchemeCollections () )
476
+ foreach ( QString path, mCollection -> listDirNames () )
487
477
{
488
478
QgsDebugMsg ( " path= " + path );
489
- item = new QgsCptCityDirectoryItem ( NULL , QFileInfo ( path ).baseName (), path,
490
- collection->collectionNames ().value ( path ),
491
- mCollectionName );
479
+ item = new QgsCptCityDirectoryItem ( NULL , QFileInfo ( path ).baseName (), path,
480
+ mCollection ->dirNamesMap ().value ( path ) );
492
481
if ( item->isValid () )
493
482
mRootItems << item;
494
- else
483
+ else
495
484
delete item;
496
485
}
497
486
}
498
487
else if ( mViewName == " selections" )
499
488
{
500
489
QgsCptCityCategoryItem* item = 0 ;
501
- QMapIterator< QString, QStringList> it ( collection-> collectionSelections () );
490
+ QMapIterator< QString, QStringList> it ( mCollection -> selectionsMap () );
502
491
while ( it.hasNext () )
503
492
{
504
493
it.next ();
505
494
QString path = it.key ();
506
- QString info = collection-> collectionNames ().value ( path );
507
- QgsDebugMsg ( " path= " + path + " info= " + info);
508
- item = new QgsCptCityCategoryItem ( NULL , path, path, info, mCollectionName );
495
+ QString info = mCollection -> dirNamesMap ().value ( path );
496
+ QgsDebugMsg ( " path= " + path + " info= " + info );
497
+ item = new QgsCptCityCategoryItem ( NULL , path, path, info );
509
498
if ( item->isValid () )
510
499
mRootItems << item;
511
- else
500
+ else
512
501
delete item;
513
502
}
514
503
}
@@ -567,7 +556,7 @@ QVariant QgsCptCityBrowserModel::data( const QModelIndex &index, int role ) cons
567
556
{
568
557
return item->icon ();
569
558
}
570
- else if ( role == Qt::FontRole &&
559
+ else if ( role == Qt::FontRole &&
571
560
( item->type () == QgsCptCityDataItem::Directory ||
572
561
item->type () == QgsCptCityDataItem::Category ) )
573
562
{
@@ -638,10 +627,6 @@ QModelIndex QgsCptCityBrowserModel::findPath( QString path )
638
627
639
628
QgsDebugMsg ( " path = " + path );
640
629
641
- QgsCptCityCollection* collection = QgsCptCityCollection::collectionRegistry ().value ( mCollectionName );
642
- if ( ! collection )
643
- return QModelIndex ();
644
-
645
630
while ( foundChild )
646
631
{
647
632
foundChild = false ; // assume that the next child item will not be found
@@ -666,7 +651,7 @@ QModelIndex QgsCptCityBrowserModel::findPath( QString path )
666
651
if ( mViewName == " selections" )
667
652
{
668
653
itemPath = item->name ();
669
- foreach ( QString childPath, collection-> collectionSelections ().value ( item->path () ) )
654
+ foreach ( QString childPath, mCollection -> selectionsMap ().value ( item->path () ) )
670
655
{
671
656
if ( childPath == path )
672
657
{
0 commit comments