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();
%Docstring
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( 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(); } );
@ -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;
@ -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 ) );
}
}

View File

@ -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.
@ -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();

View File

@ -86,6 +86,7 @@ QgsVectorLayerProperties::QgsVectorLayerProperties(
, mAuxiliaryLayerActionNew( nullptr )
, mAuxiliaryLayerActionClear( nullptr )
, mAuxiliaryLayerActionDelete( nullptr )
, mAuxiliaryLayerActionExport( nullptr )
{
setupUi( this );
connect( mLayerOrigNameLineEdit, &QLineEdit::textEdited, this, &QgsVectorLayerProperties::mLayerOrigNameLineEdit_textEdited );
@ -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();
@ -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();
@ -1540,6 +1546,7 @@ void QgsVectorLayerProperties::updateAuxiliaryStoragePage( bool reset )
mAuxiliaryLayerActionClear->setEnabled( false );
mAuxiliaryLayerActionDelete->setEnabled( false );
mAuxiliaryLayerActionExport->setEnabled( false );
if ( mLayer->isSpatial() )
mAuxiliaryLayerActionNew->setEnabled( true );
@ -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() );
}

View File

@ -162,6 +162,8 @@ class APP_EXPORT QgsVectorLayerProperties : public QgsOptionsDialogBase, private
void onAuxiliaryLayerDelete();
void onAuxiliaryLayerExport();
private:
void saveStyleAs( StyleType styleType );
@ -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 );

View File

@ -21,6 +21,7 @@
#include "qgsproject.h"
#include "qgspallabeling.h"
#include "qgsdiagramrenderer.h"
#include "qgsmemoryproviderutils.h"
#include <QFile>
@ -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;

View File

@ -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.