diff options
| author | craig <craig@11d20701-8431-0410-a711-e3c959e3b870> | 2012-04-23 19:19:14 +0000 |
|---|---|---|
| committer | craig <craig@11d20701-8431-0410-a711-e3c959e3b870> | 2012-04-23 19:19:14 +0000 |
| commit | c987c42061d9cbf46176f4eeff70ac745c20b219 (patch) | |
| tree | bc08c150a61d587da49e8e6179e2875e3685a0d2 /scribus | |
| parent | 7fb0e01395e4c4a13bbd4ce9b46c4fbdfbff6bc9 (diff) | |
| download | scribus-c987c42061d9cbf46176f4eeff70ac745c20b219.tar.gz scribus-c987c42061d9cbf46176f4eeff70ac745c20b219.tar.xz scribus-c987c42061d9cbf46176f4eeff70ac745c20b219.zip | |
hunspell plugin: add replaceWord() and sentence() functions to StoryText
git-svn-id: svn://scribus.net/branches/Version14x/Scribus@17469 11d20701-8431-0410-a711-e3c959e3b870
Diffstat (limited to 'scribus')
| -rw-r--r-- | scribus/plugins/tools/CMakeLists.txt | 6 | ||||
| -rw-r--r-- | scribus/plugins/tools/hunspellcheck/hunspelldialog.cpp | 36 | ||||
| -rw-r--r-- | scribus/text/storytext.cpp | 45 | ||||
| -rw-r--r-- | scribus/text/storytext.h | 7 |
4 files changed, 62 insertions, 32 deletions
diff --git a/scribus/plugins/tools/CMakeLists.txt b/scribus/plugins/tools/CMakeLists.txt index 10cda84..979354e 100644 --- a/scribus/plugins/tools/CMakeLists.txt +++ b/scribus/plugins/tools/CMakeLists.txt @@ -9,7 +9,7 @@ ADD_SUBDIRECTORY(subdivide) if (HAVE_ASPELL) ADD_SUBDIRECTORY(spellcheck) endif (HAVE_ASPELL) -#if (HAVE_HUNSPELL) -# ADD_SUBDIRECTORY(hunspellcheck) -#endif (HAVE_HUNSPELL) +if (HAVE_HUNSPELL) + ADD_SUBDIRECTORY(hunspellcheck) +endif (HAVE_HUNSPELL) ADD_SUBDIRECTORY(transform) diff --git a/scribus/plugins/tools/hunspellcheck/hunspelldialog.cpp b/scribus/plugins/tools/hunspellcheck/hunspelldialog.cpp index f0eb3a6..d36b08b 100644 --- a/scribus/plugins/tools/hunspellcheck/hunspelldialog.cpp +++ b/scribus/plugins/tools/hunspellcheck/hunspelldialog.cpp @@ -64,11 +64,9 @@ void HunspellDialog::goToNextWord(int i) suggestionsListWidget->clear(); suggestionsListWidget->addItems(currWF.replacements); suggestionsListWidget->setCurrentRow(0); - StoryText *iText=&fTC->itemText; - int sentencePos=qMax(0,iText->prevSentence(currWF.start)); - sentencePos=qMax(sentencePos, iText->nextWord(sentencePos)); - int nextSentencePos=qMin(iText->length(), iText->nextSentence(currWF.end)); - QString sentence=iText->text(sentencePos, nextSentencePos-sentencePos); + + int sentencePos=0; + QString sentence(fTC->itemText.sentence(currWF.start, sentencePos)); sentence.insert(currWF.end-sentencePos+currWF.changeOffset,"</b></font>"); sentence.insert(currWF.start-sentencePos+currWF.changeOffset,"<font color=red><b>"); sentenceTextEdit->setText(sentence); @@ -110,34 +108,14 @@ void HunspellDialog::changeAllWords() void HunspellDialog::replaceWord(int i) { - StoryText *iText=&fTC->itemText; - currWF=m_wfList->at(i); - (*m_wfList)[i].changed=true; - //qDebug()<<"Replacing word"<<i<<m_wfList->value(i).w<<m_wfList->value(i).changed; + //qDebug()<<"Replacing word"<<i<m_wfList->at(i).w<<m_wfList->at(i).start; QString newText(suggestionsListWidget->currentItem()->text()); - if (newText.length()==currWF.w.length()) - { - for (int j = 0; j < currWF.w.length(); ++j) - iText->replaceChar(currWF.start+j+currWF.changeOffset, newText[j]); - } - else + int lengthDiff=fTC->itemText.replaceWord(m_wfList->at(i).start+m_wfList->at(i).changeOffset, newText); + if (lengthDiff!=0) { - int lengthDiff=newText.length()-currWF.w.length(); - if (newText.length()>currWF.w.length()) - { - for (int j = 0; j < currWF.w.length(); ++j) - iText->replaceChar(currWF.start+j+currWF.changeOffset, newText[j]); - for (int j = currWF.w.length(); j < newText.length(); ++j) - iText->insertChars(currWF.start+j+currWF.changeOffset, newText.mid(j,1), true); - } - else - { - for (int j = 0; j < newText.length(); ++j) - iText->replaceChar(currWF.start+j+currWF.changeOffset, newText[j]); - iText->removeChars(currWF.start+currWF.changeOffset+newText.length(), -lengthDiff); - } for (int k=i; k<m_wfList->count();++k) (*m_wfList)[k].changeOffset+=lengthDiff; } + (*m_wfList)[i].changed=true; m_docChanged=true; } diff --git a/scribus/text/storytext.cpp b/scribus/text/storytext.cpp index 3097850..dc4cace 100644 --- a/scribus/text/storytext.cpp +++ b/scribus/text/storytext.cpp @@ -478,6 +478,42 @@ void StoryText::replaceChar(int pos, QChar ch) invalidate(pos, pos + 1); } +int StoryText::replaceWord(int pos, QString newWord) +{ + int eoWord=pos; + while(eoWord < length()) + { + if (text(eoWord).isLetterOrNumber()) + ++eoWord; + else + break; + } + QString word=text(pos,eoWord-pos); + int lengthDiff=newWord.length()-word.length(); + if (lengthDiff==0) + { + for (int j = 0; j < word.length(); ++j) + replaceChar(pos+j, newWord[j]); + } + else + { + if (lengthDiff>0) + { + for (int j = 0; j < word.length(); ++j) + replaceChar(pos+j, newWord[j]); + for (int j = word.length(); j < newWord.length(); ++j) + insertChars(pos+j, newWord.mid(j,1), true); + } + else + { + for (int j = 0; j < newWord.length(); ++j) + replaceChar(pos+j, newWord[j]); + removeChars(pos+newWord.length(), -lengthDiff); + } + } + return lengthDiff; +} + void StoryText::hyphenateWord(int pos, uint len, char* hyphens) { assert(pos >= 0); @@ -554,6 +590,15 @@ QString StoryText::text(int pos, uint len) const return result; } +QString StoryText::sentence(int pos, int &posn) +{ + int sentencePos=qMax(0, prevSentence(pos)); + sentencePos=qMax(sentencePos, nextWord(sentencePos)); + posn=sentencePos; + int nextSentencePos=qMin(length(), nextSentence(pos+1)); + return text(sentencePos, nextSentencePos-sentencePos); +} + QString StoryText::textWithSmartHyphens(int pos, uint len) const { QString result(""); diff --git a/scribus/text/storytext.h b/scribus/text/storytext.h index f218f44..381e96c 100644 --- a/scribus/text/storytext.h +++ b/scribus/text/storytext.h @@ -109,6 +109,8 @@ class SCRIBUS_API StoryText : public QObject, public SaxIO void clear(); StoryText copy() const; + +// Add, change, replace // Insert chars from another StoryText object at current cursor position void insert(const StoryText& other, bool onlySelection = false); // Insert chars from another StoryText object at specific position @@ -126,6 +128,8 @@ class SCRIBUS_API StoryText : public QObject, public SaxIO // Insert object at specific position void insertObject(int pos, PageItem* obj); void replaceChar(int pos, QChar ch); + // Replaced a word, and return the difference in length between old and new + int replaceWord(int pos, QString newWord); void hyphenateWord(int pos, uint len, char* hyphens); @@ -137,6 +141,9 @@ class SCRIBUS_API StoryText : public QObject, public SaxIO // Get text with len chars at specific position QString text(int pos, uint len) const; + //Get sentence at any position within it + QString sentence(int pos, int &posn); + bool hasObject(int pos) const; PageItem* object(int pos) const; |
