Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Add/Remove command for composer multipage
  • Loading branch information
mhugent committed Jul 31, 2012
1 parent eb08414 commit ec0a02b
Show file tree
Hide file tree
Showing 8 changed files with 156 additions and 3 deletions.
1 change: 1 addition & 0 deletions src/core/CMakeLists.txt
Expand Up @@ -112,6 +112,7 @@ SET(QGIS_CORE_SRCS
qgsnetworkaccessmanager.cpp

composer/qgsaddremoveitemcommand.cpp
composer/qgsaddremovemultiframecommand.cpp
composer/qgscomposerarrow.cpp
composer/qgscomposerframe.cpp
composer/qgscomposeritem.cpp
Expand Down
86 changes: 86 additions & 0 deletions src/core/composer/qgsaddremovemultiframecommand.cpp
@@ -0,0 +1,86 @@
/***************************************************************************
qgsaddremovemultiframecommand.cpp
---------------------------------
begin : 2012-07-31
copyright : (C) 2012 by Marco Hugentobler
email : marco dot hugentobler at sourcepole dot ch
***************************************************************************/

/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/

#include "qgsaddremovemultiframecommand.h"
#include "qgscomposermultiframe.h"
#include "qgscomposition.h"


QgsAddRemoveMultiFrameCommand::QgsAddRemoveMultiFrameCommand( State s, QgsComposerMultiFrame* multiFrame, QgsComposition* c, const QString& text, QUndoCommand* parent ):
QUndoCommand( text, parent ), mMultiFrame( multiFrame ), mComposition( c ), mState(s), mFirstRun( true )
{
}

QgsAddRemoveMultiFrameCommand::QgsAddRemoveMultiFrameCommand(): mMultiFrame( 0 ), mComposition( 0 )
{
}

QgsAddRemoveMultiFrameCommand::~QgsAddRemoveMultiFrameCommand()
{
if( mState == Removed )
{
delete mMultiFrame;
}
}

void QgsAddRemoveMultiFrameCommand::redo()
{
if( checkFirstRun() )
{
return;
}
switchState();
}

void QgsAddRemoveMultiFrameCommand::undo()
{
if( checkFirstRun() )
{
return;
}
switchState();
}

void QgsAddRemoveMultiFrameCommand::switchState()
{
if( mComposition )
{
if( mState == Added )
{
mComposition->removeMultiFrame( mMultiFrame );
mState = Removed;
}
else
{
mComposition->addMultiFrame( mMultiFrame );
mState = Added;
}
}
}

bool QgsAddRemoveMultiFrameCommand::checkFirstRun()
{
if( mFirstRun )
{
mFirstRun = false;
return true;
}
else
{
return false;
}
}
54 changes: 54 additions & 0 deletions src/core/composer/qgsaddremovemultiframecommand.h
@@ -0,0 +1,54 @@
/***************************************************************************
qgsaddremovemultiframecommand.h
-------------------------------
begin : 2012-07-31
copyright : (C) 2012 by Marco Hugentobler
email : marco dot hugentobler at sourcepole dot ch
***************************************************************************/

/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/

#ifndef QGSADDREMOVEMULTIFRAMECOMMAND_H
#define QGSADDREMOVEMULTIFRAMECOMMAND_H

#include <QUndoCommand>

class QgsComposerMultiFrame;
class QgsComposition;

class CORE_EXPORT QgsAddRemoveMultiFrameCommand: public QUndoCommand
{
public:

enum State
{
Added = 0,
Removed
};

QgsAddRemoveMultiFrameCommand( State s, QgsComposerMultiFrame* multiFrame, QgsComposition* c, const QString& text, QUndoCommand* parent = 0 );
~QgsAddRemoveMultiFrameCommand();
void redo();
void undo();

private:
QgsAddRemoveMultiFrameCommand();

//changes between added / removed state
void switchState();
bool checkFirstRun();

QgsComposerMultiFrame* mMultiFrame;
QgsComposition* mComposition;
State mState;
bool mFirstRun;
};

#endif // QGSADDREMOVEMULTIFRAMECOMMAND_H
1 change: 0 additions & 1 deletion src/core/composer/qgscomposerhtml.cpp
Expand Up @@ -112,7 +112,6 @@ void QgsComposerHtml::addFrame( QgsComposerFrame* frame )
if ( mComposition )
{
mComposition->addComposerHtmlFrame( this, frame );
mComposition->pushAddRemoveCommand( frame, tr( "HTML frame added" ) );
}
}

Expand Down
3 changes: 3 additions & 0 deletions src/core/composer/qgscomposermultiframe.cpp
Expand Up @@ -15,6 +15,7 @@

#include "qgscomposermultiframe.h"
#include "qgscomposerframe.h"
#include "qgsaddremovemultiframecommand.h"

QgsComposerMultiFrame::QgsComposerMultiFrame( QgsComposition* c ): mComposition( c ), mResizeMode( UseExistingFrames )
{
Expand Down Expand Up @@ -124,6 +125,8 @@ void QgsComposerMultiFrame::handleFrameRemoval( QgsComposerItem* item )
{
//schedule this composer multi frame for deletion
mComposition->removeMultiFrame( this );
QgsAddRemoveMultiFrameCommand* c = new QgsAddRemoveMultiFrameCommand( QgsAddRemoveMultiFrameCommand::Removed, this, mComposition, tr( "Html removed" ), 0 );
mComposition->undoStack()->push( c );
}
}
else
Expand Down
7 changes: 6 additions & 1 deletion src/core/composer/qgscomposition.cpp
Expand Up @@ -1034,10 +1034,15 @@ void QgsComposition::cancelCommand()
mActiveCommand = 0;
}

void QgsComposition::addMultiFrame( QgsComposerMultiFrame* multiFrame )
{
mMultiFrames.insert( multiFrame );
}

void QgsComposition::removeMultiFrame( QgsComposerMultiFrame* multiFrame )
{
mMultiFrames.remove( multiFrame );
delete multiFrame; //e.v. use deleteLater() in case of stability problems
//delete multiFrame; //e.v. use deleteLater() in case of stability problems
}

void QgsComposition::addComposerArrow( QgsComposerArrow* arrow )
Expand Down
3 changes: 2 additions & 1 deletion src/core/composer/qgscomposition.h
Expand Up @@ -202,7 +202,8 @@ class CORE_EXPORT QgsComposition: public QGraphicsScene
/**Deletes current command*/
void cancelCommand();

void addMultiFrame( QgsComposerMultiFrame* multiFrame ) { mMultiFrames.insert( multiFrame ); }
void addMultiFrame( QgsComposerMultiFrame* multiFrame );
/**Removes multi frame (but does not delete it)*/
void removeMultiFrame( QgsComposerMultiFrame* multiFrame );
/**Adds an arrow item to the graphics scene and advices composer to create a widget for it (through signal)*/
void addComposerArrow( QgsComposerArrow* arrow );
Expand Down
4 changes: 4 additions & 0 deletions src/gui/qgscomposerview.cpp
Expand Up @@ -34,6 +34,7 @@
#include "qgscomposershape.h"
#include "qgscomposerattributetable.h"
#include "qgslogger.h"
#include "qgsaddremovemultiframecommand.h"

QgsComposerView::QgsComposerView( QWidget* parent, const char* name, Qt::WFlags f )
: QGraphicsView( parent )
Expand Down Expand Up @@ -328,6 +329,9 @@ void QgsComposerView::mouseReleaseEvent( QMouseEvent* e )
delete mRubberBandItem;
mRubberBandItem = 0;
emit actionFinished();

QgsAddRemoveMultiFrameCommand* c = new QgsAddRemoveMultiFrameCommand( QgsAddRemoveMultiFrameCommand::Added, composerHtml, composition(), tr( "HTML added" ), 0 );
composition()->undoStack()->push( c );
}
default:
break;
Expand Down

0 comments on commit ec0a02b

Please sign in to comment.