mirror of
https://github.com/qgis/QGIS.git
synced 2025-04-15 00:04:00 -04:00
Merge pull request #6627 from slarosa/cp_layer1
[FEATURE][needs-docs] Copy&Paste Group/Layers from a QGIS project to another
This commit is contained in:
commit
330c2f70ce
@ -12,6 +12,7 @@
|
||||
|
||||
|
||||
|
||||
|
||||
class QgsMapLayer : QObject
|
||||
{
|
||||
%Docstring
|
||||
|
@ -1913,6 +1913,8 @@ void QgisApp::createActions()
|
||||
connect( mActionPasteAsNewMemoryVector, &QAction::triggered, this, [ = ] { pasteAsNewMemoryVector(); } );
|
||||
connect( mActionCopyStyle, &QAction::triggered, this, [ = ] { copyStyle(); } );
|
||||
connect( mActionPasteStyle, &QAction::triggered, this, [ = ] { pasteStyle(); } );
|
||||
connect( mActionCopyLayer, &QAction::triggered, this, &QgisApp::copyLayer );
|
||||
connect( mActionPasteLayer, &QAction::triggered, this, &QgisApp::pasteLayer );
|
||||
connect( mActionAddFeature, &QAction::triggered, this, &QgisApp::addFeature );
|
||||
connect( mActionCircularStringCurvePoint, &QAction::triggered, this, [ = ] { setMapTool( mMapTools.mCircularStringCurvePoint ); } );
|
||||
connect( mActionCircularStringRadius, &QAction::triggered, this, [ = ] { setMapTool( mMapTools.mCircularStringRadius ); } );
|
||||
@ -8539,12 +8541,6 @@ void QgisApp::copyStyle( QgsMapLayer *sourceLayer )
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
\param destinationLayer The layer that the clipboard will be pasted to
|
||||
(defaults to the active layer on the legend)
|
||||
*/
|
||||
|
||||
|
||||
void QgisApp::pasteStyle( QgsMapLayer *destinationLayer )
|
||||
{
|
||||
QgsMapLayer *selectionLayer = destinationLayer ? destinationLayer : activeLayer();
|
||||
@ -8585,6 +8581,55 @@ void QgisApp::pasteStyle( QgsMapLayer *destinationLayer )
|
||||
}
|
||||
}
|
||||
|
||||
void QgisApp::copyLayer()
|
||||
{
|
||||
QString errorMessage;
|
||||
QgsReadWriteContext readWriteContext;
|
||||
QDomDocument doc( QStringLiteral( "qgis-layer-definition" ) );
|
||||
|
||||
bool saved = QgsLayerDefinition::exportLayerDefinition( doc, mLayerTreeView->selectedNodes(), errorMessage, readWriteContext );
|
||||
|
||||
if ( !saved )
|
||||
{
|
||||
messageBar()->pushMessage( tr( "Error copying layer" ), errorMessage, Qgis::Warning );
|
||||
}
|
||||
|
||||
// Copies data in text form as well, so the XML can be pasted into a text editor
|
||||
clipboard()->setData( QGSCLIPBOARD_MAPLAYER_MIME, doc.toByteArray(), doc.toString() );
|
||||
// Enables the paste menu element
|
||||
mActionPasteLayer->setEnabled( true );
|
||||
}
|
||||
|
||||
void QgisApp::pasteLayer()
|
||||
{
|
||||
if ( clipboard()->hasFormat( QGSCLIPBOARD_MAPLAYER_MIME ) )
|
||||
{
|
||||
QDomDocument doc;
|
||||
QString errorMessage;
|
||||
QgsReadWriteContext readWriteContext;
|
||||
doc.setContent( clipboard()->data( QGSCLIPBOARD_MAPLAYER_MIME ) );
|
||||
|
||||
QgsLayerTreeNode *currentNode = mLayerTreeView->currentNode();
|
||||
QgsLayerTreeGroup *root = nullptr;
|
||||
if ( QgsLayerTree::isGroup( currentNode ) )
|
||||
{
|
||||
root = QgsLayerTree::toGroup( currentNode );
|
||||
}
|
||||
else
|
||||
{
|
||||
root = QgsProject::instance()->layerTreeRoot();
|
||||
}
|
||||
|
||||
bool loaded = QgsLayerDefinition::loadLayerDefinition( doc, QgsProject::instance(), root,
|
||||
errorMessage, readWriteContext );
|
||||
|
||||
if ( !loaded )
|
||||
{
|
||||
messageBar()->pushMessage( tr( "Error pasting layer" ), errorMessage, Qgis::Warning );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void QgisApp::copyFeatures( QgsFeatureStore &featureStore )
|
||||
{
|
||||
clipboard()->replaceWithCopyOf( featureStore );
|
||||
@ -11575,6 +11620,8 @@ void QgisApp::activateDeactivateLayerRelatedActions( QgsMapLayer *layer )
|
||||
mActionPasteFeatures->setEnabled( false );
|
||||
mActionCopyStyle->setEnabled( false );
|
||||
mActionPasteStyle->setEnabled( false );
|
||||
mActionCopyLayer->setEnabled( false );
|
||||
mActionPasteLayer->setEnabled( false );
|
||||
|
||||
mUndoDock->widget()->setEnabled( false );
|
||||
mActionUndo->setEnabled( false );
|
||||
@ -11621,6 +11668,8 @@ void QgisApp::activateDeactivateLayerRelatedActions( QgsMapLayer *layer )
|
||||
|
||||
mActionCopyStyle->setEnabled( true );
|
||||
mActionPasteStyle->setEnabled( clipboard()->hasFormat( QGSCLIPBOARD_STYLE_MIME ) );
|
||||
mActionCopyLayer->setEnabled( true );
|
||||
mActionPasteLayer->setEnabled( clipboard()->hasFormat( QGSCLIPBOARD_MAPLAYER_MIME ) );
|
||||
|
||||
/***********Vector layers****************/
|
||||
if ( layer->type() == QgsMapLayer::VectorLayer )
|
||||
|
@ -826,6 +826,10 @@ class APP_EXPORT QgisApp : public QMainWindow, private Ui::MainWindow
|
||||
(defaults to the active layer on the legend)
|
||||
*/
|
||||
void pasteStyle( QgsMapLayer *destinationLayer = nullptr );
|
||||
//! copies group or layer on the clipboard
|
||||
void copyLayer();
|
||||
//! pastes group or layer from the clipboard to layer tree
|
||||
void pasteLayer();
|
||||
|
||||
//! copies features to internal clipboard
|
||||
void copyFeatures( QgsFeatureStore &featureStore );
|
||||
|
@ -56,9 +56,15 @@ QMenu *QgsAppLayerTreeViewMenuProvider::createContextMenu()
|
||||
{
|
||||
// global menu
|
||||
menu->addAction( actions->actionAddGroup( menu ) );
|
||||
|
||||
menu->addAction( QgsApplication::getThemeIcon( QStringLiteral( "/mActionExpandTree.svg" ) ), tr( "&Expand All" ), mView, SLOT( expandAll() ) );
|
||||
menu->addAction( QgsApplication::getThemeIcon( QStringLiteral( "/mActionCollapseTree.svg" ) ), tr( "&Collapse All" ), mView, SLOT( collapseAll() ) );
|
||||
menu->addSeparator();
|
||||
if ( QgisApp::instance()->clipboard()->hasFormat( QGSCLIPBOARD_MAPLAYER_MIME ) )
|
||||
{
|
||||
QAction *actionPasteLayerOrGroup = new QAction( QgsApplication::getThemeIcon( QStringLiteral( "/mActionEditPaste.svg" ) ), tr( "Paste Layer/Group" ), menu );
|
||||
connect( actionPasteLayerOrGroup, &QAction::triggered, QgisApp::instance(), &QgisApp::pasteLayer );
|
||||
menu->addAction( actionPasteLayerOrGroup );
|
||||
}
|
||||
|
||||
// TODO: update drawing order
|
||||
}
|
||||
@ -92,6 +98,14 @@ QMenu *QgsAppLayerTreeViewMenuProvider::createContextMenu()
|
||||
menu->addAction( tr( "Paste Style" ), QgisApp::instance(), SLOT( applyStyleToGroup() ) );
|
||||
}
|
||||
|
||||
menu->addAction( tr( "Copy Group" ), QgisApp::instance(), SLOT( copyLayer() ) );
|
||||
if ( QgisApp::instance()->clipboard()->hasFormat( QGSCLIPBOARD_MAPLAYER_MIME ) )
|
||||
{
|
||||
QAction *actionPasteLayerOrGroup = new QAction( tr( "Paste Layer/Group" ), menu );
|
||||
connect( actionPasteLayerOrGroup, &QAction::triggered, QgisApp::instance(), &QgisApp::pasteLayer );
|
||||
menu->addAction( actionPasteLayerOrGroup );
|
||||
}
|
||||
|
||||
menu->addAction( tr( "Save As Layer Definition File…" ), QgisApp::instance(), SLOT( saveAsLayerDefinition() ) );
|
||||
|
||||
menu->addAction( actions->actionAddGroup( menu ) );
|
||||
@ -207,6 +221,9 @@ QMenu *QgsAppLayerTreeViewMenuProvider::createContextMenu()
|
||||
}
|
||||
|
||||
menu->addSeparator();
|
||||
QAction *actionCopyLayer = new QAction( tr( "Copy Layer" ), menu );
|
||||
connect( actionCopyLayer, &QAction::triggered, QgisApp::instance(), &QgisApp::copyLayer );
|
||||
menu->addAction( actionCopyLayer );
|
||||
|
||||
if ( vlayer )
|
||||
{
|
||||
|
@ -48,6 +48,11 @@ class QDomDocument;
|
||||
class QKeyEvent;
|
||||
class QPainter;
|
||||
|
||||
/*
|
||||
* Constants used to describe copy-paste MIME types
|
||||
*/
|
||||
#define QGSCLIPBOARD_MAPLAYER_MIME "application/qgis.maplayer"
|
||||
|
||||
/**
|
||||
* \ingroup core
|
||||
* Base class for all map layer types.
|
||||
|
@ -17,7 +17,7 @@
|
||||
<x>0</x>
|
||||
<y>0</y>
|
||||
<width>1030</width>
|
||||
<height>28</height>
|
||||
<height>22</height>
|
||||
</rect>
|
||||
</property>
|
||||
<property name="toolTip">
|
||||
@ -173,6 +173,9 @@
|
||||
<addaction name="mActionCopyStyle"/>
|
||||
<addaction name="mActionPasteStyle"/>
|
||||
<addaction name="separator"/>
|
||||
<addaction name="mActionCopyLayer"/>
|
||||
<addaction name="mActionPasteLayer"/>
|
||||
<addaction name="separator"/>
|
||||
<addaction name="mActionOpenTable"/>
|
||||
<addaction name="mActionToggleEditing"/>
|
||||
<addaction name="mActionSaveLayerEdits"/>
|
||||
@ -2983,6 +2986,27 @@ Acts on currently active editable layer</string>
|
||||
<string>Revert Project to Saved version</string>
|
||||
</property>
|
||||
</action>
|
||||
<action name="mActionCopyLayer">
|
||||
<property name="icon">
|
||||
<iconset resource="../../images/images.qrc">
|
||||
<normaloff>:/images/themes/default/mActionEditCopy.svg</normaloff>:/images/themes/default/mActionEditCopy.svg</iconset>
|
||||
</property>
|
||||
<property name="text">
|
||||
<string>Copy Layer</string>
|
||||
</property>
|
||||
</action>
|
||||
<action name="mActionPasteLayer">
|
||||
<property name="icon">
|
||||
<iconset resource="../../images/images.qrc">
|
||||
<normaloff>:/images/themes/default/mActionEditPaste.svg</normaloff>:/images/themes/default/mActionEditPaste.svg</iconset>
|
||||
</property>
|
||||
<property name="text">
|
||||
<string>Paste Layer/Group</string>
|
||||
</property>
|
||||
<property name="toolTip">
|
||||
<string>Paste Layer/Group</string>
|
||||
</property>
|
||||
</action>
|
||||
</widget>
|
||||
<resources>
|
||||
<include location="../../images/images.qrc"/>
|
||||
|
Loading…
x
Reference in New Issue
Block a user