r17469 by craig - hunspell plugin: add replaceWord() and sentence() functions to StoryText

scribus-commit scribus-commit at lists.scribus.net
Mon Apr 23 19:19:14 UTC 2012


Author: craig
Date: Mon Apr 23 19:19:14 2012
New Revision: 17469

URL: http://scribus.net/websvn/listing.php?repname=Scribus&sc=1&rev=17469
Log:
hunspell plugin: add replaceWord() and sentence() functions to StoryText

Modified:
    branches/Version14x/Scribus/scribus/plugins/tools/CMakeLists.txt
    branches/Version14x/Scribus/scribus/plugins/tools/hunspellcheck/hunspelldialog.cpp
    branches/Version14x/Scribus/scribus/text/storytext.cpp
    branches/Version14x/Scribus/scribus/text/storytext.h

Modified: branches/Version14x/Scribus/scribus/plugins/tools/CMakeLists.txt
URL: http://scribus.net/websvn/diff.php?repname=Scribus&rev=17469&path=/branches/Version14x/Scribus/scribus/plugins/tools/CMakeLists.txt
==============================================================================
--- branches/Version14x/Scribus/scribus/plugins/tools/CMakeLists.txt (original)
+++ branches/Version14x/Scribus/scribus/plugins/tools/CMakeLists.txt Mon Apr 23 19:19:14 2012
@@ -9,7 +9,7 @@
 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)

Modified: branches/Version14x/Scribus/scribus/plugins/tools/hunspellcheck/hunspelldialog.cpp
URL: http://scribus.net/websvn/diff.php?repname=Scribus&rev=17469&path=/branches/Version14x/Scribus/scribus/plugins/tools/hunspellcheck/hunspelldialog.cpp
==============================================================================
--- branches/Version14x/Scribus/scribus/plugins/tools/hunspellcheck/hunspelldialog.cpp (original)
+++ branches/Version14x/Scribus/scribus/plugins/tools/hunspellcheck/hunspelldialog.cpp Mon Apr 23 19:19:14 2012
@@ -64,11 +64,9 @@
 	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::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())
+	int lengthDiff=fTC->itemText.replaceWord(m_wfList->at(i).start+m_wfList->at(i).changeOffset, newText);
+	if (lengthDiff!=0)
 	{
-		for (int j = 0; j < currWF.w.length(); ++j)
-			iText->replaceChar(currWF.start+j+currWF.changeOffset, newText[j]);
-	}
-	else
-	{
-		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;
 }

Modified: branches/Version14x/Scribus/scribus/text/storytext.cpp
URL: http://scribus.net/websvn/diff.php?repname=Scribus&rev=17469&path=/branches/Version14x/Scribus/scribus/text/storytext.cpp
==============================================================================
--- branches/Version14x/Scribus/scribus/text/storytext.cpp (original)
+++ branches/Version14x/Scribus/scribus/text/storytext.cpp Mon Apr 23 19:19:14 2012
@@ -478,6 +478,42 @@
 	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);
@@ -552,6 +588,15 @@
 	}
 
 	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

Modified: branches/Version14x/Scribus/scribus/text/storytext.h
URL: http://scribus.net/websvn/diff.php?repname=Scribus&rev=17469&path=/branches/Version14x/Scribus/scribus/text/storytext.h
==============================================================================
--- branches/Version14x/Scribus/scribus/text/storytext.h (original)
+++ branches/Version14x/Scribus/scribus/text/storytext.h Mon Apr 23 19:19:14 2012
@@ -109,6 +109,8 @@
 
  	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 @@
 	// 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);
 	
@@ -136,6 +140,9 @@
  	QChar   text(int pos) const;
 	// 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;




More information about the scribus-commit mailing list