summaryrefslogtreecommitdiffstats
path: root/src/Authoring/Studio/_Win/UI/TimeLineToolbar.cpp
blob: 2d8c460f06986368f01621eaa32edc6d03f18a4a (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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
/****************************************************************************
**
** 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 "TimeLineToolbar.h"
#include "ui_TimeLineToolbar.h"
#include "StudioApp.h"
#include "MainFrm.h"
#include "TimeEditDlg.h"
#include "DataInputSelectDlg.h"
#include "DataInputDlg.h"
#include "Doc.h"
#include "Core.h"
#include "Dispatch.h"
#include "Qt3DSDMStudioSystem.h"
#include "ClientDataModelBridge.h"
#include "StudioObjectTypes.h"
#include "IDocumentEditor.h"
#include "DocumentEditorEnumerations.h"
#include "StudioPreferences.h"

#include <QtCore/qdatetime.h>

TimeLineToolbar::TimeLineToolbar(CMainFrame *mainFrame, const QSize &preferredSize,
                                 QWidget *pParent)
    : QWidget(pParent)
    , m_ui(new QT_PREPEND_NAMESPACE(Ui::TimeLineToolbar))
    , m_preferredSize(preferredSize)
    , m_mainFrame(mainFrame)
    , m_DataInputSelector(nullptr)
{
    m_ui->setupUi(this);

    connect(m_ui->playButton, &QToolButton::clicked,
            this, &TimeLineToolbar::onPlayButtonClicked);
    connect(m_ui->rewindButton, &QToolButton::clicked,
            mainFrame, &CMainFrame::OnPlaybackRewind);
    connect(mainFrame, &CMainFrame::playStateChanged,
            [this](bool started) {
        if (started) {
            m_ui->playButton->setIcon(QIcon(":/images/playback_tools_low-01.png"));
            m_ui->playButton->setToolTip(tr("Stop animation"));
        } else {
            m_ui->playButton->setIcon(QIcon(":/images/playback_tools_low-02.png"));
            m_ui->playButton->setToolTip(tr("Play animation"));
        }
    });

    CDoc *doc = g_StudioApp.GetCore()->GetDoc();
    connect(m_ui->timeButton, &QPushButton::clicked,
            [this, doc](){
        CTimeEditDlg timeEditDlg;
        timeEditDlg.showDialog(doc->GetCurrentViewTime(), doc, PLAYHEAD);
    });

    connect(m_ui->deleteObject, &QPushButton::clicked,
            doc, &CDoc::DeleteSelectedObject);

    CDispatch *theDispatch = g_StudioApp.GetCore()->GetDispatch();
    m_Connections.push_back(theDispatch->ConnectSelectionChange(
        std::bind(&TimeLineToolbar::OnSelectionChange, this, std::placeholders::_1)));

    connect(m_ui->addLayerButton, &QPushButton::clicked,
            this, &TimeLineToolbar::onAddLayerClicked);
    // Set as parent to mainframe to allow positioning in the main window
    m_DataInputSelector = new DataInputSelectDlg(mainFrame);
    m_DataInputSelector->hide();
    m_DataInputSelector->setWindowTitle(tr("Select timeline controller"));


    connect(m_ui->addDataInputButton, &QPushButton::clicked,
            this, &TimeLineToolbar::onAddDataInputClicked);

    theDispatch->AddDataModelListener(this);
    connect(m_DataInputSelector, &DataInputSelectDlg::dataInputChanged,
            this, &TimeLineToolbar::onDataInputChange);
}

TimeLineToolbar::~TimeLineToolbar()
{
    delete m_ui;
    delete m_DataInputSelector;
    m_Connections.clear();
}

QSize TimeLineToolbar::sizeHint() const
{
    return m_preferredSize;
}

void TimeLineToolbar::onTimeChanged(long time)
{
    QString formattedTime;
    if (time < 1000 * 60 * 60) {
        formattedTime = QTime::fromMSecsSinceStartOfDay(time).toString(
                    QStringLiteral("m:ss.zzz"));
    } else {
        formattedTime = QTime::fromMSecsSinceStartOfDay(time).toString(
                    QStringLiteral("H:mm:ss.zzz"));
    }
    m_ui->timeButton->setText(formattedTime);

    m_ui->rewindButton->setEnabled(time != 0);
}

void TimeLineToolbar::OnSelectionChange(Q3DStudio::SSelectedValue newSelectable)
{
    qt3dsdm::TInstanceHandleList selectedInstances = newSelectable.GetSelectedInstances();
    CDoc *doc = g_StudioApp.GetCore()->GetDoc();
    CClientDataModelBridge *theClientBridge = doc->GetStudioSystem()->GetClientDataModelBridge();
    bool canDelete = false;
    for (size_t idx = 0, end = selectedInstances.size(); idx < end; ++idx) {
        if (theClientBridge->CanDelete(selectedInstances[idx])) {
            canDelete = true;
            break;
        }
    }
    m_ui->deleteObject->setEnabled(canDelete);

    updateDataInputStatus(false);
}

void TimeLineToolbar::onAddDataInputClicked()
{
    showDataInputChooser();
}

// Update datainput button state according to this timecontext
// control state. If triggered via datamodel change i.e. dispatch message,
// force update from actual property values
void TimeLineToolbar::updateDataInputStatus(bool isViaDispatch)
{
    CDoc *doc = g_StudioApp.GetCore()->GetDoc();
    qt3dsdm::Qt3DSDMPropertyHandle ctrldProp;
    qt3dsdm::Qt3DSDMInstanceHandle timeCtxRoot = doc->GetActiveRootInstance();
    CClientDataModelBridge *theClientBridge = doc->GetStudioSystem()->GetClientDataModelBridge();
    // Only check for updates if we have entered new time context or receive dispatch (undo/redo).
    // Actual control changes in this time context are handled in onDataInputChange
    if (m_currTimeCtxRoot != timeCtxRoot || isViaDispatch) {
        if (theClientBridge->GetObjectType(timeCtxRoot) == EStudioObjectType::OBJTYPE_SCENE) {
            ctrldProp = theClientBridge->GetObjectDefinitions().m_Scene.m_ControlledProperty;
        } else if (theClientBridge->GetObjectType(timeCtxRoot) ==
                   EStudioObjectType::OBJTYPE_COMPONENT) {
            ctrldProp = theClientBridge->GetObjectDefinitions().m_Component.m_ControlledProperty;
        } else {
            Q_ASSERT(false);
        }

        qt3dsdm::Option<qt3dsdm::SValue> controlledPropertyVal
            = Q3DStudio::SCOPED_DOCUMENT_EDITOR(
                *doc,
                QObject::tr("Get Timeline control"))->GetInstancePropertyValue(timeCtxRoot,
                                                                               ctrldProp);
        auto existingCtrl = qt3dsdm::get<QString>(controlledPropertyVal.getValue());

        if (existingCtrl.contains("@timeline")) {
            int slideStrPos = existingCtrl.indexOf("@timeline");
            int ctrStrPos = existingCtrl.lastIndexOf(" ", slideStrPos - 2);
            m_currController = existingCtrl.mid(ctrStrPos + 1, slideStrPos - ctrStrPos - 2);
        } else {
            m_currController.clear();
        }
        // Toggle if we changed to a controlled time context, or if icon current state
        // differs from the control state of current time context
        if (m_currController.size()) {
            m_ui->addDataInputButton->setToolTip(
                tr("Timeline Controller:\n%1").arg(m_currController));
            m_ui->addDataInputButton->setIcon(QIcon(":/images/Objects-DataInput-Normal.png"));
        } else {
            // TODO actually delete the entire property instead of setting it as empty string
            m_ui->addDataInputButton->setIcon(QIcon(":/images/Objects-DataInput-Disabled.png"));
            m_ui->addDataInputButton->setToolTip(tr("No control"));
        }

        m_currTimeCtxRoot = timeCtxRoot;
    }
}

void TimeLineToolbar::showDataInputChooser()
{
    QStringList dataInputList;
    dataInputList.append(tr("[No control]"));
    for (int i = 0; i < g_StudioApp.m_dataInputDialogItems.size(); i++) {
        if (g_StudioApp.m_dataInputDialogItems[i]->type == EDataType::DataTypeRangedNumber)
            dataInputList.append(g_StudioApp.m_dataInputDialogItems[i]->name);
    }
    QString currCtr = m_currController.size() ?
        m_currController : tr("[No control]");
    m_DataInputSelector->setData(dataInputList, currCtr);
    m_DataInputSelector->showDialog(QCursor::pos());

    return;
}

void TimeLineToolbar::onDataInputChange(const QString &dataInputName)
{
    if (dataInputName == m_currController ||
        (dataInputName == tr("[No Control]") && !m_currController.size())) {
        return;
    }

    CDoc *doc = g_StudioApp.GetCore()->GetDoc();
    CClientDataModelBridge *bridge = doc->GetStudioSystem()->GetClientDataModelBridge();
    QString fullTimeControlStr;

    if (dataInputName != tr("[No control]")) {
        m_ui->addDataInputButton->setToolTip(tr("Timeline Controller:\n%1").arg(dataInputName));
        fullTimeControlStr = dataInputName + " @timeline";
        m_ui->addDataInputButton->setIcon(QIcon(":/images/Objects-DataInput-Normal.png"));
        m_currController = dataInputName;
    } else {
        m_ui->addDataInputButton->setToolTip(tr("No control"));
        // TODO actually delete the entire property instead of setting it as empty string
        m_ui->addDataInputButton->setIcon(QIcon(":/images/Objects-DataInput-Disabled.png"));
        m_currController.clear();
    }

    // To indicate that this presentation timeline is controlled by data input,
    // we set "controlled property" of this time context root (scene or component)
    // to contain the name of controller followed by special indicator "@timeline".
    // Either replace existing timeline control indicator string or append new one
    // but do not touch @slide indicator string as scene can have both
    qt3dsdm::Qt3DSDMPropertyHandle ctrldPropertyHandle;
    qt3dsdm::Qt3DSDMInstanceHandle timeCtxRoot = doc->GetActiveRootInstance();
    // Time context root is either scene or component
    if (bridge->GetObjectType(timeCtxRoot) == EStudioObjectType::OBJTYPE_SCENE)
        ctrldPropertyHandle = bridge->GetObjectDefinitions().m_Scene.m_ControlledProperty;
    else if (bridge->GetObjectType(timeCtxRoot) == EStudioObjectType::OBJTYPE_COMPONENT)
        ctrldPropertyHandle = bridge->GetObjectDefinitions().m_Component.m_ControlledProperty;
    else
        Q_ASSERT(false);

    qt3dsdm::Option<qt3dsdm::SValue> controlledPropertyVal
        = Q3DStudio::SCOPED_DOCUMENT_EDITOR(
            *doc,
            QObject::tr("Get DataInput control"))->GetInstancePropertyValue(timeCtxRoot,
                                                                            ctrldPropertyHandle);

    auto existingCtrl = qt3dsdm::get<QString>(controlledPropertyVal.getValue());
    if (existingCtrl.contains("@timeline")) {
        int slideStrPos = existingCtrl.indexOf("@timeline");
        // find the controlling datainput name and build the string to replace
        int ctrStrPos = existingCtrl.lastIndexOf(" ", slideStrPos - 2);
        QString prevCtrler = existingCtrl.mid(ctrStrPos + 1, slideStrPos - ctrStrPos - 1);
        existingCtrl.replace(prevCtrler + "@timeline", fullTimeControlStr);
    } else {
        if (!existingCtrl.isEmpty() && m_currController.size())
            existingCtrl.append(" ");
        existingCtrl.append(fullTimeControlStr);
    }

    if (existingCtrl.endsWith(" "))
        existingCtrl.chop(1);

    if (existingCtrl.startsWith(" "))
        existingCtrl.remove(0, 1);

    m_currTimeCtxRoot = timeCtxRoot;

    qt3dsdm::SValue fullCtrlPropVal
        = std::make_shared<qt3dsdm::CDataStr>(
            Q3DStudio::CString::fromQString(existingCtrl));
    Q3DStudio::SCOPED_DOCUMENT_EDITOR(*doc, QObject::tr("Set Timeline control"))
        ->SetInstancePropertyValue(timeCtxRoot, ctrldPropertyHandle, fullCtrlPropVal);
    return;
}

void TimeLineToolbar::onAddLayerClicked()
{
    using namespace Q3DStudio;

    CDoc *doc = g_StudioApp.GetCore()->GetDoc();
    CClientDataModelBridge *bridge = doc->GetStudioSystem()->GetClientDataModelBridge();

    // If active instance is component, just bail as we can't add layers to components
    qt3dsdm::Qt3DSDMInstanceHandle rootInstance = doc->GetActiveRootInstance();
    if (bridge->GetObjectType(rootInstance) == OBJTYPE_COMPONENT)
        return;

    qt3dsdm::Qt3DSDMSlideHandle slide = doc->GetActiveSlide();
    qt3dsdm::Qt3DSDMInstanceHandle layer = doc->GetActiveLayer();

    SCOPED_DOCUMENT_EDITOR(*doc, QObject::tr("Add Layer"))
        ->CreateSceneGraphInstance(qt3dsdm::ComposerObjectTypes::Layer, layer, slide,
                                   DocumentEditorInsertType::PreviousSibling,
                                   CPt(), PRIMITIVETYPE_UNKNOWN, -1);
}

void TimeLineToolbar::onPlayButtonClicked()
{
    CDoc *doc = g_StudioApp.GetCore()->GetDoc();
    if (doc->IsPlaying())
        m_mainFrame->OnPlaybackStop();
    else
        m_mainFrame->OnPlaybackPlay();
}

void TimeLineToolbar::OnBeginDataModelNotifications()
{
}

void TimeLineToolbar::OnEndDataModelNotifications()
{
    updateDataInputStatus(true);
}

void TimeLineToolbar::OnImmediateRefreshInstanceSingle(qt3dsdm::Qt3DSDMInstanceHandle inInstance)
{
    updateDataInputStatus(true);
}

void TimeLineToolbar::OnImmediateRefreshInstanceMultiple(
    qt3dsdm::Qt3DSDMInstanceHandle *inInstance, long inInstanceCount)
{
    updateDataInputStatus(true);
}