Skip to content
Permalink
Browse files

Add command class for multiframe changes

  • Loading branch information
mhugent committed Aug 3, 2012
1 parent 3ebef1b commit d703289b839e19b048770ad259eab6d1c7f032e9
@@ -1,6 +1,7 @@
#include "qgscomposerhtmlwidget.h"
#include "qgscomposerframe.h"
#include "qgscomposeritemwidget.h"
#include "qgscomposermultiframecommand.h"
#include "qgscomposerhtml.h"
#include <QFileDialog>

@@ -47,8 +48,19 @@ void QgsComposerHtmlWidget::on_mUrlLineEdit_editingFinished()
{
if ( mHtml )
{
QgsComposerMultiFrameCommand* c = new QgsComposerMultiFrameCommand( mHtml, tr( "Change html url" ) );
c->savePreviousState();
mHtml->setUrl( QUrl( mUrlLineEdit->text() ) );
mHtml->update();
c->saveAfterState();
if ( c->containsChange() )
{
mHtml->composition()->undoStack()->push( c );
mHtml->update();
}
else
{
delete c;
}
}
}

@@ -113,6 +113,7 @@ SET(QGIS_CORE_SRCS

composer/qgsaddremoveitemcommand.cpp
composer/qgsaddremovemultiframecommand.cpp
composer/qgscomposermultiframecommand.cpp
composer/qgscomposerarrow.cpp
composer/qgscomposerframe.cpp
composer/qgscomposeritem.cpp
@@ -59,6 +59,7 @@ void QgsComposerHtml::setUrl( const QUrl& url )
{
return;
}
mLoaded = false;

mUrl = url;
mWebPage->mainFrame()->load( mUrl );
@@ -126,21 +127,21 @@ void QgsComposerHtml::addFrame( QgsComposerFrame* frame, bool addCommand )
}
}

bool QgsComposerHtml::writeXML( QDomElement& elem, QDomDocument & doc ) const
bool QgsComposerHtml::writeXML( QDomElement& elem, QDomDocument & doc, bool ignoreFrames ) const
{
QDomElement htmlElem = doc.createElement( "ComposerHtml" );
htmlElem.setAttribute( "url", mUrl.toString() );
bool state = _writeXML( htmlElem, doc );
bool state = _writeXML( htmlElem, doc, ignoreFrames );
elem.appendChild( htmlElem );
return state;
}

bool QgsComposerHtml::readXML( const QDomElement& itemElem, const QDomDocument& doc )
bool QgsComposerHtml::readXML( const QDomElement& itemElem, const QDomDocument& doc, bool ignoreFrames )
{
QString urlString = itemElem.attribute( "url" );
if ( !urlString.isEmpty() )
{
setUrl( QUrl( urlString ) );
}
return _readXML( itemElem, doc );
return _readXML( itemElem, doc, ignoreFrames );
}
@@ -35,8 +35,8 @@ class QgsComposerHtml: public QgsComposerMultiFrame
QSizeF totalSize() const;
void render( QPainter* p, const QRectF& renderExtent );

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

protected:
void addFrame( QgsComposerFrame* frame, bool addCommand = false );
@@ -28,15 +28,20 @@ QgsComposerMultiFrame::QgsComposerMultiFrame(): mComposition( 0 ), mResizeMode(

QgsComposerMultiFrame::~QgsComposerMultiFrame()
{
//todo: remove all frames from composition and delete them
QList<QgsComposerFrame*>::iterator frameIt = mFrameItems.begin();
for ( ; frameIt != mFrameItems.begin(); ++frameIt )
{
mComposition->removeComposerItem( *frameIt );
delete( *frameIt );
}
}

void QgsComposerMultiFrame::setResizeMode( ResizeMode mode )
{
if ( mode != mResizeMode )
{
mResizeMode = mode;
recalculateFrameSizes();
recalculateFrameSizes( true );
emit changed();
}
}
@@ -126,8 +131,6 @@ 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
@@ -158,27 +161,33 @@ void QgsComposerMultiFrame::update()
}
}

bool QgsComposerMultiFrame::_writeXML( QDomElement& elem, QDomDocument& doc ) const
bool QgsComposerMultiFrame::_writeXML( QDomElement& elem, QDomDocument& doc, bool ignoreFrames ) const
{
elem.setAttribute( "resizeMode", mResizeMode );
QList<QgsComposerFrame*>::const_iterator frameIt = mFrameItems.constBegin();
for ( ; frameIt != mFrameItems.constEnd(); ++frameIt )
if ( !ignoreFrames )
{
( *frameIt )->writeXML( elem, doc );
QList<QgsComposerFrame*>::const_iterator frameIt = mFrameItems.constBegin();
for ( ; frameIt != mFrameItems.constEnd(); ++frameIt )
{
( *frameIt )->writeXML( elem, doc );
}
}
return true;
}

bool QgsComposerMultiFrame::_readXML( const QDomElement& itemElem, const QDomDocument& doc )
bool QgsComposerMultiFrame::_readXML( const QDomElement& itemElem, const QDomDocument& doc, bool ignoreFrames )
{
mResizeMode = ( ResizeMode )itemElem.attribute( "resizeMode", "0" ).toInt();
QDomNodeList frameList = itemElem.elementsByTagName( "ComposerFrame" );
for ( int i = 0; i < frameList.size(); ++i )
if ( !ignoreFrames )
{
QDomElement frameElem = frameList.at( i ).toElement();
QgsComposerFrame* newFrame = new QgsComposerFrame( mComposition, this, 0, 0, 0, 0 );
newFrame->readXML( frameElem, doc );
addFrame( newFrame );
QDomNodeList frameList = itemElem.elementsByTagName( "ComposerFrame" );
for ( int i = 0; i < frameList.size(); ++i )
{
QDomElement frameElem = frameList.at( i ).toElement();
QgsComposerFrame* newFrame = new QgsComposerFrame( mComposition, this, 0, 0, 0, 0 );
newFrame->readXML( frameElem, doc );
addFrame( newFrame );
}
}
return true;
}
@@ -50,11 +50,13 @@ class QgsComposerMultiFrame: public QObject
void setResizeMode( ResizeMode mode );
ResizeMode resizeMode() const { return mResizeMode; }

virtual bool writeXML( QDomElement& elem, QDomDocument & doc ) const = 0;
bool _writeXML( QDomElement& elem, QDomDocument& doc ) const;
virtual bool writeXML( QDomElement& elem, QDomDocument & doc, bool ignoreFrames = false ) const = 0;
bool _writeXML( QDomElement& elem, QDomDocument& doc, bool ignoreFrames = false ) const;

virtual bool readXML( const QDomElement& itemElem, const QDomDocument& doc ) = 0;
bool _readXML( const QDomElement& itemElem, const QDomDocument& doc );
virtual bool readXML( const QDomElement& itemElem, const QDomDocument& doc, bool ignoreFrames = false ) = 0;
bool _readXML( const QDomElement& itemElem, const QDomDocument& doc, bool ignoreFrames = false );

QgsComposition* composition() { return mComposition; }

protected:
QgsComposition* mComposition;
@@ -0,0 +1,92 @@
/***************************************************************************
qgscomposermultiframecommand.cpp
--------------------------------
begin : 2012-08-02
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 "qgscomposermultiframecommand.h"
#include "qgscomposermultiframe.h"

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

QgsComposerMultiFrameCommand::~QgsComposerMultiFrameCommand()
{
}

void QgsComposerMultiFrameCommand::undo()
{
restoreState( mPreviousState );
}

void QgsComposerMultiFrameCommand::redo()
{
if ( !checkFirstRun() )
{
return;
}
restoreState( mAfterState );
}

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

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

void QgsComposerMultiFrameCommand::saveState( QDomDocument& stateDoc )
{
if ( mMultiFrame )
{
stateDoc.clear();
QDomElement documentElement = stateDoc.createElement( "ComposerMultiFrameState" );
mMultiFrame->writeXML( documentElement, stateDoc, true );
stateDoc.appendChild( documentElement );
}
}

void QgsComposerMultiFrameCommand::restoreState( QDomDocument& stateDoc )
{
if ( mMultiFrame )
{
mMultiFrame->readXML( stateDoc.documentElement().firstChild().toElement(), stateDoc, true );
}
}

bool QgsComposerMultiFrameCommand::checkFirstRun()
{
if ( !mFirstRun )
{
false;
}
mFirstRun = false;
return true;
}

bool QgsComposerMultiFrameCommand::containsChange() const
{
return !( mPreviousState.isNull() || mAfterState.isNull() || mPreviousState.toString() == mAfterState.toString() );
}
@@ -0,0 +1,55 @@
/***************************************************************************
qgscomposermultiframecommand.h
------------------------------
begin : 2012-08-02
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 QGSCOMPOSERMULTIFRAMECOMMAND_H
#define QGSCOMPOSERMULTIFRAMECOMMAND_H

#include <QUndoCommand>
#include <QDomDocument>

class QgsComposerMultiFrame;

class QgsComposerMultiFrameCommand: public QUndoCommand
{
public:
QgsComposerMultiFrameCommand( QgsComposerMultiFrame* multiFrame, const QString& text, QUndoCommand* parent = 0 );
~QgsComposerMultiFrameCommand();

void undo();
void redo();

void savePreviousState();
void saveAfterState();

/**Returns true if previous state and after state are valid and different*/
bool containsChange() const;

private:
QgsComposerMultiFrame* mMultiFrame;

QDomDocument mPreviousState;
QDomDocument mAfterState;

bool mFirstRun;

QgsComposerMultiFrameCommand(); //forbidden
void saveState( QDomDocument& stateDoc );
void restoreState( QDomDocument& stateDoc );
bool checkFirstRun();
};

#endif // QGSCOMPOSERMULTIFRAMECOMMAND_H

0 comments on commit d703289

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