From: Bartosz Szatkowski Date: Thu, 26 Aug 2010 07:58:02 +0000 (+0200) Subject: removeAccents refactorized out to the AccentsNormalizer class X-Git-Tag: 0.4~10 X-Git-Url: http://vcs.maemo.org/git/?a=commitdiff_plain;h=refs%2Fheads%2Fbackbone;p=mdictionary removeAccents refactorized out to the AccentsNormalizer class --- diff --git a/trunk/src/base/backbone/Bookmarks.cpp b/trunk/src/base/backbone/Bookmarks.cpp index 5bdcc24..7505b28 100644 --- a/trunk/src/base/backbone/Bookmarks.cpp +++ b/trunk/src/base/backbone/Bookmarks.cpp @@ -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); diff --git a/trunk/src/base/backbone/Bookmarks.h b/trunk/src/base/backbone/Bookmarks.h index ddee782..6f8257c 100644 --- a/trunk/src/base/backbone/Bookmarks.h +++ b/trunk/src/base/backbone/Bookmarks.h @@ -42,6 +42,7 @@ #include #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 letters; QRegExp noLetter; diff --git a/trunk/src/base/base.pro b/trunk/src/base/base.pro index 20d7bb4..3e638ea 100644 --- a/trunk/src/base/base.pro +++ b/trunk/src/base/base.pro @@ -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 diff --git a/trunk/src/base/gui/DictTypeSelectDialog.h b/trunk/src/base/gui/DictTypeSelectDialog.h index cb805db..b880c4e 100644 --- a/trunk/src/base/gui/DictTypeSelectDialog.h +++ b/trunk/src/base/gui/DictTypeSelectDialog.h @@ -28,6 +28,7 @@ #include #include #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 index 0000000..9927bef --- /dev/null +++ b/trunk/src/includes/AccentsNormalizer.h @@ -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 . + + Copyright 2010 Comarch S.A. + +*******************************************************************************/ + +/*! /file AccentsNormalizer.h +\brief Common funcionality for all classes that need to remove accents from strings + +\author Bartosz Szatkowski +*/ + +#ifndef ACCENTSNORMALIZER_H +#define ACCENTSNORMALIZER_H + +#include +#include +#include + + +class AccentsNormalizer { + protected: + QMap 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 diff --git a/trunk/src/includes/CommonDictInterface.h b/trunk/src/includes/CommonDictInterface.h index 6c2b46f..f3fde62 100644 --- a/trunk/src/includes/CommonDictInterface.h +++ b/trunk/src/includes/CommonDictInterface.h @@ -34,13 +34,14 @@ #include #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"); diff --git a/trunk/src/includes/settings.h b/trunk/src/includes/settings.h index c50136a..9a8e043 100644 --- a/trunk/src/includes/settings.h +++ b/trunk/src/includes/settings.h @@ -30,11 +30,8 @@ #include #include -#include "CommonDictInterface.h" #include -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. diff --git a/trunk/src/plugins/xdxf/src/xdxfplugin.cpp b/trunk/src/plugins/xdxf/src/xdxfplugin.cpp index fdb57b8..0cac66b 100644 --- a/trunk/src/plugins/xdxf/src/xdxfplugin.cpp +++ b/trunk/src/plugins/xdxf/src/xdxfplugin.cpp @@ -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; } diff --git a/trunk/src/plugins/xdxf/src/xdxfplugin.h b/trunk/src/plugins/xdxf/src/xdxfplugin.h index ca94f55..f3cabe6 100644 --- a/trunk/src/plugins/xdxf/src/xdxfplugin.h +++ b/trunk/src/plugins/xdxf/src/xdxfplugin.h @@ -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 letters; - QRegExp noLetter; +// extern void initAccents(); + // extern QString removeAccents(QString string); };