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