Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[composer] Support merged undo/redo commands for multiframe items
  • Loading branch information
nyalldawson committed Jul 29, 2014
1 parent bdd6783 commit 3f0d094
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 7 deletions.
6 changes: 3 additions & 3 deletions src/app/composer/qgscomposerhtmlwidget.cpp
Expand Up @@ -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 );
Expand All @@ -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 );
Expand All @@ -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 );
Expand Down
24 changes: 24 additions & 0 deletions src/core/composer/qgscomposermultiframecommand.cpp
Expand Up @@ -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;
}
32 changes: 31 additions & 1 deletion src/core/composer/qgscomposermultiframecommand.h
Expand Up @@ -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;
Expand All @@ -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
18 changes: 16 additions & 2 deletions src/core/composer/qgscomposition.cpp
Expand Up @@ -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();
}

Expand Down
3 changes: 2 additions & 1 deletion src/core/composer/qgscomposition.h
Expand Up @@ -30,6 +30,7 @@

#include "qgsaddremoveitemcommand.h"
#include "qgscomposeritemcommand.h"
#include "qgscomposermultiframecommand.h"
#include "qgsatlascomposition.h"
#include "qgspaperitem.h"
#include "qgscomposerobject.h"
Expand Down Expand Up @@ -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*/
Expand Down

0 comments on commit 3f0d094

Please sign in to comment.