|
QtSpell
0.8.2
Spell checking for Qt text widgets
|
00001 /* QtSpell - Spell checking for Qt text widgets. 00002 * Copyright (c) 2014 Sandro Mani 00003 * 00004 * This program is free software; you can redistribute it and/or modify 00005 * it under the terms of the GNU General Public License as published by 00006 * the Free Software Foundation; either version 2 of the License, or 00007 * (at your option) any later version. 00008 * 00009 * This program is distributed in the hope that it will be useful, 00010 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00012 * GNU General Public License for more details. 00013 * 00014 * You should have received a copy of the GNU General Public License along 00015 * with this program; if not, write to the Free Software Foundation, Inc., 00016 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 00017 */ 00018 00019 #ifndef QTSPELL_HPP 00020 #define QTSPELL_HPP 00021 00022 #if defined(QTSPELL_LIBRARY) 00023 # define QTSPELL_API Q_DECL_EXPORT 00024 #else 00025 # define QTSPELL_API Q_DECL_IMPORT 00026 #endif 00027 00028 #include <QObject> 00029 #include <QRegExp> 00030 00031 class QLineEdit; 00032 class QMenu; 00033 class QPlainTextEdit; 00034 class QPoint; 00035 class QTextCursor; 00036 class QTextDocument; 00037 class QTextEdit; 00038 00039 namespace enchant { class Dict; } 00040 00041 00045 namespace QtSpell { 00046 00050 bool QTSPELL_API checkLanguageInstalled(const QString& lang); 00051 00053 00057 class QTSPELL_API Checker : public QObject 00058 { 00059 Q_OBJECT 00060 public: 00064 Checker(QObject* parent = 0); 00065 00069 virtual ~Checker(); 00070 00076 virtual void checkSpelling(int start = 0, int end = -1) = 0; 00077 00084 bool setLanguage(const QString& lang); 00085 00090 const QString& getLanguage() const{ return m_lang; } 00091 00097 void setDecodeLanguageCodes(bool decode){ m_decodeCodes = decode; } 00098 00103 bool getDecodeLanguageCodes() const{ return m_decodeCodes; } 00104 00109 void setShowCheckSpellingCheckbox(bool show) { m_spellingCheckbox = show; } 00110 00115 bool getShowCheckSpellingCheckbox() const{ return m_spellingCheckbox; } 00116 00121 bool getSpellingEnabled() const{ return m_spellingEnabled; } 00122 00127 void addWordToDictionary(const QString& word); 00128 00134 bool checkWord(const QString& word) const; 00135 00140 void ignoreWord(const QString& word) const; 00141 00147 QList<QString> getSpellingSuggestions(const QString& word) const; 00148 00149 00154 static QList<QString> getLanguageList(); 00155 00164 static QString decodeLanguageCode(const QString& lang); 00165 00166 public slots: 00171 void setSpellingEnabled(bool enabled) { m_spellingEnabled = enabled; checkSpelling(); } 00172 00173 signals: 00179 void languageChanged(const QString& newLang); 00180 00181 protected: 00182 void showContextMenu(QMenu* menu, const QPoint& pos, int wordPos); 00183 00184 private slots: 00185 void slotAddWord(); 00186 void slotIgnoreWord(); 00187 void slotReplaceWord(); 00188 void slotSetLanguage(bool checked); 00189 00190 private: 00191 enchant::Dict* m_speller; 00192 QString m_lang; 00193 bool m_decodeCodes; 00194 bool m_spellingCheckbox; 00195 bool m_spellingEnabled; 00196 00204 virtual QString getWord(int pos, int* start = 0, int* end = 0) const = 0; 00205 00212 virtual void insertWord(int start, int end, const QString& word) = 0; 00213 00218 virtual bool isAttached() const = 0; 00219 bool setLanguageInternal(const QString& lang); 00220 }; 00221 00223 00224 class TextEditProxy; 00225 class UndoRedoStack; 00226 00231 class QTSPELL_API TextEditChecker : public Checker 00232 { 00233 Q_OBJECT 00234 public: 00238 TextEditChecker(QObject* parent = 0); 00239 00243 ~TextEditChecker(); 00244 00249 void setTextEdit(QTextEdit* textEdit); 00250 00255 void setTextEdit(QPlainTextEdit* textEdit); 00256 00266 void setNoSpellingPropertyId(int propertyId){ m_noSpellingProperty = propertyId; } 00267 00273 int noSpellingPropertyId() const{ return m_noSpellingProperty; } 00274 00275 void checkSpelling(int start = 0, int end = -1); 00276 00284 void setUndoRedoEnabled(bool enabled); 00285 00286 public slots: 00294 void undo(); 00301 void redo(); 00302 00309 void clearUndoRedo(); 00310 00311 signals: 00319 void undoAvailable(bool available); 00320 00328 void redoAvailable(bool available); 00329 00330 private: 00331 TextEditProxy* m_textEdit; 00332 QTextDocument* m_document; 00333 UndoRedoStack* m_undoRedoStack; 00334 bool m_undoRedoInProgress; 00335 Qt::ContextMenuPolicy m_oldContextMenuPolicy; 00336 int m_noSpellingProperty; 00337 00338 QString getWord(int pos, int* start = 0, int* end = 0) const; 00339 void insertWord(int start, int end, const QString& word); 00340 bool isAttached() const{ return m_textEdit != 0; } 00341 void setTextEdit(TextEditProxy* textEdit); 00342 bool eventFilter(QObject *obj, QEvent *event); 00343 bool noSpellingPropertySet(const QTextCursor& cursor) const; 00344 00345 private slots: 00346 void slotShowContextMenu(const QPoint& pos); 00347 void slotCheckDocumentChanged(); 00348 void slotDetachTextEdit(); 00349 void slotCheckRange(int pos, int removed, int added); 00350 }; 00351 00352 } // QtSpell 00353 00354 #endif // QTSPELL_HPP
1.7.6.1