Debian Codegraph @

app/qml/KeyboardRows/KeyboardLayout.qml

raw
1 /*
2 * Copyright (C) 2015 Canonical Ltd
3 *
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 3 as
6 * published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 *
16 * Author: Filippo Scognamiglio <[email protected]>
17 */
18 import QtQuick 2.4
19 import Lomiri.Components 1.3
20
21 import "jsonParser.js" as Parser
22
23 KeyboardRow {
24 id: keyboardRow
25 keyWidth: units.gu(5)
26
27 readonly property variant modifiers: ["Control", "Alt", "Shift"]
28
29 // This label is used to compute the maximum width of all the controls.
30 Label {
31 id: hiddenLabel
32 visible: false
33 }
34
35 function dropProfile() {
36 // TODO Check if this is enough and doesn't leak.
37 for (var i = 0; i < model.length; i++)
38 model[i].destroy();
39 model = [];
40 }
41
42 function createActionString(action) {
43 switch(action.type){
44 case "key":
45 return createKeyActionString(action.key, action.mod, action.text, action.id);
46 case "string":
47 return createStringActionString(action.string, action.text);
48 }
49 }
50
51 function createOtherActionsString(actions) {
52 var result = "[";
53 for (var i = 0; i < actions.length; i++) {
54 var action = actions[i];
55 result += createActionString(action);
56
57 if (i < actions.length - 1)
58 result += ",";
59 }
60
61 return result + "]";
62 }
63
64 function createKeyActionString(key, mod, text, id) {
65 if (modifiers.indexOf(mod) === -1)
66 mod = "No";
67
68 return "Action { text: \"" + createKeyText(key, mod, text, id) + "\"; onTriggered: simulateKey(Qt.Key_"+ key + ", Qt." + mod + "Modifier); }";
69 }
70
71 function createStringActionString(string, text) {
72 var textString = text ? "text: \"" + text + "\";" : "";
73 return "Action { " + textString + " onTriggered: simulateCommand(\"" + string + "\"); }";
74 }
75
76 function createEntryString(text, actionString, otherActionsString) {
77 var objectString = "
78 import QtQuick 2.4
79 import Lomiri.Components 1.3
80
81 KeyModel {
82 text: \"" + text + "\"
83 mainAction: " + actionString + "\n
84 actions:" + otherActionsString +
85 "}"
86 return objectString;
87 }
88
89 function createKeyText(key, mod, text, id) {
90 if (id) {
91 return translator.getTranslatedNameById(translator.key, id);
92 } else if (text) {
93 return text;
94 } else if (key) {
95 return ((mod && modifiers.indexOf(mod) !== -1) ? translator.getTranslatedNameById(translator.modifier, mod) + "+" : "") + key;
96 } else
97 return "";
98 }
99
100 function loadProfile(profileObject) {
101 dropProfile();
102
103 var maxWidth = 0;
104
105 // This function might raise exceptions which are handled in KeyboardBar.qml
106 var profile = profileObject;
107
108 name = "";
109 short_name = "";
110 if (profile.id) {
111 name = translator.getTranslatedNameById(translator.name, profile.id);
112 short_name = translator.getTranslatedNameById(translator.shortName, profile.id);
113 }
114 if (name === "")
115 name = profile.name;
116 if (short_name === "")
117 short_name = profile.short_name;
118
119 var layoutModel = []
120 for (var i = 0; i < profile.buttons.length; i++) {
121 var button = profile.buttons[i];
122 var keyText = createKeyText(button.main_action.key, button.main_action.mod, button.main_action.text, button.main_action.id);
123 var mainActionString = createActionString(button.main_action);
124
125 var otherActionsString = button.other_actions
126 ? createOtherActionsString(button.other_actions)
127 : "[]";
128
129 var entryString = createEntryString(keyText, mainActionString, otherActionsString);
130
131 layoutModel.push(Qt.createQmlObject(entryString, keyboardRow));
132
133 hiddenLabel.text = keyText;
134 maxWidth = Math.max(hiddenLabel.width, maxWidth);
135 }
136
137 keyWidth = maxWidth + units.gu(3);
138 model = layoutModel;
139 }
140
141 Component.onDestruction: dropProfile();
142 }