removeAccents refactorized out to the AccentsNormalizer class backbone
authorBartosz Szatkowski <bulislaw@linux.com>
Thu, 26 Aug 2010 07:58:02 +0000 (09:58 +0200)
committerBartosz Szatkowski <bulislaw@linux.com>
Thu, 26 Aug 2010 07:58:02 +0000 (09:58 +0200)
trunk/src/base/backbone/Bookmarks.cpp
trunk/src/base/backbone/Bookmarks.h
trunk/src/base/base.pro
trunk/src/base/gui/DictTypeSelectDialog.h
trunk/src/includes/AccentsNormalizer.h [new file with mode: 0644]
trunk/src/includes/CommonDictInterface.h
trunk/src/includes/settings.h
trunk/src/plugins/xdxf/src/xdxfplugin.cpp
trunk/src/plugins/xdxf/src/xdxfplugin.h

index 5bdcc24..7505b28 100644 (file)
@@ -19,33 +19,6 @@ QSqlDatabase Bookmarks::getDbCnx(QString dbName) {
 
 
 
-void Bookmarks::initAccents() {
-    letters['a'] = QRegExp("[ÀàÁáÂÂâÃãÄäÅåæÆĀāĂ㥹]", Qt::CaseInsensitive);
-    letters['c'] = QRegExp("[ÇçÈçŒĆćĈĉĊċČč]", Qt::CaseInsensitive);
-    letters['d'] = QRegExp("[ÐĐđĎď]", Qt::CaseInsensitive);
-    letters['e'] = QRegExp("[ÈéèÉÊêËëĒēĔĕĖėĘęĚěē]", Qt::CaseInsensitive);
-    letters['f'] = QRegExp("[ſ]", Qt::CaseInsensitive);
-    letters['g'] = QRegExp("[ĠġĢģĜĝĞğ]", Qt::CaseInsensitive);
-    letters['h'] = QRegExp("[ħĤĥĦ]", Qt::CaseInsensitive);
-    letters['i'] = QRegExp("[ÌìÍíÎîÏïİijĨĩĪīĬĭĮį]", Qt::CaseInsensitive);
-    letters['j'] = QRegExp("[Ĵĵ]", Qt::CaseInsensitive);
-    letters['k'] = QRegExp("[Ķķĸ]", Qt::CaseInsensitive);
-    letters['l'] = QRegExp("[ŀŁłĹĺĻļĽľĿ]", Qt::CaseInsensitive);
-    letters['n'] = QRegExp("[ÑñŃńŅņŇňʼnŊŋ]", Qt::CaseInsensitive);
-    letters['o'] = QRegExp("[ÒòÓóÔÕõÖöØøŐőœŌōŎŏ]", Qt::CaseInsensitive);
-    letters['r'] = QRegExp("[ŕŖŖŗŘř]", Qt::CaseInsensitive);
-    letters['s'] = QRegExp("[ߊšŚśŜŝŞş]", Qt::CaseInsensitive);
-    letters['t'] = QRegExp("[ŢţŤťŦŧ]", Qt::CaseInsensitive);
-    letters['u'] = QRegExp("[ÙùÚúÛûÜüŰűŲųŨũŪūŬŮů]", Qt::CaseInsensitive);
-    letters['w'] = QRegExp("[Ŵŵ]", Qt::CaseInsensitive);
-    letters['y'] = QRegExp("[ÿÝýŶŷŸ]", Qt::CaseInsensitive);
-    letters['z'] = QRegExp("[ŹźŻżŽž]", Qt::CaseInsensitive);
-    noLetter = QRegExp("[^a-z ]", Qt::CaseInsensitive);
-}
-
-
-
-
 bool Bookmarks::checkAndCreateDb() {
     QSqlDatabase db = getDbCnx(dbName);
     if(!db.isOpen() && !db.open()) {
@@ -174,18 +147,6 @@ QStringList Bookmarks::search(QString word, QString dbName) {
 
 
 
-QString Bookmarks::removeAccents(QString string) {
-   string = string.toLower();
-   QString normalized = string.normalized(QString::NormalizationForm_KD);
-   foreach(QChar let, letters.keys())
-       normalized.replace(letters[let], QString(let));
-
-   normalized.replace(noLetter, "");
-   //qDebug() << "NORMALIZED: " << normalized;
-   return normalized;
-}
-
-
 
 bool Bookmarks::inBookmarks(QString word) {
     QSqlDatabase db = getDbCnx(dbName);
index ddee782..6f8257c 100644 (file)
@@ -42,6 +42,7 @@
 #include <QDebug>
 #include "../../includes/settings.h"
 #include "../../includes/translation.h"
+#include "../../includes/AccentsNormalizer.h"
 class BookmarkTranslation;
 
 
@@ -52,8 +53,7 @@ class BookmarkTranslation;
   special list with cached translations from all available dictionaries so
   You can search for them quickly even when You delete coresponding dict.
   */
-class Bookmarks
-{
+class Bookmarks : public AccentsNormalizer {
 public:
     Bookmarks();
 
@@ -97,11 +97,9 @@ public:
 
 private:
     bool checkAndCreateDb();
-    void initAccents();
 
     
     QString dbName;
-    QString removeAccents(QString);
     QSqlDatabase getDbCnx(QString dbName);
     QMap<QChar, QRegExp> letters;
     QRegExp noLetter;
index 20d7bb4..3e638ea 100644 (file)
@@ -1,16 +1,12 @@
 # -------------------------------------------------
 # Project created by QtCreator 2010-08-03T08:54:27
-#-------------------------------------------------
-
+# -------------------------------------------------
 QT += core \
     gui \
     sql \
     xml \
     xmlpatterns
-
 maemo5:QT += maemo5
-
-
 TARGET = mdictionary
 TEMPLATE = app
 MDICT_BINDIR = $$[MDICT_BINDIR]
@@ -35,7 +31,6 @@ SOURCES += gui/main.cpp \
     gui/WelcomeScreenWidget.cpp \
     gui/AboutWidget.cpp \
     gui/TranslationTextEdit.cpp
-
 HEADERS += gui/MainWindow.h \
     gui/AboutWidget.h \
     gui/SearchBarWidget.h \
@@ -60,12 +55,11 @@ HEADERS += gui/MainWindow.h \
     gui/BookmarksWidget.h \
     gui/WelcomeScreenWidget.h \
     ../includes/Notify.h \
-    gui/TranslationTextEdit.h
-
-FORMS +=
-
-RESOURCES += \
-    ../../../data/icons/gui.qrc
+    gui/TranslationTextEdit.h \
+    ../includes/AccentsNormalizer.h \
+    ../includes/DictDialog.h
+FORMS += 
+RESOURCES += ../../../data/icons/gui.qrc
 unix { 
     # VARIABLES
     isEmpty(PREFIX):PREFIX = /usr
@@ -93,5 +87,4 @@ unix {
     desktop.files += ../../../data/other/$${TARGET}.desktop
     icon64.files += ../../../data/icons/64x64/$${TARGET}.png
 }
-
 RESOURCES += xslt2.qrc
index cb805db..b880c4e 100644 (file)
@@ -28,6 +28,7 @@
 #include <QDialog>
 #include <QtGui>
 #include "../../includes/settings.h"
+#include "../../includes/CommonDictInterface.h"
 
 #define PLUGIN_ROW_ROLE 99      //TODO remove this
 
diff --git a/trunk/src/includes/AccentsNormalizer.h b/trunk/src/includes/AccentsNormalizer.h
new file mode 100644 (file)
index 0000000..9927bef
--- /dev/null
@@ -0,0 +1,76 @@
+/*******************************************************************************
+
+    This file is part of mDictionary.
+
+    mDictionary is free software: you can redistribute it and/or modify
+    it under the terms of the GNU General Public License as published by
+    the Free Software Foundation, either version 3 of the License, or
+    (at your option) any later version.
+
+    mDictionary is distributed in the hope that it will be useful,
+    but WITHOUT ANY WARRANTY; without even the implied warranty of
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+    GNU General Public License for more details.
+
+    You should have received a copy of the GNU General Public License
+    along with mDictionary.  If not, see <http://www.gnu.org/licenses/>.
+
+    Copyright 2010 Comarch S.A.
+
+*******************************************************************************/
+
+/*! /file AccentsNormalizer.h
+\brief Common funcionality for all classes that need to remove accents from strings
+
+\author Bartosz Szatkowski <bulislaw@linux.com>
+*/
+
+#ifndef ACCENTSNORMALIZER_H
+#define ACCENTSNORMALIZER_H
+
+#include <QMap>
+#include <QString>
+#include <QRegExp>
+
+
+class AccentsNormalizer {
+  protected:
+    QMap<QChar, QRegExp> letters;
+    QRegExp noLetter;
+
+    QString removeAccents(QString string) {
+       string = string.toLower();
+       QString normalized = string.normalized(QString::NormalizationForm_KD);
+       foreach(QChar let, letters.keys())
+           normalized.replace(letters[let], QString(let));
+
+       normalized.replace(noLetter, "");
+       //qDebug() << "NORMALIZED: " << normalized;
+       return normalized;
+    }
+    void initAccents() {
+        letters['a'] = QRegExp("[ÀàÁáÂÂâÃãÄäÅåæÆĀāĂ㥹]", Qt::CaseInsensitive);
+        letters['c'] = QRegExp("[ÇçÈçŒĆćĈĉĊċČč]", Qt::CaseInsensitive);
+        letters['d'] = QRegExp("[ÐĐđĎď]", Qt::CaseInsensitive);
+        letters['e'] = QRegExp("[ÈéèÉÊêËëĒēĔĕĖėĘęĚěē]", Qt::CaseInsensitive);
+        letters['f'] = QRegExp("[ſ]", Qt::CaseInsensitive);
+        letters['g'] = QRegExp("[ĠġĢģĜĝĞğ]", Qt::CaseInsensitive);
+        letters['h'] = QRegExp("[ħĤĥĦ]", Qt::CaseInsensitive);
+        letters['i'] = QRegExp("[ÌìÍíÎîÏïİijĨĩĪīĬĭĮį]", Qt::CaseInsensitive);
+        letters['j'] = QRegExp("[Ĵĵ]", Qt::CaseInsensitive);
+        letters['k'] = QRegExp("[Ķķĸ]", Qt::CaseInsensitive);
+        letters['l'] = QRegExp("[ŀŁłĹĺĻļĽľĿ]", Qt::CaseInsensitive);
+        letters['n'] = QRegExp("[ÑñŃńŅņŇňʼnŊŋ]", Qt::CaseInsensitive);
+        letters['o'] = QRegExp("[ÒòÓóÔÕõÖöØøŐőœŌōŎŏ]", Qt::CaseInsensitive);
+        letters['r'] = QRegExp("[ŕŖŖŗŘř]", Qt::CaseInsensitive);
+        letters['s'] = QRegExp("[ߊšŚśŜŝŞş]", Qt::CaseInsensitive);
+        letters['t'] = QRegExp("[ŢţŤťŦŧ]", Qt::CaseInsensitive);
+        letters['u'] = QRegExp("[ÙùÚúÛûÜüŰűŲųŨũŪūŬŮů]", Qt::CaseInsensitive);
+        letters['w'] = QRegExp("[Ŵŵ]", Qt::CaseInsensitive);
+        letters['y'] = QRegExp("[ÿÝýŶŷŸ]", Qt::CaseInsensitive);
+        letters['z'] = QRegExp("[ŹźŻżŽž]", Qt::CaseInsensitive);
+        noLetter = QRegExp("[^a-z ]", Qt::CaseInsensitive);
+    }
+};
+
+#endif // ACCENTSNORMALIZER_H
index 6c2b46f..f3fde62 100644 (file)
 #include <QList>
 #include "translation.h"
 #include "Notify.h"
+#include "settings.h"
+#include "AccentsNormalizer.h"
 
 class DictDialog;
-class Settings;
 
 
 //! Interface for dict engines plugins
-class CommonDictInterface : public QObject {
+class CommonDictInterface : public QObject, public AccentsNormalizer {
   Q_OBJECT
   public:
     CommonDictInterface(QObject *parent = 0):QObject(parent) {}
@@ -116,6 +117,18 @@ class CommonDictInterface : public QObject {
         \param QString text of the notification
     */
     void notify(Notify::NotifyType, QString);
+    
+protected:
+    QString removeAccents(QString string) {
+        if(settings()->value("strip_accents") == "true")
+            return AccentsNormalizer::removeAccents(string);
+        return string;
+    }
+
+    void initAccents() { AccentsNormalizer::initAccents(); }
+
+
+
 };
 
 Q_DECLARE_INTERFACE(CommonDictInterface, "CommonDictInterface/0.1");
index c50136a..9a8e043 100644 (file)
 
 #include <QString>
 #include <QHash>
-#include "CommonDictInterface.h"
 #include <QDebug>
 
-class CommonDictInterface;
-
 /*! Plugins or dictionaries may need to keep some of configuration between
   sessions, moreover Backbone or GUI may want store some additional info in
   plugin Settings.
index fdb57b8..0cac66b 100644 (file)
@@ -59,29 +59,6 @@ XdxfPlugin::~XdxfPlugin()
     delete _settings;
 }
 
-void XdxfPlugin::initAccents() {
-    letters['a'] = QRegExp("[ÀàÁáÂÂâÃãÄäÅåæÆĀāĂ㥹]", Qt::CaseInsensitive);
-    letters['c'] = QRegExp("[ÇçÈçŒĆćĈĉĊċČč]", Qt::CaseInsensitive);
-    letters['d'] = QRegExp("[ÐĐđĎď]", Qt::CaseInsensitive);
-    letters['e'] = QRegExp("[ÈéèÉÊêËëĒēĔĕĖėĘęĚěē]", Qt::CaseInsensitive);
-    letters['f'] = QRegExp("[ſ]", Qt::CaseInsensitive);
-    letters['g'] = QRegExp("[ĠġĢģĜĝĞğ]", Qt::CaseInsensitive);
-    letters['h'] = QRegExp("[ħĤĥĦ]", Qt::CaseInsensitive);
-    letters['i'] = QRegExp("[ÌìÍíÎîÏïİijĨĩĪīĬĭĮį]", Qt::CaseInsensitive);
-    letters['j'] = QRegExp("[Ĵĵ]", Qt::CaseInsensitive);
-    letters['k'] = QRegExp("[Ķķĸ]", Qt::CaseInsensitive);
-    letters['l'] = QRegExp("[ŀŁłĹĺĻļĽľĿ]", Qt::CaseInsensitive);
-    letters['n'] = QRegExp("[ÑñŃńŅņŇňʼnŊŋ]", Qt::CaseInsensitive);
-    letters['o'] = QRegExp("[ÒòÓóÔÕõÖöØøŐőœŌōŎŏ]", Qt::CaseInsensitive);
-    letters['r'] = QRegExp("[ŕŖŖŗŘř]", Qt::CaseInsensitive);
-    letters['s'] = QRegExp("[ߊšŚśŜŝŞş]", Qt::CaseInsensitive);
-    letters['t'] = QRegExp("[ŢţŤťŦŧ]", Qt::CaseInsensitive);
-    letters['u'] = QRegExp("[ÙùÚúÛûÜüŰűŲųŨũŪūŬŮů]", Qt::CaseInsensitive);
-    letters['w'] = QRegExp("[Ŵŵ]", Qt::CaseInsensitive);
-    letters['y'] = QRegExp("[ÿÝýŶŷŸ]", Qt::CaseInsensitive);
-    letters['z'] = QRegExp("[ŹźŻżŽž]", Qt::CaseInsensitive);
-    noLetter = QRegExp("[^a-z ]", Qt::CaseInsensitive);
-}
 
 QString XdxfPlugin::langFrom() const {   
     return _langFrom;
@@ -424,22 +401,6 @@ void XdxfPlugin::getDictionaryInfo() {
 
 
 
-QString XdxfPlugin::removeAccents(QString string) {
-    if(settings()->value("strip_accents") == "true") {
-        string = string.toLower();
-        QString normalized = string.normalized(QString::NormalizationForm_KD);
-        foreach(QChar let, letters.keys())
-            normalized.replace(letters[let], QString(let));
-
-        normalized.replace(noLetter, "");
-        //qDebug() << "NORMALIZED: " << normalized;
-        return normalized;
-    }
-    return string;
-}
-
-
-
 QIcon* XdxfPlugin::icon() {
     return &_icon;
 }
index ca94f55..f3cabe6 100644 (file)
@@ -119,8 +119,6 @@ Q_SIGNALS:
     void updateCachingProgress(int, int);
 
 
-protected:
-    QString removeAccents(QString);
 
 private:
 /*! returns true or false depending on whether the dictionary is cached
@@ -140,7 +138,6 @@ private:
 
     int countWords();
     bool makeCache(QString dir);
-    void initAccents();
 
     //! language from which we translate
     QString _langFrom;
@@ -167,9 +164,8 @@ private:
     Settings *_settings;
     XdxfDictDialog* _dictDialog;
     XdxfCachingDialog* cachingDialog;
-
-    QMap<QChar, QRegExp> letters;
-    QRegExp noLetter;
+//    extern void initAccents();
+ //   extern QString removeAccents(QString string);
 
 };