diff --git a/src/app/qgisapp.cpp b/src/app/qgisapp.cpp index a05ba6fec601..2f2c4ef40273 100644 --- a/src/app/qgisapp.cpp +++ b/src/app/qgisapp.cpp @@ -6505,6 +6505,7 @@ QgsVectorLayer *QgisApp::pasteToNewMemoryVector() void QgisApp::copyStyle( QgsMapLayer * sourceLayer ) { QgsMapLayer *selectionLayer = sourceLayer ? sourceLayer : activeLayer(); + if ( selectionLayer ) { QDomImplementation DomImplementation; @@ -6515,14 +6516,31 @@ void QgisApp::copyStyle( QgsMapLayer * sourceLayer ) QDomElement rootNode = doc.createElement( "qgis" ); rootNode.setAttribute( "version", QString( "%1" ).arg( QGis::QGIS_VERSION ) ); doc.appendChild( rootNode ); + + /* + * Check to see if the layer is vector - in which case we should also copy its geometryType + * to avoid eventually pasting to a layer with a different geometry + */ + if ( selectionLayer->type() == 0 ) + { + //Getting the selectionLayer geometry + QgsVectorLayer *SelectionGeometry = static_cast(selectionLayer); + QString geoType = QString::number(SelectionGeometry->geometryType()); + + //Adding geometryinformation + QDomElement layerGeometryType = doc.createElement("layerGeometryType"); + QDomText type = doc.createTextNode(geoType); + + layerGeometryType.appendChild(type); + rootNode.appendChild(layerGeometryType); + } + QString errorMsg; if ( !selectionLayer->writeSymbology( rootNode, doc, errorMsg ) ) { - QMessageBox::warning( this, - tr( "Error" ), - tr( "Cannot copy style: %1" ) - .arg( errorMsg ), - QMessageBox::Ok ); + messageBar()->pushMessage( errorMsg, + tr( "Cannot copy style: %1" ), + QgsMessageBar::CRITICAL, messageTimeout() ); return; } // Copies data in text form as well, so the XML can be pasted into a text editor @@ -6531,9 +6549,14 @@ void QgisApp::copyStyle( QgsMapLayer * sourceLayer ) mActionPasteStyle->setEnabled( true ); } } +/** + \param destinatioLayer 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(); if ( selectionLayer ) { @@ -6544,24 +6567,35 @@ void QgisApp::pasteStyle( QgsMapLayer * destinationLayer ) int errorLine, errorColumn; if ( !doc.setContent( clipboard()->data( QGSCLIPBOARD_STYLE_MIME ), false, &errorMsg, &errorLine, &errorColumn ) ) { - QMessageBox::information( this, - tr( "Error" ), - tr( "Cannot parse style: %1:%2:%3" ) - .arg( errorMsg ) - .arg( errorLine ) - .arg( errorColumn ), - QMessageBox::Ok ); - return; + + messageBar()->pushMessage( errorMsg, + tr( "Cannot parse style: %1:%2:%3" ), + QgsMessageBar::CRITICAL, messageTimeout() ); + return; } + QDomElement rootNode = doc.firstChildElement( "qgis" ); + + //Test for matching geometry type on vector layers when pasting + if (selectionLayer->type() == QgsMapLayer::LayerType::VectorLayer) + { + QgsVectorLayer *selectionVectorLayer = static_cast(selectionLayer); + int pasteLayerGeometryType = doc.elementsByTagName("layerGeometryType").item(0).toElement().text().toInt(); + if ( selectionVectorLayer->geometryType() != pasteLayerGeometryType ) + { + messageBar()->pushMessage( tr( "Cannot paste style to layer with a different geometry type" ), + tr( "Your copied style does not match the layer you are pasting to" ), + QgsMessageBar::INFO, messageTimeout() ); + return; + } + } + if ( !selectionLayer->readSymbology( rootNode, errorMsg ) ) { - QMessageBox::information( this, - tr( "Error" ), - tr( "Cannot read style: %1" ) - .arg( errorMsg ), - QMessageBox::Ok ); - return; + messageBar()->pushMessage( errorMsg, + tr( "Cannot read style: %1" ), + QgsMessageBar::CRITICAL, messageTimeout() ); + return; } mLayerTreeView->refreshLayerSymbology( selectionLayer->id() ); diff --git a/src/app/qgisapp.h b/src/app/qgisapp.h index 62754c9256da..03d40e00c89d 100644 --- a/src/app/qgisapp.h +++ b/src/app/qgisapp.h @@ -583,14 +583,13 @@ class APP_EXPORT QgisApp : public QMainWindow, private Ui::MainWindow void copyStyle( QgsMapLayer *sourceLayer = 0 ); //! pastes style on the clipboard to the active layer /** - \param destinatioLayer The layer that the clipboard will be pasted to + \param destinationLayer The layer that the clipboard will be pasted to (defaults to the active layer on the legend) */ void pasteStyle( QgsMapLayer *destinationLayer = 0 ); //! copies features to internal clipboard void copyFeatures( QgsFeatureStore & featureStore ); - void loadOGRSublayers( QString layertype, QString uri, QStringList list ); void loadGDALSublayers( QString uri, QStringList list ); diff --git a/src/core/qgsmaplayer.h b/src/core/qgsmaplayer.h index c607d88648fe..957706753995 100644 --- a/src/core/qgsmaplayer.h +++ b/src/core/qgsmaplayer.h @@ -354,6 +354,7 @@ class CORE_EXPORT QgsMapLayer : public QObject { Q_UNUSED( node ); errorMessage = QString( "Layer type %1 not supported" ).arg( type() ); return false; } + /** Read the symbology for the current layer from the Dom node supplied. * @param node node that will contain the symbology definition for this layer. * @param errorMessage reference to string that will be updated with any error messages