Skip to content

Commit

Permalink
Implement export action
Browse files Browse the repository at this point in the history
  • Loading branch information
pblottiere committed Oct 9, 2017
1 parent bca8973 commit dcec98d
Show file tree
Hide file tree
Showing 7 changed files with 86 additions and 11 deletions.
9 changes: 9 additions & 0 deletions python/core/qgsauxiliarystorage.sip
Expand Up @@ -92,6 +92,15 @@ class QgsAuxiliaryLayer : QgsVectorLayer



QgsVectorLayer *toSpatialLayer() const;
%Docstring
An auxiliary layer is not spatial. This method returns a spatial
representation of auxiliary data.

:return: A new spatial vector layer
:rtype: QgsVectorLayer
%End

bool clear();
%Docstring
Deletes all features from the layer. Changes are automatically committed
Expand Down
18 changes: 11 additions & 7 deletions src/app/qgisapp.cpp
Expand Up @@ -1856,7 +1856,7 @@ void QgisApp::createActions()
connect( mActionRollbackAllEdits, &QAction::triggered, this, &QgisApp::rollbackAllEdits );
connect( mActionCancelEdits, &QAction::triggered, this, [ = ] { cancelEdits(); } );
connect( mActionCancelAllEdits, &QAction::triggered, this, &QgisApp::cancelAllEdits );
connect( mActionLayerSaveAs, &QAction::triggered, this, &QgisApp::saveAsFile );
connect( mActionLayerSaveAs, &QAction::triggered, this, [ = ] { saveAsFile(); } );
connect( mActionSaveLayerDefinition, &QAction::triggered, this, &QgisApp::saveAsLayerDefinition );
connect( mActionRemoveLayer, &QAction::triggered, this, &QgisApp::removeLayer );
connect( mActionDuplicateLayer, &QAction::triggered, this, [ = ] { duplicateLayers(); } );
Expand Down Expand Up @@ -6442,9 +6442,11 @@ void QgisApp::attributeTable()
// the dialog will be deleted by itself on close
}

void QgisApp::saveAsRasterFile()
void QgisApp::saveAsRasterFile( QgsRasterLayer *rasterLayer )
{
QgsRasterLayer *rasterLayer = qobject_cast<QgsRasterLayer *>( activeLayer() );
if ( !rasterLayer )
rasterLayer = qobject_cast<QgsRasterLayer *>( activeLayer() );

if ( !rasterLayer )
{
return;
Expand Down Expand Up @@ -6578,20 +6580,22 @@ void QgisApp::saveAsRasterFile()
}


void QgisApp::saveAsFile()
void QgisApp::saveAsFile( QgsMapLayer *layer )
{
QgsMapLayer *layer = activeLayer();
if ( !layer )
layer = activeLayer();

if ( !layer )
return;

QgsMapLayer::LayerType layerType = layer->type();
if ( layerType == QgsMapLayer::RasterLayer )
{
saveAsRasterFile();
saveAsRasterFile( qobject_cast<QgsRasterLayer *>( layer ) );
}
else if ( layerType == QgsMapLayer::VectorLayer )
{
saveAsVectorFileGeneral();
saveAsVectorFileGeneral( qobject_cast<QgsVectorLayer *>( layer ) );
}
}

Expand Down
8 changes: 4 additions & 4 deletions src/app/qgisapp.h
Expand Up @@ -654,6 +654,9 @@ class APP_EXPORT QgisApp : public QMainWindow, private Ui::MainWindow
QSize iconSize( bool dockedToolbar = false ) const;

public slots:
//! save current vector layer
void saveAsFile( QgsMapLayer *layer = nullptr );

//! 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.
Expand Down Expand Up @@ -1439,13 +1442,10 @@ class APP_EXPORT QgisApp : public QMainWindow, private Ui::MainWindow
//! set the CAD dock widget visible
void setCadDockVisible( bool visible );

//! save current vector layer
void saveAsFile();

void saveAsLayerDefinition();

//! save current raster layer
void saveAsRasterFile();
void saveAsRasterFile( QgsRasterLayer *layer = nullptr );

//! show Python console
void showPythonDialog();
Expand Down
19 changes: 19 additions & 0 deletions src/app/qgsvectorlayerproperties.cpp
Expand Up @@ -86,6 +86,7 @@ QgsVectorLayerProperties::QgsVectorLayerProperties(
, mAuxiliaryLayerActionNew( nullptr )
, mAuxiliaryLayerActionClear( nullptr )
, mAuxiliaryLayerActionDelete( nullptr )
, mAuxiliaryLayerActionExport( nullptr )
{
setupUi( this );
connect( mLayerOrigNameLineEdit, &QLineEdit::textEdited, this, &QgsVectorLayerProperties::mLayerOrigNameLineEdit_textEdited );
Expand Down Expand Up @@ -371,6 +372,10 @@ QgsVectorLayerProperties::QgsVectorLayerProperties(
menu->addAction( mAuxiliaryLayerActionDelete );
connect( mAuxiliaryLayerActionDelete, &QAction::triggered, this, &QgsVectorLayerProperties::onAuxiliaryLayerDelete );

mAuxiliaryLayerActionExport = new QAction( tr( "Export" ), this );
menu->addAction( mAuxiliaryLayerActionExport );
connect( mAuxiliaryLayerActionExport, &QAction::triggered, this, &QgsVectorLayerProperties::onAuxiliaryLayerExport );

mAuxiliaryStorageActions->setMenu( menu );

updateAuxiliaryStoragePage();
Expand Down Expand Up @@ -1504,6 +1509,7 @@ void QgsVectorLayerProperties::updateAuxiliaryStoragePage( bool reset )
// update actions
mAuxiliaryLayerActionClear->setEnabled( true );
mAuxiliaryLayerActionDelete->setEnabled( true );
mAuxiliaryLayerActionExport->setEnabled( true );
mAuxiliaryLayerActionNew->setEnabled( false );

const QgsAuxiliaryLayer *alayer = mLayer->auxiliaryLayer();
Expand Down Expand Up @@ -1540,6 +1546,7 @@ void QgsVectorLayerProperties::updateAuxiliaryStoragePage( bool reset )

mAuxiliaryLayerActionClear->setEnabled( false );
mAuxiliaryLayerActionDelete->setEnabled( false );
mAuxiliaryLayerActionExport->setEnabled( false );

if ( mLayer->isSpatial() )
mAuxiliaryLayerActionNew->setEnabled( true );
Expand Down Expand Up @@ -1612,3 +1619,15 @@ void QgsVectorLayerProperties::onAuxiliaryLayerDelete()
mLayer->triggerRepaint();
}
}

void QgsVectorLayerProperties::onAuxiliaryLayerExport()
{
const QgsAuxiliaryLayer *alayer = mLayer->auxiliaryLayer();
if ( !alayer )
return;

std::unique_ptr<QgsVectorLayer> clone;
clone.reset( alayer->toSpatialLayer() );

QgisApp::instance()->saveAsFile( clone.get() );
}
3 changes: 3 additions & 0 deletions src/app/qgsvectorlayerproperties.h
Expand Up @@ -162,6 +162,8 @@ class APP_EXPORT QgsVectorLayerProperties : public QgsOptionsDialogBase, private

void onAuxiliaryLayerDelete();

void onAuxiliaryLayerExport();

private:

void saveStyleAs( StyleType styleType );
Expand Down Expand Up @@ -228,6 +230,7 @@ class APP_EXPORT QgsVectorLayerProperties : public QgsOptionsDialogBase, private
QAction *mAuxiliaryLayerActionNew;
QAction *mAuxiliaryLayerActionClear;
QAction *mAuxiliaryLayerActionDelete;
QAction *mAuxiliaryLayerActionExport;

private slots:
void openPanel( QgsPanelWidget *panel );
Expand Down
32 changes: 32 additions & 0 deletions src/core/qgsauxiliarystorage.cpp
Expand Up @@ -21,6 +21,7 @@
#include "qgsproject.h"
#include "qgspallabeling.h"
#include "qgsdiagramrenderer.h"
#include "qgsmemoryproviderutils.h"

#include <QFile>

Expand Down Expand Up @@ -169,6 +170,37 @@ QgsAuxiliaryLayer::QgsAuxiliaryLayer( const QString &pkField, const QString &fil
mJoinInfo.setCascadedDelete( true );
}

QgsVectorLayer *QgsAuxiliaryLayer::toSpatialLayer() const
{
QgsVectorLayer *layer = QgsMemoryProviderUtils::createMemoryLayer( QStringLiteral( "auxiliary_layer" ), fields(), mLayer->wkbType(), mLayer->crs() );

QString pkField = mJoinInfo.targetFieldName();
QgsFeature joinFeature;
QgsFeature targetFeature;
QgsFeatureIterator it = getFeatures();

layer->startEditing();
while ( it.nextFeature( joinFeature ) )
{
QString filter = QgsExpression::createFieldEqualityExpression( pkField, joinFeature.attribute( AS_JOINFIELD ) );

QgsFeatureRequest request;
request.setFilterExpression( filter );

mLayer->getFeatures( request ).nextFeature( targetFeature );

if ( targetFeature.isValid() )
{
QgsFeature newFeature( joinFeature );
newFeature.setGeometry( targetFeature.geometry() );
layer->addFeature( newFeature );
}
}
layer->commitChanges();

return layer;
}

QgsVectorLayerJoinInfo QgsAuxiliaryLayer::joinInfo() const
{
return mJoinInfo;
Expand Down
8 changes: 8 additions & 0 deletions src/core/qgsauxiliarystorage.h
Expand Up @@ -122,6 +122,14 @@ class CORE_EXPORT QgsAuxiliaryLayer : public QgsVectorLayer

QgsAuxiliaryLayer &operator=( QgsAuxiliaryLayer const &rhs ) = delete;

/**
* An auxiliary layer is not spatial. This method returns a spatial
* representation of auxiliary data.
*
* \returns A new spatial vector layer
*/
QgsVectorLayer *toSpatialLayer() const;

/**
* Deletes all features from the layer. Changes are automatically committed
* and the layer remains editable.
Expand Down

0 comments on commit dcec98d

Please sign in to comment.