Skip to content

Commit 0f77821

Browse files
committed
rework collection handling, add builtin collection (just colorbrewer for now)
1 parent d576a43 commit 0f77821

File tree

7 files changed

+346
-304
lines changed

7 files changed

+346
-304
lines changed

src/core/symbology-ng/qgscolorrampv2data.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -470,6 +470,14 @@ static const char* cptCityNames [] =
470470

471471
/* Selections from the archive */
472472
/* format: "", <section>, <item_1>,...,<item_n>,"",... */
473+
static const char* cptCitySelectionsMin [] =
474+
{
475+
"",
476+
"cb",
477+
"cb/",
478+
NULL, NULL
479+
};
480+
473481
static const char* cptCitySelections [] =
474482
{
475483
"",

src/core/symbology-ng/qgscptcitybrowsermodel.cpp

Lines changed: 57 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
begin : August 2012
55
copyright : (C) 2009 by Martin Dobias
66
copyright : (C) 2011 Radim Blazek
7-
copyright : (C) 2012 by Etienne Tourigny
7+
copyright : (C) 2012 by Etienne Tourigny
88
email : etourigny.dev at gmail.com
99
***************************************************************************
1010
* *
@@ -36,10 +36,10 @@
3636
#include "qgsmimedatautils.h"
3737

3838

39-
QgsCptCityDataItem::QgsCptCityDataItem( QgsCptCityDataItem::Type type, QgsCptCityDataItem* parent,
39+
QgsCptCityDataItem::QgsCptCityDataItem( QgsCptCityDataItem::Type type, QgsCptCityDataItem* parent,
4040
QString name, QString path, QString info )
4141
// 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 ),
4343
mName( name ), mPath( path ), mInfo( info ), mValid( true )
4444
{
4545
}
@@ -234,16 +234,16 @@ bool QgsCptCityDataItem::equal( const QgsCptCityDataItem *other )
234234

235235
// ---------------------------------------------------------------------
236236

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 ),
241241
mRamp( path, variantName )
242242
{
243243
mPopulated = true;
244-
244+
245245
// TODO how to get iconSize from treeView?
246-
QSize iconSize( 100, 15 );
246+
QSize iconSize( 100, 15 );
247247

248248
// make preview from variant if exists
249249
QStringList variantList = mRamp.variantList();
@@ -301,17 +301,15 @@ bool QgsCptCityColorRampItem::equal( const QgsCptCityDataItem *other )
301301
}
302302
//const QgsCptCityColorRampItem *o = qobject_cast<const QgsCptCityColorRampItem *> ( other );
303303
const QgsCptCityColorRampItem *o = dynamic_cast<const QgsCptCityColorRampItem *>( other );
304-
return ( mPath == o->mPath && mName == o->mName &&
304+
return ( mPath == o->mPath && mName == o->mName &&
305305
ramp().variantName() == o->ramp().variantName() );
306306
}
307307

308308
// ---------------------------------------------------------------------
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+
{
315313

316314
}
317315

@@ -326,15 +324,14 @@ QgsCptCityCollectionItem::~QgsCptCityCollectionItem()
326324
}
327325

328326
//-----------------------------------------------------------------------
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 )
333330
{
334331
mType = Directory;
335-
mValid = QDir( QgsCptCityCollection::baseDir( mCollectionName ) + "/" + mPath ).exists();
332+
mValid = QDir( QgsCptCityCollection::defaultBaseDir() ).exists();
336333
if ( ! mValid )
337-
QgsDebugMsg( "created invalid dir item, path = " + QgsCptCityCollection::baseDir( mCollectionName ) + "/" + mPath );
334+
QgsDebugMsg( "created invalid dir item, path = " + QgsCptCityCollection::defaultBaseDir() + "/" + mPath );
338335
// populate();
339336
}
340337

@@ -344,22 +341,21 @@ QgsCptCityDirectoryItem::~QgsCptCityDirectoryItem()
344341

345342
QVector<QgsCptCityDataItem*> QgsCptCityDirectoryItem::createChildren( )
346343
{
347-
QgsCptCityCollection* collection = QgsCptCityCollection::collectionRegistry().value( mCollectionName );
344+
QgsCptCityCollection* collection = QgsCptCityCollection::defaultCollection();
348345
QgsCptCityDataItem* item = 0;
349346
QVector<QgsCptCityDataItem*> children;
350347

351-
if ( ! mValid )
348+
if ( ! mValid || ! collection )
352349
return children;
353350

354351
QgsDebugMsg( "name= " + mName + " path= " + mPath );
355352

356353
// add children dirs
357-
foreach ( QString childPath, collection->listSchemeCollections( mPath ) )
354+
foreach ( QString childPath, collection->listDirNames( mPath ) )
358355
{
359356
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 ) );
363359
if ( item->isValid() )
364360
children << item;
365361
else
@@ -392,10 +388,9 @@ bool QgsCptCityDirectoryItem::equal( const QgsCptCityDataItem *other )
392388

393389

394390
//-----------------------------------------------------------------------
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 )
399394
{
400395
mType = Category;
401396
}
@@ -406,22 +401,24 @@ QgsCptCityCategoryItem::~QgsCptCityCategoryItem()
406401

407402
QVector<QgsCptCityDataItem*> QgsCptCityCategoryItem::createChildren( )
408403
{
409-
QgsCptCityCollection* collection = QgsCptCityCollection::collectionRegistry().value( mCollectionName );
404+
QgsCptCityCollection* collection = QgsCptCityCollection::defaultCollection();
410405
QgsCptCityDataItem* item = 0;
411406
QVector<QgsCptCityDataItem*> children;
412-
407+
408+
if ( ! mValid || ! collection )
409+
return children;
410+
413411
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 ) )
417415
{
418416
QgsDebugMsg( "childPath = " + childPath + " name= " + QFileInfo( childPath ).baseName() );
419417
if ( childPath.endsWith( "/" ) )
420418
{
421419
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 ) );
425422
if ( item->isValid() )
426423
children << item;
427424
else
@@ -438,7 +435,7 @@ QVector<QgsCptCityDataItem*> QgsCptCityCategoryItem::createChildren( )
438435
}
439436

440437
QgsDebugMsg( QString( "path= %1 inserted %2 children" ).arg( mPath ).arg( children.count() ) );
441-
438+
442439
return children;
443440
}
444441

@@ -454,10 +451,15 @@ bool QgsCptCityCategoryItem::equal( const QgsCptCityDataItem *other )
454451

455452
//-----------------------------------------------------------------------
456453

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 )
459456
{
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 );
461463
addRootItems();
462464
}
463465

@@ -468,47 +470,34 @@ QgsCptCityBrowserModel::~QgsCptCityBrowserModel()
468470

469471
void QgsCptCityBrowserModel::addRootItems( )
470472
{
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-
483473
if ( mViewName == "authors" )
484474
{
485475
QgsCptCityDirectoryItem* item = 0;
486-
foreach ( QString path, collection->listSchemeCollections() )
476+
foreach ( QString path, mCollection->listDirNames() )
487477
{
488478
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 ) );
492481
if ( item->isValid() )
493482
mRootItems << item;
494-
else
483+
else
495484
delete item;
496485
}
497486
}
498487
else if ( mViewName == "selections" )
499488
{
500489
QgsCptCityCategoryItem* item = 0;
501-
QMapIterator< QString, QStringList> it( collection->collectionSelections() );
490+
QMapIterator< QString, QStringList> it( mCollection->selectionsMap() );
502491
while ( it.hasNext() )
503492
{
504493
it.next();
505494
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 );
509498
if ( item->isValid() )
510499
mRootItems << item;
511-
else
500+
else
512501
delete item;
513502
}
514503
}
@@ -567,7 +556,7 @@ QVariant QgsCptCityBrowserModel::data( const QModelIndex &index, int role ) cons
567556
{
568557
return item->icon();
569558
}
570-
else if ( role == Qt::FontRole &&
559+
else if ( role == Qt::FontRole &&
571560
( item->type() == QgsCptCityDataItem::Directory ||
572561
item->type() == QgsCptCityDataItem::Category ) )
573562
{
@@ -638,10 +627,6 @@ QModelIndex QgsCptCityBrowserModel::findPath( QString path )
638627

639628
QgsDebugMsg( "path = " + path );
640629

641-
QgsCptCityCollection* collection = QgsCptCityCollection::collectionRegistry().value( mCollectionName );
642-
if ( ! collection )
643-
return QModelIndex();
644-
645630
while ( foundChild )
646631
{
647632
foundChild = false; // assume that the next child item will not be found
@@ -666,7 +651,7 @@ QModelIndex QgsCptCityBrowserModel::findPath( QString path )
666651
if ( mViewName == "selections" )
667652
{
668653
itemPath = item->name();
669-
foreach ( QString childPath, collection->collectionSelections().value( item->path() ) )
654+
foreach ( QString childPath, mCollection->selectionsMap().value( item->path() ) )
670655
{
671656
if ( childPath == path )
672657
{

src/core/symbology-ng/qgscptcitybrowsermodel.h

Lines changed: 13 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
---------------------
44
begin : August 2012
55
copyright : (C) 2009 by Martin Dobias
6-
copyright : (C) 2012 by Etienne Tourigny
6+
copyright : (C) 2012 by Etienne Tourigny
77
email : etourigny.dev at gmail.com
88
***************************************************************************
99
* *
@@ -39,7 +39,7 @@ class CORE_EXPORT QgsCptCityDataItem : public QObject
3939
Category
4040
};
4141

42-
QgsCptCityDataItem( QgsCptCityDataItem::Type type, QgsCptCityDataItem* parent,
42+
QgsCptCityDataItem( QgsCptCityDataItem::Type type, QgsCptCityDataItem* parent,
4343
QString name, QString path, QString info );
4444
virtual ~QgsCptCityDataItem();
4545

@@ -82,7 +82,7 @@ class CORE_EXPORT QgsCptCityDataItem : public QObject
8282
// try to process the data dropped on this item
8383
virtual bool handleDrop( const QMimeData * /*data*/, Qt::DropAction /*action*/ ) { return false; }
8484

85-
// static methods
85+
// static methods
8686

8787
// Find child index in vector of items using '==' operator
8888
static int findItem( QVector<QgsCptCityDataItem*> items, QgsCptCityDataItem * item );
@@ -138,8 +138,8 @@ class CORE_EXPORT QgsCptCityColorRampItem : public QgsCptCityDataItem
138138
Q_OBJECT
139139
public:
140140

141-
QgsCptCityColorRampItem( QgsCptCityDataItem* parent,
142-
QString name, QString path, QString info = QString(),
141+
QgsCptCityColorRampItem( QgsCptCityDataItem* parent,
142+
QString name, QString path, QString info = QString(),
143143
QString variantName = QString() );
144144

145145
// --- reimplemented from QgsCptCityDataItem ---
@@ -161,27 +161,22 @@ class CORE_EXPORT QgsCptCityCollectionItem : public QgsCptCityDataItem
161161
{
162162
Q_OBJECT
163163
public:
164-
QgsCptCityCollectionItem( QgsCptCityDataItem* parent,
165-
QString name, QString path, QString info,
166-
QString collectionName = DEFAULT_CPTCITY_COLLECTION );
164+
QgsCptCityCollectionItem( QgsCptCityDataItem* parent,
165+
QString name, QString path, QString info );
167166
~QgsCptCityCollectionItem();
168167

169168
void setPopulated() { mPopulated = true; }
170169
void addChild( QgsCptCityDataItem *item ) { mChildren.append( item ); }
171170

172-
protected:
173-
QString mCollectionName;
174-
175171
};
176172

177173
/** A directory: contains subdirectories and color ramps */
178174
class CORE_EXPORT QgsCptCityDirectoryItem : public QgsCptCityCollectionItem
179175
{
180176
Q_OBJECT
181177
public:
182-
QgsCptCityDirectoryItem( QgsCptCityDataItem* parent,
183-
QString name, QString path, QString info = QString(),
184-
QString collectionName = DEFAULT_CPTCITY_COLLECTION );
178+
QgsCptCityDirectoryItem( QgsCptCityDataItem* parent,
179+
QString name, QString path, QString info = QString() );
185180
~QgsCptCityDirectoryItem();
186181

187182
QVector<QgsCptCityDataItem*> createChildren();
@@ -195,8 +190,7 @@ class CORE_EXPORT QgsCptCityCategoryItem : public QgsCptCityCollectionItem
195190
{
196191
Q_OBJECT
197192
public:
198-
QgsCptCityCategoryItem( QgsCptCityDataItem* parent, QString name, QString path, QString info,
199-
QString collectionName = DEFAULT_CPTCITY_COLLECTION );
193+
QgsCptCityCategoryItem( QgsCptCityDataItem* parent, QString name, QString path, QString info );
200194
~QgsCptCityCategoryItem();
201195

202196
QVector<QgsCptCityDataItem*> createChildren();
@@ -211,8 +205,8 @@ class CORE_EXPORT QgsCptCityBrowserModel : public QAbstractItemModel
211205
Q_OBJECT
212206

213207
public:
214-
explicit QgsCptCityBrowserModel( QObject *parent = 0,
215-
QString collectionName = DEFAULT_CPTCITY_COLLECTION,
208+
explicit QgsCptCityBrowserModel( QObject *parent = 0,
209+
QgsCptCityCollection* collection = QgsCptCityCollection::defaultCollection(),
216210
QString viewName = "authors" );
217211
~QgsCptCityBrowserModel();
218212

@@ -298,7 +292,7 @@ class CORE_EXPORT QgsCptCityBrowserModel : public QAbstractItemModel
298292
void removeRootItems();
299293

300294
QVector<QgsCptCityDataItem*> mRootItems;
301-
QString mCollectionName;
295+
QgsCptCityCollection* mCollection;
302296
QString mViewName;
303297
};
304298

0 commit comments

Comments
 (0)