Skip to content

Commit d84af0d

Browse files
committed
populating browser icons in threads moved to QgsDataItem
1 parent 917cee0 commit d84af0d

15 files changed

+262
-217
lines changed

python/core/qgsdataitem.sip

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ class QgsDataItem : QObject
2828
// Populate children using children vector created by createChildren()
2929
virtual void populate();
3030
bool isPopulated();
31-
void setPopulated();
3231

3332
// Insert new child using alphabetical order based on mName, emits necessary signal to model before and after, sets parent and connects signals
3433
// refresh - refresh populated item, emit signals to model

src/core/qgsbrowsermodel.cpp

Lines changed: 16 additions & 114 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,6 @@ QgsBrowserModel::QgsBrowserModel( QObject *parent )
5151
{
5252
connect( QgsProject::instance(), SIGNAL( readProject( const QDomDocument & ) ), this, SLOT( updateProjectHome() ) );
5353
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() ) );
5754
addRootItems();
5855
}
5956

@@ -232,10 +229,6 @@ QVariant QgsBrowserModel::data( const QModelIndex &index, int role ) const
232229
}
233230
else if ( role == Qt::DecorationRole && index.column() == 0 )
234231
{
235-
if ( fetching( item ) )
236-
{
237-
return mLoadingIcon;
238-
}
239232
return item->icon();
240233
}
241234
else
@@ -330,6 +323,7 @@ QModelIndex QgsBrowserModel::findPath( QString path, Qt::MatchFlag matchFlag )
330323

331324
void QgsBrowserModel::reload()
332325
{
326+
// TODO: put items creating currently children in threads to deleteLater (does not seem urget because reload() is not used in QGIS)
333327
beginResetModel();
334328
removeRootItems();
335329
addRootItems();
@@ -398,6 +392,14 @@ void QgsBrowserModel::endRemoveItems()
398392
QgsDebugMsgLevel( "Entered", 3 );
399393
endRemoveRows();
400394
}
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+
}
401403
void QgsBrowserModel::connectItem( QgsDataItem* item )
402404
{
403405
connect( item, SIGNAL( beginInsertItems( QgsDataItem*, int, int ) ),
@@ -408,6 +410,8 @@ void QgsBrowserModel::connectItem( QgsDataItem* item )
408410
this, SLOT( beginRemoveItems( QgsDataItem*, int, int ) ) );
409411
connect( item, SIGNAL( endRemoveItems() ),
410412
this, SLOT( endRemoveItems() ) );
413+
connect( item, SIGNAL( dataChanged( QgsDataItem* ) ),
414+
this, SLOT( dataItemChanged( QgsDataItem* ) ) );
411415
}
412416

413417
QStringList QgsBrowserModel::mimeTypes() const
@@ -463,30 +467,20 @@ bool QgsBrowserModel::canFetchMore( const QModelIndex & parent ) const
463467
QgsDataItem* item = dataItem( parent );
464468
// if ( item )
465469
// 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 );
467471
}
468472

469473
void QgsBrowserModel::fetchMore( const QModelIndex & parent )
470474
{
471475
QgsDebugMsg( "Entered" );
472476
QgsDataItem* item = dataItem( parent );
473477

474-
if ( !item || fetching( item ) )
478+
if ( !item || item->state() == QgsDataItem::Populating || item->state() == QgsDataItem::Populated )
475479
return;
476480

477481
QgsDebugMsg( "path = " + item->path() );
478482

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();
490484
}
491485

492486
/* Refresh dir path */
@@ -500,104 +494,12 @@ void QgsBrowserModel::refresh( QString path )
500494
void QgsBrowserModel::refresh( const QModelIndex& theIndex )
501495
{
502496
QgsDataItem *item = dataItem( theIndex );
503-
if ( !item )
497+
if ( !item || item->state() == QgsDataItem::Populating )
504498
return;
505499

506500
QgsDebugMsg( "Refresh " + item->path() );
507501

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();
601503
}
602504

603505
void QgsBrowserModel::addFavouriteDirectory( QString favDir )

src/core/qgsbrowsermodel.h

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -113,8 +113,6 @@ class CORE_EXPORT QgsBrowserModel : public QAbstractItemModel
113113

114114
bool canFetchMore( const QModelIndex & parent ) const;
115115
void fetchMore( const QModelIndex & parent );
116-
static QVector<QgsDataItem*> createChildren( QgsDataItem *item );
117-
bool fetching( QgsDataItem *item ) const;
118116

119117
signals:
120118
/** Emitted when item children fetch was finished */
@@ -127,14 +125,11 @@ class CORE_EXPORT QgsBrowserModel : public QAbstractItemModel
127125
void endInsertItems();
128126
void beginRemoveItems( QgsDataItem *parent, int first, int last );
129127
void endRemoveItems();
128+
void dataItemChanged( QgsDataItem * item );
130129

131130
void addFavouriteDirectory( QString favDir );
132131
void removeFavourite( const QModelIndex &index );
133-
134132
void updateProjectHome();
135-
void childrenCreated();
136-
void refreshChildrenCreated();
137-
void loadingFrameChanged();
138133

139134
protected:
140135
// populates the model
@@ -144,11 +139,6 @@ class CORE_EXPORT QgsBrowserModel : public QAbstractItemModel
144139
QVector<QgsDataItem*> mRootItems;
145140
QgsFavouritesItem *mFavourites;
146141
QgsDirectoryItem *mProjectHome;
147-
148-
private:
149-
QList<QgsBrowserWatcher *> mWatchers;
150-
QMovie mLoadingMovie;
151-
QIcon mLoadingIcon;
152142
};
153143

154144
#endif // QGSBROWSERMODEL_H

0 commit comments

Comments
 (0)