Subversion Repositories Scribus

Rev

Rev 17455 | Rev 17469 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
17437 craig 1
/*
2
For general Scribus (>=1.3.2) copyright and licensing information please refer
3
to the COPYING file provided with the program. Following this notice may exist
4
a copyright and/or license notice that predates the release of Scribus 1.3.2
5
for which a new license (GPL+exception) is in place.
6
*/
7
 
8
#include <QComboBox>
9
#include <QListWidget>
10
#include <QTextEdit>
11
#include "hunspelldialog.h"
12
 
13
 
14
HunspellDialog::HunspellDialog(QWidget *parent, ScribusDoc *doc, PageItem *frameToCheck)
15
{
16
        setupUi( this );
17
        setModal( true );
18
 
19
        connect (ignoreOncePushButton, SIGNAL(clicked()), this, SLOT(goToNextWord()));
20
        connect (ignoreAllPushButton, SIGNAL(clicked()), this, SLOT(ignoreAllWords()));
21
        connect (changePushButton, SIGNAL(clicked()), this, SLOT(changeWord()));
22
        connect (changeAllPushButton, SIGNAL(clicked()), this, SLOT(changeAllWords()));
23
 
24
        m_doc=doc;
25
        m_docChanged=false;
26
        fTC=frameToCheck;
27
}
28
 
29
void HunspellDialog::set(QStringList *dictEntries, Hunspell **hspellers, QList<WordsFound> *wfList)
30
{
31
        m_dictEntries=dictEntries;
32
        m_hspellers=hspellers;
33
        m_wfList=wfList;
34
 
35
        languagesComboBox->addItems(*dictEntries);
36
 
37
        wfListIndex=0;
38
        goToNextWord(0);
39
}
40
 
41
void HunspellDialog::goToNextWord(int i)
42
{
43
        if (i>=0)
44
                wfListIndex=i;
45
        else
17455 craig 46
        {
47
                do {
48
                        ++wfListIndex;
49
                } while (wfListIndex<m_wfList->count() && (m_wfList->at(wfListIndex).changed || m_wfList->at(wfListIndex).ignore));
50
                //qDebug()<<"selected word index"<<wfListIndex;
51
        }
17437 craig 52
        if (wfListIndex>=m_wfList->count())
53
        {
54
                statusLabel->setText(tr("Spelling check complete"));
55
                suggestionsListWidget->clear();
56
                sentenceTextEdit->clear();
57
                changePushButton->setEnabled(false);
58
                changeAllPushButton->setEnabled(false);
59
                return;
60
        }
61
        else
62
                statusLabel->setText("");
63
        currWF=m_wfList->at(wfListIndex);
64
        suggestionsListWidget->clear();
65
        suggestionsListWidget->addItems(currWF.replacements);
66
        suggestionsListWidget->setCurrentRow(0);
67
        StoryText *iText=&fTC->itemText;
68
        int sentencePos=qMax(0,iText->prevSentence(currWF.start));
69
        sentencePos=qMax(sentencePos, iText->nextWord(sentencePos));
70
        int nextSentencePos=qMin(iText->length(), iText->nextSentence(currWF.end));
71
        QString sentence=iText->text(sentencePos, nextSentencePos-sentencePos);
17457 craig 72
        sentence.insert(currWF.end-sentencePos+currWF.changeOffset,"</b></font>");
73
        sentence.insert(currWF.start-sentencePos+currWF.changeOffset,"<font color=red><b>");
17437 craig 74
        sentenceTextEdit->setText(sentence);
75
 
76
}
77
 
78
void HunspellDialog::ignoreAllWords()
79
{
80
        QString wordToIgnore=m_wfList->at(wfListIndex).w;
81
        //Do we start from 0 or from the instance of the word where we are... 0 for now
82
        for(int i=0;i<m_wfList->count();++i)
83
                if(m_wfList->at(i).w==wordToIgnore)
17455 craig 84
                        (*m_wfList)[i].ignore=true;
17437 craig 85
        goToNextWord();
86
}
87
 
88
void HunspellDialog::changeWord()
89
{
17455 craig 90
        //If we have ignored a word or its already changed, skip to the next.
91
        if(m_wfList->at(wfListIndex).ignore || m_wfList->at(wfListIndex).changed)
17437 craig 92
                goToNextWord();
93
        replaceWord(wfListIndex);
94
        m_docChanged=true;
95
        goToNextWord();
96
}
97
 
98
void HunspellDialog::changeAllWords()
99
{
100
        if(m_wfList->at(wfListIndex).ignore && !m_wfList->at(wfListIndex).changed)
101
                return;
102
        QString wordToChange=m_wfList->at(wfListIndex).w;
103
        //Do we start from 0 or from the instance of the word where we are... 0 for now
104
        for(int i=0;i<m_wfList->count();++i)
105
                if(m_wfList->at(i).w==wordToChange)
17455 craig 106
                        replaceWord(i);
17437 craig 107
        m_docChanged=true;
108
        goToNextWord();
109
}
110
 
111
void HunspellDialog::replaceWord(int i)
112
{
113
        StoryText *iText=&fTC->itemText;
114
        currWF=m_wfList->at(i);
17455 craig 115
        (*m_wfList)[i].changed=true;
116
        //qDebug()<<"Replacing word"<<i<<m_wfList->value(i).w<<m_wfList->value(i).changed;
17437 craig 117
        QString newText(suggestionsListWidget->currentItem()->text());
118
        if (newText.length()==currWF.w.length())
119
        {
17457 craig 120
                for (int j = 0; j < currWF.w.length(); ++j)
121
                        iText->replaceChar(currWF.start+j+currWF.changeOffset, newText[j]);
17437 craig 122
        }
123
        else
124
        {
17457 craig 125
                int lengthDiff=newText.length()-currWF.w.length();
17437 craig 126
                if (newText.length()>currWF.w.length())
127
                {
17457 craig 128
                        for (int j = 0; j < currWF.w.length(); ++j)
129
                                iText->replaceChar(currWF.start+j+currWF.changeOffset, newText[j]);
130
                        for (int j = currWF.w.length(); j < newText.length(); ++j)
131
                                iText->insertChars(currWF.start+j+currWF.changeOffset, newText.mid(j,1), true);
17437 craig 132
                }
133
                else
134
                {
17457 craig 135
                        for (int j = 0; j < newText.length(); ++j)
136
                                iText->replaceChar(currWF.start+j+currWF.changeOffset, newText[j]);
137
                        iText->removeChars(currWF.start+currWF.changeOffset+newText.length(), -lengthDiff);
17437 craig 138
                }
17457 craig 139
                for (int k=i; k<m_wfList->count();++k)
140
                        (*m_wfList)[k].changeOffset+=lengthDiff;
17437 craig 141
        }
142
        m_docChanged=true;
143
}