]> git.sesse.net Git - kdenlive/blob - src/unicodedialog.cpp
Title Widget: Icon for align:none and Unicode Insert added
[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 /// CONSTANTS
13
14 const int MAX_LENGTH_HEX = 4;
15  
16  
17 /// CONSTRUCTORS/DECONSTRUCTORS
18
19 UnicodeDialog::UnicodeDialog(InputMethod inputMeth) : inputMethod(inputMeth), lastCursorPos(0), lastUnicodeNumber("")
20 {
21         setupUi(this);
22         connect(unicodeNumber, SIGNAL(textChanged(QString)), this, SLOT(slotTextChanged(QString)));
23         connect(unicodeNumber, SIGNAL(returnPressed()), this, SLOT(slotReturnPressed()));
24         
25         switch (inputMethod) {
26                 case InputHex:
27                         unicodeNumber->setMaxLength(MAX_LENGTH_HEX);
28                 break;
29                 
30                 case InputDec:
31                 break;
32         }
33 }
34
35 UnicodeDialog::~UnicodeDialog()
36 {
37 }
38
39
40 /// METHODS
41
42 bool UnicodeDialog::controlCharacter(QString text)
43 {
44         bool isControlCharacter = false;
45         QString t = text.toLower();
46         
47         switch (inputMethod) {
48                 case InputHex:
49                         if (t == "" 
50                                 || (t.length() == 1 && !(t == "9" || t == "a" || t == "d"))
51                                 || (t.length() == 2 && t.at(0) == QChar('1'))) {
52                                 isControlCharacter = true;
53                         }
54                         break;
55                         
56                 case InputDec:
57                         break;
58         }
59         
60         return isControlCharacter;
61 }
62
63 QString UnicodeDialog::trimmedUnicodeNumber(QString text)
64 {
65         while (text.length() > 0 && text.at(0) == QChar('0')) {
66                 text = text.remove(0, 1);
67         }
68         return text;
69 }
70
71 QString UnicodeDialog::unicodeInfo(QString unicode_number)
72 {
73         QString infoText("");
74         QString u = trimmedUnicodeNumber(unicode_number);
75         
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 &amp;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>");
88         }
89         
90         return infoText;
91 }
92
93 QString UnicodeDialog::validateText(QString text)
94 {
95         QRegExp regex("([0-9]|[a-f])", Qt::CaseInsensitive, QRegExp::RegExp2);
96         QString newText = "";
97         int pos = 0;
98         
99         switch (inputMethod) {
100                 case InputHex:
101                         // Remove all characters we don't want
102                         while ((pos = regex.indexIn(text, pos)) != -1) {
103                                 newText += regex.cap(1);
104                                 pos++;
105                         }
106                 break;
107                 
108                 case InputDec:
109                         // TODO
110                 break;
111         }
112         
113         return newText;
114 }
115
116
117 /// SLOTS
118
119 /**
120  * \brief Validates the entered Unicode number and displays its Unicode character.
121  */
122 void UnicodeDialog::slotTextChanged(QString text)
123 {
124         unicodeNumber->blockSignals(true);
125         
126         bool ok;
127         int cursorPos = unicodeNumber->cursorPosition();
128         QString newText = validateText(text);
129         
130         unicodeNumber->setText(newText);
131         unicodeNumber->setCursorPosition(cursorPos);
132         
133         // Get the decimal number as uint to create the QChar from
134         uint value;
135         switch (inputMethod) {
136                 case InputHex:
137                         value = newText.toUInt(&ok, 16);
138                 break;
139                 case InputDec:
140                         value = newText.toUInt(&ok, 10);
141                 break;
142         }
143         
144         if (!ok) {
145                 // Impossible! validateText never fails!
146         }
147         
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);
152         }
153         
154         lastCursorPos = unicodeNumber->cursorPosition();
155         lastUnicodeNumber = newText;
156         
157         labelInfoText->setText(unicodeInfo(newText));
158         unicodeChar->setText(QChar(value));
159         unicodeNumber->blockSignals(false);
160 }
161
162 /**
163  * When return pressed, we return the selected unicode character
164  * if it was not a control character.
165  */
166 void UnicodeDialog::slotReturnPressed() 
167 {
168         QString text = trimmedUnicodeNumber(unicodeNumber->text());
169         if (!controlCharacter(text)) {
170                 emit charSelected(unicodeChar->text());
171         }
172         emit accept();
173 }
174
175 #include "unicodedialog.moc"