1 /*
2 * OpenClonk, http://www.openclonk.org
3 *
4 * Copyright (c) 2001-2009, RedWolf Design GmbH, http://www.clonk.de/
5 * Copyright (c) 2013, The OpenClonk Team and contributors
6 *
7 * Distributed under the terms of the ISC license; see accompanying file
8 * "COPYING" for details.
9 *
10 * "Clonk" is a registered trademark of Matthes Bender, used with permission.
11 * See accompanying file "TRADEMARK" for details.
12 *
13 * To redistribute this file separately, substitute the full license texts
14 * for the above references.
15 */
16
17 /* String localization editors */
18
19 #include "C4Include.h"
20 #include "script/C4Value.h"
21 #include "config/C4Config.h"
22 #include "editor/C4ConsoleQtLocalizeString.h"
23 #include "c4group/C4Language.h"
24
25
26 /* Single string editor */
27
C4ConsoleQtLocalizeStringDlg(class QMainWindow * parent_window,const C4Value & translations)28 C4ConsoleQtLocalizeStringDlg::C4ConsoleQtLocalizeStringDlg(class QMainWindow *parent_window, const C4Value &translations)
29 : QDialog(parent_window, Qt::WindowSystemMenuHint | Qt::WindowTitleHint | Qt::WindowCloseButtonHint)
30 , translations(translations)
31 {
32 ui.setupUi(this);
33 // Add language editors
34 int32_t lang_index = 0;
35 C4LanguageInfo *lang_info;
36 while (lang_info = ::Languages.GetInfo(lang_index++))
37 {
38 AddEditor(lang_info->Code, lang_info->Name);
39 }
40 // Fill in values
41 C4PropList *translations_proplist = translations.getPropList();
42 assert(translations_proplist);
43 for (C4String *lang_str : translations_proplist->GetSortedLocalProperties(false))
44 {
45 if (lang_str->GetData().getLength() == 2)
46 {
47 C4Value text_val;
48 if (translations_proplist->GetPropertyByS(lang_str, &text_val))
49 {
50 C4String *text = text_val.getStr();
51 if (text)
52 {
53 QLineEdit *editor = GetEditorByLanguage(lang_str->GetCStr());
54 if (!editor)
55 {
56 // Unknown language. Just add an editor without language name.
57 editor = AddEditor(lang_str->GetCStr(), nullptr);
58 }
59 editor->setText(QString(text->GetCStr()));
60 }
61 }
62 }
63 }
64 // Size
65 adjustSize();
66 setMinimumSize(size());
67 // Focus on first empty editor
68 if (edited_languages.size())
69 {
70 edited_languages.front().value_editor->setFocus(); // fallback to first editor
71 for (const auto & langs : edited_languages)
72 {
73 if (!langs.value_editor->text().length())
74 {
75 langs.value_editor->setFocus();
76 break;
77 }
78 }
79 }
80 }
81
DoError(const char * msg)82 void C4ConsoleQtLocalizeStringDlg::DoError(const char *msg)
83 {
84 QMessageBox::critical(this, ::LoadResStr("IDS_ERR_TITLE"), QString(msg));
85 }
86
AddEditor(const char * language,const char * language_name)87 QLineEdit *C4ConsoleQtLocalizeStringDlg::AddEditor(const char *language, const char *language_name)
88 {
89 assert(!GetEditorByLanguage(language));
90 // Add editor widgets
91 int32_t row = edited_languages.size();
92 QString language_label_text(language);
93 if (language_name) language_label_text.append(FormatString(" (%s)", language_name).getData());
94 QLabel *language_label = new QLabel(language_label_text, this);
95 ui.mainGrid->addWidget(language_label, row, 0);
96 QLineEdit *value_editor = new QLineEdit(this);
97 ui.mainGrid->addWidget(value_editor, row, 1);
98 // Add to list
99 EditedLanguage new_editor;
100 SCopy(language, new_editor.language, 2);
101 new_editor.value_editor = value_editor;
102 edited_languages.push_back(new_editor);
103 return value_editor;
104 }
105
GetEditorByLanguage(const char * language)106 QLineEdit *C4ConsoleQtLocalizeStringDlg::GetEditorByLanguage(const char *language)
107 {
108 // Search text editor by language ID
109 for (const auto & langs : edited_languages)
110 {
111 if (!strcmp(langs.language, language))
112 {
113 return langs.value_editor;
114 }
115 }
116 // Not found
117 return nullptr;
118 }
119
done(int r)120 void C4ConsoleQtLocalizeStringDlg::done(int r)
121 {
122 if (QDialog::Accepted == r) // ok was pressed
123 {
124 C4PropList *translations_proplist = translations.getPropList();
125 assert(translations_proplist);
126 // Set all translations
127 for (const auto & langs : edited_languages)
128 {
129 // Empty strings are set to nil, because that allows the user to set it to fallback
130 QString text = langs.value_editor->text();
131 if (text.length())
132 {
133 C4Value text_val = C4VString(text.toUtf8());
134 translations_proplist->SetPropertyByS(::Strings.RegString(langs.language), text_val);
135 }
136 else
137 {
138 translations_proplist->ResetProperty(::Strings.RegString(langs.language));
139 }
140 }
141 }
142 // Close
143 QDialog::done(r);
144 }
145
AddLanguagePressed()146 void C4ConsoleQtLocalizeStringDlg::AddLanguagePressed()
147 {
148 bool lang_ok = false;
149 QRegExpValidator validator(QRegExp("^[a-zA-Z][a-zA-Z]$"), this);
150 QString lang_id;
151 while (!lang_ok)
152 {
153 bool ok; int q = 0;
154 lang_id = QInputDialog::getText(this, LoadResStr("IDS_CNS_ADDLANGUAGE"), LoadResStr("IDS_CNS_ADDLANGUAGEID"), QLineEdit::Normal, QString(), &ok);
155 if (!ok) return;
156 lang_ok = (validator.validate(lang_id, q) == QValidator::Acceptable);
157 if (!lang_ok)
158 {
159 DoError(LoadResStr("IDS_ERR_INVALIDLANGUAGEID"));
160 }
161 }
162 // Either add or just focus existing editor
163 QLineEdit *editor = GetEditorByLanguage(lang_id.toUtf8());
164 if (!editor)
165 {
166 editor = AddEditor(lang_id.toUtf8(), nullptr);
167 adjustSize();
168 setMinimumSize(size());
169 }
170 editor->setFocus();
171 }
172