|
15 | 15 | * *
|
16 | 16 | ***************************************************************************/
|
17 | 17 |
|
| 18 | +#include <QApplication> |
18 | 19 | #include <QMainWindow>
|
19 | 20 | #include <QMouseEvent>
|
20 | 21 | #include <QKeyEvent>
|
| 22 | +#include <QClipboard> |
| 23 | +#include <QMimeData> |
21 | 24 |
|
22 | 25 | #include "qgscomposerview.h"
|
23 | 26 | #include "qgscomposerarrow.h"
|
@@ -394,40 +397,56 @@ void QgsComposerView::keyPressEvent( QKeyEvent * e )
|
394 | 397 | QList<QgsComposerItem*> composerItemList = composition()->selectedComposerItems();
|
395 | 398 | QList<QgsComposerItem*>::iterator itemIt = composerItemList.begin();
|
396 | 399 |
|
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 ) ) |
399 | 401 | {
|
| 402 | + QDomDocument doc; |
| 403 | + QDomElement documentElement = doc.createElement( "ComposerItemClipboard" ); |
400 | 404 | for ( ; itemIt != composerItemList.end(); ++itemIt )
|
401 | 405 | {
|
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() ) |
404 | 409 | {
|
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 ) |
426 | 413 | {
|
427 |
| - emit itemRemoved( *itemIt ); |
428 |
| - pushAddRemoveCommand( *itemIt, tr( "Item deleted" ), QgsAddRemoveItemCommand::Removed ); |
| 414 | + ( *it )->writeXML( documentElement, doc ); |
429 | 415 | }
|
430 | 416 | }
|
| 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 ); |
431 | 450 | }
|
432 | 451 | }
|
433 | 452 |
|
@@ -626,6 +645,133 @@ void QgsComposerView::addComposerTable( QgsComposerAttributeTable* table )
|
626 | 645 | pushAddRemoveCommand( table, tr( "Table added" ) );
|
627 | 646 | }
|
628 | 647 |
|
| 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 | + |
629 | 775 | void QgsComposerView::groupItems()
|
630 | 776 | {
|
631 | 777 | if ( !composition() )
|
|
0 commit comments