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
26class QMenu;
27class QPlainTextEdit;
28class QPoint;
29class QTextEdit;
30
34namespace QtSpell {
35
36class CheckerPrivate;
37class TextEditCheckerPrivate;
38
42bool QTSPELL_API checkLanguageInstalled(const QString& lang);
43
45
49class QTSPELL_API Checker : public QObject
50{
51 Q_OBJECT
52public:
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
158public slots:
163 void setSpellingEnabled(bool enabled);
164
165signals:
171 void languageChanged(const QString& newLang);
172
173protected:
174 void showContextMenu(QMenu* menu, const QPoint& pos, int wordPos);
175
176private slots:
177 void slotAddWord();
178 void slotIgnoreWord();
179 void slotReplaceWord();
180 void slotSetLanguage(bool checked);
181
182private:
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
206protected:
207 Checker(CheckerPrivate& dd, QObject* parent = 0);
208 CheckerPrivate* d_ptr;
209
210private:
211 Q_DECLARE_PRIVATE(Checker)
212};
213
215
220class QTSPELL_API TextEditChecker : public Checker
221{
222 Q_OBJECT
223public:
227 TextEditChecker(QObject* parent = 0);
228
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
275public slots:
282
283 void undo();
290 void redo();
291
298 void clearUndoRedo();
299
300signals:
308 void undoAvailable(bool available);
309
317 void redoAvailable(bool available);
318
319private:
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
325private slots:
326 void slotShowContextMenu(const QPoint& pos);
327 void slotCheckDocumentChanged();
328 void slotDetachTextEdit();
329 void slotCheckRange(int pos, int removed, int added);
330
331private:
332 Q_DECLARE_PRIVATE(TextEditChecker)
333};
334
335} // QtSpell
336
337#endif // QTSPELL_HPP
void setShowCheckSpellingCheckbox(bool show)
Set whether to display an "Check spelling" checkbox in the UI.
Definition Checker.cpp:178
Checker(QObject *parent=0)
QtSpell::Checker object constructor.
Definition Checker.cpp:101
bool setLanguage(const QString &lang)
Set the spell checking language.
Definition Checker.cpp:122
virtual bool isAttached() const =0
Returns whether a widget is attached to the checker.
void setDecodeLanguageCodes(bool decode)
Set whether to decode language codes in the UI.
Definition Checker.cpp:166
QList< QString > getSpellingSuggestions(const QString &word) const
Retreive a list of spelling suggestions for the misspelled word.
Definition Checker.cpp:227
static QList< QString > getLanguageList()
Requests the list of languages available for spell checking.
Definition Checker.cpp:241
bool getShowCheckSpellingCheckbox() const
Return whether a "Check spelling" checkbox is displayed in the UI.
Definition Checker.cpp:184
bool getSpellingEnabled() const
Return whether spellchecking is performed.
Definition Checker.cpp:190
bool getDecodeLanguageCodes() const
Return whether langauge codes are decoded in the UI.
Definition Checker.cpp:172
void setSpellingEnabled(bool enabled)
Set whether spell checking should be performed.
Definition Checker.cpp:265
bool checkWord(const QString &word) const
Check the specified word.
Definition Checker.cpp:204
void languageChanged(const QString &newLang)
This signal is emitted when the user selects a new language from the spellchecker UI.
virtual void checkSpelling(int start=0, int end=-1)=0
Check the spelling.
virtual QString getWord(int pos, int *start=0, int *end=0) const =0
Get the word at the specified cursor position.
virtual void insertWord(int start, int end, const QString &word)=0
Replaces the specified range with the specified word.
static QString decodeLanguageCode(const QString &lang)
Translates a language code to a human readable format (i.e. "en_US" -> "English (United States)").
Definition Checker.cpp:250
QString getLanguage() const
Retreive the current spelling language.
Definition Checker.cpp:132
void addWordToDictionary(const QString &word)
Add the specified word to the user dictionary.
Definition Checker.cpp:196
void ignoreWord(const QString &word) const
Ignore a word for the current session.
Definition Checker.cpp:221
TextEditChecker(QObject *parent=0)
TextEditChecker object constructor.
void setUndoRedoEnabled(bool enabled)
Sets whether undo/redo functionality is enabled.
void clearUndoRedo()
Clears the undo/redo stack.
void setTextEdit(QTextEdit *textEdit)
Set the QTextEdit to check.
void checkSpelling(int start=0, int end=-1)
Check the spelling.
void setNoSpellingPropertyId(int propertyId)
Set the QTextCharFormat property identifier which marks whether a word ought to be spell-checked.
bool isAttached() const
Returns whether a widget is attached to the checker.
void redo()
Redo the last edit operation.
void redoAvailable(bool available)
Emitted when the redo stak changes.
void undo()
Undo the last edit operation.
void undoAvailable(bool available)
Emitted when the undo stack changes.
int noSpellingPropertyId() const
Returns the current QTextCharFormat property identifier which marks whether a word ought to be spell-...
void insertWord(int start, int end, const QString &word)
Replaces the specified range with the specified word.
QString getWord(int pos, int *start=0, int *end=0) const
Get the word at the specified cursor position.
QtSpell namespace.
Definition Checker.cpp:77
bool checkLanguageInstalled(const QString &lang)
Check whether the dictionary for a language is installed.
Definition Checker.cpp:96