QtSpell  0.8.5
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 QTextCursor;
36 class QTextDocument;
37 class QTextEdit;
38 
39 namespace enchant { class Dict; }
40 
41 
45 namespace QtSpell {
46 
50 bool QTSPELL_API checkLanguageInstalled(const QString& lang);
51 
53 
57 class QTSPELL_API Checker : public QObject
58 {
59  Q_OBJECT
60 public:
64  Checker(QObject* parent = 0);
65 
69  virtual ~Checker();
70 
76  virtual void checkSpelling(int start = 0, int end = -1) = 0;
77 
84  bool setLanguage(const QString& lang);
85 
90  const QString& getLanguage() const{ return m_lang; }
91 
97  void setDecodeLanguageCodes(bool decode){ m_decodeCodes = decode; }
98 
103  bool getDecodeLanguageCodes() const{ return m_decodeCodes; }
104 
109  void setShowCheckSpellingCheckbox(bool show) { m_spellingCheckbox = show; }
110 
115  bool getShowCheckSpellingCheckbox() const{ return m_spellingCheckbox; }
116 
121  bool getSpellingEnabled() const{ return m_spellingEnabled; }
122 
127  void addWordToDictionary(const QString& word);
128 
134  bool checkWord(const QString& word) const;
135 
140  void ignoreWord(const QString& word) const;
141 
147  QList<QString> getSpellingSuggestions(const QString& word) const;
148 
149 
154  static QList<QString> getLanguageList();
155 
164  static QString decodeLanguageCode(const QString& lang);
165 
166 public slots:
171  void setSpellingEnabled(bool enabled) { m_spellingEnabled = enabled; checkSpelling(); }
172 
173 signals:
179  void languageChanged(const QString& newLang);
180 
181 protected:
182  void showContextMenu(QMenu* menu, const QPoint& pos, int wordPos);
183 
184 private slots:
185  void slotAddWord();
186  void slotIgnoreWord();
187  void slotReplaceWord();
188  void slotSetLanguage(bool checked);
189 
190 private:
191  enchant::Dict* m_speller;
192  QString m_lang;
193  bool m_decodeCodes;
194  bool m_spellingCheckbox;
195  bool m_spellingEnabled;
196 
204  virtual QString getWord(int pos, int* start = 0, int* end = 0) const = 0;
205 
212  virtual void insertWord(int start, int end, const QString& word) = 0;
213 
218  virtual bool isAttached() const = 0;
219  bool setLanguageInternal(const QString& lang);
220 };
221 
223 
224 class TextEditProxy;
225 class UndoRedoStack;
226 
231 class QTSPELL_API TextEditChecker : public Checker
232 {
233  Q_OBJECT
234 public:
238  TextEditChecker(QObject* parent = 0);
239 
243  ~TextEditChecker();
244 
249  void setTextEdit(QTextEdit* textEdit);
250 
255  void setTextEdit(QPlainTextEdit* textEdit);
256 
266  void setNoSpellingPropertyId(int propertyId){ m_noSpellingProperty = propertyId; }
267 
273  int noSpellingPropertyId() const{ return m_noSpellingProperty; }
274 
275  void checkSpelling(int start = 0, int end = -1);
276 
284  void setUndoRedoEnabled(bool enabled);
285 
286 public slots:
294  void undo();
301  void redo();
302 
309  void clearUndoRedo();
310 
311 signals:
319  void undoAvailable(bool available);
320 
328  void redoAvailable(bool available);
329 
330 private:
331  TextEditProxy* m_textEdit;
332  QTextDocument* m_document;
333  UndoRedoStack* m_undoRedoStack;
334  bool m_undoRedoInProgress;
335  Qt::ContextMenuPolicy m_oldContextMenuPolicy;
336  int m_noSpellingProperty;
337 
338  QString getWord(int pos, int* start = 0, int* end = 0) const;
339  void insertWord(int start, int end, const QString& word);
340  bool isAttached() const{ return m_textEdit != 0; }
341  void setTextEdit(TextEditProxy* textEdit);
342  bool eventFilter(QObject *obj, QEvent *event);
343  bool noSpellingPropertySet(const QTextCursor& cursor) const;
344 
345 private slots:
346  void slotShowContextMenu(const QPoint& pos);
347  void slotCheckDocumentChanged();
348  void slotDetachTextEdit();
349  void slotCheckRange(int pos, int removed, int added);
350 };
351 
352 } // QtSpell
353 
354 #endif // QTSPELL_HPP
bool checkLanguageInstalled(const QString &lang)
Check whether the dictionary for a language is installed.
Definition: Checker.cpp:67
bool getSpellingEnabled() const
Return whether spellchecking is performed.
Definition: QtSpell.hpp:121
bool getDecodeLanguageCodes() const
Return whether langauge codes are decoded in the UI.
Definition: QtSpell.hpp:103
bool isAttached() const
Returns whether a widget is attached to the checker.
Definition: QtSpell.hpp:340
void setDecodeLanguageCodes(bool decode)
Set whether to decode language codes in the UI.
Definition: QtSpell.hpp:97
Checker class for QTextEdit widgets.
Definition: QtSpell.hpp:231
void setShowCheckSpellingCheckbox(bool show)
Set whether to display an "Check spelling" checkbox in the UI.
Definition: QtSpell.hpp:109
void setSpellingEnabled(bool enabled)
Set whether spell checking should be performed.
Definition: QtSpell.hpp:171
int noSpellingPropertyId() const
Returns the current QTextCharFormat property identifier which marks whether a word ought to be spell-...
Definition: QtSpell.hpp:273
QtSpell namespace.
Definition: Checker.cpp:65
void setNoSpellingPropertyId(int propertyId)
Set the QTextCharFormat property identifier which marks whether a word ought to be spell-checked...
Definition: QtSpell.hpp:266
An abstract class providing spell checking support.
Definition: QtSpell.hpp:57
const QString & getLanguage() const
Retreive the current spelling language.
Definition: QtSpell.hpp:90
bool getShowCheckSpellingCheckbox() const
Return whether a "Check spelling" checkbox is displayed in the UI.
Definition: QtSpell.hpp:115