File indexing completed on 2024-04-21 03:58:29

0001 /*
0002     This file is part of the KDE libraries
0003     SPDX-FileCopyrightText: 2002 Carsten Pfeiffer <pfeiffer@kde.org>
0004     SPDX-FileCopyrightText: 2005 Michael Brade <brade@kde.org>
0005     SPDX-FileCopyrightText: 2012 Laurent Montel <montel@kde.org>
0006 
0007    SPDX-License-Identifier: LGPL-2.0-or-later
0008 */
0009 
0010 #ifndef KTEXTEDIT_P_H
0011 #define KTEXTEDIT_P_H
0012 
0013 #include "kfind.h"
0014 #include "kfinddialog.h"
0015 #include "kreplace.h"
0016 #include "kreplacedialog.h"
0017 
0018 #include <Sonnet/SpellCheckDecorator>
0019 #include <Sonnet/Speller>
0020 
0021 #include <QSettings>
0022 #include <QTextDocumentFragment>
0023 #ifdef HAVE_SPEECH
0024 #include <QTextToSpeech>
0025 #endif
0026 
0027 class KTextEditPrivate
0028 {
0029     Q_DECLARE_PUBLIC(KTextEdit)
0030 
0031 public:
0032     explicit KTextEditPrivate(KTextEdit *qq)
0033         : q_ptr(qq)
0034         , customPalette(false)
0035         , spellCheckingEnabled(false)
0036         , findReplaceEnabled(true)
0037         , showTabAction(true)
0038         , showAutoCorrectionButton(false)
0039     {
0040         // Check the default sonnet settings to see if spellchecking should be enabled.
0041         QSettings settings(QStringLiteral("KDE"), QStringLiteral("Sonnet"));
0042         spellCheckingEnabled = settings.value(QStringLiteral("checkerEnabledByDefault"), false).toBool();
0043     }
0044 
0045     virtual ~KTextEditPrivate()
0046     {
0047         delete decorator;
0048         delete findDlg;
0049         delete find;
0050         delete replace;
0051         delete repDlg;
0052         delete speller;
0053 #ifdef HAVE_SPEECH
0054         delete textToSpeech;
0055 #endif
0056     }
0057 
0058     /**
0059      * Checks whether we should/should not consume a key used as a shortcut.
0060      * This makes it possible to handle shortcuts in the focused widget before any
0061      * window-global QAction is triggered.
0062      */
0063     bool overrideShortcut(const QKeyEvent *e);
0064     /**
0065      * Actually handle a shortcut event.
0066      */
0067     bool handleShortcut(const QKeyEvent *e);
0068 
0069     void spellCheckerMisspelling(const QString &text, int pos);
0070     void spellCheckerCorrected(const QString &, int, const QString &);
0071     void spellCheckerAutoCorrect(const QString &, const QString &);
0072     void spellCheckerCanceled();
0073     void spellCheckerFinished();
0074     void toggleAutoSpellCheck();
0075 
0076     void slotFindHighlight(const QString &text, int matchingIndex, int matchingLength);
0077     void slotReplaceText(const QString &text, int replacementIndex, int /*replacedLength*/, int matchedLength);
0078 
0079     /**
0080      * Similar to QTextEdit::clear(), only that it is possible to undo this
0081      * action.
0082      */
0083     void undoableClear();
0084 
0085     void slotAllowTab();
0086     void menuActivated(QAction *action);
0087 
0088     void init();
0089 
0090     void checkSpelling(bool force);
0091 
0092     KTextEdit *const q_ptr;
0093     QAction *autoSpellCheckAction;
0094     QAction *allowTab;
0095     QAction *spellCheckAction;
0096     QMenu *languagesMenu = nullptr;
0097     bool customPalette : 1;
0098 
0099     bool spellCheckingEnabled : 1;
0100     bool findReplaceEnabled : 1;
0101     bool showTabAction : 1;
0102     bool showAutoCorrectionButton : 1;
0103     QTextDocumentFragment originalDoc;
0104     QString spellCheckingLanguage;
0105     Sonnet::SpellCheckDecorator *decorator = nullptr;
0106     Sonnet::Speller *speller = nullptr;
0107     KFindDialog *findDlg = nullptr;
0108     KFind *find = nullptr;
0109     KReplaceDialog *repDlg = nullptr;
0110     KReplace *replace = nullptr;
0111 #ifdef HAVE_SPEECH
0112     QTextToSpeech *textToSpeech = nullptr;
0113 #endif
0114 
0115     int findIndex = 0;
0116     int repIndex = 0;
0117     int lastReplacedPosition = -1;
0118 };
0119 
0120 #endif