|
QGIS API Documentation
master-6227475
|
00001 /*************************************************************************** 00002 qgscptcityarchive.h 00003 --------------------- 00004 begin : August 2012 00005 copyright : (C) 2009 by Martin Dobias 00006 copyright : (C) 2012 by Etienne Tourigny 00007 email : etourigny.dev at gmail.com 00008 *************************************************************************** 00009 * * 00010 * This program is free software; you can redistribute it and/or modify * 00011 * it under the terms of the GNU General Public License as published by * 00012 * the Free Software Foundation; either version 2 of the License, or * 00013 * (at your option) any later version. * 00014 * * 00015 ***************************************************************************/ 00016 00017 #ifndef QGSCPTCITYARCHIVE_H 00018 #define QGSCPTCITYARCHIVE_H 00019 00020 #include "qgsvectorcolorrampv2.h" 00021 00022 #include <QAbstractItemModel> 00023 #include <QIcon> 00024 #include <QMimeData> 00025 #include <QAction> 00026 00027 class QgsCptCityColorRampV2; 00028 class QgsCptCityDataItem; 00029 class QgsCptCitySelectionItem; 00030 00031 #define DEFAULT_CPTCITY_ARCHIVE "cpt-city-qgis-min" 00032 00033 class CORE_EXPORT QgsCptCityArchive 00034 { 00035 public: 00036 QgsCptCityArchive( QString archiveName = DEFAULT_CPTCITY_ARCHIVE, 00037 QString baseDir = QString() ); 00038 ~QgsCptCityArchive(); 00039 00040 // basic dir info 00041 QString baseDir() const; 00042 static QString baseDir( QString archiveName ); 00043 static QString defaultBaseDir(); 00044 void setBaseDir( QString dirName ) { mBaseDir = dirName; } 00045 00046 // collection + selection info 00047 QString copyingFileName( const QString& dirName ) const; 00048 QString descFileName( const QString& dirName ) const; 00049 static QString findFileName( const QString & target, const QString & startDir, const QString & baseDir ); 00050 static QMap< QString, QString > copyingInfo( const QString& fileName ); 00051 static QMap< QString, QString > description( const QString& fileName ); 00053 static QMap< double, QPair<QColor, QColor> > gradientColorMap( const QString& fileName ); 00054 00055 // archive management 00056 bool isEmpty(); 00057 QString archiveName() const { return mArchiveName; } 00058 static void initArchives( bool loadAll = false ); 00059 static void initArchive( QString archiveName, QString archiveBaseDir ); 00060 static void initDefaultArchive(); 00061 static void clearArchives(); 00062 static QgsCptCityArchive* defaultArchive(); 00063 static QMap< QString, QgsCptCityArchive* > archiveRegistry(); 00064 00065 // items 00066 QVector< QgsCptCityDataItem* > rootItems() const { return mRootItems; } 00067 QVector<QgsCptCityDataItem*> selectionItems() const { return mSelectionItems; } 00068 00069 protected: 00070 00071 QString mArchiveName; 00072 QString mBaseDir; 00073 static QString mDefaultArchiveName; 00074 static QMap< QString, QgsCptCityArchive* > mArchiveRegistry; 00075 // root items, namely directories at root of archive 00076 QVector< QgsCptCityDataItem* > mRootItems; 00077 QVector<QgsCptCityDataItem*> mSelectionItems; 00078 // mapping of copyinginfo, key is fileName 00079 static QMap< QString, QMap< QString, QString > > mCopyingInfoMap; 00080 00081 }; 00082 00084 class CORE_EXPORT QgsCptCityDataItem : public QObject 00085 { 00086 Q_OBJECT 00087 public: 00088 enum Type 00089 { 00090 ColorRamp, 00091 Collection, 00092 Directory, 00093 Selection, 00094 AllRamps 00095 }; 00096 00097 QgsCptCityDataItem( QgsCptCityDataItem::Type type, QgsCptCityDataItem* parent, 00098 QString name, QString path ); 00099 virtual ~QgsCptCityDataItem(); 00100 00101 bool hasChildren(); 00102 00103 int rowCount(); 00104 // retrieve total count of "leaf" items (all children which are end nodes) 00105 virtual int leafCount() const; 00106 00107 // 00108 00109 virtual void refresh(); 00110 00111 // Create vector of children 00112 virtual QVector<QgsCptCityDataItem*> createChildren(); 00113 00114 // Populate children using children vector created by createChildren() 00115 virtual void populate(); 00116 bool isPopulated() { return mPopulated; } 00117 00118 // Insert new child using alphabetical order based on mName, emits necessary signal to model before and after, sets parent and connects signals 00119 // refresh - refresh populated item, emit signals to model 00120 virtual void addChildItem( QgsCptCityDataItem * child, bool refresh = false ); 00121 00122 // remove and delete child item, signals to browser are emitted 00123 virtual void deleteChildItem( QgsCptCityDataItem * child ); 00124 00125 // remove child item but don't delete it, signals to browser are emitted 00126 // returns pointer to the removed item or null if no such item was found 00127 virtual QgsCptCityDataItem * removeChildItem( QgsCptCityDataItem * child ); 00128 00129 virtual bool equal( const QgsCptCityDataItem *other ); 00130 00131 virtual QWidget * paramWidget() { return 0; } 00132 00133 // list of actions provided by this item - usually used for popup menu on right-click 00134 virtual QList<QAction*> actions() { return QList<QAction*>(); } 00135 00136 // whether accepts drag&drop'd layers - e.g. for import 00137 virtual bool acceptDrop() { return false; } 00138 00139 // try to process the data dropped on this item 00140 virtual bool handleDrop( const QMimeData * /*data*/, Qt::DropAction /*action*/ ) { return false; } 00141 00142 // static methods 00143 00144 // Find child index in vector of items using '==' operator 00145 static int findItem( QVector<QgsCptCityDataItem*> items, QgsCptCityDataItem * item ); 00146 00147 // members 00148 00149 Type type() const { return mType; } 00150 QgsCptCityDataItem* parent() const { return mParent; } 00151 void setParent( QgsCptCityDataItem* parent ) { mParent = parent; } 00152 QVector<QgsCptCityDataItem*> children() const { return mChildren; } 00153 virtual QIcon icon() { return mIcon; } 00154 virtual QIcon icon( const QSize& size ) { Q_UNUSED( size ) ; return icon(); } 00155 QString name() const { return mName; } 00156 QString path() const { return mPath; } 00157 QString info() const { return mInfo; } 00158 QString shortInfo() const { return mShortInfo; } 00159 00160 void setIcon( QIcon icon ) { mIcon = icon; } 00161 00162 void setToolTip( QString msg ) { mToolTip = msg; } 00163 QString toolTip() const { return mToolTip; } 00164 00165 bool isValid() { return mValid; } 00166 00167 protected: 00168 00169 Type mType; 00170 QgsCptCityDataItem* mParent; 00171 QVector<QgsCptCityDataItem*> mChildren; // easier to have it always 00172 bool mPopulated; 00173 QString mName; 00174 QString mPath; // it is also used to identify item in tree 00175 QString mInfo; 00176 QString mShortInfo; 00177 QString mToolTip; 00178 QIcon mIcon; 00179 bool mValid; 00180 00181 public slots: 00182 void emitBeginInsertItems( QgsCptCityDataItem* parent, int first, int last ); 00183 void emitEndInsertItems(); 00184 void emitBeginRemoveItems( QgsCptCityDataItem* parent, int first, int last ); 00185 void emitEndRemoveItems(); 00186 00187 signals: 00188 void beginInsertItems( QgsCptCityDataItem* parent, int first, int last ); 00189 void endInsertItems(); 00190 void beginRemoveItems( QgsCptCityDataItem* parent, int first, int last ); 00191 void endRemoveItems(); 00192 }; 00193 00195 class CORE_EXPORT QgsCptCityColorRampItem : public QgsCptCityDataItem 00196 { 00197 Q_OBJECT 00198 public: 00199 QgsCptCityColorRampItem( QgsCptCityDataItem* parent, 00200 QString name, QString path, 00201 QString variantName = QString(), 00202 bool initialize = false ); 00203 QgsCptCityColorRampItem( QgsCptCityDataItem* parent, 00204 QString name, QString path, 00205 QStringList variantList, 00206 bool initialize = false ); 00207 ~QgsCptCityColorRampItem() {} 00208 00209 // --- reimplemented from QgsCptCityDataItem --- 00210 00211 virtual bool equal( const QgsCptCityDataItem *other ); 00212 virtual int leafCount() const { return 1; } 00213 00214 // --- New virtual methods for layer item derived classes --- 00215 const QgsCptCityColorRampV2& ramp() const { return mRamp; } 00216 QIcon icon(); 00217 QIcon icon( const QSize& size ); 00218 void init(); 00219 00220 protected: 00221 00222 bool mInitialised; 00223 QgsCptCityColorRampV2 mRamp; 00224 QList< QIcon > mIcons; 00225 }; 00226 00227 00229 class CORE_EXPORT QgsCptCityCollectionItem : public QgsCptCityDataItem 00230 { 00231 Q_OBJECT 00232 public: 00233 QgsCptCityCollectionItem( QgsCptCityDataItem* parent, 00234 QString name, QString path ); 00235 ~QgsCptCityCollectionItem(); 00236 00237 void setPopulated() { mPopulated = true; } 00238 void addChild( QgsCptCityDataItem *item ) { mChildren.append( item ); } 00239 QVector<QgsCptCityDataItem*> childrenRamps( bool recursive ); 00240 00241 protected: 00242 bool mPopulatedRamps; 00243 }; 00244 00246 class CORE_EXPORT QgsCptCityDirectoryItem : public QgsCptCityCollectionItem 00247 { 00248 Q_OBJECT 00249 public: 00250 QgsCptCityDirectoryItem( QgsCptCityDataItem* parent, 00251 QString name, QString path ); 00252 ~QgsCptCityDirectoryItem(); 00253 00254 QVector<QgsCptCityDataItem*> createChildren(); 00255 00256 virtual bool equal( const QgsCptCityDataItem *other ); 00257 00258 static QgsCptCityDataItem* dataItem( QgsCptCityDataItem* parent, 00259 QString name, QString path ); 00260 00261 protected: 00262 QMap< QString, QStringList > rampsMap(); 00263 QStringList dirEntries() const; 00264 QMap< QString, QStringList > mRampsMap; 00265 }; 00266 00268 class CORE_EXPORT QgsCptCitySelectionItem : public QgsCptCityCollectionItem 00269 { 00270 Q_OBJECT 00271 public: 00272 QgsCptCitySelectionItem( QgsCptCityDataItem* parent, QString name, QString path ); 00273 ~QgsCptCitySelectionItem(); 00274 00275 QVector<QgsCptCityDataItem*> createChildren(); 00276 00277 virtual bool equal( const QgsCptCityDataItem *other ); 00278 00279 QStringList selectionsList() const { return mSelectionsList; } 00280 00281 protected: 00282 void parseXML(); 00283 QStringList mSelectionsList; 00284 }; 00285 00287 class CORE_EXPORT QgsCptCityAllRampsItem : public QgsCptCityCollectionItem 00288 { 00289 Q_OBJECT 00290 public: 00291 QgsCptCityAllRampsItem( QgsCptCityDataItem* parent, QString name, 00292 QVector<QgsCptCityDataItem*> items ); 00293 ~QgsCptCityAllRampsItem(); 00294 00295 QVector<QgsCptCityDataItem*> createChildren(); 00296 00297 protected: 00298 QVector<QgsCptCityDataItem*> mItems; 00299 }; 00300 00301 00302 class CORE_EXPORT QgsCptCityBrowserModel : public QAbstractItemModel 00303 { 00304 Q_OBJECT 00305 00306 public: 00307 00308 enum ViewType 00309 { 00310 Authors = 0, 00311 Selections = 1, 00312 List = 2 00313 }; 00314 00315 QgsCptCityBrowserModel( QObject* parent = 0, 00316 QgsCptCityArchive* archive = QgsCptCityArchive::defaultArchive(), 00317 ViewType Type = Authors ); 00318 ~QgsCptCityBrowserModel(); 00319 00320 // implemented methods from QAbstractItemModel for read-only access 00321 00324 virtual Qt::ItemFlags flags( const QModelIndex &index ) const; 00325 00330 virtual QVariant data( const QModelIndex &index, int role = Qt::DisplayRole ) const; 00331 00334 virtual QVariant headerData( int section, Qt::Orientation orientation, int role = Qt::DisplayRole ) const; 00335 00337 virtual int rowCount( const QModelIndex &parent = QModelIndex() ) const; 00338 00341 virtual int columnCount( const QModelIndex &parent = QModelIndex() ) const; 00342 00344 virtual QModelIndex index( int row, int column, const QModelIndex & parent = QModelIndex() ) const; 00345 00346 QModelIndex findItem( QgsCptCityDataItem *item, QgsCptCityDataItem *parent = 0 ) const; 00347 00351 virtual QModelIndex parent( const QModelIndex &index ) const; 00352 00354 /* virtual QStringList mimeTypes() const; */ 00355 00357 /* virtual QMimeData * mimeData( const QModelIndexList &indexes ) const; */ 00358 00360 /* virtual bool dropMimeData( const QMimeData * data, Qt::DropAction action, int row, int column, const QModelIndex & parent ); */ 00361 00362 QgsCptCityDataItem *dataItem( const QModelIndex &idx ) const; 00363 00364 bool hasChildren( const QModelIndex &parent = QModelIndex() ) const; 00365 00366 // Reload the whole model 00367 void reload(); 00368 00369 // Refresh item specified by path 00370 void refresh( QString path ); 00371 00372 // Refresh item childs 00373 void refresh( const QModelIndex &index = QModelIndex() ); 00374 00376 QModelIndex findPath( QString path ); 00377 00378 void connectItem( QgsCptCityDataItem *item ); 00379 00380 bool canFetchMore( const QModelIndex & parent ) const; 00381 void fetchMore( const QModelIndex & parent ); 00382 00383 signals: 00384 00385 public slots: 00386 //void removeItems( QgsCptCityDataItem * parent, QVector<QgsCptCityDataItem *>items ); 00387 //void addItems( QgsCptCityDataItem * parent, QVector<QgsCptCityDataItem *>items ); 00388 //void refreshItems( QgsCptCityDataItem * parent, QVector<QgsCptCityDataItem *>items ); 00389 00390 void beginInsertItems( QgsCptCityDataItem *parent, int first, int last ); 00391 void endInsertItems(); 00392 void beginRemoveItems( QgsCptCityDataItem *parent, int first, int last ); 00393 void endRemoveItems(); 00394 00395 protected: 00396 00397 // populates the model 00398 void addRootItems( ); 00399 void removeRootItems(); 00400 00401 QVector<QgsCptCityDataItem*> mRootItems; 00402 QgsCptCityArchive* mArchive; 00403 ViewType mViewType; 00404 QSize mIconSize; 00405 }; 00406 00407 #endif