summaryrefslogtreecommitdiffstats
path: root/src/dm/systems/Qt3DSDMGuides.cpp
blob: e8ea421f93216a33647d99c4a0149334f53f31a9 (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
/****************************************************************************
**
** 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$
**
****************************************************************************/
#include "Qt3DSDMPrefix.h"
#include "Qt3DSDMGuides.h"
#include <unordered_map>
#include "VectorTransactions.h"

using namespace qt3dsdm;

namespace {

#define CONNECT(x) std::make_shared<qt3dsdm::QtSignalConnection>(QObject::connect(this, x, inCallback))

class SGuideSystem : public QObject, public IGuideSystem
{
    Q_OBJECT
public:
    typedef std::unordered_map<long, SGuideInfo> TGuideMap;
    typedef std::shared_ptr<IMergeableTransaction<SGuideInfo>> TMergeableTransaction;
    typedef std::unordered_map<long, TMergeableTransaction> TGuideInfoMergeMap;

    long m_NextHandleValue;
    TGuideMap m_Guides;
    bool m_GuidesEditable;

    std::shared_ptr<ITransactionConsumer> m_CurrentTransaction;
    TGuideInfoMergeMap m_GuideMergeMap;
Q_SIGNALS:
    void guideCreated(Qt3DSDMGuideHandle, SGuideInfo);
    void guideDestroyed(Qt3DSDMGuideHandle, SGuideInfo);
    void guideModified(Qt3DSDMGuideHandle, SGuideInfo);
    void guideModifiedImmediate(Qt3DSDMGuideHandle, SGuideInfo);
public:
    SGuideSystem()
        : m_NextHandleValue(0)
        , m_GuidesEditable(true)
    {
    }

    void SignalGuideCreated(long hdl, const SGuideInfo &inInfo) { Q_EMIT guideCreated(hdl, inInfo); }

    void SignalGuideDestroyed(long hdl, const SGuideInfo &inInfo) { Q_EMIT guideDestroyed(hdl, inInfo); }

    void SignalGuideModified(long hdl, const SGuideInfo &inInfo) { Q_EMIT guideModified(hdl, inInfo); }

    Qt3DSDMGuideHandle CreateGuide() override
    {
        ++m_NextHandleValue;
        std::pair<long, SGuideInfo> entry(std::make_pair(m_NextHandleValue, SGuideInfo()));
        m_Guides.insert(entry);
        if (m_CurrentTransaction) {
            CreateHashMapInsertTransaction(__FILE__, __LINE__, m_CurrentTransaction, entry,
                                           m_Guides);
            m_CurrentTransaction->OnDoNotification(std::bind(
                &SGuideSystem::SignalGuideCreated, this, m_NextHandleValue, SGuideInfo()));
            m_CurrentTransaction->OnUndoNotification(std::bind(
                &SGuideSystem::SignalGuideDestroyed, this, m_NextHandleValue, SGuideInfo()));
        }

        return m_NextHandleValue;
    }

    SGuideInfo *InternalGetGuideInfo(Qt3DSDMGuideHandle inGuideHandle)
    {
        TGuideMap::iterator theFind = m_Guides.find((long)inGuideHandle.GetHandleValue());
        if (theFind != m_Guides.end())
            return &theFind->second;
        return NULL;
    }

    const SGuideInfo *InternalGetGuideInfo(Qt3DSDMGuideHandle inGuideHandle) const
    {
        return const_cast<SGuideSystem &>(*this).InternalGetGuideInfo(inGuideHandle);
    }

    void SetGuideInfo(Qt3DSDMGuideHandle inGuideHandle, const SGuideInfo &info) override
    {
        SGuideInfo *existing = InternalGetGuideInfo(inGuideHandle);
        long theHdlValue = (long)inGuideHandle.GetHandleValue();
        TGuideInfoMergeMap::iterator iter = m_GuideMergeMap.find(theHdlValue);
        if (iter != m_GuideMergeMap.end()) {
            iter->second->Update(info);
            *existing = info;
        } else {
            if (!existing) {
                QT3DS_ASSERT(false);
                return;
            }
            SGuideInfo oldValue(*existing);
            *existing = info;
            if (m_CurrentTransaction) {
                TMergeableTransaction newTransaction =
                    CreateHashMapSwapTransaction(__FILE__, __LINE__, m_CurrentTransaction,
                                                 theHdlValue, oldValue, info, m_Guides);
                m_GuideMergeMap.insert(std::make_pair(theHdlValue, newTransaction));
                m_CurrentTransaction->OnDoNotification(
                    std::bind(&SGuideSystem::SignalGuideModified, this, theHdlValue, info));
                m_CurrentTransaction->OnUndoNotification(
                    std::bind(&SGuideSystem::SignalGuideModified, this, theHdlValue, oldValue));
            }
        }
        if (AreDataModelSignalsEnabled())
            Q_EMIT guideModifiedImmediate(theHdlValue, info);
    }

    SGuideInfo GetGuideInfo(Qt3DSDMGuideHandle inGuideHandle) const override
    {
        const SGuideInfo *existing = InternalGetGuideInfo(inGuideHandle);
        if (existing)
            return *existing;
        QT3DS_ASSERT(false);
        return SGuideInfo();
    }

    void DeleteGuide(Qt3DSDMGuideHandle inGuideHandle) override
    {

        SGuideInfo *existing = InternalGetGuideInfo(inGuideHandle);
        if (!existing) {
            QT3DS_ASSERT(false);
            return;
        }
        long theHdlValue = (long)inGuideHandle.GetHandleValue();
        SGuideInfo oldValue = *existing;
        m_Guides.erase(theHdlValue);

        if (m_CurrentTransaction) {
            std::pair<long, SGuideInfo> entry(std::make_pair(theHdlValue, oldValue));
            CreateHashMapEraseTransaction(__FILE__, __LINE__, m_CurrentTransaction, entry,
                                          m_Guides);
            m_CurrentTransaction->OnDoNotification(
                std::bind(&SGuideSystem::SignalGuideDestroyed, this, theHdlValue, oldValue));
            m_CurrentTransaction->OnUndoNotification(
                std::bind(&SGuideSystem::SignalGuideCreated, this, theHdlValue, oldValue));
        }
    }

    TGuideHandleList GetAllGuides() const override
    {
        TGuideHandleList retval;
        for (TGuideMap::const_iterator iter = m_Guides.begin(), end = m_Guides.end(); iter != end;
             ++iter)
            retval.push_back(iter->first);
        return retval;
    }

    bool IsGuideValid(Qt3DSDMGuideHandle inGuideHandle) const override
    {
        return InternalGetGuideInfo(inGuideHandle) != NULL;
    }
    bool AreGuidesEditable() const override { return m_GuidesEditable; }
    void SetGuidesEditable(bool val) override { m_GuidesEditable = val; }

    // Undo/Redo
    void SetConsumer(std::shared_ptr<ITransactionConsumer> inConsumer) override
    {
        m_CurrentTransaction = inConsumer;
        m_GuideMergeMap.clear();
    }

    // These are events coming from undo/redo operations, not events coming directly from the
    // modification of the guides
    virtual TSignalConnectionPtr
    ConnectGuideCreated(const std::function<void(Qt3DSDMGuideHandle, SGuideInfo)> &inCallback) override
    {
        return CONNECT(&SGuideSystem::guideCreated);
    }

    virtual TSignalConnectionPtr
    ConnectGuideDestroyed(const std::function<void(Qt3DSDMGuideHandle, SGuideInfo)> &inCallback) override
    {
        return CONNECT(&SGuideSystem::guideDestroyed);
    }

    virtual TSignalConnectionPtr
    ConnectGuideModified(const std::function<void(Qt3DSDMGuideHandle, SGuideInfo)> &inCallback) override
    {
        return CONNECT(&SGuideSystem::guideModified);
    }

    // Signal happens immediately instead of on undo/redo, used for live-update of the inspector
    // palette
    TSignalConnectionPtr ConnectGuideModifiedImmediate(
        const std::function<void(Qt3DSDMGuideHandle, SGuideInfo)> &inCallback) override
    {
        return CONNECT(&SGuideSystem::guideModifiedImmediate);
    }
};
}

shared_ptr<IGuideSystem> IGuideSystem::CreateGuideSystem()
{
    return std::make_shared<SGuideSystem>();
}

#include "Qt3DSDMGuides.moc"