Repository navigation
Expand file tree
/
Copy pathMain.qml
More file actions
447 lines (384 loc) · 15.6 KB
/
Copy pathMain.qml
File metadata and controls
447 lines (384 loc) · 15.6 KB
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
// SPDX-License-Identifier: GPL-3.0-only
import QtQuick
import QtCore
import QtQuick.Controls
import QtQuick.Layouts
import QtQuick.Controls.FluentWinUI3
ApplicationWindow {
id: window
width: 720 / 1.618
height: 720
visible: true
title: qsTr("LocalTranslate")
Settings {
id: generalSettings
category: "General" // casing is important here
property alias windowWidth: window.width
property alias windowHeight: window.height
property string fromLangCode
property string toLangCode
}
Component.onCompleted: {
sourceText.forceActiveFocus(); // focus on source text as soon as app launches
}
// The original array of objects
readonly property var languages: translationBridge.languages
readonly property var sortedLanguages: languages.sort(
(a, b) => a.name
> b.name ? 1 : (b.name > a.name ? -1 : 0))
// Languages that don't use Latin script
readonly property var nonLatinLangs: ["ar", "be", "bg", "bn", "el", "fa", "gu", "he", "hi", "ja", "kn", "ko", "ml", "mt", "ru", "sr", "ta", "te", "uk", "zh"]
readonly property int controlRowHeight: 50
readonly property bool isLandscape: width > height
function doTranslate() {
if (sourceText.text.trim() === "") {
resultText.text = ""
return
}
let fromLangCode = fromLangCombo.currentValue
let toLangCode = toLangCombo.currentValue
let langPair = fromLangCode + toLangCode
let result = translationBridge.translate(sourceText.text, langPair)
resultText.text = result
}
// Debounce timer so we don't translate on every keystroke while typing.
Timer {
id: translateDebounce
interval: 400
repeat: false
onTriggered: window.doTranslate()
}
// Translate right away (e.g. on paste) and cancel any pending debounced run.
function translateImmediately() {
translateDebounce.stop()
doTranslate()
}
function clearText() {
translateDebounce.stop()
sourceText.clear()
resultText.clear()
sourceText.forceActiveFocus()
}
Shortcut {
sequences: ["Ctrl+Enter", "Ctrl+Return"]
context: Qt.ApplicationShortcut
onActivated: window.translateImmediately()
}
Shortcut {
// Pressing Ctrl+D will clear source and release text and focuses on the source text
sequences: ["Ctrl+D"]
context: Qt.ApplicationShortcut
onActivated: window.clearText()
}
RowLayout {
id: topRow
anchors.top: parent.top
anchors.left: parent.left
anchors.right: parent.right
anchors.margins: 8
spacing: 0
Text {
id: slimLogoText
text: "Local"
color: window.palette.text
font.pixelSize: 24
}
Text {
id: boldLogoText
text: "Translate"
color: window.palette.text
font.pixelSize: 24
font.bold: true
}
Item {
Layout.fillWidth: true
}
}
// ========== MAIN LAYOUT ==========
// We use 3 columns in landscape (from-frame | swap-button | to-frame)
// and 1 column in portrait (stack them top to bottom).
// So effectively, in portrait it's:
// Row 0: from-frame
// Row 1: swap-button
// Row 2: to-frame
GridLayout {
anchors.top: topRow.bottom
anchors.left: parent.left
anchors.right: parent.right
anchors.bottom: parent.bottom
rowSpacing: 4
columnSpacing: 4
anchors.margins: 8
columns: isLandscape ? 3 : 1
rows: isLandscape ? 1 : 3
// === SOURCE FRAME ===
Frame {
id: fromFrame
Layout.fillWidth: true
Layout.fillHeight: true
// If landscape => row=0, col=0
// If portrait => row=0, col=0
Layout.row: isLandscape ? 0 : 0
Layout.column: isLandscape ? 0 : 0
contentItem: Item {
anchors.fill: parent
ComboBox {
id: fromLangCombo
width: 200
font.pixelSize: 12
anchors.top: parent.top
anchors.left: parent.left
anchors.topMargin: 8
anchors.leftMargin: 8
model: window.sortedLanguages
textRole: "name"
valueRole: "code"
Component.onCompleted: {
const langCode = generalSettings.fromLangCode || "en"
currentIndex = window.sortedLanguages.findIndex(
lang => lang.code === langCode)
// set the lang in settings too
generalSettings.fromLangCode = langCode
}
onActivated: (index) => {
generalSettings.fromLangCode = model[currentIndex].code
// Re-translate with the newly selected language
window.translateImmediately()
}
}
// ScrollView for source text
ScrollView {
id: sourceScrollView
anchors.top: fromLangCombo.bottom
anchors.left: parent.left
anchors.right: parent.right
anchors.topMargin: 4
anchors.bottom: fromTransliterationView.visible ? fromTransliterationView.top : fromBottomRow.top
TextArea {
id: sourceText
width: parent.width
placeholderText: qsTr("Enter text to translate...")
wrapMode: TextEdit.Wrap
verticalAlignment: TextEdit.AlignTop
// Translate automatically after a short pause in typing.
onTextChanged: translateDebounce.restart()
// Translate immediately when text is pasted.
Keys.onPressed: (event) => {
if (event.matches(StandardKey.Paste))
Qt.callLater(window.translateImmediately)
}
}
}
// Display transliteration if non-Latin
ScrollView {
id: fromTransliterationView
anchors.left: parent.left
anchors.right: parent.right
anchors.bottom: fromBottomRow.top
anchors.bottomMargin: visible ? 4 : 0
height: visible ? Math.min(fromTransliterationText.implicitHeight,
parent.height / 3) : 0
visible: fromTransliterationText.text !== ""
clip: true
TextArea {
id: fromTransliterationText
width: parent.width
readOnly: true
wrapMode: TextEdit.Wrap
font.pointSize: 12
color: window.palette.placeholderText
leftPadding: 8
rightPadding: 8
topPadding: 4
bottomPadding: 4
background: null
text: {
let fromCode = fromLangCombo.currentValue
if (window.nonLatinLangs.includes(fromCode)) {
return translationBridge.transliterate(
sourceText.text, fromCode)
}
return ""
}
}
}
RowLayout {
id: fromBottomRow
anchors.left: parent.left
anchors.right: parent.right
anchors.bottom: parent.bottom
anchors.rightMargin: 8
anchors.leftMargin: 8
anchors.bottomMargin: 2
height: window.controlRowHeight
Button {
id: pasteButton
icon.source: "qrc:/images/paste.png"
width: 28
height: 28
font.pixelSize: 12
onClicked: {
sourceText.paste()
window.translateImmediately()
}
}
Item {
Layout.fillWidth: true
}
Button {
id: translateButton
text: qsTr("Translate")
width: 80
height: 28
highlighted: true
font.pixelSize: 12
font.bold: true
onClicked: window.translateImmediately()
ToolTip.visible: hovered
ToolTip.text: qsTr("Ctrl+Enter")
ToolTip.delay: 300
}
}
}
}
// === SWAP BUTTON IN THE MIDDLE ===
Button {
id: swapButton
icon.source: isLandscape ? "qrc:/images/arrow-left-right.png" : "qrc:/images/arrow-down-up.png"
// If landscape => row=0, col=1
// If portrait => row=1, col=0
Layout.row: isLandscape ? 0 : 1
Layout.column: isLandscape ? 1 : 0
Layout.alignment: Qt.AlignHCenter | Qt.AlignVCenter
Layout.preferredWidth: window.isLandscape ? 40 : implicitWidth
Layout.preferredHeight: window.isLandscape ? 40 : implicitHeight
onClicked: {
// If result text is not empty, move it to the source and clear it
if (resultText.text.trim() !== "") {
sourceText.text = resultText.text
resultText.text = ""
}
// Swap languages
var oldIndex = fromLangCombo.currentIndex
fromLangCombo.currentIndex = toLangCombo.currentIndex
toLangCombo.currentIndex = oldIndex
// Update settings too
generalSettings.fromLangCode = sortedLanguages[fromLangCombo.currentIndex].code
generalSettings.toLangCode = sortedLanguages[toLangCombo.currentIndex].code
// Re-translate after swapping
window.translateImmediately()
}
}
// === RESULT FRAME ===
Frame {
id: toFrame
Layout.fillWidth: true
Layout.fillHeight: true
// If landscape => row=0, col=2
// If portrait => row=2, col=0
Layout.row: isLandscape ? 0 : 2
Layout.column: isLandscape ? 2 : 0
contentItem: Item {
anchors.fill: parent
ComboBox {
id: toLangCombo
width: 200
font.pixelSize: 12
anchors.top: parent.top
anchors.left: parent.left
anchors.topMargin: 8
anchors.leftMargin: 8
model: window.sortedLanguages
textRole: "name"
valueRole: "code"
Component.onCompleted: {
const langCode = generalSettings.toLangCode || "de"
currentIndex = window.sortedLanguages.findIndex(
lang => lang.code === langCode)
// set the lang in settings too
generalSettings.toLangCode = langCode
}
onActivated: (index) => {
generalSettings.toLangCode = model[currentIndex].code
// Re-translate with the newly selected language
window.translateImmediately()
}
}
ScrollView {
id: resultScrollView
anchors.top: toLangCombo.bottom
anchors.left: parent.left
anchors.right: parent.right
anchors.topMargin: 4
anchors.bottom: toTransliterationView.visible ? toTransliterationView.top : toBottomRow.top
TextArea {
id: resultText
width: parent.width
placeholderText: qsTr("Translation will appear here")
wrapMode: TextEdit.Wrap
readOnly: true
selectByMouse: true
verticalAlignment: TextEdit.AlignTop
}
}
ScrollView {
id: toTransliterationView
anchors.left: parent.left
anchors.right: parent.right
anchors.bottom: toBottomRow.top
anchors.bottomMargin: visible ? 4 : 0
height: visible ? Math.min(toTransliterationText.implicitHeight,
parent.height / 3) : 0
visible: toTransliterationText.text !== ""
clip: true
TextArea {
id: toTransliterationText
width: parent.width
readOnly: true
wrapMode: TextEdit.Wrap
font.pointSize: 12
color: window.palette.placeholderText
leftPadding: 8
rightPadding: 8
topPadding: 4
bottomPadding: 4
background: null
text: {
let toCode = toLangCombo.currentValue
if (window.nonLatinLangs.includes(toCode)) {
return translationBridge.transliterate(
resultText.text, toCode)
}
return ""
}
}
}
RowLayout {
id: toBottomRow
anchors.left: parent.left
anchors.right: parent.right
anchors.bottom: parent.bottom
anchors.leftMargin: 8
anchors.rightMargin: 8
anchors.bottomMargin: 2
height: window.controlRowHeight
Item {
Layout.fillWidth: true
}
Button {
id: copyButton
icon.source: "qrc:/images/copy.png"
font.pixelSize: 12
onClicked: {
if (resultText.text.length > 0) {
resultText.selectAll()
resultText.copy()
resultText.deselect()
}
}
}
}
}
}
}
}