Subversion Repositories Scribus

Rev

Rev 17486 | Rev 17491 | 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"
17486 craig 12
#include "langmgr.h"
17437 craig 13
 
14
 
17485 craig 15
HunspellDialog::HunspellDialog(QWidget *parent, ScribusDoc *doc, StoryText *iText)
17437 craig 16
{
17
        setupUi( this );
18
        setModal( true );
19
 
20
        connect (ignoreOncePushButton, SIGNAL(clicked()), this, SLOT(goToNextWord()));
21
        connect (ignoreAllPushButton, SIGNAL(clicked()), this, SLOT(ignoreAllWords()));
22
        connect (changePushButton, SIGNAL(clicked()), this, SLOT(changeWord()));
23
        connect (changeAllPushButton, SIGNAL(clicked()), this, SLOT(changeAllWords()));
17486 craig 24
        connect (languagesComboBox, SIGNAL(currentIndexChanged (int)), this, SLOT(languageComboChanged(int)));
17437 craig 25
 
26
        m_doc=doc;
27
        m_docChanged=false;
17485 craig 28
        m_Itext=iText;
17486 craig 29
        m_returnToDefaultLang=false;
30
        m_primaryLangIndex=0;
17437 craig 31
}
32
 
17490 craig 33
void HunspellDialog::set(QMap<QString, QString>* dictionaryMap, Hunspell **hspellers, QList<WordsFound> *wfList)
17437 craig 34
{
17490 craig 35
        m_dictionaryMap=dictionaryMap;
17437 craig 36
        m_hspellers=hspellers;
37
        m_wfList=wfList;
17486 craig 38
        bool b=languagesComboBox->blockSignals(true);
39
        languagesComboBox->clear();
17490 craig 40
        QMap<QString, QString>::iterator it = m_dictionaryMap->begin();
41
        while (it != dictionaryMap->end())
42
        {
43
                languagesComboBox->addItem(LanguageManager::instance()->getLangFromAbbrev(it.key(), true));
44
                ++it;
45
        }
17486 craig 46
        languagesComboBox->setCurrentIndex(0);
47
        m_primaryLangIndex=0;
48
        languagesComboBox->blockSignals(b);
17437 craig 49
        wfListIndex=0;
50
        goToNextWord(0);
51
}
52
 
17486 craig 53
void HunspellDialog::updateSuggestions(QStringList &newSuggestions)
54
{
55
        suggestionsListWidget->clear();
56
        suggestionsListWidget->addItems(newSuggestions);
57
        suggestionsListWidget->setCurrentRow(0);
58
}
59
 
17437 craig 60
void HunspellDialog::goToNextWord(int i)
61
{
17486 craig 62
        if (m_returnToDefaultLang)
63
        {
64
                bool b=languagesComboBox->blockSignals(true);
65
                languagesComboBox->setCurrentIndex(m_primaryLangIndex);
66
                languagesComboBox->blockSignals(b);
67
        }
17437 craig 68
        if (i>=0)
69
                wfListIndex=i;
70
        else
17455 craig 71
        {
72
                do {
73
                        ++wfListIndex;
74
                } while (wfListIndex<m_wfList->count() && (m_wfList->at(wfListIndex).changed || m_wfList->at(wfListIndex).ignore));
75
                //qDebug()<<"selected word index"<<wfListIndex;
76
        }
17437 craig 77
        if (wfListIndex>=m_wfList->count())
78
        {
79
                statusLabel->setText(tr("Spelling check complete"));
80
                suggestionsListWidget->clear();
81
                sentenceTextEdit->clear();
82
                changePushButton->setEnabled(false);
83
                changeAllPushButton->setEnabled(false);
84
                return;
85
        }
86
        else
87
                statusLabel->setText("");
88
        currWF=m_wfList->at(wfListIndex);
17486 craig 89
        updateSuggestions(currWF.replacements);
17469 craig 90
 
91
        int sentencePos=0;
17485 craig 92
        QString sentence(m_Itext->sentence(currWF.start, sentencePos));
17457 craig 93
        sentence.insert(currWF.end-sentencePos+currWF.changeOffset,"</b></font>");
94
        sentence.insert(currWF.start-sentencePos+currWF.changeOffset,"<font color=red><b>");
17437 craig 95
        sentenceTextEdit->setText(sentence);
96
 
97
}
98
 
99
void HunspellDialog::ignoreAllWords()
100
{
101
        QString wordToIgnore=m_wfList->at(wfListIndex).w;
102
        //Do we start from 0 or from the instance of the word where we are... 0 for now
103
        for(int i=0;i<m_wfList->count();++i)
104
                if(m_wfList->at(i).w==wordToIgnore)
17455 craig 105
                        (*m_wfList)[i].ignore=true;
17437 craig 106
        goToNextWord();
107
}
108
 
109
void HunspellDialog::changeWord()
110
{
17455 craig 111
        //If we have ignored a word or its already changed, skip to the next.
112
        if(m_wfList->at(wfListIndex).ignore || m_wfList->at(wfListIndex).changed)
17437 craig 113
                goToNextWord();
114
        replaceWord(wfListIndex);
115
        goToNextWord();
116
}
117
 
118
void HunspellDialog::changeAllWords()
119
{
120
        if(m_wfList->at(wfListIndex).ignore && !m_wfList->at(wfListIndex).changed)
121
                return;
122
        QString wordToChange=m_wfList->at(wfListIndex).w;
123
        //Do we start from 0 or from the instance of the word where we are... 0 for now
124
        for(int i=0;i<m_wfList->count();++i)
125
                if(m_wfList->at(i).w==wordToChange)
17455 craig 126
                        replaceWord(i);
17437 craig 127
        goToNextWord();
128
}
129
 
130
void HunspellDialog::replaceWord(int i)
131
{
17486 craig 132
        //TODO: rehypenate after the replacement
17469 craig 133
        //qDebug()<<"Replacing word"<<i<m_wfList->at(i).w<<m_wfList->at(i).start;
17437 craig 134
        QString newText(suggestionsListWidget->currentItem()->text());
17485 craig 135
        int lengthDiff=m_Itext->replaceWord(m_wfList->at(i).start+m_wfList->at(i).changeOffset, newText);
17469 craig 136
        if (lengthDiff!=0)
17437 craig 137
        {
17457 craig 138
                for (int k=i; k<m_wfList->count();++k)
139
                        (*m_wfList)[k].changeOffset+=lengthDiff;
17437 craig 140
        }
17469 craig 141
        (*m_wfList)[i].changed=true;
17437 craig 142
        m_docChanged=true;
143
}
17486 craig 144
 
145
void HunspellDialog::languageComboChanged(int index)
146
{
147
        m_returnToDefaultLang=true;
148
        QString newLanguage=languagesComboBox->itemText(index);
149
        QString wordToCheck=m_wfList->at(wfListIndex).w;
150
//      qDebug()<<"You changed the language to:"<<newLanguage;
151
        if (!m_hspellers[index])
152
        {
153
                qDebug()<<"hspeller"<<index<<"does not exist";
154
                return;
155
        }
156
        if (m_hspellers[index]->spell(wordToCheck.toUtf8().constData())==0)
157
        {
158
                char **sugglist = NULL;
159
                int suggCount=m_hspellers[index]->suggest(&sugglist, wordToCheck.toUtf8().constData());
160
                QStringList replacements;
161
                for (int j=0; j < suggCount; ++j)
162
                {
163
                        //qDebug()<<"Suggestion "<<j<<":"<<sugglist[j];
164
                        replacements << QString::fromUtf8(sugglist[j]);
165
                }
166
                m_hspellers[index]->free_list(&sugglist, suggCount);
167
                updateSuggestions(replacements);
168
        }
169
        else
170
        {
171
                (*m_wfList)[wfListIndex].changed=true;
172
                m_docChanged=true;
173
                goToNextWord();
174
        }
175
}