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 QString UnicodeDialog::unicodeInfo(QString unicode_number)
45 QString u = unicode_number;
47 while (unicode_number.at(0) == QChar('0')) {
48 unicode_number = unicode_number.remove(0, 1);
52 // Just a placeholder for reason of ease (shifting around lines)
53 } else if (u == "2009") {
54 infoText = i18n("A thin space, in HTML also &thinsp;. See <a href=\"http://en.wikipedia.org/wiki/Space_(punctuation)\">Wikipedia:Space_(punctuation)</a>");
55 } else if (u == "2019") {
56 infoText = i18n("Punctuation Apostrophe. Should be used instead of U+0027. See <a href=\"http://en.wikipedia.org/wiki/Apostrophe\">Wikipedia:Apostrophe</a>");
57 } else if (u == "2013") {
58 infoText = i18n("An en Dash (dash of the width of an n). See <a href=\"http://en.wikipedia.org/wiki/Dash\">Wikipedia:Dash</a>");
59 } else if (u == "2014") {
60 infoText = i18n("An em Dash (dash of the widht of an m). See <a href=\"http://en.wikipedia.org/wiki/Dash\">Wikipedia:Dash</a>");
61 } else if (u == "2026") {
62 infoText = i18n("Ellipsis: If text has been left out. See <a href=\"http://en.wikipedia.org/wiki/Ellipsis\">Wikipedia:Ellipsis</a>");
69 * Validates an Unicode number.
71 QString UnicodeDialog::validateText(QString text)
73 QRegExp regex("([0-9]|[a-f])", Qt::CaseInsensitive, QRegExp::RegExp2);
77 switch (inputMethod) {
79 while ((pos = regex.indexIn(text, pos)) != -1) {
80 newText += regex.cap(1);
97 * \brief Validates the entered Unicode number and displays its Unicode character.
99 void UnicodeDialog::slotTextChanged(QString text)
101 unicodeNumber->blockSignals(true);
104 int cursorPos = unicodeNumber->cursorPosition();
105 QString newText = validateText(text);
107 unicodeNumber->setText(newText);
108 unicodeNumber->setCursorPosition(cursorPos);
111 switch (inputMethod) {
113 value = newText.toUInt(&ok, 16);
116 value = newText.toUInt(&ok, 10);
124 // If an invalid character has been entered:
125 // Reset the cursor position because the entered char has been deleted.
126 if (text != newText && newText == lastUnicodeNumber) {
127 unicodeNumber->setCursorPosition(lastCursorPos);
130 lastCursorPos = unicodeNumber->cursorPosition();
131 lastUnicodeNumber = newText;
133 labelInfoText->setText(unicodeInfo(newText));
134 unicodeChar->setText(QChar(value));
135 unicodeNumber->blockSignals(false);
138 void UnicodeDialog::slotReturnPressed()
140 emit charSelected(unicodeChar->text());
144 #include "unicodedialog.moc"