Subversion Repositories Scribus

Rev

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

Rev Author Line No. Line
17502 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>
24036 jghali 11
 
17502 craig 12
#include "hunspelldialog.h"
13
#include "langmgr.h"
24041 jghali 14
#include "undomanager.h"
15
#include "undotransaction.h"
17502 craig 16
 
24041 jghali 17
HunspellDialog::HunspellDialog(QWidget *parent, ScribusDoc *doc, PageItem *item)
17502 craig 18
{
19
        setupUi( this );
20
        setModal( true );
21
 
22
        connect (ignoreOncePushButton, SIGNAL(clicked()), this, SLOT(goToNextWord()));
23
        connect (ignoreAllPushButton, SIGNAL(clicked()), this, SLOT(ignoreAllWords()));
24
        connect (changePushButton, SIGNAL(clicked()), this, SLOT(changeWord()));
25
        connect (changeAllPushButton, SIGNAL(clicked()), this, SLOT(changeAllWords()));
26
        connect (languagesComboBox, SIGNAL(currentIndexChanged(const QString &)), this, SLOT(languageComboChanged(const QString &)));
27
 
24036 jghali 28
        m_doc = doc;
29
        m_docChanged = false;
24041 jghali 30
        m_item  = item;
31
        m_iText = &m_item->itemText;
32
}
20787 jghali 33
 
24041 jghali 34
HunspellDialog::HunspellDialog(QWidget *parent, ScribusDoc *doc, StoryText *item)
35
{
36
        setupUi( this );
37
        setModal( true );
38
 
39
        connect (ignoreOncePushButton, SIGNAL(clicked()), this, SLOT(goToNextWord()));
40
        connect (ignoreAllPushButton, SIGNAL(clicked()), this, SLOT(ignoreAllWords()));
41
        connect (changePushButton, SIGNAL(clicked()), this, SLOT(changeWord()));
42
        connect (changeAllPushButton, SIGNAL(clicked()), this, SLOT(changeAllWords()));
43
        connect (languagesComboBox, SIGNAL(currentIndexChanged(const QString &)), this, SLOT(languageComboChanged(const QString &)));
44
 
45
        m_doc = doc;
46
        m_docChanged = false;
47
        m_item  = nullptr;
48
        m_iText = &m_item->itemText;
17502 craig 49
}
50
 
17888 jghali 51
void HunspellDialog::set(QMap<QString, QString>* dictionaryMap, QMap<QString, HunspellDict*> *hspellerMap, QList<WordsFound> *wfList)
17502 craig 52
{
24036 jghali 53
        m_dictionaryMap = dictionaryMap;
54
        m_hspellerMap = hspellerMap;
55
        m_wfList = wfList;
56
        bool b = languagesComboBox->blockSignals(true);
17502 craig 57
        languagesComboBox->clear();
22509 jghali 58
 
17798 craig 59
        QStringList langsToAdd;
22509 jghali 60
        auto it = m_dictionaryMap->cbegin();
61
        while (it != m_dictionaryMap->cend())
17502 craig 62
        {
24036 jghali 63
                QString lang = LanguageManager::instance()->getLangFromAbbrev(it.key(), true);
17798 craig 64
                if (!langsToAdd.contains(lang))
24036 jghali 65
                        langsToAdd << (!lang.isEmpty() ? lang : it.key());
17502 craig 66
                ++it;
67
        }
17798 craig 68
        languagesComboBox->addItems(langsToAdd);
17502 craig 69
        languagesComboBox->setCurrentIndex(0);
24036 jghali 70
        m_primaryLangIndex = 0;
17502 craig 71
        languagesComboBox->blockSignals(b);
24036 jghali 72
        m_wfListIndex = 0;
17502 craig 73
        goToNextWord(0);
74
}
75
 
76
void HunspellDialog::updateSuggestions(QStringList &newSuggestions)
77
{
78
        suggestionsListWidget->clear();
79
        suggestionsListWidget->addItems(newSuggestions);
80
        suggestionsListWidget->setCurrentRow(0);
81
}
82
 
83
void HunspellDialog::goToNextWord(int i)
84
{
85
        if (m_returnToDefaultLang)
86
        {
24036 jghali 87
                bool b = languagesComboBox->blockSignals(true);
17502 craig 88
                languagesComboBox->setCurrentIndex(m_primaryLangIndex);
89
                languagesComboBox->blockSignals(b);
90
        }
24036 jghali 91
        if (i >= 0)
92
                m_wfListIndex = i;
17502 craig 93
        else
94
        {
95
                do {
20787 jghali 96
                        ++m_wfListIndex;
97
                } while (m_wfListIndex<m_wfList->count() && (m_wfList->at(m_wfListIndex).changed || m_wfList->at(m_wfListIndex).ignore));
98
                //qDebug()<<"selected word index"<<m_wfListIndex;
17502 craig 99
        }
20787 jghali 100
        if (m_wfListIndex>=m_wfList->count())
17502 craig 101
        {
102
                statusLabel->setText(tr("Spelling check complete"));
103
                suggestionsListWidget->clear();
104
                sentenceTextEdit->clear();
105
                changePushButton->setEnabled(false);
106
                changeAllPushButton->setEnabled(false);
18618 jghali 107
                ignoreOncePushButton->setEnabled(false);
108
                ignoreAllPushButton->setEnabled(false);
17502 craig 109
                return;
110
        }
22638 craig 111
        statusLabel->setText("");
20787 jghali 112
        currWF=m_wfList->at(m_wfListIndex);
17502 craig 113
        setLanguageCombo(currWF.lang);
114
        updateSuggestions(currWF.replacements);
115
 
24036 jghali 116
        int sentencePos = 0;
17502 craig 117
        QString sentence(m_iText->sentence(currWF.start, sentencePos));
118
        sentence.insert(currWF.end-sentencePos+currWF.changeOffset,"</b></font>");
119
        sentence.insert(currWF.start-sentencePos+currWF.changeOffset,"<font color=red><b>");
120
        sentenceTextEdit->setText(sentence);
121
}
122
 
123
void HunspellDialog::ignoreAllWords()
124
{
20787 jghali 125
        if (m_wfListIndex < 0 || m_wfListIndex >= m_wfList->count())
18618 jghali 126
                return;
20787 jghali 127
        QString wordToIgnore = m_wfList->at(m_wfListIndex).w;
17502 craig 128
        //Do we start from 0 or from the instance of the word where we are... 0 for now
22723 jghali 129
        for (int i = 0; i < m_wfList->count(); ++i)
18618 jghali 130
                if (m_wfList->at(i).w == wordToIgnore)
131
                        (*m_wfList)[i].ignore = true;
17502 craig 132
        goToNextWord();
133
}
134
 
135
void HunspellDialog::changeWord()
136
{
137
        //If we have ignored a word or its already changed, skip to the next.
22721 jghali 138
        if (m_wfList->at(m_wfListIndex).ignore || m_wfList->at(m_wfListIndex).changed)
17502 craig 139
                goToNextWord();
20787 jghali 140
        replaceWord(m_wfListIndex);
17502 craig 141
        goToNextWord();
142
}
143
 
144
void HunspellDialog::changeAllWords()
145
{
22721 jghali 146
        if (m_wfList->at(m_wfListIndex).ignore && !m_wfList->at(m_wfListIndex).changed)
17502 craig 147
                return;
24041 jghali 148
 
149
        UndoTransaction transaction;
150
        if ((m_item != nullptr) && UndoManager::undoEnabled())
151
                transaction = UndoManager::instance()->beginTransaction(m_item->getUName(), m_item->getUPixmap());
152
 
24036 jghali 153
        QString wordToChange = m_wfList->at(m_wfListIndex).w;
17502 craig 154
        //Do we start from 0 or from the instance of the word where we are... 0 for now
24041 jghali 155
        for (int i = 0; i < m_wfList->count(); ++i)
24036 jghali 156
                if (m_wfList->at(i).w == wordToChange)
17502 craig 157
                        replaceWord(i);
24041 jghali 158
 
159
        if (transaction)
160
                transaction.commit();
161
 
17502 craig 162
        goToNextWord();
163
}
164
 
165
void HunspellDialog::replaceWord(int i)
166
{
22055 jghali 167
        //TODO: rehyphenate after the replacement
24041 jghali 168
        int replaceStart = m_wfList->at(i).start + m_wfList->at(i).changeOffset;
169
        QString oldText(m_iText->word(replaceStart));
17502 craig 170
        QString newText(suggestionsListWidget->currentItem()->text());
24041 jghali 171
 
172
        UndoTransaction transaction;
173
        if ((m_item != nullptr) && UndoManager::undoEnabled())
174
        {
175
                UndoObject* undoTarget = m_item->isNoteFrame() ? dynamic_cast<UndoObject*>(m_item->doc()) : dynamic_cast<UndoObject*>(m_item);
176
                transaction = UndoManager::instance()->beginTransaction(m_item->getUName(), m_item->getUPixmap());
177
                if (oldText.length() > 0)
178
                {
179
                        auto is = new ScItemState<CharStyle>(Um::DeleteText, "", Um::IDelete);
180
                        is->set("DELETE_FRAMETEXT");
181
                        is->set("ETEA",  QString("delete_frametext"));
182
                        is->set("TEXT_STR", oldText);
183
                        is->set("START", replaceStart);
184
                        is->setItem(m_item->itemText.charStyle(replaceStart));
185
                        if (m_item->isNoteFrame())
186
                                is->set("noteframeName", m_item->getUName());
187
                        UndoManager::instance()->action(undoTarget, is);
188
                }
189
                if (newText.length() > 0)
190
                {
191
                        auto ss = new SimpleState(Um::InsertText, "", Um::ICreate);
192
                        ss->set("INSERT_FRAMETEXT");
193
                        ss->set("ETEA", QString("insert_frametext"));
194
                        ss->set("TEXT_STR", newText);
195
                        ss->set("START", replaceStart);
196
                        UndoObject * undoTarget = m_item;
197
                        if (m_item->isNoteFrame())
198
                                ss->set("noteframeName", m_item->getUName());
199
                        UndoManager::instance()->action(undoTarget, ss);
200
                }
201
        }
202
 
24036 jghali 203
        int lengthDiff = m_iText->replaceWord(m_wfList->at(i).start + m_wfList->at(i).changeOffset, newText);
204
        if (lengthDiff != 0)
17502 craig 205
        {
24041 jghali 206
                for (int k = i; k < m_wfList->count(); ++k)
24036 jghali 207
                        (*m_wfList)[k].changeOffset += lengthDiff;
17502 craig 208
        }
24041 jghali 209
 
210
        if (transaction)
211
                transaction.commit();
212
 
24036 jghali 213
        (*m_wfList)[i].changed = true;
214
        m_docChanged = true;
17502 craig 215
}
216
 
217
void HunspellDialog::languageComboChanged(const QString &newLanguage)
218
{
24036 jghali 219
        m_returnToDefaultLang = true;
220
        QString wordLang = LanguageManager::instance()->getAbbrevFromLang(newLanguage, false);
17502 craig 221
        if (!m_hspellerMap->contains(wordLang) )
222
        {
24036 jghali 223
                //qDebug() << "hspeller"<<wordLang<<"does not exist";
17502 craig 224
                return;
225
        }
24036 jghali 226
        //qDebug() << wordLang << newLanguage;
227
        if (m_wfList->count() == 0)
17798 craig 228
                return;
24036 jghali 229
        if (m_wfListIndex >= m_wfList->count())
230
                m_wfListIndex = 0;
231
        QString word = m_wfList->at(m_wfListIndex).w;
232
        if ((*m_hspellerMap)[wordLang]->spell(word) == 0)
17502 craig 233
        {
17888 jghali 234
                QStringList replacements = (*m_hspellerMap)[wordLang]->suggest(word);
17502 craig 235
                updateSuggestions(replacements);
236
        }
237
        else
238
        {
24036 jghali 239
                (*m_wfList)[m_wfListIndex].changed = true;
240
                m_docChanged = true;
17502 craig 241
                goToNextWord();
242
        }
243
}
244
 
245
void HunspellDialog::setLanguageCombo(const QString &newLangAbbrev)
246
{
22509 jghali 247
        bool found = false;
248
 
249
        auto it = m_dictionaryMap->cbegin();
250
        while (it != m_dictionaryMap->cend())
17502 craig 251
        {
22509 jghali 252
                if (it.key() == newLangAbbrev)
21371 craig 253
                {
24036 jghali 254
                        found = true;
17502 craig 255
                        break;
21371 craig 256
                }
17502 craig 257
                ++it;
258
        }
22509 jghali 259
        int i = 0;
21371 craig 260
        if (found)
22509 jghali 261
                i = languagesComboBox->findText(LanguageManager::instance()->getLangFromAbbrev(newLangAbbrev,true));
262
        bool b = languagesComboBox->blockSignals(true);
17502 craig 263
        languagesComboBox->setCurrentIndex(i);
264
        languagesComboBox->blockSignals(b);
265
}