Subversion Repositories Scribus

Rev

Rev 17455 | Go to most recent revision | Details | 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
        changeOffset=0;
28
}
29
 
30
void HunspellDialog::set(QStringList *dictEntries, Hunspell **hspellers, QList<WordsFound> *wfList)
31
{
32
        m_dictEntries=dictEntries;
33
        m_hspellers=hspellers;
34
        m_wfList=wfList;
35
 
36
        languagesComboBox->addItems(*dictEntries);
37
 
38
        wfListIndex=0;
39
        goToNextWord(0);
40
}
41
 
42
void HunspellDialog::goToNextWord(int i)
43
{
44
        if (i>=0)
45
                wfListIndex=i;
46
        else
47
                ++wfListIndex;
48
        if (wfListIndex>=m_wfList->count())
49
        {
50
                statusLabel->setText(tr("Spelling check complete"));
51
                suggestionsListWidget->clear();
52
                sentenceTextEdit->clear();
53
                changePushButton->setEnabled(false);
54
                changeAllPushButton->setEnabled(false);
55
                return;
56
        }
57
        else
58
                statusLabel->setText("");
59
        currWF=m_wfList->at(wfListIndex);
60
        suggestionsListWidget->clear();
61
        suggestionsListWidget->addItems(currWF.replacements);
62
        suggestionsListWidget->setCurrentRow(0);
63
        StoryText *iText=&fTC->itemText;
64
        int sentencePos=qMax(0,iText->prevSentence(currWF.start));
65
        sentencePos=qMax(sentencePos, iText->nextWord(sentencePos));
66
        int nextSentencePos=qMin(iText->length(), iText->nextSentence(currWF.end));
67
        QString sentence=iText->text(sentencePos, nextSentencePos-sentencePos);
68
        sentence.insert(currWF.end-sentencePos+changeOffset,"</b></font>");
69
        sentence.insert(currWF.start-sentencePos+changeOffset,"<font color=red><b>");
70
        sentenceTextEdit->setText(sentence);
71
 
72
}
73
 
74
void HunspellDialog::ignoreAllWords()
75
{
76
        QString wordToIgnore=m_wfList->at(wfListIndex).w;
77
        //Do we start from 0 or from the instance of the word where we are... 0 for now
78
        for(int i=0;i<m_wfList->count();++i)
79
                if(m_wfList->at(i).w==wordToIgnore)
80
                        m_wfList->value(i).ignore=true;
81
        goToNextWord();
82
}
83
 
84
void HunspellDialog::changeWord()
85
{
86
        //If we have ignored a word, skip to the next.
87
        if(m_wfList->at(wfListIndex).ignore && !m_wfList->at(wfListIndex).changed)
88
                goToNextWord();
89
        replaceWord(wfListIndex);
90
        m_docChanged=true;
91
        goToNextWord();
92
}
93
 
94
void HunspellDialog::changeAllWords()
95
{
96
        if(m_wfList->at(wfListIndex).ignore && !m_wfList->at(wfListIndex).changed)
97
                return;
98
        QString wordToChange=m_wfList->at(wfListIndex).w;
99
        //Do we start from 0 or from the instance of the word where we are... 0 for now
100
        for(int i=0;i<m_wfList->count();++i)
101
                if(m_wfList->at(i).w==wordToChange)
102
                {
103
                        m_wfList->value(i).changed=true;
104
                }
105
        m_docChanged=true;
106
        goToNextWord();
107
}
108
 
109
void HunspellDialog::replaceWord(int i)
110
{
111
        StoryText *iText=&fTC->itemText;
112
        currWF=m_wfList->at(i);
113
        m_wfList->value(i).changed=true;
114
        QString newText(suggestionsListWidget->currentItem()->text());
115
        if (newText.length()==currWF.w.length())
116
        {
117
                for (int i = 0; i < currWF.w.length(); ++i)
118
                        iText->replaceChar(currWF.start+i+changeOffset, newText[i]);
119
        }
120
        else
121
        {
122
                if (newText.length()>currWF.w.length())
123
                {
124
                        for (int i = 0; i < currWF.w.length(); ++i)
125
                                iText->replaceChar(currWF.start+i+changeOffset, newText[i]);
126
                        for (int i = currWF.w.length(); i < newText.length(); ++i)
127
                                iText->insertChars(currWF.start+i+changeOffset, newText.mid(i,1), true);
128
                        int lengthDiff=newText.length()-currWF.w.length();
129
                        changeOffset+=lengthDiff;
130
                        qDebug()<<"Change Offset is:"<<changeOffset;
131
                }
132
                else
133
                {
134
                        for (int i = 0; i < newText.length(); ++i)
135
                                iText->replaceChar(currWF.start+i+changeOffset, newText[i]);
136
                        int lengthDiff=currWF.w.length() - newText.length();
137
                        iText->removeChars(currWF.start+changeOffset+newText.length(), lengthDiff);
138
                        changeOffset-=lengthDiff;
139
                        qDebug()<<"Change Offset is:"<<changeOffset;
140
                }
141
        }
142
        m_docChanged=true;
143
}