• Skip to content
  • Skip to link menu
  • KDE API Reference
  • kdelibs-4.14.38 API Reference
  • KDE Home
  • Contact Us
 

KDEUI

  • kdeui
  • sonnet
dialog.cpp
Go to the documentation of this file.
1
22#include "dialog.h"
23#include "ui_sonnetui.h"
24
25#include "backgroundchecker.h"
26#include "speller.h"
27#include "filter_p.h"
28#include "settings_p.h"
29
30#include <kconfig.h>
31#include <kguiitem.h>
32#include <klocale.h>
33#include <kmessagebox.h>
34#include <kprogressdialog.h>
35#include <kdebug.h>
36
37#include <QtGui/QListView>
38#include <QtGui/QStringListModel>
39#include <QtGui/QPushButton>
40#include <QtGui/QComboBox>
41#include <QtGui/QLabel>
42#include <QtCore/QTimer>
43
44
45namespace Sonnet
46{
47
48//to initially disable sorting in the suggestions listview
49#define NONSORTINGCOLUMN 2
50
51class ReadOnlyStringListModel: public QStringListModel
52{
53public:
54 ReadOnlyStringListModel(QObject* parent):QStringListModel(parent){}
55 Qt::ItemFlags flags(const QModelIndex& index) const {Q_UNUSED(index); return Qt::ItemIsEnabled | Qt::ItemIsSelectable;}
56};
57
58class Dialog::Private
59{
60public:
61 Ui_SonnetUi ui;
62 ReadOnlyStringListModel *suggestionsModel;
63 QWidget *wdg;
64 KProgressDialog *progressDialog;
65 QString originalBuffer;
66 BackgroundChecker *checker;
67
68 Word currentWord;
69 QMap<QString, QString> replaceAllMap;
70 bool restart;//used when text is distributed across several qtextedits, eg in KAider
71
72 QMap<QString, QString> dictsMap;
73
74 int progressDialogTimeout;
75 bool showCompletionMessageBox;
76 bool spellCheckContinuedAfterReplacement;
77 bool canceled;
78
79 void deleteProgressDialog(bool directly)
80 {
81 if (progressDialog)
82 {
83 progressDialog->hide();
84 if (directly)
85 {
86 delete progressDialog;
87 }
88 else
89 {
90 progressDialog->deleteLater();
91 }
92 progressDialog = NULL;
93 }
94 }
95};
96
97Dialog::Dialog(BackgroundChecker *checker,
98 QWidget *parent)
99 : KDialog(parent),
100 d(new Private)
101{
102 setModal(true);
103 setCaption(i18nc("@title:window", "Check Spelling"));
104 setButtons(Help | Cancel | User1);
105 setButtonGuiItem(User1, KGuiItem(i18nc("@action:button", "&Finished")));
106
107 setDefaultButton(User1);
108 d->checker = checker;
109
110 d->canceled = false;
111 d->showCompletionMessageBox = false;
112 d->spellCheckContinuedAfterReplacement = true;
113 d->progressDialogTimeout = -1;
114 d->progressDialog = NULL;
115
116 initGui();
117 initConnections();
118 setMainWidget(d->wdg);
119 setHelp(QString(),"sonnet");
120}
121
122Dialog::~Dialog()
123{
124 delete d;
125}
126
127void Dialog::initConnections()
128{
129 connect( d->ui.m_addBtn, SIGNAL(clicked()),
130 SLOT(slotAddWord()) );
131 connect( d->ui.m_replaceBtn, SIGNAL(clicked()),
132 SLOT(slotReplaceWord()) );
133 connect( d->ui.m_replaceAllBtn, SIGNAL(clicked()),
134 SLOT(slotReplaceAll()) );
135 connect( d->ui.m_skipBtn, SIGNAL(clicked()),
136 SLOT(slotSkip()) );
137 connect( d->ui.m_skipAllBtn, SIGNAL(clicked()),
138 SLOT(slotSkipAll()) );
139 connect( d->ui.m_suggestBtn, SIGNAL(clicked()),
140 SLOT(slotSuggest()) );
141 connect( d->ui.m_language, SIGNAL(activated(QString)),
142 SLOT(slotChangeLanguage(QString)) );
143 connect( d->ui.m_suggestions, SIGNAL(clicked(QModelIndex)),
144 SLOT(slotSelectionChanged(QModelIndex)) );
145 connect( d->checker, SIGNAL(misspelling(QString,int)),
146 SLOT(slotMisspelling(QString,int)) );
147 connect( d->checker, SIGNAL(done()),
148 SLOT(slotDone()) );
149 connect( d->ui.m_suggestions, SIGNAL(doubleClicked(QModelIndex)),
150 SLOT(slotReplaceWord()) );
151 connect( this, SIGNAL(user1Clicked()), this, SLOT(slotFinished()) );
152 connect( this, SIGNAL(cancelClicked()),this, SLOT(slotCancel()) );
153 connect( d->ui.m_replacement, SIGNAL(returnPressed()), this, SLOT(slotReplaceWord()) );
154 connect( d->ui.m_autoCorrect, SIGNAL(clicked()),
155 SLOT(slotAutocorrect()) );
156 // button use by kword/kpresenter
157 // hide by default
158 d->ui.m_autoCorrect->hide();
159}
160
161void Dialog::initGui()
162{
163 d->wdg = new QWidget(this);
164 d->ui.setupUi(d->wdg);
165 setGuiEnabled(false);
166
167 //d->ui.m_suggestions->setSorting( NONSORTINGCOLUMN );
168 fillDictionaryComboBox();
169 d->restart = false;
170
171 d->suggestionsModel=new ReadOnlyStringListModel(this);
172 d->ui.m_suggestions->setModel(d->suggestionsModel);
173}
174
175void Dialog::activeAutoCorrect( bool _active )
176{
177 if ( _active )
178 d->ui.m_autoCorrect->show();
179 else
180 d->ui.m_autoCorrect->hide();
181}
182
183void Dialog::showProgressDialog(int timeout)
184{
185 d->progressDialogTimeout = timeout;
186}
187
188void Dialog::showSpellCheckCompletionMessage( bool b )
189{
190 d->showCompletionMessageBox = b;
191}
192
193void Dialog::setSpellCheckContinuedAfterReplacement( bool b )
194{
195 d->spellCheckContinuedAfterReplacement = b;
196}
197
198void Dialog::slotAutocorrect()
199{
200 setGuiEnabled(false);
201 setProgressDialogVisible(true);
202 kDebug();
203 emit autoCorrect(d->currentWord.word, d->ui.m_replacement->text() );
204 slotReplaceWord();
205}
206
207void Dialog::setGuiEnabled(bool b)
208{
209 d->wdg->setEnabled(b);
210}
211
212void Dialog::setProgressDialogVisible(bool b)
213{
214 if (!b)
215 {
216 d->deleteProgressDialog(true);
217 }
218 else if(d->progressDialogTimeout >= 0)
219 {
220 if (d->progressDialog)
221 {
222 return;
223 }
224 d->progressDialog = new KProgressDialog(this, i18nc("@title:window", "Check Spelling"),
225 i18nc("progress label", "Spell checking in progress..."));
226 d->progressDialog->setModal(true);
227 d->progressDialog->setAutoClose(false);
228 d->progressDialog->setAutoReset(false);
229 // create an 'indefinite' progress box as we currently cannot get progress feedback from
230 // the speller
231 d->progressDialog->progressBar()->reset();
232 d->progressDialog->progressBar()->setRange(0, 0);
233 d->progressDialog->progressBar()->setValue(0);
234 connect(d->progressDialog, SIGNAL(cancelClicked()), this, SLOT(slotCancel()));
235 d->progressDialog->setMinimumDuration(d->progressDialogTimeout);
236 }
237}
238
239void Dialog::slotFinished()
240{
241 kDebug();
242 setProgressDialogVisible(false);
243 emit stop();
244 //FIXME: should we emit done here?
245 emit done(d->checker->text());
246 emit spellCheckStatus(i18n("Spell check stopped."));
247 accept();
248}
249
250void Dialog::slotCancel()
251{
252 kDebug();
253 d->canceled = true;
254 d->deleteProgressDialog(false); // this method can be called in response to
255 // pressing 'Cancel' on the dialog
256 emit cancel();
257 emit spellCheckStatus(i18n("Spell check canceled."));
258 reject();
259}
260
261QString Dialog::originalBuffer() const
262{
263 return d->originalBuffer;
264}
265
266QString Dialog::buffer() const
267{
268 return d->checker->text();
269}
270
271void Dialog::setBuffer(const QString &buf)
272{
273 d->originalBuffer = buf;
274 //it is possible to change buffer inside slot connected to done() signal
275 d->restart = true;
276}
277
278void Dialog::fillDictionaryComboBox()
279{
280 Speller speller = d->checker->speller();
281 d->dictsMap = speller.availableDictionaries();
282 QStringList langs = d->dictsMap.keys();
283 d->ui.m_language->clear();
284 d->ui.m_language->addItems(langs);
285 updateDictionaryComboBox();
286}
287
288void Dialog::updateDictionaryComboBox()
289{
290 Speller speller = d->checker->speller();
291 d->ui.m_language->setCurrentIndex(d->dictsMap.values().indexOf(speller.language()));
292}
293
294void Dialog::updateDialog( const QString& word )
295{
296 d->ui.m_unknownWord->setText( word );
297 d->ui.m_contextLabel->setText( d->checker->currentContext() );
298 const QStringList suggs = d->checker->suggest( word );
299
300 if (suggs.isEmpty())
301 d->ui.m_replacement->clear();
302 else
303 d->ui.m_replacement->setText( suggs.first() );
304 fillSuggestions( suggs );
305}
306
307void Dialog::show()
308{
309 kDebug()<<"Showing dialog";
310 d->canceled = false;
311 fillDictionaryComboBox();
312 updateDictionaryComboBox();
313 if (d->originalBuffer.isEmpty())
314 {
315 d->checker->start();
316 }
317 else
318 {
319 d->checker->setText(d->originalBuffer);
320 }
321 setProgressDialogVisible(true);
322}
323
324void Dialog::slotAddWord()
325{
326 setGuiEnabled(false);
327 setProgressDialogVisible(true);
328 d->checker->addWordToPersonal(d->currentWord.word);
329 d->checker->continueChecking();
330}
331
332void Dialog::slotReplaceWord()
333{
334 setGuiEnabled(false);
335 setProgressDialogVisible(true);
336 QString replacementText = d->ui.m_replacement->text();
337 emit replace( d->currentWord.word, d->currentWord.start,
338 replacementText );
339
340 if( d->spellCheckContinuedAfterReplacement ) {
341 d->checker->replace(d->currentWord.start,
342 d->currentWord.word,
343 replacementText);
344 d->checker->continueChecking();
345 }
346 else {
347 d->checker->stop();
348 }
349}
350
351void Dialog::slotReplaceAll()
352{
353 setGuiEnabled(false);
354 setProgressDialogVisible(true);
355 d->replaceAllMap.insert( d->currentWord.word,
356 d->ui.m_replacement->text() );
357 slotReplaceWord();
358}
359
360void Dialog::slotSkip()
361{
362 setGuiEnabled(false);
363 setProgressDialogVisible(true);
364 d->checker->continueChecking();
365}
366
367void Dialog::slotSkipAll()
368{
369 setGuiEnabled(false);
370 setProgressDialogVisible(true);
371 //### do we want that or should we have a d->ignoreAll list?
372 Speller speller = d->checker->speller();
373 speller.addToPersonal(d->currentWord.word);
374 d->checker->setSpeller(speller);
375 d->checker->continueChecking();
376}
377
378void Dialog::slotSuggest()
379{
380 QStringList suggs = d->checker->suggest( d->ui.m_replacement->text() );
381 fillSuggestions( suggs );
382}
383
384void Dialog::slotChangeLanguage(const QString &lang)
385{
386 Speller speller = d->checker->speller();
387 QString languageCode = d->dictsMap[lang];
388 if (!languageCode.isEmpty()) {
389 d->checker->changeLanguage(languageCode);
390 slotSuggest();
391 emit languageChanged(languageCode);
392 }
393}
394
395void Dialog::slotSelectionChanged(const QModelIndex &item)
396{
397 d->ui.m_replacement->setText( item.data().toString() );
398}
399
400void Dialog::fillSuggestions( const QStringList& suggs )
401{
402 d->suggestionsModel->setStringList(suggs);
403}
404
405void Dialog::slotMisspelling(const QString& word, int start)
406{
407 setGuiEnabled(true);
408 setProgressDialogVisible(false);
409 emit misspelling(word, start);
410 //NOTE this is HACK I had to introduce because BackgroundChecker lacks 'virtual' marks on methods
411 //this dramatically reduces spellchecking time in Lokalize
412 //as this doesn't fetch suggestions for words that are present in msgid
413 if (!updatesEnabled())
414 return;
415
416 kDebug()<<"Dialog misspelling!!";
417 d->currentWord = Word( word, start );
418 if ( d->replaceAllMap.contains( word ) ) {
419 d->ui.m_replacement->setText( d->replaceAllMap[ word ] );
420 slotReplaceWord();
421 } else {
422 updateDialog( word );
423 }
424 KDialog::show();
425}
426
427void Dialog::slotDone()
428{
429 d->restart=false;
430 emit done(d->checker->text());
431 if (d->restart)
432 {
433 updateDictionaryComboBox();
434 d->checker->setText(d->originalBuffer);
435 d->restart=false;
436 }
437 else
438 {
439 setProgressDialogVisible(false);
440 kDebug()<<"Dialog done!";
441 emit spellCheckStatus(i18n("Spell check complete."));
442 accept();
443 if(!d->canceled && d->showCompletionMessageBox)
444 {
445 KMessageBox::information(this, i18n("Spell check complete."), i18nc("@title:window", "Check Spelling"));
446 }
447 }
448}
449
450}
451
452#include "dialog.moc"
backgroundchecker.h
KDialog
A dialog base class with standard buttons and predefined layouts.
Definition: kdialog.h:129
KDialog::setButtonGuiItem
void setButtonGuiItem(ButtonCode id, const KGuiItem &item)
Sets the KGuiItem directly for the button instead of using 3 methods to set the text,...
Definition: kdialog.cpp:699
KDialog::setMainWidget
void setMainWidget(QWidget *widget)
Sets the main widget of the dialog.
Definition: kdialog.cpp:338
KDialog::setHelp
void setHelp(const QString &anchor, const QString &appname=QString())
Sets the help path and topic.
Definition: kdialog.cpp:967
KDialog::setButtons
void setButtons(ButtonCodes buttonMask)
Creates (or recreates) the button box and all the buttons in it.
Definition: kdialog.cpp:206
KDialog::Help
@ Help
Show Help button. (this button will run the help set with setHelp)
Definition: kdialog.h:139
KDialog::User1
@ User1
Show User defined button 1.
Definition: kdialog.h:150
KDialog::Cancel
@ Cancel
Show Cancel-button. (this button reject()s the dialog; result set to QDialog::Rejected)
Definition: kdialog.h:144
KDialog::cancelClicked
void cancelClicked()
The Cancel button was pressed.
KDialog::setDefaultButton
void setDefaultButton(ButtonCode id)
Sets the button that will be activated when the Enter key is pressed.
Definition: kdialog.cpp:287
KDialog::setCaption
virtual void setCaption(const QString &caption)
Make a KDE compliant caption.
Definition: kdialog.cpp:469
KDialog::user1Clicked
void user1Clicked()
The User1 button was pressed.
KGuiItem
An abstract class for GUI data such as ToolTip and Icon.
Definition: kguiitem.h:37
KMessageBox::information
static void information(QWidget *parent, const QString &text, const QString &caption=QString(), const QString &dontShowAgainName=QString(), Options options=Notify)
Display an "Information" dialog.
Definition: kmessagebox.cpp:960
KProgressDialog
A dialog with a progress bar.
Definition: kprogressdialog.h:48
QMap
QObject
QWidget
Sonnet::BackgroundChecker
Sonnet::Dialog::autoCorrect
void autoCorrect(const QString &currentWord, const QString &replaceWord)
Sonnet::Dialog::~Dialog
~Dialog()
Definition: dialog.cpp:122
Sonnet::Dialog::setSpellCheckContinuedAfterReplacement
void setSpellCheckContinuedAfterReplacement(bool b)
Controls whether the spell checking is continued after the replacement of a misspelled word has been ...
Definition: dialog.cpp:193
Sonnet::Dialog::misspelling
void misspelling(const QString &word, int start)
Sonnet::Dialog::spellCheckStatus
void spellCheckStatus(const QString &)
Signal sends when spell checking is finished/stopped/completed.
Sonnet::Dialog::Dialog
Dialog(BackgroundChecker *checker, QWidget *parent)
Definition: dialog.cpp:97
Sonnet::Dialog::setBuffer
void setBuffer(const QString &)
Definition: dialog.cpp:271
Sonnet::Dialog::cancel
void cancel()
Sonnet::Dialog::stop
void stop()
Sonnet::Dialog::activeAutoCorrect
void activeAutoCorrect(bool _active)
Definition: dialog.cpp:175
Sonnet::Dialog::showProgressDialog
void showProgressDialog(int timeout=500)
Controls whether an (indefinite) progress dialog is shown when the spell checking takes longer than t...
Definition: dialog.cpp:183
Sonnet::Dialog::done
void done(const QString &newBuffer)
The dialog won't be closed if you setBuffer() in slot connected to this signal.
Sonnet::Dialog::buffer
QString buffer() const
Definition: dialog.cpp:266
Sonnet::Dialog::languageChanged
void languageChanged(const QString &language)
Emitted when the user changes the language used for spellchecking, which is shown in a combobox of th...
Sonnet::Dialog::showSpellCheckCompletionMessage
void showSpellCheckCompletionMessage(bool b=true)
Controls whether a message box indicating the completion of the spell checking is shown or not.
Definition: dialog.cpp:188
Sonnet::Dialog::show
void show()
Definition: dialog.cpp:307
Sonnet::Dialog::originalBuffer
QString originalBuffer() const
Definition: dialog.cpp:261
Sonnet::Dialog::replace
void replace(const QString &oldWord, int start, const QString &newWord)
Sonnet::Speller
Sonnet::Speller::availableDictionaries
QMap< QString, QString > availableDictionaries() const
dialog.h
filter_p.h
kDebug
#define kDebug
kconfig.h
kdebug.h
kguiitem.h
timeout
int timeout
klocale.h
i18n
QString i18n(const char *text)
i18nc
QString i18nc(const char *ctxt, const char *text)
kmessagebox.h
kprogressdialog.h
Sonnet
dialog.cpp
settings_p.h
speller.h
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Mon Feb 20 2023 00:00:00 by doxygen 1.9.6 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

KDEUI

Skip menu "KDEUI"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Modules
  • Related Pages

kdelibs-4.14.38 API Reference

Skip menu "kdelibs-4.14.38 API Reference"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDEWebKit
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • kio
  • KIOSlave
  • KJS
  •   KJS-API
  •   WTF
  • kjsembed
  • KNewStuff
  • KParts
  • KPty
  • Kross
  • KUnitConversion
  • KUtils
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver
Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal