Skip to content

Commit fa0a9b7

Browse files
committedMay 12, 2013
don't reload the whole browser model when the project home changes (fixes #7520)
1 parent d8dac0c commit fa0a9b7

File tree

10 files changed

+149
-82
lines changed

10 files changed

+149
-82
lines changed
 

‎python/core/qgsbrowsermodel.sip

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,6 @@ class QgsBrowserModel : QAbstractItemModel
5454

5555
bool hasChildren( const QModelIndex &parent = QModelIndex() ) const;
5656

57-
// Reload the whole model
58-
void reload();
59-
6057
// Refresh item specified by path
6158
void refresh( QString path );
6259

@@ -68,14 +65,23 @@ class QgsBrowserModel : QAbstractItemModel
6865

6966
void connectItem( QgsDataItem *item );
7067

68+
bool canFetchMore( const QModelIndex & parent ) const;
69+
void fetchMore( const QModelIndex & parent );
70+
7171
public slots:
72+
// Reload the whole model
73+
void reload();
7274
void beginInsertItems( QgsDataItem *parent, int first, int last );
7375
void endInsertItems();
7476
void beginRemoveItems( QgsDataItem *parent, int first, int last );
7577
void endRemoveItems();
7678

77-
protected:
79+
void addFavouriteDirectory( QString favDir );
80+
void removeFavourite( const QModelIndex &index );
7881

82+
void updateProjectHome();
83+
84+
protected:
7985
// populates the model
8086
void addRootItems();
8187
void removeRootItems();

‎python/core/qgsdataitem.sip

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ class QgsDataItem : QObject
2929

3030
// Populate children using children vector created by createChildren()
3131
virtual void populate();
32+
bool isPopulated();
3233

3334
// Insert new child using alphabetical order based on mName, emits necessary signal to model before and after, sets parent and connects signals
3435
// refresh - refresh populated item, emit signals to model
@@ -212,6 +213,22 @@ class QgsErrorItem : QgsDataItem
212213
};
213214

214215

216+
// ---------
217+
218+
class CORE_EXPORT QgsDirectoryParamWidget : public QTreeWidget
219+
{
220+
Q_OBJECT
221+
222+
public:
223+
QgsDirectoryParamWidget( QString path, QWidget* parent = NULL );
224+
225+
protected:
226+
void mousePressEvent( QMouseEvent* event );
227+
228+
public slots:
229+
void showHideColumn();
230+
};
231+
215232
/** Contains various Favourites directories */
216233
class QgsFavouritesItem : QgsDataCollectionItem
217234
{
@@ -224,6 +241,9 @@ class QgsFavouritesItem : QgsDataCollectionItem
224241

225242
QVector<QgsDataItem*> createChildren();
226243

244+
void addDirectory( QString favIcon );
245+
void removeDirectory( QgsDirectoryItem *item );
246+
227247
static const QIcon &iconFavourites();
228248
};
229249

‎python/core/qgsproject.sip

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -90,13 +90,10 @@ class QgsProject : QObject
9090
- Registers maplayers
9191

9292
@note it's presumed that the caller has already reset the map canvas, map registry, and legend
93-
94-
@exception
95-
9693
*/
9794
//@{
9895
bool read( const QFileInfo & file );
99-
bool read( );
96+
bool read();
10097
//@}
10198

10299

@@ -126,7 +123,7 @@ class QgsProject : QObject
126123
*/
127124
//@{
128125
bool write( const QFileInfo & file );
129-
bool write( );
126+
bool write();
130127
//@}
131128

132129

@@ -252,7 +249,8 @@ class QgsProject : QObject
252249

253250
/**Creates a maplayer instance defined in an arbitrary project file. Caller takes ownership
254251
@return the layer or 0 in case of error
255-
@note: added in version 1.8*/
252+
@note: added in version 1.8
253+
*/
256254
/*
257255
bool createEmbeddedLayer( const QString& layerId, const QString& projectFilePath, QList<QDomNode>& brokenNodes,
258256
QList< QPair< QgsVectorLayer*, QDomElement > >& vectorLayerList, bool saveFlag = true );
@@ -276,6 +274,11 @@ class QgsProject : QObject
276274
@note added in version 1.9*/
277275
bool topologicalEditing() const;
278276

277+
/** Return project's home path
278+
@return home path of project (or QString::null if not set)
279+
@note added in version 2.0 */
280+
QString homePath() const;
281+
279282
protected:
280283

281284
/** Set error message from read/write operation
@@ -290,32 +293,34 @@ class QgsProject : QObject
290293
// bool addLayer( const QDomElement& layerElem, QList<QDomNode>& brokenNodes, QList< QPair< QgsVectorLayer*, QDomElement > >& vectorLayerList );
291294

292295
signals:
293-
294296
//! emitted when project is being read
295297
void readProject( const QDomDocument & );
296298

297299
//! emitted when project is being written
298300
void writeProject( QDomDocument & );
299301

300302
/**
301-
* Emitted, after the basic initialisation of a layer from the project
303+
* Emitted, after the basic initialisation of a layer from the project
302304
* file is done. You can use this signal to read additional information
303305
* from the project file.
304306
*
305307
* @param mapLayer The map layer which is being initialized
306308
* @param layerNode The layer node from the project file
307309
*/
308-
void readMapLayer( QgsMapLayer* mapLayer, const QDomElement& layerNode );
310+
void readMapLayer( QgsMapLayer *mapLayer, const QDomElement &layerNode );
309311

310312
/**
311313
* Emitted, when a layer is being saved. You can use this method to save
312314
* additional information to the layer.
313315
*
314316
* @param mapLayer The map layer which is being initialized
315-
* @param layerNode The layer node from the project file
317+
* @param layerElem The layer element from the project file
316318
* @param doc The document
317319
*/
318-
void writeMapLayer( QgsMapLayer* mapLayer, QDomElement& layerElem, QDomDocument& doc );
320+
void writeMapLayer( QgsMapLayer *mapLayer, QDomElement &layerElem, QDomDocument &doc );
321+
322+
//! emitted when the project file has been written and closed
323+
void projectSaved();
319324

320325
//! emitted when an old project file is read.
321326
void oldProjectVersionWarning( QString );
@@ -325,6 +330,8 @@ class QgsProject : QObject
325330
// @param n number of layers
326331
void layerLoaded( int i, int n );
327332

333+
void loadingLayer( QString );
334+
328335
void snapSettingsChanged();
329336

330337
private:

‎src/app/qgsbrowserdockwidget.cpp

Lines changed: 7 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -92,8 +92,8 @@ class QgsBrowserTreeFilterProxyModel : public QSortFilterProxyModel
9292
public:
9393

9494
QgsBrowserTreeFilterProxyModel( QObject *parent )
95-
: QSortFilterProxyModel( parent ), mModel( 0 ),
96-
mFilter( "" ), mPatternSyntax( QRegExp::Wildcard )
95+
: QSortFilterProxyModel( parent ), mModel( 0 )
96+
, mFilter( "" ), mPatternSyntax( QRegExp::Wildcard )
9797
{
9898
setDynamicSortFilter( true );
9999
}
@@ -266,10 +266,12 @@ QgsBrowserDockWidget::QgsBrowserDockWidget( QString name, QWidget * parent ) :
266266
void QgsBrowserDockWidget::showEvent( QShowEvent * e )
267267
{
268268
// delayed initialization of the model
269-
if ( mModel == NULL )
269+
if ( !mModel )
270270
{
271271
mModel = new QgsBrowserModel( mBrowserView );
272272

273+
connect( QgisApp::instance(), SIGNAL( newProject() ), mModel, SLOT( updateProjectHome() ) );
274+
273275
mProxyModel = new QgsBrowserTreeFilterProxyModel( this );
274276
mProxyModel->setBrowserModel( mModel );
275277
mBrowserView->setModel( mProxyModel );
@@ -286,10 +288,6 @@ void QgsBrowserDockWidget::showEvent( QShowEvent * e )
286288
if ( item && item->type() == QgsDataItem::Favourites )
287289
mBrowserView->expand( index );
288290
}
289-
290-
connect( QgsProject::instance(), SIGNAL( readProject( const QDomDocument & ) ), mModel, SLOT( reload() ) );
291-
connect( QgsProject::instance(), SIGNAL( writeProject( QDomDocument & ) ), mModel, SLOT( reload() ) );
292-
connect( QgisApp::instance(), SIGNAL( newProject() ), mModel, SLOT( reload() ) );
293291
}
294292

295293
QDockWidget::showEvent( e );
@@ -380,34 +378,12 @@ void QgsBrowserDockWidget::addFavouriteDirectory()
380378

381379
void QgsBrowserDockWidget::addFavouriteDirectory( QString favDir )
382380
{
383-
QSettings settings;
384-
QStringList favDirs = settings.value( "/browser/favourites" ).toStringList();
385-
favDirs.append( favDir );
386-
settings.setValue( "/browser/favourites", favDirs );
387-
388-
// reload the browser model so that the newly added favourite directory is shown
389-
mModel->reload();
381+
mModel->addFavouriteDirectory( favDir );
390382
}
391383

392384
void QgsBrowserDockWidget::removeFavourite()
393385
{
394-
QModelIndex index = mProxyModel->mapToSource( mBrowserView->currentIndex() );
395-
QgsDataItem* item = mModel->dataItem( index );
396-
397-
if ( !item )
398-
return;
399-
if ( item->type() != QgsDataItem::Directory )
400-
return;
401-
402-
QString favDir = item->path();
403-
404-
QSettings settings;
405-
QStringList favDirs = settings.value( "/browser/favourites" ).toStringList();
406-
favDirs.removeAll( favDir );
407-
settings.setValue( "/browser/favourites", favDirs );
408-
409-
// reload the browser model so that the favourite directory is not shown anymore
410-
mModel->reload();
386+
mModel->removeFavourite( mProxyModel->mapToSource( mBrowserView->currentIndex() ) );
411387
}
412388

413389
void QgsBrowserDockWidget::refresh()

‎src/app/qgsbrowserdockwidget.h

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,7 @@ class QgsBrowserDockWidget : public QDockWidget, private Ui::QgsBrowserDockWidge
3030
Q_OBJECT
3131
public:
3232
explicit QgsBrowserDockWidget( QString name, QWidget *parent = 0 );
33-
34-
signals:
33+
void addFavouriteDirectory( QString favDir );
3534

3635
public slots:
3736
void addLayerAtIndex( const QModelIndex& index );
@@ -54,16 +53,12 @@ class QgsBrowserDockWidget : public QDockWidget, private Ui::QgsBrowserDockWidge
5453
void toggleFastScan();
5554

5655
protected:
57-
void addFavouriteDirectory( QString favDir );
58-
5956
void refreshModel( const QModelIndex& index );
6057

6158
void showEvent( QShowEvent * event );
6259

6360
void addLayer( QgsLayerItem *layerItem );
6461

65-
// removed dataItem(), call mModel->dataItem directly (to avoid passing index from the wrong model)
66-
6762
QgsBrowserTreeView* mBrowserView;
6863
QgsBrowserModel* mModel;
6964
QgsBrowserTreeFilterProxyModel* mProxyModel;

‎src/core/qgsbrowsermodel.cpp

Lines changed: 48 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,13 @@
2828

2929
#include <QSettings>
3030

31-
QgsBrowserModel::QgsBrowserModel( QObject *parent ) :
32-
QAbstractItemModel( parent )
31+
QgsBrowserModel::QgsBrowserModel( QObject *parent )
32+
: QAbstractItemModel( parent )
33+
, mFavourites( 0 )
34+
, mProjectHome( 0 )
3335
{
36+
connect( QgsProject::instance(), SIGNAL( readProject( const QDomDocument & ) ), this, SLOT( updateProjectHome() ) );
37+
connect( QgsProject::instance(), SIGNAL( writeProject( QDomDocument & ) ), this, SLOT( updateProjectHome() ) );
3438
addRootItems();
3539
}
3640

@@ -39,32 +43,47 @@ QgsBrowserModel::~QgsBrowserModel()
3943
removeRootItems();
4044
}
4145

42-
void QgsBrowserModel::addRootItems()
46+
void QgsBrowserModel::updateProjectHome()
4347
{
44-
QgsDirectoryItem *item;
45-
48+
int idx = mRootItems.indexOf( mProjectHome );
4649
QString home = QgsProject::instance()->homePath();
4750

48-
if ( !home.isNull() )
51+
delete mProjectHome;
52+
53+
mProjectHome = home.isNull() ? 0 : new QgsDirectoryItem( NULL, tr( "Project home" ), home );
54+
if ( mProjectHome )
4955
{
50-
item = new QgsDirectoryItem( NULL, tr( "Project home" ), home );
51-
mRootItems << item;
56+
connectItem( mProjectHome );
57+
if ( idx < 0 )
58+
mRootItems.insert( 0, mProjectHome );
59+
else
60+
mRootItems.replace( idx, mProjectHome );
61+
}
62+
else if ( idx >= 0 )
63+
{
64+
mRootItems.remove( idx );
5265
}
66+
emit layoutChanged();
67+
}
68+
69+
void QgsBrowserModel::addRootItems()
70+
{
71+
updateProjectHome();
5372

5473
// give the home directory a prominent second place
55-
item = new QgsDirectoryItem( NULL, tr( "Home" ), QDir::homePath() );
74+
QgsDirectoryItem *item = new QgsDirectoryItem( NULL, tr( "Home" ), QDir::homePath() );
5675
QStyle *style = QApplication::style();
5776
QIcon homeIcon( style->standardPixmap( QStyle::SP_DirHomeIcon ) );
5877
item->setIcon( homeIcon );
5978
connectItem( item );
6079
mRootItems << item;
6180

6281
// add favourite directories
63-
QgsFavouritesItem *favitem = new QgsFavouritesItem( NULL, tr( "Favourites" ) );
64-
if ( favitem )
82+
mFavourites = new QgsFavouritesItem( NULL, tr( "Favourites" ) );
83+
if ( mFavourites )
6584
{
66-
connectItem( favitem );
67-
mRootItems << favitem;
85+
connectItem( mFavourites );
86+
mRootItems << mFavourites ;
6887
}
6988

7089
// add drives
@@ -107,7 +126,7 @@ void QgsBrowserModel::addRootItems()
107126
continue;
108127
}
109128

110-
dataItem_t * dataItem = ( dataItem_t * ) cast_to_fptr( library->resolve( "dataItem" ) );
129+
dataItem_t *dataItem = ( dataItem_t * ) cast_to_fptr( library->resolve( "dataItem" ) );
111130
if ( !dataItem )
112131
{
113132
QgsDebugMsg( library->fileName() + " does not have dataItem" );
@@ -426,3 +445,18 @@ void QgsBrowserModel::fetchMore( const QModelIndex & parent )
426445
item->populate();
427446
QgsDebugMsg( "path = " + item->path() );
428447
}
448+
449+
void QgsBrowserModel::addFavouriteDirectory( QString favDir )
450+
{
451+
Q_ASSERT( mFavourites );
452+
mFavourites->addDirectory( favDir );
453+
}
454+
455+
void QgsBrowserModel::removeFavourite( const QModelIndex &index )
456+
{
457+
QgsDirectoryItem *item = dynamic_cast<QgsDirectoryItem *>( dataItem( index ) );
458+
if ( !item )
459+
return;
460+
461+
mFavourites->removeDirectory( item );
462+
}

‎src/core/qgsbrowsermodel.h

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,19 +92,24 @@ class CORE_EXPORT QgsBrowserModel : public QAbstractItemModel
9292
public slots:
9393
// Reload the whole model
9494
void reload();
95-
9695
void beginInsertItems( QgsDataItem *parent, int first, int last );
9796
void endInsertItems();
9897
void beginRemoveItems( QgsDataItem *parent, int first, int last );
9998
void endRemoveItems();
10099

101-
protected:
100+
void addFavouriteDirectory( QString favDir );
101+
void removeFavourite( const QModelIndex &index );
102102

103+
void updateProjectHome();
104+
105+
protected:
103106
// populates the model
104107
void addRootItems();
105108
void removeRootItems();
106109

107110
QVector<QgsDataItem*> mRootItems;
111+
QgsFavouritesItem *mFavourites;
112+
QgsDirectoryItem *mProjectHome;
108113
};
109114

110115
#endif // QGSBROWSERMODEL_H

‎src/core/qgsdataitem.cpp

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ void QgsDataItem::populate()
190190

191191
QApplication::setOverrideCursor( Qt::WaitCursor );
192192

193-
QVector<QgsDataItem*> children = createChildren( );
193+
QVector<QgsDataItem*> children = createChildren();
194194
foreach ( QgsDataItem *child, children )
195195
{
196196
// initialization, do not refresh! That would result in infinite loop (beginInsertItems->rowCount->populate)
@@ -301,7 +301,7 @@ void QgsDataItem::refresh()
301301

302302
QApplication::setOverrideCursor( Qt::WaitCursor );
303303

304-
QVector<QgsDataItem*> items = createChildren( );
304+
QVector<QgsDataItem*> items = createChildren();
305305

306306
// Remove no more present items
307307
QVector<QgsDataItem*> remove;
@@ -448,7 +448,7 @@ QgsDirectoryItem::~QgsDirectoryItem()
448448
{
449449
}
450450

451-
QVector<QgsDataItem*> QgsDirectoryItem::createChildren( )
451+
QVector<QgsDataItem*> QgsDirectoryItem::createChildren()
452452
{
453453
QVector<QgsDataItem*> children;
454454
QDir dir( mPath );
@@ -692,17 +692,16 @@ QgsFavouritesItem::~QgsFavouritesItem()
692692
{
693693
}
694694

695-
QVector<QgsDataItem*> QgsFavouritesItem::createChildren( )
695+
QVector<QgsDataItem*> QgsFavouritesItem::createChildren()
696696
{
697697
QVector<QgsDataItem*> children;
698-
QgsDataItem* item;
699698

700699
QSettings settings;
701700
QStringList favDirs = settings.value( "/browser/favourites", QVariant() ).toStringList();
702701

703702
foreach ( QString favDir, favDirs )
704703
{
705-
item = new QgsDirectoryItem( this, favDir, favDir );
704+
QgsDataItem *item = new QgsDirectoryItem( this, favDir, favDir );
706705
if ( item )
707706
{
708707
children.append( item );
@@ -712,6 +711,29 @@ QVector<QgsDataItem*> QgsFavouritesItem::createChildren( )
712711
return children;
713712
}
714713

714+
void QgsFavouritesItem::addDirectory( QString favDir )
715+
{
716+
QSettings settings;
717+
QStringList favDirs = settings.value( "/browser/favourites" ).toStringList();
718+
favDirs.append( favDir );
719+
settings.setValue( "/browser/favourites", favDirs );
720+
721+
addChildItem( new QgsDirectoryItem( this, favDir, favDir ), true );
722+
}
723+
724+
void QgsFavouritesItem::removeDirectory( QgsDirectoryItem *item )
725+
{
726+
if ( !item )
727+
return;
728+
729+
QSettings settings;
730+
QStringList favDirs = settings.value( "/browser/favourites" ).toStringList();
731+
favDirs.removeAll( item->path() );
732+
settings.setValue( "/browser/favourites", favDirs );
733+
734+
deleteChildItem( item );
735+
}
736+
715737
//-----------------------------------------------------------------------
716738
QStringList QgsZipItem::mProviderNames = QStringList();
717739
QVector<dataItem_t *> QgsZipItem::mDataItemPtr = QVector<dataItem_t*>();
@@ -849,7 +871,7 @@ char **VSIReadDirRecursive1( const char *pszPath )
849871
return papszOFiles;
850872
}
851873

852-
QVector<QgsDataItem*> QgsZipItem::createChildren( )
874+
QVector<QgsDataItem*> QgsZipItem::createChildren()
853875
{
854876
QVector<QgsDataItem*> children;
855877
QString tmpPath;

‎src/core/qgsdataitem.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,7 @@ class CORE_EXPORT QgsDirectoryItem : public QgsDataCollectionItem
241241

242242
virtual bool equal( const QgsDataItem *other );
243243

244-
virtual QWidget * paramWidget();
244+
virtual QWidget *paramWidget();
245245

246246
/* static QVector<QgsDataProvider*> mProviders; */
247247
//! @note not available via python bindings
@@ -290,6 +290,9 @@ class CORE_EXPORT QgsFavouritesItem : public QgsDataCollectionItem
290290

291291
QVector<QgsDataItem*> createChildren();
292292

293+
void addDirectory( QString favIcon );
294+
void removeDirectory( QgsDirectoryItem *item );
295+
293296
static const QIcon &iconFavourites();
294297
};
295298

‎src/core/qgsproject.h

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ class CORE_EXPORT QgsProject : public QObject
136136
*/
137137
//@{
138138
bool read( const QFileInfo & file );
139-
bool read( );
139+
bool read();
140140
//@}
141141

142142

@@ -166,7 +166,7 @@ class CORE_EXPORT QgsProject : public QObject
166166
*/
167167
//@{
168168
bool write( const QFileInfo & file );
169-
bool write( );
169+
bool write();
170170
//@}
171171

172172

@@ -307,12 +307,12 @@ class CORE_EXPORT QgsProject : public QObject
307307
void setTopologicalEditing( bool enabled );
308308

309309
/**Convenience function to query topological editing status
310-
@note added in version 1.9*/
310+
@note added in version 1.9*/
311311
bool topologicalEditing() const;
312312

313313
/** Return project's home path
314-
@return home path of project (or QString::null if not set)
315-
@note added in version 2.0 */
314+
@return home path of project (or QString::null if not set)
315+
@note added in version 2.0 */
316316
QString homePath() const;
317317

318318
protected:
@@ -330,7 +330,6 @@ class CORE_EXPORT QgsProject : public QObject
330330
bool addLayer( const QDomElement& layerElem, QList<QDomNode>& brokenNodes, QList< QPair< QgsVectorLayer*, QDomElement > >& vectorLayerList );
331331

332332
signals:
333-
334333
//! emitted when project is being read
335334
void readProject( const QDomDocument & );
336335

0 commit comments

Comments
 (0)
Please sign in to comment.