diff --git a/src/app/composer/qgscomposer.cpp b/src/app/composer/qgscomposer.cpp old mode 100755 new mode 100644 index 378195347290..bf55bce7ff68 --- a/src/app/composer/qgscomposer.cpp +++ b/src/app/composer/qgscomposer.cpp @@ -201,10 +201,10 @@ QgsComposer::QgsComposer( QgisApp *qgis, const QString& title ): QMainWindow(), //create composer view mView = new QgsComposerView( mViewFrame ); - connectSlots(); //init undo/redo buttons mComposition = new QgsComposition( mQgis->mapCanvas()->mapRenderer() ); + mActionUndo->setEnabled( false ); mActionRedo->setEnabled( false ); if ( mComposition->undoStack() ) @@ -213,6 +213,8 @@ QgsComposer::QgsComposer( QgisApp *qgis, const QString& title ): QMainWindow(), connect( mComposition->undoStack(), SIGNAL( canRedoChanged( bool ) ), mActionRedo, SLOT( setEnabled( bool ) ) ); } + connectSlots(); + mComposition->setParent( mView ); mView->setComposition( mComposition ); @@ -324,6 +326,15 @@ void QgsComposer::connectSlots() connect( mView, SIGNAL( composerArrowAdded( QgsComposerArrow* ) ), this, SLOT( addComposerArrow( QgsComposerArrow* ) ) ); connect( mView, SIGNAL( composerTableAdded( QgsComposerAttributeTable* ) ), this, SLOT( addComposerTable( QgsComposerAttributeTable* ) ) ); connect( mView, SIGNAL( actionFinished() ), this, SLOT( setSelectionTool() ) ); + + connect( mComposition, SIGNAL( composerArrowAdded( QgsComposerArrow* ) ), this, SLOT( addComposerArrow( QgsComposerArrow* ) ) ); + connect( mComposition, SIGNAL( composerLabelAdded( QgsComposerLabel* ) ), this, SLOT( addComposerLabel( QgsComposerLabel* ) ) ); + connect( mComposition, SIGNAL( composerMapAdded( QgsComposerMap* ) ), this, SLOT( addComposerMap( QgsComposerMap* ) ) ); + connect( mComposition, SIGNAL( composerScaleBarAdded( QgsComposerScaleBar* ) ), this, SLOT( addComposerScaleBar( QgsComposerScaleBar* ) ) ); + connect( mComposition, SIGNAL( composerLegendAdded( QgsComposerLegend* ) ), this, SLOT( addComposerLegend( QgsComposerLegend* ) ) ); + connect( mComposition, SIGNAL( composerPictureAdded( QgsComposerPicture* ) ), this, SLOT( addComposerPicture( QgsComposerPicture* ) ) ); + connect( mComposition, SIGNAL( composerShapeAdded( QgsComposerShape* ) ), this, SLOT( addComposerShape( QgsComposerShape* ) ) ); + connect( mComposition, SIGNAL( composerTableAdded( QgsComposerAttributeTable* ) ), this, SLOT( addComposerTable( QgsComposerAttributeTable* ) ) ); } void QgsComposer::open( void ) @@ -1306,7 +1317,6 @@ void QgsComposer::readXML( const QDomElement& composerElem, const QDomDocument& mCompositionOptionsLayout = 0; mView = new QgsComposerView( mViewFrame ); - connectSlots(); //read composition settings mComposition = new QgsComposition( mQgis->mapCanvas()->mapRenderer() ); @@ -1317,6 +1327,8 @@ void QgsComposer::readXML( const QDomElement& composerElem, const QDomDocument& mComposition->readXML( compositionElem, doc ); } + connectSlots(); + QGridLayout *l = new QGridLayout( mViewFrame ); l->setMargin( 0 ); l->addWidget( mView, 0, 0 ); @@ -1331,7 +1343,12 @@ void QgsComposer::readXML( const QDomElement& composerElem, const QDomDocument& mCompositionOptionsLayout->addWidget( compositionWidget ); //read and restore all the items + if ( mComposition ) + { + mComposition->addItemsFromXML( composerElem, doc ); + } +#if 0 //composer labels QDomNodeList composerLabelList = composerElem.elementsByTagName( "ComposerLabel" ); for ( int i = 0; i < composerLabelList.size(); ++i ) @@ -1465,6 +1482,7 @@ void QgsComposer::readXML( const QDomElement& composerElem, const QDomDocument& newTable->setSelected( true ); showItemOptions( newTable ); } +#endif //0 mComposition->sortZList(); mView->setComposition( mComposition ); diff --git a/src/core/composer/qgscomposition.cpp b/src/core/composer/qgscomposition.cpp index 41f26d7f8da1..21589edd5594 100644 --- a/src/core/composer/qgscomposition.cpp +++ b/src/core/composer/qgscomposition.cpp @@ -18,6 +18,15 @@ #include "qgscomposeritem.h" #include "qgscomposermap.h" #include "qgspaperitem.h" +#include "qgscomposerarrow.h" +#include "qgscomposerlabel.h" +#include "qgscomposerlegend.h" +#include "qgscomposermap.h" +#include "qgscomposeritemgroup.h" +#include "qgscomposerpicture.h" +#include "qgscomposerscalebar.h" +#include "qgscomposershape.h" +#include "qgscomposerattributetable.h" #include "qgslogger.h" #include #include @@ -252,6 +261,113 @@ bool QgsComposition::readXML( const QDomElement& compositionElem, const QDomDocu return true; } +void QgsComposition::addItemsFromXML( const QDomElement& elem, const QDomDocument& doc, QPointF* pos ) +{ + QDomNodeList composerLabelList = elem.elementsByTagName( "ComposerLabel" ); + for ( int i = 0; i < composerLabelList.size(); ++i ) + { + QDomElement currentComposerLabelElem = composerLabelList.at( i ).toElement(); + QgsComposerLabel* newLabel = new QgsComposerLabel( this ); + newLabel->readXML( currentComposerLabelElem, doc ); + if ( pos ) + { + newLabel->setItemPosition( pos->x(), pos->y() ); + } + addComposerLabel( newLabel ); + } + // map + QDomNodeList composerMapList = elem.elementsByTagName( "ComposerMap" ); + for ( int i = 0; i < composerMapList.size(); ++i ) + { + QDomElement currentComposerMapElem = composerMapList.at( i ).toElement(); + QgsComposerMap* newMap = new QgsComposerMap( this ); + newMap->readXML( currentComposerMapElem, doc ); + if ( pos ) + { + newMap->setItemPosition( pos->x(), pos->y() ); + } + addComposerMap( newMap ); + } + // arrow + QDomNodeList composerArrowList = elem.elementsByTagName( "ComposerArrow" ); + for ( int i = 0; i < composerArrowList.size(); ++i ) + { + QDomElement currentComposerArrowElem = composerArrowList.at( i ).toElement(); + QgsComposerArrow* newArrow = new QgsComposerArrow( this ); + newArrow->readXML( currentComposerArrowElem, doc ); + if ( pos ) + { + newArrow->setItemPosition( pos->x(), pos->y() ); + } + addComposerArrow( newArrow ); + } + // scalebar + QDomNodeList composerScaleBarList = elem.elementsByTagName( "ComposerScaleBar" ); + for ( int i = 0; i < composerScaleBarList.size(); ++i ) + { + QDomElement currentComposerScaleBarElem = composerScaleBarList.at( i ).toElement(); + QgsComposerScaleBar* newScaleBar = new QgsComposerScaleBar( this ); + newScaleBar->readXML( currentComposerScaleBarElem, doc ); + if ( pos ) + { + newScaleBar->setItemPosition( pos->x(), pos->y() ); + } + addComposerScaleBar( newScaleBar ); + } + // shape + QDomNodeList composerShapeList = elem.elementsByTagName( "ComposerShape" ); + for ( int i = 0; i < composerShapeList.size(); ++i ) + { + QDomElement currentComposerShapeElem = composerShapeList.at( i ).toElement(); + QgsComposerShape* newShape = new QgsComposerShape( this ); + newShape->readXML( currentComposerShapeElem, doc ); + if ( pos ) + { + newShape->setItemPosition( pos->x(), pos->y() ); + } + addComposerShape( newShape ); + } + // picture + QDomNodeList composerPictureList = elem.elementsByTagName( "ComposerPicture" ); + for ( int i = 0; i < composerPictureList.size(); ++i ) + { + QDomElement currentComposerPictureElem = composerPictureList.at( i ).toElement(); + QgsComposerPicture* newPicture = new QgsComposerPicture( this ); + newPicture->readXML( currentComposerPictureElem, doc ); + if ( pos ) + { + newPicture->setItemPosition( pos->x(), pos->y() ); + } + addComposerPicture( newPicture ); + } + // legend + QDomNodeList composerLegendList = elem.elementsByTagName( "ComposerLegend" ); + for ( int i = 0; i < composerLegendList.size(); ++i ) + { + QDomElement currentComposerLegendElem = composerLegendList.at( i ).toElement(); + QgsComposerLegend* newLegend = new QgsComposerLegend( this ); + newLegend->readXML( currentComposerLegendElem, doc ); + if ( pos ) + { + newLegend->setItemPosition( pos->x(), pos->y() ); + } + addComposerLegend( newLegend ); + } + // table + QDomNodeList composerTableList = elem.elementsByTagName( "ComposerAttributeTable" ); + for ( int i = 0; i < composerTableList.size(); ++i ) + { + QDomElement currentComposerTableElem = composerTableList.at( i ).toElement(); + QgsComposerAttributeTable* newTable = new QgsComposerAttributeTable( this ); + newTable->readXML( currentComposerTableElem, doc ); + if ( pos ) + { + newTable->setItemPosition( pos->x(), pos->y() ); + } + addComposerTable( newTable ); + } +} + void QgsComposition::addItemToZList( QgsComposerItem* item ) { if ( !item ) @@ -833,3 +949,99 @@ void QgsComposition::cancelCommand() delete mActiveCommand; mActiveCommand = 0; } + +void QgsComposition::addComposerArrow( QgsComposerArrow* arrow ) +{ + addItem( arrow ); + emit composerArrowAdded( arrow ); + clearSelection(); + arrow->setSelected( true ); + emit selectedItemChanged( arrow ); + //pushAddRemoveCommand( arrow, tr( "Arrow added" ) ); +} + +void QgsComposition::addComposerLabel( QgsComposerLabel* label ) +{ + addItem( label ); + emit composerLabelAdded( label ); + clearSelection(); + label->setSelected( true ); + emit selectedItemChanged( label ); + //pushAddRemoveCommand( label, tr( "Label added" ) ); +} + +void QgsComposition::addComposerMap( QgsComposerMap* map ) +{ + addItem( map ); + //set default preview mode to cache. Must be done here between adding composer map to scene and emiting signal + map->setPreviewMode( QgsComposerMap::Cache ); + map->cache(); + emit composerMapAdded( map ); + clearSelection(); + map->setSelected( true ); + emit selectedItemChanged( map ); + //pushAddRemoveCommand( map, tr( "Map added" ) ); +} + +void QgsComposition::addComposerScaleBar( QgsComposerScaleBar* scaleBar ) +{ + //take first available map + QList mapItemList = composerMapItems(); + if ( mapItemList.size() > 0 ) + { + scaleBar->setComposerMap( mapItemList.at( 0 ) ); + } + scaleBar->applyDefaultSize(); //4 segments, 1/5 of composer map width + addItem( scaleBar ); + emit composerScaleBarAdded( scaleBar ); + clearSelection(); + scaleBar->setSelected( true ); + emit selectedItemChanged( scaleBar ); + //pushAddRemoveCommand( scaleBar, tr( "Scale bar added" ) ); +} + +void QgsComposition::addComposerLegend( QgsComposerLegend* legend ) +{ + //take first available map + QList mapItemList = composerMapItems(); + if ( mapItemList.size() > 0 ) + { + legend->setComposerMap( mapItemList.at( 0 ) ); + } + addItem( legend ); + emit composerLegendAdded( legend ); + clearSelection(); + legend->setSelected( true ); + emit selectedItemChanged( legend ); + //pushAddRemoveCommand( legend, tr( "Legend added" ) ); +} + +void QgsComposition::addComposerPicture( QgsComposerPicture* picture ) +{ + addItem( picture ); + emit composerPictureAdded( picture ); + clearSelection(); + picture->setSelected( true ); + emit selectedItemChanged( picture ); + //pushAddRemoveCommand( picture, tr( "Picture added" ) ); +} + +void QgsComposition::addComposerShape( QgsComposerShape* shape ) +{ + addItem( shape ); + emit composerShapeAdded( shape ); + clearSelection(); + shape->setSelected( true ); + emit selectedItemChanged( shape ); + //pushAddRemoveCommand( shape, tr( "Shape added" ) ); +} + +void QgsComposition::addComposerTable( QgsComposerAttributeTable* table ) +{ + addItem( table ); + emit composerTableAdded( table ); + clearSelection(); + table->setSelected( true ); + emit selectedItemChanged( table ); + //pushAddRemoveCommand( table, tr( "Table added" ) ); +} diff --git a/src/core/composer/qgscomposition.h b/src/core/composer/qgscomposition.h index c47977ccef9b..e9fe18b97edd 100644 --- a/src/core/composer/qgscomposition.h +++ b/src/core/composer/qgscomposition.h @@ -28,8 +28,16 @@ class QgsComposerMap; class QgsPaperItem; class QGraphicsRectItem; class QgsMapRenderer; - class QDomElement; +class QgsComposerArrow; +class QgsComposerItem; +class QgsComposerLabel; +class QgsComposerLegend; +class QgsComposerMap; +class QgsComposerPicture; +class QgsComposerScaleBar; +class QgsComposerShape; +class QgsComposerAttributeTable; /** \ingroup MapComposer * Graphics scene for map printing. The class manages the paper item which always @@ -132,6 +140,12 @@ class CORE_EXPORT QgsComposition: public QGraphicsScene /**Reads settings from xml file*/ bool readXML( const QDomElement& compositionElem, const QDomDocument& doc ); + /**Add items from XML representation to the graphics scene (for project file reading, pasting items from clipboard) + @param elem items parent element, e.g. or + @param doc xml document + @param pos item position. Optional, take position from xml if 0*/ + void addItemsFromXML( const QDomElement& elem, const QDomDocument& doc, QPointF* pos = 0 ); + /**Adds item to z list. Usually called from constructor of QgsComposerItem*/ void addItemToZList( QgsComposerItem* item ); /**Removes item from z list. Usually called from destructor of QgsComposerItem*/ @@ -173,6 +187,22 @@ class CORE_EXPORT QgsComposition: public QGraphicsScene /**Deletes current command*/ void cancelCommand(); + /**Adds an arrow item to the graphics scene and advices composer to create a widget for it (through signal)*/ + void addComposerArrow( QgsComposerArrow* arrow ); + /**Adds label to the graphics scene and advices composer to create a widget for it (through signal)*/ + void addComposerLabel( QgsComposerLabel* label ); + /**Adds map to the graphics scene and advices composer to create a widget for it (through signal)*/ + void addComposerMap( QgsComposerMap* map ); + /**Adds scale bar to the graphics scene and advices composer to create a widget for it (through signal)*/ + void addComposerScaleBar( QgsComposerScaleBar* scaleBar ); + /**Adds legend to the graphics scene and advices composer to create a widget for it (through signal)*/ + void addComposerLegend( QgsComposerLegend* legend ); + /**Adds picture to the graphics scene and advices composer to create a widget for it (through signal)*/ + void addComposerPicture( QgsComposerPicture* picture ); + /**Adds a composer shape to the graphics scene and advices composer to create a widget for it (through signal)*/ + void addComposerShape( QgsComposerShape* shape ); + /**Adds a composer table to the graphics scene and advices composer to create a widget for it (through signal)*/ + void addComposerTable( QgsComposerAttributeTable* table ); private: /**Pointer to map renderer of QGIS main map*/ @@ -218,6 +248,25 @@ class CORE_EXPORT QgsComposition: public QGraphicsScene signals: void paperSizeChanged(); + + /**Is emitted when selected item changed. If 0, no item is selected*/ + void selectedItemChanged( QgsComposerItem* selected ); + /**Is emitted when new composer arrow has been added to the view*/ + void composerArrowAdded( QgsComposerArrow* arrow ); + /**Is emitted when new composer label has been added to the view*/ + void composerLabelAdded( QgsComposerLabel* label ); + /**Is emitted when new composer map has been added to the view*/ + void composerMapAdded( QgsComposerMap* map ); + /**Is emitted when new composer scale bar has been added*/ + void composerScaleBarAdded( QgsComposerScaleBar* scalebar ); + /**Is emitted when a new composer legend has been added*/ + void composerLegendAdded( QgsComposerLegend* legend ); + /**Is emitted when a new composer picture has been added*/ + void composerPictureAdded( QgsComposerPicture* picture ); + /**Is emitted when a new composer shape has been added*/ + void composerShapeAdded( QgsComposerShape* shape ); + /**Is emitted when a new composer table has been added*/ + void composerTableAdded( QgsComposerAttributeTable* table ); }; #endif diff --git a/src/gui/qgscomposerview.cpp b/src/gui/qgscomposerview.cpp index 2ff079d1e747..c26f70b7d956 100644 --- a/src/gui/qgscomposerview.cpp +++ b/src/gui/qgscomposerview.cpp @@ -436,7 +436,11 @@ void QgsComposerView::keyPressEvent( QKeyEvent * e ) QDomElement docElem = doc.documentElement(); if ( docElem.tagName() == "ComposerItemClipboard" ) { - addItemsfromXML( docElem, doc ); + if ( composition() ) + { + QPointF pt = mapToScene( mapFromGlobal( QCursor::pos() ) ); + composition()->addItemsFromXML( docElem, doc, &pt ); + } } } } @@ -678,100 +682,6 @@ void QgsComposerView::removeItem( QgsComposerItem* item ) } } -void QgsComposerView::addItemsfromXML( const QDomElement& docElem, const QDomDocument& doc ) -{ - QPointF scenePoint = mapToScene( mapFromGlobal( QCursor::pos() ) ); - - // label - QDomNodeList composerLabelList = docElem.elementsByTagName( "ComposerLabel" ); - for ( int i = 0; i < composerLabelList.size(); ++i ) - { - QDomElement currentComposerLabelElem = composerLabelList.at( i ).toElement(); - QgsComposerLabel* newLabel = new QgsComposerLabel( composition() ); - newLabel->readXML( currentComposerLabelElem, doc ); - newLabel->setItemPosition( scenePoint.x(), scenePoint.y() ); - addComposerLabel( newLabel ); - emit actionFinished(); - } - // map - QDomNodeList composerMapList = docElem.elementsByTagName( "ComposerMap" ); - for ( int i = 0; i < composerMapList.size(); ++i ) - { - QDomElement currentComposerMapElem = composerMapList.at( i ).toElement(); - QgsComposerMap* newMap = new QgsComposerMap( composition() ); - newMap->readXML( currentComposerMapElem, doc ); - newMap->setItemPosition( scenePoint.x(), scenePoint.y() ); - addComposerMap( newMap ); - emit actionFinished(); - } - // arrow - QDomNodeList composerArrowList = docElem.elementsByTagName( "ComposerArrow" ); - for ( int i = 0; i < composerArrowList.size(); ++i ) - { - QDomElement currentComposerArrowElem = composerArrowList.at( i ).toElement(); - QgsComposerArrow* newArrow = new QgsComposerArrow( composition() ); - newArrow->readXML( currentComposerArrowElem, doc ); - newArrow->setItemPosition( scenePoint.x(), scenePoint.y() ); - addComposerArrow( newArrow ); - emit actionFinished(); - } - // scalebar - QDomNodeList composerScaleBarList = docElem.elementsByTagName( "ComposerScaleBar" ); - for ( int i = 0; i < composerScaleBarList.size(); ++i ) - { - QDomElement currentComposerScaleBarElem = composerScaleBarList.at( i ).toElement(); - QgsComposerScaleBar* newScaleBar = new QgsComposerScaleBar( composition() ); - newScaleBar->readXML( currentComposerScaleBarElem, doc ); - newScaleBar->setItemPosition( scenePoint.x(), scenePoint.y() ); - addComposerScaleBar( newScaleBar ); - emit actionFinished(); - } - // shape - QDomNodeList composerShapeList = docElem.elementsByTagName( "ComposerShape" ); - for ( int i = 0; i < composerShapeList.size(); ++i ) - { - QDomElement currentComposerShapeElem = composerShapeList.at( i ).toElement(); - QgsComposerShape* newShape = new QgsComposerShape( composition() ); - newShape->readXML( currentComposerShapeElem, doc ); - newShape->setItemPosition( scenePoint.x(), scenePoint.y() ); - addComposerShape( newShape ); - emit actionFinished(); - } - // picture - QDomNodeList composerPictureList = docElem.elementsByTagName( "ComposerPicture" ); - for ( int i = 0; i < composerPictureList.size(); ++i ) - { - QDomElement currentComposerPictureElem = composerPictureList.at( i ).toElement(); - QgsComposerPicture* newPicture = new QgsComposerPicture( composition() ); - newPicture->readXML( currentComposerPictureElem, doc ); - newPicture->setItemPosition( scenePoint.x(), scenePoint.y() ); - addComposerPicture( newPicture ); - emit actionFinished(); - } - // legend - QDomNodeList composerLegendList = docElem.elementsByTagName( "ComposerLegend" ); - for ( int i = 0; i < composerLegendList.size(); ++i ) - { - QDomElement currentComposerLegendElem = composerLegendList.at( i ).toElement(); - QgsComposerLegend* newLegend = new QgsComposerLegend( composition() ); - newLegend->readXML( currentComposerLegendElem, doc ); - newLegend->setItemPosition( scenePoint.x(), scenePoint.y() ); - addComposerLegend( newLegend ); - emit actionFinished(); - } - // table - QDomNodeList composerTableList = docElem.elementsByTagName( "ComposerAttributeTable" ); - for ( int i = 0; i < composerTableList.size(); ++i ) - { - QDomElement currentComposerTableElem = composerTableList.at( i ).toElement(); - QgsComposerAttributeTable* newTable = new QgsComposerAttributeTable( composition() ); - newTable->readXML( currentComposerTableElem, doc ); - newTable->setItemPosition( scenePoint.x(), scenePoint.y() ); - addComposerTable( newTable ); - emit actionFinished(); - } -} - void QgsComposerView::groupItems() { if ( !composition() ) diff --git a/src/gui/qgscomposerview.h b/src/gui/qgscomposerview.h index 4ab9884a9f94..872b05b5c515 100644 --- a/src/gui/qgscomposerview.h +++ b/src/gui/qgscomposerview.h @@ -100,9 +100,6 @@ class GUI_EXPORT QgsComposerView: public QGraphicsView /**Remove item from the graphics scene*/ void removeItem( QgsComposerItem* item ); - /**Add items from XML representation to the graphics scene (for pasting items from clipboard)*/ - void addItemsfromXML( const QDomElement& docElem, const QDomDocument& doc ); - /**Returns the composer main window*/ QMainWindow* composerWindow();