Commit 2ff21c6a authored by Laurent Montel's avatar Laurent Montel 😁

Autocorrect text when necessary

parent 6ef2355c
......@@ -43,7 +43,9 @@ AutoCorrection::AutoCorrection()
mReplaceSingleQuotes(false),
mEnabled(false),
mSuperScriptAppendix(false),
mAddNonBreakingSpace(false)
mAddNonBreakingSpace(false),
mMaxFindStringLenght(0),
mMinFindStringLenght(0)
{
// default double quote open 0x201c
// default double quote close 0x201d
......@@ -216,6 +218,15 @@ void AutoCorrection::setTwoUpperLetterExceptions(const QSet<QString> &exceptions
void AutoCorrection::setAutocorrectEntries(const QHash<QString, QString> &entries)
{
mMaxFindStringLenght = 0;
mMinFindStringLenght = 0;
QHashIterator<QString, QString> i(entries);
while (i.hasNext()) {
i.next();
const int findStringLenght(i.key().length());
mMaxFindStringLenght = qMax(mMaxFindStringLenght, findStringLenght);
mMinFindStringLenght = qMin(mMinFindStringLenght, findStringLenght);
}
mAutocorrectEntries = entries;
}
......@@ -755,6 +766,15 @@ int AutoCorrection::advancedAutocorrect()
return -1;
}
const int actualWordLength(actualWord.length());
if (actualWordLength < mMinFindStringLenght) {
return -1;
}
if (actualWordLength > mMaxFindStringLenght) {
return -1;
}
// If the last char is punctuation, drop it for now
bool hasPunctuation = false;
QChar lastChar = actualWord.at(actualWord.length() - 1);
......@@ -990,11 +1010,13 @@ void AutoCorrection::readAutoCorrectionXmlFile(bool forceGlobal)
mTypographicSingleQuotes = typographicDefaultSingleQuotes();
mTypographicDoubleQuotes = typographicDefaultDoubleQuotes();
}
mMaxFindStringLenght = import.maxFindStringLenght();
mMinFindStringLenght = import.minFindStringLenght();
}
}
} else {
ImportKMailAutocorrection import;
if (import.import(LocalFile, ImportAbstractAutocorrection::All)) {
if (import.import(localFileName, ImportAbstractAutocorrection::All)) {
mUpperCaseExceptions = import.upperCaseExceptions();
mTwoUpperLetterExceptions = import.twoUpperLetterExceptions();
mAutocorrectEntries = import.autocorrectEntries();
......@@ -1006,6 +1028,8 @@ void AutoCorrection::readAutoCorrectionXmlFile(bool forceGlobal)
if (!fname.isEmpty() && import.import(fname, ImportAbstractAutocorrection::SuperScript)) {
mSuperScriptEntries = import.superScriptEntries();
}
mMaxFindStringLenght = import.maxFindStringLenght();
mMinFindStringLenght = import.minFindStringLenght();
}
}
......
......@@ -126,7 +126,8 @@ private:
bool mSuperScriptAppendix;
bool mAddNonBreakingSpace;
int mMaxFindStringLenght;
int mMinFindStringLenght;
QString mWord;
QTextCursor mCursor;
......
......@@ -20,7 +20,9 @@
using namespace PimCommon;
ImportAbstractAutocorrection::ImportAbstractAutocorrection(QWidget *parent)
: mParent(parent)
: mMaxFindStringLenght(0),
mMinFindStringLenght(0),
mParent(parent)
{
}
......@@ -53,6 +55,16 @@ AutoCorrection::TypographicQuotes ImportAbstractAutocorrection::typographicDoubl
return mTypographicDoubleQuotes;
}
int ImportAbstractAutocorrection::maxFindStringLenght() const
{
return mMaxFindStringLenght;
}
int ImportAbstractAutocorrection::minFindStringLenght() const
{
return mMinFindStringLenght;
}
QHash<QString, QString> ImportAbstractAutocorrection::superScriptEntries() const
{
return mSuperScriptEntries;
......
......@@ -47,7 +47,13 @@ public:
AutoCorrection::TypographicQuotes typographicSingleQuotes() const;
AutoCorrection::TypographicQuotes typographicDoubleQuotes() const;
int maxFindStringLenght() const;
int minFindStringLenght() const;
protected:
int mMaxFindStringLenght;
int mMinFindStringLenght;
QSet<QString> mUpperCaseExceptions;
QSet<QString> mTwoUpperLetterExceptions;
QHash<QString, QString> mAutocorrectEntries;
......
......@@ -69,6 +69,8 @@ bool ImportKMailAutocorrection::import(const QString &fileName, LoadAttribute lo
/* Load advanced autocorrect entry, including the format */
const QDomElement item = de.namedItem(QStringLiteral("items")).toElement();
mMaxFindStringLenght = 0;
mMinFindStringLenght = 0;
if (!item.isNull()) {
const QDomNodeList nl = item.childNodes();
const int numberOfElement(nl.count());
......@@ -76,6 +78,9 @@ bool ImportKMailAutocorrection::import(const QString &fileName, LoadAttribute lo
const QDomElement element = nl.item(i).toElement();
const QString find = element.attribute(QStringLiteral("find"));
const QString replace = element.attribute(QStringLiteral("replace"));
const int findLenght(find.length());
mMaxFindStringLenght = qMax(findLenght, mMaxFindStringLenght);
mMinFindStringLenght = qMin(findLenght, mMinFindStringLenght);
mAutocorrectEntries.insert(find, replace);
}
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment