Skip to content

Commit ffd7c94

Browse files
committed
[FEATURE] Add project loading support from browser
1 parent 19dcd21 commit ffd7c94

File tree

5 files changed

+87
-2
lines changed

5 files changed

+87
-2
lines changed

python/core/qgsdataitem.sip

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ class QgsDataItem : QObject
4747
sipType = sipType_QgsZipItem;
4848
else if (qobject_cast<QgsDataCollectionItem*>(sipCpp))
4949
sipType = sipType_QgsDataCollectionItem;
50+
else if (qobject_cast<QgsProjectItem*>(sipCpp))
51+
sipType = sipType_QgsProjectItem;
5052
else
5153
sipType = 0;
5254
%End
@@ -324,6 +326,21 @@ class QgsDirectoryItem : QgsDataCollectionItem
324326
// static QVector<QLibrary*> mLibraries;
325327
};
326328

329+
/**
330+
Data item that can be used to represent QGIS projects.
331+
*/
332+
class QgsProjectItem : QgsDataItem
333+
{
334+
%TypeHeaderCode
335+
#include <qgsdataitem.h>
336+
%End
337+
public:
338+
339+
QgsProjectItem( QgsDataItem* parent, const QString& name, const QString& path );
340+
~QgsProjectItem();
341+
342+
};
343+
327344
/**
328345
Data item that can be used to report problems (e.g. network error)
329346
*/

src/app/qgsbrowserdockwidget.cpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -541,6 +541,16 @@ void QgsBrowserDockWidget::addLayerAtIndex( const QModelIndex& index )
541541
QgsDebugMsg( QString( "rowCount() = %1" ).arg( mModel->rowCount( mProxyModel->mapToSource( index ) ) ) );
542542
QgsDataItem *item = mModel->dataItem( mProxyModel->mapToSource( index ) );
543543

544+
if ( item != NULL && item->type() == QgsDataItem::Project )
545+
{
546+
QgsProjectItem *projectItem = qobject_cast<QgsProjectItem*>( item );
547+
if ( projectItem != NULL )
548+
{
549+
QApplication::setOverrideCursor( Qt::WaitCursor );
550+
QgisApp::instance()->openFile( projectItem->path() );
551+
QApplication::restoreOverrideCursor();
552+
}
553+
}
544554
if ( item != NULL && item->type() == QgsDataItem::Layer )
545555
{
546556
QgsLayerItem *layerItem = qobject_cast<QgsLayerItem*>( item );
@@ -566,6 +576,21 @@ void QgsBrowserDockWidget::addSelectedLayers()
566576
QModelIndexList list = mBrowserView->selectionModel()->selectedIndexes();
567577
qSort( list );
568578

579+
// If any of the layer items are QGIS we just open and exit the loop
580+
for ( int i = 0; i <= list.size(); i++ )
581+
{
582+
QgsDataItem *item = mModel->dataItem( mProxyModel->mapToSource( list[i] ) );
583+
if ( item && item->type() == QgsDataItem::Project )
584+
{
585+
QgsProjectItem *projectItem = qobject_cast<QgsProjectItem*>( item );
586+
if ( projectItem )
587+
QgisApp::instance()->openFile( projectItem->path() );
588+
589+
QApplication::restoreOverrideCursor();
590+
return;
591+
}
592+
}
593+
569594
// add items in reverse order so they are in correct order in the layers dock
570595
for ( int i = list.size() - 1; i >= 0; i-- )
571596
{

src/core/qgsbrowsermodel.cpp

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include <QApplication>
1717
#include <QStyle>
1818
#include <QtConcurrentMap>
19+
#include <QUrl>
1920

2021
#include "qgis.h"
2122
#include "qgsapplication.h"
@@ -185,7 +186,7 @@ Qt::ItemFlags QgsBrowserModel::flags( const QModelIndex & index ) const
185186
Qt::ItemFlags flags = Qt::ItemIsEnabled | Qt::ItemIsSelectable;
186187

187188
QgsDataItem* ptr = ( QgsDataItem* ) index.internalPointer();
188-
if ( ptr->type() == QgsDataItem::Layer )
189+
if ( ptr->type() == QgsDataItem::Layer || ptr->type() == QgsDataItem::Project )
189190
{
190191
flags |= Qt::ItemIsDragEnabled;
191192
}
@@ -448,6 +449,16 @@ QMimeData * QgsBrowserModel::mimeData( const QModelIndexList &indexes ) const
448449
if ( index.isValid() )
449450
{
450451
QgsDataItem* ptr = ( QgsDataItem* ) index.internalPointer();
452+
if ( ptr->type() == QgsDataItem::Project )
453+
{
454+
QMimeData *mimeData = new QMimeData();
455+
QUrl url = QUrl::fromLocalFile( ptr->path() );
456+
QList<QUrl> urls;
457+
urls << url;
458+
mimeData->setUrls( urls );
459+
return mimeData;
460+
}
461+
451462
if ( ptr->type() != QgsDataItem::Layer ) continue;
452463
QgsLayerItem *layer = ( QgsLayerItem* ) ptr;
453464
lst.append( QgsMimeDataUtils::Uri( layer ) );

src/core/qgsdataitem.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -813,6 +813,13 @@ QVector<QgsDataItem*> QgsDirectoryItem::createChildren()
813813
QString path = dir.absoluteFilePath( name );
814814
QFileInfo fileInfo( path );
815815

816+
if ( fileInfo.suffix() == "qgs" )
817+
{
818+
QgsDataItem * item = new QgsProjectItem( this, name, path );
819+
children.append( item );
820+
continue;
821+
}
822+
816823
// vsizip support was added to GDAL/OGR 1.6 but GDAL_VERSION_NUM not available here
817824
// so we assume it's available anyway
818825
{
@@ -1064,6 +1071,17 @@ void QgsDirectoryParamWidget::showHideColumn()
10641071
settings.setValue( "/dataitem/directoryHiddenColumns", lst );
10651072
}
10661073

1074+
QgsProjectItem::QgsProjectItem( QgsDataItem* parent, const QString &name, const QString& path )
1075+
: QgsDataItem( QgsDataItem::Project, parent, name, path )
1076+
{
1077+
mIconName = ":/images/icons/qgis-icon-16x16.png";
1078+
1079+
setState( Populated ); // no more children
1080+
}
1081+
1082+
QgsProjectItem::~QgsProjectItem()
1083+
{
1084+
}
10671085

10681086
QgsErrorItem::QgsErrorItem( QgsDataItem* parent, const QString& error, const QString& path )
10691087
: QgsDataItem( QgsDataItem::Error, parent, error, path )

src/core/qgsdataitem.h

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,8 @@ class CORE_EXPORT QgsDataItem : public QObject
8484
Directory,
8585
Layer,
8686
Error,
87-
Favourites
87+
Favourites,
88+
Project
8889
};
8990

9091
/** Create new data item. */
@@ -424,6 +425,19 @@ class CORE_EXPORT QgsDirectoryItem : public QgsDataCollectionItem
424425
bool mRefreshLater;
425426
};
426427

428+
/**
429+
Data item that can be used to represent QGIS projects.
430+
*/
431+
class CORE_EXPORT QgsProjectItem : public QgsDataItem
432+
{
433+
Q_OBJECT
434+
public:
435+
436+
QgsProjectItem( QgsDataItem* parent, const QString& name, const QString& path );
437+
~QgsProjectItem();
438+
439+
};
440+
427441
/**
428442
Data item that can be used to report problems (e.g. network error)
429443
*/

0 commit comments

Comments
 (0)