1111#include " qgsbrowsermodel.h"
1212
1313
14- QgsBrowserModel::QgsBrowserModel (QObject *parent) :
15- QAbstractItemModel(parent)
14+ QgsBrowserModel::QgsBrowserModel ( QObject *parent ) :
15+ QAbstractItemModel( parent )
1616{
1717 QStyle *style = QApplication::style ();
1818 mIconDirectory = QIcon ( style->standardPixmap ( QStyle::SP_DirClosedIcon ) );
1919 mIconDirectory .addPixmap ( style->standardPixmap ( QStyle::SP_DirOpenIcon ),
2020 QIcon::Normal, QIcon::On );
2121
22- foreach ( QFileInfo drive, QDir::drives ())
22+ foreach ( QFileInfo drive, QDir::drives () )
2323 {
2424 QString path = drive.absolutePath ();
25- QgsDirectoryItem *item = new QgsDirectoryItem (NULL , path, path);
25+ QgsDirectoryItem *item = new QgsDirectoryItem ( NULL , path, path );
2626
27- connectItem (item);
27+ connectItem ( item );
2828 mRootItems << item;
2929 }
3030
3131 // Add non file top level items
32- foreach ( QString key, QgsProviderRegistry::instance ()->providerList () )
32+ foreach ( QString key, QgsProviderRegistry::instance ()->providerList () )
3333 {
34- QLibrary *library = QgsProviderRegistry::instance ()->getLibrary (key);
34+ QLibrary *library = QgsProviderRegistry::instance ()->getLibrary ( key );
3535 if ( !library ) continue ;
3636
37- dataCapabilities_t * dataCapabilities = (dataCapabilities_t *) cast_to_fptr ( library->resolve (" dataCapabilities" ) );
38- if ( !dataCapabilities ) {
39- QgsDebugMsg ( library->fileName () + " does not have dataCapabilities" );
37+ dataCapabilities_t * dataCapabilities = ( dataCapabilities_t * ) cast_to_fptr ( library->resolve ( " dataCapabilities" ) );
38+ if ( !dataCapabilities )
39+ {
40+ QgsDebugMsg ( library->fileName () + " does not have dataCapabilities" );
4041 continue ;
4142 }
4243
4344 int capabilities = dataCapabilities ();
44- if ( capabilities == QgsDataProvider::NoDataCapabilities )
45+ if ( capabilities == QgsDataProvider::NoDataCapabilities )
4546 {
46- QgsDebugMsg ( library->fileName () + " does not have any dataCapabilities" );
47+ QgsDebugMsg ( library->fileName () + " does not have any dataCapabilities" );
4748 continue ;
4849 }
4950
50- dataItem_t * dataItem = (dataItem_t *) cast_to_fptr ( library->resolve ( " dataItem" ) );
51+ dataItem_t * dataItem = ( dataItem_t * ) cast_to_fptr ( library->resolve ( " dataItem" ) );
5152 if ( ! dataItem )
5253 {
53- QgsDebugMsg ( library->fileName () + " does not have dataItem" );
54+ QgsDebugMsg ( library->fileName () + " does not have dataItem" );
5455 continue ;
5556 }
5657
57- QgsDataItem * item = dataItem ( " " , NULL ); // empty path -> top level
58+ QgsDataItem * item = dataItem ( " " , NULL ); // empty path -> top level
5859 if ( item )
5960 {
60- QgsDebugMsg ( " Add new top level item : " + item->name () );
61- connectItem (item);
61+ QgsDebugMsg ( " Add new top level item : " + item->name () );
62+ connectItem ( item );
6263 mRootItems << item;
6364 }
6465 }
6566}
6667
6768QgsBrowserModel::~QgsBrowserModel ()
6869{
69- foreach (QgsDataItem* item, mRootItems )
70+ foreach ( QgsDataItem* item, mRootItems )
71+ {
7072 delete item;
73+ }
7174}
7275
7376
7477Qt::ItemFlags QgsBrowserModel::flags ( const QModelIndex & index ) const
7578{
76- if (!index.isValid ())
79+ if ( !index.isValid () )
7780 return 0 ;
7881
7982 return Qt::ItemIsEnabled | Qt::ItemIsSelectable;
8083}
8184
8285QVariant QgsBrowserModel::data ( const QModelIndex & index, int role ) const
8386{
84- if (!index.isValid ())
87+ if ( !index.isValid () )
8588 return QVariant ();
8689
87- QgsDataItem* ptr = (QgsDataItem*) index.internalPointer ();
90+ QgsDataItem* ptr = ( QgsDataItem* ) index.internalPointer ();
8891
89- if (role == Qt::DisplayRole)
92+ if ( role == Qt::DisplayRole )
9093 {
91- return QVariant (ptr->name ());
94+ return QVariant ( ptr->name () );
9295 }
93- else if (role == Qt::DecorationRole && index.column () == 0 )
96+ else if ( role == Qt::DecorationRole && index.column () == 0 )
9497 {
9598 return QVariant ( ptr->icon () );
9699 }
@@ -101,49 +104,47 @@ QVariant QgsBrowserModel::data( const QModelIndex & index, int role ) const
101104
102105QVariant QgsBrowserModel::headerData ( int section, Qt::Orientation orientation, int role ) const
103106{
104- if (orientation == Qt::Horizontal && role == Qt::DisplayRole)
107+ if ( orientation == Qt::Horizontal && role == Qt::DisplayRole )
105108 {
106- return QVariant (" header" );
109+ return QVariant ( " header" );
107110 }
108111
109112 return QVariant ();
110113}
111114
112115int QgsBrowserModel::rowCount ( const QModelIndex & parent ) const
113116{
114- // qDebug("rowCount: idx: (valid %d) %d %d", parent.isValid(), parent.row(), parent.column());
117+ // qDebug("rowCount: idx: (valid %d) %d %d", parent.isValid(), parent.row(), parent.column());
115118
116- if (!parent.isValid ())
117- {
118- // root item: its children are top level items
119- return mRootItems .count (); // mRoot
120- }
121- else
122- {
123- // ordinary item: number of its children
124- QgsDataItem* ptr = (QgsDataItem*) parent.internalPointer ();
119+ if ( !parent.isValid () )
120+ {
121+ // root item: its children are top level items
122+ return mRootItems .count (); // mRoot
123+ }
124+ else
125+ {
126+ // ordinary item: number of its children
127+ QgsDataItem* ptr = ( QgsDataItem* ) parent.internalPointer ();
125128
126- return ptr->rowCount ();
127- }
129+ return ptr->rowCount ();
130+ }
128131}
129132
130- bool QgsBrowserModel::hasChildren ( const QModelIndex & parent ) const
133+ bool QgsBrowserModel::hasChildren ( const QModelIndex & parent ) const
131134{
132- if (!parent.isValid ())
135+ if ( !parent.isValid () )
133136 {
134137 return true ; // root item: its children are top level items
135138 }
136139 else
137140 {
138- QgsDataItem* ptr = (QgsDataItem*) parent.internalPointer ();
141+ QgsDataItem* ptr = ( QgsDataItem* ) parent.internalPointer ();
139142
140143 return ptr->hasChildren ();
141144 }
142-
143- return false ;
144145}
145146
146- int QgsBrowserModel::columnCount ( const QModelIndex & parent ) const
147+ int QgsBrowserModel::columnCount ( const QModelIndex & parent ) const
147148{
148149 return 1 ;
149150}
@@ -152,28 +153,28 @@ QModelIndex QgsBrowserModel::index( int row, int column, const QModelIndex & par
152153{
153154 // qDebug("index: idx: (valid %d) %d %d", parent.isValid(), parent.row(), parent.column());
154155
155- if (!parent.isValid ())
156+ if ( !parent.isValid () )
156157 {
157158 // this is the root item, parent of the top level items
158- Q_ASSERT (column == 0 && row >= 0 && row < mRootItems .count ());
159- return createIndex (row,column, mRootItems [row]);
159+ Q_ASSERT ( column == 0 && row >= 0 && row < mRootItems .count () );
160+ return createIndex ( row, column, mRootItems [row] );
160161 }
161162 else
162163 {
163164 // this is ordinary item: return a valid index if the requested child exists
164- QgsDataItem* ptr = (QgsDataItem*) parent.internalPointer ();
165- if (ptr->type () == QgsDataItem::Directory || ptr->type () == QgsDataItem::Collection)
165+ QgsDataItem* ptr = ( QgsDataItem* ) parent.internalPointer ();
166+ if ( ptr->type () == QgsDataItem::Directory || ptr->type () == QgsDataItem::Collection )
166167 {
167168 // this is a directory: get index of its subdir!
168- QgsDirectoryItem* di = (QgsDirectoryItem*) ptr;
169- return createIndex (row, column, di->children ().at (row) );
169+ QgsDirectoryItem* di = ( QgsDirectoryItem* ) ptr;
170+ return createIndex ( row, column, di->children ().at ( row ) );
170171 }
171- if (ptr->type () == QgsDataItem::Layer)
172+ if ( ptr->type () == QgsDataItem::Layer )
172173 {
173174 return QModelIndex (); // has no children
174175 }
175176
176- Q_ASSERT (false && " unknown item in index()" );
177+ Q_ASSERT ( false && " unknown item in index()" );
177178 }
178179
179180 return QModelIndex (); // if the child does not exist
@@ -196,7 +197,7 @@ QModelIndex QgsBrowserModel::index( QgsDataItem *item )
196197 break ;
197198 }
198199 }
199- QgsDebugMsg ( QString ( " row = %1" ).arg (row) );
200+ QgsDebugMsg ( QString ( " row = %1" ).arg ( row ) );
200201 Q_ASSERT ( row >= 0 );
201202 index = createIndex ( row, 0 , item );
202203
@@ -205,51 +206,51 @@ QModelIndex QgsBrowserModel::index( QgsDataItem *item )
205206
206207QModelIndex QgsBrowserModel::parent ( const QModelIndex & index ) const
207208{
208- if (!index.isValid ())
209+ if ( !index.isValid () )
209210 return QModelIndex ();
210211
211212 // return QModelInde of parent, i.e. where the parent is within its parent :-)
212213
213214 // qDebug("parent of: %d %d", index.row(), index.column());
214215
215- QgsDataItem* ptr = (QgsDataItem*) index.internalPointer ();
216+ QgsDataItem* ptr = ( QgsDataItem* ) index.internalPointer ();
216217 QgsDataItem* parentItem = ptr->parent ();
217218
218- if (parentItem == NULL )
219+ if ( parentItem == NULL )
219220 {
220221 // parent of our root is invalid index
221222 return QModelIndex ();
222223 }
223224
224225 const QVector<QgsDataItem*>& children =
225- parentItem->parent () ? ((QgsDirectoryItem*)parentItem->parent ())->children () : mRootItems ;
226- Q_ASSERT (children.count () > 0 );
226+ parentItem->parent () ? (( QgsDirectoryItem* )parentItem->parent () )->children () : mRootItems ;
227+ Q_ASSERT ( children.count () > 0 );
227228
228- for (int i = 0 ; i < children.count (); i++)
229+ for ( int i = 0 ; i < children.count (); i++ )
229230 {
230- if (children[i] == parentItem)
231- return createIndex (i, 0 , parentItem);
231+ if ( children[i] == parentItem )
232+ return createIndex ( i, 0 , parentItem );
232233 }
233234
234- Q_ASSERT (false && " parent not found!" );
235+ Q_ASSERT ( false && " parent not found!" );
235236 return QModelIndex ();
236237}
237238
238239/* Refresh dir path */
239240void QgsBrowserModel::refresh ( QString path, const QModelIndex& theIndex )
240241{
241- QStringList paths = path.split (' /' );
242- for ( int i = 0 ; i < rowCount (theIndex); i++ )
242+ QStringList paths = path.split ( ' /' );
243+ for ( int i = 0 ; i < rowCount ( theIndex ); i++ )
243244 {
244- QModelIndex idx = index (i, 0 , theIndex);
245- QgsDataItem* ptr = (QgsDataItem*) idx.internalPointer ();
245+ QModelIndex idx = index ( i, 0 , theIndex );
246+ QgsDataItem* ptr = ( QgsDataItem* ) idx.internalPointer ();
246247 if ( ptr->path () == path )
247248 {
248249 QgsDebugMsg ( " Arrived " + ptr->path () );
249250 ptr->refresh ();
250251 return ;
251252 }
252- if ( path.indexOf ( ptr->path () ) == 0 )
253+ if ( path.indexOf ( ptr->path () ) == 0 )
253254 {
254255 refresh ( path, idx );
255256 break ;
@@ -260,13 +261,13 @@ void QgsBrowserModel::refresh( QString path, const QModelIndex& theIndex )
260261/* Refresh item */
261262void QgsBrowserModel::refresh ( const QModelIndex& theIndex )
262263{
263- if ( !theIndex.isValid () ) // root
264+ if ( !theIndex.isValid () ) // root
264265 {
265266 // Nothing to do I believe, mRootItems are always the same
266267 }
267268 else
268269 {
269- QgsDataItem* ptr = (QgsDataItem*) theIndex.internalPointer ();
270+ QgsDataItem* ptr = ( QgsDataItem* ) theIndex.internalPointer ();
270271 QgsDebugMsg ( " Refresh " + ptr->path () );
271272 ptr->refresh ();
272273 }
@@ -277,13 +278,13 @@ void QgsBrowserModel::beginInsertItems( QgsDataItem* parent, int first, int last
277278 QgsDebugMsg ( " parent mPath = " + parent->path () );
278279 QModelIndex idx = index ( parent );
279280 if ( !idx.isValid () ) return ;
280- QgsDebugMsg ( " valid" );
281+ QgsDebugMsg ( " valid" );
281282 beginInsertRows ( idx, first, last );
282- QgsDebugMsg ( " end" );
283+ QgsDebugMsg ( " end" );
283284}
284285void QgsBrowserModel::endInsertItems ()
285286{
286- QgsDebugMsg ( " Entered" );
287+ QgsDebugMsg ( " Entered" );
287288 endInsertRows ();
288289}
289290void QgsBrowserModel::beginRemoveItems ( QgsDataItem* parent, int first, int last )
@@ -295,17 +296,17 @@ void QgsBrowserModel::beginRemoveItems( QgsDataItem* parent, int first, int last
295296}
296297void QgsBrowserModel::endRemoveItems ()
297298{
298- QgsDebugMsg ( " Entered" );
299+ QgsDebugMsg ( " Entered" );
299300 endRemoveRows ();
300301}
301- void QgsBrowserModel::connectItem ( QgsDataItem* item )
302+ void QgsBrowserModel::connectItem ( QgsDataItem* item )
302303{
303- connect ( item, SIGNAL (beginInsertItems ( QgsDataItem*, int , int )),
304- this , SLOT (beginInsertItems ( QgsDataItem*, int , int )) );
305- connect ( item, SIGNAL (endInsertItems () ),
306- this , SLOT (endInsertItems ()) );
307- connect ( item, SIGNAL (beginRemoveItems ( QgsDataItem*, int , int )),
308- this , SLOT (beginRemoveItems ( QgsDataItem*, int , int )) );
309- connect ( item, SIGNAL (endRemoveItems () ),
310- this , SLOT (endRemoveItems ()) );
304+ connect ( item, SIGNAL ( beginInsertItems ( QgsDataItem*, int , int ) ),
305+ this , SLOT ( beginInsertItems ( QgsDataItem*, int , int ) ) );
306+ connect ( item, SIGNAL ( endInsertItems () ),
307+ this , SLOT ( endInsertItems () ) );
308+ connect ( item, SIGNAL ( beginRemoveItems ( QgsDataItem*, int , int ) ),
309+ this , SLOT ( beginRemoveItems ( QgsDataItem*, int , int ) ) );
310+ connect ( item, SIGNAL ( endRemoveItems () ),
311+ this , SLOT ( endRemoveItems () ) );
311312}
0 commit comments