18 #include <QApplication>
23 #include <QMouseEvent>
24 #include <QTreeWidget>
25 #include <QTreeWidgetItem>
35 #include "qgsconfig.h"
46 : mArchiveName( archiveName ), mBaseDir( baseDir )
48 QgsDebugMsg(
"archiveName = " + archiveName +
" baseDir = " + baseDir );
52 foreach ( QString path, QDir(
mBaseDir ).entryList( QDir::Dirs | QDir::NoDotAndDotDot, QDir::Name ) )
54 if ( path ==
"selections" )
66 QDir seldir(
mBaseDir +
"/" +
"selections" );
67 QgsDebugMsg(
"populating selection from " + seldir.path() );
68 foreach ( QString selfile, seldir.entryList( QStringList(
"*.xml" ), QDir::Files ) )
70 QgsDebugMsg(
"file= " + seldir.path() +
"/" + selfile );
72 seldir.dirName() +
"/" + selfile );
74 if ( selItem->isValid() )
113 if ( archiveName.isNull() )
127 baseDir = settings.value(
"CptCity/baseDir",
140 if ( startDir ==
"" || ! startDir.startsWith( baseDir ) )
143 QDir dir = QDir( startDir );
145 while ( ! dir.exists( target ) && dir.path() !=
baseDir )
150 if ( ! dir.exists( target ) )
153 return dir.path() +
"/" + target;
173 if ( fileName.isNull() )
178 QgsDebugMsg(
"found copying info in copyingInfoMap, file = " + fileName );
186 if ( !f.open( QFile::ReadOnly ) )
188 QgsDebugMsg(
"Couldn't open xml file: " + fileName );
193 QDomDocument doc(
"license" );
194 if ( !doc.setContent( &f ) )
197 QgsDebugMsg(
"Couldn't parse xml file: " + fileName );
203 QDomElement docElem = doc.documentElement();
204 if ( docElem.tagName() !=
"copying" )
206 QgsDebugMsg(
"Incorrect root tag: " + docElem.tagName() );
211 QDomElement authorsElement = docElem.firstChildElement(
"authors" );
212 if ( authorsElement.isNull() )
218 QDomElement e = authorsElement.firstChildElement();
220 while ( ! e.isNull() )
222 if ( e.tagName() ==
"author" )
224 if ( ! e.firstChildElement(
"name" ).isNull() )
225 authors << e.firstChildElement(
"name" ).text().simplified();
228 e = e.nextSiblingElement();
230 copyingMap[
"authors" ] = authors.join(
", " );
234 QDomElement licenseElement = docElem.firstChildElement(
"license" );
235 if ( licenseElement.isNull() )
241 QDomElement e = licenseElement.firstChildElement(
"informal" );
243 copyingMap[
"license/informal" ] = e.text().simplified();
244 e = licenseElement.firstChildElement(
"year" );
246 copyingMap[
"license/year" ] = e.text().simplified();
247 e = licenseElement.firstChildElement(
"text" );
248 if ( ! e.isNull() && e.attribute(
"href" ) != QString() )
249 copyingMap[
"license/url" ] = e.attribute(
"href" );
253 QDomElement element = docElem.firstChildElement(
"src" );
254 if ( element.isNull() )
260 QDomElement e = element.firstChildElement(
"link" );
261 if ( ! e.isNull() && e.attribute(
"href" ) != QString() )
262 copyingMap[
"src/link" ] = e.attribute(
"href" );
274 QgsDebugMsg(
"description fileName = " + fileName );
277 if ( ! f.open( QFile::ReadOnly ) )
279 QgsDebugMsg(
"description file " + fileName +
" ] does not exist" );
285 QDomDocument doc(
"description" );
286 if ( !doc.setContent( &f, &errMsg ) )
289 QgsDebugMsg(
"Couldn't parse file " + fileName +
" : " + errMsg );
295 QDomElement docElem = doc.documentElement();
296 if ( docElem.tagName() !=
"description" )
298 QgsDebugMsg(
"Incorrect root tag: " + docElem.tagName() );
303 QDomElement e = docElem.firstChildElement(
"name" );
308 descMap[
"name" ] = e.text().simplified();
309 e = docElem.firstChildElement(
"full" );
314 descMap[
"full" ] = e.text().simplified();
321 QMap< double, QPair<QColor, QColor> > colorMap;
325 if ( !f.open( QFile::ReadOnly ) )
327 QgsDebugMsg(
"Couldn't open SVG file: " + fileName );
332 QDomDocument doc(
"gradient" );
333 if ( !doc.setContent( &f ) )
336 QgsDebugMsg(
"Couldn't parse SVG file: " + fileName );
341 QDomElement docElem = doc.documentElement();
343 if ( docElem.tagName() !=
"svg" )
345 QgsDebugMsg(
"Incorrect root tag: " + docElem.tagName() );
350 QDomElement rampsElement = docElem.firstChildElement(
"linearGradient" );
351 if ( rampsElement.isNull() )
353 QDomNodeList nodeList = docElem.elementsByTagName(
"linearGradient" );
354 if ( ! nodeList.isEmpty() )
355 rampsElement = nodeList.at( 0 ).toElement();
357 if ( rampsElement.isNull() )
364 QDomElement e = rampsElement.firstChildElement();
366 while ( !e.isNull() )
368 if ( e.tagName() ==
"stop" )
372 QString offsetStr = e.attribute(
"offset" );
373 QString colorStr = e.attribute(
"stop-color",
"" );
374 QString opacityStr = e.attribute(
"stop-opacity",
"1.0" );
375 if ( offsetStr.endsWith(
"%" ) )
376 offset = offsetStr.remove( offsetStr.size() - 1, 1 ).toDouble() / 100.0;
378 offset = offsetStr.toDouble();
382 if ( color != QColor() )
384 int alpha = opacityStr.toDouble() * 255;
385 color.setAlpha( alpha );
386 if ( colorMap.contains( offset ) )
387 colorMap[offset].second = color;
389 colorMap[offset] = qMakePair( color, color );
393 QgsDebugMsg( QString(
"at offset=%1 invalid color" ).arg( offset ) );
401 e = e.nextSiblingElement();
425 QgsDebugMsg(
"archiveName = " + archiveName +
" archiveBaseDir = " + archiveBaseDir );
436 QString
baseDir = settings.value(
"CptCity/baseDir",
442 initArchive( defArchiveName, baseDir +
"/" + defArchiveName );
448 QString
baseDir, defArchiveName;
452 baseDir = settings.value(
"CptCity/baseDir",
457 QgsDebugMsg(
"baseDir= " + baseDir +
" defArchiveName= " + defArchiveName );
461 foreach ( QString entry, dir.entryList( QStringList(
"cpt-city*" ), QDir::Dirs ) )
463 if ( QFile::exists( baseDir +
"/" + entry +
"/VERSION.xml" ) )
464 archivesMap[ entry ] = baseDir +
"/" + entry;
469 archivesMap[ defArchiveName ] = baseDir +
"/" + defArchiveName;
472 for ( QgsStringMap::iterator it = archivesMap.begin();
473 it != archivesMap.end(); ++it )
475 if ( QDir( it.value() ).exists() )
479 QgsDebugMsg( QString(
"not loading archive [%1] because dir %2 does not exist " ).arg( it.key() ).arg( it.value() ) );
487 for ( QMap< QString, QgsCptCityArchive* >::iterator it =
mArchiveRegistry.begin();
497 QString name, QString path )
499 : QObject(), mType( type ), mParent( parent ), mPopulated( false ),
500 mName( name ), mPath( path ), mValid( true )
528 QVector<QgsCptCityDataItem*>
children;
539 QApplication::setOverrideCursor( Qt::WaitCursor );
549 QApplication::restoreOverrideCursor();
653 for (
int i = 0; i < items.size(); i++ )
656 if ( items[i]->
equal( item ) )
666 QApplication::setOverrideCursor( Qt::WaitCursor );
671 QVector<QgsCptCityDataItem*>
remove;
674 if (
findItem( items, child ) >= 0 )
676 remove.append( child );
695 QApplication::restoreOverrideCursor();
700 if ( metaObject()->className() == other->metaObject()->className() &&
711 QString name, QString path, QString variantName,
bool initialize )
713 mInitialised( false ), mRamp( path, variantName, false )
722 QString name, QString path, QStringList variantList,
bool initialize )
724 mInitialised( false ), mRamp( path, variantList, QString(), false )
757 if ( variantList.isEmpty() )
762 mInfo = QString::number( count ) +
" " +
tr(
"colors" ) +
" - ";
770 mInfo +=
tr(
"continuous (multi)" );
776 mInfo = QString::number( variantList.count() ) +
" " +
tr(
"variants" );
805 return icon( QSize( 100, 15 ) );
812 if ( icon.availableSizes().contains( size ) )
826 QPixmap blankPixmap( size );
827 blankPixmap.fill( Qt::white );
828 icon = QIcon( blankPixmap );
838 QString name, QString path )
855 QVector< QgsCptCityDataItem* > rampItems;
856 QVector< QgsCptCityDataItem* > deleteItems;
865 QgsDebugMsgLevel( QString(
"child path= %1 coll= %2 ramp = %3" ).arg( childItem->
path() ).arg( collectionItem != 0 ).arg( rampItem != 0 ), 2 );
866 if ( collectionItem && recursive )
876 rampItems << rampItem;
878 deleteItems << rampItem;
889 QgsDebugMsg( QString(
"item %1 is invalid, will be deleted" ).arg( deleteItem->
path() ) );
901 QString name, QString path )
915 mPath +
"/" +
"DESC.xml";
917 if ( descMap.contains(
"name" ) )
918 mInfo = descMap.value(
"name" );
930 return QVector<QgsCptCityDataItem*>();
932 QVector<QgsCptCityDataItem*>
children;
935 QMapIterator< QString, QStringList> it(
rampsMap() );
936 while ( it.hasNext() )
954 children << childItem;
957 QgsDebugMsg( QString(
"name= %1 path= %2 found %3 children" ).arg(
mName ).arg(
mPath ).arg( children.count() ) );
967 QString curName, prevName, prevPath, curVariant, curSep, schemeName;
968 QStringList listVariant;
969 QStringList schemeNamesAll, schemeNames;
970 bool prevAdd, curAdd;
973 schemeNamesAll = dir.entryList( QStringList(
"*.svg" ), QDir::Files, QDir::Name );
976 for (
int i = 0; i < schemeNamesAll.count(); i++ )
979 schemeName = schemeNamesAll[i];
980 schemeName.chop( 4 );
983 curName = schemeName;
988 if ( schemeName.length() > 1 && schemeName.endsWith(
"a" ) && ! listVariant.isEmpty() &&
989 (( prevName + listVariant.last() +
"a" ) == curName ) )
992 curVariant = listVariant.last() +
"a";
996 QRegExp rxVariant(
"^(.*[^\\d])(\\d{1,3})$" );
997 int pos = rxVariant.indexIn( schemeName );
1000 curName = rxVariant.cap( 1 );
1001 curVariant = rxVariant.cap( 2 );
1005 curSep = curName.right( 1 );
1006 if ( curSep ==
"-" || curSep ==
"_" )
1009 curVariant = curSep + curVariant;
1012 if ( prevName ==
"" )
1018 if ( curName ==
"" )
1019 curName =
"__empty__";
1021 if ( curName == prevName )
1024 if ( i == schemeNamesAll.count() - 1 )
1026 listVariant << curVariant;
1030 if ( prevName !=
"" )
1035 if ( i == schemeNamesAll.count() - 1 )
1044 if ( listVariant.count() == 0 )
1048 schemeNames << prevName;
1051 else if ( listVariant.count() <= 3 )
1054 for (
int j = 0; j < listVariant.count(); j++ )
1057 schemeNames << prevName + listVariant[j];
1058 mRampsMap[
mPath +
"/" + prevName + listVariant[j] ] = QStringList();
1065 schemeNames << prevName;
1067 listVariant.clear();
1071 if ( curVariant !=
"" )
1072 curName += curVariant;
1073 schemeNames << curName;
1077 if ( prevAdd || curAdd )
1080 if ( curVariant !=
"" )
1081 listVariant << curVariant;
1098 "/" +
mPath ).entryList( QDir::Dirs | QDir::NoDotAndDotDot, QDir::Name );
1108 return (
path() == other->
path() );
1112 QString name, QString path )
1114 QgsDebugMsg(
"name= " + name +
" path= " + path );
1118 if ( dirItem && ! dirItem->
isValid() )
1127 QStringList theDirEntries = dirItem->
dirEntries();
1128 QMap< QString, QStringList > theRampsMap = dirItem->
rampsMap();
1130 QgsDebugMsg( QString(
"item has %1 dirs and %2 ramps" ).arg( theDirEntries.count() ).arg( theRampsMap.count() ) );
1133 if ( theDirEntries.count() > 0 )
1137 if ( theRampsMap.count() == 0 )
1144 else if ( theRampsMap.count() == 1 )
1149 theRampsMap.begin().key(), theRampsMap.begin().value() );
1163 QString name, QString path )
1167 mValid = ! path.isNull();
1179 return QVector<QgsCptCityDataItem*>();
1182 QVector<QgsCptCityDataItem*>
children;
1189 QgsDebugMsg(
"childPath = " + childPath +
" name= " + QFileInfo( childPath ).baseName() );
1190 if ( childPath.endsWith(
"/" ) )
1192 childPath.chop( 1 );
1198 children << childItem;
1207 if ( item->isValid() )
1214 QgsDebugMsg( QString(
"path= %1 inserted %2 children" ).arg(
mPath ).arg( children.count() ) );
1225 QFile f( filename );
1226 if ( ! f.open( QFile::ReadOnly ) )
1234 QDomDocument doc(
"selection" );
1235 if ( !doc.setContent( &f, &errMsg ) )
1238 QgsDebugMsg(
"Couldn't parse file " + filename +
" : " + errMsg );
1244 QDomElement docElem = doc.documentElement();
1245 if ( docElem.tagName() !=
"selection" )
1247 QgsDebugMsg(
"Incorrect root tag: " + docElem.tagName() );
1250 QDomElement e = docElem.firstChildElement(
"name" );
1251 if ( ! e.isNull() && ! e.text().isNull() )
1253 mInfo = docElem.firstChildElement(
"synopsis" ).text().simplified();
1256 QDomElement collectsElem = docElem.firstChildElement(
"seealsocollects" );
1257 e = collectsElem.firstChildElement(
"collect" );
1258 while ( ! e.isNull() )
1260 if ( ! e.attribute(
"dir" ).isNull() )
1265 e = e.nextSiblingElement();
1268 QDomElement gradientsElem = docElem.firstChildElement(
"gradients" );
1269 e = gradientsElem.firstChildElement(
"gradient" );
1270 while ( ! e.isNull() )
1272 if ( ! e.attribute(
"dir" ).isNull() )
1276 mSelectionsList << e.attribute(
"dir" ) +
"/" + e.attribute(
"file" );
1278 e = e.nextSiblingElement();
1289 return (
path() == other->
path() );
1294 QString name, QVector<QgsCptCityDataItem*> items )
1309 return QVector<QgsCptCityDataItem*>();
1311 QVector<QgsCptCityDataItem*>
children;
1328 : QAbstractItemModel( parent ), mArchive( archive ), mViewType( viewType )
1368 if ( !index.isValid() )
1371 Qt::ItemFlags
flags = Qt::ItemIsEnabled | Qt::ItemIsSelectable;
1378 if ( !index.isValid() )
1387 else if ( role == Qt::DisplayRole )
1389 if ( index.column() == 0 )
1390 return item->
name();
1391 if ( index.column() == 1 )
1393 return item->
info();
1396 else if ( role == Qt::ToolTipRole )
1400 return item->
path() +
"\n" + item->
info();
1403 else if ( role == Qt::DecorationRole && index.column() == 1 &&
1409 else if ( role == Qt::FontRole &&
1410 ( dynamic_cast< QgsCptCityCollectionItem* >( item ) != 0 ) )
1414 font.setPointSize( 11 );
1415 font.setBold(
true );
1428 Q_UNUSED( section );
1429 if ( orientation == Qt::Horizontal && role == Qt::DisplayRole )
1432 return QVariant(
tr(
"Name" ) );
1433 else if ( section == 1 )
1434 return QVariant(
tr(
"Info" ) );
1443 if ( !parent.isValid() )
1452 return item ? item->
rowCount() : 0;
1458 if ( !parent.isValid() )
1474 QModelIndex theIndex;
1475 bool foundParent =
false, foundChild =
true;
1481 if ( path.isEmpty() )
1483 for (
int i = 0; i <
rowCount( theIndex ); i++ )
1485 QModelIndex idx =
index( i, 0, theIndex );
1488 return QModelIndex();
1490 itemPath = item->
path();
1492 if ( itemPath == path )
1500 while ( foundChild )
1506 if ( itemPath.isEmpty() )
1508 for ( ; i <
rowCount( theIndex ); i++ )
1510 QModelIndex idx =
index( i, 0, theIndex );
1513 return QModelIndex();
1515 itemPath = item->
path();
1517 if ( itemPath == path )
1523 if ( ! itemPath.endsWith(
"/" ) )
1526 foundParent =
false;
1538 if ( childPath.endsWith(
"/" ) )
1539 childPath.chop( 1 );
1541 if ( path.startsWith( childPath ) )
1550 else if ( path.startsWith( itemPath ) )
1568 return QModelIndex();
1582 QModelIndex idx =
findPath( path );
1583 if ( idx.isValid() )
1596 return item ? createIndex( row, column, item ) : QModelIndex();
1603 return QModelIndex();
1610 const QVector<QgsCptCityDataItem*> &items = parent ? parent->
children() :
mRootItems;
1612 for (
int i = 0; i < items.size(); i++ )
1614 if ( items[i] == item )
1615 return createIndex( i, 0, item );
1617 QModelIndex childIndex =
findItem( item, items[i] );
1618 if ( childIndex.isValid() )
1622 return QModelIndex();
1639 QModelIndex idx =
findItem( parent );
1640 if ( !idx.isValid() )
1643 beginInsertRows( idx, first, last );
1654 QModelIndex idx =
findItem( parent );
1655 if ( !idx.isValid() )
1657 beginRemoveRows( idx, first, last );
1706 QStringList QgsCptCityBrowserModel::mimeTypes()
const
1711 types <<
"application/x-vnd.qgis.qgis.uri";
1715 QMimeData * QgsCptCityBrowserModel::mimeData(
const QModelIndexList &indexes )
const
1718 foreach (
const QModelIndex &
index, indexes )
1720 if ( index.isValid() )
1723 if ( ptr->
type() != QgsCptCityDataItem::Layer )
continue;
1731 bool QgsCptCityBrowserModel::dropMimeData(
const QMimeData * data, Qt::DropAction action,
int row,
int column,
const QModelIndex & parent )
1749 void *v = idx.internalPointer();
1751 Q_ASSERT( !v || d );