Skip to content

Commit f79dbcd

Browse files
committed
browser restore state fix
1 parent ac10e69 commit f79dbcd

6 files changed

+85
-42
lines changed

src/app/qgsbrowserdockwidget.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,7 @@ void QgsBrowserDockWidget::showEvent( QShowEvent * e )
296296
mModel = new QgsBrowserModel( mBrowserView );
297297

298298
connect( QgisApp::instance(), SIGNAL( newProject() ), mModel, SLOT( updateProjectHome() ) );
299-
connect( mModel, SIGNAL( fetchFinished( const QModelIndex & ) ), SLOT( fetchFinished( const QModelIndex & ) ) );
299+
connect( mModel, SIGNAL( stateChanged( const QModelIndex &, QgsDataItem::State ) ), SLOT( stateChanged( const QModelIndex &, QgsDataItem::State ) ) );
300300

301301
mProxyModel = new QgsBrowserTreeFilterProxyModel( this );
302302
mProxyModel->setBrowserModel( mModel );
@@ -744,12 +744,15 @@ void QgsBrowserDockWidget::restoreState()
744744
}
745745
}
746746

747-
void QgsBrowserDockWidget::fetchFinished( const QModelIndex & index )
747+
void QgsBrowserDockWidget::stateChanged( const QModelIndex & index, QgsDataItem::State oldState )
748748
{
749749
QgsDataItem *item = mModel->dataItem( index );
750750
if ( !item )
751751
return;
752752

753+
if ( oldState != QgsDataItem::Populating || item->state() != QgsDataItem::Populated )
754+
return;
755+
753756
QgsDebugMsg( "path = " + item->path() );
754757

755758
QSettings settings;

src/app/qgsbrowserdockwidget.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
#include <QDockWidget>
1919
#include <ui_qgsbrowserdockwidgetbase.h>
2020

21+
#include "qgsdataitem.h"
22+
2123
class QgsBrowserModel;
2224
class QModelIndex;
2325
class QgsBrowserTreeView;
@@ -54,7 +56,7 @@ class APP_EXPORT QgsBrowserDockWidget : public QDockWidget, private Ui::QgsBrows
5456
void showProperties();
5557
void toggleFastScan();
5658

57-
void fetchFinished( const QModelIndex & index );
59+
void stateChanged( const QModelIndex & index, QgsDataItem::State oldState );
5860

5961
protected:
6062
void refreshModel( const QModelIndex& index );

src/core/qgsbrowsermodel.cpp

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -392,14 +392,25 @@ void QgsBrowserModel::endRemoveItems()
392392
QgsDebugMsgLevel( "Entered", 3 );
393393
endRemoveRows();
394394
}
395-
void QgsBrowserModel::dataItemChanged( QgsDataItem * item )
395+
void QgsBrowserModel::itemDataChanged( QgsDataItem * item )
396396
{
397397
QgsDebugMsgLevel( "Entered", 3 );
398398
QModelIndex idx = findItem( item );
399399
if ( !idx.isValid() )
400400
return;
401401
emit dataChanged( idx, idx );
402402
}
403+
void QgsBrowserModel::itemStateChanged( QgsDataItem * item, QgsDataItem::State oldState )
404+
{
405+
QgsDebugMsg( "Entered" );
406+
if ( !item )
407+
return;
408+
QModelIndex idx = findItem( item );
409+
if ( !idx.isValid() )
410+
return;
411+
QgsDebugMsg( QString( "item %1 state changed %2 -> %3" ).arg( item->path() ).arg( oldState ).arg( item->state() ) );
412+
emit stateChanged( idx, oldState );
413+
}
403414
void QgsBrowserModel::connectItem( QgsDataItem* item )
404415
{
405416
connect( item, SIGNAL( beginInsertItems( QgsDataItem*, int, int ) ),
@@ -411,7 +422,9 @@ void QgsBrowserModel::connectItem( QgsDataItem* item )
411422
connect( item, SIGNAL( endRemoveItems() ),
412423
this, SLOT( endRemoveItems() ) );
413424
connect( item, SIGNAL( dataChanged( QgsDataItem* ) ),
414-
this, SLOT( dataItemChanged( QgsDataItem* ) ) );
425+
this, SLOT( itemDataChanged( QgsDataItem* ) ) );
426+
connect( item, SIGNAL( stateChanged( QgsDataItem*, QgsDataItem::State ) ),
427+
this, SLOT( itemStateChanged( QgsDataItem*, QgsDataItem::State ) ) );
415428
}
416429

417430
QStringList QgsBrowserModel::mimeTypes() const

src/core/qgsbrowsermodel.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ class CORE_EXPORT QgsBrowserModel : public QAbstractItemModel
116116

117117
signals:
118118
/** Emitted when item children fetch was finished */
119-
void fetchFinished( const QModelIndex & index );
119+
void stateChanged( const QModelIndex & index, QgsDataItem::State oldState );
120120

121121
public slots:
122122
// Reload the whole model
@@ -125,7 +125,8 @@ class CORE_EXPORT QgsBrowserModel : public QAbstractItemModel
125125
void endInsertItems();
126126
void beginRemoveItems( QgsDataItem *parent, int first, int last );
127127
void endRemoveItems();
128-
void dataItemChanged( QgsDataItem * item );
128+
void itemDataChanged( QgsDataItem * item );
129+
void itemStateChanged( QgsDataItem * item, QgsDataItem::State oldState );
129130

130131
void addFavouriteDirectory( QString favDir );
131132
void removeFavourite( const QModelIndex &index );

src/core/qgsdataitem.cpp

Lines changed: 49 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -151,9 +151,9 @@ const QIcon &QgsZipItem::iconZip()
151151

152152
QMap<QString, QIcon> QgsDataItem::mIconMap = QMap<QString, QIcon>();
153153

154-
int QgsDataItem::mLoadingCount = 0;
155-
QMovie * QgsDataItem::mLoadingMovie = 0;
156-
QIcon QgsDataItem::mLoadingIcon = QIcon();
154+
int QgsDataItem::mPopulatingCount = 0;
155+
QMovie * QgsDataItem::mPopulatingMovie = 0;
156+
QIcon QgsDataItem::mPopulatingIcon = QIcon();
157157

158158
QgsDataItem::QgsDataItem( QgsDataItem::Type type, QgsDataItem* parent, QString name, QString path )
159159
// Do not pass parent to QObject, Qt would delete this when parent is deleted
@@ -177,7 +177,7 @@ QgsDataItem::~QgsDataItem()
177177
QIcon QgsDataItem::icon()
178178
{
179179
if ( state() == Populating )
180-
return mLoadingIcon;
180+
return mPopulatingIcon;
181181

182182
if ( !mIcon.isNull() )
183183
return mIcon;
@@ -215,6 +215,14 @@ void QgsDataItem::emitDataChanged()
215215
emit dataChanged( this );
216216
}
217217

218+
void QgsDataItem::emitStateChanged( QgsDataItem* item, QgsDataItem::State oldState )
219+
{
220+
if ( !item )
221+
return;
222+
QgsDebugMsg( QString( "item %1 state changed %2 -> %3" ).arg( item->path() ).arg( oldState ).arg( item->state() ) );
223+
emit stateChanged( item, oldState );
224+
}
225+
218226
QVector<QgsDataItem*> QgsDataItem::createChildren()
219227
{
220228
return QVector<QgsDataItem*>();
@@ -248,10 +256,10 @@ void QgsDataItem::populate()
248256
QVector<QgsDataItem*> QgsDataItem::runCreateChildren( QgsDataItem* item )
249257
{
250258
QgsDebugMsg( "path = " + item->path() );
251-
//QTime time;
252-
//time.start();
259+
QTime time;
260+
time.start();
253261
QVector <QgsDataItem*> children = item->createChildren();
254-
//QgsDebugMsg( QString( "%1 children created in %2 ms" ).arg( children.size() ).arg( time.elapsed() ) );
262+
QgsDebugMsg( QString( "%1 children created in %2 ms" ).arg( children.size() ).arg( time.elapsed() ) );
255263
// Children objects must be pushed to main thread.
256264
foreach ( QgsDataItem* child, children )
257265
{
@@ -425,6 +433,8 @@ void QgsDataItem::addChildItem( QgsDataItem * child, bool refresh )
425433
this, SLOT( emitEndRemoveItems() ) );
426434
connect( child, SIGNAL( dataChanged( QgsDataItem* ) ),
427435
this, SLOT( emitDataChanged( QgsDataItem* ) ) );
436+
connect( child, SIGNAL( stateChanged( QgsDataItem*, QgsDataItem::State ) ),
437+
this, SLOT( emitStateChanged( QgsDataItem*, QgsDataItem::State ) ) );
428438

429439
if ( refresh )
430440
emit endInsertItems();
@@ -458,6 +468,8 @@ QgsDataItem * QgsDataItem::removeChildItem( QgsDataItem * child )
458468
this, SLOT( emitEndRemoveItems() ) );
459469
disconnect( child, SIGNAL( dataChanged( QgsDataItem* ) ),
460470
this, SLOT( emitDataChanged( QgsDataItem* ) ) );
471+
disconnect( child, SIGNAL( stateChanged( QgsDataItem*, QgsDataItem::State ) ),
472+
this, SLOT( emitStateChanged( QgsDataItem*, QgsDataItem::State ) ) );
461473
child->setParent( 0 );
462474
return child;
463475
}
@@ -483,9 +495,9 @@ bool QgsDataItem::equal( const QgsDataItem *other )
483495
return false;
484496
}
485497

486-
void QgsDataItem::setLoadingIcon()
498+
void QgsDataItem::setPopulatingIcon()
487499
{
488-
mLoadingIcon = QIcon( mLoadingMovie->currentPixmap() );
500+
mPopulatingIcon = QIcon( mPopulatingMovie->currentPixmap() );
489501
}
490502

491503
QgsDataItem::State QgsDataItem::state() const
@@ -499,37 +511,42 @@ QgsDataItem::State QgsDataItem::state() const
499511

500512
void QgsDataItem::setState( State state )
501513
{
514+
QgsDebugMsg( QString( "item %1 set state %2 -> %3" ).arg( path() ).arg( this->state() ).arg( state ) );
502515
if ( state == mState )
503516
return;
504517

518+
State oldState = mState;
519+
505520
if ( state == Populating ) // start loading
506521
{
507-
if ( !mLoadingMovie )
522+
if ( !mPopulatingMovie )
508523
{
509524
// QApplication as parent to ensure that it is deleted before QApplication
510-
mLoadingMovie = new QMovie( QApplication::instance() );
511-
mLoadingMovie->setFileName( QgsApplication::iconPath( "/mIconLoading.gif" ) );
512-
mLoadingMovie->setCacheMode( QMovie::CacheAll );
513-
connect( mLoadingMovie, SIGNAL( frameChanged( int ) ), SLOT( setLoadingIcon() ) );
525+
mPopulatingMovie = new QMovie( QApplication::instance() );
526+
mPopulatingMovie->setFileName( QgsApplication::iconPath( "/mIconLoading.gif" ) );
527+
mPopulatingMovie->setCacheMode( QMovie::CacheAll );
528+
connect( mPopulatingMovie, SIGNAL( frameChanged( int ) ), SLOT( setPopulatingIcon() ) );
514529
}
515-
connect( mLoadingMovie, SIGNAL( frameChanged( int ) ), SLOT( emitDataChanged() ) );
516-
mLoadingCount++;
517-
mLoadingMovie->setPaused( false );
530+
connect( mPopulatingMovie, SIGNAL( frameChanged( int ) ), SLOT( emitDataChanged() ) );
531+
mPopulatingCount++;
532+
mPopulatingMovie->setPaused( false );
518533
}
519-
else if ( mState == Populating && mLoadingMovie ) // stop loading
534+
else if ( mState == Populating && mPopulatingMovie ) // stop loading
520535
{
521-
disconnect( mLoadingMovie, SIGNAL( frameChanged( int ) ), this, SLOT( emitDataChanged() ) );
522-
mLoadingCount--;
523-
if ( mLoadingCount == 0 )
536+
disconnect( mPopulatingMovie, SIGNAL( frameChanged( int ) ), this, SLOT( emitDataChanged() ) );
537+
mPopulatingCount--;
538+
if ( mPopulatingCount == 0 )
524539
{
525-
mLoadingMovie->setPaused( true );
540+
mPopulatingMovie->setPaused( true );
526541
}
527542
}
528543

529544
mState = state;
530545
// for backward compatibility (if subclass access mPopulated directly)
531546
// TODO: remove in 3.0
532547
mPopulated = state == Populated;
548+
549+
emit stateChanged( this, oldState );
533550
}
534551

535552
// ---------------------------------------------------------------------
@@ -657,6 +674,8 @@ QgsDirectoryItem::~QgsDirectoryItem()
657674

658675
QIcon QgsDirectoryItem::icon()
659676
{
677+
if ( state() == Populating )
678+
return populatingIcon();
660679
return iconDir();
661680
}
662681

@@ -969,13 +988,13 @@ QVector<dataItem_t *> QgsZipItem::mDataItemPtr = QVector<dataItem_t*>();
969988
QgsZipItem::QgsZipItem( QgsDataItem* parent, QString name, QString path )
970989
: QgsDataCollectionItem( parent, name, path )
971990
{
972-
mDirPath = path;
991+
mFilePath = path;
973992
init();
974993
}
975994

976-
QgsZipItem::QgsZipItem( QgsDataItem* parent, QString name, QString dirPath, QString path )
995+
QgsZipItem::QgsZipItem( QgsDataItem* parent, QString name, QString filePath, QString path )
977996
: QgsDataCollectionItem( parent, name, path )
978-
, mDirPath( dirPath )
997+
, mFilePath( filePath )
979998
{
980999
init();
9811000
}
@@ -984,7 +1003,7 @@ void QgsZipItem::init()
9841003
{
9851004
mType = Collection; //Zip??
9861005
mIconName = "/mIconZip.png";
987-
mVsiPrefix = vsiPrefix( mDirPath );
1006+
mVsiPrefix = vsiPrefix( mFilePath );
9881007

9891008
if ( mProviderNames.size() == 0 )
9901009
{
@@ -1137,7 +1156,7 @@ QVector<QgsDataItem*> QgsZipItem::createChildren()
11371156
foreach ( QString fileName, mZipFileList )
11381157
{
11391158
QFileInfo info( fileName );
1140-
tmpPath = mVsiPrefix + path() + "/" + fileName;
1159+
tmpPath = mVsiPrefix + mFilePath + "/" + fileName;
11411160
QgsDebugMsgLevel( "tmpPath = " + tmpPath, 3 );
11421161

11431162
// foreach( dataItem_t *dataItem, mDataItemPtr )
@@ -1295,7 +1314,7 @@ const QStringList & QgsZipItem::getZipFileList()
12951314
QSettings settings;
12961315
QString scanZipSetting = settings.value( "/qgis/scanZipInBrowser2", "basic" ).toString();
12971316

1298-
QgsDebugMsgLevel( QString( "mDirPath = %1 name= %2 scanZipSetting= %3 vsiPrefix= %4" ).arg( mDirPath ).arg( name() ).arg( scanZipSetting ).arg( mVsiPrefix ), 3 );
1317+
QgsDebugMsgLevel( QString( "mFIlePath = %1 name= %2 scanZipSetting= %3 vsiPrefix= %4" ).arg( mFilePath ).arg( name() ).arg( scanZipSetting ).arg( mVsiPrefix ), 3 );
12991318

13001319
// if scanZipBrowser == no: skip to the next file
13011320
if ( scanZipSetting == "no" )
@@ -1305,7 +1324,7 @@ const QStringList & QgsZipItem::getZipFileList()
13051324

13061325
// get list of files inside zip file
13071326
QgsDebugMsgLevel( QString( "Open file %1 with gdal vsi" ).arg( mVsiPrefix + path() ), 3 );
1308-
char **papszSiblingFiles = VSIReadDirRecursive1( QString( mVsiPrefix + mDirPath ).toLocal8Bit().constData() );
1327+
char **papszSiblingFiles = VSIReadDirRecursive1( QString( mVsiPrefix + mFilePath ).toLocal8Bit().constData() );
13091328
if ( papszSiblingFiles )
13101329
{
13111330
for ( int i = 0; i < CSLCount( papszSiblingFiles ); i++ )
@@ -1320,7 +1339,7 @@ const QStringList & QgsZipItem::getZipFileList()
13201339
}
13211340
else
13221341
{
1323-
QgsDebugMsg( QString( "Error reading %1" ).arg( mDirPath ) );
1342+
QgsDebugMsg( QString( "Error reading %1" ).arg( mFilePath ) );
13241343
}
13251344

13261345
return mZipFileList;

src/core/qgsdataitem.h

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ typedef QgsDataItem * dataItem_t( QString, QgsDataItem* );
4141
class CORE_EXPORT QgsDataItem : public QObject
4242
{
4343
Q_OBJECT
44+
Q_ENUMS( Type )
45+
Q_ENUMS( State )
4446
public:
4547
enum Type
4648
{
@@ -157,6 +159,7 @@ class CORE_EXPORT QgsDataItem : public QObject
157159
protected:
158160
virtual void populate( QVector<QgsDataItem*> children );
159161
virtual void refresh( QVector<QgsDataItem*> children );
162+
QIcon populatingIcon() { return mPopulatingIcon; }
160163

161164
Type mType;
162165
Capabilities mCapabilities;
@@ -183,24 +186,26 @@ class CORE_EXPORT QgsDataItem : public QObject
183186
void emitEndRemoveItems();
184187
void emitDataChanged( QgsDataItem* item );
185188
void emitDataChanged( );
189+
void emitStateChanged( QgsDataItem* item, QgsDataItem::State oldState );
186190
void childrenCreated();
187-
void setLoadingIcon();
191+
void setPopulatingIcon();
188192

189193
signals:
190194
void beginInsertItems( QgsDataItem* parent, int first, int last );
191195
void endInsertItems();
192196
void beginRemoveItems( QgsDataItem* parent, int first, int last );
193197
void endRemoveItems();
194198
void dataChanged( QgsDataItem * item );
199+
void stateChanged( QgsDataItem * item, QgsDataItem::State oldState );
195200

196201
private:
197202
static QVector<QgsDataItem*> runCreateChildren( QgsDataItem* item );
198203

199204
QFutureWatcher< QVector <QgsDataItem*> > *mWatcher;
200205
// number of items currently in loading (populating) state
201-
static int mLoadingCount;
202-
static QMovie * mLoadingMovie;
203-
static QIcon mLoadingIcon;
206+
static int mPopulatingCount;
207+
static QMovie * mPopulatingMovie;
208+
static QIcon mPopulatingIcon;
204209
};
205210

206211
Q_DECLARE_OPERATORS_FOR_FLAGS( QgsDataItem::Capabilities )
@@ -366,7 +371,7 @@ class CORE_EXPORT QgsZipItem : public QgsDataCollectionItem
366371
Q_OBJECT
367372

368373
protected:
369-
QString mDirPath;
374+
QString mFilePath;
370375
QString mVsiPrefix;
371376
QStringList mZipFileList;
372377

0 commit comments

Comments
 (0)