Added tooltip for search limit
[mdictionary] / trunk / src / includes / AccentsNormalizer.h
1 /*******************************************************************************
2
3     This file is part of mDictionary.
4
5     mDictionary is free software: you can redistribute it and/or modify
6     it under the terms of the GNU General Public License as published by
7     the Free Software Foundation, either version 3 of the License, or
8     (at your option) any later version.
9
10     mDictionary is distributed in the hope that it will be useful,
11     but WITHOUT ANY WARRANTY; without even the implied warranty of
12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13     GNU General Public License for more details.
14
15     You should have received a copy of the GNU General Public License
16     along with mDictionary.  If not, see <http://www.gnu.org/licenses/>.
17
18     Copyright 2010 Comarch S.A.
19
20 *******************************************************************************/
21
22 /*! /file AccentsNormalizer.h
23 \brief Common funcionality for all classes that need to remove accents from strings
24
25 \author Bartosz Szatkowski <bulislaw@linux.com>
26 */
27
28 #ifndef ACCENTSNORMALIZER_H
29 #define ACCENTSNORMALIZER_H
30
31 #include <QMap>
32 #include <QString>
33 #include <QRegExp>
34
35
36 class AccentsNormalizer {
37   protected:
38     QMap<QChar, QRegExp> letters;
39     QRegExp noLetter;
40
41     QString removeAccents(QString string) {
42        string = string.toLower();
43        QString normalized = string.normalized(QString::NormalizationForm_KD);
44        foreach(QChar let, letters.keys())
45            normalized.replace(letters[let], QString(let));
46
47        normalized.replace(noLetter, "");
48        //qDebug() << "NORMALIZED: " << normalized;
49        return normalized;
50     }
51     void initAccents() {
52         letters['a'] = QRegExp(QString::fromUtf8("[ÀàÁáÂÂâÃãÄäÅåæÆĀāĂ㥹]"), Qt::CaseInsensitive);
53         letters['c'] = QRegExp(QString::fromUtf8("[ÇçÈçŒĆćĈĉĊċČč]"), Qt::CaseInsensitive);
54         letters['d'] = QRegExp(QString::fromUtf8("[ÐĐđĎď]"), Qt::CaseInsensitive);
55         letters['e'] = QRegExp(QString::fromUtf8("[ÈéèÉÊêËëĒēĔĕĖėĘęĚěē]"), Qt::CaseInsensitive);
56         letters['f'] = QRegExp(QString::fromUtf8("[ſ]"), Qt::CaseInsensitive);
57         letters['g'] = QRegExp(QString::fromUtf8("[ĠġĢģĜĝĞğ]"), Qt::CaseInsensitive);
58         letters['h'] = QRegExp(QString::fromUtf8("[ħĤĥĦ]"), Qt::CaseInsensitive);
59         letters['i'] = QRegExp(QString::fromUtf8("[ÌìÍíÎîÏïİijĨĩĪīĬĭĮį]"), Qt::CaseInsensitive);
60         letters['j'] = QRegExp(QString::fromUtf8("[Ĵĵ]"), Qt::CaseInsensitive);
61         letters['k'] = QRegExp(QString::fromUtf8("[Ķķĸ]"), Qt::CaseInsensitive);
62         letters['l'] = QRegExp(QString::fromUtf8("[ŀŁłĹĺĻļĽľĿ]"), Qt::CaseInsensitive);
63         letters['n'] = QRegExp(QString::fromUtf8("[ÑñŃńŅņŇňʼnŊŋ]"), Qt::CaseInsensitive);
64         letters['o'] = QRegExp(QString::fromUtf8("[ÒòÓóÔÕõÖöØøŐőœŌōŎŏ]"), Qt::CaseInsensitive);
65         letters['r'] = QRegExp(QString::fromUtf8("[ŕŖŖŗŘř]"), Qt::CaseInsensitive);
66         letters['s'] = QRegExp(QString::fromUtf8("[ߊšŚśŜŝŞş]"), Qt::CaseInsensitive);
67         letters['t'] = QRegExp(QString::fromUtf8("[ŢţŤťŦŧ]"), Qt::CaseInsensitive);
68         letters['u'] = QRegExp(QString::fromUtf8("[ÙùÚúÛûÜüŰűŲųŨũŪūŬŮů]"), Qt::CaseInsensitive);
69         letters['w'] = QRegExp(QString::fromUtf8("[Ŵŵ]"), Qt::CaseInsensitive);
70         letters['y'] = QRegExp(QString::fromUtf8("[ÿÝýŶŷŸ]"), Qt::CaseInsensitive);
71         letters['z'] = QRegExp(QString::fromUtf8("[ŹźŻżŽž]"), Qt::CaseInsensitive);
72         noLetter = QRegExp("[^a-z ]", Qt::CaseInsensitive);
73     }
74 };
75
76 #endif // ACCENTSNORMALIZER_H