summaryrefslogtreecommitdiffstats
path: root/src/dm/systems/Qt3DSDMTransactions.h
blob: f6ed3da06e3659676111696445e9d39732a9be9d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
/****************************************************************************
**
** Copyright (C) 1993-2009 NVIDIA Corporation.
** Copyright (C) 2017 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of Qt 3D Studio.
**
** $QT_BEGIN_LICENSE:GPL-EXCEPT$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 as published by the Free Software
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
**
** $QT_END_LICENSE$
**
****************************************************************************/
#pragma once
#ifndef QT3DSDM_TRANSACTIONS_H
#define QT3DSDM_TRANSACTIONS_H
#include "Qt3DSDMDataTypes.h"
#include "StandardExtensions.h"
#include <functional>

namespace qt3dsdm {

/**
 *	Transaction is some small entity that changes data.  A transaction consumer is expected to
 *execute
 *	a list of these either forward on a "redo" command or backwards on an "undo" command.
 *
 *	Transactions merely change data.  There are two other lists on consumers for "do"
 *notifications
 *	and "undo" notifications.  These lists control the events sent out to the UI about what
 *actually
 *	changed in the model.  Note that undo/do notifications may be more clever than simple
 *send-signal
 *	commands in that they may check if an object is alive or not before sending a notification
 *and may
 *	decline to send a notification if the target object is not currently alive.
 *
 *	Currently the undo/redo system first executes all of the transactions and then sends the
 *appropriate
 *	notifications.  This means that when the UI receives any notification, the model is in its
 *most-updated
 *	state.
 */
class ITransaction
{
public:
    const char *m_File;
    int m_Line;

    ITransaction(const char *inFile, int inLine)
        : m_File(inFile)
        , m_Line(inLine)
    {
    }
    virtual ~ITransaction() {}
    virtual void Do() = 0;
    virtual void Undo() = 0;
};

/**
 *	Merging allows us to efficient handle situations where the users are continuously
 *	modifying an object (or set of objects).  These are referred to as live-update
 *	scenarios.  Dragging an object in the 3d view or dragging keyframes in the timeline
 *	would be an example of live-update scenarios.
 */
template <typename TValueType>
class IMergeableTransaction
{
public:
    virtual ~IMergeableTransaction() {}
    // Called when a new value has arrived and we would rather update
    // an existing representation rather than create a new one.
    virtual void Update(const TValueType &inValue) = 0;
};

/**
 *	A consumer is an object that records the transaction.  This interface keeps the
 *	base producer objects from binding to how the transaction or the notifications
 *	are stored.
 */
class ITransactionConsumer
{
public:
    virtual ~ITransactionConsumer() {}
    virtual void OnTransaction(std::shared_ptr<ITransaction> inTransaction) = 0;
    // Notifications to be sent for undo/redo  These are used to
    // notify clients that something is different.
    virtual void OnDoNotification(std::function<void()> inNotification) = 0;
    virtual void OnUndoNotification(std::function<void()> inNotification) = 0;
};

///////////////////////////////////////////////////////////////////////////////
// Implementation of helper objects and functions.
///////////////////////////////////////////////////////////////////////////////

typedef std::vector<std::function<void()>> TVoidFunctionList;

typedef std::shared_ptr<ITransactionConsumer> TTransactionConsumerPtr;

class ITransactionProducer
{
public:
    virtual ~ITransactionProducer() {}
    virtual void SetConsumer(TTransactionConsumerPtr inConsumer) = 0;
};

template <typename TDoTransaction, typename TUndoTransaction>
class CGenericTransaction : public ITransaction
{
    Q_DISABLE_COPY(CGenericTransaction)

    TUndoTransaction m_UndoTransaction;
    TDoTransaction m_DoTransaction;

public:
    CGenericTransaction(const char *inFile, int inLine, TDoTransaction inDo,
                        TUndoTransaction inUndo)
        : ITransaction(inFile, inLine)
        , m_UndoTransaction(inUndo)
        , m_DoTransaction(inDo)
    {
    }
    void Do() override { m_DoTransaction(); }
    void Undo() override { m_UndoTransaction(); }
};

template <typename TDoTransaction, typename TUndoTransaction>
ITransaction *DoCreateGenericTransaction(const char *inFile, int inLine, TDoTransaction inDo,
                                         TUndoTransaction inUndo)
{
    return static_cast<ITransaction *>(
        new CGenericTransaction<TDoTransaction, TUndoTransaction>(inFile, inLine, inDo, inUndo));
}

#define CREATE_GENERIC_TRANSACTION(inDo, inUndo)                                                   \
    DoCreateGenericTransaction(__FILE__, __LINE__, inDo, inUndo)

typedef std::shared_ptr<ITransaction> TTransactionPtr;
typedef std::vector<TTransactionPtr> TTransactionPtrList;

struct CTransactionConsumer : public ITransactionConsumer
{
    TTransactionPtrList m_TransactionList;
    TVoidFunctionList m_DoNotifications;
    TVoidFunctionList m_UndoNotifications;
    void OnTransaction(TTransactionPtr inTransaction) override
    {
        m_TransactionList.push_back(inTransaction);
    }
    void OnDoNotification(std::function<void()> inNotification) override
    {
        m_DoNotifications.push_back(inNotification);
    }
    void OnUndoNotification(std::function<void()> inNotification) override
    {
        m_UndoNotifications.push_back(inNotification);
    }

    // Merge with another CTransactionConsumer
    virtual void Merge(const ITransactionConsumer *inConsumer)
    {
        const CTransactionConsumer *theConsumer =
            static_cast<const CTransactionConsumer *>(inConsumer);
        m_TransactionList.insert(m_TransactionList.begin(), theConsumer->m_TransactionList.begin(),
                                 theConsumer->m_TransactionList.end());
    }
    void Reset()
    {
        m_TransactionList.clear();
        m_DoNotifications.clear();
        m_UndoNotifications.clear();
    }
};

template <typename TTransactionType>
inline void RunWithConsumer(TTransactionConsumerPtr inConsumer, TTransactionType inTransaction)
{
    if (inConsumer)
        inTransaction(inConsumer);
}

template <typename TDoTransaction, typename TUndoTransaction>
inline void CreateGenericTransactionWithConsumer(const char *inFile, int inLine,
                                                 TTransactionConsumerPtr inConsumer,
                                                 TDoTransaction inDoTransaction,
                                                 TUndoTransaction inUndoTransaction)
{
    if (inConsumer)
        inConsumer->OnTransaction(TTransactionPtr(
            DoCreateGenericTransaction(inFile, inLine, inDoTransaction, inUndoTransaction)));
}

template <typename TItemType>
inline void DoSetConsumer(TTransactionConsumerPtr inConsumer,
                          std::shared_ptr<TItemType> inTypePtr)
{
    ITransactionProducer *theProducer = dynamic_cast<ITransactionProducer *>(inTypePtr.get());
    if (theProducer)
        theProducer->SetConsumer(inConsumer);
}

template <typename TContainer>
inline void Undo(TContainer &inTransactions)
{
    std::for_each(inTransactions.rbegin(), inTransactions.rend(), std::bind(&ITransaction::Undo,
                                                                            std::placeholders::_1));
}

template <typename TContainer>
inline void Redo(TContainer &inTransactions)
{
    do_all(inTransactions, std::bind(&ITransaction::Do, std::placeholders::_1));
}

template <typename TItemType>
void Notify(std::vector<TItemType> &inNotifications)
{
    do_all(inNotifications, std::bind(&TItemType::operator(), std::placeholders::_1));
}

template <typename TItemType>
void NotifyReverse(std::vector<TItemType> &inNotifications)
{
    std::for_each(inNotifications.rbegin(), inNotifications.rend(),
                  std::bind(&TItemType::operator(), std::placeholders::_1));
}
}

#endif