main.qml 11.3 KB
Newer Older
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
/****************************************************************************
**
** Copyright (C) 2017 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the Qt 3D Studio Demos.
**
** $QT_BEGIN_LICENSE:BSD$
** 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.
**
** BSD License Usage
** Alternatively, you may use this file under the terms of the BSD license
** as follows:
**
** "Redistribution and use in source and binary forms, with or without
** modification, are permitted provided that the following conditions are
** met:
**   * Redistributions of source code must retain the above copyright
**     notice, this list of conditions and the following disclaimer.
**   * Redistributions in binary form must reproduce the above copyright
**     notice, this list of conditions and the following disclaimer in
**     the documentation and/or other materials provided with the
**     distribution.
**   * Neither the name of The Qt Company Ltd nor the names of its
**     contributors may be used to endorse or promote products derived
**     from this software without specific prior written permission.
**
**
** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
**
** $QT_END_LICENSE$
**
****************************************************************************/

51
import QtQuick 2.9
52
import QtStudio3D 1.1
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
import QtQuick.Controls 2.1

Item {
    id: mainview

    focus: true

    property bool startupAnimation: true
    property bool allowSelection: true

    property int contentIndex: 1
    property int previousContentIndex: 0
    property int maxContentIndex: 5

    property int selectedMenuIndex: 0
    property int maxMenuIndex: 5

70 71 72 73 74 75 76 77 78 79 80 81 82
    property bool useMetric: true
    property bool redTheme: true

    property string esc: "On"
    property string trafficSigns: "Recognition"
    property string autoHighBeam: "On"
    property string hdLampDelay: "20 seconds"
    property string auxHeater: "On"
    property string parkHeater: "Time 2"
    property string chimesPark: "Off"
    property string chimesInfo: "Off"
    property string chimesWarning: "On"

83 84 85
    Studio3D {
        id: studio3D
        width: parent.width
86
        height: parent.height
87 88 89
        anchors.verticalCenter: parent.verticalCenter

        ViewerSettings {
90
            scaleMode: ViewerSettings.ScaleModeFit
91 92 93 94 95 96 97 98 99 100 101
        }

        Presentation {
            id: cluster
            source: "qrc:/presentation/mainPresentation.uia"

            onSlideEntered: {
                if (name === "Base")
                    startupAnimation = false;
            }

102
            DataInput {
103
                id: mainScene
104
                name: "mainSlide"
105 106
            }

107
            DataInput {
108
                id: carScene
109
                name: "carSlide"
110 111
            }

112 113 114
            DataInput {
                id: tachometerNeedle
                name: "tacho"
115 116
            }

117 118 119
            DataInput {
                id: tachometerText
                name: "tachoText"
120 121
            }

122
            DataInput {
123
                id: speedometerNeedle
124
                name: "speed"
125 126
            }

127 128 129
            DataInput {
                id: speedometerText
                name: "speedText"
130 131
            }

132 133 134
            DataInput {
                id: displayColor
                name: "colorSlide"
135 136
            }

137
            DataInput {
138
                id: speedometerUnit
139
                name: "speedUnitSlide"
140 141
            }

142 143 144
            Element {
                id: carBodyMaterial
                elementPath: "contentPresentation:Scene.Layer.Car.Car.body.lambert2"
145 146
            }

147 148 149 150 151 152 153 154 155 156 157 158 159
            SubPresentationSettings {
                qmlStreams: [
                    QmlStream {
                        presentationId: "navigationStream"
                        Loader {
                            id: contentStream
                            active: false
                            source: ""
                        }
                    }
                ]
            }
        }
160

161 162 163 164 165
        onPresentationReady: {
            indicatorPane.visible = true;
            content.visible = true;
        }

166 167
        // Hack. We need to draw something in QML in each frame to minimize the jamming of
        // Studio3D content.
168
        property bool direction: false
169
        onFrameUpdate: {
170 171
            direction = !direction;
            if (direction)
172
                studio3D.x += 0.1;
173
            else
174
                studio3D.x -= 0.1;
175
        }
176 177 178 179 180 181
    }

    OverlayContainer {
        id: indicatorPane
        scale: mainview.width / 2560
        anchors.centerIn: mainview
182
        visible: false
183 184 185 186 187 188 189
    }

    Loader {
        id: content
        scale: mainview.width / 2560
        source: "qrc:/SettingsContainer.qml"
        anchors.centerIn: parent
190
        visible: false
191 192
    }

193 194 195 196 197
    onRedThemeChanged: {
        if (redTheme) {
            carBodyMaterial.setAttribute("diffuse.r", 0.855);
            carBodyMaterial.setAttribute("diffuse.g", 0.14);
            carBodyMaterial.setAttribute("diffuse.b", 0.144);
198
            displayColor.setValue("red");
199 200 201 202
        } else {
            carBodyMaterial.setAttribute("diffuse.r", 0.08);
            carBodyMaterial.setAttribute("diffuse.g", 0.08);
            carBodyMaterial.setAttribute("diffuse.b", 1.0);
203
            displayColor.setValue("blue");
204 205 206
        }
    }

207 208
    onUseMetricChanged: {
        if (useMetric)
209
            speedometerUnit.setValue("kph");
210
        else
211
            speedometerUnit.setValue("mph");
212 213
    }

214
    Keys.onRightPressed: {
215
        userActivity();
216 217 218 219
        menuSelect(true);
    }

    Keys.onLeftPressed: {
220
        userActivity();
221 222 223 224
        menuSelect(false);
    }

    Keys.onUpPressed: {
225
        userActivity();
226 227 228 229 230 231
        selectedMenuIndex--;
        if (selectedMenuIndex < 0)
            selectedMenuIndex = 0;
    }

    Keys.onDownPressed: {
232
        userActivity();
233 234 235 236 237 238 239 240 241 242 243 244 245 246 247
        selectedMenuIndex++;
        if (selectedMenuIndex > maxMenuIndex)
            selectedMenuIndex = maxMenuIndex;
    }

    // Simulated speed and rpm data
    SimuData {
        id: simuData

        onTimeChanged: {
            indicatorPane.time.text = time;
        }

        // Connect speed
        onSpeedChanged: {
248 249 250 251
            speedometerNeedle.setValue(speed);
            if (useMetric)
                speedometerText.setValue(speed.toFixed(0));
            else
252
                speedometerText.setValue((speed / 1.625).toFixed(0));
253 254
            tachometerNeedle.setValue(rpm);
            tachometerText.setValue((rpm / 1000.0).toFixed(1));
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
        }
    }

    function menuSelect(next) {
        if (!allowSelection)
            return;

        previousContentIndex = contentIndex;

        // settings - navigation - music - call - car
        if (next)
            contentIndex++;
        else
            contentIndex--;

        if (contentIndex > maxContentIndex)
            contentIndex = 1;
        else if (contentIndex < 1)
            contentIndex = maxContentIndex;

        selectContent(contentIndex);
    }

    function selectContent(index) {
        if (previousContentIndex === index)
            return;

        selectedMenuIndex = 0;
        indicatorPane.select(index);
        contentStream.source = "";
        contentStream.active = false;

        if (previousContentIndex === 2) { // from Navigation
            allowSelection = false;
            selectionTimer.start();
290
            mainScene.setValue("ToBase");
291 292 293
        } else if (previousContentIndex === 5) { // from Car
            allowSelection = false;
            selectionTimer.start();
294
            carScene.setValue("FadeOut");
295 296 297 298 299 300 301 302 303 304
        }

        switch (index) {
        case 1: // Settings
            content.source = "qrc:/SettingsContainer.qml";
            break;
        case 2: // Navigation
            allowSelection = false;
            selectionTimer.start();
            content.source = "";
305
            mainScene.setValue("ToNavigation");
306 307 308 309 310 311 312 313 314 315 316 317 318
            contentStream.source = "qrc:/Navigation.qml";
            contentStream.active = true;
            break;
        case 3: // Music
            content.source = "qrc:/MusicContainer.qml";
            break;
        case 4: // Call
            content.source = "qrc:/ContactsContainer.qml";
            break;
        case 5: // Car
            allowSelection = false;
            selectionTimer.start();
            content.source = "";
319
            carScene.setValue("FadeIn");
320 321 322 323
            break;
        }
    }

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
    // Trigger this to stop automatic demoing mode and/or
    // keep it from not starting.
    function userActivity() {
        automaticDemoDelay.restart();
        if (automaticDemoingTimer.running)
            automaticDemoingTimer.stop();
    }

    Timer {
        id: automaticDemoDelay
        // Non-interactivity delay
        interval: 60000
        repeat: false
        running: true
        onTriggered: {
            automaticDemoingTimer.start();
        }
    }

    Timer {
        id: automaticDemoingTimer
        interval: 8000
        repeat: true
        running: false
        triggeredOnStart: true
        onTriggered: {
            // Switch mode automatically when triggered
            mainview.menuSelect(true);
        }
    }

355 356 357 358 359
    Timer {
        id: selectionTimer
        interval: 1000
        onTriggered: allowSelection = true;
    }
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

    Image {
        id: automaticIcon
        readonly property bool show: automaticDemoingTimer.running
        property real iconOpacity: 0
        anchors.top: parent.top
        anchors.topMargin: 16
        anchors.left: parent.left
        anchors.leftMargin: 16
        source: "qrc:/icons/automatic.png"
        width: 48
        height: 24
        opacity: show ? iconOpacity : 0
        SequentialAnimation on iconOpacity {
            running: automaticIcon.show
            loops: Animation.Infinite
            alwaysRunToEnd: true
            NumberAnimation {
                to: 0.2
                duration: 1000
                easing.type: Easing.InOutQuad
            }
            NumberAnimation {
                to: 0
                duration: 1000
                easing.type: Easing.InOutQuad
            }
        }
    }

    MouseArea {
        anchors.fill: parent
        z: 100
        onPressed: {
            // Record user activity and pass event further
            userActivity();
            mouse.accepted = false;
        }
    }
399
}