QtSpell  0.7.2
Spell checking for Qt text widgets
QtSpell.hpp
1 /* QtSpell - Spell checking for Qt text widgets.
2  * Copyright (c) 2014 Sandro Mani
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17  */
18 
19 #ifndef QTSPELL_HPP
20 #define QTSPELL_HPP
21 
22 #if defined(QTSPELL_LIBRARY)
23 # define QTSPELL_API Q_DECL_EXPORT
24 #else
25 # define QTSPELL_API Q_DECL_IMPORT
26 #endif
27 
28 #include <QObject>
29 #include <QRegExp>
30 
31 class QLineEdit;
32 class QMenu;
33 class QPlainTextEdit;
34 class QPoint;
35 class QTextDocument;
36 class QTextEdit;
37 
38 namespace enchant { class Dict; }
39 
40 namespace QtSpell {
41 
45 class QTSPELL_API Checker : public QObject
46 {
47  Q_OBJECT
48 public:
52  Checker(QObject* parent = 0);
53 
57  virtual ~Checker();
58 
64  virtual void checkSpelling(int start = 0, int end = -1) = 0;
65 
72  bool setLanguage(const QString& lang);
73 
78  const QString& getLanguage() const{ return m_lang; }
79 
85  void setDecodeLanguageCodes(bool decode){ m_decodeCodes = decode; }
86 
91  bool getDecodeLanguageCodes() const{ return m_decodeCodes; }
92 
97  void setShowCheckSpellingCheckbox(bool show) { m_spellingCheckbox = show; }
98 
103  bool getShowCheckSpellingCheckbox() const{ return m_spellingCheckbox; }
104 
109  bool getSpellingEnabled() const{ return m_spellingEnabled; }
110 
115  void addWordToDictionary(const QString& word);
116 
122  bool checkWord(const QString& word) const;
123 
128  void ignoreWord(const QString& word) const;
129 
135  QList<QString> getSpellingSuggestions(const QString& word) const;
136 
137 
142  static QList<QString> getLanguageList();
143 
152  static QString decodeLanguageCode(const QString& lang);
153 
154 public slots:
159  void setSpellingEnabled(bool enabled) { m_spellingEnabled = enabled; checkSpelling(); }
160 
161 signals:
167  void languageChanged(const QString& newLang);
168 
169 protected:
170  void showContextMenu(QMenu* menu, const QPoint& pos, int wordPos);
171 
172 private slots:
173  void slotAddWord();
174  void slotIgnoreWord();
175  void slotReplaceWord();
176  void slotSetLanguage(bool checked);
177 
178 private:
179  enchant::Dict* m_speller;
180  QString m_lang;
181  bool m_decodeCodes;
182  bool m_spellingCheckbox;
183  bool m_spellingEnabled;
184  QRegExp m_wordRegEx;
185 
193  virtual QString getWord(int pos, int* start = 0, int* end = 0) const = 0;
194 
201  virtual void insertWord(int start, int end, const QString& word) = 0;
202 
207  virtual bool isAttached() const = 0;
208  bool setLanguageInternal(const QString& lang);
209 };
210 
212 
213 class TextEditProxy;
214 class UndoRedoStack;
215 
220 class QTSPELL_API TextEditChecker : public Checker
221 {
222  Q_OBJECT
223 public:
227  TextEditChecker(QObject* parent = 0);
228 
232  ~TextEditChecker();
233 
238  void setTextEdit(QTextEdit* textEdit);
239 
244  void setTextEdit(QPlainTextEdit* textEdit);
245 
246  void checkSpelling(int start = 0, int end = -1);
247 
254  void clearUndoRedo();
255 
263  void setUndoRedoEnabled(bool enabled);
264 
265 public slots:
273  void undo();
280  void redo();
281 
282 signals:
290  void undoAvailable(bool available);
291 
299  void redoAvailable(bool available);
300 
301 private:
302  TextEditProxy* m_textEdit;
303  QTextDocument* m_document;
304  UndoRedoStack* m_undoRedoStack;
305  bool m_undoRedoInProgress;
306  Qt::ContextMenuPolicy m_oldContextMenuPolicy;
307 
308  QString getWord(int pos, int* start = 0, int* end = 0) const;
309  void insertWord(int start, int end, const QString& word);
310  bool isAttached() const{ return m_textEdit != 0; }
311  void setTextEdit(TextEditProxy* textEdit);
312  bool eventFilter(QObject *obj, QEvent *event);
313 
314 private slots:
315  void slotShowContextMenu(const QPoint& pos);
316  void slotCheckDocumentChanged();
317  void slotDetachTextEdit();
318  void slotCheckRange(int pos, int removed, int added);
319 };
320 
321 } // QtSpell
322 
323 #endif // QTSPELL_HPP
bool getSpellingEnabled() const
Return whether spellchecking is performed.
Definition: QtSpell.hpp:109
bool isAttached() const
Returns whether a widget is attached to the checker.
Definition: QtSpell.hpp:310
const QString & getLanguage() const
Retreive the current spelling language.
Definition: QtSpell.hpp:78
bool getDecodeLanguageCodes() const
Return whether langauge codes are decoded in the UI.
Definition: QtSpell.hpp:91
void setDecodeLanguageCodes(bool decode)
Set whether to decode language codes in the UI.
Definition: QtSpell.hpp:85
Checker class for QTextEdit widgets.
Definition: QtSpell.hpp:220
void setShowCheckSpellingCheckbox(bool show)
Set whether to display an "Check spelling" checkbox in the UI.
Definition: QtSpell.hpp:97
bool getShowCheckSpellingCheckbox() const
Return whether a "Check spelling" checkbox is displayed in the UI.
Definition: QtSpell.hpp:103
void setSpellingEnabled(bool enabled)
Set whether spell checking should be performed.
Definition: QtSpell.hpp:159
An abstract class providing spell checking support.
Definition: QtSpell.hpp:45