]> git.sesse.net Git - kdenlive/blob - src/unicodedialog.cpp
#596: Added possibility to enter unicode characters in the Title widget.
[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 QString UnicodeDialog::unicodeInfo(QString unicode_number)
43 {
44         QString infoText("");
45         QString u = unicode_number;
46         
47         while (unicode_number.at(0) == QChar('0')) {
48                 unicode_number = unicode_number.remove(0, 1);
49         }
50         
51         if (false) {
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 &amp;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>");
63         }
64         
65         return infoText;
66 }
67
68 /**
69  * Validates an Unicode number.
70  */
71 QString UnicodeDialog::validateText(QString text)
72 {
73         QRegExp regex("([0-9]|[a-f])", Qt::CaseInsensitive, QRegExp::RegExp2);
74         QString newText = "";
75         int pos = 0;
76         
77         switch (inputMethod) {
78                 case InputHex:
79                         while ((pos = regex.indexIn(text, pos)) != -1) {
80                                 newText += regex.cap(1);
81                                 pos++;
82                         }
83                 break;
84                 
85                 case InputDec:
86                         // TODO
87                 break;
88         }
89         
90         return newText;
91 }
92
93
94 /// SLOTS
95
96 /**
97  * \brief Validates the entered Unicode number and displays its Unicode character.
98  */
99 void UnicodeDialog::slotTextChanged(QString text)
100 {
101         unicodeNumber->blockSignals(true);
102         
103         bool ok;
104         int cursorPos = unicodeNumber->cursorPosition();
105         QString newText = validateText(text);
106         
107         unicodeNumber->setText(newText);
108         unicodeNumber->setCursorPosition(cursorPos);
109         
110         uint value;
111         switch (inputMethod) {
112                 case InputHex:
113                         value = newText.toUInt(&ok, 16);
114                 break;
115                 case InputDec:
116                         value = newText.toUInt(&ok, 10);
117                 break;
118         }
119         
120         if (!ok) {
121                 //TODO!
122         }
123         
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);
128         }
129         
130         lastCursorPos = unicodeNumber->cursorPosition();
131         lastUnicodeNumber = newText;
132         
133         labelInfoText->setText(unicodeInfo(newText));
134         unicodeChar->setText(QChar(value));
135         unicodeNumber->blockSignals(false);
136 }
137
138 void UnicodeDialog::slotReturnPressed() 
139 {
140         emit charSelected(unicodeChar->text());
141         emit accept();
142 }
143
144 #include "unicodedialog.moc"