File indexing completed on 2024-04-21 03:58:28
0001 /* 0002 This file is part of the KDE libraries 0003 SPDX-FileCopyrightText: 2002 Carsten Pfeiffer <pfeiffer@kde.org> 0004 0005 SPDX-License-Identifier: LGPL-2.0-or-later 0006 */ 0007 0008 #ifndef KTEXTEDIT_H 0009 #define KTEXTEDIT_H 0010 0011 #include "ktextwidgets_export.h" 0012 0013 #include <QTextEdit> 0014 #include <memory> 0015 #include <sonnet/highlighter.h> 0016 0017 namespace Sonnet 0018 { 0019 class SpellCheckDecorator; 0020 } 0021 0022 class KTextEditPrivate; 0023 0024 /** 0025 * @class KTextEdit ktextedit.h <KTextEdit> 0026 * 0027 * @short A KDE'ified QTextEdit 0028 * 0029 * This is just a little subclass of QTextEdit, implementing 0030 * some standard KDE features, like cursor auto-hiding, configurable 0031 * wheelscrolling (fast-scroll or zoom), spell checking and deleting of entire 0032 * words with Ctrl-Backspace or Ctrl-Delete. 0033 * 0034 * This text edit provides two ways of spell checking: background checking, 0035 * which will mark incorrectly spelled words red, and a spell check dialog, 0036 * which lets the user check and correct all incorrectly spelled words. 0037 * 0038 * Basic rule: whenever you want to use QTextEdit, use KTextEdit! 0039 * 0040 * \image html ktextedit.png "KTextEdit Widget" 0041 * 0042 * @see QTextEdit 0043 * @author Carsten Pfeiffer <pfeiffer@kde.org> 0044 */ 0045 class KTEXTWIDGETS_EXPORT KTextEdit : public QTextEdit // krazy:exclude=qclasses 0046 { 0047 Q_OBJECT 0048 Q_PROPERTY(bool checkSpellingEnabled READ checkSpellingEnabled WRITE setCheckSpellingEnabled) 0049 Q_PROPERTY(QString spellCheckingLanguage READ spellCheckingLanguage WRITE setSpellCheckingLanguage) 0050 0051 public: 0052 /** 0053 * Constructs a KTextEdit object. See QTextEdit::QTextEdit 0054 * for details. 0055 */ 0056 explicit KTextEdit(const QString &text, QWidget *parent = nullptr); 0057 0058 /** 0059 * Constructs a KTextEdit object. See QTextEdit::QTextEdit 0060 * for details. 0061 */ 0062 explicit KTextEdit(QWidget *parent = nullptr); 0063 0064 /** 0065 * Destroys the KTextEdit object. 0066 */ 0067 ~KTextEdit() override; 0068 0069 /** 0070 * Reimplemented to set a proper "deactivated" background color. 0071 */ 0072 virtual void setReadOnly(bool readOnly); 0073 0074 /** 0075 * Turns background spell checking for this text edit on or off. 0076 * Note that spell checking is only available in read-writable KTextEdits. 0077 * 0078 * Enabling spell checking will set back the current highlighter to the one 0079 * returned by createHighlighter(). 0080 * 0081 * @see checkSpellingEnabled() 0082 * @see isReadOnly() 0083 * @see setReadOnly() 0084 */ 0085 virtual void setCheckSpellingEnabled(bool check); 0086 0087 /** 0088 * Returns true if background spell checking is enabled for this text edit. 0089 * Note that it even returns true if this is a read-only KTextEdit, 0090 * where spell checking is actually disabled. 0091 * By default spell checking is disabled. 0092 * 0093 * @see setCheckSpellingEnabled() 0094 */ 0095 virtual bool checkSpellingEnabled() const; 0096 0097 /** 0098 * Returns true if the given paragraph or block should be spellcheck. 0099 * For example, a mail client does not want to check quoted text, and 0100 * would return false here (by checking whether the block starts with a 0101 * quote sign). 0102 * 0103 * Always returns true by default. 0104 * 0105 */ 0106 virtual bool shouldBlockBeSpellChecked(const QString &block) const; 0107 0108 /** 0109 * Selects the characters at the specified position. Any previous 0110 * selection will be lost. The cursor is moved to the first character 0111 * of the new selection. 0112 * 0113 * @param length The length of the selection, in number of characters 0114 * @param pos The position of the first character of the selection 0115 */ 0116 void highlightWord(int length, int pos); 0117 0118 /** 0119 * Allows to create a specific highlighter if reimplemented. 0120 * 0121 * By default, it creates a normal highlighter, based on the config 0122 * file given to setSpellCheckingConfigFileName(). 0123 * 0124 * This highlighter is set each time spell checking is toggled on by 0125 * calling setCheckSpellingEnabled(), but can later be overridden by calling 0126 * setHighlighter(). 0127 * 0128 * @see setHighlighter() 0129 * @see highlighter() 0130 * @see setSpellCheckingConfigFileName() 0131 */ 0132 virtual void createHighlighter(); 0133 0134 /** 0135 * Returns the current highlighter, which is 0 if spell checking is disabled. 0136 * The default highlighter is the one created by createHighlighter(), but 0137 * might be overridden by setHighlighter(). 0138 * 0139 * @see setHighlighter() 0140 * @see createHighlighter() 0141 */ 0142 Sonnet::Highlighter *highlighter() const; 0143 0144 /** 0145 * Sets a custom background spell highlighter for this text edit. 0146 * Normally, the highlighter returned by createHighlighter() will be 0147 * used to detect and highlight incorrectly spelled words, but this 0148 * function allows to set a custom highlighter. 0149 * 0150 * This has to be called after enabling spell checking with 0151 * setCheckSpellingEnabled(), otherwise it has no effect. 0152 * 0153 * Ownership is transferred to the KTextEdit 0154 * 0155 * @see highlighter() 0156 * @see createHighlighter() 0157 * @param highLighter the new highlighter which will be used now 0158 */ 0159 void setHighlighter(Sonnet::Highlighter *_highLighter); 0160 0161 /** 0162 * Return standard KTextEdit popupMenu 0163 * @since 4.1 0164 */ 0165 virtual QMenu *mousePopupMenu(); 0166 0167 /** 0168 * Enable find replace action. 0169 * @since 4.1 0170 */ 0171 void enableFindReplace(bool enabled); 0172 0173 /** 0174 * @return the spell checking language which was set by 0175 * setSpellCheckingLanguage(), the spellcheck dialog or the spellcheck 0176 * config dialog, or an empty string if that has never been called. 0177 * @since 4.2 0178 */ 0179 const QString &spellCheckingLanguage() const; 0180 0181 /** 0182 * @since 4.10 0183 */ 0184 void showTabAction(bool show); 0185 0186 /** 0187 * @since 4.10 0188 */ 0189 void showAutoCorrectButton(bool show); 0190 0191 /** 0192 * @since 4.10 0193 * create a modal spellcheck dialogbox and spellCheckingFinished signal we sent when 0194 * we finish spell checking or spellCheckingCanceled signal when we cancel spell checking 0195 */ 0196 void forceSpellChecking(); 0197 0198 Q_SIGNALS: 0199 /** 0200 * emit signal when we activate or not autospellchecking 0201 * 0202 * @since 4.1 0203 */ 0204 void checkSpellingChanged(bool); 0205 0206 /** 0207 * Signal sends when spell checking is finished/stopped/completed 0208 * @since 4.1 0209 */ 0210 void spellCheckStatus(const QString &); 0211 0212 /** 0213 * Emitted when the user changes the language in the spellcheck dialog 0214 * shown by checkSpelling() or when calling setSpellCheckingLanguage(). 0215 * 0216 * @param language the new language the user selected 0217 * @since 4.1 0218 */ 0219 void languageChanged(const QString &language); 0220 0221 /** 0222 * Emitted before the context menu is displayed. 0223 * 0224 * The signal allows you to add your own entries into the 0225 * the context menu that is created on demand. 0226 * 0227 * NOTE: Do not store the pointer to the QMenu 0228 * provided through since it is created and deleted 0229 * on demand. 0230 * 0231 * @param p the context menu about to be displayed 0232 * @since 4.5 0233 */ 0234 void aboutToShowContextMenu(QMenu *menu); 0235 0236 /** 0237 * @since 4.10 0238 */ 0239 void spellCheckerAutoCorrect(const QString ¤tWord, const QString &autoCorrectWord); 0240 0241 /** 0242 * signal spellCheckingFinished is sent when we finish spell check or we click on "Terminate" button in sonnet dialogbox 0243 * @since 4.10 0244 */ 0245 void spellCheckingFinished(); 0246 0247 /** 0248 * signal spellCheckingCanceled is sent when we cancel spell checking. 0249 * @since 4.10 0250 */ 0251 void spellCheckingCanceled(); 0252 0253 public Q_SLOTS: 0254 0255 /** 0256 * Set the spell check language which will be used for highlighting spelling 0257 * mistakes and for the spellcheck dialog. 0258 * The languageChanged() signal will be emitted when the new language is 0259 * different from the old one. 0260 * 0261 * @since 4.1 0262 */ 0263 void setSpellCheckingLanguage(const QString &language); 0264 0265 /** 0266 * Show a dialog to check the spelling. The spellCheckStatus() signal 0267 * will be emitted when the spell checking dialog is closed. 0268 */ 0269 void checkSpelling(); 0270 0271 /** 0272 * Opens a Sonnet::ConfigDialog for this text edit. 0273 * The spellcheck language of the config dialog is set to the current spellcheck 0274 * language of the textedit. If the user changes the language in that dialog, 0275 * the languageChanged() signal is emitted. 0276 * 0277 * @param configFileName The file which is used to store and load the config 0278 * settings 0279 * @param windowIcon the icon which is used for the titlebar of the spell dialog 0280 * window. Can be empty, then no icon is set. 0281 * 0282 * @since 4.2 0283 */ 0284 void showSpellConfigDialog(const QString &windowIcon = QString()); 0285 0286 /** 0287 * Create replace dialogbox 0288 * @since 4.1 0289 */ 0290 void replace(); 0291 0292 /** 0293 * Add custom spell checker decorator 0294 * @since 5.11 0295 */ 0296 void addTextDecorator(Sonnet::SpellCheckDecorator *decorator); 0297 0298 /** 0299 * @brief clearDecorator clear the spellcheckerdecorator 0300 * @since 5.11 0301 */ 0302 void clearDecorator(); 0303 0304 protected Q_SLOTS: 0305 /** 0306 * @since 4.1 0307 */ 0308 void slotDoReplace(); 0309 void slotReplaceNext(); 0310 void slotDoFind(); 0311 void slotFind(); 0312 void slotFindNext(); 0313 /** 0314 * @since 5.11 0315 */ 0316 void slotFindPrevious(); 0317 void slotReplace(); 0318 /** 0319 * @since 4.3 0320 */ 0321 void slotSpeakText(); 0322 0323 protected: 0324 /** 0325 * Reimplemented to catch "delete word" shortcut events. 0326 */ 0327 bool event(QEvent *) override; 0328 0329 /** 0330 * Reimplemented for internal reasons 0331 */ 0332 void keyPressEvent(QKeyEvent *) override; 0333 0334 /** 0335 * Reimplemented to instantiate a KDictSpellingHighlighter, if 0336 * spellchecking is enabled. 0337 */ 0338 void focusInEvent(QFocusEvent *) override; 0339 0340 /** 0341 * Deletes a word backwards from the current cursor position, 0342 * if available. 0343 */ 0344 virtual void deleteWordBack(); 0345 0346 /** 0347 * Deletes a word forwards from the current cursor position, 0348 * if available. 0349 */ 0350 virtual void deleteWordForward(); 0351 0352 /** 0353 * Reimplemented from QTextEdit to add spelling related items 0354 * when appropriate. 0355 */ 0356 void contextMenuEvent(QContextMenuEvent *) override; 0357 0358 protected: 0359 KTEXTWIDGETS_NO_EXPORT KTextEdit(KTextEditPrivate &dd, const QString &text, QWidget *parent); 0360 KTEXTWIDGETS_NO_EXPORT KTextEdit(KTextEditPrivate &dd, QWidget *parent); 0361 0362 protected: 0363 std::unique_ptr<class KTextEditPrivate> const d_ptr; 0364 0365 private: 0366 Q_DECLARE_PRIVATE(KTextEdit) 0367 }; 0368 0369 #endif // KTEXTEDIT_H