summaryrefslogtreecommitdiffstats
path: root/src/Authoring/Qt3DStudio/UI/StudioAppPrefsPage.cpp
blob: df918d199c73a32ff3f8605d100b871987ec3a4d (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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
/****************************************************************************
**
** Copyright (C) 2002 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 "ui_StudioAppPrefsPage.h"

#include "Doc.h"
#include "StudioAppPrefsPage.h"
#include "StudioConst.h"
#include "StudioProjectSettings.h"
#include "StudioPreferences.h"
#include "StudioApp.h"
#include "StudioPreferences.h"
#include "Views.h"
#include "Qt3DSDMStudioSystem.h"
#include "ClientDataModelBridge.h"
#include "Core.h"
#include "IStudioRenderer.h"
#include "MainFrm.h"

#include <QtWidgets/qmessagebox.h>
#include <QtWidgets/qlistview.h>
#include <QtGui/qstandarditemmodel.h>
#include <QtCore/qdiriterator.h>

//==============================================================================
/**
 *	Constructor: Initializes the object.
 */
//==============================================================================
CStudioAppPrefsPage::CStudioAppPrefsPage(QWidget *parent)
    : CStudioPreferencesPropPage(parent)
    , m_timebarShowTime(false)
    , m_interpolationIsSmooth(false)
    , m_restartNeeded(false)
    , m_autosaveChanged(false)
    , m_ui(new Ui::StudioAppPrefsPage)
{
    m_font = QFont(CStudioPreferences::fontFaceName());
    m_font.setPixelSize(CStudioPreferences::fontSize());

    // Create a bold font for the group box text
    m_boldFont = m_font;
    m_boldFont.setBold(true);

    onInitDialog();
}

//==============================================================================
/**
 *	Destructor: Releases the object.
 */
//==============================================================================
CStudioAppPrefsPage::~CStudioAppPrefsPage()
{
}

/////////////////////////////////////////////////////////////////////////////
// CStudioAppPrefsPage message handlers

void CStudioAppPrefsPage::onInitDialog()
{
    m_ui->setupUi(this);

    // Add tool tips for controls
    m_ui->m_DefaultInterpolation->setToolTip(tr("Set default keyframe interpolation type"));
    m_ui->m_checkTimelineAbsoluteSnapping->setToolTip(tr("Enable timeline snapping grid"));
    m_ui->m_checkLegacyViewer->setToolTip(tr("Enable preview with Qt3D Runtime Viewer"));
    m_ui->m_SnapRangeCombo->setToolTip(tr("Set resolution of timeline snapping grid"));
    m_ui->m_buttonRestoreDefaults->setToolTip(tr("Click to restore default Studio settings"));

    // Set fonts for child windows.
    for (auto w : findChildren<QWidget *>())
        w->setFont(m_font);

    // Make the group text bold
    for (auto w : findChildren<QGroupBox *>())
        w->setFont(m_boldFont);

    // Hidden until we have some other Preview configurations than just Viewer
    m_ui->groupBoxPreview->setVisible(false);

    // Load the settings for the controls
    loadSettings();

    auto activated = static_cast<void(QComboBox::*)(int)>(&QComboBox::activated);
    connect(m_ui->m_buttonRestoreDefaults, &QPushButton::clicked,
            this, &CStudioAppPrefsPage::onButtonRestoreDefaults);
    connect(m_ui->m_buttonResetLayout, &QPushButton::clicked, [=](){
        onApply(); // Save changed preferences before resetting, as it causes Studio to shut down
        CStudioPreferencesPropPage::endDialog(PREFS_RESET_LAYOUT);
    });
    connect(m_ui->m_DefaultInterpolation, activated, this, [=](){ setModified(true); });
    connect(m_ui->m_SnapRangeCombo, activated, this, [=](){ setModified(true); });
    connect(m_ui->m_checkTimelineAbsoluteSnapping, &QCheckBox::clicked,
            this, [=](){ setModified(true); enableOptions(); });
    connect(m_ui->m_checkLegacyViewer, &QCheckBox::clicked,
            this, [=](){ setModified(true); m_restartNeeded = true; });
    connect(m_ui->m_EditViewStartupView, activated, this, [=](){ setModified(true); });
    connect(m_ui->helperGridLinesSpinBox,
            static_cast<void (QSpinBox::*)(int)>(&QSpinBox::valueChanged),
            this, [=](){ setModified(true); });
    connect(m_ui->helperGridSpacingSpinBox,
            static_cast<void (QSpinBox::*)(int)>(&QSpinBox::valueChanged),
            this, [=](){ setModified(true); });
    connect(m_ui->selectorWidth,
            static_cast<void (QDoubleSpinBox::*)(double)>(&QDoubleSpinBox::valueChanged),
            this, [=](){ setModified(true); m_restartNeeded = true; });
    connect(m_ui->selectorLength,
            static_cast<void (QDoubleSpinBox::*)(double)>(&QDoubleSpinBox::valueChanged),
            this, [=](){ setModified(true); m_restartNeeded = true; });
    connect(m_ui->autosaveEnabled, &QCheckBox::clicked, this,
            [=](){ setModified(true); m_autosaveChanged = true; });
    connect(m_ui->autosaveInterval, static_cast<void (QSpinBox::*)(int)>(&QSpinBox::valueChanged),
            this, [=](){ setModified(true); m_autosaveChanged = true; });
    connect(m_ui->clearAutosaveFiles, &QPushButton::clicked,
            this, &CStudioAppPrefsPage::onClearAutosaveFiles);
#if 0 // Removed until we have some other Preview configurations than just Viewer
    connect(m_ui->m_PreviewSelector, activated,
            this, &CStudioAppPrefsPage::onChangePreviewConfiguration);
#endif
}

//==============================================================================
/**
 *	LoadSettings: Load the settings from the CDoc and set the control values.
 *
 *	@param	None
 */
//==============================================================================
void CStudioAppPrefsPage::loadSettings()
{
    // Get the Interpolation Preference
    m_ui->m_DefaultInterpolation->addItem(tr("Smooth"));
    m_ui->m_DefaultInterpolation->addItem(tr("Linear"));

    int theInterpolationPref = 0;
    if (CStudioPreferences::isInterpolation())
        theInterpolationPref = 0;
    else
        theInterpolationPref = 1;
    m_ui->m_DefaultInterpolation->setCurrentIndex(theInterpolationPref);

    // Timeline snapping grid
    m_ui->m_checkTimelineAbsoluteSnapping->setChecked(
                CStudioPreferences::isTimelineSnappingGridActive());

    // Legacy viewer
    m_ui->m_checkLegacyViewer->setChecked(CStudioPreferences::isLegacyViewerActive());

    // Helper grid
    m_ui->helperGridLinesSpinBox->setValue(CStudioPreferences::helperGridLines());
    m_ui->helperGridSpacingSpinBox->setValue(CStudioPreferences::helperGridSpacing());

    // Tool handles
    m_ui->selectorWidth->setValue(double(CStudioPreferences::selectorLineWidth()));
    m_ui->selectorLength->setValue(double(CStudioPreferences::selectorLineLength()));

    // The scale mode
    m_ui->m_SnapRangeCombo->addItem(tr("1 Second"));
    m_ui->m_SnapRangeCombo->addItem(tr("0.5 Seconds"));
    m_ui->m_SnapRangeCombo->addItem(tr("0.1 Seconds"));
    int theResolution = CStudioPreferences::timelineSnappingGridResolution();
    m_ui->m_SnapRangeCombo->setCurrentIndex(theResolution);

    // Autosave options
    m_ui->autosaveEnabled->setChecked(CStudioPreferences::isAutoSavePreference());
    m_ui->autosaveInterval->setValue(CStudioPreferences::autoSaveDelay());

    initEditStartViewCombo();

    enableOptions();

#if 0 // Removed until we have some other Preview configurations than just Viewer
    loadPreviewSelections();
#endif
}

/**
 *	SaveSettings: Save the settings from the controls to the CDoc
 *
 *	@param	None
 */
//==============================================================================
void CStudioAppPrefsPage::saveSettings()
{
    // Default interpolation
    g_StudioApp.GetCore()->GetDoc()->SetDefaultKeyframeInterpolation(
                m_ui->m_DefaultInterpolation->currentIndex() == 0);

    // Timeline settings
    // Timeline snapping grid
    CStudioPreferences::setTimelineSnappingGridActive(
                m_ui->m_checkTimelineAbsoluteSnapping->isChecked());
    int theCurrentSelection = m_ui->m_SnapRangeCombo->currentIndex();
    CStudioPreferences::setTimelineSnappingGridResolution(ESnapGridResolution(theCurrentSelection));

    // Viewing settings
    // Legacy viewer
    CStudioPreferences::setLegacyViewerActive(m_ui->m_checkLegacyViewer->isChecked());
    // Preferred Startup View
    CStudioPreferences::setPreferredStartupView(m_ui->m_EditViewStartupView->currentIndex());

    // VisualAids settings
    // Helper grid
    CStudioPreferences::setHelperGridLines(m_ui->helperGridLinesSpinBox->value());
    CStudioPreferences::setHelperGridSpacing(m_ui->helperGridSpacingSpinBox->value());
    // Tool handles
    CStudioPreferences::setSelectorLineWidth(float(m_ui->selectorWidth->value()));
    CStudioPreferences::setSelectorLineLength(float(m_ui->selectorLength->value()));

    // Autosave settings
    CStudioPreferences::setAutoSavePreference(m_ui->autosaveEnabled->isChecked());
    CStudioPreferences::setAutoSaveDelay(m_ui->autosaveInterval->value());
    enableAutosave(m_ui->autosaveEnabled->isChecked());
    setAutosaveInterval(m_ui->autosaveInterval->value());
    m_autosaveChanged = false;

#if 0 // Removed until we have some other Preview configurations than just Viewer
    savePreviewSettings();
#endif

    checkRestartCondition();
}

//==============================================================================
/**
 *	OnApply: Handler for the Apply button
 *
 *	@param	None
 */
//==============================================================================
bool CStudioAppPrefsPage::onApply()
{
    // Apply was clicked - save settings and disable the Apply button
    saveSettings();

    setModified(false);

    // Request that the renderer refreshes as settings may have changed
    g_StudioApp.getRenderer().RequestRender();

    return CStudioPreferencesPropPage::onApply();
}

//==============================================================================
/**
 *	OnButtonRestoreDefaults: Restore the defaults and exit the preferences.
 *
 *	@param	None
 */
//==============================================================================
void CStudioAppPrefsPage::onButtonRestoreDefaults()
{
    int theChoice = 0;

    // Ask the user if she really wants to do this
    theChoice = QMessageBox::question(this,
                                      tr("Restore Defaults"),
                                      tr("Are you sure that you want to restore all program "
                                         "\ndefaults? Your current settings will be lost."));

    // If the "yes" button was selected
    if (theChoice == QMessageBox::Yes) {
        // Restore default preferences by passing PREFS_RESET_DEFAULTS back
        // to the CStudioDocPreferences (that called this preferences sheet)
        CStudioPreferencesPropPage::endDialog(PREFS_RESET_DEFAULTS);
    }
}

//==============================================================================
/**
 *	EnableOptions: Enable/disable options.
 *
 *	@param	None
 */
//==============================================================================
void CStudioAppPrefsPage::enableOptions()
{
    m_ui->m_SnapRangeCombo->setEnabled(m_ui->m_checkTimelineAbsoluteSnapping->isChecked());
}

//==============================================================================
/**
 *	Initialise the combo box that displays the preferred startup view.
 *	Set the initial selection to that saved to the preferences
 */
//==============================================================================
void CStudioAppPrefsPage::initEditStartViewCombo()
{
    Q3DStudio::IStudioRenderer &theRenderer = g_StudioApp.getRenderer();
    QStringList theCameraNames;
    theRenderer.GetEditCameraList(theCameraNames);
    m_ui->m_EditViewStartupView->addItems(theCameraNames);
    m_ui->m_EditViewStartupView->addItem(tr("Scene Camera View"));

    m_ui->m_EditViewStartupView->insertSeparator(m_ui->m_EditViewStartupView->count() - 1);

    // adding a 1px spacing, else the separator will disappear sometimes (QComboBox bug)
    qobject_cast<QListView *>(m_ui->m_EditViewStartupView->view())->setSpacing(1);

    int thePreferredView = CStudioPreferences::preferredStartupView();
    int theNumItems = m_ui->m_EditViewStartupView->count();

    if (thePreferredView >= 0 && thePreferredView < theNumItems - 2)
        m_ui->m_EditViewStartupView->setCurrentIndex(thePreferredView);
    else // default to scene camera view
        m_ui->m_EditViewStartupView->setCurrentIndex(theNumItems - 1);
}

#if 0 // Removed until we have some other Preview configurations than just Viewer
void CStudioAppPrefsPage::loadPreviewSelections()
{
    // Load the configurations from all the .build files
    Q3DStudio::CBuildConfigurations &theConfig = g_StudioApp.GetCore()->GetBuildConfigurations();
    Q3DStudio::CBuildConfigurations::TBuildConfigurations theConfigurations =
            theConfig.GetConfigurations();
    Q3DStudio::CBuildConfigurations::TBuildConfigurations::iterator theIter;
    for (theIter = theConfigurations.begin(); theIter != theConfigurations.end(); ++theIter) {
        const Q3DStudio::CString &theConfig = theIter->first;
        m_ui->m_PreviewSelector->addItem(theConfig.toQString());
        m_ui->m_PreviewSelector->setItemData(m_ui->m_PreviewSelector->count() - 1,
                                             QVariant::fromValue(theIter->second));
    }

    int thePreviewSelected = m_ui->m_PreviewSelector->findText(
                CStudioPreferences::GetPreviewConfig().toQString());
    m_ui->m_PreviewSelector->setCurrentIndex(thePreviewSelected);
    if (thePreviewSelected == -1) {
        // select the first build configuration, or if no conriguration, the first application, i.e.
        // AMPlayer
        m_ui->m_PreviewSelector->setCurrentIndex(0);
        long thePreviewCount = m_ui->m_PreviewSelector->count();
        for (long theIndex = 0; theIndex < thePreviewCount; ++theIndex) {
            if (m_ui->m_PreviewSelector->itemData(
                        theIndex).value<Q3DStudio::CBuildConfiguration *>() != nullptr) {
                m_ui->m_PreviewSelector->setCurrentIndex(theIndex);
                break;
            }
        }
    }

    LoadBuildProperties();
}

//==============================================================================
/**
 *	When the build configuration is changed, all the properties have to be updated.
 */
//==============================================================================
void CStudioAppPrefsPage::onChangePreviewConfiguration()
{
    LoadBuildProperties();
}
#endif

void CStudioAppPrefsPage::enableAutosave(bool enabled)
{
    if (m_autosaveChanged)
        g_StudioApp.SetAutosaveEnabled(enabled);
}

void CStudioAppPrefsPage::setAutosaveInterval(int interval)
{
    if (m_autosaveChanged)
        g_StudioApp.SetAutosaveInterval(interval);
}

void CStudioAppPrefsPage::onClearAutosaveFiles()
{
    // Find all *_autosave.uip files and delete them
    QDirIterator files(g_StudioApp.GetCore()->GetDoc()->GetDocumentDirectory());
    while (files.hasNext()) {
        if (files.next().endsWith(QLatin1String("_autosave.uip")))
            QFile::remove(files.filePath());
    }
}

void CStudioAppPrefsPage::checkRestartCondition()
{
    if (m_restartNeeded) {
        // If special settings have changed, a restart of Studio is needed
        int retval = QMessageBox::question(this, tr("Restart Needed"),
                                           tr("Some settings were changed that require a\n"
                                              "restart of the Qt 3D Studio to take effect.\n"
                                              "Restart now?"));

        if (retval == QMessageBox::Yes) {
            g_StudioApp.GetViews()->getMainFrame()->handleGeometryAndState(true);
            CStudioPreferences::savePreferences();
            CStudioPreferencesPropPage::endDialog(PREFS_SETTINGS_RESTART);
        }

        // Just show the dialog once (unless the values are changed again)
        m_restartNeeded = false;
    }
}

//==============================================================================
/**
 *	Load the build properties for the current preview application selected
 */
//==============================================================================
#if 0 // Removed until we have some other Preview configurations than just Viewer
void CStudioAppPrefsPage::loadBuildProperties()
{
    // Remove those dynamic controls
    RemovePreviewPropertyControls();

    if (m_ui->m_PreviewSelector->count() > 0) {
        Q3DStudio::CBuildConfiguration *theConfig =
                m_ui->m_PreviewSelector->itemData(
                    m_ui->m_PreviewSelector->currentIndex())
                .value<Q3DStudio::CBuildConfiguration *>();
        if (theConfig) {
            // Only configuration read from .build files will have the ItemDataPtr set.

            Q3DStudio::CBuildConfiguration::TConfigProperties &theProperties =
                    theConfig->GetBuildProperties();

            auto layout = qobject_cast<QFormLayout *>(m_ui->groupBoxPreview->layout());
            auto activated = static_cast<void(QComboBox::*)(int)>(&QComboBox::activated);

            if (theProperties.empty() == false) {
                Q3DStudio::CBuildConfiguration::TConfigProperties::iterator theIter;

                for (theIter = theProperties.begin(); theIter != theProperties.end(); ++theIter) {
                    Q3DStudio::CBuildConfiguration::TConfigPropertyValues &theValues =
                            theIter->GetAcceptableValues();
                    // Only create the combo if there is more than 1 choices
                    if (theValues.size() > 1) {
                        Q3DStudio::CBuildConfiguration::TConfigPropertyValues::iterator
                                theValueIter;
                        long theMaxLength = 0;
                        for (theValueIter = theValues.begin(); theValueIter != theValues.end();
                             ++theValueIter) {
                            long theLabelLength = theValueIter->GetLabel().Length();
                            if (theLabelLength > theMaxLength)
                                theMaxLength = theLabelLength;
                        }

                        QLabel *theStaticText = new QLabel(theIter->GetLabel().toQString());
                        theStaticText->setFont(m_Font);
                        QComboBox *thePropertyDropdown = new QComboBox();
                        connect(thePropertyDropdown, activated, [&]() {SetModified(true);});
                        thePropertyDropdown->setFont(m_Font);
                        layout->addRow(theStaticText, thePropertyDropdown);

                        m_BuildProperties.push_back(std::make_pair(
                                                        &*theIter, std::make_pair(theStaticText, thePropertyDropdown)));

                        Q3DStudio::CString thePropertyValue =
                                CStudioPreferences::GetPreviewProperty(theIter->GetName());
                        for (theValueIter = theValues.begin(); theValueIter != theValues.end();
                             ++theValueIter) {
                            thePropertyDropdown->addItem(theValueIter->GetLabel().toQString());
                            thePropertyDropdown->setItemData(thePropertyDropdown->count() - 1, QVariant::fromValue(&*theValueIter));
                            if (theValueIter->GetName() == thePropertyValue)
                                thePropertyDropdown->setCurrentIndex(thePropertyDropdown->count() - 1);
                        }

                        // Select the first entry
                        if (thePropertyDropdown->currentIndex() == -1)
                            thePropertyDropdown->setCurrentIndex(0);
                    }
                }
            }
        }
    }
}

void CStudioAppPrefsPage::SavePreviewSettings()
{
    QString thePreviewApp = m_ui->m_PreviewSelector->currentText();
    CStudioPreferences::setPreviewConfig(Q3DStudio::CString::fromQString(thePreviewApp));

    std::list<TBuildNameControlPair>::iterator theIter;
    for (theIter = m_BuildProperties.begin(); theIter != m_BuildProperties.end(); ++theIter) {
        QComboBox *theCombo = theIter->second.second;
        Q3DStudio::CString theName = theIter->first->GetName();
        Q3DStudio::CBuildConfiguration::SConfigPropertyValue *thePropertyValue =
                theCombo->itemData(theCombo->currentIndex())
                .value<Q3DStudio::CBuildConfiguration::SConfigPropertyValue *>();
        CStudioPreferences::setPreviewProperty(theName, thePropertyValue->GetName());
    }
}

//==============================================================================
/**
 *	Remove all the dynamically added controls that was read in from the build file
 */
//==============================================================================
void CStudioAppPrefsPage::RemovePreviewPropertyControls()
{
    // Remove the created control
    std::list<TBuildNameControlPair>::iterator theIter;
    for (theIter = m_BuildProperties.begin(); theIter != m_BuildProperties.end(); ++theIter) {
        delete theIter->second.first;
        delete theIter->second.second;
    }
    m_BuildProperties.clear();
}
#endif