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

KDEUI

  • sources
  • kde-4.14
  • kdelibs
  • 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 
45 namespace Sonnet
46 {
47 
48 //to initially disable sorting in the suggestions listview
49 #define NONSORTINGCOLUMN 2
50 
51 class ReadOnlyStringListModel: public QStringListModel
52 {
53 public:
54  ReadOnlyStringListModel(QObject* parent):QStringListModel(parent){}
55  Qt::ItemFlags flags(const QModelIndex& index) const {Q_UNUSED(index); return Qt::ItemIsEnabled | Qt::ItemIsSelectable;}
56 };
57 
58 class Dialog::Private
59 {
60 public:
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 
97 Dialog::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 
122 Dialog::~Dialog()
123 {
124  delete d;
125 }
126 
127 void 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 
161 void 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 
175 void Dialog::activeAutoCorrect( bool _active )
176 {
177  if ( _active )
178  d->ui.m_autoCorrect->show();
179  else
180  d->ui.m_autoCorrect->hide();
181 }
182 
183 void Dialog::showProgressDialog(int timeout)
184 {
185  d->progressDialogTimeout = timeout;
186 }
187 
188 void Dialog::showSpellCheckCompletionMessage( bool b )
189 {
190  d->showCompletionMessageBox = b;
191 }
192 
193 void Dialog::setSpellCheckContinuedAfterReplacement( bool b )
194 {
195  d->spellCheckContinuedAfterReplacement = b;
196 }
197 
198 void 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 
207 void Dialog::setGuiEnabled(bool b)
208 {
209  d->wdg->setEnabled(b);
210 }
211 
212 void 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 
239 void 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 
250 void 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 
261 QString Dialog::originalBuffer() const
262 {
263  return d->originalBuffer;
264 }
265 
266 QString Dialog::buffer() const
267 {
268  return d->checker->text();
269 }
270 
271 void 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 
278 void 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 
288 void Dialog::updateDictionaryComboBox()
289 {
290  Speller speller = d->checker->speller();
291  d->ui.m_language->setCurrentIndex(d->dictsMap.values().indexOf(speller.language()));
292 }
293 
294 void 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 
307 void 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 
324 void Dialog::slotAddWord()
325 {
326  setGuiEnabled(false);
327  setProgressDialogVisible(true);
328  d->checker->addWordToPersonal(d->currentWord.word);
329  d->checker->continueChecking();
330 }
331 
332 void 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 
351 void 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 
360 void Dialog::slotSkip()
361 {
362  setGuiEnabled(false);
363  setProgressDialogVisible(true);
364  d->checker->continueChecking();
365 }
366 
367 void 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 
378 void Dialog::slotSuggest()
379 {
380  QStringList suggs = d->checker->suggest( d->ui.m_replacement->text() );
381  fillSuggestions( suggs );
382 }
383 
384 void 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 
395 void Dialog::slotSelectionChanged(const QModelIndex &item)
396 {
397  d->ui.m_replacement->setText( item.data().toString() );
398 }
399 
400 void Dialog::fillSuggestions( const QStringList& suggs )
401 {
402  d->suggestionsModel->setStringList(suggs);
403 }
404 
405 void 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 
427 void 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"
i18n
QString i18n(const char *text)
QModelIndex
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, tooltip and whatsthis strings.
Definition: kdialog.cpp:699
QWidget
QDialog::reject
virtual void reject()
Sonnet::Dialog::~Dialog
~Dialog()
Definition: dialog.cpp:122
QDialog::setModal
void setModal(bool modal)
kdebug.h
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...
backgroundchecker.h
KDialog::Cancel
Show Cancel-button. (this button reject()s the dialog; result set to QDialog::Rejected) ...
Definition: kdialog.h:144
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
kconfig.h
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
KDialog::setHelp
void setHelp(const QString &anchor, const QString &appname=QString())
Sets the help path and topic.
Definition: kdialog.cpp:967
QMap< QString, QString >
Sonnet::Dialog::stop
void stop()
Sonnet::Dialog::replace
void replace(const QString &oldWord, int start, const QString &newWord)
settings_p.h
Sonnet::Dialog::setBuffer
void setBuffer(const QString &)
Definition: dialog.cpp:271
KDialog
A dialog base class with standard buttons and predefined layouts.
Definition: kdialog.h:128
kDebug
static QDebug kDebug(bool cond, int area=KDE_DEFAULT_DEBUG_AREA)
klocale.h
KDialog::setCaption
virtual void setCaption(const QString &caption)
Make a KDE compliant caption.
Definition: kdialog.cpp:469
i18nc
QString i18nc(const char *ctxt, const char *text)
KDialog::setMainWidget
void setMainWidget(QWidget *widget)
Sets the main widget of the dialog.
Definition: kdialog.cpp:338
Sonnet::Dialog::cancel
void cancel()
Sonnet::Dialog::show
void show()
Definition: dialog.cpp:307
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::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
QWidget::updatesEnabled
bool updatesEnabled() const
KDialog::cancelClicked
void cancelClicked()
The Cancel button was pressed.
QObject
KGuiItem
An abstract class for GUI data such as ToolTip and Icon.
Definition: kguiitem.h:36
QList::isEmpty
bool isEmpty() const
Sonnet::Dialog::spellCheckStatus
void spellCheckStatus(const QString &)
Signal sends when spell checking is finished/stopped/completed.
QString::isEmpty
bool isEmpty() const
speller.h
Sonnet::Dialog::Dialog
Dialog(BackgroundChecker *checker, QWidget *parent)
Definition: dialog.cpp:97
QList::first
T & first()
QString
KDialog::setButtons
void setButtons(ButtonCodes buttonMask)
Creates (or recreates) the button box and all the buttons in it.
Definition: kdialog.cpp:206
KDialog::Help
Show Help button. (this button will run the help set with setHelp)
Definition: kdialog.h:139
QStringListModel
QDialog::accept
virtual void accept()
Sonnet::Dialog::activeAutoCorrect
void activeAutoCorrect(bool _active)
Definition: dialog.cpp:175
QStringList
Sonnet::Speller
Sonnet::Dialog::misspelling
void misspelling(const QString &word, int start)
kprogressdialog.h
KDialog::setDefaultButton
void setDefaultButton(ButtonCode id)
Sets the button that will be activated when the Enter key is pressed.
Definition: kdialog.cpp:287
Sonnet::Speller::availableDictionaries
QMap< QString, QString > availableDictionaries() const
KProgressDialog
A dialog with a progress bar.
Definition: kprogressdialog.h:47
dialog.h
QModelIndex::data
QVariant data(int role) const
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::originalBuffer
QString originalBuffer() const
Definition: dialog.cpp:261
Sonnet::Dialog::autoCorrect
void autoCorrect(const QString &currentWord, const QString &replaceWord)
Sonnet::BackgroundChecker
QWidget::QWidget
QWidget(QWidget *parent, QFlags< Qt::WindowType > f)
QWidget::show
void show()
KDialog::user1Clicked
void user1Clicked()
The User1 button was pressed.
KDialog::User1
Show User defined button 1.
Definition: kdialog.h:150
kmessagebox.h
QObject::connect
bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
kguiitem.h
QVariant::toString
QString toString() const
filter_p.h
Sonnet::Dialog::buffer
QString buffer() const
Definition: dialog.cpp:266
Qt::ItemFlags
typedef ItemFlags
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Jun 22 2020 13:23:58 by doxygen 1.8.7 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 API Reference

Skip menu "kdelibs 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

Search



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