1/* GCompris - ActivityConfig.qml
2 *
3 * SPDX-FileCopyrightText: 2020 Johnny Jazeix <jazeix@gmail.com>
4 *
5 * Authors:
6 *   Johnny Jazeix <jazeix@gmail.com>
7 *
8 *   SPDX-License-Identifier: GPL-3.0-or-later
9 */
10import QtQuick 2.9
11import GCompris 1.0
12
13import "../../core"
14import "qrc:/gcompris/src/core/core.js" as Core
15
16Item {
17    id: activityConfiguration
18    property Item background
19    property alias localeBox: localeBox
20    property alias speedSlider: speedSlider
21    property int speedSetting: 10
22    property string locale: "system"
23    property string configurationLocale: "system"
24    width: if(background) background.width
25    height: childrenRect.height
26    property alias availableLangs: langs.languages
27    LanguageList {
28        id: langs
29    }
30
31    Column {
32        spacing: 10 * ApplicationInfo.ratio
33        width: activityConfiguration.width
34        GCComboBox {
35            id: localeBox
36            model: langs.languages
37            background: activityConfiguration.background
38            label: qsTr("Select your locale")
39        }
40        GCText {
41            id: speedSliderText
42            text: qsTr("Speed")
43            fontSize: mediumSize
44            wrapMode: Text.WordWrap
45        }
46        GCSlider {
47            id: speedSlider
48            width: 250 * ApplicationInfo.ratio
49            value: speedSetting
50            maximumValue: 10
51            minimumValue: 1
52            scrollEnabled: false
53        }
54    }
55
56    function setLocale(localeToSet) {
57        // Store the locale as-is to be displayed in menu
58        configurationLocale = localeToSet
59        activityConfiguration.locale = Core.resolveLocale(localeToSet)
60    }
61
62    property var dataToSave
63    function setDefaultValues() {
64        // Recreate the binding
65        speedSlider.value = Qt.binding(function() {return activityConfiguration.speedSetting;})
66
67        var localeUtf8 = dataToSave.locale;
68        if(localeUtf8 !== "system") {
69            localeUtf8 += ".UTF-8";
70        }
71
72        if(dataToSave.locale) {
73            setLocale(localeUtf8)
74        }
75        else {
76            activityConfiguration.localeBox.currentIndex = 0
77            setLocale(activityConfiguration.availableLangs[0].locale)
78        }
79
80        if(dataToSave.speedSetting) {
81            activityConfiguration.speedSetting = dataToSave.speedSetting
82        }
83        else {
84            activityConfiguration.speedSetting = 10
85        }
86
87        for(var i = 0 ; i < activityConfiguration.availableLangs.length ; i ++) {
88            if(activityConfiguration.availableLangs[i].locale === localeUtf8) {
89                activityConfiguration.localeBox.currentIndex = i;
90                break;
91            }
92        }
93    }
94
95    function saveValues() {
96        var newLocale = activityConfiguration.availableLangs[activityConfiguration.localeBox.currentIndex].locale;
97        // Remove .UTF-8
98        if(newLocale.indexOf('.') != -1) {
99            newLocale = newLocale.substring(0, newLocale.indexOf('.'))
100        }
101
102        speedSetting = speedSlider.value
103
104        setLocale(newLocale);
105        dataToSave = {"locale": newLocale, "speedSetting": speedSetting, "activityLocale": activityConfiguration.locale}
106    }
107}
108