1 /***************************************************************************
2 * Copyright (C) 2008 by Simon Andreas Eugster (simon.eu@gmail.com) *
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 ***************************************************************************/
10 #include "unicodedialog.h"
12 #include <QWheelEvent>
16 const int MAX_LENGTH_HEX = 4;
17 const uint MAX_UNICODE_V1 = 65535;
20 UnicodeDialog::UnicodeDialog(InputMethod inputMeth, QWidget *parent)
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()));
31 UnicodeDialog::~UnicodeDialog()
35 void UnicodeDialog::slotAccept()
37 mUnicodeWidget->slotReturnPressed();
42 /// CONSTRUCTORS/DECONSTRUCTORS
44 UnicodeWidget::UnicodeWidget(UnicodeDialog::InputMethod inputMeth, QWidget *parent)
46 inputMethod(inputMeth),
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()));
57 switch (inputMethod) {
58 case UnicodeDialog::InputHex:
59 unicodeNumber->setMaxLength(MAX_LENGTH_HEX);
62 case UnicodeDialog::InputDec:
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
75 UnicodeWidget::~UnicodeWidget()
80 void UnicodeWidget::showLastUnicode()
82 unicodeNumber->setText(m_lastUnicodeNumber);
83 unicodeNumber->selectAll();
84 slotTextChanged(m_lastUnicodeNumber);
87 bool UnicodeWidget::controlCharacter(const QString &text)
89 bool isControlCharacter = false;
90 QString t = text.toLower();
92 switch (inputMethod) {
93 case UnicodeDialog::InputHex:
95 || (t.length() == 1 && !(t == "9" || t == "a" || t == "d"))
96 || (t.length() == 2 && t.at(0) == QChar('1'))) {
97 isControlCharacter = true;
101 case UnicodeDialog::InputDec:
103 isControlCharacter = controlCharacter(text.toUInt(&ok, 16));
107 return isControlCharacter;
110 bool UnicodeWidget::controlCharacter(uint value)
112 bool isControlCharacter = false;
114 if (value < 32 && !(value == 9 || value == 10 || value == 13)) {
115 isControlCharacter = true;
117 return isControlCharacter;
121 QString UnicodeWidget::trimmedUnicodeNumber(QString text)
123 while (!text.isEmpty() && text.at(0) == QChar('0')) {
124 text = text.remove(0, 1);
129 QString UnicodeWidget::unicodeInfo(const QString &unicode)
131 QString infoText(i18n("<small>(no character selected)</small>"));
132 if (unicode.length() == 0) return infoText;
134 QString u = trimmedUnicodeNumber(unicode).toLower();
136 if (controlCharacter(u)) {
137 infoText = i18n("Control character. Cannot be inserted/printed. See <a href=\"http://en.wikipedia.org/wiki/Control_character\">Wikipedia:Control_character</a>");
138 } else if (u == "a") {
139 infoText = i18n("Line Feed (newline character, \\\\n)");
140 } else if (u == "20") {
141 infoText = i18n("Standard space character. (Other space characters: U+00a0, U+2000–200b, U+202f)");
142 } else if (u == "a0") {
143 infoText = i18n("No-break space. &nbsp; in HTML. See U+2009 and U+0020.");
144 } else if (u == "ab" || u == "bb" || u == "2039" || u == "203a") {
145 infoText = i18n("<p><strong>«</strong> (u+00ab, <code>&lfquo;</code> in HTML) and <strong>»</strong> (u+00bb, <code>&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>‹</strong> and <strong>›</strong> (U+2039/203a, <code>&lsaquo;/&rsaquo;</code>) are their single quote equivalents.</p><p>See <a href=\"http://en.wikipedia.org/wiki/Guillemets\">Wikipedia:Guillemets</a></p>");
146 } else if (u == "2002") {
147 infoText = i18n("En Space (width of an n)");
148 } else if (u == "2003") {
149 infoText = i18n("Em Space (width of an m)");
150 } else if (u == "2004") {
151 infoText = i18n("Three-Per-Em Space. Width: 1/3 of one <em>em</em>");
152 } else if (u == "2005") {
153 infoText = i18n("Four-Per-Em Space. Width: 1/4 of one <em>em</em>");
154 } else if (u == "2006") {
155 infoText = i18n("Six-Per-Em Space. Width: 1/6 of one <em>em</em>");
156 } else if (u == "2007") {
157 infoText = i18n("Figure space (non-breaking). Width of a digit if digits have fixed width in this font.");
158 } else if (u == "2008") {
159 infoText = i18n("Punctuation Space. Width the same as between a punctuation character and the next character.");
160 } else if (u == "2009") {
161 infoText = i18n("Thin space, in HTML also &thinsp;. See U+202f and <a href=\"http://en.wikipedia.org/wiki/Space_(punctuation)\">Wikipedia:Space_(punctuation)</a>");
162 } else if (u == "200a") {
163 infoText = i18n("Hair Space. Thinner than U+2009.");
164 } else if (u == "2019") {
165 infoText = i18n("Punctuation Apostrophe. Should be used instead of U+0027. See <a href=\"http://en.wikipedia.org/wiki/Apostrophe\">Wikipedia:Apostrophe</a>");
166 } else if (u == "2013") {
167 infoText = i18n("<p>An en Dash (dash of the width of an n).</p><p>Usage examples: In English language for value ranges (1878–1903), for relationships/connections (Zurich–Dublin). In the German language it is also used (with spaces!) for showing thoughts: “Es war – wie immer in den Ferien – ein regnerischer Tag.</p> <p>See <a href=\"http://en.wikipedia.org/wiki/Dash\">Wikipedia:Dash</a></p>");
168 } else if (u == "2014") {
169 infoText = i18n("<p>An em Dash (dash of the width of an m).</p><p>Usage examples: In English language to mark—like here—thoughts. Traditionally without spaces. </p><p>See <a href=\"http://en.wikipedia.org/wiki/Dash\">Wikipedia:Dash</a></p>");
170 } else if (u == "202f") {
171 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, ␣): 230␣V, −21␣°C, 50␣lb, <em>but</em> 90° (no space). In German for abbreviations (like: i. d. R. instead of i. d. R. with U+00a0).</p><p>See <a href=\"http://de.wikipedia.org/wiki/Schmales_Leerzeichen\">Wikipedia:de:Schmales_Leerzeichen</a></p>");
172 } else if (u == "2026") {
173 infoText = i18n("Ellipsis: If text has been left o… See <a href=\"http://en.wikipedia.org/wiki/Ellipsis\">Wikipedia:Ellipsis</a>");
174 } else if (u == "2212") {
175 infoText = i18n("Minus sign. For numbers: −42");
176 } else if (u == "2423") {
177 infoText = i18n("Open box; stands for a space.");
178 } else if (u == "2669") {
179 infoText = i18n("Quarter note (Am.) or crochet (Brit.). See <a href=\"http://en.wikipedia.org/wiki/Quarter_note\">Wikipedia:Quarter_note</a>");
180 } else if (u == "266a" || u == "266b") {
181 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>");
182 } else if (u == "266c") {
183 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>");
184 } else if (u == "1D162") {
185 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>");
187 infoText = i18n("<small>No additional information available for this character.</small>");
193 QString UnicodeWidget::validateText(const QString &text)
195 QRegExp regex("([0-9]|[a-f])", Qt::CaseInsensitive, QRegExp::RegExp2);
199 switch (inputMethod) {
200 case UnicodeDialog::InputHex:
201 // Remove all characters we don't want
202 while ((pos = regex.indexIn(text, pos)) != -1) {
203 newText += regex.cap(1);
208 case UnicodeDialog::InputDec:
216 void UnicodeWidget::updateOverviewChars(uint unicode)
222 for (i = 1; i <= 4; ++i) {
223 if (unicode > i && !controlCharacter(unicode - i)) {
225 left = QChar(unicode - i) + left;
229 for (i = 1; i <= 8; ++i) {
230 if (unicode + i <= MAX_UNICODE_V1 && !controlCharacter(unicode + i)) {
231 right += QChar(unicode + i);
236 leftChars->setText(left);
237 rightChars->setText(right);
241 void UnicodeWidget::clearOverviewChars()
243 leftChars->setText("");
244 rightChars->setText("");
247 QString UnicodeWidget::nextUnicode(const QString &text, Direction direction)
253 switch (inputMethod) {
254 case UnicodeDialog::InputHex:
255 value = text.toUInt(&ok, 16);
265 if (value == (uint) - 1) value = MAX_UNICODE_V1;
266 if (value > MAX_UNICODE_V1) value = 0;
268 newText.setNum(value, 16);
271 case UnicodeDialog::InputDec:
278 void UnicodeWidget::readChoices()
280 // Get a pointer to a shared configuration instance, then get the TitleWidget group.
281 KSharedConfigPtr config = KGlobal::config();
282 KConfigGroup titleConfig(config, "TitleWidget");
284 // Default is 2013 because there is also (perhaps interesting) information.
285 m_lastUnicodeNumber = titleConfig.readEntry("unicode_number", QString("2013"));
288 void UnicodeWidget::writeChoices()
290 // Get a pointer to a shared configuration instance, then get the TitleWidget group.
291 KSharedConfigPtr config = KGlobal::config();
292 KConfigGroup titleConfig(config, "TitleWidget");
294 titleConfig.writeEntry("unicode_number", m_lastUnicodeNumber);
301 * \brief Validates the entered Unicode number and displays its Unicode character.
303 void UnicodeWidget::slotTextChanged(const QString &text)
305 unicodeNumber->blockSignals(true);
307 QString newText = validateText(text);
308 if (newText.length() == 0) {
309 unicodeChar->setText("");
310 unicodeNumber->setText("");
311 clearOverviewChars();
313 m_lastUnicodeNumber = "";
314 labelInfoText->setText(unicodeInfo(""));
318 int cursorPos = unicodeNumber->cursorPosition();
320 unicodeNumber->setText(newText);
321 unicodeNumber->setCursorPosition(cursorPos);
323 // Get the decimal number as uint to create the QChar from
326 switch (inputMethod) {
327 case UnicodeDialog::InputHex:
328 value = newText.toUInt(&ok, 16);
330 case UnicodeDialog::InputDec:
331 value = newText.toUInt(&ok, 10);
334 updateOverviewChars(value);
337 // Impossible! validateText never fails!
340 // If an invalid character has been entered:
341 // Reset the cursor position because the entered char has been deleted.
342 if (text != newText && newText == m_lastUnicodeNumber) {
343 unicodeNumber->setCursorPosition(m_lastCursorPos);
346 m_lastCursorPos = unicodeNumber->cursorPosition();
347 m_lastUnicodeNumber = newText;
349 labelInfoText->setText(unicodeInfo(newText));
350 unicodeChar->setText(QChar(value));
353 unicodeNumber->blockSignals(false);
357 * When return pressed, we return the selected unicode character
358 * if it was not a control character.
360 void UnicodeWidget::slotReturnPressed()
362 unicodeNumber->setFocus();
363 const QString text = trimmedUnicodeNumber(unicodeNumber->text());
364 if (!controlCharacter(text)) {
365 emit charSelected(unicodeChar->text());
370 void UnicodeWidget::slotNextUnicode()
372 const QString text = unicodeNumber->text();
373 unicodeNumber->setText(nextUnicode(text, Forward));
376 void UnicodeWidget::slotPrevUnicode()
378 const QString text = unicodeNumber->text();
379 unicodeNumber->setText(nextUnicode(text, Backward));
382 void UnicodeWidget::wheelEvent(QWheelEvent * event)
384 if (frame->underMouse()) {
385 if (event->delta() > 0)
392 #include "unicodedialog.moc"