Subversion Repositories Scribus

Rev

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