kdeui: simplify highlighter management of KTextEdit

instead of virtual method for creating one and a setter now there is only
a setter with ownership of the highlighter belonging to the parent (the
case for the default-created highlighter) or caller of
KTextEdit::setHighlighter()

Signed-off-by: Ivailo Monev <xakepa10@gmail.com>
This commit is contained in:
Ivailo Monev 2024-04-08 12:04:34 +03:00
parent 52a8caa5ee
commit 213163aa45
2 changed files with 16 additions and 102 deletions

View file

@ -128,7 +128,6 @@ class KTextEdit::Private
bool checkSpellingEnabled;
bool findReplaceEnabled;
bool showTabAction;
QString spellCheckingLanguage;
KSpellHighlighter *highlighter;
KFindDialog *findDlg;
KFind *find;
@ -184,10 +183,6 @@ QRect KTextEdit::Private::clickMessageRect() const
void KTextEdit::Private::init()
{
KCursor::setAutoHideCursor(parent, true, false);
parent->connect(
parent, SIGNAL(languageChanged(QString)),
parent, SLOT(setSpellCheckingLanguage(QString))
);
}
KTextEdit::KTextEdit(const QString &text, QWidget *parent)
@ -209,24 +204,6 @@ KTextEdit::~KTextEdit()
delete d;
}
const QString& KTextEdit::spellCheckingLanguage() const
{
return d->spellCheckingLanguage;
}
void KTextEdit::setSpellCheckingLanguage(const QString &_language)
{
if (highlighter()) {
highlighter()->setCurrentLanguage(_language);
highlighter()->rehighlight();
}
if (_language != d->spellCheckingLanguage) {
d->spellCheckingLanguage = _language;
emit languageChanged(_language);
}
}
bool KTextEdit::event(QEvent* ev)
{
if (ev->type() == QEvent::ShortcutOverride) {
@ -535,11 +512,6 @@ void KTextEdit::contextMenuEvent(QContextMenuEvent *event)
}
}
void KTextEdit::createHighlighter()
{
setHighlighter(new KSpellHighlighter(KGlobal::config().data(), this));
}
KSpellHighlighter* KTextEdit::highlighter() const
{
return d->highlighter;
@ -547,7 +519,6 @@ KSpellHighlighter* KTextEdit::highlighter() const
void KTextEdit::setHighlighter(KSpellHighlighter *highLighter)
{
delete d->highlighter;
d->highlighter = highLighter;
}
@ -564,14 +535,15 @@ void KTextEdit::setCheckSpellingEnabled(bool check)
d->checkSpellingEnabled = check;
if (check) {
if (!isReadOnly() && !d->highlighter) {
createHighlighter();
if (!d->spellCheckingLanguage.isEmpty()) {
setSpellCheckingLanguage(d->spellCheckingLanguage);
}
d->highlighter = new KSpellHighlighter(KGlobal::config().data(), this);
}
if (d->highlighter) {
d->highlighter->setDocument(document());
}
} else {
delete d->highlighter;
d->highlighter = nullptr;
if (d->highlighter) {
d->highlighter->setDocument(nullptr);
}
}
emit checkSpellingChanged(check);
@ -589,9 +561,6 @@ void KTextEdit::setReadOnly(bool readOnly)
}
if (readOnly) {
delete d->highlighter;
d->highlighter = nullptr;
d->customPalette = testAttribute(Qt::WA_SetPalette);
QPalette p = palette();
QColor color = p.color(QPalette::Disabled, QPalette::Background);
@ -608,16 +577,11 @@ void KTextEdit::setReadOnly(bool readOnly)
} else {
setPalette(QPalette());
}
if (d->checkSpellingEnabled && !d->highlighter) {
createHighlighter();
if (!d->spellCheckingLanguage.isEmpty()) {
setSpellCheckingLanguage(d->spellCheckingLanguage);
}
}
}
QTextEdit::setReadOnly(readOnly);
setCheckSpellingEnabled(!readOnly);
}
void KTextEdit::highlightWord(int length, int pos)

View file

@ -49,7 +49,6 @@ class KDEUI_EXPORT KTextEdit : public QTextEdit
Q_OBJECT
Q_PROPERTY(QString clickMessage READ clickMessage WRITE setClickMessage)
Q_PROPERTY(bool checkSpellingEnabled READ checkSpellingEnabled WRITE setCheckSpellingEnabled)
Q_PROPERTY(QString spellCheckingLanguage READ spellCheckingLanguage WRITE setSpellCheckingLanguage)
public:
/**
@ -78,9 +77,6 @@ public:
* Turns background spell checking for this text edit on or off.
* Note that spell checking is only available in read-writable KTextEdits.
*
* Enabling spell checking will set back the current highlighter to the one
* returned by createHighlighter().
*
* @see checkSpellingEnabled()
* @see isReadOnly()
* @see setReadOnly()
@ -108,43 +104,23 @@ public:
void highlightWord(int length, int pos);
/**
* Allows to create a specific highlighter if reimplemented.
*
* By default, it creates a normal highlighter.
*
* This highlighter is set each time spell checking is toggled on by
* calling setCheckSpellingEnabled(), but can later be overridden by calling
* setHighlighter().
* Returns the current highlighter, may be null if spell checking is not
* enabled. The default highlighter might be overridden by setHighlighter().
*
* @see setHighlighter()
* @see highlighter()
*/
virtual void createHighlighter();
/**
* Returns the current highlighter, which is 0 if spell checking is disabled.
* The default highlighter is the one created by createHighlighter(), but
* might be overridden by setHighlighter().
*
* @see setHighlighter()
* @see createHighlighter()
*/
KSpellHighlighter* highlighter() const;
/**
* Sets a custom backgound spell highlighter for this text edit.
* Normally, the highlighter returned by createHighlighter() will be
* used to detect and highlight incorrectly spelled words, but this
* function allows to set a custom highlighter.
*
* This has to be called after enabling spell checking with
* setCheckSpellingEnabled(), otherwise it has no effect.
* Normally, the highlighter is created when spell checking is enabled but
* this function allows to set a custom highlighter. Note that ownership
* of the highlighter belongs to the caller.
*
* @see highlighter()
* @see createHighlighter()
* @param highLighter the new highlighter which will be used now
*/
void setHighlighter(KSpellHighlighter *_highLighter);
void setHighlighter(KSpellHighlighter *highLighter);
/**
* Return standard KTextEdit popupMenu
@ -159,14 +135,6 @@ public:
*/
void enableFindReplace(bool enabled);
/**
* @return the spell checking language which was set by
* setSpellCheckingLanguage(), the spellcheck dialog or the spellcheck
* config dialog, or an empty string if that has never been called.
* @since 4.2
*/
const QString& spellCheckingLanguage() const;
/**
* This makes the text edit display a grayed-out hinting text as long as
* the user didn't enter any text. It is often used as indication about
@ -192,15 +160,7 @@ Q_SIGNALS:
*
* @since 4.1
*/
void checkSpellingChanged( bool );
/**
* Emitted when calling setSpellCheckingLanguage().
*
* @param language the new language the user selected
* @since 4.1
*/
void languageChanged(const QString &language);
void checkSpellingChanged(bool check);
/**
* Emitted before the context menu is displayed.
@ -218,16 +178,6 @@ Q_SIGNALS:
void aboutToShowContextMenu(QMenu *menu);
public Q_SLOTS:
/**
* Set the spell check language which will be used for highlighting spelling
* mistakes and for the spellcheck dialog.
* The languageChanged() signal will be emitted when the new language is
* different from the old one.
*
* @since 4.1
*/
void setSpellCheckingLanguage(const QString &language);
/**
* Create replace dialogbox
* @since 4.1