Skip to content
Permalink
Browse files

Further changes to undo-mechanism. Currently not working

  • Loading branch information
mhugent committed Aug 8, 2012
1 parent b2782c0 commit b40d0af2e6e71b6c73d3978dc0bc5584d03c8305
@@ -21,6 +21,10 @@ QgsComposerFrame::QgsComposerFrame( QgsComposition* c, QgsComposerMultiFrame* mf
{
}

QgsComposerFrame::QgsComposerFrame(): QgsComposerItem( 0, 0, 0, 0, 0 ), mMultiFrame( 0 )
{
}

QgsComposerFrame::~QgsComposerFrame()
{
}
@@ -70,3 +74,19 @@ void QgsComposerFrame::paint( QPainter* painter, const QStyleOptionGraphicsItem*
drawSelectionBoxes( painter );
}
}

void QgsComposerFrame::beginItemCommand( const QString& text )
{
if ( mComposition )
{
mComposition->beginMultiFrameCommand( multiFrame(), text );
}
}

void QgsComposerFrame::endItemCommand()
{
if ( mComposition )
{
mComposition->endMultiFrameCommand();
}
}
@@ -33,13 +33,17 @@ class QgsComposerFrame: public QgsComposerItem

void paint( QPainter* painter, const QStyleOptionGraphicsItem* itemStyle, QWidget* pWidget );

void beginItemCommand( const QString& text );
void endItemCommand();

bool writeXML( QDomElement& elem, QDomDocument & doc ) const;
bool readXML( const QDomElement& itemElem, const QDomDocument& doc );

int type() const { return ComposerFrame; }

QgsComposerMultiFrame* multiFrame() { return mMultiFrame; }


private:
QgsComposerFrame(); //forbidden

@@ -22,8 +22,8 @@
#include <QWebFrame>
#include <QWebPage>

QgsComposerHtml::QgsComposerHtml( QgsComposition* c, qreal x, qreal y, qreal width, qreal height ): QgsComposerMultiFrame( c ), mWebPage( 0 ),
mLoaded( false ), mHtmlUnitsToMM( 1.0 )
QgsComposerHtml::QgsComposerHtml( QgsComposition* c, qreal x, qreal y, qreal width, qreal height, bool createUndoCommands ): QgsComposerMultiFrame( c, createUndoCommands ),
mWebPage( 0 ), mLoaded( false ), mHtmlUnitsToMM( 1.0 )
{
mHtmlUnitsToMM = htmlUnitsToMM();
mWebPage = new QWebPage();
@@ -38,7 +38,7 @@ QgsComposerHtml::QgsComposerHtml( QgsComposition* c, qreal x, qreal y, qreal wid
}
}

QgsComposerHtml::QgsComposerHtml(): QgsComposerMultiFrame( 0 ), mWebPage( 0 ), mLoaded( false ), mHtmlUnitsToMM( 1.0 )
QgsComposerHtml::QgsComposerHtml(): QgsComposerMultiFrame( 0, false ), mWebPage( 0 ), mLoaded( false ), mHtmlUnitsToMM( 1.0 )
{
}

@@ -126,6 +126,7 @@ bool QgsComposerHtml::writeXML( QDomElement& elem, QDomDocument & doc, bool igno

bool QgsComposerHtml::readXML( const QDomElement& itemElem, const QDomDocument& doc, bool ignoreFrames )
{
deleteFrames();
QString urlString = itemElem.attribute( "url" );
if ( !urlString.isEmpty() )
{
@@ -25,7 +25,7 @@ class QgsComposerHtml: public QgsComposerMultiFrame
{
Q_OBJECT
public:
QgsComposerHtml( QgsComposition* c, qreal x, qreal y, qreal width, qreal height );
QgsComposerHtml( QgsComposition* c, qreal x, qreal y, qreal width, qreal height, bool createUndoCommands );
QgsComposerHtml();
~QgsComposerHtml();

@@ -24,6 +24,7 @@

#include "qgscomposition.h"
#include "qgscomposeritem.h"
#include "qgscomposerframe.h"


#include <limits>
@@ -380,9 +381,9 @@ void QgsComposerItem::mouseReleaseEvent( QGraphicsSceneMouseEvent * event )
return;
}

beginCommand( tr( "Change item position" ) );
beginItemCommand( tr( "Change item position" ) );
changeItemRectangle( mouseMoveStopPoint, mMouseMoveStartPos, this, diffX, diffY, this );
endCommand();
endItemCommand();

//reset default action
mCurrentMouseMoveAction = QgsComposerItem::MoveItem;
@@ -190,10 +190,14 @@ class CORE_EXPORT QgsComposerItem: public QObject, public QGraphicsRectItem

const QgsComposition* composition() const {return mComposition;}

virtual void beginItemCommand( const QString& text ) { beginCommand( text ); }

/**Starts new composer undo command
@param commandText command title
@param c context for mergeable commands (unknown for non-mergeable commands*/
void beginCommand( const QString& commandText, QgsComposerMergeCommand::Context c = QgsComposerMergeCommand::Unknown );

virtual void endItemCommand() { endCommand(); }
/**Finish current command and push it onto the undo stack */
void endCommand();
void cancelCommand();
@@ -17,7 +17,7 @@
#include "qgscomposerframe.h"
#include "qgsaddremovemultiframecommand.h"

QgsComposerMultiFrame::QgsComposerMultiFrame( QgsComposition* c ): mComposition( c ), mResizeMode( UseExistingFrames )
QgsComposerMultiFrame::QgsComposerMultiFrame( QgsComposition* c, bool createUndoCommands ): mComposition( c ), mResizeMode( UseExistingFrames ), mCreateUndoCommands( createUndoCommands )
{
mComposition->addMultiFrame( this );
}
@@ -158,6 +158,17 @@ void QgsComposerMultiFrame::update()
}
}

void QgsComposerMultiFrame::deleteFrames()
{
QList<QgsComposerFrame*>::iterator frameIt = mFrameItems.begin();
for ( ; frameIt != mFrameItems.end(); ++frameIt )
{
mComposition->removeComposerItem( *frameIt );
delete *frameIt;
}
mFrameItems.clear();
}

bool QgsComposerMultiFrame::_writeXML( QDomElement& elem, QDomDocument& doc, bool ignoreFrames ) const
{
elem.setAttribute( "resizeMode", mResizeMode );
@@ -39,7 +39,7 @@ class QgsComposerMultiFrame: public QObject
ExtendToNextPage //duplicates last frame to next page to fit the total size
};

QgsComposerMultiFrame( QgsComposition* c );
QgsComposerMultiFrame( QgsComposition* c, bool createUndoCommands );
virtual ~QgsComposerMultiFrame();
virtual QSizeF totalSize() const = 0;
virtual void render( QPainter* p, const QRectF& renderExtent ) = 0;
@@ -58,10 +58,18 @@ class QgsComposerMultiFrame: public QObject

QgsComposition* composition() { return mComposition; }

bool createUndoCommands() const { return mCreateUndoCommands; }
void setCreateUndoCommands( bool enabled ) { mCreateUndoCommands = enabled; }

/**Removes and deletes all frames from mComposition*/
void deleteFrames();

protected:
QgsComposition* mComposition;
QList<QgsComposerFrame*> mFrameItems;
ResizeMode mResizeMode;
/**True: creates QgsMultiFrameCommands on internal changes (e.g. changing frames )*/
bool mCreateUndoCommands;

virtual void addFrame( QgsComposerFrame* frame ) = 0;

@@ -19,7 +19,11 @@
#include "qgscomposermultiframe.h"

QgsComposerMultiFrameCommand::QgsComposerMultiFrameCommand( QgsComposerMultiFrame* multiFrame, const QString& text, QUndoCommand* parent ):
QUndoCommand( text, parent ), mMultiFrame( multiFrame ), mFirstRun( false )
QUndoCommand( text, parent ), mMultiFrame( multiFrame ), mFirstRun( true )
{
}

QgsComposerMultiFrameCommand::QgsComposerMultiFrameCommand(): QUndoCommand( "", 0 ), mMultiFrame( 0 ), mFirstRun( false )
{
}

@@ -43,18 +47,12 @@ void QgsComposerMultiFrameCommand::redo()

void QgsComposerMultiFrameCommand::savePreviousState()
{
if ( mMultiFrame )
{
saveState( mPreviousState );
}
saveState( mPreviousState );
}

void QgsComposerMultiFrameCommand::saveAfterState()
{
if ( mMultiFrame )
{
saveState( mAfterState );
}
saveState( mAfterState );
}

void QgsComposerMultiFrameCommand::saveState( QDomDocument& stateDoc )
@@ -80,7 +78,7 @@ bool QgsComposerMultiFrameCommand::checkFirstRun()
{
if ( !mFirstRun )
{
false;
return false;
}
mFirstRun = false;
return true;
@@ -27,6 +27,7 @@
#include "qgscomposerscalebar.h"
#include "qgscomposershape.h"
#include "qgscomposerattributetable.h"
#include "qgscomposermultiframecommand.h"
#include "qgslogger.h"
#include "qgspaintenginehack.h"
#include "qgspaperitem.h"
@@ -39,7 +40,7 @@

QgsComposition::QgsComposition( QgsMapRenderer* mapRenderer ):
QGraphicsScene( 0 ), mMapRenderer( mapRenderer ), mPlotStyle( QgsComposition::Preview ), mPageWidth( 297 ), mPageHeight( 210 ), mSpaceBetweenPages( 10 ), mPrintAsRaster( false ), mSelectionTolerance( 0.0 ),
mSnapToGrid( false ), mSnapGridResolution( 0.0 ), mSnapGridOffsetX( 0.0 ), mSnapGridOffsetY( 0.0 ), mActiveCommand( 0 )
mSnapToGrid( false ), mSnapGridResolution( 0.0 ), mSnapGridOffsetX( 0.0 ), mSnapGridOffsetY( 0.0 ), mActiveItemCommand( 0 ), mActiveMultiFrameCommand( 0 )
{
setBackgroundBrush( Qt::gray );
addPaperItem();
@@ -50,7 +51,7 @@ QgsComposition::QgsComposition( QgsMapRenderer* mapRenderer ):

QgsComposition::QgsComposition():
QGraphicsScene( 0 ), mMapRenderer( 0 ), mPlotStyle( QgsComposition::Preview ), mPageWidth( 297 ), mPageHeight( 210 ), mSpaceBetweenPages( 10 ), mPrintAsRaster( false ),
mSelectionTolerance( 0.0 ), mSnapToGrid( false ), mSnapGridResolution( 0.0 ), mSnapGridOffsetX( 0.0 ), mSnapGridOffsetY( 0.0 ), mActiveCommand( 0 )
mSelectionTolerance( 0.0 ), mSnapToGrid( false ), mSnapGridResolution( 0.0 ), mSnapGridOffsetX( 0.0 ), mSnapGridOffsetY( 0.0 ), mActiveItemCommand( 0 ), mActiveMultiFrameCommand( 0 )
{
loadSettings();
}
@@ -62,6 +63,8 @@ QgsComposition::~QgsComposition()
// this class is deconstructed - to avoid segfaults
// when composer items access in destructor composition that isn't valid anymore
clear();
delete mActiveItemCommand;
delete mActiveMultiFrameCommand;
}

void QgsComposition::setPaperSize( double width, double height )
@@ -459,7 +462,7 @@ void QgsComposition::addItemsFromXML( const QDomElement& elem, const QDomDocumen
for ( int i = 0; i < composerHtmlList.size(); ++i )
{
QDomElement currentHtmlElem = composerHtmlList.at( i ).toElement();
QgsComposerHtml* newHtml = new QgsComposerHtml( this, 0, 0, 0, 0 );
QgsComposerHtml* newHtml = new QgsComposerHtml( this, 0, 0, 0, 0, true );
newHtml->readXML( currentHtmlElem, doc );
this->addMultiFrame( newHtml );
}
@@ -993,45 +996,69 @@ void QgsComposition::saveSettings()

void QgsComposition::beginCommand( QgsComposerItem* item, const QString& commandText, QgsComposerMergeCommand::Context c )
{
delete mActiveCommand;
delete mActiveItemCommand;
if ( !item )
{
mActiveCommand = 0;
mActiveItemCommand = 0;
return;
}

if ( c == QgsComposerMergeCommand::Unknown )
{
mActiveCommand = new QgsComposerItemCommand( item, commandText );
mActiveItemCommand = new QgsComposerItemCommand( item, commandText );
}
else
{
mActiveCommand = new QgsComposerMergeCommand( c, item, commandText );
mActiveItemCommand = new QgsComposerMergeCommand( c, item, commandText );
}
mActiveCommand->savePreviousState();
mActiveItemCommand->savePreviousState();
}

void QgsComposition::endCommand()
{
if ( mActiveCommand )
if ( mActiveItemCommand )
{
mActiveCommand->saveAfterState();
if ( mActiveCommand->containsChange() ) //protect against empty commands
mActiveItemCommand->saveAfterState();
if ( mActiveItemCommand->containsChange() ) //protect against empty commands
{
mUndoStack.push( mActiveCommand );
mUndoStack.push( mActiveItemCommand );
}
else
{
delete mActiveCommand;
delete mActiveItemCommand;
}
mActiveCommand = 0;
mActiveItemCommand = 0;
}
}

void QgsComposition::cancelCommand()
{
delete mActiveCommand;
mActiveCommand = 0;
delete mActiveItemCommand;
mActiveItemCommand = 0;
}

void QgsComposition::beginMultiFrameCommand( QgsComposerMultiFrame* multiFrame, const QString& text )
{
delete mActiveMultiFrameCommand;
mActiveMultiFrameCommand = new QgsComposerMultiFrameCommand( multiFrame, text );
mActiveMultiFrameCommand->savePreviousState();
}

void QgsComposition::endMultiFrameCommand()
{
if ( mActiveMultiFrameCommand )
{
mActiveMultiFrameCommand->saveAfterState();
if ( mActiveMultiFrameCommand->containsChange() )
{
mUndoStack.push( mActiveMultiFrameCommand );
}
else
{
delete mActiveMultiFrameCommand;
}
mActiveMultiFrameCommand = 0;
}
}

void QgsComposition::addMultiFrame( QgsComposerMultiFrame* multiFrame )
@@ -1177,7 +1204,14 @@ void QgsComposition::removeComposerItem( QgsComposerItem* item )
else
{
emit itemRemoved( item );
pushAddRemoveCommand( item, tr( "Item deleted" ), QgsAddRemoveItemCommand::Removed );
if ( item->type() == QgsComposerItem::ComposerFrame ) //multiframe tracks item changes
{

}
else
{
pushAddRemoveCommand( item, tr( "Item deleted" ), QgsAddRemoveItemCommand::Removed );
}
}
}
}
@@ -22,8 +22,8 @@
#include <QSet>
#include <QUndoStack>

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

class QgsComposerFrame;
class QgsComposerItem;
@@ -43,6 +43,7 @@ class QgsComposerScaleBar;
class QgsComposerShape;
class QgsComposerAttributeTable;
class QgsComposerMultiFrame;
class QgsComposerMultiFrameCommand;

/** \ingroup MapComposer
* Graphics scene for map printing. The class manages the paper item which always
@@ -202,6 +203,9 @@ class CORE_EXPORT QgsComposition: public QGraphicsScene
/**Deletes current command*/
void cancelCommand();

void beginMultiFrameCommand( QgsComposerMultiFrame* multiFrame, const QString& text );
void endMultiFrameCommand();

void addMultiFrame( QgsComposerMultiFrame* multiFrame );
/**Removes multi frame (but does not delete it)*/
void removeMultiFrame( QgsComposerMultiFrame* multiFrame );
@@ -283,7 +287,8 @@ class CORE_EXPORT QgsComposition: public QGraphicsScene

QUndoStack mUndoStack;

QgsComposerItemCommand* mActiveCommand;
QgsComposerItemCommand* mActiveItemCommand;
QgsComposerMultiFrameCommand* mActiveMultiFrameCommand;

QgsComposition(); //default constructor is forbidden

0 comments on commit b40d0af

Please sign in to comment.
You can’t perform that action at this time.