kdeui: format and indent

Signed-off-by: Ivailo Monev <xakepa10@gmail.com>
This commit is contained in:
Ivailo Monev 2024-04-06 22:21:26 +03:00
parent 4f55f6b021
commit afd9c69c7b
2 changed files with 432 additions and 400 deletions

View file

@ -50,6 +50,13 @@
#include <kspellhighlighter.h>
#include <QDebug>
static void deleteWord(QTextCursor cursor, const QTextCursor::MoveOperation op)
{
cursor.clearSelection();
cursor.movePosition(op, QTextCursor::KeepAnchor);
cursor.removeSelectedText();
}
class KTextEdit::Private
{
public:
@ -59,7 +66,13 @@ class KTextEdit::Private
checkSpellingEnabled(false),
findReplaceEnabled(true),
showTabAction(true),
highlighter( 0 ), findDlg(0),find(0),repDlg(0),replace(0), findIndex(0), repIndex(0),
highlighter(nullptr),
findDlg(nullptr),
find(nullptr),
repDlg(nullptr),
replace(nullptr),
findIndex(0),
repIndex(0),
lastReplacedPosition(-1)
{
//Check the default settings to see if spellchecking should be enabled.
@ -117,12 +130,12 @@ class KTextEdit::Private
QAction *autoSpellCheckAction;
QAction *allowTab;
QString clickMessage;
bool italicizePlaceholder : 1;
bool customPalette : 1;
bool italicizePlaceholder;
bool customPalette;
bool checkSpellingEnabled : 1;
bool findReplaceEnabled: 1;
bool showTabAction: 1;
bool checkSpellingEnabled;
bool findReplaceEnabled;
bool showTabAction;
QTextDocumentFragment originalDoc;
QString spellCheckingLanguage;
KSpellHighlighter *highlighter;
@ -156,12 +169,12 @@ void KTextEdit::Private::slotAllowTab()
void KTextEdit::Private::menuActivated(QAction *action)
{
if ( action == autoSpellCheckAction )
if (action == autoSpellCheckAction) {
toggleAutoSpellCheck();
else if ( action == allowTab )
} else if (action == allowTab) {
slotAllowTab();
}
}
void KTextEdit::Private::slotFindHighlight(const QString &text, int matchingIndex, int matchingLength)
{
@ -174,8 +187,8 @@ void KTextEdit::Private::slotFindHighlight(const QString& text, int matchingInde
parent->ensureCursorVisible();
}
void KTextEdit::Private::slotReplaceText(const QString &text, int replacementIndex, int replacedLength, int matchedLength) {
void KTextEdit::Private::slotReplaceText(const QString &text, int replacementIndex, int replacedLength, int matchedLength)
{
// kDebug() << "Replace: [" << text << "] ri:" << replacementIndex << " rl:" << replacedLength << " ml:" << matchedLength;
QTextCursor tc = parent->textCursor();
tc.setPosition(replacementIndex);
@ -199,18 +212,22 @@ QRect KTextEdit::Private::clickMessageRect() const
void KTextEdit::Private::init()
{
KCursor::setAutoHideCursor(parent, true, false);
parent->connect(parent, SIGNAL(languageChanged(QString)),
parent, SLOT(setSpellCheckingLanguage(QString)));
parent->connect(
parent, SIGNAL(languageChanged(QString)),
parent, SLOT(setSpellCheckingLanguage(QString))
);
}
KTextEdit::KTextEdit(const QString &text, QWidget *parent)
: QTextEdit( text, parent ), d( new Private( this ) )
: QTextEdit(text, parent),
d( new Private(this))
{
d->init();
}
KTextEdit::KTextEdit(QWidget *parent)
: QTextEdit( parent ), d( new Private( this ) )
: QTextEdit(parent),
d(new Private(this))
{
d->init();
}
@ -252,7 +269,7 @@ bool KTextEdit::event(QEvent* ev)
bool KTextEdit::Private::handleShortcut(const QKeyEvent *event)
{
const int key = event->key() | event->modifiers();
const int key = (event->key() | event->modifiers());
if (KStandardShortcut::copy().contains(key)) {
parent->copy();
@ -264,20 +281,24 @@ bool KTextEdit::Private::handleShortcut(const QKeyEvent* event)
parent->cut();
return true;
} else if (KStandardShortcut::undo().contains(key)) {
if(!parent->isReadOnly())
if (!parent->isReadOnly()) {
parent->undo();
}
return true;
} else if (KStandardShortcut::redo().contains(key)) {
if(!parent->isReadOnly())
if (!parent->isReadOnly()) {
parent->redo();
}
return true;
} else if (KStandardShortcut::deleteWordBack().contains(key)) {
if (!parent->isReadOnly())
if (!parent->isReadOnly()) {
parent->deleteWordBack();
}
return true;
} else if ( KStandardShortcut::deleteWordForward().contains(key)) {
if (!parent->isReadOnly())
if (!parent->isReadOnly()) {
parent->deleteWordForward();
}
return true;
} else if ( KStandardShortcut::backwardWord().contains(key)) {
QTextCursor cursor = parent->textCursor();
@ -352,25 +373,21 @@ bool KTextEdit::Private::handleShortcut(const QKeyEvent* event)
parent->slotFindNext();
return true;
} else if (findReplaceEnabled && KStandardShortcut::replace().contains(key)) {
if (!parent->isReadOnly())
if (!parent->isReadOnly()) {
parent->slotReplace();
}
return true;
} else if (KStandardShortcut::pasteSelection().contains(key)) {
QString text = QApplication::clipboard()->text(QClipboard::Selection);
if ( !text.isEmpty() )
parent->insertPlainText( text ); // TODO: check if this is html? (MiB)
if (!text.isEmpty()) {
// TODO: check if this is html? (MiB)
parent->insertPlainText(text);
}
return true;
}
return false;
}
static void deleteWord(QTextCursor cursor, QTextCursor::MoveOperation op)
{
cursor.clearSelection();
cursor.movePosition( op, QTextCursor::KeepAnchor );
cursor.removeSelectedText();
}
void KTextEdit::deleteWordBack()
{
deleteWord(textCursor(), QTextCursor::PreviousWord);
@ -384,14 +401,17 @@ void KTextEdit::deleteWordForward()
QMenu *KTextEdit::mousePopupMenu()
{
QMenu *popup = createStandardContextMenu();
if (!popup) return 0;
connect( popup, SIGNAL(triggered(QAction*)),
this, SLOT(menuActivated(QAction*)) );
if (!popup) {
return nullptr;
}
connect(
popup, SIGNAL(triggered(QAction*)),
this, SLOT(menuActivated(QAction*))
);
const bool emptyDocument = document()->isEmpty();
if( !isReadOnly() )
{
if (!isReadOnly()) {
popup->addSeparator();
d->autoSpellCheckAction = popup->addAction(i18n("Auto Spell Check"));
d->autoSpellCheckAction->setCheckable( true );
@ -436,9 +456,11 @@ void KTextEdit::contextMenuEvent(QContextMenuEvent *event)
QTextCursor cursor = textCursor();
// Check if the user clicked a selected word
const bool selectedWordClicked = cursor.hasSelection() &&
const bool selectedWordClicked = (
cursor.hasSelection() &&
mousePos >= cursor.selectionStart() &&
mousePos <= cursor.selectionEnd();
mousePos <= cursor.selectionEnd()
);
// Get the word under the (mouse-)cursor and see if it is misspelled.
// Don't include apostrophes at the start/end of the word in the selection.
@ -460,27 +482,33 @@ void KTextEdit::contextMenuEvent(QContextMenuEvent *event)
selectedWord = selectedWord.right(selectedWord.size() - 1);
wordSelectCursor.movePosition(QTextCursor::NextCharacter, QTextCursor::MoveAnchor);
}
if (selectedWord.endsWith('\'') || selectedWord.endsWith('\"'))
if (selectedWord.endsWith('\'') || selectedWord.endsWith('\"')) {
selectedWord.chop(1);
}
wordSelectCursor.movePosition(QTextCursor::NextCharacter,
QTextCursor::KeepAnchor, selectedWord.size());
wordSelectCursor.movePosition(
QTextCursor::NextCharacter,
QTextCursor::KeepAnchor, selectedWord.size()
);
const bool wordIsMisspelled = isMouseCursorInsideWord &&
const bool wordIsMisspelled = (
isMouseCursorInsideWord &&
checkSpellingEnabled() &&
!selectedWord.isEmpty() &&
highlighter() &&
highlighter()->isWordMisspelled(selectedWord);
highlighter()->isWordMisspelled(selectedWord)
);
// If the user clicked a selected word, do nothing.
// If the user clicked somewhere else, move the cursor there.
// If the user clicked on a misspelled word, select that word.
// Same behavior as in OpenOffice Writer.
if (!selectedWordClicked) {
if (wordIsMisspelled)
if (wordIsMisspelled) {
setTextCursor(wordSelectCursor);
else
} else {
setTextCursor(cursorAtMouse);
}
cursor = textCursor();
}
@ -501,8 +529,7 @@ void KTextEdit::contextMenuEvent(QContextMenuEvent *event)
if (reps.isEmpty()) {
QAction *suggestionsAction = menu.addAction(i18n("No suggestions for %1", selectedWord));
suggestionsAction->setEnabled(false);
}
else {
} else {
QStringList::const_iterator end(reps.constEnd());
for (QStringList::const_iterator it = reps.constBegin(); it != end; ++it) {
menu.addAction(*it);
@ -522,14 +549,11 @@ void KTextEdit::contextMenuEvent(QContextMenuEvent *event)
if (selectedAction == ignoreAction) {
highlighter()->ignoreWord(selectedWord);
highlighter()->rehighlight();
}
else if (selectedAction == addToDictAction) {
} else if (selectedAction == addToDictAction) {
highlighter()->addWordToDictionary(selectedWord);
highlighter()->rehighlight();
}
} else {
// Other actions can only be one of the suggested words
else {
const QString replacement = selectedAction->text();
Q_ASSERT(reps.contains(replacement));
cursor.insertText(replacement);
@ -558,33 +582,33 @@ void KTextEdit::setHighlighter(KSpellHighlighter *highLighter)
void KTextEdit::setCheckSpellingEnabled(bool check)
{
emit checkSpellingChanged(check);
if ( check == d->checkSpellingEnabled )
if (check == d->checkSpellingEnabled) {
return;
}
// From the above statment we know know that if we're turning checking
// on that we need to create a new highlighter and if we're turning it
// off we should remove the old one.
d->checkSpellingEnabled = check;
if ( check )
{
if (check) {
if (hasFocus()) {
createHighlighter();
if (!spellCheckingLanguage().isEmpty())
if (!spellCheckingLanguage().isEmpty()) {
setSpellCheckingLanguage(spellCheckingLanguage());
}
}
else
{
} else {
delete d->highlighter;
d->highlighter = 0;
d->highlighter = nullptr;
}
}
void KTextEdit::focusInEvent(QFocusEvent *event)
{
if ( d->checkSpellingEnabled && !isReadOnly() && !d->highlighter )
if (d->checkSpellingEnabled && !isReadOnly() && !d->highlighter) {
createHighlighter();
}
QTextEdit::focusInEvent(event);
}
@ -596,15 +620,17 @@ bool KTextEdit::checkSpellingEnabled() const
void KTextEdit::setReadOnly(bool readOnly)
{
if ( !readOnly && hasFocus() && d->checkSpellingEnabled && !d->highlighter )
if (!readOnly && hasFocus() && d->checkSpellingEnabled && !d->highlighter) {
createHighlighter();
}
if ( readOnly == isReadOnly() )
if (readOnly == isReadOnly()) {
return;
}
if (readOnly) {
delete d->highlighter;
d->highlighter = 0;
d->highlighter = nullptr;
d->customPalette = testAttribute(Qt::WA_SetPalette);
QPalette p = palette();
@ -619,9 +645,10 @@ void KTextEdit::setReadOnly( bool readOnly )
p.setColor(QPalette::Base, color);
p.setColor(QPalette::Background, color);
setPalette(p);
} else
} else {
setPalette(QPalette());
}
}
QTextEdit::setReadOnly(readOnly);
}
@ -637,14 +664,14 @@ void KTextEdit::highlightWord( int length, int pos )
void KTextEdit::replace()
{
if ( document()->isEmpty() ) // saves having to track the text changes
if (document()->isEmpty()) {
// saves having to track the text changes
return;
}
if (d->repDlg) {
KWindowSystem::activateWindow(d->repDlg->winId());
} else {
d->repDlg = new KReplaceDialog(this, 0,
QStringList(), QStringList(), false);
d->repDlg = new KReplaceDialog(this, 0, QStringList(), QStringList(), false);
connect(d->repDlg, SIGNAL(okClicked()), this, SLOT(slotDoReplace()));
}
d->repDlg->show();
@ -673,21 +700,25 @@ void KTextEdit::slotDoReplace()
// Connect highlight signal to code which handles highlighting
// of found text.
connect(d->replace, SIGNAL(highlight(QString,int,int)),
this, SLOT(slotFindHighlight(QString,int,int)));
connect(
d->replace, SIGNAL(highlight(QString,int,int)),
this, SLOT(slotFindHighlight(QString,int,int))
);
connect(d->replace, SIGNAL(findNext()), this, SLOT(slotReplaceNext()));
connect(d->replace, SIGNAL(replace(QString,int,int,int)),
this, SLOT(slotReplaceText(QString,int,int,int)));
connect(
d->replace, SIGNAL(replace(QString,int,int,int)),
this, SLOT(slotReplaceText(QString,int,int,int))
);
d->repDlg->close();
slotReplaceNext();
}
void KTextEdit::slotReplaceNext()
{
if (!d->replace)
if (!d->replace) {
return;
}
d->lastReplacedPosition = -1;
if (!(d->replace->options() & KReplaceDialog::PromptOnReplace)) {
@ -697,8 +728,9 @@ void KTextEdit::slotReplaceNext()
KFind::Result res = KFind::NoMatch;
if (d->replace->needData())
if (d->replace->needData()) {
d->replace->setData(toPlainText(), d->repIndex);
}
res = d->replace->replace();
if (!(d->replace->options() & KReplaceDialog::PromptOnReplace)) {
textCursor().endEditBlock(); // #48541
@ -716,7 +748,7 @@ void KTextEdit::slotReplaceNext()
if (res == KFind::NoMatch) {
d->replace->displayFinalDialog();
d->replace->disconnect(this);
d->replace->deleteLater(); // we are in a slot connected to m_replace, don't delete it right away
d->replace->deleteLater(); // in a slot connected to m_replace, don't delete it right away
d->replace = 0;
ensureCursorVisible();
// or if ( m_replace->shouldRestart() ) { reinit (w/o FromCursor) and call slotReplaceNext(); }
@ -725,17 +757,15 @@ void KTextEdit::slotReplaceNext()
}
}
void KTextEdit::slotDoFind()
{
if (!d->findDlg) {
// Should really assert()
return;
}
if( d->findDlg->pattern().isEmpty())
{
if (d->findDlg->pattern().isEmpty()) {
delete d->find;
d->find = 0;
d->find = nullptr;
return;
}
delete d->find;
@ -747,8 +777,10 @@ void KTextEdit::slotDoFind()
// Connect highlight signal to code which handles highlighting
// of found text.
connect(d->find, SIGNAL(highlight(QString,int,int)),
this, SLOT(slotFindHighlight(QString,int,int)));
connect(
d->find, SIGNAL(highlight(QString,int,int)),
this, SLOT(slotFindHighlight(QString,int,int))
);
connect(d->find, SIGNAL(findNext()), this, SLOT(slotFindNext()));
d->findDlg->close();
@ -756,41 +788,41 @@ void KTextEdit::slotDoFind()
slotFindNext();
}
void KTextEdit::slotFindNext()
{
if (!d->find)
if (!d->find) {
return;
if(document()->isEmpty())
{
}
if (document()->isEmpty()) {
d->find->disconnect(this);
d->find->deleteLater(); // we are in a slot connected to m_find, don't delete right away
d->find = 0;
d->find->deleteLater(); // in a slot connected to m_find, don't delete right away
d->find = nullptr;
return;
}
KFind::Result res = KFind::NoMatch;
if (d->find->needData())
if (d->find->needData()) {
d->find->setData(toPlainText(), d->findIndex);
}
res = d->find->find();
if (res == KFind::NoMatch) {
d->find->displayFinalDialog();
d->find->disconnect(this);
d->find->deleteLater(); // we are in a slot connected to m_find, don't delete right away
d->find = 0;
d->find->deleteLater(); // in a slot connected to m_find, don't delete right away
d->find = nullptr;
// or if ( m_find->shouldRestart() ) { reinit (w/o FromCursor) and call slotFindNext(); }
} else {
// m_find->closeFindNextDialog();
}
}
void KTextEdit::slotFind()
{
if ( document()->isEmpty() ) // saves having to track the text changes
if (document()->isEmpty()) {
// saves having to track the text changes
return;
}
if (d->findDlg) {
KWindowSystem::activateWindow(d->findDlg->winId());
} else {
@ -803,14 +835,14 @@ void KTextEdit::slotFind()
void KTextEdit::slotReplace()
{
if ( document()->isEmpty() ) // saves having to track the text changes
if (document()->isEmpty()) {
// saves having to track the text changes
return;
}
if (d->repDlg) {
KWindowSystem::activateWindow(d->repDlg->winId());
} else {
d->repDlg = new KReplaceDialog(this, 0,
QStringList(), QStringList(), false);
d->repDlg = new KReplaceDialog(this, 0, QStringList(), QStringList(), false);
connect( d->repDlg, SIGNAL(okClicked()), this, SLOT(slotDoReplace()));
}
d->repDlg->show();
@ -828,7 +860,7 @@ void KTextEdit::showTabAction( bool show )
bool KTextEdit::Private::overrideShortcut(const QKeyEvent *event)
{
const int key = event->key() | event->modifiers();
const int key = (event->key() | event->modifiers());
if (KStandardShortcut::copy().contains(key)) {
return true;

View file

@ -1,4 +1,5 @@
/* This file is part of the KDE libraries
/*
This file is part of the KDE libraries
Copyright (C) 2002 Carsten Pfeiffer <pfeiffer@kde.org>
This library is free software; you can redistribute it and/or
@ -43,7 +44,7 @@
* @see QTextEdit
* @author Carsten Pfeiffer <pfeiffer@kde.org>
*/
class KDEUI_EXPORT KTextEdit : public QTextEdit //krazy:exclude=qclasses
class KDEUI_EXPORT KTextEdit : public QTextEdit
{
Q_OBJECT
Q_PROPERTY(QString clickMessage READ clickMessage WRITE setClickMessage)
@ -55,13 +56,13 @@ class KDEUI_EXPORT KTextEdit : public QTextEdit //krazy:exclude=qclasses
* Constructs a KTextEdit object. See QTextEdit::QTextEdit
* for details.
*/
explicit KTextEdit( const QString& text, QWidget *parent = 0 );
explicit KTextEdit(const QString &text, QWidget *parent = nullptr);
/**
* Constructs a KTextEdit object. See QTextEdit::QTextEdit
* for details.
*/
explicit KTextEdit( QWidget *parent = 0 );
explicit KTextEdit(QWidget *parent = nullptr);
/**
* Destroys the KTextEdit object.
@ -180,7 +181,6 @@ class KDEUI_EXPORT KTextEdit : public QTextEdit //krazy:exclude=qclasses
*/
QString clickMessage() const;
/**
* @since 4.10
*/