summaryrefslogtreecommitdiffstats
path: root/src/Authoring/Studio/Application/DurationEditDlg.cpp
blob: 0d7c48999563661f863b16498ff8b396f161b463 (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
341
342
343
344
345
346
/****************************************************************************
**
** Copyright (C) 2002 NVIDIA Corporation.
** Copyright (C) 2018 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 "ui_DurationEditDlg.h"
#include "DurationEditDlg.h"
#include "IDoc.h"
#include "Bindings/ITimelineKeyframesManager.h"

#include <QtGui/qvalidator.h>

//=============================================================================
/**
 * Constructor
 */
CDurationEditDlg::CDurationEditDlg(QWidget *pParent)
    : QDialog(pParent)
    , m_ui(new Ui::DurationEditDlg)
    , m_Doc(nullptr)
    , m_KeyframesManager(nullptr)
    , m_Callback(nullptr)
    , m_MaxTime(0)
    , m_MaxTimeDisplay(0)
    , m_MinTimeDisplay(0)
    , m_InitialTimeStart(0)
    , m_InitialTimeEnd(0)
    , m_minStart(-1)
    , m_secStart(-1)
    , m_minEnd(-1)
    , m_secEnd(-1)
{
    m_ui->setupUi(this);
    setAutoFillBackground(true);

    QIntValidator *minValidator = new QIntValidator(this);
    minValidator->setRange(0, 9999);
    m_ui->lineEditMinutes->setValidator(minValidator);
    m_ui->lineEditEndMinutes->setValidator(minValidator);
    QIntValidator *secValidator = new QIntValidator(this);
    secValidator->setRange(0, 59);
    m_ui->lineEditSeconds->setValidator(secValidator);
    m_ui->lineEditEndSeconds->setValidator(secValidator);
    QIntValidator *msecValidator = new QIntValidator(this);
    msecValidator->setRange(0, 999);
    m_ui->lineEditMilliseconds->setValidator(msecValidator);
    m_ui->lineEditEndMilliseconds->setValidator(msecValidator);

    connect(m_ui->lineEditMinutes, &QLineEdit::textEdited,
            this, &CDurationEditDlg::onStartTimeChanged);
    connect(m_ui->lineEditSeconds, &QLineEdit::textEdited,
            this, &CDurationEditDlg::onStartTimeChanged);
    connect(m_ui->lineEditMilliseconds, &QLineEdit::textEdited,
            this, &CDurationEditDlg::onStartTimeChanged);

    connect(m_ui->lineEditEndMinutes, &QLineEdit::textEdited,
            this, &CDurationEditDlg::onEndTimeChanged);
    connect(m_ui->lineEditEndSeconds, &QLineEdit::textEdited,
            this, &CDurationEditDlg::onEndTimeChanged);
    connect(m_ui->lineEditEndMilliseconds, &QLineEdit::textEdited,
            this, &CDurationEditDlg::onEndTimeChanged);

    window()->setFixedSize(size());
}

CDurationEditDlg::~CDurationEditDlg()
{
    delete m_ui;
}

void CDurationEditDlg::setKeyframesManager(ITimelineKeyframesManager *inKeyframesManager)
{
    m_KeyframesManager = inKeyframesManager;
}

//=============================================================================
/**
 *  showDialog: Initializes and shows the Duration Edit Dialog Box.
 *  @param startTime is the initial start time, which will be shown when the time edit
 *                   dialog box pops up
 *  @param endTime is the initial end time, which will be shown when the time edit
 *                 dialog box pops up
 *  @param inDoc this can be nullptr where its not applicable
 *  @param inCallback is the target object for the callbacks
 */
void CDurationEditDlg::showDialog(long startTime, long endTime, IDoc *inDoc,
                                  ITimeChangeCallback *inCallback)
{
    m_InitialTimeStart = startTime;
    m_InitialTimeEnd = endTime;
    m_Doc = inDoc;
    m_Callback = inCallback;

    m_MinTimeDisplay = 0;
    // if it is a Timebar, this will be adjusted, else this should be initialized to some value at
    // least, for OverflowHandling to work correctly
    m_MaxTimeDisplay = LONG_MAX;

    // 9999:59:999 converted to milliseconds
    m_MaxTime = timeConversion(9999, CONVERT_MIN_TO_MSEC)
            + timeConversion(59, CONVERT_SEC_TO_MSEC) + 999;

    // Set initial values to dialog
    formatTime(m_InitialTimeStart, true);
    formatTime(m_InitialTimeEnd, false);

    // Present the dialog
    exec();
}

void CDurationEditDlg::formatTime(long inTime, bool startTime)
{
    long theTime = inTime;
    long min = 0;
    long sec = 0;
    long msec = 0;

    // Translates the m_initialTime (in milliseconds) into Minutes, Seconds and Milliseconds
    if (inTime != 0) {
        min = timeConversion(theTime, CONVERT_MSEC_TO_MIN);
        theTime = theTime - timeConversion(min, CONVERT_MIN_TO_MSEC);
        sec = timeConversion(theTime, CONVERT_MSEC_TO_SEC);
        theTime = theTime - timeConversion(sec, CONVERT_SEC_TO_MSEC);
        msec = theTime;
    }

    if (startTime) {
        m_ui->lineEditMinutes->setText(QString::number(min));
        m_ui->lineEditSeconds->setText(QString::number(sec));
        m_ui->lineEditMilliseconds->setText(QString::number(msec));

        // Select the biggest non-zero unit
        if (min > 0) {
            m_ui->lineEditMinutes->setFocus();
            m_ui->lineEditMinutes->selectAll();
        } else if (sec > 0) {
            m_ui->lineEditSeconds->setFocus();
            m_ui->lineEditSeconds->selectAll();
        } else {
            m_ui->lineEditMilliseconds->setFocus();
            m_ui->lineEditMilliseconds->selectAll();
        }
    } else {
        m_ui->lineEditEndMinutes->setText(QString::number(min));
        m_ui->lineEditEndSeconds->setText(QString::number(sec));
        m_ui->lineEditEndMilliseconds->setText(QString::number(msec));
    }
}

void CDurationEditDlg::showEvent(QShowEvent *ev)
{
    QDialog::showEvent(ev);
}

void CDurationEditDlg::accept()
{
    m_Callback->Commit();
    QDialog::accept();
}

void CDurationEditDlg::reject()
{
    m_Callback->Rollback();
    QDialog::reject();
}

long CDurationEditDlg::numberOfDigits(long number)
{
    long theNumberOfDigits = 0;
    for (long theNumber = number; theNumber >= 1; theNumber = theNumber / 10)
        theNumberOfDigits++;
    return theNumberOfDigits;
}

//==============================================================================
/**
 *  timeConversion:         Converts inTime to the format specified by inFlags.
 *                          For example:
 *                          inTime = 5 sec inFlags = CONVERT_SEC_TO_MSEC
 *                          The method will convert 5 sec into 5000 msec and
 *                          returns the result.
 *  @param  inTime          stores the time to be converted.
 *          inOperationCode determines the type of time conversion to be done on the
 *                          inTime.
 *  @return theResult       stores the result of the time conversion.
 */
long CDurationEditDlg::timeConversion(long inTime, long inOperationCode)
{
    long theResult = 0;
    switch (inOperationCode) {
    case CONVERT_MIN_TO_MSEC:
        theResult = inTime * 60 * 1000;
        break;
    case CONVERT_SEC_TO_MSEC:
        theResult = inTime * 1000;
        break;
    case CONVERT_MSEC_TO_MIN:
        theResult = inTime / (60 * 1000);
        break;
    case CONVERT_MSEC_TO_SEC:
        theResult = inTime / 1000;
        break;
    }
    return theResult;
}

//==============================================================================
/**
 *  timeConversion:         Takes in the time in mins:secs:msec and convert it to
 *                          the corresponding time in msec.
 *  @param  inMin           stores the minutes to be converted.
 *          inSec           stores the seconds to be converted.
 *          inMsec          stores the milliseconds to be converted.
 *          inOperationCode determines the type of time conversion to be done on the
 *                          inMin, inSec and inMsec.
 *  @return theResult       stores the result of the time conversion.
 */
long CDurationEditDlg::timeConversion(long inMin, long inSec, long inMsec, long inOperationCode)
{
    long theResult = 0;
    switch (inOperationCode) {
    case CONVERT_TIME_TO_MSEC:
        theResult = timeConversion(inMin, CONVERT_MIN_TO_MSEC)
                + timeConversion(inSec, CONVERT_SEC_TO_MSEC) + inMsec;
        break;
    }
    return theResult;
}

//==============================================================================
/**
 *  timeConversion:         Takes in the time in milliseconds and converts them
 *                          to min : sec : msec.
 *  @param  inTotalTime     stores the total time in msec.
 *          ioMin           stores the mins result of the time conversion
 *          ioSec           stores the secs result of the time conversion
 *          ioMsec          stores the msecs result of the time conversion
 *          inOperationCode determines the type of time conversion to be done on the
 *                          inTotalTime.
 */
void CDurationEditDlg::timeConversion(long inTotalTime, long *ioMin, long *ioSec, long *ioMsec,
                                      long inOperationCode)
{
    switch (inOperationCode) {
    case CONVERT_MSEC_TO_MIN_SEC_MSEC:
        *ioMin = timeConversion(inTotalTime, CONVERT_MSEC_TO_MIN);
        *ioSec = inTotalTime - timeConversion(*ioMin, CONVERT_MIN_TO_MSEC);
        *ioSec = timeConversion(*ioSec, CONVERT_MSEC_TO_SEC);
        *ioMsec = inTotalTime - timeConversion(*ioMin, CONVERT_MIN_TO_MSEC)
                - timeConversion(*ioSec, CONVERT_SEC_TO_MSEC);
        break;
    }
}

void CDurationEditDlg::updateObjectTime(long inTime, bool startTime)
{
    if (m_Callback) {
        if (startTime)
            m_Callback->ChangeStartTime(inTime); // Update Start Time
        else
            m_Callback->ChangeEndTime(inTime); // Update End Time
    }
}

void CDurationEditDlg::onStartTimeChanged()
{
    // Making sure that the start time is not greater than the end time, when
    // the user modifies the start time of the timebar
    m_MaxTimeDisplay = m_InitialTimeEnd; // the initial end time
    m_MinTimeDisplay = 0;

    long min = m_ui->lineEditMinutes->text().toInt();
    long sec = m_ui->lineEditSeconds->text().toInt();
    long msec = m_ui->lineEditMilliseconds->text().toInt();

    long theGoToTime = timeConversion(min, CONVERT_MIN_TO_MSEC)
            + timeConversion(sec, CONVERT_SEC_TO_MSEC) + msec;

    // Go to the time specified in the start time edit display
    updateObjectTime(theGoToTime, true);

    // If max number of digits reached in a number field, select the next
    if (m_minStart != min && numberOfDigits(min) == 4) {
        m_ui->lineEditSeconds->setFocus();
        m_ui->lineEditSeconds->selectAll();
    } else if (m_secStart != sec && numberOfDigits(sec) == 2) {
        m_ui->lineEditMilliseconds->setFocus();
        m_ui->lineEditMilliseconds->selectAll();
    }

    m_minStart = min;
    m_secStart = sec;
}

void CDurationEditDlg::onEndTimeChanged()
{
    // Let the end time of the time bar go as far as possible
    m_MaxTimeDisplay = m_MaxTime;
    m_MinTimeDisplay = m_InitialTimeStart; // the initial start time

    long min = m_ui->lineEditEndMinutes->text().toInt();
    long sec = m_ui->lineEditEndSeconds->text().toInt();
    long msec = m_ui->lineEditEndMilliseconds->text().toInt();

    long theGoToTime = timeConversion(min, CONVERT_MIN_TO_MSEC)
            + timeConversion(sec, CONVERT_SEC_TO_MSEC) + msec;

    // Go to the time specified in the end time edit display
    updateObjectTime(theGoToTime, false);

    // If max number of digits reached in a number field, select the next
    if (m_minEnd != min && numberOfDigits(min) == 4) {
        m_ui->lineEditEndSeconds->setFocus();
        m_ui->lineEditEndSeconds->selectAll();
    } else if (m_secEnd != sec && numberOfDigits(sec) == 2) {
        m_ui->lineEditEndMilliseconds->setFocus();
        m_ui->lineEditEndMilliseconds->selectAll();
    }

    m_minEnd = min;
    m_secEnd = sec;
}