Skip to content

Commit

Permalink
Merge pull request #5578 from gacarrillor/iface_copy_paste_features
Browse files Browse the repository at this point in the history
Expose through iface methods to copy/paste features between given layers
  • Loading branch information
m-kuhn committed Nov 20, 2017
2 parents 970a723 + a00ce9e commit 67b68a8
Show file tree
Hide file tree
Showing 8 changed files with 58 additions and 12 deletions.
12 changes: 12 additions & 0 deletions python/gui/qgisinterface.sip
Expand Up @@ -767,6 +767,18 @@ Start a blank project
:rtype: bool
%End

virtual void copySelectionToClipboard( QgsMapLayer * ) = 0;
%Docstring
Copy selected features from the layer to clipboard
.. versionadded:: 3.0
%End

virtual void pasteFromClipboard( QgsMapLayer * ) = 0;
%Docstring
Paste features from clipboard to the layer
.. versionadded:: 3.0
%End

virtual int addToolBarIcon( QAction *qAction ) = 0;
%Docstring
Add an icon to the plugins toolbar
Expand Down
12 changes: 6 additions & 6 deletions src/app/qgisapp.cpp
Expand Up @@ -1822,9 +1822,9 @@ void QgisApp::createActions()

connect( mActionUndo, &QAction::triggered, mUndoWidget, &QgsUndoWidget::undo );
connect( mActionRedo, &QAction::triggered, mUndoWidget, &QgsUndoWidget::redo );
connect( mActionCutFeatures, &QAction::triggered, this, [ = ] { editCut(); } );
connect( mActionCopyFeatures, &QAction::triggered, this, [ = ] { editCopy(); } );
connect( mActionPasteFeatures, &QAction::triggered, this, [ = ] { editPaste(); } );
connect( mActionCutFeatures, &QAction::triggered, this, [ = ] { cutSelectionToClipboard(); } );
connect( mActionCopyFeatures, &QAction::triggered, this, [ = ] { copySelectionToClipboard(); } );
connect( mActionPasteFeatures, &QAction::triggered, this, [ = ] { pasteFromClipboard(); } );
connect( mActionPasteAsNewVector, &QAction::triggered, this, &QgisApp::pasteAsNewVector );
connect( mActionPasteAsNewMemoryVector, &QAction::triggered, this, [ = ] { pasteAsNewMemoryVector(); } );
connect( mActionCopyStyle, &QAction::triggered, this, [ = ] { copyStyle(); } );
Expand Down Expand Up @@ -8084,7 +8084,7 @@ void QgisApp::addPart()
}


void QgisApp::editCut( QgsMapLayer *layerContainingSelection )
void QgisApp::cutSelectionToClipboard( QgsMapLayer *layerContainingSelection )
{
// Test for feature support in this layer
QgsVectorLayer *selectionVectorLayer = qobject_cast<QgsVectorLayer *>( layerContainingSelection ? layerContainingSelection : activeLayer() );
Expand All @@ -8098,7 +8098,7 @@ void QgisApp::editCut( QgsMapLayer *layerContainingSelection )
selectionVectorLayer->endEditCommand();
}

void QgisApp::editCopy( QgsMapLayer *layerContainingSelection )
void QgisApp::copySelectionToClipboard( QgsMapLayer *layerContainingSelection )
{
QgsVectorLayer *selectionVectorLayer = qobject_cast<QgsVectorLayer *>( layerContainingSelection ? layerContainingSelection : activeLayer() );
if ( !selectionVectorLayer )
Expand All @@ -8113,7 +8113,7 @@ void QgisApp::clipboardChanged()
activateDeactivateLayerRelatedActions( activeLayer() );
}

void QgisApp::editPaste( QgsMapLayer *destinationLayer )
void QgisApp::pasteFromClipboard( QgsMapLayer *destinationLayer )
{
QgsVectorLayer *pasteVectorLayer = qobject_cast<QgsVectorLayer *>( destinationLayer ? destinationLayer : activeLayer() );
if ( !pasteVectorLayer )
Expand Down
6 changes: 3 additions & 3 deletions src/app/qgisapp.h
Expand Up @@ -745,21 +745,21 @@ class APP_EXPORT QgisApp : public QMainWindow, private Ui::MainWindow
\param layerContainingSelection The layer that the selection will be taken from
(defaults to the active layer on the legend)
*/
void editCut( QgsMapLayer *layerContainingSelection = nullptr );
void cutSelectionToClipboard( QgsMapLayer *layerContainingSelection = nullptr );
//! copies selected features on the active layer to the clipboard

/**
\param layerContainingSelection The layer that the selection will be taken from
(defaults to the active layer on the legend)
*/
void editCopy( QgsMapLayer *layerContainingSelection = nullptr );
void copySelectionToClipboard( QgsMapLayer *layerContainingSelection = nullptr );
//! copies features on the clipboard to the active layer

/**
\param destinationLayer The layer that the clipboard will be pasted to
(defaults to the active layer on the legend)
*/
void editPaste( QgsMapLayer *destinationLayer = nullptr );
void pasteFromClipboard( QgsMapLayer *destinationLayer = nullptr );
//! copies features on the clipboard to a new vector layer
void pasteAsNewVector();
//! copies features on the clipboard to a new memory vector layer
Expand Down
10 changes: 10 additions & 0 deletions src/app/qgisappinterface.cpp
Expand Up @@ -186,6 +186,16 @@ bool QgisAppInterface::setActiveLayer( QgsMapLayer *layer )
return qgis->setActiveLayer( layer );
}

void QgisAppInterface::copySelectionToClipboard( QgsMapLayer *layer )
{
return qgis->copySelectionToClipboard( layer );
}

void QgisAppInterface::pasteFromClipboard( QgsMapLayer *layer )
{
return qgis->pasteFromClipboard( layer );
}

void QgisAppInterface::addPluginToMenu( const QString &name, QAction *action )
{
qgis->addPluginToMenu( name, action );
Expand Down
12 changes: 12 additions & 0 deletions src/app/qgisappinterface.h
Expand Up @@ -96,6 +96,18 @@ class APP_EXPORT QgisAppInterface : public QgisInterface
//! set the active layer (layer selected in the legend)
bool setActiveLayer( QgsMapLayer *layer ) override;

/**
* Copy selected features from the layer to clipboard
* \since QGIS 3.0
*/
virtual void copySelectionToClipboard( QgsMapLayer *layer ) override;

/**
* Paste features from clipboard to the layer
* \since QGIS 3.0
*/
virtual void pasteFromClipboard( QgsMapLayer *layer ) override;

//! Add an icon to the plugins toolbar
int addToolBarIcon( QAction *qAction ) override;

Expand Down
4 changes: 2 additions & 2 deletions src/app/qgsattributetabledialog.cpp
Expand Up @@ -730,12 +730,12 @@ void QgsAttributeTableDialog::mActionExpressionSelect_triggered()

void QgsAttributeTableDialog::mActionCopySelectedRows_triggered()
{
QgisApp::instance()->editCopy( mLayer );
QgisApp::instance()->copySelectionToClipboard( mLayer );
}

void QgsAttributeTableDialog::mActionPasteFeatures_triggered()
{
QgisApp::instance()->editPaste( mLayer );
QgisApp::instance()->pasteFromClipboard( mLayer );
}


Expand Down
12 changes: 12 additions & 0 deletions src/gui/qgisinterface.h
Expand Up @@ -422,6 +422,18 @@ class GUI_EXPORT QgisInterface : public QObject
*/
virtual bool setActiveLayer( QgsMapLayer * ) = 0;

/**
* Copy selected features from the layer to clipboard
* \since QGIS 3.0
*/
virtual void copySelectionToClipboard( QgsMapLayer * ) = 0;

/**
* Paste features from clipboard to the layer
* \since QGIS 3.0
*/
virtual void pasteFromClipboard( QgsMapLayer * ) = 0;

//! Add an icon to the plugins toolbar
virtual int addToolBarIcon( QAction *qAction ) = 0;

Expand Down
2 changes: 1 addition & 1 deletion tests/src/app/testqgisappclipboard.cpp
Expand Up @@ -103,7 +103,7 @@ void TestQgisAppClipboard::copyPaste()

// copy all features to clipboard
inputLayer->selectAll();
mQgisApp->editCopy( inputLayer );
mQgisApp->copySelectionToClipboard( inputLayer );

QgsFeatureList features = mQgisApp->clipboard()->copyOf();
qDebug() << features.size() << " features copied to clipboard";
Expand Down

0 comments on commit 67b68a8

Please sign in to comment.