Skip to content

Commit fafa77a

Browse files
Added proper error handling for translations & Fixed translate button
1 parent 41ccd4e commit fafa77a

3 files changed

Lines changed: 41 additions & 33 deletions

File tree

src/application/common/enums/error_code.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ enum class APPLICATION_EXPORT ErrorCode
4343
NameTooLong, // 24
4444
AutomaticLoginFailed, // 25
4545
TranslationLimitReached, // 26
46-
TranslationRequestTooLong, // 26
46+
TranslationRequestTooLong, // 27
4747
};
4848

4949
Q_ENUM_NS(ErrorCode)

src/presentation/desktop/modules/CustomComponents/comboBox/MComboBox.qml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -217,14 +217,14 @@ Item {
217217
selectionPopup.close()
218218
}
219219

220-
function selectItemByValue(itemName, propertyName) {
220+
function selectItemByValue(itemName, propertyName, initialSelect = false) {
221221
if (propertyName === undefined) {
222222
propertyName = contentPropertyName
223223
}
224224

225225
for (var i = 0; i < selectionPopup.model.count; i++) {
226226
if (selectionPopup.model.get(i)[propertyName] === itemName) {
227-
selectItem(i)
227+
selectItem(i, initialSelect)
228228
return
229229
}
230230
}

src/presentation/desktop/readingPage/MTranslationPopup.qml

Lines changed: 38 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@ Popup {
3030

3131
let lastLang = AppInfoController.lastTranslationLanguage
3232

33-
targetLanguageComboBox.selectItemByValue(lastLang.toUpperCase(), "code")
33+
targetLanguageComboBox.selectItemByValue(lastLang.toUpperCase(),
34+
"code", true)
3435
internal.sendTranslationRequest()
3536
}
3637

@@ -178,32 +179,29 @@ Popup {
178179
loadingAnimation.visible = false
179180
}
180181

181-
function onTranslationRequestTooLong() {
182-
loadingAnimation.visible = false
182+
function onTranslationErrorOccured() {
183+
loadingAnimation.visible = true
184+
errorItem.visible = true
185+
errorText.text = qsTr(
186+
'Opps! Something went wrong. Please try again.')
183187
}
184188

185189
function onTranslationLimitReached() {
186190
loadingAnimation.visible = false
191+
errorItem.visible = true
192+
//: Make sure that the words make a valid sentence
193+
errorText.text = qsTr('You have reached your daily limit.') + ' '
194+
+ ' <a href="update" style="color: ' + Style.colorBasePurple
195+
+ '; text-decoration: none;">' + qsTr(
196+
'Upgrade') + '</a> ' + qsTr('to continue.')
187197
}
188198

189-
function onTranslationErrorOccured() {
199+
function onTranslationRequestTooLong() {
190200
loadingAnimation.visible = false
201+
errorItem.visible = true
202+
errorText.text = qsTr(
203+
'Oops! The text is too long. Please shorten your selection.')
191204
}
192-
193-
// function onExplanationLimitReached() {
194-
// errorItem.visible = true
195-
// //: Make sure that the words make a valid sentence
196-
// errorText.text = qsTr('You have reached your daily limit.') + ' '
197-
// + ' <a href="update" style="color: ' + Style.colorBasePurple
198-
// + '; text-decoration: none;">' + qsTr(
199-
// 'Upgrade') + '</a> ' + qsTr('to continue.')
200-
// }
201-
202-
// function onExplanationRequestTooLong() {
203-
// errorItem.visible = true
204-
// errorText.text = qsTr(
205-
// 'Oops! The text is too long. Please shorten your selection.')
206-
// }
207205
}
208206

209207
ColumnLayout {
@@ -249,7 +247,10 @@ Popup {
249247
allowUnselectingItems: false
250248

251249
model: langModel
252-
onItemChanged: sourceLanguageComboBox.closePopup()
250+
onItemChanged: {
251+
translateButton.active = true
252+
sourceLanguageComboBox.closePopup()
253+
}
253254
}
254255

255256
Item {
@@ -282,7 +283,10 @@ Popup {
282283
allowUnselectingItems: false
283284

284285
model: langModel
285-
onItemChanged: targetLanguageComboBox.closePopup()
286+
onItemChanged: {
287+
translateButton.active = true
288+
targetLanguageComboBox.closePopup()
289+
}
286290
}
287291
}
288292

@@ -419,9 +423,9 @@ Popup {
419423
Image {
420424
id: errorIllustration
421425
Layout.alignment: Qt.AlignHCenter
422-
Layout.topMargin: -30
426+
Layout.topMargin: -16
423427
source: Icons.attentionPurple
424-
sourceSize.width: 270
428+
sourceSize.width: 160
425429
fillMode: Image.PreserveAspectFit
426430
}
427431

@@ -432,7 +436,7 @@ Popup {
432436
text: ""
433437
color: Style.colorTitle
434438
font.weight: Font.Medium
435-
font.pointSize: Fonts.size14
439+
font.pointSize: Fonts.size13
436440
onLinkActivated: Qt.openUrlExternally(
437441
AppInfoController.website + "/pricing")
438442

@@ -485,29 +489,31 @@ Popup {
485489
}
486490

487491
MButton {
488-
id: applyButton
492+
id: translateButton
489493
Layout.preferredWidth: 124
490494
Layout.preferredHeight: 38
491495
Layout.topMargin: 8
492496
Layout.alignment: Qt.AlignLeft
493-
borderWidth: 0
494-
backgroundColor: Style.colorBasePurple
497+
borderWidth: active ? 0 : 1
498+
backgroundColor: active ? Style.colorBasePurple : "transparent"
495499
text: qsTr("Translate")
496-
textColor: Style.colorFocusedButtonText
500+
textColor: active ? Style.colorFocusedButtonText : Style.colorUnfocusedButtonText
497501
fontWeight: Font.Medium
498502
fontSize: Fonts.size12
499503

500-
onClicked: internal.sendTranslationRequest()
504+
onClicked: if (active)
505+
internal.sendTranslationRequest()
501506
}
502507
}
503508

504509
QtObject {
505510
id: internal
506511
property int maxTextHeight: baseRoot.height / 4
507-
property int minTextHeight: 120
512+
property int minTextHeight: 140
508513

509514
function sendTranslationRequest() {
510515
root.translation = ""
516+
errorItem.visible = false
511517
loadingAnimation.visible = true
512518

513519
AppInfoController.lastTranslationLanguage
@@ -518,6 +524,8 @@ Popup {
518524
"code")
519525

520526
AiToolsController.getTranslation(root.input, sourceLang, targetLang)
527+
528+
translateButton.active = false
521529
}
522530
}
523531
}

0 commit comments

Comments
 (0)