Skip to content

Commit

Permalink
[FEATURE] Add project loading support from browser
Browse files Browse the repository at this point in the history
  • Loading branch information
NathanW2 committed Dec 11, 2015
1 parent 19dcd21 commit ffd7c94
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 2 deletions.
17 changes: 17 additions & 0 deletions python/core/qgsdataitem.sip
Expand Up @@ -47,6 +47,8 @@ class QgsDataItem : QObject
sipType = sipType_QgsZipItem;
else if (qobject_cast<QgsDataCollectionItem*>(sipCpp))
sipType = sipType_QgsDataCollectionItem;
else if (qobject_cast<QgsProjectItem*>(sipCpp))
sipType = sipType_QgsProjectItem;
else
sipType = 0;
%End
Expand Down Expand Up @@ -324,6 +326,21 @@ class QgsDirectoryItem : QgsDataCollectionItem
// static QVector<QLibrary*> mLibraries;
};

/**
Data item that can be used to represent QGIS projects.
*/
class QgsProjectItem : QgsDataItem
{
%TypeHeaderCode
#include <qgsdataitem.h>
%End
public:

QgsProjectItem( QgsDataItem* parent, const QString& name, const QString& path );
~QgsProjectItem();

};

/**
Data item that can be used to report problems (e.g. network error)
*/
Expand Down
25 changes: 25 additions & 0 deletions src/app/qgsbrowserdockwidget.cpp
Expand Up @@ -541,6 +541,16 @@ void QgsBrowserDockWidget::addLayerAtIndex( const QModelIndex& index )
QgsDebugMsg( QString( "rowCount() = %1" ).arg( mModel->rowCount( mProxyModel->mapToSource( index ) ) ) );
QgsDataItem *item = mModel->dataItem( mProxyModel->mapToSource( index ) );

if ( item != NULL && item->type() == QgsDataItem::Project )
{
QgsProjectItem *projectItem = qobject_cast<QgsProjectItem*>( item );
if ( projectItem != NULL )
{
QApplication::setOverrideCursor( Qt::WaitCursor );
QgisApp::instance()->openFile( projectItem->path() );
QApplication::restoreOverrideCursor();
}
}
if ( item != NULL && item->type() == QgsDataItem::Layer )
{
QgsLayerItem *layerItem = qobject_cast<QgsLayerItem*>( item );
Expand All @@ -566,6 +576,21 @@ void QgsBrowserDockWidget::addSelectedLayers()
QModelIndexList list = mBrowserView->selectionModel()->selectedIndexes();
qSort( list );

// If any of the layer items are QGIS we just open and exit the loop
for ( int i = 0; i <= list.size(); i++ )
{
QgsDataItem *item = mModel->dataItem( mProxyModel->mapToSource( list[i] ) );
if ( item && item->type() == QgsDataItem::Project )
{
QgsProjectItem *projectItem = qobject_cast<QgsProjectItem*>( item );
if ( projectItem )
QgisApp::instance()->openFile( projectItem->path() );

QApplication::restoreOverrideCursor();
return;
}
}

// add items in reverse order so they are in correct order in the layers dock
for ( int i = list.size() - 1; i >= 0; i-- )
{
Expand Down
13 changes: 12 additions & 1 deletion src/core/qgsbrowsermodel.cpp
Expand Up @@ -16,6 +16,7 @@
#include <QApplication>
#include <QStyle>
#include <QtConcurrentMap>
#include <QUrl>

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

QgsDataItem* ptr = ( QgsDataItem* ) index.internalPointer();
if ( ptr->type() == QgsDataItem::Layer )
if ( ptr->type() == QgsDataItem::Layer || ptr->type() == QgsDataItem::Project )
{
flags |= Qt::ItemIsDragEnabled;
}
Expand Down Expand Up @@ -448,6 +449,16 @@ QMimeData * QgsBrowserModel::mimeData( const QModelIndexList &indexes ) const
if ( index.isValid() )
{
QgsDataItem* ptr = ( QgsDataItem* ) index.internalPointer();
if ( ptr->type() == QgsDataItem::Project )
{
QMimeData *mimeData = new QMimeData();
QUrl url = QUrl::fromLocalFile( ptr->path() );
QList<QUrl> urls;
urls << url;
mimeData->setUrls( urls );
return mimeData;
}

if ( ptr->type() != QgsDataItem::Layer ) continue;
QgsLayerItem *layer = ( QgsLayerItem* ) ptr;
lst.append( QgsMimeDataUtils::Uri( layer ) );
Expand Down
18 changes: 18 additions & 0 deletions src/core/qgsdataitem.cpp
Expand Up @@ -813,6 +813,13 @@ QVector<QgsDataItem*> QgsDirectoryItem::createChildren()
QString path = dir.absoluteFilePath( name );
QFileInfo fileInfo( path );

if ( fileInfo.suffix() == "qgs" )
{
QgsDataItem * item = new QgsProjectItem( this, name, path );
children.append( item );
continue;
}

// vsizip support was added to GDAL/OGR 1.6 but GDAL_VERSION_NUM not available here
// so we assume it's available anyway
{
Expand Down Expand Up @@ -1064,6 +1071,17 @@ void QgsDirectoryParamWidget::showHideColumn()
settings.setValue( "/dataitem/directoryHiddenColumns", lst );
}

QgsProjectItem::QgsProjectItem( QgsDataItem* parent, const QString &name, const QString& path )
: QgsDataItem( QgsDataItem::Project, parent, name, path )
{
mIconName = ":/images/icons/qgis-icon-16x16.png";

setState( Populated ); // no more children
}

QgsProjectItem::~QgsProjectItem()
{
}

QgsErrorItem::QgsErrorItem( QgsDataItem* parent, const QString& error, const QString& path )
: QgsDataItem( QgsDataItem::Error, parent, error, path )
Expand Down
16 changes: 15 additions & 1 deletion src/core/qgsdataitem.h
Expand Up @@ -84,7 +84,8 @@ class CORE_EXPORT QgsDataItem : public QObject
Directory,
Layer,
Error,
Favourites
Favourites,
Project
};

/** Create new data item. */
Expand Down Expand Up @@ -424,6 +425,19 @@ class CORE_EXPORT QgsDirectoryItem : public QgsDataCollectionItem
bool mRefreshLater;
};

/**
Data item that can be used to represent QGIS projects.
*/
class CORE_EXPORT QgsProjectItem : public QgsDataItem
{
Q_OBJECT
public:

QgsProjectItem( QgsDataItem* parent, const QString& name, const QString& path );
~QgsProjectItem();

};

/**
Data item that can be used to report problems (e.g. network error)
*/
Expand Down

0 comments on commit ffd7c94

Please sign in to comment.