Skip to content

Commit 376d45b

Browse files
author
marco
committedNov 29, 2011
[FEATURE]: Copy paste for composer item. Provided by Anna Kratochvilova in ticket #4070
1 parent 66e2ab0 commit 376d45b

File tree

2 files changed

+181
-27
lines changed

2 files changed

+181
-27
lines changed
 

‎src/gui/qgscomposerview.cpp

Lines changed: 173 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,12 @@
1515
* *
1616
***************************************************************************/
1717

18+
#include <QApplication>
1819
#include <QMainWindow>
1920
#include <QMouseEvent>
2021
#include <QKeyEvent>
22+
#include <QClipboard>
23+
#include <QMimeData>
2124

2225
#include "qgscomposerview.h"
2326
#include "qgscomposerarrow.h"
@@ -394,40 +397,56 @@ void QgsComposerView::keyPressEvent( QKeyEvent * e )
394397
QList<QgsComposerItem*> composerItemList = composition()->selectedComposerItems();
395398
QList<QgsComposerItem*>::iterator itemIt = composerItemList.begin();
396399

397-
//delete selected items
398-
if ( e->key() == Qt::Key_Delete || e->key() == Qt::Key_Backspace )
400+
if ( e->matches( QKeySequence::Copy ) || e->matches( QKeySequence::Cut ) )
399401
{
402+
QDomDocument doc;
403+
QDomElement documentElement = doc.createElement( "ComposerItemClipboard" );
400404
for ( ; itemIt != composerItemList.end(); ++itemIt )
401405
{
402-
QgsComposerMap* map = dynamic_cast<QgsComposerMap *>( *itemIt );
403-
if ( !map || !map->isDrawing() ) //don't delete a composer map while it draws
406+
// copy each item in a group
407+
QgsComposerItemGroup* itemGroup = dynamic_cast<QgsComposerItemGroup*>( *itemIt );
408+
if ( itemGroup && composition() )
404409
{
405-
composition()->removeItem( *itemIt );
406-
QgsComposerItemGroup* itemGroup = dynamic_cast<QgsComposerItemGroup*>( *itemIt );
407-
if ( itemGroup && composition() )
408-
{
409-
//add add/remove item command for every item in the group
410-
QUndoCommand* parentCommand = new QUndoCommand( tr( "Remove item group" ) );
411-
412-
QSet<QgsComposerItem*> groupedItems = itemGroup->items();
413-
QSet<QgsComposerItem*>::iterator it = groupedItems.begin();
414-
for ( ; it != groupedItems.end(); ++it )
415-
{
416-
QgsAddRemoveItemCommand* subcommand = new QgsAddRemoveItemCommand( QgsAddRemoveItemCommand::Removed, *it, composition(), "", parentCommand );
417-
connectAddRemoveCommandSignals( subcommand );
418-
emit itemRemoved( *it );
419-
}
420-
421-
composition()->undoStack()->push( parentCommand );
422-
delete itemGroup;
423-
emit itemRemoved( itemGroup );
424-
}
425-
else
410+
QSet<QgsComposerItem*> groupedItems = itemGroup->items();
411+
QSet<QgsComposerItem*>::iterator it = groupedItems.begin();
412+
for ( ; it != groupedItems.end(); ++it )
426413
{
427-
emit itemRemoved( *itemIt );
428-
pushAddRemoveCommand( *itemIt, tr( "Item deleted" ), QgsAddRemoveItemCommand::Removed );
414+
( *it )->writeXML( documentElement, doc );
429415
}
430416
}
417+
( *itemIt )->writeXML( documentElement, doc );
418+
if ( e->matches( QKeySequence::Cut ) )
419+
{
420+
removeItem( *itemIt );
421+
}
422+
}
423+
doc.appendChild( documentElement );
424+
QMimeData *mimeData = new QMimeData;
425+
mimeData->setData( "text/xml", doc.toByteArray() );
426+
QClipboard *clipboard = QApplication::clipboard();
427+
clipboard->setMimeData( mimeData );
428+
}
429+
430+
if ( e->matches( QKeySequence::Paste ) )
431+
{
432+
QDomDocument doc;
433+
QClipboard *clipboard = QApplication::clipboard();
434+
if ( doc.setContent( clipboard->mimeData()->data( "text/xml" ) ) )
435+
{
436+
QDomElement docElem = doc.documentElement();
437+
if ( docElem.tagName() == "ComposerItemClipboard" )
438+
{
439+
addItemsfromXML( docElem, doc );
440+
}
441+
}
442+
}
443+
444+
//delete selected items
445+
if ( e->key() == Qt::Key_Delete || e->key() == Qt::Key_Backspace )
446+
{
447+
for ( ; itemIt != composerItemList.end(); ++itemIt )
448+
{
449+
removeItem( *itemIt );
431450
}
432451
}
433452

@@ -626,6 +645,133 @@ void QgsComposerView::addComposerTable( QgsComposerAttributeTable* table )
626645
pushAddRemoveCommand( table, tr( "Table added" ) );
627646
}
628647

648+
void QgsComposerView::removeItem( QgsComposerItem* item )
649+
{
650+
QgsComposerMap* map = dynamic_cast<QgsComposerMap *>( item );
651+
if ( !map || !map->isDrawing() ) //don't delete a composer map while it draws
652+
{
653+
composition()->removeItem( item );
654+
QgsComposerItemGroup* itemGroup = dynamic_cast<QgsComposerItemGroup*>( item );
655+
if ( itemGroup && composition() )
656+
{
657+
//add add/remove item command for every item in the group
658+
QUndoCommand* parentCommand = new QUndoCommand( tr( "Remove item group" ) );
659+
660+
QSet<QgsComposerItem*> groupedItems = itemGroup->items();
661+
QSet<QgsComposerItem*>::iterator it = groupedItems.begin();
662+
for ( ; it != groupedItems.end(); ++it )
663+
{
664+
QgsAddRemoveItemCommand* subcommand = new QgsAddRemoveItemCommand( QgsAddRemoveItemCommand::Removed, *it, composition(), "", parentCommand );
665+
connectAddRemoveCommandSignals( subcommand );
666+
emit itemRemoved( *it );
667+
}
668+
669+
composition()->undoStack()->push( parentCommand );
670+
delete itemGroup;
671+
emit itemRemoved( itemGroup );
672+
}
673+
else
674+
{
675+
emit itemRemoved( item );
676+
pushAddRemoveCommand( item, tr( "Item deleted" ), QgsAddRemoveItemCommand::Removed );
677+
}
678+
}
679+
}
680+
681+
void QgsComposerView::addItemsfromXML( const QDomElement& docElem, const QDomDocument& doc )
682+
{
683+
QPointF scenePoint = mapToScene( mapFromGlobal( QCursor::pos() ) );
684+
685+
// label
686+
QDomNodeList composerLabelList = docElem.elementsByTagName( "ComposerLabel" );
687+
for ( int i = 0; i < composerLabelList.size(); ++i )
688+
{
689+
QDomElement currentComposerLabelElem = composerLabelList.at( i ).toElement();
690+
QgsComposerLabel* newLabel = new QgsComposerLabel( composition() );
691+
newLabel->readXML( currentComposerLabelElem, doc );
692+
newLabel->setItemPosition( scenePoint.x(), scenePoint.y() );
693+
addComposerLabel( newLabel );
694+
emit actionFinished();
695+
}
696+
// map
697+
QDomNodeList composerMapList = docElem.elementsByTagName( "ComposerMap" );
698+
for ( int i = 0; i < composerMapList.size(); ++i )
699+
{
700+
QDomElement currentComposerMapElem = composerMapList.at( i ).toElement();
701+
QgsComposerMap* newMap = new QgsComposerMap( composition() );
702+
newMap->readXML( currentComposerMapElem, doc );
703+
newMap->setItemPosition( scenePoint.x(), scenePoint.y() );
704+
addComposerMap( newMap );
705+
emit actionFinished();
706+
}
707+
// arrow
708+
QDomNodeList composerArrowList = docElem.elementsByTagName( "ComposerArrow" );
709+
for ( int i = 0; i < composerArrowList.size(); ++i )
710+
{
711+
QDomElement currentComposerArrowElem = composerArrowList.at( i ).toElement();
712+
QgsComposerArrow* newArrow = new QgsComposerArrow( composition() );
713+
newArrow->readXML( currentComposerArrowElem, doc );
714+
newArrow->setItemPosition( scenePoint.x(), scenePoint.y() );
715+
addComposerArrow( newArrow );
716+
emit actionFinished();
717+
}
718+
// scalebar
719+
QDomNodeList composerScaleBarList = docElem.elementsByTagName( "ComposerScaleBar" );
720+
for ( int i = 0; i < composerScaleBarList.size(); ++i )
721+
{
722+
QDomElement currentComposerScaleBarElem = composerScaleBarList.at( i ).toElement();
723+
QgsComposerScaleBar* newScaleBar = new QgsComposerScaleBar( composition() );
724+
newScaleBar->readXML( currentComposerScaleBarElem, doc );
725+
newScaleBar->setItemPosition( scenePoint.x(), scenePoint.y() );
726+
addComposerScaleBar( newScaleBar );
727+
emit actionFinished();
728+
}
729+
// shape
730+
QDomNodeList composerShapeList = docElem.elementsByTagName( "ComposerShape" );
731+
for ( int i = 0; i < composerShapeList.size(); ++i )
732+
{
733+
QDomElement currentComposerShapeElem = composerShapeList.at( i ).toElement();
734+
QgsComposerShape* newShape = new QgsComposerShape( composition() );
735+
newShape->readXML( currentComposerShapeElem, doc );
736+
newShape->setItemPosition( scenePoint.x(), scenePoint.y() );
737+
addComposerShape( newShape );
738+
emit actionFinished();
739+
}
740+
// picture
741+
QDomNodeList composerPictureList = docElem.elementsByTagName( "ComposerPicture" );
742+
for ( int i = 0; i < composerPictureList.size(); ++i )
743+
{
744+
QDomElement currentComposerPictureElem = composerPictureList.at( i ).toElement();
745+
QgsComposerPicture* newPicture = new QgsComposerPicture( composition() );
746+
newPicture->readXML( currentComposerPictureElem, doc );
747+
newPicture->setItemPosition( scenePoint.x(), scenePoint.y() );
748+
addComposerPicture( newPicture );
749+
emit actionFinished();
750+
}
751+
// legend
752+
QDomNodeList composerLegendList = docElem.elementsByTagName( "ComposerLegend" );
753+
for ( int i = 0; i < composerLegendList.size(); ++i )
754+
{
755+
QDomElement currentComposerLegendElem = composerLegendList.at( i ).toElement();
756+
QgsComposerLegend* newLegend = new QgsComposerLegend( composition() );
757+
newLegend->readXML( currentComposerLegendElem, doc );
758+
newLegend->setItemPosition( scenePoint.x(), scenePoint.y() );
759+
addComposerLegend( newLegend );
760+
emit actionFinished();
761+
}
762+
// table
763+
QDomNodeList composerTableList = docElem.elementsByTagName( "ComposerAttributeTable" );
764+
for ( int i = 0; i < composerTableList.size(); ++i )
765+
{
766+
QDomElement currentComposerTableElem = composerTableList.at( i ).toElement();
767+
QgsComposerAttributeTable* newTable = new QgsComposerAttributeTable( composition() );
768+
newTable->readXML( currentComposerTableElem, doc );
769+
newTable->setItemPosition( scenePoint.x(), scenePoint.y() );
770+
addComposerTable( newTable );
771+
emit actionFinished();
772+
}
773+
}
774+
629775
void QgsComposerView::groupItems()
630776
{
631777
if ( !composition() )

‎src/gui/qgscomposerview.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
#include <QGraphicsView>
2121
#include "qgsaddremoveitemcommand.h"
2222

23+
class QDomDocument;
24+
class QDomElement;
2325
class QKeyEvent;
2426
class QMainWindow;
2527
class QMouseEvent;
@@ -95,6 +97,12 @@ class GUI_EXPORT QgsComposerView: public QGraphicsView
9597
/**Adds a composer table to the graphics scene and advices composer to create a widget for it (through signal)*/
9698
void addComposerTable( QgsComposerAttributeTable* table );
9799

100+
/**Remove item from the graphics scene*/
101+
void removeItem( QgsComposerItem* item );
102+
103+
/**Add items from XML representation to the graphics scene (for pasting items from clipboard)*/
104+
void addItemsfromXML( const QDomElement& docElem, const QDomDocument& doc );
105+
98106
/**Returns the composer main window*/
99107
QMainWindow* composerWindow();
100108

0 commit comments

Comments
 (0)
Please sign in to comment.