Skip to content

Commit

Permalink
Support for project items in browser for PostgreSQL
Browse files Browse the repository at this point in the history
Switched from using URLs in mime data to URI list for drag-n-drop of projects
so that they are handled in the same way as the other browser items.
  • Loading branch information
wonder-sk committed Apr 7, 2018
1 parent fe686d3 commit 2e847e2
Show file tree
Hide file tree
Showing 12 changed files with 46 additions and 19 deletions.
3 changes: 3 additions & 0 deletions python/core/qgsdataitem.sip.in
Expand Up @@ -586,6 +586,9 @@ Data item that can be used to represent QGIS projects.

virtual bool hasDragEnabled() const;

virtual QgsMimeDataUtils::Uri mimeUri() const;


};

class QgsErrorItem : QgsDataItem
Expand Down
2 changes: 1 addition & 1 deletion python/gui/qgsbrowserdockwidget.sip.in
Expand Up @@ -116,7 +116,7 @@ Splitter has been moved
%End

signals:
void openFile( const QString & );
void openFile( const QString &fileName, const QString &fileTypeHint = QString() );
%Docstring
Emitted when a file needs to be opened
%End
Expand Down
8 changes: 6 additions & 2 deletions src/app/qgisapp.cpp
Expand Up @@ -1683,6 +1683,10 @@ void QgisApp::handleDropUriList( const QgsMimeDataUtils::UriList &lst )
}
}
}
else if ( u.layerType == QStringLiteral( "project" ) )
{
openFile( u.uri, QStringLiteral( "project" ) );
}
}
}

Expand Down Expand Up @@ -6049,11 +6053,11 @@ bool QgisApp::openLayer( const QString &fileName, bool allowInteractive )


// Open a file specified by a commandline argument, Drop or FileOpen event.
void QgisApp::openFile( const QString &fileName )
void QgisApp::openFile( const QString &fileName, const QString &fileTypeHint )
{
// check to see if we are opening a project file
QFileInfo fi( fileName );
if ( fi.suffix() == QLatin1String( "qgs" ) || fi.suffix() == QLatin1String( "qgz" ) )
if ( fileTypeHint == QStringLiteral( "project" ) || fi.suffix() == QLatin1String( "qgs" ) || fi.suffix() == QLatin1String( "qgz" ) )
{
QgsDebugMsg( "Opening project " + fileName );
openProject( fileName );
Expand Down
2 changes: 1 addition & 1 deletion src/app/qgisapp.h
Expand Up @@ -703,7 +703,7 @@ class APP_EXPORT QgisApp : public QMainWindow, private Ui::MainWindow
//! Process the list of URIs that have been dropped in QGIS
void handleDropUriList( const QgsMimeDataUtils::UriList &lst );
//! Convenience function to open either a project or a layer file.
void openFile( const QString &fileName );
void openFile( const QString &fileName, const QString &fileTypeHint = QString() );
void layerTreeViewDoubleClicked( const QModelIndex &index );
//! Make sure the insertion point for new layers is up-to-date with the current item in layer tree view
void updateNewLayerInsertionPoint();
Expand Down
10 changes: 0 additions & 10 deletions src/core/qgsbrowsermodel.cpp
Expand Up @@ -467,16 +467,6 @@ QMimeData *QgsBrowserModel::mimeData( const QModelIndexList &indexes ) const
if ( index.isValid() )
{
QgsDataItem *ptr = reinterpret_cast< 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;
}

QgsMimeDataUtils::Uri uri = ptr->mimeUri();
if ( uri.isValid() )
lst.append( uri );
Expand Down
9 changes: 9 additions & 0 deletions src/core/qgsdataitem.cpp
Expand Up @@ -1092,6 +1092,15 @@ QgsProjectItem::QgsProjectItem( QgsDataItem *parent, const QString &name, const
setState( Populated ); // no more children
}

QgsMimeDataUtils::Uri QgsProjectItem::mimeUri() const
{
QgsMimeDataUtils::Uri u;
u.layerType = QStringLiteral( "project" );
u.name = mName;
u.uri = mPath;
return u;
}

QgsErrorItem::QgsErrorItem( QgsDataItem *parent, const QString &error, const QString &path )
: QgsDataItem( QgsDataItem::Error, parent, error, path )
{
Expand Down
2 changes: 2 additions & 0 deletions src/core/qgsdataitem.h
Expand Up @@ -598,6 +598,8 @@ class CORE_EXPORT QgsProjectItem : public QgsDataItem

bool hasDragEnabled() const override { return true; }

QgsMimeDataUtils::Uri mimeUri() const override;

};

/**
Expand Down
3 changes: 2 additions & 1 deletion src/core/qgsmimedatautils.h
Expand Up @@ -63,13 +63,14 @@ class CORE_EXPORT QgsMimeDataUtils
*/
QgsRasterLayer *rasterLayer( bool &owner, QString &error ) const;

//! Type of URI. Recognized types: "vector" / "raster" / "plugin" / "custom"
//! Type of URI. Recognized types: "vector" / "raster" / "plugin" / "custom" / "project"
QString layerType;

/**
* For "vector" / "raster" type: provider id.
* For "plugin" type: plugin layer type name.
* For "custom" type: key of its QgsCustomDropHandler
* For "project" type: unused
*/
QString providerKey;
//! Human readable name to be used e.g. in layer tree
Expand Down
4 changes: 2 additions & 2 deletions src/gui/qgsbrowserdockwidget.cpp
Expand Up @@ -361,7 +361,7 @@ void QgsBrowserDockWidget::addLayerAtIndex( const QModelIndex &index )
if ( projectItem )
{
QApplication::setOverrideCursor( Qt::WaitCursor );
emit openFile( projectItem->path() );
emit openFile( projectItem->path(), QStringLiteral( "project" ) );
QApplication::restoreOverrideCursor();
}
}
Expand Down Expand Up @@ -393,7 +393,7 @@ void QgsBrowserDockWidget::addSelectedLayers()
{
QgsProjectItem *projectItem = qobject_cast<QgsProjectItem *>( item );
if ( projectItem )
emit openFile( projectItem->path() );
emit openFile( projectItem->path(), QStringLiteral( "project" ) );

QApplication::restoreOverrideCursor();
return;
Expand Down
2 changes: 1 addition & 1 deletion src/gui/qgsbrowserdockwidget.h
Expand Up @@ -100,7 +100,7 @@ class GUI_EXPORT QgsBrowserDockWidget : public QgsDockWidget, private Ui::QgsBro

signals:
//! Emitted when a file needs to be opened
void openFile( const QString & );
void openFile( const QString &fileName, const QString &fileTypeHint = QString() );
//! Emitted when drop uri list needs to be handled
void handleDropUriList( const QgsMimeDataUtils::UriList & );
//! Connections changed in the browser
Expand Down
2 changes: 1 addition & 1 deletion src/gui/qgsdatasourcemanagerdialog.h
Expand Up @@ -106,7 +106,7 @@ class GUI_EXPORT QgsDataSourceManagerDialog : public QgsOptionsDialogBase, priva
//! Emitted when a DB layer was selected for addition: for signal forwarding to QgisApp
void addDatabaseLayers( const QStringList &layerPathList, const QString &providerKey );
//! Emitted when a file needs to be opened
void openFile( const QString & );
void openFile( const QString &fileName, const QString &fileTypeHint = QString() );
//! Emitted when drop uri list needs to be handled from the browser
void handleDropUriList( const QgsMimeDataUtils::UriList & );
//! Update project home directory
Expand Down
18 changes: 18 additions & 0 deletions src/providers/postgres/qgspostgresdataitems.cpp
Expand Up @@ -16,11 +16,13 @@

#include "qgspostgresconn.h"
#include "qgspostgresconnpool.h"
#include "qgspostgresprojectstorage.h"
#include "qgscolumntypethread.h"
#include "qgslogger.h"
#include "qgsdatasourceuri.h"
#include "qgsapplication.h"
#include "qgsmessageoutput.h"
#include "qgsprojectstorageregistry.h"
#include "qgsvectorlayer.h"

#ifdef HAVE_GUI
Expand Down Expand Up @@ -573,6 +575,22 @@ QVector<QgsDataItem *> QgsPGSchemaItem::createChildren()
}

QgsPostgresConnPool::instance()->releaseConnection( conn );

if ( QgsProjectStorage *storage = QgsApplication::projectStorageRegistry()->projectStorageFromType( "postgresql" ) )
{
QgsPostgresProjectUri postUri;
postUri.connInfo = uri;
postUri.schemaName = mName;
QString schemaUri = QgsPostgresProjectStorage::encodeUri( postUri );
const QStringList projectNames = storage->listProjects( schemaUri );
for ( const QString &projectName : projectNames )
{
QgsPostgresProjectUri projectUri( postUri );
projectUri.projectName = projectName;
items.append( new QgsProjectItem( this, projectName, QgsPostgresProjectStorage::encodeUri( projectUri ) ) );
}
}

return items;
}

Expand Down

0 comments on commit 2e847e2

Please sign in to comment.