Skip to content
Permalink
Browse files
Move loading of items to qgscomposition to avoid duplicated code
  • Loading branch information
marco committed Dec 4, 2011
1 parent bde0427 commit 59c28db602f65b1a7f4b4c91cef04dbaa2b93076
Showing with 287 additions and 101 deletions.
  1. +20 −2 src/app/composer/qgscomposer.cpp
  2. +212 −0 src/core/composer/qgscomposition.cpp
  3. +50 −1 src/core/composer/qgscomposition.h
  4. +5 −95 src/gui/qgscomposerview.cpp
  5. +0 −3 src/gui/qgscomposerview.h
22 src/app/composer/qgscomposer.cpp 100755 → 100644
@@ -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 );
@@ -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 <QDomDocument>
#include <QDomElement>
@@ -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<const QgsComposerMap*> 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<const QgsComposerMap*> 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" ) );
}
@@ -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. <Composer> or <ComposerItemClipboard>
@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

0 comments on commit 59c28db

Please sign in to comment.