Skip to content
Permalink
Browse files
Merge pull request #2057 from Hvitnov/fix12655
Fix #12655 copy/paste style between different vector layer geometry types
  • Loading branch information
Hugo Mercier committed May 25, 2015
2 parents 4e14f8d + a5351fc commit 0b9ea9a6160b17294b46b070c1b491bdebb93865
Showing with 56 additions and 22 deletions.
  1. +54 −20 src/app/qgisapp.cpp
  2. +1 −2 src/app/qgisapp.h
  3. +1 −0 src/core/qgsmaplayer.h
@@ -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<QgsVectorLayer*>(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<QgsVectorLayer*>(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() );
@@ -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 );

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

0 comments on commit 0b9ea9a

Please sign in to comment.