@@ -6505,6 +6505,7 @@ QgsVectorLayer *QgisApp::pasteToNewMemoryVector()
6505
6505
void QgisApp::copyStyle ( QgsMapLayer * sourceLayer )
6506
6506
{
6507
6507
QgsMapLayer *selectionLayer = sourceLayer ? sourceLayer : activeLayer ();
6508
+
6508
6509
if ( selectionLayer )
6509
6510
{
6510
6511
QDomImplementation DomImplementation;
@@ -6515,14 +6516,31 @@ void QgisApp::copyStyle( QgsMapLayer * sourceLayer )
6515
6516
QDomElement rootNode = doc.createElement ( " qgis" );
6516
6517
rootNode.setAttribute ( " version" , QString ( " %1" ).arg ( QGis::QGIS_VERSION ) );
6517
6518
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
+
6518
6538
QString errorMsg;
6519
6539
if ( !selectionLayer->writeSymbology ( rootNode, doc, errorMsg ) )
6520
6540
{
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 () );
6526
6544
return ;
6527
6545
}
6528
6546
// 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 )
6531
6549
mActionPasteStyle ->setEnabled ( true );
6532
6550
}
6533
6551
}
6552
+ /* *
6553
+ \param destinatioLayer The layer that the clipboard will be pasted to
6554
+ (defaults to the active layer on the legend)
6555
+ */
6556
+
6534
6557
6535
6558
void QgisApp::pasteStyle ( QgsMapLayer * destinationLayer )
6536
- {
6559
+ {
6537
6560
QgsMapLayer *selectionLayer = destinationLayer ? destinationLayer : activeLayer ();
6538
6561
if ( selectionLayer )
6539
6562
{
@@ -6544,24 +6567,35 @@ void QgisApp::pasteStyle( QgsMapLayer * destinationLayer )
6544
6567
int errorLine, errorColumn;
6545
6568
if ( !doc.setContent ( clipboard ()->data ( QGSCLIPBOARD_STYLE_MIME ), false , &errorMsg, &errorLine, &errorColumn ) )
6546
6569
{
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 ;
6555
6575
}
6576
+
6556
6577
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
+
6557
6593
if ( !selectionLayer->readSymbology ( rootNode, errorMsg ) )
6558
6594
{
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 ;
6565
6599
}
6566
6600
6567
6601
mLayerTreeView ->refreshLayerSymbology ( selectionLayer->id () );
0 commit comments