@@ -51,9 +51,6 @@ QgsBrowserModel::QgsBrowserModel( QObject *parent )
51
51
{
52
52
connect ( QgsProject::instance (), SIGNAL ( readProject ( const QDomDocument & ) ), this , SLOT ( updateProjectHome () ) );
53
53
connect ( QgsProject::instance (), SIGNAL ( writeProject ( QDomDocument & ) ), this , SLOT ( updateProjectHome () ) );
54
- mLoadingMovie .setFileName ( QgsApplication::iconPath ( " /mIconLoading.gif" ) );
55
- mLoadingMovie .setCacheMode ( QMovie::CacheAll );
56
- connect ( &mLoadingMovie , SIGNAL ( frameChanged ( int ) ), SLOT ( loadingFrameChanged () ) );
57
54
addRootItems ();
58
55
}
59
56
@@ -232,10 +229,6 @@ QVariant QgsBrowserModel::data( const QModelIndex &index, int role ) const
232
229
}
233
230
else if ( role == Qt::DecorationRole && index.column () == 0 )
234
231
{
235
- if ( fetching ( item ) )
236
- {
237
- return mLoadingIcon ;
238
- }
239
232
return item->icon ();
240
233
}
241
234
else
@@ -330,6 +323,7 @@ QModelIndex QgsBrowserModel::findPath( QString path, Qt::MatchFlag matchFlag )
330
323
331
324
void QgsBrowserModel::reload ()
332
325
{
326
+ // TODO: put items creating currently children in threads to deleteLater (does not seem urget because reload() is not used in QGIS)
333
327
beginResetModel ();
334
328
removeRootItems ();
335
329
addRootItems ();
@@ -398,6 +392,14 @@ void QgsBrowserModel::endRemoveItems()
398
392
QgsDebugMsgLevel ( " Entered" , 3 );
399
393
endRemoveRows ();
400
394
}
395
+ void QgsBrowserModel::dataItemChanged ( QgsDataItem * item )
396
+ {
397
+ QgsDebugMsgLevel ( " Entered" , 3 );
398
+ QModelIndex idx = findItem ( item );
399
+ if ( !idx.isValid () )
400
+ return ;
401
+ emit dataChanged ( idx, idx );
402
+ }
401
403
void QgsBrowserModel::connectItem ( QgsDataItem* item )
402
404
{
403
405
connect ( item, SIGNAL ( beginInsertItems ( QgsDataItem*, int , int ) ),
@@ -408,6 +410,8 @@ void QgsBrowserModel::connectItem( QgsDataItem* item )
408
410
this , SLOT ( beginRemoveItems ( QgsDataItem*, int , int ) ) );
409
411
connect ( item, SIGNAL ( endRemoveItems () ),
410
412
this , SLOT ( endRemoveItems () ) );
413
+ connect ( item, SIGNAL ( dataChanged ( QgsDataItem* ) ),
414
+ this , SLOT ( dataItemChanged ( QgsDataItem* ) ) );
411
415
}
412
416
413
417
QStringList QgsBrowserModel::mimeTypes () const
@@ -463,30 +467,20 @@ bool QgsBrowserModel::canFetchMore( const QModelIndex & parent ) const
463
467
QgsDataItem* item = dataItem ( parent );
464
468
// if ( item )
465
469
// QgsDebugMsg( QString( "path = %1 canFetchMore = %2" ).arg( item->path() ).arg( item && ! item->isPopulated() ) );
466
- return ( item && ! item->isPopulated () );
470
+ return ( item && item->state () == QgsDataItem::NotPopulated );
467
471
}
468
472
469
473
void QgsBrowserModel::fetchMore ( const QModelIndex & parent )
470
474
{
471
475
QgsDebugMsg ( " Entered" );
472
476
QgsDataItem* item = dataItem ( parent );
473
477
474
- if ( !item || fetching ( item ) )
478
+ if ( !item || item-> state () == QgsDataItem::Populating || item-> state () == QgsDataItem::Populated )
475
479
return ;
476
480
477
481
QgsDebugMsg ( " path = " + item->path () );
478
482
479
- if ( item->isPopulated () )
480
- return ;
481
-
482
- QList<QgsDataItem*> itemList;
483
- itemList << item;
484
- QgsBrowserWatcher * watcher = new QgsBrowserWatcher ( item );
485
- connect ( watcher, SIGNAL ( finished () ), SLOT ( childrenCreated () ) );
486
- watcher->setFuture ( QtConcurrent::mapped ( itemList, QgsBrowserModel::createChildren ) );
487
- mWatchers .append ( watcher );
488
- mLoadingMovie .setPaused ( false );
489
- emit dataChanged ( parent, parent );
483
+ item->populate ();
490
484
}
491
485
492
486
/* Refresh dir path */
@@ -500,104 +494,12 @@ void QgsBrowserModel::refresh( QString path )
500
494
void QgsBrowserModel::refresh ( const QModelIndex& theIndex )
501
495
{
502
496
QgsDataItem *item = dataItem ( theIndex );
503
- if ( !item )
497
+ if ( !item || item-> state () == QgsDataItem::Populating )
504
498
return ;
505
499
506
500
QgsDebugMsg ( " Refresh " + item->path () );
507
501
508
- QList<QgsDataItem*> itemList;
509
- itemList << item;
510
- QgsBrowserWatcher * watcher = new QgsBrowserWatcher ( item );
511
- connect ( watcher, SIGNAL ( finished () ), SLOT ( refreshChildrenCreated () ) );
512
- watcher->setFuture ( QtConcurrent::mapped ( itemList, QgsBrowserModel::createChildren ) );
513
- mWatchers .append ( watcher );
514
- mLoadingMovie .setPaused ( false );
515
- emit dataChanged ( theIndex, theIndex );
516
- }
517
-
518
- // This is expected to be run in a separate thread
519
- QVector<QgsDataItem*> QgsBrowserModel::createChildren ( QgsDataItem* item )
520
- {
521
- QgsDebugMsg ( " Entered" );
522
- QTime time;
523
- time.start ();
524
- QVector <QgsDataItem*> children = item->createChildren ();
525
- QgsDebugMsg ( QString ( " %1 children created in %2 ms" ).arg ( children.size () ).arg ( time.elapsed () ) );
526
- // Children objects must be pushed to main thread.
527
- foreach ( QgsDataItem* child, children )
528
- {
529
- if ( !child ) // should not happen
530
- continue ;
531
- // However it seems to work without resetting parent, the Qt doc says that
532
- // "The object cannot be moved if it has a parent."
533
- QgsDebugMsg ( " moveToThread child" + child->path () );
534
- child->setParent ( 0 );
535
- child->moveToThread ( QApplication::instance ()->thread () );
536
- child->setParent ( item );
537
- }
538
- return children;
539
- }
540
-
541
- void QgsBrowserModel::childrenCreated ()
542
- {
543
- QgsBrowserWatcher *watcher = dynamic_cast <QgsBrowserWatcher *>( sender () );
544
- if ( !watcher )
545
- return ;
546
- QgsDataItem* item = watcher->item ();
547
- QVector <QgsDataItem*> children = watcher->result ();
548
- QgsDebugMsg ( QString ( " path = %1 children.size() = %2" ).arg ( item->path () ).arg ( children.size () ) );
549
- QModelIndex index = findItem ( item );
550
- if ( !index.isValid () ) // check if item still exists
551
- return ;
552
- item->populate ( children );
553
- emit dataChanged ( index, index );
554
- emit fetchFinished ( index );
555
- }
556
-
557
- void QgsBrowserModel::refreshChildrenCreated ()
558
- {
559
- QgsBrowserWatcher *watcher = dynamic_cast <QgsBrowserWatcher *>( sender () );
560
- if ( !watcher )
561
- return ;
562
- QgsDataItem* item = watcher->item ();
563
- QVector <QgsDataItem*> children = watcher->result ();
564
- QgsDebugMsg ( QString ( " path = %1 children.size() = %2" ).arg ( item->path () ).arg ( children.size () ) );
565
- QModelIndex index = findItem ( item );
566
- if ( !index.isValid () ) // check if item still exists
567
- return ;
568
- item->refresh ( children );
569
- emit dataChanged ( index, index );
570
- }
571
-
572
- bool QgsBrowserModel::fetching ( QgsDataItem* item ) const
573
- {
574
- foreach ( QgsBrowserWatcher * watcher, mWatchers )
575
- {
576
- if ( !watcher->isFinished () && watcher->item () == item )
577
- return true ;
578
- }
579
- return false ;
580
- }
581
-
582
- void QgsBrowserModel::loadingFrameChanged ()
583
- {
584
- mLoadingIcon = QIcon ( mLoadingMovie .currentPixmap () );
585
- int notFinished = 0 ;
586
- foreach ( QgsBrowserWatcher * watcher, mWatchers )
587
- {
588
- if ( watcher->isFinished () )
589
- {
590
- delete watcher;
591
- mWatchers .removeOne ( watcher );
592
- continue ;
593
- }
594
- QModelIndex index = findItem ( watcher->item () );
595
- QgsDebugMsg ( QString ( " path = %1 not finished" ).arg ( watcher->item ()->path () ) );
596
- emit dataChanged ( index, index );
597
- notFinished++;
598
- }
599
- if ( notFinished == 0 )
600
- mLoadingMovie .setPaused ( true );
502
+ item->refresh ();
601
503
}
602
504
603
505
void QgsBrowserModel::addFavouriteDirectory ( QString favDir )
0 commit comments