1/*
2 *  SPDX-FileCopyrightText: 2012 Sebastian Gottfried <sebastiangottfried@web.de>
3 *  SPDX-FileCopyrightText: 2015 Sebastian Gottfried <sebastiangottfried@web.de>
4 *
5 *  SPDX-License-Identifier: GPL-2.0-or-later
6 */
7
8import QtQuick 2.9
9import QtQuick.Layouts 1.3
10import QtQuick.Controls 2.2 as Controls
11import ktouch 1.0
12
13import "../common"
14
15FocusScope {
16    id: root
17
18    property Profile profile
19    property string currentKeyboardLayoutName
20    property string selectedKeyboardLayoutName
21    property DataIndexKeyboardLayout selectedKeyboardLayout
22    property DataIndexCourse selectedCourse
23
24    signal lessonSelected(variant course, variant lesson)
25    signal courseSelectec(Course course)
26
27    function selectLastUsedCourse() {
28        if (!profile) {
29            return
30        }
31
32        var courseId = profile.lastUsedCourseId;
33
34        // fist try to to select the course the user has used last
35        for (var i = 0; i < allCoursesModel.rowCount(); i++) {
36            var dataIndexCourse = allCoursesModel.data(allCoursesModel.index(i, 0), ResourceModel.DataRole);
37            if (dataIndexCourse.id === courseId) {
38                root.selectedCourse = dataIndexCourse
39                return
40            }
41        }
42
43        // if this fails try to select course matching the current keyboard layout
44        if (coursesForCurrentKeyboardLayoutModel.rowCount() > 0) {
45            var course = coursesForCurrentKeyboardLayoutModel.data(coursesForCurrentKeyboardLayoutModel.index(0, 0), ResourceModel.DataRole);
46            root.selectedCourse = course
47            return;
48        }
49
50        // finally just select the first course
51        if (allCoursesModel.rowCount() > 0) {
52            root.selectedCourse = allCoursesModel.data(allCoursesModel.index(0, 0), ResourceModel.DataRole);
53        }
54    }
55
56    onSelectedCourseChanged: {
57        root.selectedKeyboardLayoutName = root.selectedCourse.keyboardLayoutName;
58
59        for (var i = 0; i < ktouch.globalDataIndex.keyboardLayoutCount; i++)
60        {
61            var dataIndexLayout = ktouch.globalDataIndex.keyboardLayout(i)
62
63            if (dataIndexLayout.name === root.selectedKeyboardLayoutName) {
64                root.selectedKeyboardLayout = dataIndexLayout;
65                return
66            }
67        }
68
69        root.selectedKeyboardLayout = null;
70    }
71
72    function saveLastUsedCourse(course) {
73        if (profile.lastUsedCourseId != course.id) {
74            profile.lastUsedCourseId = course.id;
75            profileDataAccess.updateProfile(profileDataAccess.indexOfProfile(profile));
76        }
77    }
78
79    onProfileChanged: selectLastUsedCourse()
80
81    ResourceModel {
82        id: allResourcesModel
83        dataIndex: ktouch.globalDataIndex
84        onRowsRemoved: {
85            selectLastUsedCourse()
86
87        }
88        onRowsInserted: {
89            selectLastUsedCourse()
90        }
91    }
92
93    CategorizedResourceSortFilterProxyModel {
94        id: allCoursesModel
95        resourceModel: allResourcesModel
96        resourceTypeFilter: ResourceModel.CourseItem
97    }
98
99
100    CategorizedResourceSortFilterProxyModel {
101        id: coursesForCurrentKeyboardLayoutModel
102        resourceModel: allResourcesModel
103        resourceTypeFilter: ResourceModel.CourseItem
104        keyboardLayoutNameFilter: root.currentKeyboardLayoutName
105    }
106
107    CategorizedResourceSortFilterProxyModel {
108        id: currentKeyboardLayoutsModel
109        resourceModel: allResourcesModel
110        resourceTypeFilter: ResourceModel.KeyboardLayoutItem
111        keyboardLayoutNameFilter: root.currentKeyboardLayoutName
112    }
113
114    CategorizedResourceSortFilterProxyModel {
115        id: otherKeyboardLayoutsModel
116        resourceModel: allResourcesModel
117        resourceTypeFilter: ResourceModel.KeyboardLayoutItem
118        keyboardLayoutNameFilter: root.currentKeyboardLayoutName
119        invertedKeyboardLayoutNameFilter: true
120    }
121
122    KColorScheme {
123        id: courseSelectorColorScheme
124        colorGroup: KColorScheme.Active
125        colorSet: KColorScheme.View
126    }
127
128    Rectangle {
129        id: bg
130        anchors.fill: parent
131        color: courseSelectorColorScheme.normalBackground
132    }
133
134    Flickable {
135        clip: true
136        anchors.fill: parent
137        contentWidth: width
138        contentHeight: content.height
139        Column {
140            id: content
141            width: parent.width
142
143            Loader {
144                width: parent.width
145                active: root.currentKeyboardLayoutName != 'unknown'
146                sourceComponent: CourseSelectorKeyboardLayoutList {
147                    width: parent.width
148                    title: i18n("Courses For Your Keyboard Layout")
149                    model: currentKeyboardLayoutsModel
150                    resourceModel: allResourcesModel
151                    colorScheme: courseSelectorColorScheme
152                    selectedKeyboardLayoutName: root.selectedKeyboardLayoutName
153                    selectedCourse: root.selectedCourse
154                    onCourseSelected: {
155                        root.selectedCourse = course
156                        root.saveLastUsedCourse(course)
157                    }
158                }
159            }
160
161            CourseSelectorKeyboardLayoutList {
162                width: parent.width
163                title: root.currentKeyboardLayoutName == 'unknown'? i18n("Courses"): i18n("Other Courses")
164                model: otherKeyboardLayoutsModel
165                resourceModel: allResourcesModel
166                colorScheme: courseSelectorColorScheme
167                selectedKeyboardLayoutName: root.selectedKeyboardLayoutName
168                selectedCourse: root.selectedCourse
169                onCourseSelected: {
170                    root.selectedCourse = course
171                    root.saveLastUsedCourse(course)
172                }
173            }
174        }
175
176        Controls.ScrollBar.vertical: ScrollBar { }
177    }
178
179
180
181
182
183
184}
185