Skip to content

Commit

Permalink
Move more item related functions from composerview to composeritem
Browse files Browse the repository at this point in the history
  • Loading branch information
marco committed Dec 7, 2011
1 parent a5f15d8 commit d4c497b
Show file tree
Hide file tree
Showing 6 changed files with 97 additions and 79 deletions.
23 changes: 23 additions & 0 deletions python/core/qgscomposition.sip
Original file line number Diff line number Diff line change
Expand Up @@ -142,4 +142,27 @@ class QgsComposition: QGraphicsScene
void endCommand();
/**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 );

/**Remove item from the graphics scene. Additionally to QGraphicsScene::removeItem, this function considers undo/redo command*/
void removeComposerItem( QgsComposerItem* item );

/**Convenience function to create a QgsAddRemoveItemCommand, connect its signals and push it to the undo stack*/
void pushAddRemoveCommand( QgsComposerItem* item, const QString& text, QgsAddRemoveItemCommand::State state );
};
3 changes: 0 additions & 3 deletions python/gui/qgscomposerview.sip
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,6 @@ class QgsComposerView: QGraphicsView
void setPaintingEnabled( bool enabled );
bool paintingEnabled() const;

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

protected:
void mousePressEvent( QMouseEvent* );
void mouseReleaseEvent( QMouseEvent* );
Expand Down
51 changes: 51 additions & 0 deletions src/core/composer/qgscomposition.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1046,6 +1046,57 @@ void QgsComposition::addComposerTable( QgsComposerAttributeTable* table )
//pushAddRemoveCommand( table, tr( "Table added" ) );
}

void QgsComposition::removeComposerItem( QgsComposerItem* item )
{
QgsComposerMap* map = dynamic_cast<QgsComposerMap *>( item );
if ( !map || !map->isDrawing() ) //don't delete a composer map while it draws
{
removeItem( item );
QgsComposerItemGroup* itemGroup = dynamic_cast<QgsComposerItemGroup*>( item );
if ( itemGroup )
{
//add add/remove item command for every item in the group
QUndoCommand* parentCommand = new QUndoCommand( tr( "Remove item group" ) );

QSet<QgsComposerItem*> groupedItems = itemGroup->items();
QSet<QgsComposerItem*>::iterator it = groupedItems.begin();
for ( ; it != groupedItems.end(); ++it )
{
QgsAddRemoveItemCommand* subcommand = new QgsAddRemoveItemCommand( QgsAddRemoveItemCommand::Removed, *it, this, "", parentCommand );
connectAddRemoveCommandSignals( subcommand );
emit itemRemoved( *it );
}

undoStack()->push( parentCommand );
delete itemGroup;
emit itemRemoved( itemGroup );
}
else
{
emit itemRemoved( item );
pushAddRemoveCommand( item, tr( "Item deleted" ), QgsAddRemoveItemCommand::Removed );
}
}
}

void QgsComposition::pushAddRemoveCommand( QgsComposerItem* item, const QString& text, QgsAddRemoveItemCommand::State state )
{
QgsAddRemoveItemCommand* c = new QgsAddRemoveItemCommand( state, item, this, text );
connectAddRemoveCommandSignals( c );
undoStack()->push( c );
}

void QgsComposition::connectAddRemoveCommandSignals( QgsAddRemoveItemCommand* c )
{
if ( !c )
{
return;
}

QObject::connect( c, SIGNAL( itemRemoved( QgsComposerItem* ) ), this, SIGNAL( itemRemoved( QgsComposerItem* ) ) );
QObject::connect( c, SIGNAL( itemAdded( QgsComposerItem* ) ), this, SLOT( sendItemAddedSignal( QgsComposerItem* ) ) );
}

void QgsComposition::sendItemAddedSignal( QgsComposerItem* item )
{
//cast and send proper signal
Expand Down
9 changes: 9 additions & 0 deletions src/core/composer/qgscomposition.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include <QUndoStack>

#include "qgscomposeritemcommand.h"
#include "qgsaddremoveitemcommand.h"

class QgsComposerItem;
class QgsComposerMap;
Expand Down Expand Up @@ -204,6 +205,12 @@ class CORE_EXPORT QgsComposition: public QGraphicsScene
/**Adds a composer table to the graphics scene and advices composer to create a widget for it (through signal)*/
void addComposerTable( QgsComposerAttributeTable* table );

/**Remove item from the graphics scene. Additionally to QGraphicsScene::removeItem, this function considers undo/redo command*/
void removeComposerItem( QgsComposerItem* item );

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

public slots:
/**Casts object to the proper subclass type and calls corresponding itemAdded signal*/
void sendItemAddedSignal( QgsComposerItem* item );
Expand Down Expand Up @@ -250,6 +257,8 @@ class CORE_EXPORT QgsComposition: public QGraphicsScene
void loadSettings();
void saveSettings();

void connectAddRemoveCommandSignals( QgsAddRemoveItemCommand* c );

signals:
void paperSizeChanged();

Expand Down
82 changes: 13 additions & 69 deletions src/gui/qgscomposerview.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ void QgsComposerView::mousePressEvent( QMouseEvent* e )
newLabelItem->setSceneRect( QRectF( snappedScenePoint.x(), snappedScenePoint.y(), newLabelItem->rect().width(), newLabelItem->rect().height() ) );
composition()->addComposerLabel( newLabelItem );
emit actionFinished();
pushAddRemoveCommand( newLabelItem, tr( "Label added" ) );
composition()->pushAddRemoveCommand( newLabelItem, tr( "Label added" ) );
}
break;

Expand All @@ -162,7 +162,7 @@ void QgsComposerView::mousePressEvent( QMouseEvent* e )
}
newScaleBar->applyDefaultSize(); //4 segments, 1/5 of composer map width
emit actionFinished();
pushAddRemoveCommand( newScaleBar, tr( "Scale bar added" ) );
composition()->pushAddRemoveCommand( newScaleBar, tr( "Scale bar added" ) );
}
break;

Expand All @@ -175,7 +175,7 @@ void QgsComposerView::mousePressEvent( QMouseEvent* e )
composition()->addComposerLegend( newLegend );
newLegend->updateLegend();
emit actionFinished();
pushAddRemoveCommand( newLegend, tr( "Legend added" ) );
composition()->pushAddRemoveCommand( newLegend, tr( "Legend added" ) );
}
break;
}
Expand All @@ -186,7 +186,7 @@ void QgsComposerView::mousePressEvent( QMouseEvent* e )
newPicture->setSceneRect( QRectF( snappedScenePoint.x(), snappedScenePoint.y(), 30, 30 ) );
composition()->addComposerPicture( newPicture );
emit actionFinished();
pushAddRemoveCommand( newPicture, tr( "Picture added" ) );
composition()->pushAddRemoveCommand( newPicture, tr( "Picture added" ) );
}
break;
case AddTable:
Expand All @@ -196,7 +196,7 @@ void QgsComposerView::mousePressEvent( QMouseEvent* e )
newTable->setSceneRect( QRectF( snappedScenePoint.x(), snappedScenePoint.y(), 50, 50 ) );
composition()->addComposerTable( newTable );
emit actionFinished();
pushAddRemoveCommand( newTable, tr( "Table added" ) );
composition()->pushAddRemoveCommand( newTable, tr( "Table added" ) );
}
break;
default:
Expand Down Expand Up @@ -253,7 +253,7 @@ void QgsComposerView::mouseReleaseEvent( QMouseEvent* e )
delete mRubberBandLineItem;
mRubberBandLineItem = 0;
emit actionFinished();
pushAddRemoveCommand( composerArrow, tr( "Arrow added" ) );
composition()->pushAddRemoveCommand( composerArrow, tr( "Arrow added" ) );
}
break;

Expand All @@ -273,7 +273,7 @@ void QgsComposerView::mouseReleaseEvent( QMouseEvent* e )
delete mRubberBandItem;
mRubberBandItem = 0;
emit actionFinished();
pushAddRemoveCommand( composerShape, tr( "Shape added" ) );
composition()->pushAddRemoveCommand( composerShape, tr( "Shape added" ) );
}
break;

Expand All @@ -296,7 +296,7 @@ void QgsComposerView::mouseReleaseEvent( QMouseEvent* e )
delete mRubberBandItem;
mRubberBandItem = 0;
emit actionFinished();
pushAddRemoveCommand( composerMap, tr( "Map added" ) );
composition()->pushAddRemoveCommand( composerMap, tr( "Map added" ) );
}
break;

Expand Down Expand Up @@ -439,7 +439,7 @@ void QgsComposerView::keyPressEvent( QKeyEvent * e )
( *itemIt )->writeXML( documentElement, doc );
if ( e->matches( QKeySequence::Cut ) )
{
removeItem( *itemIt );
composition()->removeComposerItem( *itemIt );
}
}
doc.appendChild( documentElement );
Expand Down Expand Up @@ -472,7 +472,10 @@ void QgsComposerView::keyPressEvent( QKeyEvent * e )
{
for ( ; itemIt != composerItemList.end(); ++itemIt )
{
removeItem( *itemIt );
if ( composition() )
{
composition()->removeComposerItem( *itemIt );
}
}
}

Expand Down Expand Up @@ -575,39 +578,6 @@ QgsComposition* QgsComposerView::composition()
return 0;
}

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

QSet<QgsComposerItem*> groupedItems = itemGroup->items();
QSet<QgsComposerItem*>::iterator it = groupedItems.begin();
for ( ; it != groupedItems.end(); ++it )
{
QgsAddRemoveItemCommand* subcommand = new QgsAddRemoveItemCommand( QgsAddRemoveItemCommand::Removed, *it, composition(), "", parentCommand );
connectAddRemoveCommandSignals( subcommand );
emit itemRemoved( *it );
}

composition()->undoStack()->push( parentCommand );
delete itemGroup;
emit itemRemoved( itemGroup );
}
else
{
emit itemRemoved( item );
pushAddRemoveCommand( item, tr( "Item deleted" ), QgsAddRemoveItemCommand::Removed );
}
}
}

void QgsComposerView::groupItems()
{
if ( !composition() )
Expand Down Expand Up @@ -676,29 +646,3 @@ QMainWindow* QgsComposerView::composerWindow()

return 0;
}

void QgsComposerView::connectAddRemoveCommandSignals( QgsAddRemoveItemCommand* c )
{
if ( !c )
{
return;
}

if ( composition() )
{
QObject::connect( c, SIGNAL( itemRemoved( QgsComposerItem* ) ), composition(), SIGNAL( itemRemoved( QgsComposerItem* ) ) );
QObject::connect( c, SIGNAL( itemAdded( QgsComposerItem* ) ), composition(), SLOT( sendItemAddedSignal( QgsComposerItem* ) ) );
}
}

void QgsComposerView::pushAddRemoveCommand( QgsComposerItem* item, const QString& text, QgsAddRemoveItemCommand::State state )
{
if ( !composition() )
{
return;
}

QgsAddRemoveItemCommand* c = new QgsAddRemoveItemCommand( state, item, composition(), text );
connectAddRemoveCommandSignals( c );
composition()->undoStack()->push( c );
}
8 changes: 1 addition & 7 deletions src/gui/qgscomposerview.h
Original file line number Diff line number Diff line change
Expand Up @@ -80,18 +80,12 @@ class GUI_EXPORT QgsComposerView: public QGraphicsView
/**Returns the composition or 0 in case of error*/
QgsComposition* composition();

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

/**Returns the composer main window*/
QMainWindow* composerWindow();

void setPaintingEnabled( bool enabled ) { mPaintingEnabled = enabled; }
bool paintingEnabled() const { return mPaintingEnabled; }

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

protected:
void mousePressEvent( QMouseEvent* );
void mouseReleaseEvent( QMouseEvent* );
Expand Down Expand Up @@ -126,7 +120,7 @@ class GUI_EXPORT QgsComposerView: public QGraphicsView

bool mPaintingEnabled;

void connectAddRemoveCommandSignals( QgsAddRemoveItemCommand* c );
//void connectAddRemoveCommandSignals( QgsAddRemoveItemCommand* c );

signals:
/**Is emitted when selected item changed. If 0, no item is selected*/
Expand Down

0 comments on commit d4c497b

Please sign in to comment.