1 | /* |
2 | * highlighter.h |
3 | * |
4 | * Copyright (C) 2004 Zack Rusin <zack@kde.org> |
5 | * |
6 | * This library is free software; you can redistribute it and/or |
7 | * modify it under the terms of the GNU Lesser General Public |
8 | * License as published by the Free Software Foundation; either |
9 | * version 2.1 of the License, or (at your option) any later version. |
10 | * |
11 | * This library is distributed in the hope that it will be useful, |
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
14 | * Lesser General Public License for more details. |
15 | * |
16 | * You should have received a copy of the GNU Lesser General Public |
17 | * License along with this library; if not, write to the Free Software |
18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
19 | * 02110-1301 USA |
20 | */ |
21 | #ifndef SONNET_HIGHLIGHTER_H |
22 | #define SONNET_HIGHLIGHTER_H |
23 | |
24 | #include <QtGui/QSyntaxHighlighter> |
25 | #include <QtCore/QStringList> |
26 | #include <kdemacros.h> |
27 | #include <kdeui_export.h> |
28 | |
29 | class QTextEdit; |
30 | |
31 | namespace Sonnet |
32 | { |
33 | /// The Sonnet Highlighter |
34 | class KDEUI_EXPORT Highlighter : public QSyntaxHighlighter |
35 | { |
36 | Q_OBJECT |
37 | public: |
38 | explicit Highlighter(QTextEdit *textEdit, |
39 | const QString &configFile = QString(), |
40 | const QColor &col=QColor()); |
41 | ~Highlighter(); |
42 | |
43 | bool spellCheckerFound() const; |
44 | |
45 | QString currentLanguage() const; |
46 | void setCurrentLanguage(const QString &lang); |
47 | |
48 | static QStringList personalWords(); |
49 | |
50 | /** |
51 | * @short Enable/Disable spell checking. |
52 | * |
53 | * If @p active is true then spell checking is enabled; otherwise it |
54 | * is disabled. Note that you have to disable automatic (de)activation |
55 | * with @ref setAutomatic() before you change the state of spell |
56 | * checking if you want to persistently enable/disable spell |
57 | * checking. |
58 | * |
59 | * @param active if true, then spell checking is enabled |
60 | * |
61 | * @see isActive(), setAutomatic() |
62 | */ |
63 | void setActive(bool active); |
64 | |
65 | /** |
66 | * Returns the state of spell checking. |
67 | * |
68 | * @return true if spell checking is active |
69 | * |
70 | * @see setActive() |
71 | */ |
72 | bool isActive() const; |
73 | |
74 | bool automatic() const; |
75 | |
76 | void setAutomatic(bool automatic); |
77 | |
78 | /** |
79 | * Adds the given word permanently to the dictionary. It will never |
80 | * be marked as misspelled again, even after restarting the application. |
81 | * |
82 | * @param word the word which will be added to the dictionary |
83 | * @since 4.1 |
84 | */ |
85 | void addWordToDictionary(const QString &word); |
86 | |
87 | /** |
88 | * Ignores the given word. This word will not be marked misspelled for |
89 | * this session. It will again be marked as misspelled when creating |
90 | * new highlighters. |
91 | * |
92 | * @param word the word which will be ignored |
93 | * @since 4.1 |
94 | */ |
95 | void ignoreWord(const QString &word); |
96 | |
97 | /** |
98 | * Returns a list of suggested replacements for the given misspelled word. |
99 | * If the word is not misspelled, the list will be empty. |
100 | * |
101 | * @param word the misspelled word |
102 | * @param max at most this many suggestions will be returned. If this is |
103 | * -1, as many suggestions as the spell backend supports will |
104 | * be returned. |
105 | * @return a list of suggested replacements for the word |
106 | * @since 4.1 |
107 | */ |
108 | QStringList suggestionsForWord(const QString &word, int max = 10 ); |
109 | |
110 | /** |
111 | * Checks if a given word is marked as misspelled by the highlighter. |
112 | * |
113 | * @param word the word to be checked |
114 | * @return true if the given word is misspelled. |
115 | * @since 4.1 |
116 | */ |
117 | bool isWordMisspelled(const QString &word); |
118 | |
119 | /** |
120 | * Sets the color in which the highlighter underlines misspelled words. |
121 | * @since 4.2 |
122 | */ |
123 | void setMisspelledColor(const QColor &color); |
124 | |
125 | /** |
126 | * Return true if checker is enabled by default |
127 | * @since 4.5 |
128 | */ |
129 | bool checkerEnabledByDefault() const; |
130 | |
131 | Q_SIGNALS: |
132 | |
133 | /** |
134 | * Emitted when as-you-type spell checking is enabled or disabled. |
135 | * |
136 | * @param description is a i18n description of the new state, |
137 | * with an optional reason |
138 | */ |
139 | void activeChanged(const QString &description); |
140 | |
141 | /** |
142 | * |
143 | * @param originalWord missspelled word |
144 | * |
145 | * @param suggestions list of word which can replace missspelled word |
146 | * |
147 | * @deprecated use isWordMisspelled() and suggestionsForWord() instead. |
148 | */ |
149 | QT_MOC_COMPAT void newSuggestions(const QString &originalWord, const QStringList &suggestions); |
150 | |
151 | protected: |
152 | |
153 | virtual void highlightBlock(const QString &text); |
154 | virtual void setMisspelled(int start, int count); |
155 | virtual void unsetMisspelled(int start, int count); |
156 | |
157 | bool eventFilter(QObject *o, QEvent *e); |
158 | bool intraWordEditing() const; |
159 | void setIntraWordEditing(bool editing); |
160 | |
161 | public Q_SLOTS: |
162 | void slotAutoDetection(); |
163 | void slotRehighlight(); |
164 | private: |
165 | virtual void connectNotify(const char* signal); |
166 | virtual void disconnectNotify(const char* signal); |
167 | class Private; |
168 | Private *const d; |
169 | Q_DISABLE_COPY( Highlighter ) |
170 | }; |
171 | |
172 | } |
173 | |
174 | #endif |
175 | |