File indexing completed on 2024-04-28 15:34:21
0001 /* 0002 * highlighter.h 0003 * 0004 * SPDX-FileCopyrightText: 2004 Zack Rusin <zack@kde.org> 0005 * SPDX-FileCopyrightText: 2013 Martin Sandsmark <martin.sandsmark@kde.org> 0006 * 0007 * SPDX-License-Identifier: LGPL-2.1-or-later 0008 */ 0009 #ifndef SONNET_HIGHLIGHTER_H 0010 #define SONNET_HIGHLIGHTER_H 0011 0012 #include "sonnetui_export.h" 0013 #include <QStringList> 0014 #include <QSyntaxHighlighter> 0015 0016 class QTextEdit; 0017 class QPlainTextEdit; 0018 0019 namespace Sonnet 0020 { 0021 class HighlighterPrivate; 0022 /// The Sonnet Highlighter class, used for drawing pretty red lines in text fields 0023 class SONNETUI_EXPORT Highlighter : public QSyntaxHighlighter 0024 { 0025 Q_OBJECT 0026 public: 0027 explicit Highlighter(QTextEdit *textEdit, const QColor &col = QColor()); 0028 0029 /** 0030 * @brief Highlighter 0031 * @param textEdit 0032 * @param col define spellchecking color. 0033 * @since 5.12 0034 */ 0035 explicit Highlighter(QPlainTextEdit *textEdit, const QColor &col = QColor()); 0036 ~Highlighter() override; 0037 0038 /** 0039 * Returns whether a spell checking backend with support for the 0040 * @ref currentLanguage was found. 0041 * 0042 * @return true if spell checking is supported for the current language. 0043 */ 0044 bool spellCheckerFound() const; 0045 0046 /** 0047 * Returns the current language used for spell checking. 0048 * 0049 * @return the language code for the current language. 0050 */ 0051 QString currentLanguage() const; 0052 0053 /** 0054 * @short Enable/Disable spell checking. 0055 * 0056 * If @p active is true then spell checking is enabled; otherwise it 0057 * is disabled. Note that you have to disable automatic (de)activation 0058 * with @ref setAutomatic() before you change the state of spell 0059 * checking if you want to persistently enable/disable spell 0060 * checking. 0061 * 0062 * @param active if true, then spell checking is enabled 0063 * 0064 * @see isActive(), setAutomatic() 0065 */ 0066 void setActive(bool active); 0067 0068 /** 0069 * Returns the state of spell checking. 0070 * 0071 * @return true if spell checking is active 0072 * 0073 * @see setActive() 0074 */ 0075 bool isActive() const; 0076 0077 /** 0078 * Returns the state of the automatic disabling of spell checking. 0079 * 0080 * @return true if spell checking is automatically disabled if there's 0081 * too many errors 0082 */ 0083 bool automatic() const; 0084 0085 /** 0086 * Sets whether to automatically disable spell checking if there's too 0087 * many errors. 0088 * 0089 * @param automatic if true, spell checking will be disabled if there's 0090 * a significant amount of errors. 0091 */ 0092 void setAutomatic(bool automatic); 0093 0094 /** 0095 * Returns whether the automatic language detection is disabled, 0096 * overriding the Sonnet settings. 0097 * 0098 * @return true if the automatic language detection is disabled 0099 * @since 5.71 0100 */ 0101 bool autoDetectLanguageDisabled() const; 0102 0103 /** 0104 * Sets whether to disable the automatic language detection. 0105 * 0106 * @param autoDetectDisabled if true, the language will not be 0107 * detected automatically by the spell checker, even if the option 0108 * is enabled in the Sonnet settings. 0109 * @since 5.71 0110 */ 0111 void setAutoDetectLanguageDisabled(bool autoDetectDisabled); 0112 0113 /** 0114 * Adds the given word permanently to the dictionary. It will never 0115 * be marked as misspelled again, even after restarting the application. 0116 * 0117 * @param word the word which will be added to the dictionary 0118 * @since 4.1 0119 */ 0120 void addWordToDictionary(const QString &word); 0121 0122 /** 0123 * Ignores the given word. This word will not be marked misspelled for 0124 * this session. It will again be marked as misspelled when creating 0125 * new highlighters. 0126 * 0127 * @param word the word which will be ignored 0128 * @since 4.1 0129 */ 0130 void ignoreWord(const QString &word); 0131 0132 /** 0133 * Returns a list of suggested replacements for the given misspelled word. 0134 * If the word is not misspelled, the list will be empty. 0135 * 0136 * @param word the misspelled word 0137 * @param max at most this many suggestions will be returned. If this is 0138 * -1, as many suggestions as the spell backend supports will 0139 * be returned. 0140 * @return a list of suggested replacements for the word 0141 * @since 4.1 0142 */ 0143 QStringList suggestionsForWord(const QString &word, int max = 10); 0144 0145 /** 0146 * Returns a list of suggested replacements for the given misspelled word. 0147 * If the word is not misspelled, the list will be empty. 0148 * 0149 * @param word the misspelled word 0150 * @param cursor the cursor pointing to the beginning of that word. This is used 0151 * to determine the language to use, when AutoDetectLanguage is enabled. 0152 * @param max at most this many suggestions will be returned. If this is 0153 * -1, as many suggestions as the spell backend supports will 0154 * be returned. 0155 * @return a list of suggested replacements for the word 0156 * @since 5.42 0157 */ 0158 QStringList suggestionsForWord(const QString &word, const QTextCursor &cursor, int max = 10); 0159 0160 /** 0161 * Checks if a given word is marked as misspelled by the highlighter. 0162 * 0163 * @param word the word to be checked 0164 * @return true if the given word is misspelled. 0165 * @since 4.1 0166 */ 0167 bool isWordMisspelled(const QString &word); 0168 0169 /** 0170 * Sets the color in which the highlighter underlines misspelled words. 0171 * @since 4.2 0172 */ 0173 void setMisspelledColor(const QColor &color); 0174 0175 /** 0176 * Return true if checker is enabled by default 0177 * @since 4.5 0178 */ 0179 bool checkerEnabledByDefault() const; 0180 0181 /** 0182 * Set a new @ref QTextDocument for this highlighter to operate on. 0183 * 0184 * @param document the new document to operate on. 0185 */ 0186 void setDocument(QTextDocument *document); 0187 0188 Q_SIGNALS: 0189 0190 /** 0191 * Emitted when as-you-type spell checking is enabled or disabled. 0192 * 0193 * @param description is a i18n description of the new state, 0194 * with an optional reason 0195 */ 0196 void activeChanged(const QString &description); 0197 0198 protected: 0199 void highlightBlock(const QString &text) override; 0200 virtual void setMisspelled(int start, int count); 0201 virtual void unsetMisspelled(int start, int count); 0202 0203 bool eventFilter(QObject *o, QEvent *e) override; 0204 bool intraWordEditing() const; 0205 void setIntraWordEditing(bool editing); 0206 0207 public Q_SLOTS: 0208 /** 0209 * Set language to use for spell checking. 0210 * 0211 * @param language the language code for the new language to use. 0212 */ 0213 void setCurrentLanguage(const QString &language); 0214 0215 /** 0216 * Run auto detection, disabling spell checking if too many errors are found. 0217 */ 0218 void slotAutoDetection(); 0219 0220 /** 0221 * Force a new highlighting. 0222 */ 0223 void slotRehighlight(); 0224 0225 private Q_SLOTS: 0226 SONNETUI_NO_EXPORT void contentsChange(int pos, int added, int removed); 0227 0228 private: 0229 HighlighterPrivate *const d; 0230 Q_DISABLE_COPY(Highlighter) 0231 }; 0232 } 0233 0234 #endif