Skip to content
Permalink
Browse files

[composer] Support merged undo/redo commands for multiframe items

  • Loading branch information
nyalldawson committed Jul 29, 2014
1 parent bdd6783 commit 3f0d0944e26ffc4284d1eabf32146c55a0036655
@@ -200,7 +200,7 @@ void QgsComposerHtmlWidget::on_mMaxDistanceSpinBox_valueChanged( double val )
if ( composition )
{
blockSignals( true );
composition->beginMultiFrameCommand( mHtml, tr( "Page break distance changed" ) );
composition->beginMultiFrameCommand( mHtml, tr( "Page break distance changed" ), QgsComposerMultiFrameMergeCommand::HtmlBreakDistance );
mHtml->setMaxBreakDistance( val );
composition->endMultiFrameCommand();
blockSignals( false );
@@ -218,7 +218,7 @@ void QgsComposerHtmlWidget::htmlEditorChanged()
if ( composition )
{
blockSignals( true );
composition->beginMultiFrameCommand( mHtml, tr( "HTML changed" ) );
composition->beginMultiFrameCommand( mHtml, tr( "HTML changed" ), QgsComposerMultiFrameMergeCommand::HtmlSource );
mHtml->setHtml( mHtmlEditor->text() );
composition->endMultiFrameCommand();
blockSignals( false );
@@ -237,7 +237,7 @@ void QgsComposerHtmlWidget::stylesheetEditorChanged()
if ( composition )
{
blockSignals( true );
composition->beginMultiFrameCommand( mHtml, tr( "User stylesheet changed" ) );
composition->beginMultiFrameCommand( mHtml, tr( "User stylesheet changed" ), QgsComposerMultiFrameMergeCommand::HtmlStylesheet );
mHtml->setUserStylesheet( mStylesheetEditor->text() );
composition->endMultiFrameCommand();
blockSignals( false );
@@ -90,3 +90,27 @@ bool QgsComposerMultiFrameCommand::containsChange() const
{
return !( mPreviousState.isNull() || mAfterState.isNull() || mPreviousState.toString() == mAfterState.toString() );
}


QgsComposerMultiFrameMergeCommand::QgsComposerMultiFrameMergeCommand( QgsComposerMultiFrameMergeCommand::Context c, QgsComposerMultiFrame *multiFrame, const QString &text )
: QgsComposerMultiFrameCommand( multiFrame, text )
, mContext( c )
{

}

QgsComposerMultiFrameMergeCommand::~QgsComposerMultiFrameMergeCommand()
{

}

bool QgsComposerMultiFrameMergeCommand::mergeWith( const QUndoCommand *command )
{
const QgsComposerMultiFrameCommand* c = dynamic_cast<const QgsComposerMultiFrameCommand*>( command );
if ( !c || mMultiFrame != c->multiFrame() )
{
return false;
}
mAfterState = c->afterState();
return true;
}
@@ -35,10 +35,15 @@ class CORE_EXPORT QgsComposerMultiFrameCommand: public QUndoCommand
void savePreviousState();
void saveAfterState();

QDomDocument previousState() const { return mPreviousState.cloneNode().toDocument(); }
QDomDocument afterState() const { return mAfterState.cloneNode().toDocument(); }

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

private:
const QgsComposerMultiFrame* multiFrame() const { return mMultiFrame; }

protected:
QgsComposerMultiFrame* mMultiFrame;

QDomDocument mPreviousState;
@@ -52,4 +57,29 @@ class CORE_EXPORT QgsComposerMultiFrameCommand: public QUndoCommand
bool checkFirstRun();
};

/**A composer command that merges together with other commands having the same context (=id)
* for multi frame items. Keeps the oldest previous state and uses the newest after state.
* The purpose is to avoid too many micro changes in the history*/
class CORE_EXPORT QgsComposerMultiFrameMergeCommand: public QgsComposerMultiFrameCommand
{
public:
enum Context
{
Unknown = 0,
//composer html
HtmlSource,
HtmlStylesheet,
HtmlBreakDistance
};

QgsComposerMultiFrameMergeCommand( Context c, QgsComposerMultiFrame* multiFrame, const QString& text );
~QgsComposerMultiFrameMergeCommand();

bool mergeWith( const QUndoCommand * command );
int id() const { return ( int )mContext; }

private:
Context mContext;
};

#endif // QGSCOMPOSERMULTIFRAMECOMMAND_H
@@ -2135,10 +2135,24 @@ void QgsComposition::cancelCommand()
mActiveItemCommand = 0;
}

void QgsComposition::beginMultiFrameCommand( QgsComposerMultiFrame* multiFrame, const QString& text )
void QgsComposition::beginMultiFrameCommand( QgsComposerMultiFrame* multiFrame, const QString& text, const QgsComposerMultiFrameMergeCommand::Context c )
{
delete mActiveMultiFrameCommand;
mActiveMultiFrameCommand = new QgsComposerMultiFrameCommand( multiFrame, text );

if ( !multiFrame )
{
mActiveMultiFrameCommand = 0;
return;
}

if ( c == QgsComposerMultiFrameMergeCommand::Unknown )
{
mActiveMultiFrameCommand = new QgsComposerMultiFrameCommand( multiFrame, text );
}
else
{
mActiveMultiFrameCommand = new QgsComposerMultiFrameMergeCommand( c, multiFrame, text );
}
mActiveMultiFrameCommand->savePreviousState();
}

@@ -30,6 +30,7 @@

#include "qgsaddremoveitemcommand.h"
#include "qgscomposeritemcommand.h"
#include "qgscomposermultiframecommand.h"
#include "qgsatlascomposition.h"
#include "qgspaperitem.h"
#include "qgscomposerobject.h"
@@ -430,7 +431,7 @@ class CORE_EXPORT QgsComposition : public QGraphicsScene
/**Deletes current command*/
void cancelCommand();

void beginMultiFrameCommand( QgsComposerMultiFrame* multiFrame, const QString& text );
void beginMultiFrameCommand( QgsComposerMultiFrame* multiFrame, const QString& text, const QgsComposerMultiFrameMergeCommand::Context c = QgsComposerMultiFrameMergeCommand::Unknown );
void endMultiFrameCommand();

/**Adds multiframe. The object is owned by QgsComposition until removeMultiFrame is called*/

0 comments on commit 3f0d094

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