Skip to content

Commit 3f0d094

Browse files
committed
[composer] Support merged undo/redo commands for multiframe items
1 parent bdd6783 commit 3f0d094

File tree

5 files changed

+76
-7
lines changed

5 files changed

+76
-7
lines changed

src/app/composer/qgscomposerhtmlwidget.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ void QgsComposerHtmlWidget::on_mMaxDistanceSpinBox_valueChanged( double val )
200200
if ( composition )
201201
{
202202
blockSignals( true );
203-
composition->beginMultiFrameCommand( mHtml, tr( "Page break distance changed" ) );
203+
composition->beginMultiFrameCommand( mHtml, tr( "Page break distance changed" ), QgsComposerMultiFrameMergeCommand::HtmlBreakDistance );
204204
mHtml->setMaxBreakDistance( val );
205205
composition->endMultiFrameCommand();
206206
blockSignals( false );
@@ -218,7 +218,7 @@ void QgsComposerHtmlWidget::htmlEditorChanged()
218218
if ( composition )
219219
{
220220
blockSignals( true );
221-
composition->beginMultiFrameCommand( mHtml, tr( "HTML changed" ) );
221+
composition->beginMultiFrameCommand( mHtml, tr( "HTML changed" ), QgsComposerMultiFrameMergeCommand::HtmlSource );
222222
mHtml->setHtml( mHtmlEditor->text() );
223223
composition->endMultiFrameCommand();
224224
blockSignals( false );
@@ -237,7 +237,7 @@ void QgsComposerHtmlWidget::stylesheetEditorChanged()
237237
if ( composition )
238238
{
239239
blockSignals( true );
240-
composition->beginMultiFrameCommand( mHtml, tr( "User stylesheet changed" ) );
240+
composition->beginMultiFrameCommand( mHtml, tr( "User stylesheet changed" ), QgsComposerMultiFrameMergeCommand::HtmlStylesheet );
241241
mHtml->setUserStylesheet( mStylesheetEditor->text() );
242242
composition->endMultiFrameCommand();
243243
blockSignals( false );

src/core/composer/qgscomposermultiframecommand.cpp

+24
Original file line numberDiff line numberDiff line change
@@ -90,3 +90,27 @@ bool QgsComposerMultiFrameCommand::containsChange() const
9090
{
9191
return !( mPreviousState.isNull() || mAfterState.isNull() || mPreviousState.toString() == mAfterState.toString() );
9292
}
93+
94+
95+
QgsComposerMultiFrameMergeCommand::QgsComposerMultiFrameMergeCommand( QgsComposerMultiFrameMergeCommand::Context c, QgsComposerMultiFrame *multiFrame, const QString &text )
96+
: QgsComposerMultiFrameCommand( multiFrame, text )
97+
, mContext( c )
98+
{
99+
100+
}
101+
102+
QgsComposerMultiFrameMergeCommand::~QgsComposerMultiFrameMergeCommand()
103+
{
104+
105+
}
106+
107+
bool QgsComposerMultiFrameMergeCommand::mergeWith( const QUndoCommand *command )
108+
{
109+
const QgsComposerMultiFrameCommand* c = dynamic_cast<const QgsComposerMultiFrameCommand*>( command );
110+
if ( !c || mMultiFrame != c->multiFrame() )
111+
{
112+
return false;
113+
}
114+
mAfterState = c->afterState();
115+
return true;
116+
}

src/core/composer/qgscomposermultiframecommand.h

+31-1
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,15 @@ class CORE_EXPORT QgsComposerMultiFrameCommand: public QUndoCommand
3535
void savePreviousState();
3636
void saveAfterState();
3737

38+
QDomDocument previousState() const { return mPreviousState.cloneNode().toDocument(); }
39+
QDomDocument afterState() const { return mAfterState.cloneNode().toDocument(); }
40+
3841
/**Returns true if previous state and after state are valid and different*/
3942
bool containsChange() const;
4043

41-
private:
44+
const QgsComposerMultiFrame* multiFrame() const { return mMultiFrame; }
45+
46+
protected:
4247
QgsComposerMultiFrame* mMultiFrame;
4348

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

60+
/**A composer command that merges together with other commands having the same context (=id)
61+
* for multi frame items. Keeps the oldest previous state and uses the newest after state.
62+
* The purpose is to avoid too many micro changes in the history*/
63+
class CORE_EXPORT QgsComposerMultiFrameMergeCommand: public QgsComposerMultiFrameCommand
64+
{
65+
public:
66+
enum Context
67+
{
68+
Unknown = 0,
69+
//composer html
70+
HtmlSource,
71+
HtmlStylesheet,
72+
HtmlBreakDistance
73+
};
74+
75+
QgsComposerMultiFrameMergeCommand( Context c, QgsComposerMultiFrame* multiFrame, const QString& text );
76+
~QgsComposerMultiFrameMergeCommand();
77+
78+
bool mergeWith( const QUndoCommand * command );
79+
int id() const { return ( int )mContext; }
80+
81+
private:
82+
Context mContext;
83+
};
84+
5585
#endif // QGSCOMPOSERMULTIFRAMECOMMAND_H

src/core/composer/qgscomposition.cpp

+16-2
Original file line numberDiff line numberDiff line change
@@ -2135,10 +2135,24 @@ void QgsComposition::cancelCommand()
21352135
mActiveItemCommand = 0;
21362136
}
21372137

2138-
void QgsComposition::beginMultiFrameCommand( QgsComposerMultiFrame* multiFrame, const QString& text )
2138+
void QgsComposition::beginMultiFrameCommand( QgsComposerMultiFrame* multiFrame, const QString& text, const QgsComposerMultiFrameMergeCommand::Context c )
21392139
{
21402140
delete mActiveMultiFrameCommand;
2141-
mActiveMultiFrameCommand = new QgsComposerMultiFrameCommand( multiFrame, text );
2141+
2142+
if ( !multiFrame )
2143+
{
2144+
mActiveMultiFrameCommand = 0;
2145+
return;
2146+
}
2147+
2148+
if ( c == QgsComposerMultiFrameMergeCommand::Unknown )
2149+
{
2150+
mActiveMultiFrameCommand = new QgsComposerMultiFrameCommand( multiFrame, text );
2151+
}
2152+
else
2153+
{
2154+
mActiveMultiFrameCommand = new QgsComposerMultiFrameMergeCommand( c, multiFrame, text );
2155+
}
21422156
mActiveMultiFrameCommand->savePreviousState();
21432157
}
21442158

src/core/composer/qgscomposition.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030

3131
#include "qgsaddremoveitemcommand.h"
3232
#include "qgscomposeritemcommand.h"
33+
#include "qgscomposermultiframecommand.h"
3334
#include "qgsatlascomposition.h"
3435
#include "qgspaperitem.h"
3536
#include "qgscomposerobject.h"
@@ -430,7 +431,7 @@ class CORE_EXPORT QgsComposition : public QGraphicsScene
430431
/**Deletes current command*/
431432
void cancelCommand();
432433

433-
void beginMultiFrameCommand( QgsComposerMultiFrame* multiFrame, const QString& text );
434+
void beginMultiFrameCommand( QgsComposerMultiFrame* multiFrame, const QString& text, const QgsComposerMultiFrameMergeCommand::Context c = QgsComposerMultiFrameMergeCommand::Unknown );
434435
void endMultiFrameCommand();
435436

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

0 commit comments

Comments
 (0)