author | eckhart.koppen@nokia.com |
Wed, 31 Mar 2010 11:06:36 +0300 | |
changeset 7 | f7bc934e204c |
parent 0 | 1918ee327afb |
permissions | -rw-r--r-- |
0 | 1 |
/**************************************************************************** |
2 |
** |
|
7
f7bc934e204c
5cabc75a39ca2f064f70b40f72ed93c74c4dc19b
eckhart.koppen@nokia.com
parents:
0
diff
changeset
|
3 |
** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). |
0 | 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 |
#include "messageeditorwidgets.h" |
|
43 |
#include "messagehighlighter.h" |
|
44 |
||
45 |
#include <translator.h> |
|
46 |
||
47 |
#include <QAbstractTextDocumentLayout> |
|
48 |
#include <QAction> |
|
49 |
#include <QApplication> |
|
50 |
#include <QClipboard> |
|
51 |
#include <QDebug> |
|
52 |
#include <QLayout> |
|
53 |
#include <QMenu> |
|
54 |
#include <QMessageBox> |
|
55 |
#include <QPainter> |
|
56 |
#include <QScrollArea> |
|
57 |
#include <QTextBlock> |
|
58 |
#include <QTextDocumentFragment> |
|
59 |
#include <QToolButton> |
|
60 |
#include <QVBoxLayout> |
|
61 |
||
62 |
QT_BEGIN_NAMESPACE |
|
63 |
||
64 |
ExpandingTextEdit::ExpandingTextEdit(QWidget *parent) |
|
65 |
: QTextEdit(parent) |
|
66 |
{ |
|
67 |
setSizePolicy(QSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::Expanding)); |
|
68 |
||
69 |
setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff); |
|
70 |
setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff); |
|
71 |
||
72 |
QAbstractTextDocumentLayout *docLayout = document()->documentLayout(); |
|
73 |
connect(docLayout, SIGNAL(documentSizeChanged(QSizeF)), SLOT(updateHeight(QSizeF))); |
|
74 |
connect(this, SIGNAL(cursorPositionChanged()), this, SLOT(reallyEnsureCursorVisible())); |
|
75 |
||
76 |
m_minimumHeight = qRound(docLayout->documentSize().height()) + frameWidth() * 2; |
|
77 |
} |
|
78 |
||
79 |
void ExpandingTextEdit::updateHeight(const QSizeF &documentSize) |
|
80 |
{ |
|
81 |
m_minimumHeight = qRound(documentSize.height()) + frameWidth() * 2; |
|
82 |
updateGeometry(); |
|
83 |
} |
|
84 |
||
85 |
QSize ExpandingTextEdit::sizeHint() const |
|
86 |
{ |
|
87 |
return QSize(100, m_minimumHeight); |
|
88 |
} |
|
89 |
||
90 |
QSize ExpandingTextEdit::minimumSizeHint() const |
|
91 |
{ |
|
92 |
return QSize(100, m_minimumHeight); |
|
93 |
} |
|
94 |
||
95 |
void ExpandingTextEdit::reallyEnsureCursorVisible() |
|
96 |
{ |
|
97 |
QObject *ancestor = parent(); |
|
98 |
while (ancestor) { |
|
99 |
QScrollArea *scrollArea = qobject_cast<QScrollArea*>(ancestor); |
|
100 |
if (scrollArea && |
|
101 |
(scrollArea->verticalScrollBarPolicy() != Qt::ScrollBarAlwaysOff && |
|
102 |
scrollArea->horizontalScrollBarPolicy() != Qt::ScrollBarAlwaysOff)) { |
|
103 |
const QRect &r = cursorRect(); |
|
104 |
const QPoint &c = mapTo(scrollArea->widget(), r.center()); |
|
105 |
scrollArea->ensureVisible(c.x(), c.y()); |
|
106 |
break; |
|
107 |
} |
|
108 |
ancestor = ancestor->parent(); |
|
109 |
} |
|
110 |
} |
|
111 |
||
112 |
FormatTextEdit::FormatTextEdit(QWidget *parent) |
|
113 |
: ExpandingTextEdit(parent) |
|
114 |
{ |
|
115 |
setLineWrapMode(QTextEdit::WidgetWidth); |
|
116 |
setAcceptRichText(false); |
|
117 |
QTextOption option = document()->defaultTextOption(); |
|
118 |
option.setFlags(option.flags() |
|
119 |
| QTextOption::ShowLineAndParagraphSeparators |
|
120 |
| QTextOption::ShowTabsAndSpaces); |
|
121 |
document()->setDefaultTextOption(option); |
|
122 |
||
123 |
// Do not set different background if disabled |
|
124 |
QPalette p = palette(); |
|
125 |
p.setColor(QPalette::Disabled, QPalette::Base, p.color(QPalette::Active, QPalette::Base)); |
|
126 |
setPalette(p); |
|
127 |
||
128 |
setEditable(true); |
|
129 |
||
130 |
m_highlighter = new MessageHighlighter(this); |
|
131 |
} |
|
132 |
||
133 |
void FormatTextEdit::setEditable(bool editable) |
|
134 |
{ |
|
135 |
// save default frame style |
|
136 |
static int framed = frameStyle(); |
|
137 |
static Qt::FocusPolicy defaultFocus = focusPolicy(); |
|
138 |
||
139 |
if (editable) { |
|
140 |
setFrameStyle(framed); |
|
141 |
setFocusPolicy(defaultFocus); |
|
142 |
} else { |
|
143 |
setFrameStyle(QFrame::NoFrame | QFrame::Plain); |
|
144 |
setFocusPolicy(Qt::NoFocus); |
|
145 |
} |
|
146 |
||
147 |
setReadOnly(!editable); |
|
148 |
} |
|
149 |
||
150 |
void FormatTextEdit::setPlainText(const QString &text, bool userAction) |
|
151 |
{ |
|
152 |
if (!userAction) { |
|
153 |
// Prevent contentsChanged signal |
|
154 |
bool oldBlockState = blockSignals(true); |
|
155 |
document()->setUndoRedoEnabled(false); |
|
156 |
ExpandingTextEdit::setPlainText(text); |
|
157 |
// highlighter is out of sync because of blocked signals |
|
158 |
m_highlighter->rehighlight(); |
|
159 |
document()->setUndoRedoEnabled(true); |
|
160 |
blockSignals(oldBlockState); |
|
161 |
} else { |
|
162 |
ExpandingTextEdit::setPlainText(text); |
|
163 |
} |
|
164 |
} |
|
165 |
||
166 |
FormWidget::FormWidget(const QString &label, bool isEditable, QWidget *parent) |
|
167 |
: QWidget(parent), |
|
168 |
m_hideWhenEmpty(false) |
|
169 |
{ |
|
170 |
QVBoxLayout *layout = new QVBoxLayout; |
|
171 |
layout->setMargin(0); |
|
172 |
||
173 |
m_label = new QLabel(this); |
|
7
f7bc934e204c
5cabc75a39ca2f064f70b40f72ed93c74c4dc19b
eckhart.koppen@nokia.com
parents:
0
diff
changeset
|
174 |
QFont fnt; |
f7bc934e204c
5cabc75a39ca2f064f70b40f72ed93c74c4dc19b
eckhart.koppen@nokia.com
parents:
0
diff
changeset
|
175 |
fnt.setBold(true); |
f7bc934e204c
5cabc75a39ca2f064f70b40f72ed93c74c4dc19b
eckhart.koppen@nokia.com
parents:
0
diff
changeset
|
176 |
m_label->setFont(fnt); |
0 | 177 |
m_label->setText(label); |
178 |
layout->addWidget(m_label); |
|
179 |
||
180 |
m_editor = new FormatTextEdit(this); |
|
181 |
m_editor->setEditable(isEditable); |
|
182 |
//m_textEdit->setWhatsThis(tr("This area shows text from an auxillary translation.")); |
|
183 |
layout->addWidget(m_editor); |
|
184 |
||
185 |
setLayout(layout); |
|
186 |
||
187 |
connect(m_editor, SIGNAL(textChanged()), SLOT(slotTextChanged())); |
|
188 |
connect(m_editor, SIGNAL(selectionChanged()), SLOT(slotSelectionChanged())); |
|
189 |
connect(m_editor, SIGNAL(cursorPositionChanged()), SIGNAL(cursorPositionChanged())); |
|
190 |
} |
|
191 |
||
192 |
void FormWidget::slotTextChanged() |
|
193 |
{ |
|
194 |
emit textChanged(m_editor); |
|
195 |
} |
|
196 |
||
197 |
void FormWidget::slotSelectionChanged() |
|
198 |
{ |
|
199 |
emit selectionChanged(m_editor); |
|
200 |
} |
|
201 |
||
202 |
void FormWidget::setTranslation(const QString &text, bool userAction) |
|
203 |
{ |
|
204 |
m_editor->setPlainText(text, userAction); |
|
205 |
if (m_hideWhenEmpty) |
|
206 |
setHidden(text.isEmpty()); |
|
207 |
} |
|
208 |
||
209 |
void FormWidget::setEditingEnabled(bool enable) |
|
210 |
{ |
|
211 |
// Use read-only state so that the text can still be copied |
|
212 |
m_editor->setReadOnly(!enable); |
|
213 |
m_label->setEnabled(enable); |
|
214 |
} |
|
215 |
||
216 |
||
217 |
class ButtonWrapper : public QWidget |
|
218 |
{ |
|
219 |
// no Q_OBJECT: no need to, and don't want the useless moc file |
|
220 |
||
221 |
public: |
|
222 |
ButtonWrapper(QWidget *wrapee, QWidget *relator) : m_wrapee(wrapee) |
|
223 |
{ |
|
224 |
setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Ignored); |
|
225 |
QBoxLayout *box = new QVBoxLayout; |
|
226 |
box->setMargin(0); |
|
227 |
setLayout(box); |
|
228 |
box->addWidget(wrapee, 0, Qt::AlignBottom); |
|
229 |
if (relator) |
|
230 |
relator->installEventFilter(this); |
|
231 |
} |
|
232 |
||
233 |
protected: |
|
234 |
virtual bool eventFilter(QObject *object, QEvent *event) |
|
235 |
{ |
|
236 |
if (event->type() == QEvent::Resize) { |
|
237 |
QWidget *relator = static_cast<QWidget *>(object); |
|
238 |
setFixedHeight((relator->height() + layout()->spacing() + m_wrapee->height()) / 2); |
|
239 |
} |
|
240 |
return false; |
|
241 |
} |
|
242 |
||
243 |
private: |
|
244 |
QWidget *m_wrapee; |
|
245 |
}; |
|
246 |
||
247 |
FormMultiWidget::FormMultiWidget(const QString &label, QWidget *parent) |
|
248 |
: QWidget(parent), |
|
249 |
m_hideWhenEmpty(false), |
|
250 |
m_multiEnabled(false), |
|
251 |
m_plusIcon(QIcon(QLatin1String(":/images/plus.png"))), // make static |
|
252 |
m_minusIcon(QIcon(QLatin1String(":/images/minus.png"))) |
|
253 |
{ |
|
254 |
m_label = new QLabel(this); |
|
7
f7bc934e204c
5cabc75a39ca2f064f70b40f72ed93c74c4dc19b
eckhart.koppen@nokia.com
parents:
0
diff
changeset
|
255 |
QFont fnt; |
f7bc934e204c
5cabc75a39ca2f064f70b40f72ed93c74c4dc19b
eckhart.koppen@nokia.com
parents:
0
diff
changeset
|
256 |
fnt.setBold(true); |
f7bc934e204c
5cabc75a39ca2f064f70b40f72ed93c74c4dc19b
eckhart.koppen@nokia.com
parents:
0
diff
changeset
|
257 |
m_label->setFont(fnt); |
0 | 258 |
m_label->setText(label); |
259 |
||
260 |
m_plusButtons.append( |
|
261 |
new ButtonWrapper(makeButton(m_plusIcon, SLOT(plusButtonClicked())), 0)); |
|
262 |
} |
|
263 |
||
264 |
QAbstractButton *FormMultiWidget::makeButton(const QIcon &icon, const char *slot) |
|
265 |
{ |
|
266 |
QAbstractButton *btn = new QToolButton(this); |
|
267 |
btn->setIcon(icon); |
|
268 |
btn->setFixedSize(icon.availableSizes().first() /* + something */); |
|
269 |
btn->setFocusPolicy(Qt::NoFocus); |
|
270 |
connect(btn, SIGNAL(clicked()), slot); |
|
271 |
return btn; |
|
272 |
} |
|
273 |
||
274 |
void FormMultiWidget::addEditor(int idx) |
|
275 |
{ |
|
276 |
FormatTextEdit *editor = new FormatTextEdit(this); |
|
277 |
m_editors.insert(idx, editor); |
|
278 |
||
279 |
m_minusButtons.insert(idx, makeButton(m_minusIcon, SLOT(minusButtonClicked()))); |
|
280 |
m_plusButtons.insert(idx + 1, |
|
281 |
new ButtonWrapper(makeButton(m_plusIcon, SLOT(plusButtonClicked())), editor)); |
|
282 |
||
283 |
connect(editor, SIGNAL(textChanged()), SLOT(slotTextChanged())); |
|
284 |
connect(editor, SIGNAL(selectionChanged()), SLOT(slotSelectionChanged())); |
|
285 |
connect(editor, SIGNAL(cursorPositionChanged()), SIGNAL(cursorPositionChanged())); |
|
286 |
editor->installEventFilter(this); |
|
287 |
||
288 |
emit editorCreated(editor); |
|
289 |
} |
|
290 |
||
291 |
bool FormMultiWidget::eventFilter(QObject *watched, QEvent *event) |
|
292 |
{ |
|
293 |
int i = 0; |
|
294 |
while (m_editors.at(i) != watched) |
|
295 |
if (++i >= m_editors.count()) // Happens when deleting an editor |
|
296 |
return false; |
|
297 |
if (event->type() == QEvent::FocusOut) { |
|
298 |
m_minusButtons.at(i)->setToolTip(QString()); |
|
299 |
m_plusButtons.at(i)->setToolTip(QString()); |
|
300 |
m_plusButtons.at(i + 1)->setToolTip(QString()); |
|
301 |
} else if (event->type() == QEvent::FocusIn) { |
|
302 |
m_minusButtons.at(i)->setToolTip(/*: translate, but don't change */ tr("Alt+Delete")); |
|
303 |
m_plusButtons.at(i)->setToolTip(/*: translate, but don't change */ tr("Shift+Alt+Insert")); |
|
304 |
m_plusButtons.at(i + 1)->setToolTip(/*: translate, but don't change */ tr("Alt+Insert")); |
|
305 |
} else if (event->type() == QEvent::KeyPress) { |
|
306 |
QKeyEvent *ke = static_cast<QKeyEvent *>(event); |
|
307 |
if (ke->modifiers() & Qt::AltModifier) { |
|
308 |
if (ke->key() == Qt::Key_Delete) { |
|
309 |
deleteEditor(i); |
|
310 |
return true; |
|
311 |
} else if (ke->key() == Qt::Key_Insert) { |
|
312 |
if (!(ke->modifiers() & Qt::ShiftModifier)) |
|
313 |
++i; |
|
314 |
insertEditor(i); |
|
315 |
return true; |
|
316 |
} |
|
317 |
} |
|
318 |
} |
|
319 |
return false; |
|
320 |
} |
|
321 |
||
322 |
void FormMultiWidget::updateLayout() |
|
323 |
{ |
|
324 |
delete layout(); |
|
325 |
||
326 |
QGridLayout *layout = new QGridLayout; |
|
327 |
layout->setMargin(0); |
|
328 |
setLayout(layout); |
|
329 |
||
330 |
bool variants = m_multiEnabled && m_label->isEnabled(); |
|
331 |
||
332 |
layout->addWidget(m_label, 0, 0, 1, variants ? 3 : 1); |
|
333 |
||
334 |
for (int i = 0; i < m_plusButtons.count(); ++i) { |
|
335 |
if (variants) |
|
336 |
layout->addWidget(m_plusButtons.at(i), 1 + i * 2, 0, 2, 1, Qt::AlignTop); |
|
337 |
m_plusButtons.at(i)->setVisible(variants); |
|
338 |
} |
|
339 |
for (int j = 0; j < m_minusButtons.count(); ++j) { |
|
340 |
if (variants) |
|
341 |
layout->addWidget(m_minusButtons.at(j), 2 + j * 2, 2, 2, 1, Qt::AlignVCenter); |
|
342 |
m_minusButtons.at(j)->setVisible(variants); |
|
343 |
} |
|
344 |
for (int k = 0; k < m_editors.count(); ++k) |
|
345 |
layout->addWidget(m_editors.at(k), 2 + k * 2, variants ? 1 : 0, 2, 1, Qt::AlignVCenter); |
|
346 |
||
347 |
updateGeometry(); |
|
348 |
} |
|
349 |
||
350 |
void FormMultiWidget::slotTextChanged() |
|
351 |
{ |
|
352 |
emit textChanged(static_cast<QTextEdit *>(sender())); |
|
353 |
} |
|
354 |
||
355 |
void FormMultiWidget::slotSelectionChanged() |
|
356 |
{ |
|
357 |
emit selectionChanged(static_cast<QTextEdit *>(sender())); |
|
358 |
} |
|
359 |
||
360 |
void FormMultiWidget::setTranslation(const QString &text, bool userAction) |
|
361 |
{ |
|
362 |
QStringList texts = text.split(QChar(Translator::BinaryVariantSeparator), QString::KeepEmptyParts); |
|
363 |
||
364 |
while (m_editors.count() > texts.count()) { |
|
365 |
delete m_minusButtons.takeLast(); |
|
366 |
delete m_plusButtons.takeLast(); |
|
367 |
delete m_editors.takeLast(); |
|
368 |
} |
|
369 |
while (m_editors.count() < texts.count()) |
|
370 |
addEditor(m_editors.count()); |
|
371 |
updateLayout(); |
|
372 |
||
373 |
for (int i = 0; i < texts.count(); ++i) |
|
374 |
// XXX this will emit n textChanged signals |
|
375 |
m_editors.at(i)->setPlainText(texts.at(i), userAction); |
|
376 |
||
377 |
if (m_hideWhenEmpty) |
|
378 |
setHidden(text.isEmpty()); |
|
379 |
} |
|
380 |
||
381 |
QString FormMultiWidget::getTranslation() const |
|
382 |
{ |
|
383 |
QString ret; |
|
384 |
for (int i = 0; i < m_editors.count(); ++i) { |
|
385 |
if (i) |
|
386 |
ret += QChar(Translator::BinaryVariantSeparator); |
|
387 |
ret += m_editors.at(i)->toPlainText(); |
|
388 |
} |
|
389 |
return ret; |
|
390 |
} |
|
391 |
||
392 |
void FormMultiWidget::setEditingEnabled(bool enable) |
|
393 |
{ |
|
394 |
// Use read-only state so that the text can still be copied |
|
395 |
for (int i = 0; i < m_editors.count(); ++i) |
|
396 |
m_editors.at(i)->setReadOnly(!enable); |
|
397 |
m_label->setEnabled(enable); |
|
398 |
if (m_multiEnabled) |
|
399 |
updateLayout(); |
|
400 |
} |
|
401 |
||
402 |
void FormMultiWidget::setMultiEnabled(bool enable) |
|
403 |
{ |
|
404 |
m_multiEnabled = enable; |
|
405 |
if (m_label->isEnabled()) |
|
406 |
updateLayout(); |
|
407 |
} |
|
408 |
||
409 |
void FormMultiWidget::minusButtonClicked() |
|
410 |
{ |
|
411 |
int i = 0; |
|
412 |
while (m_minusButtons.at(i) != sender()) |
|
413 |
++i; |
|
414 |
deleteEditor(i); |
|
415 |
} |
|
416 |
||
417 |
void FormMultiWidget::plusButtonClicked() |
|
418 |
{ |
|
419 |
QWidget *btn = static_cast<QAbstractButton *>(sender())->parentWidget(); |
|
420 |
int i = 0; |
|
421 |
while (m_plusButtons.at(i) != btn) |
|
422 |
++i; |
|
423 |
insertEditor(i); |
|
424 |
} |
|
425 |
||
426 |
void FormMultiWidget::deleteEditor(int idx) |
|
427 |
{ |
|
428 |
if (m_editors.count() == 1) { |
|
429 |
// Don't just clear(), so the undo history is not lost |
|
430 |
QTextCursor c = m_editors.first()->textCursor(); |
|
431 |
c.select(QTextCursor::Document); |
|
432 |
c.removeSelectedText(); |
|
433 |
} else { |
|
434 |
if (!m_editors.at(idx)->toPlainText().isEmpty()) { |
|
435 |
if (QMessageBox::question(topLevelWidget(), tr("Confirmation - Qt Linguist"), |
|
436 |
tr("Delete non-empty length variant?"), |
|
437 |
QMessageBox::Yes|QMessageBox::No, QMessageBox::Yes) |
|
438 |
!= QMessageBox::Yes) |
|
439 |
return; |
|
440 |
} |
|
441 |
delete m_editors.takeAt(idx); |
|
442 |
delete m_minusButtons.takeAt(idx); |
|
443 |
delete m_plusButtons.takeAt(idx + 1); |
|
444 |
updateLayout(); |
|
445 |
emit textChanged(m_editors.at((m_editors.count() == idx) ? idx - 1 : idx)); |
|
446 |
} |
|
447 |
} |
|
448 |
||
449 |
void FormMultiWidget::insertEditor(int idx) |
|
450 |
{ |
|
451 |
addEditor(idx); |
|
452 |
updateLayout(); |
|
453 |
emit textChanged(m_editors.at(idx)); |
|
454 |
} |
|
455 |
||
456 |
QT_END_NAMESPACE |