1 // Aseprite
2 // Copyright (C) 2001-2018  David Capello
3 //
4 // This program is distributed under the terms of
5 // the End-User License Agreement for Aseprite.
6 
7 #ifndef APP_TRANSACTION_H_INCLUDED
8 #define APP_TRANSACTION_H_INCLUDED
9 #pragma once
10 
11 #include <string>
12 
13 namespace app {
14 
15   class Cmd;
16   class CmdTransaction;
17   class Context;
18   class DocRange;
19   class DocUndo;
20 
21   enum Modification {
22     ModifyDocument,      // This item changes the "saved status" of the document.
23     DoesntModifyDocument // This item doesn't modify the document.
24   };
25 
26   // High-level class to group a set of commands to modify the
27   // document atomically, with enough information to rollback the
28   // whole operation if something fails (e.g. an exceptions is thrown)
29   // in the middle of the procedure.
30   //
31   // You have to wrap every call to an transaction with a
32   // ContextWriter. The preferred usage is as follows:
33   //
34   // {
35   //   ContextWriter writer(context);
36   //   Transaction transaction(context, "My big operation");
37   //   ...
38   //   transaction.commit();
39   // }
40   //
41   class Transaction {
42   public:
43     // Starts a undoable sequence of operations in a transaction that
44     // can be committed or rollbacked.  All the operations will be
45     // grouped in the sprite's undo as an atomic operation.
46     Transaction(Context* ctx, const std::string& label, Modification mod = ModifyDocument);
47     virtual ~Transaction();
48 
49     // Can be used to change the new document range resulting from
50     // executing this transaction. This range can be used then in
51     // undo/redo operations to restore the Timeline selection/range.
52     void setNewDocRange(const DocRange& range);
53 
54     // This must be called to commit all the changes, so the undo will
55     // be finally added in the sprite.
56     //
57     // If you don't use this routine, all the changes will be discarded
58     // (if the sprite's undo was enabled when the Transaction was
59     // created).
60     //
61     // WARNING: This must be called from the main UI thread, because
62     // it will generate a DocUndo::add() which triggers a
63     // DocUndoObserver::onAddUndoState() notification, which
64     // updates the Undo History window UI.
65     void commit();
66 
67     void execute(Cmd* cmd);
68 
69   private:
70     void rollback();
71 
72     Context* m_ctx;
73     DocUndo* m_undo;
74     CmdTransaction* m_cmds;
75   };
76 
77 } // namespace app
78 
79 #endif
80