QtSpell  1.0.1
Spell checking for Qt text widgets
QtSpell.hpp
1 /* QtSpell - Spell checking for Qt text widgets.
2  * Copyright (c) 2014-2022 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 #include "QtSpellExport.hpp"
23 
24 #include <QObject>
25 
26 class QMenu;
27 class QPlainTextEdit;
28 class QPoint;
29 class QTextEdit;
30 
34 namespace QtSpell {
35 
36 class CheckerPrivate;
37 class TextEditCheckerPrivate;
38 
42 bool QTSPELL_API checkLanguageInstalled(const QString& lang);
43 
45 
49 class QTSPELL_API Checker : public QObject
50 {
51  Q_OBJECT
52 public:
56  Checker(QObject* parent = 0);
57 
61  virtual ~Checker();
62 
68  virtual void checkSpelling(int start = 0, int end = -1) = 0;
69 
76  bool setLanguage(const QString& lang);
77 
82  QString getLanguage() const;
83 
89  void setDecodeLanguageCodes(bool decode);
90 
95  bool getDecodeLanguageCodes() const;
96 
101  void setShowCheckSpellingCheckbox(bool show);
102 
107  bool getShowCheckSpellingCheckbox() const;
108 
113  bool getSpellingEnabled() const;
114 
119  void addWordToDictionary(const QString& word);
120 
126  bool checkWord(const QString& word) const;
127 
132  void ignoreWord(const QString& word) const;
133 
139  QList<QString> getSpellingSuggestions(const QString& word) const;
140 
141 
146  static QList<QString> getLanguageList();
147 
156  static QString decodeLanguageCode(const QString& lang);
157 
158 public slots:
163  void setSpellingEnabled(bool enabled);
164 
165 signals:
171  void languageChanged(const QString& newLang);
172 
173 protected:
174  void showContextMenu(QMenu* menu, const QPoint& pos, int wordPos);
175 
176 private slots:
177  void slotAddWord();
178  void slotIgnoreWord();
179  void slotReplaceWord();
180  void slotSetLanguage(bool checked);
181 
182 private:
190  virtual QString getWord(int pos, int* start = 0, int* end = 0) const = 0;
191 
198  virtual void insertWord(int start, int end, const QString& word) = 0;
199 
204  virtual bool isAttached() const = 0;
205 
206 protected:
207  Checker(CheckerPrivate& dd, QObject* parent = 0);
208  CheckerPrivate* d_ptr;
209 
210 private:
211  Q_DECLARE_PRIVATE(Checker)
212 };
213 
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 
255  void setNoSpellingPropertyId(int propertyId);
256 
262  int noSpellingPropertyId() const;
263 
264  void checkSpelling(int start = 0, int end = -1);
265 
273  void setUndoRedoEnabled(bool enabled);
274 
275 public slots:
283  void undo();
290  void redo();
291 
298  void clearUndoRedo();
299 
300 signals:
308  void undoAvailable(bool available);
309 
317  void redoAvailable(bool available);
318 
319 private:
320  QString getWord(int pos, int* start = 0, int* end = 0) const;
321  void insertWord(int start, int end, const QString& word);
322  bool isAttached() const;
323  bool eventFilter(QObject *obj, QEvent *event);
324 
325 private slots:
326  void slotShowContextMenu(const QPoint& pos);
327  void slotCheckDocumentChanged();
328  void slotDetachTextEdit();
329  void slotCheckRange(int pos, int removed, int added);
330 
331 private:
332  Q_DECLARE_PRIVATE(TextEditChecker)
333 };
334 
335 } // QtSpell
336 
337 #endif // QTSPELL_HPP
bool checkLanguageInstalled(const QString &lang)
Check whether the dictionary for a language is installed.
Definition: Checker.cpp:96
Checker class for QTextEdit widgets.
Definition: QtSpell.hpp:220
QtSpell namespace.
Definition: Checker.cpp:77
An abstract class providing spell checking support.
Definition: QtSpell.hpp:49