~boghison/lang/trunk

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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
import QtQuick 2.0
import Ubuntu.Components 1.1
import U1db 1.0 as U1db
import Ubuntu.Components.Popups 1.0
import Ubuntu.Components.ListItems 1.0 as ListItem

MainView {
    applicationName: "com.ubuntu.developer.boghison.lang"
    width: units.gu(100)
    height: units.gu(75)
    useDeprecatedToolbar: false
    automaticOrientation: true

    function handleShape(shapeColor, shapeTxt){
        translationModel.clear();
        definitionModel.clear();
        var vocabularies = vocabDB.contents["vocabularies"];
        var tempContents = {};
        for (var i = 0; i < vocabularies.length; i++){
            if(vocabularies[i]["color"] == shapeColor && vocabularies[i]["name"] == shapeTxt) tempContents = vocabularies[i]
        }
        var definitions = tempContents["definitions"];
        if (definitions.length != 0){
            for (var i = 0; i < definitions.length; i++){
                definitionModel.append({"word": definitions[i]["word"], "definition": definitions[i]["definition"]});
            }
        }
        var translations = tempContents["translations"]
        if (translations.length != 0){
            for (var i = 0; i < translations.length; i++){
                translationModel.append({"from": translations[i]["from"], "to": translations[i]["to"]});
            }
        }
        mainStack.push(mainPage.createObject(mainStack, {"title": shapeTxt, "contentColor": shapeColor}));
    }

    ListModel {
        id: translationModel
    }

    ListModel {
        id: definitionModel
    }

    Item {
        U1db.Database {
            id: vocabDBDB
            path: "~/.local/share/com.ubuntu.developer.boghison.lang/vocabDB"
        }
        U1db.Document {
            docId: "vocabDB"
            id: vocabDB
            database: vocabDBDB
            create: true
            defaults: {
                "vocabularies": []
            }
        }
    }

    PageStack {
        id: mainStack
        Component.onCompleted: push(vocabPage)

        Component {
            id: vocabPage

            CollectionsPage {}
        }

        Component {
            id: addVocab

            AddCollectionDialog {}
        }
        Component {
            id: mainPage

            Page {
                id: propertyPage
                visible: false
                title: mainPage.title
                property string contentColor: mainPage.contentColor
                head.actions: [
                    Action{
                        text: i18n.tr("Add")
                        iconName: "add"
                        onTriggered: PopupUtils.open(addWord)
                    },
                    Action {
                        text: i18n.tr("Delete")
                        iconName: "delete"
                        onTriggered: PopupUtils.open(deleteVocab)
                    }

                ]
                head {
                    sections {
                        model: [i18n.tr("Definitions"), i18n.tr("Translations")]
                    }
                }

                ListView {
                    id: mainListView
                    interactive: contentHeight > height
                    width: parent.width
                    height: parent.height
                    model: (propertyPage.head.sections.selectedIndex == 0) ? definitionModel : translationModel
                    delegate: ListItem.Subtitled{
                        text: (propertyPage.head.sections.selectedIndex == 0) ? word : from
                        subText: (propertyPage.head.sections.selectedIndex == 0) ? definition : to

                        MouseArea {
                            anchors.fill: parent
                            onClicked: mainStack.push(Qt.resolvedUrl("wordPage.qml"), {"word":(propertyPage.head.sections.selectedIndex == 0) ? word : from, "definition": (propertyPage.head.sections.selectedIndex == 0) ? definition : to, "title": (propertyPage.head.sections.selectedIndex == 0) ? "Definition" : "Translation", "name": propertyPage.title, "color": propertyPage.contentColor});
                        }
                    }
                }
                Component {
                    id: deleteVocab

                    DeleteCollectionDialog {
                        name: propertyPage.title
                    }
                }

                Component {
                    id: addWord

                    Dialog {
                        id: addWordDialog
                        title: i18n.tr("Add a new " + ((propertyPage.head.sections.selectedIndex == 0) ? "definition" : "translation"))

                        Item {
                            width: parent.width
                            height: units.gu(40)

                            Flickable {
                                anchors.fill: parent
                                clip: true
                                interactive: contentHeight > parent.height
                                contentHeight: addWordColumn.height

                                Column {
                                    width: parent.width
                                    spacing: units.gu(1)
                                    id: addWordColumn

                                    Label {
                                        text: i18n.tr("Word:")
                                    }
                                    TextField {
                                        id: wordField
                                        width: parent.width
                                    }
                                    Label {
                                        text: (propertyPage.head.sections.selectedIndex == 0) ? "Definition:" : "Translation:"
                                    }
                                    TextField {
                                        id: definTranslationField
                                        width: parent.width
                                    }
                                    Row{
                                        width: parent.width

                                        Button {
                                            width: parent.width/2
                                            text: i18n.tr("Save")
                                            action: Action{
                                                onTriggered: {
                                                    if (wordField.text != "" && definTranslationField.text != ""){
                                                        var tempContents = {};
                                                        var definOrTranslation = (propertyPage.head.sections.selectedIndex == 0) ? "definition" : "translation";
                                                        tempContents = vocabDB.contents;
                                                        for (var i = 0; i < tempContents["vocabularies"].length; i++){
                                                            if (tempContents["vocabularies"][i]["name"] === propertyPage.title && tempContents["vocabularies"][i]["color"] == propertyPage.contentColor){
                                                                if(definOrTranslation == "definition"){
                                                                    tempContents["vocabularies"][i]["definitions"].push({"word": wordField.text, "definition": definTranslationField.text});
                                                                    definitionModel.append({"word": wordField.text, "definition": definTranslationField.text});
                                                                }
                                                                else{
                                                                    tempContents["vocabularies"][i]["translations"].push({"from": wordField.text, "to": definTranslationField.text});
                                                                    translationModel.append({"from": wordField.text, "to": definTranslationField.text});
                                                                }
                                                            }
                                                        }
                                                        vocabDB.contents = tempContents;
                                                        PopupUtils.close(addWordDialog);
                                                    }
                                                }
                                            }
                                        }
                                        Button{
                                            text: i18n.tr("Cancel")
                                            width: parent.width/2
                                            action: Action{
                                                onTriggered: PopupUtils.close(addWordDialog)
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}