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"
14 const int MAX_LENGTH_HEX = 4;
17 /// CONSTRUCTORS/DECONSTRUCTORS
19 UnicodeDialog::UnicodeDialog(InputMethod inputMeth) : inputMethod(inputMeth), lastCursorPos(0), lastUnicodeNumber("")
22 connect(unicodeNumber, SIGNAL(textChanged(QString)), this, SLOT(slotTextChanged(QString)));
23 connect(unicodeNumber, SIGNAL(returnPressed()), this, SLOT(slotReturnPressed()));
25 switch (inputMethod) {
27 unicodeNumber->setMaxLength(MAX_LENGTH_HEX);
35 UnicodeDialog::~UnicodeDialog()
42 bool UnicodeDialog::controlCharacter(QString text)
44 bool isControlCharacter = false;
45 QString t = text.toLower();
47 switch (inputMethod) {
50 || (t.length() == 1 && !(t == "9" || t == "a" || t == "d"))
51 || (t.length() == 2 && t.at(0) == QChar('1'))) {
52 isControlCharacter = true;
60 return isControlCharacter;
63 QString UnicodeDialog::trimmedUnicodeNumber(QString text)
65 while (text.length() > 0 && text.at(0) == QChar('0')) {
66 text = text.remove(0, 1);
71 QString UnicodeDialog::unicodeInfo(QString unicode_number)
74 QString u = trimmedUnicodeNumber(unicode_number);
76 if (controlCharacter(u)) {
77 infoText = i18n("Control character. Cannot be inserted/printed. See <a href=\"http://en.wikipedia.org/wiki/Control_character\">Wikipedia:Control_character</a>");
78 } else if (u == "2009") {
79 infoText = i18n("A thin space, in HTML also &thinsp;. See <a href=\"http://en.wikipedia.org/wiki/Space_(punctuation)\">Wikipedia:Space_(punctuation)</a>");
80 } else if (u == "2019") {
81 infoText = i18n("Punctuation Apostrophe. Should be used instead of U+0027. See <a href=\"http://en.wikipedia.org/wiki/Apostrophe\">Wikipedia:Apostrophe</a>");
82 } else if (u == "2013") {
83 infoText = i18n("An en Dash (dash of the width of an n). See <a href=\"http://en.wikipedia.org/wiki/Dash\">Wikipedia:Dash</a>");
84 } else if (u == "2014") {
85 infoText = i18n("An em Dash (dash of the widht of an m). See <a href=\"http://en.wikipedia.org/wiki/Dash\">Wikipedia:Dash</a>");
86 } else if (u == "2026") {
87 infoText = i18n("Ellipsis: If text has been left out. See <a href=\"http://en.wikipedia.org/wiki/Ellipsis\">Wikipedia:Ellipsis</a>");
93 QString UnicodeDialog::validateText(QString text)
95 QRegExp regex("([0-9]|[a-f])", Qt::CaseInsensitive, QRegExp::RegExp2);
99 switch (inputMethod) {
101 // Remove all characters we don't want
102 while ((pos = regex.indexIn(text, pos)) != -1) {
103 newText += regex.cap(1);
120 * \brief Validates the entered Unicode number and displays its Unicode character.
122 void UnicodeDialog::slotTextChanged(QString text)
124 unicodeNumber->blockSignals(true);
127 int cursorPos = unicodeNumber->cursorPosition();
128 QString newText = validateText(text);
130 unicodeNumber->setText(newText);
131 unicodeNumber->setCursorPosition(cursorPos);
133 // Get the decimal number as uint to create the QChar from
135 switch (inputMethod) {
137 value = newText.toUInt(&ok, 16);
140 value = newText.toUInt(&ok, 10);
145 // Impossible! validateText never fails!
148 // If an invalid character has been entered:
149 // Reset the cursor position because the entered char has been deleted.
150 if (text != newText && newText == lastUnicodeNumber) {
151 unicodeNumber->setCursorPosition(lastCursorPos);
154 lastCursorPos = unicodeNumber->cursorPosition();
155 lastUnicodeNumber = newText;
157 labelInfoText->setText(unicodeInfo(newText));
158 unicodeChar->setText(QChar(value));
159 unicodeNumber->blockSignals(false);
163 * When return pressed, we return the selected unicode character
164 * if it was not a control character.
166 void UnicodeDialog::slotReturnPressed()
168 QString text = trimmedUnicodeNumber(unicodeNumber->text());
169 if (!controlCharacter(text)) {
170 emit charSelected(unicodeChar->text());
175 #include "unicodedialog.moc"