]> git.sesse.net Git - kdenlive/blob - src/unicodedialog.cpp
0fcbc253df643901258f06915c4e73a5c1ee13fc
[kdenlive] / src / unicodedialog.cpp
1 /***************************************************************************
2  *   Copyright (C) 2008 by Simon Andreas Eugster (simon.eu@gmail.com)      *
3  *                                                                         *
4  *   This program is free software; you can redistribute it and/or modify  *
5  *   it under the terms of the GNU General Public License as published by  *
6  *   the Free Software Foundation; either version 2 of the License, or     *
7  *   (at your option) any later version.                                   *
8  ***************************************************************************/
9
10 #include "unicodedialog.h"
11
12 #include <QWheelEvent>
13
14 /// CONSTANTS
15
16 const int MAX_LENGTH_HEX = 4;
17 const uint MAX_UNICODE_V1 = 65535;
18
19
20 UnicodeDialog::UnicodeDialog(InputMethod inputMeth, QWidget *parent)
21     : KDialog(parent)
22 {
23     setCaption( i18n("Details") );
24     setButtons( Ok|Cancel );
25     mUnicodeWidget = new UnicodeWidget(inputMeth);
26     connect(mUnicodeWidget, SIGNAL(charSelected(QString)), SIGNAL(charSelected(QString)));
27     setMainWidget(mUnicodeWidget);
28     connect(this, SIGNAL(okClicked()), SLOT(slotAccept()));
29 }
30
31 UnicodeDialog::~UnicodeDialog()
32 {
33 }
34
35 void UnicodeDialog::slotAccept()
36 {
37     mUnicodeWidget->slotReturnPressed();
38     accept();
39 }
40
41
42 /// CONSTRUCTORS/DECONSTRUCTORS
43
44 UnicodeWidget::UnicodeWidget(UnicodeDialog::InputMethod inputMeth, QWidget *parent)
45     : QWidget(parent),
46         inputMethod(inputMeth),
47         m_lastCursorPos(0)
48 {
49     setupUi(this);
50     readChoices();
51     showLastUnicode();
52     connect(unicodeNumber, SIGNAL(textChanged(QString)), this, SLOT(slotTextChanged(QString)));
53     connect(unicodeNumber, SIGNAL(returnPressed()), this, SLOT(slotReturnPressed()));
54     connect(arrowUp, SIGNAL(clicked()), this, SLOT(slotPrevUnicode()));
55     connect(arrowDown, SIGNAL(clicked()), this, SLOT(slotNextUnicode()));
56
57     switch (inputMethod) {
58     case UnicodeDialog::InputHex:
59         unicodeNumber->setMaxLength(MAX_LENGTH_HEX);
60         break;
61
62     case UnicodeDialog::InputDec:
63         break;
64     }
65
66     arrowUp->setShortcut(Qt::Key_Up);
67     arrowDown->setShortcut(Qt::Key_Down);
68     unicode_link->setText(i18n("Information about unicode characters: <a href=\"http://decodeunicode.org\">http://decodeunicode.org</a>"));
69     arrowUp->setToolTip(i18n("Previous Unicode character (Arrow Up)"));
70     arrowDown->setToolTip(i18n("Next Unicode character (Arrow Down)"));
71     unicodeNumber->setToolTip(i18n("Enter your Unicode number here. Allowed characters: [0-9] and [a-f]."));
72     unicodeNumber->selectAll(); // Selection will be reset by setToolTip and similar, so set it here
73
74 }
75
76 UnicodeWidget::~UnicodeWidget()
77 {
78 }
79
80
81 /// METHODS
82
83 void UnicodeWidget::showLastUnicode()
84 {
85     unicodeNumber->setText(m_lastUnicodeNumber);
86     unicodeNumber->selectAll();
87     slotTextChanged(m_lastUnicodeNumber);
88 }
89
90 bool UnicodeWidget::controlCharacter(const QString &text)
91 {
92     bool isControlCharacter = false;
93     QString t = text.toLower();
94
95     switch (inputMethod) {
96     case UnicodeDialog::InputHex:
97         if (t.isEmpty()
98                 || (t.length() == 1 && !(t == "9" || t == "a" || t == "d"))
99                 || (t.length() == 2 && t.at(0) == QChar('1'))) {
100             isControlCharacter = true;
101         }
102         break;
103
104     case UnicodeDialog::InputDec:
105         bool ok;
106         isControlCharacter = controlCharacter(text.toUInt(&ok, 16));
107         break;
108     }
109
110     return isControlCharacter;
111 }
112
113 bool UnicodeWidget::controlCharacter(uint value)
114 {
115     bool isControlCharacter = false;
116
117     if (value < 32 && !(value == 9 || value == 10 || value == 13)) {
118         isControlCharacter = true;
119     }
120     return isControlCharacter;
121
122 }
123
124 QString UnicodeWidget::trimmedUnicodeNumber(QString text)
125 {
126     while (text.length() > 0 && text.at(0) == QChar('0')) {
127         text = text.remove(0, 1);
128     }
129     return text;
130 }
131
132 QString UnicodeWidget::unicodeInfo(const QString &unicode)
133 {
134     QString infoText(i18n("<small>(no character selected)</small>"));
135     if (unicode.length() == 0) return infoText;
136
137     QString u = trimmedUnicodeNumber(unicode).toLower();
138
139     if (controlCharacter(u)) {
140         infoText = i18n("Control character. Cannot be inserted/printed. See <a href=\"http://en.wikipedia.org/wiki/Control_character\">Wikipedia:Control_character</a>");
141     } else if (u == "a") {
142         infoText = i18n("Line Feed (newline character, \\\\n)");
143     } else if (u == "20") {
144         infoText = i18n("Standard space character. (Other space characters: U+00a0, U+2000&#x2013;200b, U+202f)");
145     } else if (u == "a0") {
146         infoText = i18n("No-break space. &amp;nbsp; in HTML. See U+2009 and U+0020.");
147     } else if (u == "ab" || u == "bb" || u == "2039" || u == "203a") {
148         infoText = i18n("<p><strong>&laquo;</strong> (u+00ab, <code>&amp;lfquo;</code> in HTML) and <strong>&raquo;</strong> (u+00bb, <code>&amp;rfquo;</code> in HTML) are called Guillemets or angle quotes. Usage in different countries: France (with non-breaking Space 0x00a0), Switzerland, Germany, Finland and Sweden.</p><p><strong>&lsaquo;</strong> and <strong>&rsaquo;</strong> (U+2039/203a, <code>&amp;lsaquo;/&amp;rsaquo;</code>) are their single quote equivalents.</p><p>See <a href=\"http://en.wikipedia.org/wiki/Guillemets\">Wikipedia:Guillemets</a></p>");
149     } else if (u == "2002") {
150         infoText = i18n("En Space (width of an n)");
151     } else if (u == "2003") {
152         infoText = i18n("Em Space (width of an m)");
153     } else if (u == "2004") {
154         infoText = i18n("Three-Per-Em Space. Width: 1/3 of one <em>em</em>");
155     } else if (u == "2005") {
156         infoText = i18n("Four-Per-Em Space. Width: 1/4 of one <em>em</em>");
157     } else if (u == "2006") {
158         infoText = i18n("Six-Per-Em Space. Width: 1/6 of one <em>em</em>");
159     } else if (u == "2007") {
160         infoText = i18n("Figure space (non-breaking). Width of a digit if digits have fixed width in this font.");
161     } else if (u == "2008") {
162         infoText = i18n("Punctuation Space. Width the same as between a punctuation character and the next character.");
163     } else if (u == "2009") {
164         infoText = i18n("Thin space, in HTML also &amp;thinsp;. See U+202f and <a href=\"http://en.wikipedia.org/wiki/Space_(punctuation)\">Wikipedia:Space_(punctuation)</a>");
165     } else if (u == "200a") {
166         infoText = i18n("Hair Space. Thinner than U+2009.");
167     } else if (u == "2019") {
168         infoText = i18n("Punctuation Apostrophe. Should be used instead of U+0027. See <a href=\"http://en.wikipedia.org/wiki/Apostrophe\">Wikipedia:Apostrophe</a>");
169     } else if (u == "2013") {
170         infoText = i18n("<p>An en Dash (dash of the width of an n).</p><p>Usage examples: In English language for value ranges (1878&#x2013;1903), for relationships/connections (Zurich&#x2013;Dublin). In the German language it is also used (with spaces!) for showing thoughts: &ldquo;Es war &#x2013; wie immer in den Ferien &#x2013; ein regnerischer Tag.</p> <p>See <a href=\"http://en.wikipedia.org/wiki/Dash\">Wikipedia:Dash</a></p>");
171     } else if (u == "2014") {
172         infoText = i18n("<p>An em Dash (dash of the width of an m).</p><p>Usage examples: In English language to mark&#x2014;like here&#x2014;thoughts. Traditionally without spaces. </p><p>See <a href=\"http://en.wikipedia.org/wiki/Dash\">Wikipedia:Dash</a></p>");
173     } else if (u == "202f") {
174         infoText = i18n("<p>Narrow no-break space. Has the same width as U+2009.</p><p>Usage: For units (spaces are marked with U+2423, &#x2423;): 230&#x2423;V, &#x2212;21&#x2423;&deg;C, 50&#x2423;lb, <em>but</em> 90&deg; (no space). In German for abbreviations (like: i.&#x202f;d.&#x202f;R. instead of i.&#xa0;d.&#xa0;R. with U+00a0).</p><p>See <a href=\"http://de.wikipedia.org/wiki/Schmales_Leerzeichen\">Wikipedia:de:Schmales_Leerzeichen</a></p>");
175     } else if (u == "2026") {
176         infoText = i18n("Ellipsis: If text has been left o&#x2026; See <a href=\"http://en.wikipedia.org/wiki/Ellipsis\">Wikipedia:Ellipsis</a>");
177     } else if (u == "2212") {
178         infoText = i18n("Minus sign. For numbers: &#x2212;42");
179     } else if (u == "2423") {
180         infoText = i18n("Open box; stands for a space.");
181     } else if (u == "2669") {
182         infoText = i18n("Quarter note (Am.) or crochet (Brit.). See <a href=\"http://en.wikipedia.org/wiki/Quarter_note\">Wikipedia:Quarter_note</a>");
183     } else if (u == "266a" || u == "266b") {
184         infoText = i18n("Eighth note (Am.) or quaver (Brit.). Half as long as a quarter note (U+2669). See <a href=\"http://en.wikipedia.org/wiki/Eighth_note\">Wikipedia:Eighth_note</a>");
185     } else if (u == "266c") {
186         infoText = i18n("Sixteenth note (Am.) or semiquaver (Brit.). Half as long as an eighth note (U+266a). See <a href=\"http://en.wikipedia.org/wiki/Sixteenth_note\">Wikipedia:Sixteenth_note</a>");
187     } else if (u == "1D162") {
188         infoText = i18n("Thirty-second note (Am.) or demisemiquaver (Brit.). Half as long as a sixteenth note (U+266b). See <a href=\"http://en.wikipedia.org/wiki/Thirty-second_note\">Wikipedia:Thirty-second_note</a>");
189     } else {
190         infoText = i18n("<small>No additional information available for this character.</small>");
191     }
192
193     return infoText;
194 }
195
196 QString UnicodeWidget::validateText(const QString &text)
197 {
198     QRegExp regex("([0-9]|[a-f])", Qt::CaseInsensitive, QRegExp::RegExp2);
199     QString newText;
200     int pos = 0;
201
202     switch (inputMethod) {
203     case UnicodeDialog::InputHex:
204         // Remove all characters we don't want
205         while ((pos = regex.indexIn(text, pos)) != -1) {
206             newText += regex.cap(1);
207             pos++;
208         }
209         break;
210
211     case UnicodeDialog::InputDec:
212         // TODO
213         break;
214     }
215
216     return newText;
217 }
218
219 void UnicodeWidget::updateOverviewChars(uint unicode)
220 {
221     QString left;
222     QString right;
223     uint i;
224
225     for (i = 1; i <= 4; i++) {
226         if (unicode > i && !controlCharacter(unicode - i)) {
227             left = ' ' + left;
228             left = QChar(unicode - i) + left;
229         }
230     }
231
232     for (i = 1; i <= 8; i++) {
233         if (unicode + i <= MAX_UNICODE_V1 && !controlCharacter(unicode + i)) {
234             right += QChar(unicode + i);
235             right += ' ';
236         }
237     }
238
239     leftChars->setText(left);
240     rightChars->setText(right);
241
242 }
243
244 void UnicodeWidget::clearOverviewChars()
245 {
246     leftChars->setText("");
247     rightChars->setText("");
248 }
249
250 QString UnicodeWidget::nextUnicode(const QString &text, Direction direction)
251 {
252     uint value = 0;
253     QString newText;
254     bool ok;
255
256     switch (inputMethod) {
257     case UnicodeDialog::InputHex:
258         value = text.toUInt(&ok, 16);
259         switch (direction) {
260         case Backward:
261             value--;
262             break;
263         default:
264             value++;
265             break;
266         }
267         // Wrapping
268         if (value == (uint) - 1) value = MAX_UNICODE_V1;
269         if (value > MAX_UNICODE_V1) value = 0;
270
271         newText.setNum(value, 16);
272         break;
273
274     case UnicodeDialog::InputDec:
275         break;
276     }
277
278     return newText;
279 }
280
281 void UnicodeWidget::readChoices()
282 {
283     // Get a pointer to a shared configuration instance, then get the TitleWidget group.
284     KSharedConfigPtr config = KGlobal::config();
285     KConfigGroup titleConfig(config, "TitleWidget");
286
287     // Default is 2013 because there is also (perhaps interesting) information.
288     m_lastUnicodeNumber = titleConfig.readEntry("unicode_number", QString("2013"));
289 }
290
291 void UnicodeWidget::writeChoices()
292 {
293     // Get a pointer to a shared configuration instance, then get the TitleWidget group.
294     KSharedConfigPtr config = KGlobal::config();
295     KConfigGroup titleConfig(config, "TitleWidget");
296
297     titleConfig.writeEntry("unicode_number", m_lastUnicodeNumber);
298 }
299
300
301 /// SLOTS
302
303 /**
304  * \brief Validates the entered Unicode number and displays its Unicode character.
305  */
306 void UnicodeWidget::slotTextChanged(const QString &text)
307 {
308     unicodeNumber->blockSignals(true);
309
310     QString newText = validateText(text);
311     if (newText.length() == 0) {
312         unicodeChar->setText("");
313         unicodeNumber->setText("");
314         clearOverviewChars();
315         m_lastCursorPos = 0;
316         m_lastUnicodeNumber = "";
317         labelInfoText->setText(unicodeInfo(""));
318
319     } else {
320
321         int cursorPos = unicodeNumber->cursorPosition();
322
323         unicodeNumber->setText(newText);
324         unicodeNumber->setCursorPosition(cursorPos);
325
326         // Get the decimal number as uint to create the QChar from
327         bool ok;
328         uint value = 0;
329         switch (inputMethod) {
330         case UnicodeDialog::InputHex:
331             value = newText.toUInt(&ok, 16);
332             break;
333         case UnicodeDialog::InputDec:
334             value = newText.toUInt(&ok, 10);
335             break;
336         }
337         updateOverviewChars(value);
338
339         if (!ok) {
340             // Impossible! validateText never fails!
341         }
342
343         // If an invalid character has been entered:
344         // Reset the cursor position because the entered char has been deleted.
345         if (text != newText && newText == m_lastUnicodeNumber) {
346             unicodeNumber->setCursorPosition(m_lastCursorPos);
347         }
348
349         m_lastCursorPos = unicodeNumber->cursorPosition();
350         m_lastUnicodeNumber = newText;
351
352         labelInfoText->setText(unicodeInfo(newText));
353         unicodeChar->setText(QChar(value));
354     }
355
356     unicodeNumber->blockSignals(false);
357 }
358
359 /**
360  * When return pressed, we return the selected unicode character
361  * if it was not a control character.
362  */
363 void UnicodeWidget::slotReturnPressed()
364 {
365     unicodeNumber->setFocus();
366     const QString text = trimmedUnicodeNumber(unicodeNumber->text());
367     if (!controlCharacter(text)) {
368         emit charSelected(unicodeChar->text());
369         writeChoices();
370     }
371 }
372
373 void UnicodeWidget::slotNextUnicode()
374 {
375     const QString text = unicodeNumber->text();
376     unicodeNumber->setText(nextUnicode(text, Forward));
377 }
378
379 void UnicodeWidget::slotPrevUnicode()
380 {
381     const QString text = unicodeNumber->text();
382     unicodeNumber->setText(nextUnicode(text, Backward));
383 }
384
385 void UnicodeWidget::wheelEvent(QWheelEvent * event)
386 {
387     if (frame->underMouse()) {
388         if (event->delta() > 0)
389             slotNextUnicode();
390         else
391             slotPrevUnicode();
392     }
393 }
394
395 #include "unicodedialog.moc"