composerClipboard.diff

Anna Kratochvilova, 2011-11-11 11:08 AM

Download (11 KB)

View differences:

src/gui/qgscomposerview.cpp
18 18
#include <QMainWindow>
19 19
#include <QMouseEvent>
20 20
#include <QKeyEvent>
21
#include <QClipboard>
22
#include <QMimeData>
23
#include <QCursor>
21 24

  
22 25
#include "qgscomposerview.h"
23 26
#include "qgscomposerarrow.h"
......
30 33
#include "qgscomposershape.h"
31 34
#include "qgscomposerattributetable.h"
32 35
#include "qgslogger.h"
36
#include "qgsapplication.h"
33 37

  
34 38
QgsComposerView::QgsComposerView( QWidget* parent, const char* name, Qt::WFlags f )
35 39
    : QGraphicsView( parent )
......
394 398
  QList<QgsComposerItem*> composerItemList = composition()->selectedComposerItems();
395 399
  QList<QgsComposerItem*>::iterator itemIt = composerItemList.begin();
396 400

  
397
  //delete selected items
398
  if ( e->key() == Qt::Key_Delete || e->key() == Qt::Key_Backspace )
401
  if ( e->matches( QKeySequence::Copy ) || e->matches( QKeySequence::Cut ) )
399 402
  {
403
    QDomDocument doc;
404
    QDomElement documentElement = doc.createElement( "ComposerCopyItem" );//?
400 405
    for ( ; itemIt != composerItemList.end(); ++itemIt )
401 406
    {
402
      QgsComposerMap* map = dynamic_cast<QgsComposerMap *>( *itemIt );
403
      if ( !map || !map->isDrawing() ) //don't delete a composer map while it draws
407
      // copy each item in a group
408
      QgsComposerItemGroup* itemGroup = dynamic_cast<QgsComposerItemGroup*>( *itemIt );
409
      if ( itemGroup && composition() )
404 410
      {
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
411
        QSet<QgsComposerItem*> groupedItems = itemGroup->items();
412
        QSet<QgsComposerItem*>::iterator it = groupedItems.begin();
413
        for ( ; it != groupedItems.end(); ++it )
426 414
        {
427
          emit itemRemoved( *itemIt );
428
          pushAddRemoveCommand( *itemIt, tr( "Item deleted" ), QgsAddRemoveItemCommand::Removed );
415
          ( *it )->writeXML( documentElement, doc );
429 416
        }
430 417
      }
418
      ( *itemIt )->writeXML( documentElement, doc );
419

  
420
      if ( e->matches( QKeySequence::Cut ) )
421
      {
422
        removeItem( *itemIt );
423
      }
424
    }
425

  
426
    doc.appendChild( documentElement );
427
    QMimeData *mimeData = new QMimeData;
428
    mimeData->setData( "text/xml", doc.toByteArray() );
429
    QClipboard *clipboard = QApplication::clipboard();
430
    clipboard->setMimeData( mimeData );
431
  }
432

  
433
  if ( e->matches( QKeySequence::Paste ) )
434
  {
435
    QDomDocument doc;
436
    QClipboard *clipboard = QApplication::clipboard();
437
    if ( doc.setContent( clipboard->mimeData()->data("text/xml") ) )
438
    {
439
      QDomElement docElem = doc.documentElement();
440
      if (docElem.tagName() == "ComposerCopyItem")
441
      {
442
        addItemsfromXML( docElem, doc );
443
      }
444
    }
445
  }
446
  //delete selected items
447
  if ( e->key() == Qt::Key_Delete || e->key() == Qt::Key_Backspace )
448
  {
449
    for ( ; itemIt != composerItemList.end(); ++itemIt )
450
    {
451
      removeItem(*itemIt);
431 452
    }
432 453
  }
433 454

  
......
673 694
  }
674 695
}
675 696

  
697
void QgsComposerView::removeItem( QgsComposerItem* item )
698
{
699
    QgsComposerMap* map = dynamic_cast<QgsComposerMap *>( item );
700
    if ( !map || !map->isDrawing() ) //don't delete a composer map while it draws
701
    {
702
      composition()->removeItem( item );
703
      QgsComposerItemGroup* itemGroup = dynamic_cast<QgsComposerItemGroup*>( item );
704
      if ( itemGroup && composition() )
705
      {
706
        //add add/remove item command for every item in the group
707
        QUndoCommand* parentCommand = new QUndoCommand( tr( "Remove item group" ) );
708

  
709
        QSet<QgsComposerItem*> groupedItems = itemGroup->items();
710
        QSet<QgsComposerItem*>::iterator it = groupedItems.begin();
711
        for ( ; it != groupedItems.end(); ++it )
712
        {
713
          QgsAddRemoveItemCommand* subcommand = new QgsAddRemoveItemCommand( QgsAddRemoveItemCommand::Removed, *it, composition(), "", parentCommand );
714
          connectAddRemoveCommandSignals( subcommand );
715
          emit itemRemoved( *it );
716
        }
717

  
718
        composition()->undoStack()->push( parentCommand );
719
        delete itemGroup;
720
        emit itemRemoved( itemGroup );
721
      }
722
      else
723
      {
724
        emit itemRemoved( item );
725
        pushAddRemoveCommand( item, tr( "Item deleted" ), QgsAddRemoveItemCommand::Removed );
726
      }
727
    }
728
}
729

  
676 730
void QgsComposerView::sendItemAddedSignal( QgsComposerItem* item )
677 731
{
678 732
  //cast and send proper signal
......
778 832
  connectAddRemoveCommandSignals( c );
779 833
  composition()->undoStack()->push( c );
780 834
}
835

  
836
void QgsComposerView::addItemsfromXML( const QDomElement& docElem, const QDomDocument& doc)
837
{
838
  QPointF scenePoint = mapToScene( mapFromGlobal(QCursor::pos()) );
839

  
840
  // label
841
  QDomNodeList composerLabelList = docElem.elementsByTagName( "ComposerLabel" );
842
  for ( int i = 0; i < composerLabelList.size(); ++i )
843
  {
844
    QDomElement currentComposerLabelElem = composerLabelList.at( i ).toElement();
845
    QgsComposerLabel* newLabel = new QgsComposerLabel( composition() );
846
    newLabel->readXML( currentComposerLabelElem, doc );
847
    newLabel->setItemPosition( scenePoint.x(), scenePoint.y() );
848
    addComposerLabel( newLabel );
849
    emit actionFinished();
850
  }
851
  // arrow
852
  QDomNodeList composerArrowList = docElem.elementsByTagName( "ComposerArrow" );
853
  for ( int i = 0; i < composerArrowList.size(); ++i )
854
  {
855
    QDomElement currentComposerArrowElem = composerArrowList.at( i ).toElement();
856
    QgsComposerArrow* newArrow = new QgsComposerArrow( composition() );
857
    newArrow->readXML( currentComposerArrowElem, doc );
858
    newArrow->setItemPosition( scenePoint.x(), scenePoint.y() );
859
    addComposerArrow( newArrow );
860
    emit actionFinished();
861
  }
862
  // scalebar
863
  QDomNodeList composerScaleBarList = docElem.elementsByTagName( "ComposerScaleBar" );
864
  for ( int i = 0; i < composerScaleBarList.size(); ++i )
865
  {
866
    QDomElement currentComposerScaleBarElem = composerScaleBarList.at( i ).toElement();
867
    QgsComposerScaleBar* newScaleBar = new QgsComposerScaleBar( composition() );
868
    newScaleBar->readXML( currentComposerScaleBarElem, doc );
869
    newScaleBar->setItemPosition( scenePoint.x(), scenePoint.y() );
870
    addComposerScaleBar( newScaleBar );
871
    emit actionFinished();
872
  }
873
  // shape
874
  QDomNodeList composerShapeList = docElem.elementsByTagName( "ComposerShape" );
875
  for ( int i = 0; i < composerShapeList.size(); ++i )
876
  {
877
    QDomElement currentComposerShapeElem = composerShapeList.at( i ).toElement();
878
    QgsComposerShape* newShape = new QgsComposerShape( composition() );
879
    newShape->readXML( currentComposerShapeElem, doc );
880
    newShape->setItemPosition( scenePoint.x(), scenePoint.y() );
881
    addComposerShape( newShape );
882
    emit actionFinished();
883
  }
884
  // picture
885
  QDomNodeList composerPictureList = docElem.elementsByTagName( "ComposerPicture" );
886
  for ( int i = 0; i < composerPictureList.size(); ++i )
887
  {
888
    QDomElement currentComposerPictureElem = composerPictureList.at( i ).toElement();
889
    QgsComposerPicture* newPicture = new QgsComposerPicture( composition() );
890
    newPicture->readXML( currentComposerPictureElem, doc );
891
    newPicture->setItemPosition( scenePoint.x(), scenePoint.y() );
892
    addComposerPicture( newPicture );
893
    emit actionFinished();
894
  }
895
  // legend
896
  QDomNodeList composerLegendList = docElem.elementsByTagName( "ComposerLegend" );
897
  for ( int i = 0; i < composerLegendList.size(); ++i )
898
  {
899
    QDomElement currentComposerLegendElem = composerLegendList.at( i ).toElement();
900
    QgsComposerLegend* newLegend = new QgsComposerLegend( composition() );
901
    newLegend->readXML( currentComposerLegendElem, doc );
902
    newLegend->setItemPosition( scenePoint.x(), scenePoint.y() );
903
    addComposerLegend( newLegend );
904
    emit actionFinished();
905
  }
906
  // table
907
  QDomNodeList composerTableList = docElem.elementsByTagName( "ComposerAttributeTable" );
908
  for ( int i = 0; i < composerTableList.size(); ++i )
909
  {
910
    QDomElement currentComposerTableElem = composerTableList.at( i ).toElement();
911
    QgsComposerAttributeTable* newTable = new QgsComposerAttributeTable( composition() );
912
    newTable->readXML( currentComposerTableElem, doc );
913
    newTable->setItemPosition( scenePoint.x(), scenePoint.y() );
914
    addComposerTable( newTable );
915
    emit actionFinished();
916
  }
917
  // table
918
  QDomNodeList composerMapList = docElem.elementsByTagName( "ComposerMap" );
919
  for ( int i = 0; i < composerMapList.size(); ++i )
920
  {
921
    QDomElement currentComposerMapElem = composerMapList.at( i ).toElement();
922
    QgsComposerMap* newMap = new QgsComposerMap( composition() );
923
    newMap->readXML( currentComposerMapElem, doc );
924
    newMap->setItemPosition( scenePoint.x(), scenePoint.y() );
925
    addComposerMap( newMap );
926
    emit actionFinished();
927
  }
928
}
src/gui/qgscomposerview.h
18 18
#define QGSCOMPOSERVIEW_H
19 19

  
20 20
#include <QGraphicsView>
21
#include <QDomDocument>
22
#include <QDomElement>
21 23
#include "qgsaddremoveitemcommand.h"
22 24

  
23 25
class QKeyEvent;
......
95 97
    /**Adds a composer table to the graphics scene and advices composer to create a widget for it (through signal)*/
96 98
    void addComposerTable( QgsComposerAttributeTable* table );
97 99

  
100
    /**Remove item from the graphics scene*/
101
    void removeItem( QgsComposerItem* item );
102

  
98 103
    /**Returns the composer main window*/
99 104
    QMainWindow* composerWindow();
100 105

  
......
104 109
    /**Convenience function to create a QgsAddRemoveItemCommand, connect its signals and push it to the undo stack*/
105 110
    void pushAddRemoveCommand( QgsComposerItem* item, const QString& text, QgsAddRemoveItemCommand::State state = QgsAddRemoveItemCommand::Added );
106 111

  
112
    /**Add items from XML representation to the graphics scene (for pasting items from clipboard)*/
113
    void addItemsfromXML( const QDomElement& docElem, const QDomDocument& doc );
114

  
107 115
  protected:
108 116
    void mousePressEvent( QMouseEvent* );
109 117
    void mouseReleaseEvent( QMouseEvent* );