0
|
1 |
/****************************************************************************
|
|
2 |
**
|
|
3 |
** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
|
|
4 |
** All rights reserved.
|
|
5 |
** Contact: Nokia Corporation (qt-info@nokia.com)
|
|
6 |
**
|
|
7 |
** This file is part of the Qt Linguist of the Qt Toolkit.
|
|
8 |
**
|
|
9 |
** $QT_BEGIN_LICENSE:LGPL$
|
|
10 |
** No Commercial Usage
|
|
11 |
** This file contains pre-release code and may not be distributed.
|
|
12 |
** You may use this file in accordance with the terms and conditions
|
|
13 |
** contained in the Technology Preview License Agreement accompanying
|
|
14 |
** this package.
|
|
15 |
**
|
|
16 |
** GNU Lesser General Public License Usage
|
|
17 |
** Alternatively, this file may be used under the terms of the GNU Lesser
|
|
18 |
** General Public License version 2.1 as published by the Free Software
|
|
19 |
** Foundation and appearing in the file LICENSE.LGPL included in the
|
|
20 |
** packaging of this file. Please review the following information to
|
|
21 |
** ensure the GNU Lesser General Public License version 2.1 requirements
|
|
22 |
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
|
23 |
**
|
|
24 |
** In addition, as a special exception, Nokia gives you certain additional
|
|
25 |
** rights. These rights are described in the Nokia Qt LGPL Exception
|
|
26 |
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
|
|
27 |
**
|
|
28 |
** If you have questions regarding the use of this file, please contact
|
|
29 |
** Nokia at qt-info@nokia.com.
|
|
30 |
**
|
|
31 |
**
|
|
32 |
**
|
|
33 |
**
|
|
34 |
**
|
|
35 |
**
|
|
36 |
**
|
|
37 |
**
|
|
38 |
** $QT_END_LICENSE$
|
|
39 |
**
|
|
40 |
****************************************************************************/
|
|
41 |
|
|
42 |
/* TRANSLATOR MessageEditor
|
|
43 |
|
|
44 |
This is the right panel of the main window.
|
|
45 |
*/
|
|
46 |
|
|
47 |
#include "messageeditor.h"
|
|
48 |
#include "messageeditorwidgets.h"
|
|
49 |
#include "simtexth.h"
|
|
50 |
#include "phrasemodel.h"
|
|
51 |
|
|
52 |
#include <QApplication>
|
|
53 |
#include <QBoxLayout>
|
|
54 |
#include <QClipboard>
|
|
55 |
#include <QDebug>
|
|
56 |
#include <QDockWidget>
|
|
57 |
#include <QHeaderView>
|
|
58 |
#include <QKeyEvent>
|
|
59 |
#include <QMainWindow>
|
|
60 |
#include <QPainter>
|
|
61 |
#include <QTreeView>
|
|
62 |
#include <QVBoxLayout>
|
|
63 |
|
|
64 |
QT_BEGIN_NAMESPACE
|
|
65 |
|
|
66 |
#ifdef NEVER_TRUE
|
|
67 |
// Allow translators to provide localized names for QLocale::languageToString
|
|
68 |
// At least the own language should be translated ... This is a "hack" until
|
|
69 |
// functionality is provided within Qt (see task 196275).
|
|
70 |
static const char * language_strings[] =
|
|
71 |
{
|
|
72 |
QT_TRANSLATE_NOOP("MessageEditor", "Russian"),
|
|
73 |
QT_TRANSLATE_NOOP("MessageEditor", "German"),
|
|
74 |
QT_TRANSLATE_NOOP("MessageEditor", "Japanese"),
|
|
75 |
QT_TRANSLATE_NOOP("MessageEditor", "French"),
|
|
76 |
QT_TRANSLATE_NOOP("MessageEditor", "Polish"),
|
|
77 |
QT_TRANSLATE_NOOP("MessageEditor", "Chinese")
|
|
78 |
};
|
|
79 |
#endif
|
|
80 |
|
|
81 |
/*
|
|
82 |
MessageEditor class impl.
|
|
83 |
|
|
84 |
Handles layout of dock windows and the editor page.
|
|
85 |
*/
|
|
86 |
MessageEditor::MessageEditor(MultiDataModel *dataModel, QMainWindow *parent)
|
|
87 |
: QScrollArea(parent->centralWidget()),
|
|
88 |
m_dataModel(dataModel),
|
|
89 |
m_currentModel(-1),
|
|
90 |
m_currentNumerus(-1),
|
|
91 |
m_lengthVariants(false),
|
|
92 |
m_undoAvail(false),
|
|
93 |
m_redoAvail(false),
|
|
94 |
m_cutAvail(false),
|
|
95 |
m_copyAvail(false),
|
|
96 |
m_selectionHolder(0),
|
|
97 |
m_focusWidget(0)
|
|
98 |
{
|
|
99 |
setObjectName(QLatin1String("scroll area"));
|
|
100 |
|
|
101 |
// Use white explicitly as the background color for the editor page.
|
|
102 |
QPalette p;
|
|
103 |
p.setColor(QPalette::Active, QPalette::Base, Qt::white);
|
|
104 |
p.setColor(QPalette::Inactive, QPalette::Base, Qt::white);
|
|
105 |
p.setColor(QPalette::Disabled, QPalette::Base, Qt::white);
|
|
106 |
p.setColor(QPalette::Active, QPalette::Window, Qt::white);
|
|
107 |
p.setColor(QPalette::Inactive, QPalette::Window, Qt::white);
|
|
108 |
p.setColor(QPalette::Disabled, QPalette::Window, Qt::white);
|
|
109 |
setPalette(p);
|
|
110 |
|
|
111 |
setupEditorPage();
|
|
112 |
|
|
113 |
// Signals
|
|
114 |
connect(qApp->clipboard(), SIGNAL(dataChanged()),
|
|
115 |
SLOT(clipboardChanged()));
|
|
116 |
connect(m_dataModel, SIGNAL(modelAppended()),
|
|
117 |
SLOT(messageModelAppended()));
|
|
118 |
connect(m_dataModel, SIGNAL(modelDeleted(int)),
|
|
119 |
SLOT(messageModelDeleted(int)));
|
|
120 |
connect(m_dataModel, SIGNAL(allModelsDeleted()),
|
|
121 |
SLOT(allModelsDeleted()));
|
|
122 |
connect(m_dataModel, SIGNAL(languageChanged(int)),
|
|
123 |
SLOT(setTargetLanguage(int)));
|
|
124 |
|
|
125 |
m_tabOrderTimer.setSingleShot(true);
|
|
126 |
connect(&m_tabOrderTimer, SIGNAL(timeout()), SLOT(reallyFixTabOrder()));
|
|
127 |
|
|
128 |
clipboardChanged();
|
|
129 |
|
|
130 |
setWhatsThis(tr("This whole panel allows you to view and edit "
|
|
131 |
"the translation of some source text."));
|
|
132 |
showNothing();
|
|
133 |
}
|
|
134 |
|
|
135 |
void MessageEditor::setupEditorPage()
|
|
136 |
{
|
|
137 |
QFrame *editorPage = new QFrame;
|
|
138 |
editorPage->setObjectName(QLatin1String("editorPage"));
|
|
139 |
|
|
140 |
editorPage->setStyleSheet(QLatin1String(
|
|
141 |
"QFrame#editorPage { border-image: url(:/images/transbox.png) 12 16 16 12 repeat;"
|
|
142 |
" border-width: 12px 16px 16px 12px; }"
|
|
143 |
"QFrame#editorPage { background-color: white; }"
|
|
144 |
"QLabel { font-weight: bold; }"
|
|
145 |
));
|
|
146 |
editorPage->setSizePolicy(QSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed));
|
|
147 |
|
|
148 |
m_source = new FormWidget(tr("Source text"), false);
|
|
149 |
m_source->setHideWhenEmpty(true);
|
|
150 |
m_source->setWhatsThis(tr("This area shows the source text."));
|
|
151 |
connect(m_source, SIGNAL(selectionChanged(QTextEdit *)),
|
|
152 |
SLOT(selectionChanged(QTextEdit *)));
|
|
153 |
|
|
154 |
m_pluralSource = new FormWidget(tr("Source text (Plural)"), false);
|
|
155 |
m_pluralSource->setHideWhenEmpty(true);
|
|
156 |
m_pluralSource->setWhatsThis(tr("This area shows the plural form of the source text."));
|
|
157 |
connect(m_pluralSource, SIGNAL(selectionChanged(QTextEdit *)),
|
|
158 |
SLOT(selectionChanged(QTextEdit *)));
|
|
159 |
|
|
160 |
m_commentText = new FormWidget(tr("Developer comments"), false);
|
|
161 |
m_commentText->setHideWhenEmpty(true);
|
|
162 |
m_commentText->setObjectName(QLatin1String("comment/context view"));
|
|
163 |
m_commentText->setWhatsThis(tr("This area shows a comment that"
|
|
164 |
" may guide you, and the context in which the text"
|
|
165 |
" occurs.") );
|
|
166 |
|
|
167 |
QBoxLayout *subLayout = new QVBoxLayout;
|
|
168 |
|
|
169 |
subLayout->setMargin(5);
|
|
170 |
subLayout->addWidget(m_source);
|
|
171 |
subLayout->addWidget(m_pluralSource);
|
|
172 |
subLayout->addWidget(m_commentText);
|
|
173 |
|
|
174 |
m_layout = new QVBoxLayout;
|
|
175 |
m_layout->setSpacing(2);
|
|
176 |
m_layout->setMargin(2);
|
|
177 |
m_layout->addLayout(subLayout);
|
|
178 |
m_layout->addStretch(1);
|
|
179 |
editorPage->setLayout(m_layout);
|
|
180 |
|
|
181 |
setWidget(editorPage);
|
|
182 |
setWidgetResizable(true);
|
|
183 |
}
|
|
184 |
|
|
185 |
QPalette MessageEditor::paletteForModel(int model) const
|
|
186 |
{
|
|
187 |
QBrush brush = m_dataModel->brushForModel(model);
|
|
188 |
QPalette pal;
|
|
189 |
|
|
190 |
if (m_dataModel->isModelWritable(model)) {
|
|
191 |
pal.setBrush(QPalette::Window, brush);
|
|
192 |
} else {
|
|
193 |
QPixmap pm(brush.texture().size());
|
|
194 |
pm.fill();
|
|
195 |
QPainter p(&pm);
|
|
196 |
p.fillRect(brush.texture().rect(), brush);
|
|
197 |
pal.setBrush(QPalette::Window, pm);
|
|
198 |
}
|
|
199 |
return pal;
|
|
200 |
}
|
|
201 |
|
|
202 |
void MessageEditor::messageModelAppended()
|
|
203 |
{
|
|
204 |
int model = m_editors.size();
|
|
205 |
m_editors.append(MessageEditorData());
|
|
206 |
MessageEditorData &ed = m_editors.last();
|
|
207 |
ed.pluralEditMode = false;
|
|
208 |
ed.fontSize = font().pointSize();
|
|
209 |
ed.container = new QWidget;
|
|
210 |
if (model > 0) {
|
|
211 |
ed.container->setPalette(paletteForModel(model));
|
|
212 |
ed.container->setAutoFillBackground(true);
|
|
213 |
if (model == 1) {
|
|
214 |
m_editors[0].container->setPalette(paletteForModel(0));
|
|
215 |
m_editors[0].container->setAutoFillBackground(true);
|
|
216 |
}
|
|
217 |
}
|
|
218 |
bool writable = m_dataModel->isModelWritable(model);
|
|
219 |
ed.transCommentText = new FormWidget(QString(), true);
|
|
220 |
ed.transCommentText->setEditingEnabled(writable);
|
|
221 |
ed.transCommentText->setHideWhenEmpty(!writable);
|
|
222 |
ed.transCommentText->setWhatsThis(tr("Here you can enter comments for your own use."
|
|
223 |
" They have no effect on the translated applications.") );
|
|
224 |
ed.transCommentText->getEditor()->installEventFilter(this);
|
|
225 |
connect(ed.transCommentText, SIGNAL(selectionChanged(QTextEdit *)),
|
|
226 |
SLOT(selectionChanged(QTextEdit *)));
|
|
227 |
connect(ed.transCommentText, SIGNAL(textChanged(QTextEdit *)),
|
|
228 |
SLOT(emitTranslatorCommentChanged(QTextEdit *)));
|
|
229 |
connect(ed.transCommentText, SIGNAL(textChanged(QTextEdit *)), SLOT(resetHoverSelection()));
|
|
230 |
connect(ed.transCommentText, SIGNAL(cursorPositionChanged()), SLOT(resetHoverSelection()));
|
|
231 |
fixTabOrder();
|
|
232 |
QBoxLayout *box = new QVBoxLayout(ed.container);
|
|
233 |
box->setMargin(5);
|
|
234 |
box->addWidget(ed.transCommentText);
|
|
235 |
box->addSpacing(ed.transCommentText->getEditor()->fontMetrics().height() / 2);
|
|
236 |
m_layout->addWidget(ed.container);
|
|
237 |
setTargetLanguage(model);
|
|
238 |
}
|
|
239 |
|
|
240 |
void MessageEditor::allModelsDeleted()
|
|
241 |
{
|
|
242 |
foreach (const MessageEditorData &med, m_editors)
|
|
243 |
med.container->deleteLater();
|
|
244 |
m_editors.clear();
|
|
245 |
m_currentModel = -1;
|
|
246 |
// Do not emit activeModelChanged() - the main window will refresh anyway
|
|
247 |
m_currentNumerus = -1;
|
|
248 |
showNothing();
|
|
249 |
}
|
|
250 |
|
|
251 |
void MessageEditor::messageModelDeleted(int model)
|
|
252 |
{
|
|
253 |
m_editors[model].container->deleteLater();
|
|
254 |
m_editors.removeAt(model);
|
|
255 |
if (model <= m_currentModel) {
|
|
256 |
if (model < m_currentModel || m_currentModel == m_editors.size())
|
|
257 |
--m_currentModel;
|
|
258 |
// Do not emit activeModelChanged() - the main window will refresh anyway
|
|
259 |
if (m_currentModel >= 0) {
|
|
260 |
if (m_currentNumerus >= m_editors[m_currentModel].transTexts.size())
|
|
261 |
m_currentNumerus = m_editors[m_currentModel].transTexts.size() - 1;
|
|
262 |
activeEditor()->setFocus();
|
|
263 |
} else {
|
|
264 |
m_currentNumerus = -1;
|
|
265 |
}
|
|
266 |
}
|
|
267 |
if (m_editors.size() == 1) {
|
|
268 |
m_editors[0].container->setAutoFillBackground(false);
|
|
269 |
} else {
|
|
270 |
for (int i = model; i < m_editors.size(); ++i)
|
|
271 |
m_editors[i].container->setPalette(paletteForModel(i));
|
|
272 |
}
|
|
273 |
}
|
|
274 |
|
|
275 |
void MessageEditor::addPluralForm(int model, const QString &label, bool writable)
|
|
276 |
{
|
|
277 |
FormMultiWidget *transEditor = new FormMultiWidget(label);
|
|
278 |
connect(transEditor, SIGNAL(editorCreated(QTextEdit *)), SLOT(editorCreated(QTextEdit *)));
|
|
279 |
transEditor->setEditingEnabled(writable);
|
|
280 |
transEditor->setHideWhenEmpty(!writable);
|
|
281 |
if (!m_editors[model].transTexts.isEmpty())
|
|
282 |
transEditor->setVisible(false);
|
|
283 |
transEditor->setMultiEnabled(m_lengthVariants);
|
|
284 |
static_cast<QBoxLayout *>(m_editors[model].container->layout())->insertWidget(
|
|
285 |
m_editors[model].transTexts.count(), transEditor);
|
|
286 |
|
|
287 |
connect(transEditor, SIGNAL(selectionChanged(QTextEdit *)),
|
|
288 |
SLOT(selectionChanged(QTextEdit *)));
|
|
289 |
connect(transEditor, SIGNAL(textChanged(QTextEdit *)),
|
|
290 |
SLOT(emitTranslationChanged(QTextEdit *)));
|
|
291 |
connect(transEditor, SIGNAL(textChanged(QTextEdit *)), SLOT(resetHoverSelection()));
|
|
292 |
connect(transEditor, SIGNAL(cursorPositionChanged()), SLOT(resetHoverSelection()));
|
|
293 |
|
|
294 |
m_editors[model].transTexts << transEditor;
|
|
295 |
}
|
|
296 |
|
|
297 |
void MessageEditor::editorCreated(QTextEdit *te)
|
|
298 |
{
|
|
299 |
FormMultiWidget *snd = static_cast<FormMultiWidget *>(sender());
|
|
300 |
for (int model = 0; ; ++model) {
|
|
301 |
MessageEditorData med = m_editors.at(model);
|
|
302 |
if (med.transTexts.contains(snd)) {
|
|
303 |
QFont font;
|
|
304 |
font.setPointSize(static_cast<int>(med.fontSize));
|
|
305 |
te->setFont(font);
|
|
306 |
|
|
307 |
te->installEventFilter(this);
|
|
308 |
|
|
309 |
fixTabOrder();
|
|
310 |
return;
|
|
311 |
}
|
|
312 |
}
|
|
313 |
}
|
|
314 |
|
|
315 |
void MessageEditor::fixTabOrder()
|
|
316 |
{
|
|
317 |
m_tabOrderTimer.start(0);
|
|
318 |
}
|
|
319 |
|
|
320 |
void MessageEditor::reallyFixTabOrder()
|
|
321 |
{
|
|
322 |
QWidget *prev = this;
|
|
323 |
foreach (const MessageEditorData &med, m_editors) {
|
|
324 |
foreach (FormMultiWidget *fmw, med.transTexts)
|
|
325 |
foreach (QTextEdit *te, fmw->getEditors()) {
|
|
326 |
setTabOrder(prev, te);
|
|
327 |
prev = te;
|
|
328 |
}
|
|
329 |
QTextEdit *te = med.transCommentText->getEditor();
|
|
330 |
setTabOrder(prev, te);
|
|
331 |
prev = te;
|
|
332 |
}
|
|
333 |
}
|
|
334 |
|
|
335 |
/*! internal
|
|
336 |
Returns all translations for an item.
|
|
337 |
The number of translations is dependent on if we have a plural form or not.
|
|
338 |
If we don't have a plural form, then this should only contain one item.
|
|
339 |
Otherwise it will contain the number of numerus forms for the particular language.
|
|
340 |
*/
|
|
341 |
QStringList MessageEditor::translations(int model) const
|
|
342 |
{
|
|
343 |
QStringList translations;
|
|
344 |
for (int i = 0; i < m_editors[model].transTexts.count() &&
|
|
345 |
m_editors[model].transTexts.at(i)->isVisible(); ++i)
|
|
346 |
translations << m_editors[model].transTexts[i]->getTranslation();
|
|
347 |
return translations;
|
|
348 |
}
|
|
349 |
|
|
350 |
static void clearSelection(QTextEdit *t)
|
|
351 |
{
|
|
352 |
bool oldBlockState = t->blockSignals(true);
|
|
353 |
QTextCursor c = t->textCursor();
|
|
354 |
c.clearSelection();
|
|
355 |
t->setTextCursor(c);
|
|
356 |
t->blockSignals(oldBlockState);
|
|
357 |
}
|
|
358 |
|
|
359 |
void MessageEditor::selectionChanged(QTextEdit *te)
|
|
360 |
{
|
|
361 |
if (te != m_selectionHolder) {
|
|
362 |
if (m_selectionHolder)
|
|
363 |
clearSelection(m_selectionHolder);
|
|
364 |
m_selectionHolder = (te->textCursor().hasSelection() ? te : 0);
|
|
365 |
updateCanCutCopy();
|
|
366 |
}
|
|
367 |
}
|
|
368 |
|
|
369 |
void MessageEditor::resetHoverSelection()
|
|
370 |
{
|
|
371 |
if (m_selectionHolder &&
|
|
372 |
(m_selectionHolder == m_source->getEditor()
|
|
373 |
|| m_selectionHolder == m_pluralSource->getEditor()))
|
|
374 |
resetSelection();
|
|
375 |
}
|
|
376 |
|
|
377 |
void MessageEditor::resetSelection()
|
|
378 |
{
|
|
379 |
if (m_selectionHolder) {
|
|
380 |
clearSelection(m_selectionHolder);
|
|
381 |
m_selectionHolder = 0;
|
|
382 |
updateCanCutCopy();
|
|
383 |
}
|
|
384 |
}
|
|
385 |
|
|
386 |
void MessageEditor::activeModelAndNumerus(int *model, int *numerus) const
|
|
387 |
{
|
|
388 |
for (int j = 0; j < m_editors.count(); ++j) {
|
|
389 |
for (int i = 0; i < m_editors[j].transTexts.count(); ++i)
|
|
390 |
foreach (QTextEdit *te, m_editors[j].transTexts[i]->getEditors())
|
|
391 |
if (m_focusWidget == te) {
|
|
392 |
*model = j;
|
|
393 |
*numerus = i;
|
|
394 |
return;
|
|
395 |
}
|
|
396 |
if (m_focusWidget == m_editors[j].transCommentText->getEditor()) {
|
|
397 |
*model = j;
|
|
398 |
*numerus = -1;
|
|
399 |
return;
|
|
400 |
}
|
|
401 |
}
|
|
402 |
*model = -1;
|
|
403 |
*numerus = -1;
|
|
404 |
}
|
|
405 |
|
|
406 |
QTextEdit *MessageEditor::activeTranslation() const
|
|
407 |
{
|
|
408 |
if (m_currentNumerus < 0)
|
|
409 |
return 0;
|
|
410 |
foreach (QTextEdit *te, m_editors[m_currentModel].transTexts[m_currentNumerus]->getEditors())
|
|
411 |
if (te->hasFocus())
|
|
412 |
return te;
|
|
413 |
return 0; // This cannot happen
|
|
414 |
}
|
|
415 |
|
|
416 |
QTextEdit *MessageEditor::activeOr1stTranslation() const
|
|
417 |
{
|
|
418 |
if (m_currentNumerus < 0) {
|
|
419 |
for (int i = 0; i < m_editors.size(); ++i)
|
|
420 |
if (m_editors[i].container->isVisible()
|
|
421 |
&& !m_editors[i].transTexts.first()->getEditors().first()->isReadOnly())
|
|
422 |
return m_editors[i].transTexts.first()->getEditors().first();
|
|
423 |
return 0;
|
|
424 |
}
|
|
425 |
return activeTranslation();
|
|
426 |
}
|
|
427 |
|
|
428 |
QTextEdit *MessageEditor::activeTransComment() const
|
|
429 |
{
|
|
430 |
if (m_currentModel < 0 || m_currentNumerus >= 0)
|
|
431 |
return 0;
|
|
432 |
return m_editors[m_currentModel].transCommentText->getEditor();
|
|
433 |
}
|
|
434 |
|
|
435 |
QTextEdit *MessageEditor::activeEditor() const
|
|
436 |
{
|
|
437 |
if (QTextEdit *te = activeTransComment())
|
|
438 |
return te;
|
|
439 |
return activeTranslation();
|
|
440 |
}
|
|
441 |
|
|
442 |
QTextEdit *MessageEditor::activeOr1stEditor() const
|
|
443 |
{
|
|
444 |
if (QTextEdit *te = activeTransComment())
|
|
445 |
return te;
|
|
446 |
return activeOr1stTranslation();
|
|
447 |
}
|
|
448 |
|
|
449 |
void MessageEditor::setTargetLanguage(int model)
|
|
450 |
{
|
|
451 |
const QStringList &numerusForms = m_dataModel->model(model)->numerusForms();
|
|
452 |
const QString &langLocalized = m_dataModel->model(model)->localizedLanguage();
|
|
453 |
for (int i = 0; i < numerusForms.count(); ++i) {
|
|
454 |
const QString &label = tr("%1 translation (%2)").arg(langLocalized, numerusForms[i]);
|
|
455 |
if (!i)
|
|
456 |
m_editors[model].firstForm = label;
|
|
457 |
if (i >= m_editors[model].transTexts.count())
|
|
458 |
addPluralForm(model, label, m_dataModel->isModelWritable(model));
|
|
459 |
else
|
|
460 |
m_editors[model].transTexts[i]->setLabel(label);
|
|
461 |
m_editors[model].transTexts[i]->setVisible(!i || m_editors[model].pluralEditMode);
|
|
462 |
m_editors[model].transTexts[i]->setWhatsThis(
|
|
463 |
tr("This is where you can enter or modify"
|
|
464 |
" the translation of the above source text.") );
|
|
465 |
}
|
|
466 |
for (int j = m_editors[model].transTexts.count() - numerusForms.count(); j > 0; --j)
|
|
467 |
delete m_editors[model].transTexts.takeLast();
|
|
468 |
m_editors[model].invariantForm = tr("%1 translation").arg(langLocalized);
|
|
469 |
m_editors[model].transCommentText->setLabel(tr("%1 translator comments").arg(langLocalized));
|
|
470 |
}
|
|
471 |
|
|
472 |
MessageEditorData *MessageEditor::modelForWidget(const QObject *o)
|
|
473 |
{
|
|
474 |
for (int j = 0; j < m_editors.count(); ++j) {
|
|
475 |
for (int i = 0; i < m_editors[j].transTexts.count(); ++i)
|
|
476 |
foreach (QTextEdit *te, m_editors[j].transTexts[i]->getEditors())
|
|
477 |
if (te == o)
|
|
478 |
return &m_editors[j];
|
|
479 |
if (m_editors[j].transCommentText->getEditor() == o)
|
|
480 |
return &m_editors[j];
|
|
481 |
}
|
|
482 |
return 0;
|
|
483 |
}
|
|
484 |
|
|
485 |
static bool applyFont(MessageEditorData *med)
|
|
486 |
{
|
|
487 |
QFont font;
|
|
488 |
font.setPointSize(static_cast<int>(med->fontSize));
|
|
489 |
for (int i = 0; i < med->transTexts.count(); ++i)
|
|
490 |
foreach (QTextEdit *te, med->transTexts[i]->getEditors())
|
|
491 |
te->setFont(font);
|
|
492 |
med->transCommentText->getEditor()->setFont(font);
|
|
493 |
return true;
|
|
494 |
}
|
|
495 |
|
|
496 |
static bool incFont(MessageEditorData *med)
|
|
497 |
{
|
|
498 |
if (!med || med->fontSize >= 32)
|
|
499 |
return true;
|
|
500 |
med->fontSize *= 1.2;
|
|
501 |
return applyFont(med);
|
|
502 |
}
|
|
503 |
|
|
504 |
static bool decFont(MessageEditorData *med)
|
|
505 |
{
|
|
506 |
if (!med || med->fontSize <= 8)
|
|
507 |
return true;
|
|
508 |
med->fontSize /= 1.2;
|
|
509 |
return applyFont(med);
|
|
510 |
}
|
|
511 |
|
|
512 |
bool MessageEditor::eventFilter(QObject *o, QEvent *e)
|
|
513 |
{
|
|
514 |
// handle copying from the source
|
|
515 |
if (e->type() == QEvent::ShortcutOverride) {
|
|
516 |
QKeyEvent *ke = static_cast<QKeyEvent *>(e);
|
|
517 |
|
|
518 |
if (ke->modifiers() & Qt::ControlModifier) {
|
|
519 |
if (ke->key() == Qt::Key_C) {
|
|
520 |
if (m_source->getEditor()->textCursor().hasSelection()) {
|
|
521 |
m_source->getEditor()->copy();
|
|
522 |
return true;
|
|
523 |
}
|
|
524 |
if (m_pluralSource->getEditor()->textCursor().hasSelection()) {
|
|
525 |
m_pluralSource->getEditor()->copy();
|
|
526 |
return true;
|
|
527 |
}
|
|
528 |
} else if (ke->key() == Qt::Key_A) {
|
|
529 |
return true;
|
|
530 |
}
|
|
531 |
}
|
|
532 |
} else if (e->type() == QEvent::KeyPress) {
|
|
533 |
QKeyEvent *ke = static_cast<QKeyEvent *>(e);
|
|
534 |
if (ke->modifiers() & Qt::ControlModifier) {
|
|
535 |
if (ke->key() == Qt::Key_Plus || ke->key() == Qt::Key_Equal)
|
|
536 |
return incFont(modelForWidget(o));
|
|
537 |
if (ke->key() == Qt::Key_Minus)
|
|
538 |
return decFont(modelForWidget(o));
|
|
539 |
} else {
|
|
540 |
// Ctrl-Tab is still passed through to the textedit and causes a tab to be inserted.
|
|
541 |
if (ke->key() == Qt::Key_Tab) {
|
|
542 |
focusNextChild();
|
|
543 |
return true;
|
|
544 |
}
|
|
545 |
}
|
|
546 |
} else if (e->type() == QEvent::Wheel) {
|
|
547 |
QWheelEvent *we = static_cast<QWheelEvent *>(e);
|
|
548 |
if (we->modifiers() & Qt::ControlModifier) {
|
|
549 |
if (we->delta() > 0)
|
|
550 |
return incFont(modelForWidget(o));
|
|
551 |
return decFont(modelForWidget(o));
|
|
552 |
}
|
|
553 |
} else if (e->type() == QEvent::FocusIn) {
|
|
554 |
QWidget *widget = static_cast<QWidget *>(o);
|
|
555 |
if (widget != m_focusWidget)
|
|
556 |
trackFocus(widget);
|
|
557 |
}
|
|
558 |
|
|
559 |
return QScrollArea::eventFilter(o, e);
|
|
560 |
}
|
|
561 |
|
|
562 |
void MessageEditor::grabFocus(QWidget *widget)
|
|
563 |
{
|
|
564 |
if (widget != m_focusWidget) {
|
|
565 |
widget->setFocus();
|
|
566 |
trackFocus(widget);
|
|
567 |
}
|
|
568 |
}
|
|
569 |
|
|
570 |
void MessageEditor::trackFocus(QWidget *widget)
|
|
571 |
{
|
|
572 |
m_focusWidget = widget;
|
|
573 |
|
|
574 |
int model, numerus;
|
|
575 |
activeModelAndNumerus(&model, &numerus);
|
|
576 |
if (model != m_currentModel || numerus != m_currentNumerus) {
|
|
577 |
resetSelection();
|
|
578 |
m_currentModel = model;
|
|
579 |
m_currentNumerus = numerus;
|
|
580 |
emit activeModelChanged(activeModel());
|
|
581 |
updateBeginFromSource();
|
|
582 |
updateUndoRedo();
|
|
583 |
updateCanPaste();
|
|
584 |
}
|
|
585 |
}
|
|
586 |
|
|
587 |
void MessageEditor::showNothing()
|
|
588 |
{
|
|
589 |
m_source->clearTranslation();
|
|
590 |
m_pluralSource->clearTranslation();
|
|
591 |
m_commentText->clearTranslation();
|
|
592 |
for (int j = 0; j < m_editors.count(); ++j) {
|
|
593 |
setEditingEnabled(j, false);
|
|
594 |
foreach (FormMultiWidget *widget, m_editors[j].transTexts)
|
|
595 |
widget->clearTranslation();
|
|
596 |
m_editors[j].transCommentText->clearTranslation();
|
|
597 |
}
|
|
598 |
emit pasteAvailable(false);
|
|
599 |
updateBeginFromSource();
|
|
600 |
updateUndoRedo();
|
|
601 |
}
|
|
602 |
|
|
603 |
void MessageEditor::showMessage(const MultiDataIndex &index)
|
|
604 |
{
|
|
605 |
m_currentIndex = index;
|
|
606 |
|
|
607 |
bool hadMsg = false;
|
|
608 |
for (int j = 0; j < m_editors.size(); ++j) {
|
|
609 |
|
|
610 |
MessageEditorData &ed = m_editors[j];
|
|
611 |
|
|
612 |
MessageItem *item = m_dataModel->messageItem(index, j);
|
|
613 |
if (!item) {
|
|
614 |
ed.container->hide();
|
|
615 |
continue;
|
|
616 |
}
|
|
617 |
ed.container->show();
|
|
618 |
|
|
619 |
if (!hadMsg) {
|
|
620 |
|
|
621 |
// Source text form
|
|
622 |
m_source->setTranslation(item->text());
|
|
623 |
m_pluralSource->setTranslation(item->pluralText());
|
|
624 |
// Use location from first non-obsolete message
|
|
625 |
if (!item->fileName().isEmpty()) {
|
|
626 |
QString toolTip = tr("'%1'\nLine: %2").arg(item->fileName(), QString::number(item->lineNumber()));
|
|
627 |
m_source->setToolTip(toolTip);
|
|
628 |
} else {
|
|
629 |
m_source->setToolTip(QLatin1String(""));
|
|
630 |
}
|
|
631 |
|
|
632 |
// Comment field
|
|
633 |
QString commentText = item->comment().simplified();
|
|
634 |
|
|
635 |
if (!item->extraComment().isEmpty()) {
|
|
636 |
if (!commentText.isEmpty())
|
|
637 |
commentText += QLatin1String("\n");
|
|
638 |
commentText += item->extraComment().simplified();
|
|
639 |
}
|
|
640 |
|
|
641 |
m_commentText->setTranslation(commentText);
|
|
642 |
|
|
643 |
hadMsg = true;
|
|
644 |
}
|
|
645 |
|
|
646 |
setEditingEnabled(j, m_dataModel->isModelWritable(j)
|
|
647 |
&& item->message().type() != TranslatorMessage::Obsolete);
|
|
648 |
|
|
649 |
// Translation label
|
|
650 |
ed.pluralEditMode = item->translations().count() > 1;
|
|
651 |
ed.transTexts.first()->setLabel(ed.pluralEditMode ? ed.firstForm : ed.invariantForm);
|
|
652 |
|
|
653 |
// Translation forms
|
|
654 |
if (item->text().isEmpty() && !item->context().isEmpty()) {
|
|
655 |
for (int i = 0; i < ed.transTexts.size(); ++i)
|
|
656 |
ed.transTexts.at(i)->setVisible(false);
|
|
657 |
} else {
|
|
658 |
QStringList normalizedTranslations =
|
|
659 |
m_dataModel->model(j)->normalizedTranslations(*item);
|
|
660 |
for (int i = 0; i < ed.transTexts.size(); ++i) {
|
|
661 |
bool shouldShow = (i < normalizedTranslations.count());
|
|
662 |
if (shouldShow)
|
|
663 |
setTranslation(j, normalizedTranslations.at(i), i);
|
|
664 |
else
|
|
665 |
setTranslation(j, QString(), i);
|
|
666 |
ed.transTexts.at(i)->setVisible(i == 0 || shouldShow);
|
|
667 |
}
|
|
668 |
}
|
|
669 |
|
|
670 |
ed.transCommentText->setTranslation(item->translatorComment().trimmed(), false);
|
|
671 |
}
|
|
672 |
|
|
673 |
updateUndoRedo();
|
|
674 |
}
|
|
675 |
|
|
676 |
void MessageEditor::setTranslation(int model, const QString &translation, int numerus)
|
|
677 |
{
|
|
678 |
MessageEditorData &ed = m_editors[model];
|
|
679 |
if (numerus >= ed.transTexts.count())
|
|
680 |
numerus = 0;
|
|
681 |
FormMultiWidget *transForm = ed.transTexts[numerus];
|
|
682 |
transForm->setTranslation(translation, false);
|
|
683 |
|
|
684 |
updateBeginFromSource();
|
|
685 |
}
|
|
686 |
|
|
687 |
void MessageEditor::setTranslation(int latestModel, const QString &translation)
|
|
688 |
{
|
|
689 |
int numerus;
|
|
690 |
if (m_currentNumerus < 0) {
|
|
691 |
numerus = 0;
|
|
692 |
} else {
|
|
693 |
latestModel = m_currentModel;
|
|
694 |
numerus = m_currentNumerus;
|
|
695 |
}
|
|
696 |
FormMultiWidget *transForm = m_editors[latestModel].transTexts[numerus];
|
|
697 |
transForm->getEditors().first()->setFocus();
|
|
698 |
transForm->setTranslation(translation, true);
|
|
699 |
|
|
700 |
updateBeginFromSource();
|
|
701 |
}
|
|
702 |
|
|
703 |
void MessageEditor::setEditingEnabled(int model, bool enabled)
|
|
704 |
{
|
|
705 |
MessageEditorData &ed = m_editors[model];
|
|
706 |
foreach (FormMultiWidget *widget, ed.transTexts)
|
|
707 |
widget->setEditingEnabled(enabled);
|
|
708 |
ed.transCommentText->setEditingEnabled(enabled);
|
|
709 |
|
|
710 |
updateCanPaste();
|
|
711 |
}
|
|
712 |
|
|
713 |
void MessageEditor::setLengthVariants(bool on)
|
|
714 |
{
|
|
715 |
m_lengthVariants = on;
|
|
716 |
foreach (const MessageEditorData &ed, m_editors)
|
|
717 |
foreach (FormMultiWidget *widget, ed.transTexts)
|
|
718 |
widget->setMultiEnabled(on);
|
|
719 |
}
|
|
720 |
|
|
721 |
void MessageEditor::undo()
|
|
722 |
{
|
|
723 |
activeEditor()->document()->undo();
|
|
724 |
}
|
|
725 |
|
|
726 |
void MessageEditor::redo()
|
|
727 |
{
|
|
728 |
activeEditor()->document()->redo();
|
|
729 |
}
|
|
730 |
|
|
731 |
void MessageEditor::updateUndoRedo()
|
|
732 |
{
|
|
733 |
bool newUndoAvail = false;
|
|
734 |
bool newRedoAvail = false;
|
|
735 |
if (QTextEdit *te = activeEditor()) {
|
|
736 |
QTextDocument *doc = te->document();
|
|
737 |
newUndoAvail = doc->isUndoAvailable();
|
|
738 |
newRedoAvail = doc->isRedoAvailable();
|
|
739 |
}
|
|
740 |
|
|
741 |
if (newUndoAvail != m_undoAvail) {
|
|
742 |
m_undoAvail = newUndoAvail;
|
|
743 |
emit undoAvailable(newUndoAvail);
|
|
744 |
}
|
|
745 |
|
|
746 |
if (newRedoAvail != m_redoAvail) {
|
|
747 |
m_redoAvail = newRedoAvail;
|
|
748 |
emit redoAvailable(newRedoAvail);
|
|
749 |
}
|
|
750 |
}
|
|
751 |
|
|
752 |
void MessageEditor::cut()
|
|
753 |
{
|
|
754 |
m_selectionHolder->cut();
|
|
755 |
}
|
|
756 |
|
|
757 |
void MessageEditor::copy()
|
|
758 |
{
|
|
759 |
m_selectionHolder->copy();
|
|
760 |
}
|
|
761 |
|
|
762 |
void MessageEditor::updateCanCutCopy()
|
|
763 |
{
|
|
764 |
bool newCopyState = false;
|
|
765 |
bool newCutState = false;
|
|
766 |
|
|
767 |
if (m_selectionHolder) {
|
|
768 |
newCopyState = true;
|
|
769 |
newCutState = !m_selectionHolder->isReadOnly();
|
|
770 |
}
|
|
771 |
|
|
772 |
if (newCopyState != m_copyAvail) {
|
|
773 |
m_copyAvail = newCopyState;
|
|
774 |
emit copyAvailable(m_copyAvail);
|
|
775 |
}
|
|
776 |
|
|
777 |
if (newCutState != m_cutAvail) {
|
|
778 |
m_cutAvail = newCutState;
|
|
779 |
emit cutAvailable(m_cutAvail);
|
|
780 |
}
|
|
781 |
}
|
|
782 |
|
|
783 |
void MessageEditor::paste()
|
|
784 |
{
|
|
785 |
activeEditor()->paste();
|
|
786 |
}
|
|
787 |
|
|
788 |
void MessageEditor::updateCanPaste()
|
|
789 |
{
|
|
790 |
QTextEdit *te;
|
|
791 |
emit pasteAvailable(!m_clipboardEmpty
|
|
792 |
&& (te = activeEditor()) && !te->isReadOnly());
|
|
793 |
}
|
|
794 |
|
|
795 |
void MessageEditor::clipboardChanged()
|
|
796 |
{
|
|
797 |
// this is expensive, so move it out of the common path in updateCanPaste
|
|
798 |
m_clipboardEmpty = qApp->clipboard()->text().isNull();
|
|
799 |
updateCanPaste();
|
|
800 |
}
|
|
801 |
|
|
802 |
void MessageEditor::selectAll()
|
|
803 |
{
|
|
804 |
// make sure we don't select the selection of a translator textedit,
|
|
805 |
// if we really want the source text editor to be selected.
|
|
806 |
QTextEdit *te;
|
|
807 |
if ((te = m_source->getEditor())->underMouse()
|
|
808 |
|| (te = m_pluralSource->getEditor())->underMouse()
|
|
809 |
|| ((te = activeEditor()) && te->hasFocus()))
|
|
810 |
te->selectAll();
|
|
811 |
}
|
|
812 |
|
|
813 |
void MessageEditor::emitTranslationChanged(QTextEdit *widget)
|
|
814 |
{
|
|
815 |
grabFocus(widget); // DND proofness
|
|
816 |
updateBeginFromSource();
|
|
817 |
updateUndoRedo();
|
|
818 |
emit translationChanged(translations(m_currentModel));
|
|
819 |
}
|
|
820 |
|
|
821 |
void MessageEditor::emitTranslatorCommentChanged(QTextEdit *widget)
|
|
822 |
{
|
|
823 |
grabFocus(widget); // DND proofness
|
|
824 |
updateUndoRedo();
|
|
825 |
emit translatorCommentChanged(m_editors[m_currentModel].transCommentText->getTranslation());
|
|
826 |
}
|
|
827 |
|
|
828 |
void MessageEditor::updateBeginFromSource()
|
|
829 |
{
|
|
830 |
bool overwrite = false;
|
|
831 |
if (QTextEdit *activeEditor = activeTranslation())
|
|
832 |
overwrite = !activeEditor->isReadOnly()
|
|
833 |
&& activeEditor->toPlainText().trimmed().isEmpty();
|
|
834 |
emit beginFromSourceAvailable(overwrite);
|
|
835 |
}
|
|
836 |
|
|
837 |
void MessageEditor::beginFromSource()
|
|
838 |
{
|
|
839 |
MessageItem *item = m_dataModel->messageItem(m_currentIndex, m_currentModel);
|
|
840 |
setTranslation(m_currentModel,
|
|
841 |
m_currentNumerus > 0 && !item->pluralText().isEmpty() ?
|
|
842 |
item->pluralText() : item->text());
|
|
843 |
}
|
|
844 |
|
|
845 |
void MessageEditor::setEditorFocus()
|
|
846 |
{
|
|
847 |
if (!widget()->hasFocus())
|
|
848 |
if (QTextEdit *activeEditor = activeOr1stEditor())
|
|
849 |
activeEditor->setFocus();
|
|
850 |
}
|
|
851 |
|
|
852 |
void MessageEditor::setEditorFocus(int model)
|
|
853 |
{
|
|
854 |
if (m_currentModel != model) {
|
|
855 |
if (model < 0) {
|
|
856 |
resetSelection();
|
|
857 |
m_currentNumerus = -1;
|
|
858 |
m_currentModel = -1;
|
|
859 |
m_focusWidget = 0;
|
|
860 |
emit activeModelChanged(activeModel());
|
|
861 |
updateBeginFromSource();
|
|
862 |
updateUndoRedo();
|
|
863 |
updateCanPaste();
|
|
864 |
} else {
|
|
865 |
m_editors[model].transTexts.first()->getEditors().first()->setFocus();
|
|
866 |
}
|
|
867 |
}
|
|
868 |
}
|
|
869 |
|
|
870 |
bool MessageEditor::focusNextUnfinished(int start)
|
|
871 |
{
|
|
872 |
for (int j = start; j < m_editors.count(); ++j)
|
|
873 |
if (m_dataModel->isModelWritable(j))
|
|
874 |
if (MessageItem *item = m_dataModel->messageItem(m_currentIndex, j))
|
|
875 |
if (item->type() == TranslatorMessage::Unfinished) {
|
|
876 |
m_editors[j].transTexts.first()->getEditors().first()->setFocus();
|
|
877 |
return true;
|
|
878 |
}
|
|
879 |
return false;
|
|
880 |
}
|
|
881 |
|
|
882 |
void MessageEditor::setUnfinishedEditorFocus()
|
|
883 |
{
|
|
884 |
focusNextUnfinished(0);
|
|
885 |
}
|
|
886 |
|
|
887 |
bool MessageEditor::focusNextUnfinished()
|
|
888 |
{
|
|
889 |
return focusNextUnfinished(m_currentModel + 1);
|
|
890 |
}
|
|
891 |
|
|
892 |
QT_END_NAMESPACE
|