1/* GCompris - ActivityConfig.qml
2 *
3* SPDX-FileCopyrightText: 2020 Timothée Giet <animtim@gmail.com>
4 *
5 * Authors:
6 *   Timothée Giet <animtim@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 string locale: "system"
21    property alias normalModeConfig: normalModeConfig
22    property int savedMode: 11
23    property alias letterCaseBox: letterCaseBox
24    property int savedLetterCase: 0
25    width: if(background) background.width;
26    height: childrenRect.height
27    property alias availableLangs: langs.languages
28
29    LanguageList {
30        id: langs
31    }
32
33    Column {
34        id: column
35        spacing: 10
36        width: activityConfiguration.width
37
38        GCDialogCheckBox {
39            id: normalModeConfig
40            width: column.width - 50
41            text: qsTr("All the words")
42            checked: savedMode === 11 ? true : false
43            onCheckedChanged: easyModeConfig.checked = !normalModeConfig.checked;
44        }
45
46        GCDialogCheckBox {
47            id: easyModeConfig
48            width: column.width - 50
49            text: qsTr("Only 5 words")
50            checked: savedMode === 5 ? true : false
51            onCheckedChanged: normalModeConfig.checked = !easyModeConfig.checked;
52        }
53
54        Flow {
55            spacing: 5
56            width: activityConfiguration.width
57            GCComboBox {
58                id: letterCaseBox
59                label: qsTr("Select the case for the letters to search")
60                background: activityConfiguration.background
61                model: [
62                {"text": qsTr("Mixed Case"), "value": Font.MixedCase},
63                {"text": qsTr("Upper Case"), "value": Font.AllUppercase},
64                {"text": qsTr("Lower Case"), "value": Font.AllLowercase}
65                ]
66                currentText: model[savedLetterCase].text
67                currentIndex: savedLetterCase
68            }
69        }
70
71        Flow {
72            spacing: 5
73            width: activityConfiguration.width
74            GCComboBox {
75                id: localeBox
76                model: langs.languages
77                background: activityConfiguration.background
78                label: qsTr("Select your locale")
79            }
80        }
81    }
82
83    function setLocale(localeToSet) {
84        // Store the locale as-is to be displayed in menu
85        activityConfiguration.locale = Core.resolveLocale(localeToSet);
86    }
87
88    property var dataToSave
89    function setDefaultValues() {
90        var localeUtf8 = dataToSave.locale;
91        if(localeUtf8 !== "system") {
92            localeUtf8 += ".UTF-8";
93        }
94
95        if(dataToSave.locale) {
96            setLocale(localeUtf8);
97        } else {
98            activityConfiguration.localeBox.currentIndex = 0;
99            setLocale(activityConfiguration.availableLangs[0].locale);
100        }
101
102        if(dataToSave.savedMode) {
103            activityConfiguration.savedMode = dataToSave.savedMode;
104        } else {
105            activityConfiguration.savedMode = 11;
106        }
107
108        if(dataToSave.savedLetterCase) {
109            activityConfiguration.savedLetterCase = dataToSave.savedLetterCase;
110        } else {
111            activityConfiguration.savedLetterCase = 0;
112        }
113
114        for(var i = 0 ; i < activityConfiguration.availableLangs.length ; i ++) {
115            if(activityConfiguration.availableLangs[i].locale === localeUtf8) {
116                activityConfiguration.localeBox.currentIndex = i;
117                break;
118            }
119        }
120    }
121
122    function saveValues() {
123        var newLocale =
124        activityConfiguration.availableLangs[activityConfiguration.localeBox.currentIndex].locale;
125        // Remove .UTF-8
126        if(newLocale.indexOf('.') != -1) {
127            newLocale = newLocale.substring(0, newLocale.indexOf('.'));
128        }
129
130        activityConfiguration.savedMode = activityConfiguration.normalModeConfig.checked ? 11 : 5;
131        activityConfiguration.savedLetterCase = activityConfiguration.letterCaseBox.currentIndex;
132
133        setLocale(newLocale);
134
135        dataToSave = {"locale": newLocale,
136                      "savedMode": activityConfiguration.savedMode,
137                      "savedLetterCase": activityConfiguration.savedLetterCase};
138    }
139}
140