Implement export action

This commit is contained in:
Blottiere Paul 2017-08-29 08:59:42 +01:00
parent bca8973465
commit dcec98dfda
7 changed files with 86 additions and 11 deletions

View File

@ -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(); bool clear();
%Docstring %Docstring
Deletes all features from the layer. Changes are automatically committed Deletes all features from the layer. Changes are automatically committed

View File

@ -1856,7 +1856,7 @@ void QgisApp::createActions()
connect( mActionRollbackAllEdits, &QAction::triggered, this, &QgisApp::rollbackAllEdits ); connect( mActionRollbackAllEdits, &QAction::triggered, this, &QgisApp::rollbackAllEdits );
connect( mActionCancelEdits, &QAction::triggered, this, [ = ] { cancelEdits(); } ); connect( mActionCancelEdits, &QAction::triggered, this, [ = ] { cancelEdits(); } );
connect( mActionCancelAllEdits, &QAction::triggered, this, &QgisApp::cancelAllEdits ); 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( mActionSaveLayerDefinition, &QAction::triggered, this, &QgisApp::saveAsLayerDefinition );
connect( mActionRemoveLayer, &QAction::triggered, this, &QgisApp::removeLayer ); connect( mActionRemoveLayer, &QAction::triggered, this, &QgisApp::removeLayer );
connect( mActionDuplicateLayer, &QAction::triggered, this, [ = ] { duplicateLayers(); } ); connect( mActionDuplicateLayer, &QAction::triggered, this, [ = ] { duplicateLayers(); } );
@ -6442,9 +6442,11 @@ void QgisApp::attributeTable()
// the dialog will be deleted by itself on close // 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 ) if ( !rasterLayer )
{ {
return; return;
@ -6578,20 +6580,22 @@ void QgisApp::saveAsRasterFile()
} }
void QgisApp::saveAsFile() void QgisApp::saveAsFile( QgsMapLayer *layer )
{ {
QgsMapLayer *layer = activeLayer(); if ( !layer )
layer = activeLayer();
if ( !layer ) if ( !layer )
return; return;
QgsMapLayer::LayerType layerType = layer->type(); QgsMapLayer::LayerType layerType = layer->type();
if ( layerType == QgsMapLayer::RasterLayer ) if ( layerType == QgsMapLayer::RasterLayer )
{ {
saveAsRasterFile(); saveAsRasterFile( qobject_cast<QgsRasterLayer *>( layer ) );
} }
else if ( layerType == QgsMapLayer::VectorLayer ) else if ( layerType == QgsMapLayer::VectorLayer )
{ {
saveAsVectorFileGeneral(); saveAsVectorFileGeneral( qobject_cast<QgsVectorLayer *>( layer ) );
} }
} }

View File

@ -654,6 +654,9 @@ class APP_EXPORT QgisApp : public QMainWindow, private Ui::MainWindow
QSize iconSize( bool dockedToolbar = false ) const; QSize iconSize( bool dockedToolbar = false ) const;
public slots: public slots:
//! save current vector layer
void saveAsFile( QgsMapLayer *layer = nullptr );
//! Process the list of URIs that have been dropped in QGIS //! Process the list of URIs that have been dropped in QGIS
void handleDropUriList( const QgsMimeDataUtils::UriList &lst ); void handleDropUriList( const QgsMimeDataUtils::UriList &lst );
//! Convenience function to open either a project or a layer file. //! Convenience function to open either a project or a layer file.
@ -1439,13 +1442,10 @@ class APP_EXPORT QgisApp : public QMainWindow, private Ui::MainWindow
//! set the CAD dock widget visible //! set the CAD dock widget visible
void setCadDockVisible( bool visible ); void setCadDockVisible( bool visible );
//! save current vector layer
void saveAsFile();
void saveAsLayerDefinition(); void saveAsLayerDefinition();
//! save current raster layer //! save current raster layer
void saveAsRasterFile(); void saveAsRasterFile( QgsRasterLayer *layer = nullptr );
//! show Python console //! show Python console
void showPythonDialog(); void showPythonDialog();

View File

@ -86,6 +86,7 @@ QgsVectorLayerProperties::QgsVectorLayerProperties(
, mAuxiliaryLayerActionNew( nullptr ) , mAuxiliaryLayerActionNew( nullptr )
, mAuxiliaryLayerActionClear( nullptr ) , mAuxiliaryLayerActionClear( nullptr )
, mAuxiliaryLayerActionDelete( nullptr ) , mAuxiliaryLayerActionDelete( nullptr )
, mAuxiliaryLayerActionExport( nullptr )
{ {
setupUi( this ); setupUi( this );
connect( mLayerOrigNameLineEdit, &QLineEdit::textEdited, this, &QgsVectorLayerProperties::mLayerOrigNameLineEdit_textEdited ); connect( mLayerOrigNameLineEdit, &QLineEdit::textEdited, this, &QgsVectorLayerProperties::mLayerOrigNameLineEdit_textEdited );
@ -371,6 +372,10 @@ QgsVectorLayerProperties::QgsVectorLayerProperties(
menu->addAction( mAuxiliaryLayerActionDelete ); menu->addAction( mAuxiliaryLayerActionDelete );
connect( mAuxiliaryLayerActionDelete, &QAction::triggered, this, &QgsVectorLayerProperties::onAuxiliaryLayerDelete ); 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 ); mAuxiliaryStorageActions->setMenu( menu );
updateAuxiliaryStoragePage(); updateAuxiliaryStoragePage();
@ -1504,6 +1509,7 @@ void QgsVectorLayerProperties::updateAuxiliaryStoragePage( bool reset )
// update actions // update actions
mAuxiliaryLayerActionClear->setEnabled( true ); mAuxiliaryLayerActionClear->setEnabled( true );
mAuxiliaryLayerActionDelete->setEnabled( true ); mAuxiliaryLayerActionDelete->setEnabled( true );
mAuxiliaryLayerActionExport->setEnabled( true );
mAuxiliaryLayerActionNew->setEnabled( false ); mAuxiliaryLayerActionNew->setEnabled( false );
const QgsAuxiliaryLayer *alayer = mLayer->auxiliaryLayer(); const QgsAuxiliaryLayer *alayer = mLayer->auxiliaryLayer();
@ -1540,6 +1546,7 @@ void QgsVectorLayerProperties::updateAuxiliaryStoragePage( bool reset )
mAuxiliaryLayerActionClear->setEnabled( false ); mAuxiliaryLayerActionClear->setEnabled( false );
mAuxiliaryLayerActionDelete->setEnabled( false ); mAuxiliaryLayerActionDelete->setEnabled( false );
mAuxiliaryLayerActionExport->setEnabled( false );
if ( mLayer->isSpatial() ) if ( mLayer->isSpatial() )
mAuxiliaryLayerActionNew->setEnabled( true ); mAuxiliaryLayerActionNew->setEnabled( true );
@ -1612,3 +1619,15 @@ void QgsVectorLayerProperties::onAuxiliaryLayerDelete()
mLayer->triggerRepaint(); 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() );
}

View File

@ -162,6 +162,8 @@ class APP_EXPORT QgsVectorLayerProperties : public QgsOptionsDialogBase, private
void onAuxiliaryLayerDelete(); void onAuxiliaryLayerDelete();
void onAuxiliaryLayerExport();
private: private:
void saveStyleAs( StyleType styleType ); void saveStyleAs( StyleType styleType );
@ -228,6 +230,7 @@ class APP_EXPORT QgsVectorLayerProperties : public QgsOptionsDialogBase, private
QAction *mAuxiliaryLayerActionNew; QAction *mAuxiliaryLayerActionNew;
QAction *mAuxiliaryLayerActionClear; QAction *mAuxiliaryLayerActionClear;
QAction *mAuxiliaryLayerActionDelete; QAction *mAuxiliaryLayerActionDelete;
QAction *mAuxiliaryLayerActionExport;
private slots: private slots:
void openPanel( QgsPanelWidget *panel ); void openPanel( QgsPanelWidget *panel );

View File

@ -21,6 +21,7 @@
#include "qgsproject.h" #include "qgsproject.h"
#include "qgspallabeling.h" #include "qgspallabeling.h"
#include "qgsdiagramrenderer.h" #include "qgsdiagramrenderer.h"
#include "qgsmemoryproviderutils.h"
#include <QFile> #include <QFile>
@ -169,6 +170,37 @@ QgsAuxiliaryLayer::QgsAuxiliaryLayer( const QString &pkField, const QString &fil
mJoinInfo.setCascadedDelete( true ); 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 QgsVectorLayerJoinInfo QgsAuxiliaryLayer::joinInfo() const
{ {
return mJoinInfo; return mJoinInfo;

View File

@ -122,6 +122,14 @@ class CORE_EXPORT QgsAuxiliaryLayer : public QgsVectorLayer
QgsAuxiliaryLayer &operator=( QgsAuxiliaryLayer const &rhs ) = delete; 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 * Deletes all features from the layer. Changes are automatically committed
* and the layer remains editable. * and the layer remains editable.