Skip to content

Commit

Permalink
[mesh] option to add mesh layer from data source manager dialog
Browse files Browse the repository at this point in the history
  • Loading branch information
PeterPetrik authored and wonder-sk committed Jul 13, 2018
1 parent bf56bd3 commit 4f70173
Show file tree
Hide file tree
Showing 13 changed files with 448 additions and 0 deletions.
1 change: 1 addition & 0 deletions images/images.qrc
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@
<file>themes/default/mActionDistributeTop.svg</file>
<file>themes/default/mActionDistributeVCenter.svg</file>
<file>themes/default/mActionAddLayer.svg</file>
<file>themes/default/mActionAddMeshLayer.svg</file>
<file>themes/default/mActionAddAllToOverview.svg</file>
<file>themes/default/mActionAddArrow.svg</file>
<file>themes/default/mActionAddBasicShape.svg</file>
Expand Down
106 changes: 106 additions & 0 deletions images/themes/default/mActionAddMeshLayer.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
7 changes: 7 additions & 0 deletions python/gui/auto_generated/qgsabstractdatasourcewidget.sip.in
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,13 @@ Emitted when a vector layer has been selected for addition.

If ``providerKey`` is not specified, the default provider key associated with the source
will be used.
%End

void addMeshLayer( const QString &uri, const QString &providerKey );
%Docstring
Emitted when a mesh layer has been selected for addition.

.. versionadded:: 3.4
%End

void addVectorLayers( const QStringList &layerList, const QString &encoding, const QString &dataSourceType );
Expand Down
49 changes: 49 additions & 0 deletions src/app/qgisapp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1765,6 +1765,7 @@ void QgisApp::dataSourceManager( const QString &pageName )
this, SLOT( addVectorLayer( QString const &, QString const &, QString const & ) ) );
connect( mDataSourceManagerDialog, SIGNAL( addVectorLayers( QStringList const &, QString const &, QString const & ) ),
this, SLOT( addVectorLayers( QStringList const &, QString const &, QString const & ) ) );
connect( mDataSourceManagerDialog, &QgsDataSourceManagerDialog::addMeshLayer, this, &QgisApp::addMeshLayer );
connect( mDataSourceManagerDialog, &QgsDataSourceManagerDialog::showProgress, this, &QgisApp::showProgress );
connect( mDataSourceManagerDialog, &QgsDataSourceManagerDialog::showStatusMessage, this, &QgisApp::showStatusMessage );
connect( mDataSourceManagerDialog, &QgsDataSourceManagerDialog::addDatabaseLayers, this, &QgisApp::addDatabaseLayers );
Expand Down Expand Up @@ -4484,6 +4485,54 @@ bool QgisApp::addVectorLayers( const QStringList &layerQStringList, const QStrin
return true;
} // QgisApp::addVectorLayer()


bool QgisApp::addMeshLayer( const QString &uri, const QString &providerKey )
{
bool wasfrozen = mMapCanvas->isFrozen();
QgsSettings settings;

QFileInfo fi( uri );
QString base = fi.completeBaseName();

if ( settings.value( QStringLiteral( "qgis/formatLayerName" ), false ).toBool() )
{
base = QgsMapLayer::formatLayerName( base );
}

QgsDebugMsg( "completeBaseName: " + base );

// create the layer
QgsMeshLayer::LayerOptions options;
std::unique_ptr<QgsMeshLayer> layer( new QgsMeshLayer( uri, base, providerKey, options ) );

if ( ! layer || !layer->isValid() )
{
QString msg = tr( "%1 is not a valid or recognized data source." ).arg( uri );
messageBar()->pushMessage( tr( "Invalid Data Source" ), msg, Qgis::Critical, messageTimeout() );

// since the layer is bad, stomp on it
return false;
}

// Register this layer with the layers registry
freezeCanvases();
bool ok;
layer->loadDefaultStyle( ok );
layer->loadDefaultMetadata( ok );
QgsProject::instance()->addMapLayer( layer.release() );

activateDeactivateLayerRelatedActions( activeLayer() );

// Only update the map if we frozen in this method
// Let the caller do it otherwise
if ( !wasfrozen )
{
freezeCanvases( false );
refreshMapCanvas();
}
return true;
} // QgisApp::addMeshLayer()

// present a dialog to choose zipitem layers
bool QgisApp::askUserForZipItemLayers( const QString &path )
{
Expand Down
6 changes: 6 additions & 0 deletions src/app/qgisapp.h
Original file line number Diff line number Diff line change
Expand Up @@ -922,6 +922,12 @@ class APP_EXPORT QgisApp : public QMainWindow, private Ui::MainWindow
*/
bool addRasterLayers( const QStringList &layerQStringList, bool guiWarning = true );

/**
* Adds a mesh layer directly without prompting user for location
* \returns true if successfully added layer
*/
bool addMeshLayer( const QString &uri, const QString &providerKey );

//! Open a plugin layer using its provider
QgsPluginLayer *addPluginLayer( const QString &uri, const QString &baseName, const QString &providerKey );

Expand Down
6 changes: 6 additions & 0 deletions src/gui/qgsabstractdatasourcewidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,12 @@ class GUI_EXPORT QgsAbstractDataSourceWidget : public QDialog
*/
void addVectorLayer( const QString &uri, const QString &layerName, const QString &providerKey = QString() );

/**
* Emitted when a mesh layer has been selected for addition.
* \since QGIS 3.4
*/
void addMeshLayer( const QString &uri, const QString &providerKey );

/**
* Emitted when one or more OGR supported layers are selected for addition
* \param layerList list of layers protocol URIs
Expand Down
2 changes: 2 additions & 0 deletions src/gui/qgsdatasourcemanagerdialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,8 @@ void QgsDataSourceManagerDialog::makeConnections( QgsAbstractDataSourceWidget *d
// Raster
connect( dlg, SIGNAL( addRasterLayer( QString const &, QString const &, QString const & ) ),
this, SIGNAL( addRasterLayer( QString const &, QString const &, QString const & ) ) );
// Mesh
connect( dlg, &QgsAbstractDataSourceWidget::addMeshLayer, this, &QgsDataSourceManagerDialog::addMeshLayer );

// Virtual
connect( dlg, SIGNAL( replaceVectorLayer( QString, QString, QString, QString ) ),
Expand Down
5 changes: 5 additions & 0 deletions src/gui/qgsdatasourcemanagerdialog.h
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,11 @@ class GUI_EXPORT QgsDataSourceManagerDialog : public QgsOptionsDialogBase, priva
void addRasterLayer();
//! Emitted when a vector layer was selected for addition: for signal forwarding to QgisApp
void addVectorLayer( const QString &vectorLayerPath, const QString &baseName, const QString &providerKey );
/*
* Emitted when a mesh layer was selected for addition: for signal forwarding to QgisApp\
* \since QGIS 3.4
*/
void addMeshLayer( const QString &uri, const QString &providerKey );
//! Replace the selected layer by a vector layer defined by uri, layer name, data source uri
void replaceSelectedVectorLayer( const QString &oldId, const QString &uri, const QString &layerName, const QString &provider );
//! Emitted when a one or more layer were selected for addition: for signal forwarding to QgisApp
Expand Down
19 changes: 19 additions & 0 deletions src/providers/mdal/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,14 @@ SET(MDAL_MOC_HDRS
SET(MDAL_HDRS
)

IF (WITH_GUI)
SET(MDAL_SRCS ${MDAL_SRCS}
qgsmdalsourceselect.cpp
)
SET(MDAL_MOC_HDRS ${MDAL_MOC_HDRS}
qgsmdalsourceselect.h
)
ENDIF ()

########################################################
# Compile internal MDAL
Expand Down Expand Up @@ -58,8 +66,13 @@ INCLUDE_DIRECTORIES (
${CMAKE_SOURCE_DIR}/src/core/expression
${CMAKE_SOURCE_DIR}/src/core/geometry
${CMAKE_SOURCE_DIR}/src/core/metadata
${CMAKE_SOURCE_DIR}/src/gui
${CMAKE_SOURCE_DIR}/src/gui/auth

${CMAKE_BINARY_DIR}/src/core
${CMAKE_BINARY_DIR}/src/gui
${CMAKE_BINARY_DIR}/src/ui

)

QT5_WRAP_CPP(MDAL_MOC_SRCS ${MDAL_MOC_HDRS})
Expand All @@ -70,6 +83,12 @@ TARGET_LINK_LIBRARIES (mdalprovider
${MDAL_LIBRARY}
)

IF (WITH_GUI)
TARGET_LINK_LIBRARIES (mdalprovider
qgis_gui
)
ENDIF ()

# clang-tidy
IF(CLANG_TIDY_EXE)
SET_TARGET_PROPERTIES(
Expand Down
34 changes: 34 additions & 0 deletions src/providers/mdal/qgsmdalprovider.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@

#include "qgsmdalprovider.h"

#ifdef HAVE_GUI
#include "qgssourceselectprovider.h"
#include "qgsmdalsourceselect.h"
#endif

static const QString TEXT_PROVIDER_KEY = QStringLiteral( "mdal" );
static const QString TEXT_PROVIDER_DESCRIPTION = QStringLiteral( "MDAL provider" );

Expand Down Expand Up @@ -229,3 +234,32 @@ QGISEXTERN void cleanupProvider()
{
}

#ifdef HAVE_GUI

//! Provider for mdal mesh source select
class QgsMdalMeshSourceSelectProvider : public QgsSourceSelectProvider
{
public:

QString providerKey() const override { return QStringLiteral( "mdal" ); }
QString text() const override { return QObject::tr( "Mesh" ); }
int ordering() const override { return QgsSourceSelectProvider::OrderLocalProvider + 22; }
QIcon icon() const override { return QgsApplication::getThemeIcon( QStringLiteral( "/mActionAddMeshLayer.svg" ) ); }
QgsAbstractDataSourceWidget *createDataSourceWidget( QWidget *parent = nullptr, Qt::WindowFlags fl = Qt::Widget, QgsProviderRegistry::WidgetMode widgetMode = QgsProviderRegistry::WidgetMode::Embedded ) const override
{
return new QgsMdalSourceSelect( parent, fl, widgetMode );
}
};


QGISEXTERN QList<QgsSourceSelectProvider *> *sourceSelectProviders()
{
QList<QgsSourceSelectProvider *> *providers = new QList<QgsSourceSelectProvider *>();

*providers
<< new QgsMdalMeshSourceSelectProvider;

return providers;
}

#endif
59 changes: 59 additions & 0 deletions src/providers/mdal/qgsmdalsourceselect.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/***************************************************************************
qgsmdalsourceselect.cpp
-----------------------
begin : July 2018
copyright : (C) 2018 by Peter Petrik
email : zilolv at gmail dot com
***************************************************************************/

/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/

#include <QMessageBox>

#include "qgsmdalsourceselect.h"
#include "qgsproviderregistry.h"
#include "ogr/qgsogrhelperfunctions.h"

QgsMdalSourceSelect::QgsMdalSourceSelect( QWidget *parent, Qt::WindowFlags fl, QgsProviderRegistry::WidgetMode widgetMode ):
QgsAbstractDataSourceWidget( parent, fl, widgetMode )
{
setupUi( this );
setupButtons( buttonBox );

mFileWidget->setDialogTitle( tr( "Open MDAL Supported Mesh Dataset(s)" ) );
mFileWidget->setFilter( QStringLiteral( "Mesh File (*.2dm);;All files (*.*)" ) );
mFileWidget->setStorageMode( QgsFileWidget::GetMultipleFiles );
connect( mFileWidget, &QgsFileWidget::fileChanged, this, [ = ]( const QString & path )
{
mMeshPath = path;
emit enableButtons( ! mMeshPath.isEmpty() );
} );
}

void QgsMdalSourceSelect::addButtonClicked()
{
if ( mMeshPath.isEmpty() )
{
QMessageBox::information( this,
tr( "Add mesh layer" ),
tr( "No layers selected." ) );
return;
}

for ( const QString &path : QgsFileWidget::splitFilePaths( mMeshPath ) )
{
emit addMeshLayer( path, QStringLiteral( "mdal" ) );
}
}

QGISEXTERN QgsMdalSourceSelect *selectWidget( QWidget *parent, Qt::WindowFlags fl, QgsProviderRegistry::WidgetMode widgetMode )
{
return new QgsMdalSourceSelect( parent, fl, widgetMode );
}
Loading

0 comments on commit 4f70173

Please sign in to comment.