From cee4d0c0b7bcd1a412b2d4713e80a3ffb676a6e9 Mon Sep 17 00:00:00 2001 From: Jarek Kobus Date: Mon, 17 Oct 2016 11:11:09 +0200 Subject: Add a resetClean() method to the undo stack With the current API it is not possible to reset the index into -1. We have setClean() method, but we are lacking setDirty(). This is needed in case when the document has changed outside of the editor and nothing has changed in the undo stack history. In this case we don't know the state of the file modified externally so we need to mark that editor's contents is different from the file contents and undoing or redoing commands can't bring the editor to the clean state. This may also be useful to call it when we created a new document and haven't saved it yet or when the document was restored from backup file. Task-number: QTCREATORBUG-17048 Change-Id: I64e2052b3559299e0b6939831557a07a59a851b6 Reviewed-by: Friedemann Kleint Reviewed-by: Lars Knoll --- src/widgets/util/qundostack.cpp | 27 +++- src/widgets/util/qundostack.h | 1 + .../widgets/util/qundostack/tst_qundostack.cpp | 144 +++++++++++++++++++++ 3 files changed, 171 insertions(+), 1 deletion(-) diff --git a/src/widgets/util/qundostack.cpp b/src/widgets/util/qundostack.cpp index 18f85ca505..59d517e77b 100644 --- a/src/widgets/util/qundostack.cpp +++ b/src/widgets/util/qundostack.cpp @@ -633,7 +633,7 @@ void QUndoStack::push(QUndoCommand *cmd) commands, it emits the signal cleanChanged(). This signal is also emitted when the stack leaves the clean state. - \sa isClean(), cleanIndex() + \sa isClean(), resetClean(), cleanIndex() */ void QUndoStack::setClean() @@ -647,6 +647,30 @@ void QUndoStack::setClean() d->setIndex(d->index, true); } +/*! + \since 5.8 + + Leaves the clean state and emits cleanChanged() if the stack was clean. + This method resets the clean index to -1. + + This is typically called in the following cases, when a document has been: + \li created basing on some template and has not been saved, + so no filename has been associated with the document yet. + \li restored from a backup file. + \li changed outside of the editor and the user did not reload it. + + \sa isClean(), setClean(), cleanIndex() +*/ + +void QUndoStack::resetClean() +{ + Q_D(QUndoStack); + const bool was_clean = isClean(); + d->clean_index = -1; + if (was_clean) + emit cleanChanged(false); +} + /*! If the stack is in the clean state, returns \c true; otherwise returns \c false. @@ -668,6 +692,7 @@ bool QUndoStack::isClean() const some commands are undone, then a new command is pushed. Since push() deletes all the undone commands before pushing the new command, the stack can't return to the clean state again. In this case, this function returns -1. + The -1 may also be returned after an explicit call to resetClean(). \sa isClean(), setClean() */ diff --git a/src/widgets/util/qundostack.h b/src/widgets/util/qundostack.h index f4db78300b..ca918b0618 100644 --- a/src/widgets/util/qundostack.h +++ b/src/widgets/util/qundostack.h @@ -128,6 +128,7 @@ public: public Q_SLOTS: void setClean(); + void resetClean(); void setIndex(int idx); void undo(); void redo(); diff --git a/tests/auto/widgets/util/qundostack/tst_qundostack.cpp b/tests/auto/widgets/util/qundostack/tst_qundostack.cpp index 8573cea35f..a3e7219892 100644 --- a/tests/auto/widgets/util/qundostack/tst_qundostack.cpp +++ b/tests/auto/widgets/util/qundostack/tst_qundostack.cpp @@ -1200,6 +1200,150 @@ void tst_QUndoStack::setClean() true, // undoChanged true); // redoChanged QCOMPARE(stack.cleanIndex(), -1); + + stack.setClean(); + QCOMPARE(str, QString()); + checkState(redoTextChangedSpy, + canRedoChangedSpy, + undoTextChangedSpy, + redoAction, + undoAction, + canUndoChangedSpy, + cleanChangedSpy, + indexChangedSpy, + stack, + true, // clean + 1, // count + 0, // index + false, // canUndo + "", // undoText + true, // canRedo + "insert", // redoText + true, // cleanChanged + false, // indexChanged + false, // undoChanged + false); // redoChanged + QCOMPARE(stack.cleanIndex(), 0); + + stack.resetClean(); + QCOMPARE(str, QString()); + checkState(redoTextChangedSpy, + canRedoChangedSpy, + undoTextChangedSpy, + redoAction, + undoAction, + canUndoChangedSpy, + cleanChangedSpy, + indexChangedSpy, + stack, + false, // clean + 1, // count + 0, // index + false, // canUndo + "", // undoText + true, // canRedo + "insert", // redoText + true, // cleanChanged + false, // indexChanged + false, // undoChanged + false); // redoChanged + QCOMPARE(stack.cleanIndex(), -1); + + stack.redo(); + QCOMPARE(str, QString("foo")); + checkState(redoTextChangedSpy, + canRedoChangedSpy, + undoTextChangedSpy, + redoAction, + undoAction, + canUndoChangedSpy, + cleanChangedSpy, + indexChangedSpy, + stack, + false, // clean + 1, // count + 1, // index + true, // canUndo + "insert", // undoText + false, // canRedo + "", // redoText + false, // cleanChanged + true, // indexChanged + true, // undoChanged + true); // redoChanged + QCOMPARE(stack.cleanIndex(), -1); + + stack.setClean(); + QCOMPARE(str, QString("foo")); + checkState(redoTextChangedSpy, + canRedoChangedSpy, + undoTextChangedSpy, + redoAction, + undoAction, + canUndoChangedSpy, + cleanChangedSpy, + indexChangedSpy, + stack, + true, // clean + 1, // count + 1, // index + true, // canUndo + "insert", // undoText + false, // canRedo + "", // redoText + true, // cleanChanged + false, // indexChanged + false, // undoChanged + false); // redoChanged + QCOMPARE(stack.cleanIndex(), 1); + + stack.undo(); + QCOMPARE(str, QString()); + checkState(redoTextChangedSpy, + canRedoChangedSpy, + undoTextChangedSpy, + redoAction, + undoAction, + canUndoChangedSpy, + cleanChangedSpy, + indexChangedSpy, + stack, + false, // clean + 1, // count + 0, // index + false, // canUndo + "", // undoText + true, // canRedo + "insert", // redoText + true, // cleanChanged + true, // indexChanged + true, // undoChanged + true); // redoChanged + QCOMPARE(stack.cleanIndex(), 1); + + stack.resetClean(); + QCOMPARE(str, QString()); + checkState(redoTextChangedSpy, + canRedoChangedSpy, + undoTextChangedSpy, + redoAction, + undoAction, + canUndoChangedSpy, + cleanChangedSpy, + indexChangedSpy, + stack, + false, // clean + 1, // count + 0, // index + false, // canUndo + "", // undoText + true, // canRedo + "insert", // redoText + false, // cleanChanged + false, // indexChanged + false, // undoChanged + false); // redoChanged + QCOMPARE(stack.cleanIndex(), -1); } void tst_QUndoStack::clear() -- cgit v1.2.3