@@ -43,20 +43,20 @@ QgsBrowserModel::QgsBrowserModel( QObject *parent ) :
43
43
}
44
44
45
45
int capabilities = dataCapabilities ();
46
- if ( capabilities == QgsDataProvider::NoDataCapabilities )
46
+ if ( capabilities == QgsDataProvider::NoDataCapabilities )
47
47
{
48
48
QgsDebugMsg ( library->fileName () + " does not have any dataCapabilities" );
49
49
continue ;
50
50
}
51
51
52
52
dataItem_t * dataItem = ( dataItem_t * ) cast_to_fptr ( library->resolve ( " dataItem" ) );
53
- if ( ! dataItem )
53
+ if ( !dataItem )
54
54
{
55
55
QgsDebugMsg ( library->fileName () + " does not have dataItem" );
56
56
continue ;
57
57
}
58
58
59
- QgsDataItem * item = dataItem ( " " , NULL ); // empty path -> top level
59
+ QgsDataItem *item = dataItem ( " " , NULL ); // empty path -> top level
60
60
if ( item )
61
61
{
62
62
QgsDebugMsg ( " Add new top level item : " + item->name () );
@@ -83,24 +83,29 @@ Qt::ItemFlags QgsBrowserModel::flags( const QModelIndex & index ) const
83
83
return Qt::ItemIsEnabled | Qt::ItemIsSelectable;
84
84
}
85
85
86
- QVariant QgsBrowserModel::data ( const QModelIndex & index, int role ) const
86
+ QVariant QgsBrowserModel::data ( const QModelIndex &index, int role ) const
87
87
{
88
88
if ( !index.isValid () )
89
89
return QVariant ();
90
90
91
- QgsDataItem* ptr = ( QgsDataItem* ) index.internalPointer ();
92
-
93
- if ( role == Qt::DisplayRole )
91
+ QgsDataItem *item = dataItem ( index );
92
+ if ( !item )
94
93
{
95
- return QVariant ( ptr->name () );
94
+ return QVariant ();
95
+ }
96
+ else if ( role == Qt::DisplayRole )
97
+ {
98
+ return item->name ();
96
99
}
97
100
else if ( role == Qt::DecorationRole && index.column () == 0 )
98
101
{
99
- return QVariant ( ptr->icon () );
102
+ return item->icon ();
103
+ }
104
+ else
105
+ {
106
+ // unsupported role
107
+ return QVariant ();
100
108
}
101
-
102
- // unsupported role
103
- return QVariant ();
104
109
}
105
110
106
111
QVariant QgsBrowserModel::headerData ( int section, Qt::Orientation orientation, int role ) const
@@ -113,7 +118,7 @@ QVariant QgsBrowserModel::headerData( int section, Qt::Orientation orientation,
113
118
return QVariant ();
114
119
}
115
120
116
- int QgsBrowserModel::rowCount ( const QModelIndex & parent ) const
121
+ int QgsBrowserModel::rowCount ( const QModelIndex &parent ) const
117
122
{
118
123
// qDebug("rowCount: idx: (valid %d) %d %d", parent.isValid(), parent.row(), parent.column());
119
124
@@ -125,159 +130,100 @@ int QgsBrowserModel::rowCount( const QModelIndex & parent ) const
125
130
else
126
131
{
127
132
// ordinary item: number of its children
128
- QgsDataItem* ptr = ( QgsDataItem* ) parent.internalPointer ();
129
-
130
- return ptr->rowCount ();
133
+ QgsDataItem *item = dataItem ( parent );
134
+ return item ? item->rowCount () : 0 ;
131
135
}
132
136
}
133
137
134
138
bool QgsBrowserModel::hasChildren ( const QModelIndex & parent ) const
135
139
{
136
140
if ( !parent.isValid () )
137
- {
138
141
return true ; // root item: its children are top level items
139
- }
140
- else
141
- {
142
- QgsDataItem* ptr = ( QgsDataItem* ) parent.internalPointer ();
143
142
144
- return ptr-> hasChildren ( );
145
- }
143
+ QgsDataItem *item = dataItem ( parent );
144
+ return item && item-> hasChildren ();
146
145
}
147
146
148
147
int QgsBrowserModel::columnCount ( const QModelIndex & parent ) const
149
148
{
150
149
return 1 ;
151
150
}
152
151
153
- QModelIndex QgsBrowserModel::index ( int row, int column, const QModelIndex & parent ) const
152
+ /* Refresh dir path */
153
+ void QgsBrowserModel::refresh ( QString path, const QModelIndex &theIndex )
154
154
{
155
- // qDebug("index: idx: (valid %d) %d %d", parent.isValid(), parent.row(), parent.column());
156
-
157
- if ( !parent.isValid () )
158
- {
159
- // this is the root item, parent of the top level items
160
- Q_ASSERT ( column == 0 && row >= 0 && row < mRootItems .count () );
161
- return createIndex ( row, column, mRootItems [row] );
162
- }
163
- else
155
+ QStringList paths = path.split ( ' /' );
156
+ for ( int i = 0 ; i < rowCount ( theIndex ); i++ )
164
157
{
165
- // this is ordinary item: return a valid index if the requested child exists
166
- QgsDataItem* ptr = ( QgsDataItem* ) parent.internalPointer ();
167
- if ( ptr->type () == QgsDataItem::Directory || ptr->type () == QgsDataItem::Collection )
168
- {
169
- // this is a directory: get index of its subdir!
170
- QgsDirectoryItem* di = ( QgsDirectoryItem* ) ptr;
171
- return createIndex ( row, column, di->children ().at ( row ) );
172
- }
173
- if ( ptr->type () == QgsDataItem::Layer )
158
+ QModelIndex idx = index ( i, 0 , theIndex );
159
+ QgsDataItem *item = dataItem ( idx );
160
+ if ( !item )
161
+ break ;
162
+
163
+ if ( item->path () == path )
174
164
{
175
- return QModelIndex (); // has no children
165
+ QgsDebugMsg ( " Arrived " + item->path () );
166
+ item->refresh ();
167
+ return ;
176
168
}
177
169
178
- Q_ASSERT ( false && " unknown item in index()" );
179
- }
180
-
181
- return QModelIndex (); // if the child does not exist
182
- }
183
-
184
- QModelIndex QgsBrowserModel::index ( QgsDataItem *item )
185
- {
186
- // Item index
187
- QModelIndex index = QModelIndex ();
188
-
189
- const QVector<QgsDataItem*>& children = item->parent () ? item->parent ()->children () : mRootItems ;
190
-
191
- Q_ASSERT ( children.size () > 0 );
192
- int row = -1 ;
193
- for ( int i = 0 ; i < children.size (); i++ )
194
- {
195
- if ( item == children[i] )
170
+ if ( path.indexOf ( item->path () ) == 0 )
196
171
{
197
- row = i ;
172
+ refresh ( path, idx ) ;
198
173
break ;
199
174
}
200
175
}
201
- QgsDebugMsg ( QString ( " row = %1" ).arg ( row ) );
202
- Q_ASSERT ( row >= 0 );
203
- index = createIndex ( row, 0 , item );
176
+ }
204
177
205
- return index;
178
+ QModelIndex QgsBrowserModel::index ( int row, int column, const QModelIndex &parent ) const
179
+ {
180
+ QgsDataItem *p = dataItem ( parent );
181
+ const QVector<QgsDataItem*> &items = p ? p->children () : mRootItems ;
182
+ QgsDataItem *item = items.value ( row, 0 );
183
+ return item ? createIndex ( row, column, item ) : QModelIndex ();
206
184
}
207
185
208
- QModelIndex QgsBrowserModel::parent ( const QModelIndex & index ) const
186
+ QModelIndex QgsBrowserModel::parent ( const QModelIndex &index ) const
209
187
{
210
- if ( !index.isValid () )
188
+ QgsDataItem *item = dataItem ( index );
189
+ if ( !item )
211
190
return QModelIndex ();
212
191
213
- // return QModelInde of parent, i.e. where the parent is within its parent :-)
214
-
215
- // qDebug("parent of: %d %d", index.row(), index.column());
192
+ return findItem ( item->parent () );
193
+ }
216
194
217
- QgsDataItem* ptr = ( QgsDataItem* ) index.internalPointer ();
218
- QgsDataItem* parentItem = ptr->parent ();
195
+ QModelIndex QgsBrowserModel::findItem ( QgsDataItem *item, QgsDataItem *parent ) const
196
+ {
197
+ const QVector<QgsDataItem*> &items = parent ? parent->children () : mRootItems ;
219
198
220
- if ( parentItem == NULL )
199
+ for ( int i = 0 ; i < items. size (); i++ )
221
200
{
222
- // parent of our root is invalid index
223
- return QModelIndex ();
224
- }
225
-
226
- const QVector<QgsDataItem*>& children =
227
- parentItem->parent () ? (( QgsDirectoryItem* )parentItem->parent () )->children () : mRootItems ;
228
- Q_ASSERT ( children.count () > 0 );
201
+ if ( items[i] == item )
202
+ return createIndex ( i, 0 , item );
229
203
230
- for ( int i = 0 ; i < children.count (); i++ )
231
- {
232
- if ( children[i] == parentItem )
233
- return createIndex ( i, 0 , parentItem );
204
+ QModelIndex childIndex = findItem ( item, items[i] );
205
+ if ( childIndex.isValid () )
206
+ return childIndex;
234
207
}
235
208
236
- Q_ASSERT ( false && " parent not found!" );
237
209
return QModelIndex ();
238
210
}
239
211
240
- /* Refresh dir path */
241
- void QgsBrowserModel::refresh ( QString path, const QModelIndex& theIndex )
242
- {
243
- QStringList paths = path.split ( ' /' );
244
- for ( int i = 0 ; i < rowCount ( theIndex ); i++ )
245
- {
246
- QModelIndex idx = index ( i, 0 , theIndex );
247
- QgsDataItem* ptr = ( QgsDataItem* ) idx.internalPointer ();
248
- if ( ptr->path () == path )
249
- {
250
- QgsDebugMsg ( " Arrived " + ptr->path () );
251
- ptr->refresh ();
252
- return ;
253
- }
254
- if ( path.indexOf ( ptr->path () ) == 0 )
255
- {
256
- refresh ( path, idx );
257
- break ;
258
- }
259
- }
260
- }
261
-
262
212
/* Refresh item */
263
213
void QgsBrowserModel::refresh ( const QModelIndex& theIndex )
264
214
{
265
- if ( !theIndex.isValid () ) // root
266
- {
267
- // Nothing to do I believe, mRootItems are always the same
268
- }
269
- else
270
- {
271
- QgsDataItem* ptr = ( QgsDataItem* ) theIndex.internalPointer ();
272
- QgsDebugMsg ( " Refresh " + ptr->path () );
273
- ptr->refresh ();
274
- }
215
+ QgsDataItem *item = dataItem ( theIndex );
216
+ if ( !item )
217
+ return ;
218
+
219
+ QgsDebugMsg ( " Refresh " + item->path () );
220
+ item->refresh ();
275
221
}
276
222
277
- void QgsBrowserModel::beginInsertItems ( QgsDataItem* parent, int first, int last )
223
+ void QgsBrowserModel::beginInsertItems ( QgsDataItem * parent, int first, int last )
278
224
{
279
225
QgsDebugMsg ( " parent mPath = " + parent->path () );
280
- QModelIndex idx = index ( parent );
226
+ QModelIndex idx = findItem ( parent );
281
227
if ( !idx.isValid () )
282
228
return ;
283
229
QgsDebugMsg ( " valid" );
@@ -289,10 +235,10 @@ void QgsBrowserModel::endInsertItems()
289
235
QgsDebugMsg ( " Entered" );
290
236
endInsertRows ();
291
237
}
292
- void QgsBrowserModel::beginRemoveItems ( QgsDataItem* parent, int first, int last )
238
+ void QgsBrowserModel::beginRemoveItems ( QgsDataItem * parent, int first, int last )
293
239
{
294
240
QgsDebugMsg ( " parent mPath = " + parent->path () );
295
- QModelIndex idx = index ( parent );
241
+ QModelIndex idx = findItem ( parent );
296
242
if ( !idx.isValid () )
297
243
return ;
298
244
beginRemoveRows ( idx, first, last );
@@ -313,3 +259,11 @@ void QgsBrowserModel::connectItem( QgsDataItem* item )
313
259
connect ( item, SIGNAL ( endRemoveItems () ),
314
260
this , SLOT ( endRemoveItems () ) );
315
261
}
262
+
263
+ QgsDataItem *QgsBrowserModel::dataItem ( const QModelIndex &idx ) const
264
+ {
265
+ void *v = idx.internalPointer ();
266
+ QgsDataItem *d = reinterpret_cast <QgsDataItem*>( v );
267
+ Q_ASSERT ( !v || d );
268
+ return d;
269
+ }
0 commit comments