summaryrefslogtreecommitdiffstats
path: root/src/gui/doc/snippets/code/src_gui_util_qundostack.cpp
blob: fe3ced1d0258798053f2128cddefee918d6b7405 (plain)
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
// Copyright (C) 2016 The Qt Company Ltd.// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause//! [0]class AppendText :public QUndoCommand {public:AppendText(QString *doc,const QString &text):m_document(doc),m_text(text) {setText("append text"); }voidundo() override { m_document->chop(m_text.length()); }voidredo() override { m_document->append(m_text); }private: QString *m_document; QString m_text;};//! [0]//! [1] MyCommand *command1 =newMyCommand(); stack->push(command1); MyCommand *command2 =newMyCommand(); stack->push(command2); stack->undo(); MyCommand *command3 =newMyCommand(); stack->push(command3);// command2 gets deleted//! [1]//! [2] QUndoCommand *insertRed =newQUndoCommand();// an empty command insertRed->setText("insert red text");newInsertText(document, idx, text, insertRed);// becomes child of insertRednewSetColor(document, idx, text.length(),Qt::red, insertRed); stack.push(insertRed);//! [2]//! [3]boolAppendText::mergeWith(const QUndoCommand *other){if(other->id() !=id())// make sure other is also an AppendText commandreturn false; m_text +=static_cast<const AppendText*>(other)->m_text;return true;}//! [3]//! [4] stack.beginMacro("insert red text"); stack.push(newInsertText(document, idx, text)); stack.push(newSetColor(document, idx, text.length(),Qt::red)); stack.endMacro();// indexChanged() is emitted//! [4]//! [5] QUndoCommand *insertRed =newQUndoCommand();// an empty command insertRed->setText("insert red text");newInsertText(document, idx, text, insertRed);// becomes child of insertRednewSetColor(document, idx, text.length(),Qt::red, insertRed); stack.push(insertRed);//! [5]
close