Skip to content

Commit 0daaf23

Browse files
Hugo Merciertimlinux
Hugo Mercier
authored andcommittedApr 17, 2012
Implement basic copy/paste of styles between layers
1 parent fc373b3 commit 0daaf23

File tree

4 files changed

+121
-2
lines changed

4 files changed

+121
-2
lines changed
 

‎src/app/legend/qgslegend.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
#include <QMouseEvent>
4343
#include <QPixmap>
4444
#include <QTreeWidgetItem>
45+
#include <QClipboard>
4546

4647
const int AUTOSCROLL_MARGIN = 16;
4748

@@ -697,6 +698,16 @@ void QgsLegend::handleRightClickEvent( QTreeWidgetItem* item, const QPoint& posi
697698
// ends here
698699
}
699700

701+
if ( selectedLayers().length() == 1 )
702+
{
703+
QgisApp* app = QgisApp::instance();
704+
theMenu.addAction( tr( "Copy Style" ), app, SLOT( copyStyle() ) );
705+
if ( QApplication::clipboard()->mimeData()->hasFormat( "application/qgis.style" ) )
706+
{
707+
theMenu.addAction( tr( "Paste Style" ), app, SLOT( pasteStyle() ) );
708+
}
709+
}
710+
700711
theMenu.addAction( QgisApp::getThemeIcon( "/folder_new.png" ), tr( "&Add New Group" ), this, SLOT( addGroupToCurrentItem() ) );
701712
theMenu.addAction( QgisApp::getThemeIcon( "/mActionExpandTree.png" ), tr( "&Expand All" ), this, SLOT( expandAll() ) );
702713
theMenu.addAction( QgisApp::getThemeIcon( "/mActionCollapseTree.png" ), tr( "&Collapse All" ), this, SLOT( collapseAll() ) );

‎src/app/qgisapp.cpp

Lines changed: 77 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -824,6 +824,8 @@ void QgisApp::createActions()
824824
connect( mActionCutFeatures, SIGNAL( triggered() ), this, SLOT( editCut() ) );
825825
connect( mActionCopyFeatures, SIGNAL( triggered() ), this, SLOT( editCopy() ) );
826826
connect( mActionPasteFeatures, SIGNAL( triggered() ), this, SLOT( editPaste() ) );
827+
connect( mActionCopyStyle, SIGNAL( triggered() ), this, SLOT( copyStyle() ) );
828+
connect( mActionPasteStyle, SIGNAL( triggered() ), this, SLOT( pasteStyle() ) );
827829
connect( mActionAddFeature, SIGNAL( triggered() ), this, SLOT( addFeature() ) );
828830
connect( mActionMoveFeature, SIGNAL( triggered() ), this, SLOT( moveFeature() ) );
829831
connect( mActionReshapeFeatures, SIGNAL( triggered() ), this, SLOT( reshapeFeatures() ) );
@@ -4297,7 +4299,6 @@ void QgisApp::editCut( QgsMapLayer * layerContainingSelection )
42974299
}
42984300
}
42994301

4300-
43014302
void QgisApp::editCopy( QgsMapLayer * layerContainingSelection )
43024303
{
43034304
if ( mMapCanvas && mMapCanvas->isDrawing() )
@@ -4376,6 +4377,76 @@ void QgisApp::editPaste( QgsMapLayer *destinationLayer )
43764377
}
43774378
}
43784379

4380+
void QgisApp::copyStyle( QgsMapLayer * sourceLayer )
4381+
{
4382+
QgsMapLayer *selectionLayer = sourceLayer ? sourceLayer : activeLayer();
4383+
if ( selectionLayer )
4384+
{
4385+
QDomImplementation DomImplementation;
4386+
QDomDocumentType documentType =
4387+
DomImplementation.createDocumentType(
4388+
"qgis", "http://mrcc.com/qgis.dtd", "SYSTEM" );
4389+
QDomDocument doc( documentType );
4390+
QDomElement rootNode = doc.createElement( "qgis" );
4391+
rootNode.setAttribute( "version", QString( "%1" ).arg( QGis::QGIS_VERSION ) );
4392+
doc.appendChild( rootNode );
4393+
QString errorMsg;
4394+
if ( !selectionLayer->writeSymbology( rootNode, doc, errorMsg ) )
4395+
{
4396+
QMessageBox::warning( this,
4397+
tr( "Error" ),
4398+
tr( "Cannot copy style: %1" )
4399+
.arg( errorMsg ),
4400+
QMessageBox::Ok );
4401+
return;
4402+
}
4403+
QClipboard* clipboard = QApplication::clipboard();
4404+
QMimeData *mimeData = new QMimeData();
4405+
mimeData->setData( "application/qgis.style", doc.toByteArray() );
4406+
// transfers the ownership to the clipboard object
4407+
clipboard->setMimeData( mimeData );
4408+
}
4409+
}
4410+
4411+
void QgisApp::pasteStyle( QgsMapLayer * destinationLayer )
4412+
{
4413+
QgsMapLayer *selectionLayer = destinationLayer ? destinationLayer : activeLayer();
4414+
if ( selectionLayer )
4415+
{
4416+
QClipboard* clipboard = QApplication::clipboard();
4417+
4418+
const QMimeData* mimeData = clipboard->mimeData();
4419+
if ( mimeData->hasFormat( "application/qgis.style" ) )
4420+
{
4421+
QDomDocument doc( "qgis" );
4422+
QString errorMsg;
4423+
int errorLine, errorColumn;
4424+
if ( !doc.setContent ( mimeData->data( "application/qgis.style" ), false, &errorMsg, &errorLine, &errorColumn ) )
4425+
{
4426+
QMessageBox::information( this,
4427+
tr( "Error" ),
4428+
tr( "Cannot parse style: %1:%2:%3" )
4429+
.arg( errorMsg )
4430+
.arg( errorLine )
4431+
.arg( errorColumn ),
4432+
QMessageBox::Ok );
4433+
return;
4434+
}
4435+
QDomElement rootNode = doc.firstChildElement( "qgis" );
4436+
if ( !selectionLayer->readSymbology( rootNode, errorMsg ) )
4437+
{
4438+
QMessageBox::information( this,
4439+
tr( "Error" ),
4440+
tr( "Cannot read style: %1" )
4441+
.arg( errorMsg ),
4442+
QMessageBox::Ok );
4443+
return;
4444+
}
4445+
4446+
mMapLegend->refreshLayerSymbology( selectionLayer->id(), false );
4447+
}
4448+
}
4449+
}
43794450

43804451
void QgisApp::pasteTransformations()
43814452
{
@@ -6299,6 +6370,8 @@ void QgisApp::activateDeactivateLayerRelatedActions( QgsMapLayer* layer )
62996370
mActionCutFeatures->setEnabled( false );
63006371
mActionCopyFeatures->setEnabled( false );
63016372
mActionPasteFeatures->setEnabled( false );
6373+
mActionCopyStyle->setEnabled( false );
6374+
mActionPasteStyle->setEnabled( false );
63026375

63036376
mActionUndo->setEnabled( false );
63046377
mActionRedo->setEnabled( false );
@@ -6329,6 +6402,9 @@ void QgisApp::activateDeactivateLayerRelatedActions( QgsMapLayer* layer )
63296402
mActionAddToOverview->setEnabled( true );
63306403
mActionZoomToLayer->setEnabled( true );
63316404

6405+
mActionCopyStyle->setEnabled( true );
6406+
mActionPasteStyle->setEnabled( QApplication::clipboard()->mimeData()->hasFormat( "application/qgis.style" ) );
6407+
63326408
/***********Vector layers****************/
63336409
if ( layer->type() == QgsMapLayer::VectorLayer )
63346410
{

‎src/app/qgisapp.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -429,6 +429,17 @@ class QgisApp : public QMainWindow, private Ui::MainWindow
429429
*/
430430
void editPaste( QgsMapLayer * destinationLayer = 0 );
431431

432+
/**
433+
\param sourceLayer The layer where the style will be taken from
434+
(defaults to the active layer on the legend)
435+
*/
436+
void copyStyle( QgsMapLayer * sourceLayer = 0 );
437+
//! copies style on the clipboard to the active layer
438+
/**
439+
\param destinatioLayer The layer that the clipboard will be pasted to
440+
(defaults to the active layer on the legend)
441+
*/
442+
void pasteStyle( QgsMapLayer * destinationLayer = 0 );
432443
void loadOGRSublayers( QString layertype, QString uri, QStringList list );
433444
void loadGDALSublayers( QString uri, QStringList list );
434445

‎src/ui/qgisapp.ui

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
<x>0</x>
1818
<y>0</y>
1919
<width>1052</width>
20-
<height>21</height>
20+
<height>25</height>
2121
</rect>
2222
</property>
2323
<widget class="QMenu" name="mEditMenu">
@@ -152,6 +152,9 @@
152152
<addaction name="mActionAddLayerSeparator"/>
153153
<addaction name="mActionAddWfsLayer"/>
154154
<addaction name="separator"/>
155+
<addaction name="mActionCopyStyle"/>
156+
<addaction name="mActionPasteStyle"/>
157+
<addaction name="separator"/>
155158
<addaction name="mActionOpenTable"/>
156159
<addaction name="mActionSaveEdits"/>
157160
<addaction name="mActionToggleEditing"/>
@@ -1648,6 +1651,24 @@
16481651
<string>Offset Curve</string>
16491652
</property>
16501653
</action>
1654+
<action name="mActionCopyStyle">
1655+
<property name="icon">
1656+
<iconset resource="../../images/images.qrc">
1657+
<normaloff>:/images/themes/default/mActionEditCopy.png</normaloff>:/images/themes/default/mActionEditCopy.png</iconset>
1658+
</property>
1659+
<property name="text">
1660+
<string>Copy style</string>
1661+
</property>
1662+
</action>
1663+
<action name="mActionPasteStyle">
1664+
<property name="icon">
1665+
<iconset resource="../../images/images.qrc">
1666+
<normaloff>:/images/themes/default/mActionEditPaste.png</normaloff>:/images/themes/default/mActionEditPaste.png</iconset>
1667+
</property>
1668+
<property name="text">
1669+
<string>Paste style</string>
1670+
</property>
1671+
</action>
16511672
</widget>
16521673
<resources>
16531674
<include location="../../images/images.qrc"/>

0 commit comments

Comments
 (0)
Please sign in to comment.