HomeView.qml 13.5 KB
Newer Older
Titta Heikkala's avatar
Titta Heikkala committed
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
/****************************************************************************
**
** 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$
**
****************************************************************************/

import QtQuick 2.8
import QtQuick.Controls 2.1
Laszlo Agocs's avatar
Laszlo Agocs committed
53
import QtStudio3D 2.0
Titta Heikkala's avatar
Titta Heikkala committed
54 55 56 57 58 59 60 61 62

Item {
    id: home

    property real red: 0
    property real blue: 255
    property real green: 0
    property real light: 0

63 64
    property var currentTempTarget
    property var currentLightTarget
Titta Heikkala's avatar
Titta Heikkala committed
65 66 67 68 69 70 71

    Timer {
        id: goToHomeTimer
        interval: 10000
        running: false
        repeat: false

72
        onTriggered: slideInput.setValue("BackToInitialView");
Titta Heikkala's avatar
Titta Heikkala committed
73 74 75 76
    }

    Timer {
        id: showMenuTimer
77
        interval: 750
Titta Heikkala's avatar
Titta Heikkala committed
78 79 80
        running: false
        repeat: false

81
        onTriggered: mainMenu.show = true;
Titta Heikkala's avatar
Titta Heikkala committed
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
    }

    signal studio3DPresentationReady()

    //
    // Qt 3D Studio presentation
    //
    Studio3D {
        anchors.fill: parent

        ViewerSettings {
            scaleMode: ViewerSettings.ScaleModeFill
        }

        Presentation {
            id: presentation
98
            source: "qrc:/uip/houseStudio.uia"
Titta Heikkala's avatar
Titta Heikkala committed
99

100
            function switchToRoom(slideName) {
101 102 103 104 105
                livingRoomLight.setValue(slideName === "LivingRoom" ? 100 : 0);
                masterBedRoomLight.setValue(slideName === "MasterBedRoom" ? 100 : 0);
                bedRoomLight.setValue(slideName === "BedRoom" ? 100 : 0);
                bathRoomLight.setValue(slideName === "BathRoom" ? 100 : 0);
                entranceLight.setValue(slideName === "Entrance" ? 100 : 0);
106 107
            }

108 109 110 111 112 113 114
            // This DataInput item is used to control current slide. The presentation has
            // a corresponding data input of "String" type linked to main Scene slides.
            DataInput {
                id: slideInput
                name: "slideInput"
            }

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
            DataInput {
                id: entranceLight
                name: "lightEntrance"
            }

            DataInput {
                id: bathRoomLight
                name: "lightBathRoom"
            }

            DataInput {
                id: livingRoomLight
                name: "lightLivingRoom"
            }

            DataInput {
                id: masterBedRoomLight
                name: "lightMasterBedRoom"
            }

            DataInput {
                id: bedRoomLight
                name: "lightBedRoom"
            }

            DataInput {
                id: entranceHeat
                name: "heatEntrance"
            }

            DataInput {
                id: bathRoomHeat
                name: "heatBathRoom"
            }

            DataInput {
                id: livingRoomHeat
                name: "heatLivingRoom"
            }

            DataInput {
                id: masterBedRoomHeat
                name: "heatMasterBedRoom"
            }

            DataInput {
                id: bedRoomHeat
                name: "heatBedRoom"
            }

Titta Heikkala's avatar
Titta Heikkala committed
165 166 167 168 169
            SceneElement {
                id: scene
                elementPath: "Scene"
                onCurrentSlideNameChanged: {
                    var startTimer = false;
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185
                    if (currentSlideName === "LivingRoom") {
                        currentTempTarget = livingRoomHeat;
                        currentLightTarget = livingRoomLight;
                        sliderToolbar.show = true;
                    } else if (currentSlideName === "MasterBedRoom") {
                        currentTempTarget = masterBedRoomHeat;
                        currentLightTarget = masterBedRoomLight;
                        sliderToolbar.show = true;
                    } else if (currentSlideName === "BedRoom") {
                        currentTempTarget = bedRoomHeat;
                        currentLightTarget = bedRoomLight;
                        sliderToolbar.show = true;
                    } else if (currentSlideName === "BathRoom") {
                        currentTempTarget = bathRoomHeat;
                        currentLightTarget = bathRoomLight;
                        sliderToolbar.show = true;
Titta Heikkala's avatar
Titta Heikkala committed
186
                    } else if (currentSlideName === "Entrance") {
187 188 189
                        currentTempTarget = entranceHeat;
                        currentLightTarget = entranceLight;
                        sliderToolbar.show = true;
Titta Heikkala's avatar
Titta Heikkala committed
190 191
                    } else if (currentSlideName === "FloorPlan") {
                        startTimer = true;
192
                        sliderToolbar.show = false;
Titta Heikkala's avatar
Titta Heikkala committed
193
                    } else {
194
                        sliderToolbar.show = false;
Titta Heikkala's avatar
Titta Heikkala committed
195 196
                    }

197 198
                    presentation.switchToRoom(currentSlideName);

Titta Heikkala's avatar
Titta Heikkala committed
199 200
                    if (currentSlideName === "FloorPlan") {
                        if (previousSlideName === "InitialView")
201
                            mainMenu.show = true;
Titta Heikkala's avatar
Titta Heikkala committed
202
                        else
203
                            showMenuTimer.start();
Titta Heikkala's avatar
Titta Heikkala committed
204 205
                    } else {
                        if (showMenuTimer.running)
206 207
                            showMenuTimer.stop();
                        mainMenu.show = false;
Titta Heikkala's avatar
Titta Heikkala committed
208 209 210
                    }

                    if (startTimer)
211
                        goToHomeTimer.start();
Titta Heikkala's avatar
Titta Heikkala committed
212
                    else if (goToHomeTimer.running)
213
                        goToHomeTimer.stop();
Titta Heikkala's avatar
Titta Heikkala committed
214

215 216 217 218
                    if (currentTempTarget) {
                        currentTempTarget.setValue(Qt.vector3d(red, green, blue));
                        currentLightTarget.setValue(light);
                    }
Titta Heikkala's avatar
Titta Heikkala committed
219 220 221 222
                }
            }
        }

Tomi Korpipää's avatar
Tomi Korpipää committed
223
        onPresentationReady: {
224
            home.studio3DPresentationReady();
Tomi Korpipää's avatar
Tomi Korpipää committed
225
        }
Titta Heikkala's avatar
Titta Heikkala committed
226 227
    }

Titta Heikkala's avatar
Titta Heikkala committed
228 229 230 231
    Connections {
        target: knxBackend
        onBoardUpdate: {
            if (lightNum > 0 && lightNum < 5) {
232 233 234
                var oldValue = houseModel.data(houseModel.index(lightNum - 1, 0), 3);
                houseModel.setData(houseModel.index(lightNum - 1, 0), !oldValue, 3);
                lightControlLoader.item.value = houseModel.data(houseModel.index(index, 0), 3);
Titta Heikkala's avatar
Titta Heikkala committed
235 236 237 238
            }
        }
    }

Titta Heikkala's avatar
Titta Heikkala committed
239 240 241 242
    MouseArea {
        id: mouseArea
        anchors.fill: parent
        onClicked: {
243
            if (scene.currentSlideName === "BackToInitialView") {
244 245
                slideInput.setValue("InitialView");
                disableMouse(2200);
246
            } else if (scene.currentSlideName === "FloorPlan") {
247 248
                slideInput.setValue("BackToInitialView");
                disableMouse(2100);
249
            } else if (scene.currentSlideName !== "InitialView") {
250 251
                slideInput.setValue("FloorPlan");
                disableMouse(1000);
Titta Heikkala's avatar
Titta Heikkala committed
252 253 254 255
            }
        }

        function disableMouse(animationDuration) {
256 257 258
            mouseArea.enabled = false;
            mouseTimer.interval = animationDuration;
            mouseTimer.start();
Titta Heikkala's avatar
Titta Heikkala committed
259 260 261 262 263 264 265 266
        }

        Timer {
            id: mouseTimer
            interval: 1000
            running: false
            repeat: false

267
            onTriggered: mouseArea.enabled = true;
Titta Heikkala's avatar
Titta Heikkala committed
268 269 270 271 272 273 274 275 276
        }
    }

    //
    // Room label items
    //
    MainMenu {
        id: mainMenu

277
        property bool show: false
Titta Heikkala's avatar
Titta Heikkala committed
278 279
        property int currentIndex: -1

280 281 282 283 284 285 286 287
        visible: opacity > 0
        opacity: show
        Behavior on opacity {
            NumberAnimation {
                duration: 250
                easing.type: Easing.InOutQuad
            }
        }
Titta Heikkala's avatar
Titta Heikkala committed
288 289 290
        anchors.fill: parent

        onCurrentMenuItemChanged: {
291
            slideInput.setValue(currentItem)
Titta Heikkala's avatar
Titta Heikkala committed
292 293 294

            // Store temperature and light to model before changing the slider value
            if (currentIndex != -1) {
295 296 297
                houseModel.setData(houseModel.index(currentIndex, 0), tempSlider.currentValue, 2);
                houseModel.setData(houseModel.index(currentIndex, 0),
                                   lightControlLoader.item.currentValue, 3);
Titta Heikkala's avatar
Titta Heikkala committed
298
            }
299 300 301 302
            currentIndex = index;
            var temp = houseModel.data(houseModel.index(index, 0), 2);
            tempSlider.value = (temp - 15) / 15;
            lightControlLoader.item.value = houseModel.data(houseModel.index(index, 0), 3);
Titta Heikkala's avatar
Titta Heikkala committed
303 304 305 306 307 308 309 310
        }
    }

    //
    // Toolbar for Sliders
    //
    Item {
        id: sliderToolbar
311
        property bool show: false
Titta Heikkala's avatar
Titta Heikkala committed
312
        anchors.bottom: parent.bottom
Titta Heikkala's avatar
Titta Heikkala committed
313 314 315
        height: tempSlider.height
        anchors.horizontalCenter: parent.horizontalCenter
        width: lightControlLoader.width + sizesMap.controlMargin + tempSlider.width
Titta Heikkala's avatar
Titta Heikkala committed
316

317 318 319 320 321 322 323 324
        visible: opacity > 0
        opacity: show
        Behavior on opacity {
            NumberAnimation {
                duration: 250
                easing.type: Easing.InOutQuad
            }
        }
Titta Heikkala's avatar
Titta Heikkala committed
325

Titta Heikkala's avatar
Titta Heikkala committed
326 327 328 329
        Loader {
            id: lightControlLoader
            source: knxBackend ? "LightSwitch.qml" : "LightSlider.qml"
            asynchronous: true
Titta Heikkala's avatar
Titta Heikkala committed
330 331
            anchors.bottom: parent.bottom
            anchors.bottomMargin: sizesMap.controlMargin
Titta Heikkala's avatar
Titta Heikkala committed
332 333 334
            anchors.left: parent.left
            width: knxBackend ? (sizesMap.controlHeight * 2) : (mainMenu.width / 3)
        }
Titta Heikkala's avatar
Titta Heikkala committed
335

Titta Heikkala's avatar
Titta Heikkala committed
336 337
        Connections {
            target: lightControlLoader.item
Titta Heikkala's avatar
Titta Heikkala committed
338
            onLightValueChanged: {
339 340 341 342
                var newLightValue = lightControlLoader.item.lightValue * 100;
                home.light = newLightValue;
                if (currentLightTarget)
                    currentLightTarget.setValue(newLightValue);
Titta Heikkala's avatar
Titta Heikkala committed
343
                if (knxBackend) {
344 345 346 347 348 349 350 351 352 353
                    var lightNum = 0;
                    if (scene.currentSlideName === "LivingRoom")
                        lightNum = 1;
                    else if (scene.currentSlideName === "MasterBedRoom")
                        lightNum = 2;
                    else if (scene.currentSlideName === "BedRoom")
                        lightNum = 3;
                    else if (scene.currentSlideName === "BathRoom")
                        lightNum = 4;
                    knxBackend.toggleLight(lightNum);
Titta Heikkala's avatar
Titta Heikkala committed
354
                }
Titta Heikkala's avatar
Titta Heikkala committed
355 356 357 358 359 360 361
            }
        }

        TempSlider {
            id: tempSlider
            anchors.bottom: parent.bottom
            anchors.bottomMargin: sizesMap.controlMargin
Titta Heikkala's avatar
Titta Heikkala committed
362 363
            anchors.right: parent.right
            width: mainMenu.width / 3
Titta Heikkala's avatar
Titta Heikkala committed
364 365

            onRedColorChanged: {
366 367
                home.red = redColor;
                currentTempTarget.setValue(Qt.vector3d(redColor, home.green, home.blue));
Titta Heikkala's avatar
Titta Heikkala committed
368 369
            }
            onBlueColorChanged: {
370 371
                home.blue = blueColor;
                currentTempTarget.setValue(Qt.vector3d(home.red, home.green, blueColor));
Titta Heikkala's avatar
Titta Heikkala committed
372 373
            }
            onGreenColorChanged: {
374 375
                home.green = greenColor;
                currentTempTarget.setValue(Qt.vector3d(home.red, greenColor, home.blue));
Titta Heikkala's avatar
Titta Heikkala committed
376
            }
377 378 379 380

            onEconomyChanged: {
                if (knxBackend) {
                    // Toggle red light based on temperature changes
381 382 383 384 385 386 387 388 389 390 391 392
                    // The initial state of red lights on the board needs to be based on the default
                    // temperatures
                    var lightNum = 0;
                    if (scene.currentSlideName === "LivingRoom")
                        lightNum = 9;
                    else if (scene.currentSlideName === "MasterBedRoom")
                        lightNum = 10;
                    else if (scene.currentSlideName === "BedRoom")
                        lightNum = 11;
                    else if (scene.currentSlideName === "BathRoom")
                        lightNum = 12;
                    knxBackend.toggleLight(lightNum);
393 394
                }
            }
Titta Heikkala's avatar
Titta Heikkala committed
395 396 397
        }
    }
}