QtSpell  0.7.4
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 
192  virtual QString getWord(int pos, int* start = 0, int* end = 0) const = 0;
193 
200  virtual void insertWord(int start, int end, const QString& word) = 0;
201 
206  virtual bool isAttached() const = 0;
207  bool setLanguageInternal(const QString& lang);
208 };
209 
211 
212 class TextEditProxy;
213 class UndoRedoStack;
214 
219 class QTSPELL_API TextEditChecker : public Checker
220 {
221  Q_OBJECT
222 public:
226  TextEditChecker(QObject* parent = 0);
227 
231  ~TextEditChecker();
232 
237  void setTextEdit(QTextEdit* textEdit);
238 
243  void setTextEdit(QPlainTextEdit* textEdit);
244 
245  void checkSpelling(int start = 0, int end = -1);
246 
253  void clearUndoRedo();
254 
262  void setUndoRedoEnabled(bool enabled);
263 
264 public slots:
272  void undo();
279  void redo();
280 
281 signals:
289  void undoAvailable(bool available);
290 
298  void redoAvailable(bool available);
299 
300 private:
301  TextEditProxy* m_textEdit;
302  QTextDocument* m_document;
303  UndoRedoStack* m_undoRedoStack;
304  bool m_undoRedoInProgress;
305  Qt::ContextMenuPolicy m_oldContextMenuPolicy;
306 
307  QString getWord(int pos, int* start = 0, int* end = 0) const;
308  void insertWord(int start, int end, const QString& word);
309  bool isAttached() const{ return m_textEdit != 0; }
310  void setTextEdit(TextEditProxy* textEdit);
311  bool eventFilter(QObject *obj, QEvent *event);
312 
313 private slots:
314  void slotShowContextMenu(const QPoint& pos);
315  void slotCheckDocumentChanged();
316  void slotDetachTextEdit();
317  void slotCheckRange(int pos, int removed, int added);
318 };
319 
320 } // QtSpell
321 
322 #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:309
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:219
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