Skip to content

Commit 0b9ea9a

Browse files
author
Hugo Mercier
committedMay 25, 2015
Merge pull request #2057 from Hvitnov/fix12655
Fix #12655 copy/paste style between different vector layer geometry types
2 parents 4e14f8d + a5351fc commit 0b9ea9a

File tree

3 files changed

+56
-22
lines changed

3 files changed

+56
-22
lines changed
 

‎src/app/qgisapp.cpp

Lines changed: 54 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -6505,6 +6505,7 @@ QgsVectorLayer *QgisApp::pasteToNewMemoryVector()
65056505
void QgisApp::copyStyle( QgsMapLayer * sourceLayer )
65066506
{
65076507
QgsMapLayer *selectionLayer = sourceLayer ? sourceLayer : activeLayer();
6508+
65086509
if ( selectionLayer )
65096510
{
65106511
QDomImplementation DomImplementation;
@@ -6515,14 +6516,31 @@ void QgisApp::copyStyle( QgsMapLayer * sourceLayer )
65156516
QDomElement rootNode = doc.createElement( "qgis" );
65166517
rootNode.setAttribute( "version", QString( "%1" ).arg( QGis::QGIS_VERSION ) );
65176518
doc.appendChild( rootNode );
6519+
6520+
/*
6521+
* Check to see if the layer is vector - in which case we should also copy its geometryType
6522+
* to avoid eventually pasting to a layer with a different geometry
6523+
*/
6524+
if ( selectionLayer->type() == 0 )
6525+
{
6526+
//Getting the selectionLayer geometry
6527+
QgsVectorLayer *SelectionGeometry = static_cast<QgsVectorLayer*>(selectionLayer);
6528+
QString geoType = QString::number(SelectionGeometry->geometryType());
6529+
6530+
//Adding geometryinformation
6531+
QDomElement layerGeometryType = doc.createElement("layerGeometryType");
6532+
QDomText type = doc.createTextNode(geoType);
6533+
6534+
layerGeometryType.appendChild(type);
6535+
rootNode.appendChild(layerGeometryType);
6536+
}
6537+
65186538
QString errorMsg;
65196539
if ( !selectionLayer->writeSymbology( rootNode, doc, errorMsg ) )
65206540
{
6521-
QMessageBox::warning( this,
6522-
tr( "Error" ),
6523-
tr( "Cannot copy style: %1" )
6524-
.arg( errorMsg ),
6525-
QMessageBox::Ok );
6541+
messageBar()->pushMessage( errorMsg,
6542+
tr( "Cannot copy style: %1" ),
6543+
QgsMessageBar::CRITICAL, messageTimeout() );
65266544
return;
65276545
}
65286546
// 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 )
65316549
mActionPasteStyle->setEnabled( true );
65326550
}
65336551
}
6552+
/**
6553+
\param destinatioLayer The layer that the clipboard will be pasted to
6554+
(defaults to the active layer on the legend)
6555+
*/
6556+
65346557

65356558
void QgisApp::pasteStyle( QgsMapLayer * destinationLayer )
6536-
{
6559+
{
65376560
QgsMapLayer *selectionLayer = destinationLayer ? destinationLayer : activeLayer();
65386561
if ( selectionLayer )
65396562
{
@@ -6544,24 +6567,35 @@ void QgisApp::pasteStyle( QgsMapLayer * destinationLayer )
65446567
int errorLine, errorColumn;
65456568
if ( !doc.setContent( clipboard()->data( QGSCLIPBOARD_STYLE_MIME ), false, &errorMsg, &errorLine, &errorColumn ) )
65466569
{
6547-
QMessageBox::information( this,
6548-
tr( "Error" ),
6549-
tr( "Cannot parse style: %1:%2:%3" )
6550-
.arg( errorMsg )
6551-
.arg( errorLine )
6552-
.arg( errorColumn ),
6553-
QMessageBox::Ok );
6554-
return;
6570+
6571+
messageBar()->pushMessage( errorMsg,
6572+
tr( "Cannot parse style: %1:%2:%3" ),
6573+
QgsMessageBar::CRITICAL, messageTimeout() );
6574+
return;
65556575
}
6576+
65566577
QDomElement rootNode = doc.firstChildElement( "qgis" );
6578+
6579+
//Test for matching geometry type on vector layers when pasting
6580+
if (selectionLayer->type() == QgsMapLayer::LayerType::VectorLayer)
6581+
{
6582+
QgsVectorLayer *selectionVectorLayer = static_cast<QgsVectorLayer*>(selectionLayer);
6583+
int pasteLayerGeometryType = doc.elementsByTagName("layerGeometryType").item(0).toElement().text().toInt();
6584+
if ( selectionVectorLayer->geometryType() != pasteLayerGeometryType )
6585+
{
6586+
messageBar()->pushMessage( tr( "Cannot paste style to layer with a different geometry type" ),
6587+
tr( "Your copied style does not match the layer you are pasting to" ),
6588+
QgsMessageBar::INFO, messageTimeout() );
6589+
return;
6590+
}
6591+
}
6592+
65576593
if ( !selectionLayer->readSymbology( rootNode, errorMsg ) )
65586594
{
6559-
QMessageBox::information( this,
6560-
tr( "Error" ),
6561-
tr( "Cannot read style: %1" )
6562-
.arg( errorMsg ),
6563-
QMessageBox::Ok );
6564-
return;
6595+
messageBar()->pushMessage( errorMsg,
6596+
tr( "Cannot read style: %1" ),
6597+
QgsMessageBar::CRITICAL, messageTimeout() );
6598+
return;
65656599
}
65666600

65676601
mLayerTreeView->refreshLayerSymbology( selectionLayer->id() );

‎src/app/qgisapp.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -583,14 +583,13 @@ class APP_EXPORT QgisApp : public QMainWindow, private Ui::MainWindow
583583
void copyStyle( QgsMapLayer *sourceLayer = 0 );
584584
//! pastes style on the clipboard to the active layer
585585
/**
586-
\param destinatioLayer The layer that the clipboard will be pasted to
586+
\param destinationLayer The layer that the clipboard will be pasted to
587587
(defaults to the active layer on the legend)
588588
*/
589589
void pasteStyle( QgsMapLayer *destinationLayer = 0 );
590590

591591
//! copies features to internal clipboard
592592
void copyFeatures( QgsFeatureStore & featureStore );
593-
594593
void loadOGRSublayers( QString layertype, QString uri, QStringList list );
595594
void loadGDALSublayers( QString uri, QStringList list );
596595

‎src/core/qgsmaplayer.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -354,6 +354,7 @@ class CORE_EXPORT QgsMapLayer : public QObject
354354
{ Q_UNUSED( node ); errorMessage = QString( "Layer type %1 not supported" ).arg( type() ); return false; }
355355

356356

357+
357358
/** Read the symbology for the current layer from the Dom node supplied.
358359
* @param node node that will contain the symbology definition for this layer.
359360
* @param errorMessage reference to string that will be updated with any error messages

0 commit comments

Comments
 (0)
Please sign in to comment.