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,16 +50,29 @@
#include <kspellhighlighter.h> #include <kspellhighlighter.h>
#include <QDebug> #include <QDebug>
static void deleteWord(QTextCursor cursor, const QTextCursor::MoveOperation op)
{
cursor.clearSelection();
cursor.movePosition(op, QTextCursor::KeepAnchor);
cursor.removeSelectedText();
}
class KTextEdit::Private class KTextEdit::Private
{ {
public: public:
Private( KTextEdit *_parent ) Private(KTextEdit *_parent)
: parent( _parent ), : parent(_parent),
customPalette( false ), customPalette(false),
checkSpellingEnabled( false ), checkSpellingEnabled(false),
findReplaceEnabled(true), findReplaceEnabled(true),
showTabAction(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) lastReplacedPosition(-1)
{ {
//Check the default settings to see if spellchecking should be enabled. //Check the default settings to see if spellchecking should be enabled.
@ -89,15 +102,15 @@ class KTextEdit::Private
* This makes it possible to handle shortcuts in the focused widget before any * This makes it possible to handle shortcuts in the focused widget before any
* window-global QAction is triggered. * window-global QAction is triggered.
*/ */
bool overrideShortcut(const QKeyEvent* e); bool overrideShortcut(const QKeyEvent *e);
/** /**
* Actually handle a shortcut event. * Actually handle a shortcut event.
*/ */
bool handleShortcut(const QKeyEvent* e); bool handleShortcut(const QKeyEvent *e);
void toggleAutoSpellCheck(); void toggleAutoSpellCheck();
void slotFindHighlight(const QString& text, int matchingIndex, int matchingLength); void slotFindHighlight(const QString &text, int matchingIndex, int matchingLength);
void slotReplaceText(const QString &text, int replacementIndex, int /*replacedLength*/, int matchedLength); void slotReplaceText(const QString &text, int replacementIndex, int /*replacedLength*/, int matchedLength);
/** /**
@ -107,7 +120,7 @@ class KTextEdit::Private
void undoableClear(); void undoableClear();
void slotAllowTab(); void slotAllowTab();
void menuActivated( QAction* action ); void menuActivated(QAction *action);
QRect clickMessageRect() const; QRect clickMessageRect() const;
@ -117,12 +130,12 @@ class KTextEdit::Private
QAction *autoSpellCheckAction; QAction *autoSpellCheckAction;
QAction *allowTab; QAction *allowTab;
QString clickMessage; QString clickMessage;
bool italicizePlaceholder : 1; bool italicizePlaceholder;
bool customPalette : 1; bool customPalette;
bool checkSpellingEnabled : 1; bool checkSpellingEnabled;
bool findReplaceEnabled: 1; bool findReplaceEnabled;
bool showTabAction: 1; bool showTabAction;
QTextDocumentFragment originalDoc; QTextDocumentFragment originalDoc;
QString spellCheckingLanguage; QString spellCheckingLanguage;
KSpellHighlighter *highlighter; KSpellHighlighter *highlighter;
@ -136,7 +149,7 @@ class KTextEdit::Private
void KTextEdit::Private::toggleAutoSpellCheck() void KTextEdit::Private::toggleAutoSpellCheck()
{ {
parent->setCheckSpellingEnabled( !parent->checkSpellingEnabled() ); parent->setCheckSpellingEnabled(!parent->checkSpellingEnabled());
} }
void KTextEdit::Private::undoableClear() void KTextEdit::Private::undoableClear()
@ -151,22 +164,22 @@ void KTextEdit::Private::undoableClear()
void KTextEdit::Private::slotAllowTab() void KTextEdit::Private::slotAllowTab()
{ {
parent->setTabChangesFocus( !parent->tabChangesFocus() ); parent->setTabChangesFocus(!parent->tabChangesFocus());
} }
void KTextEdit::Private::menuActivated( QAction* action ) void KTextEdit::Private::menuActivated(QAction *action)
{ {
if ( action == autoSpellCheckAction ) if (action == autoSpellCheckAction) {
toggleAutoSpellCheck(); toggleAutoSpellCheck();
else if ( action == allowTab ) } else if (action == allowTab) {
slotAllowTab(); slotAllowTab();
}
} }
void KTextEdit::Private::slotFindHighlight(const QString &text, int matchingIndex, int matchingLength)
void KTextEdit::Private::slotFindHighlight(const QString& text, int matchingIndex, int matchingLength)
{ {
Q_UNUSED(text) Q_UNUSED(text)
//kDebug() << "Highlight: [" << text << "] mi:" << matchingIndex << " ml:" << matchingLength; // kDebug() << "Highlight: [" << text << "] mi:" << matchingIndex << " ml:" << matchingLength;
QTextCursor tc = parent->textCursor(); QTextCursor tc = parent->textCursor();
tc.setPosition(matchingIndex); tc.setPosition(matchingIndex);
tc.movePosition(QTextCursor::NextCharacter, QTextCursor::KeepAnchor, matchingLength); tc.movePosition(QTextCursor::NextCharacter, QTextCursor::KeepAnchor, matchingLength);
@ -174,9 +187,9 @@ void KTextEdit::Private::slotFindHighlight(const QString& text, int matchingInde
parent->ensureCursorVisible(); 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; // kDebug() << "Replace: [" << text << "] ri:" << replacementIndex << " rl:" << replacedLength << " ml:" << matchedLength;
QTextCursor tc = parent->textCursor(); QTextCursor tc = parent->textCursor();
tc.setPosition(replacementIndex); tc.setPosition(replacementIndex);
tc.movePosition(QTextCursor::NextCharacter, QTextCursor::KeepAnchor, matchedLength); tc.movePosition(QTextCursor::NextCharacter, QTextCursor::KeepAnchor, matchedLength);
@ -199,18 +212,22 @@ QRect KTextEdit::Private::clickMessageRect() const
void KTextEdit::Private::init() void KTextEdit::Private::init()
{ {
KCursor::setAutoHideCursor(parent, true, false); KCursor::setAutoHideCursor(parent, true, false);
parent->connect(parent, SIGNAL(languageChanged(QString)), parent->connect(
parent, SLOT(setSpellCheckingLanguage(QString))); parent, SIGNAL(languageChanged(QString)),
parent, SLOT(setSpellCheckingLanguage(QString))
);
} }
KTextEdit::KTextEdit( const QString& text, QWidget *parent ) KTextEdit::KTextEdit(const QString &text, QWidget *parent)
: QTextEdit( text, parent ), d( new Private( this ) ) : QTextEdit(text, parent),
d( new Private(this))
{ {
d->init(); d->init();
} }
KTextEdit::KTextEdit( QWidget *parent ) KTextEdit::KTextEdit(QWidget *parent)
: QTextEdit( parent ), d( new Private( this ) ) : QTextEdit(parent),
d(new Private(this))
{ {
d->init(); d->init();
} }
@ -241,7 +258,7 @@ void KTextEdit::setSpellCheckingLanguage(const QString &_language)
bool KTextEdit::event(QEvent* ev) bool KTextEdit::event(QEvent* ev)
{ {
if (ev->type() == QEvent::ShortcutOverride) { if (ev->type() == QEvent::ShortcutOverride) {
QKeyEvent *e = static_cast<QKeyEvent *>( ev ); QKeyEvent *e = static_cast<QKeyEvent*>(ev);
if (d->overrideShortcut(e)) { if (d->overrideShortcut(e)) {
e->accept(); e->accept();
return true; return true;
@ -250,46 +267,50 @@ bool KTextEdit::event(QEvent* ev)
return QTextEdit::event(ev); return QTextEdit::event(ev);
} }
bool KTextEdit::Private::handleShortcut(const QKeyEvent* event) 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 ) ) { if (KStandardShortcut::copy().contains(key)) {
parent->copy(); parent->copy();
return true; return true;
} else if ( KStandardShortcut::paste().contains( key ) ) { } else if (KStandardShortcut::paste().contains(key)) {
parent->paste(); parent->paste();
return true; return true;
} else if ( KStandardShortcut::cut().contains( key ) ) { } else if (KStandardShortcut::cut().contains(key)) {
parent->cut(); parent->cut();
return true; return true;
} else if ( KStandardShortcut::undo().contains( key ) ) { } else if (KStandardShortcut::undo().contains(key)) {
if(!parent->isReadOnly()) if (!parent->isReadOnly()) {
parent->undo(); parent->undo();
}
return true; return true;
} else if ( KStandardShortcut::redo().contains( key ) ) { } else if (KStandardShortcut::redo().contains(key)) {
if(!parent->isReadOnly()) if (!parent->isReadOnly()) {
parent->redo(); parent->redo();
}
return true; return true;
} else if ( KStandardShortcut::deleteWordBack().contains( key ) ) { } else if (KStandardShortcut::deleteWordBack().contains(key)) {
if (!parent->isReadOnly()) if (!parent->isReadOnly()) {
parent->deleteWordBack(); parent->deleteWordBack();
}
return true; return true;
} else if ( KStandardShortcut::deleteWordForward().contains( key ) ) { } else if ( KStandardShortcut::deleteWordForward().contains(key)) {
if (!parent->isReadOnly()) if (!parent->isReadOnly()) {
parent->deleteWordForward(); parent->deleteWordForward();
}
return true; return true;
} else if ( KStandardShortcut::backwardWord().contains( key ) ) { } else if ( KStandardShortcut::backwardWord().contains(key)) {
QTextCursor cursor = parent->textCursor(); QTextCursor cursor = parent->textCursor();
cursor.movePosition( QTextCursor::PreviousWord ); cursor.movePosition(QTextCursor::PreviousWord);
parent->setTextCursor( cursor ); parent->setTextCursor(cursor);
return true; return true;
} else if ( KStandardShortcut::forwardWord().contains( key ) ) { } else if (KStandardShortcut::forwardWord().contains(key)) {
QTextCursor cursor = parent->textCursor(); QTextCursor cursor = parent->textCursor();
cursor.movePosition( QTextCursor::NextWord ); cursor.movePosition(QTextCursor::NextWord);
parent->setTextCursor( cursor ); parent->setTextCursor(cursor);
return true; return true;
} else if ( KStandardShortcut::next().contains( key ) ) { } else if ( KStandardShortcut::next().contains(key)) {
QTextCursor cursor = parent->textCursor(); QTextCursor cursor = parent->textCursor();
bool moved = false; bool moved = false;
qreal lastY = parent->cursorRect(cursor).bottom(); qreal lastY = parent->cursorRect(cursor).bottom();
@ -307,7 +328,7 @@ bool KTextEdit::Private::handleShortcut(const QKeyEvent* event)
} }
parent->setTextCursor(cursor); parent->setTextCursor(cursor);
return true; return true;
} else if ( KStandardShortcut::prior().contains( key ) ) { } else if (KStandardShortcut::prior().contains(key)) {
QTextCursor cursor = parent->textCursor(); QTextCursor cursor = parent->textCursor();
bool moved = false; bool moved = false;
qreal lastY = parent->cursorRect(cursor).bottom(); qreal lastY = parent->cursorRect(cursor).bottom();
@ -325,25 +346,25 @@ bool KTextEdit::Private::handleShortcut(const QKeyEvent* event)
} }
parent->setTextCursor(cursor); parent->setTextCursor(cursor);
return true; return true;
} else if ( KStandardShortcut::begin().contains( key ) ) { } else if ( KStandardShortcut::begin().contains(key)) {
QTextCursor cursor = parent->textCursor(); QTextCursor cursor = parent->textCursor();
cursor.movePosition( QTextCursor::Start ); cursor.movePosition(QTextCursor::Start);
parent->setTextCursor( cursor ); parent->setTextCursor(cursor);
return true; return true;
} else if ( KStandardShortcut::end().contains( key ) ) { } else if (KStandardShortcut::end().contains(key)) {
QTextCursor cursor = parent->textCursor(); QTextCursor cursor = parent->textCursor();
cursor.movePosition( QTextCursor::End ); cursor.movePosition(QTextCursor::End);
parent->setTextCursor( cursor ); parent->setTextCursor(cursor);
return true; return true;
} else if ( KStandardShortcut::beginningOfLine().contains( key ) ) { } else if (KStandardShortcut::beginningOfLine().contains(key)) {
QTextCursor cursor = parent->textCursor(); QTextCursor cursor = parent->textCursor();
cursor.movePosition( QTextCursor::StartOfLine ); cursor.movePosition(QTextCursor::StartOfLine);
parent->setTextCursor( cursor ); parent->setTextCursor(cursor);
return true; return true;
} else if ( KStandardShortcut::endOfLine().contains( key ) ) { } else if (KStandardShortcut::endOfLine().contains(key)) {
QTextCursor cursor = parent->textCursor(); QTextCursor cursor = parent->textCursor();
cursor.movePosition( QTextCursor::EndOfLine ); cursor.movePosition(QTextCursor::EndOfLine);
parent->setTextCursor( cursor ); parent->setTextCursor(cursor);
return true; return true;
} else if (findReplaceEnabled && KStandardShortcut::find().contains(key)) { } else if (findReplaceEnabled && KStandardShortcut::find().contains(key)) {
parent->slotFind(); parent->slotFind();
@ -352,25 +373,21 @@ bool KTextEdit::Private::handleShortcut(const QKeyEvent* event)
parent->slotFindNext(); parent->slotFindNext();
return true; return true;
} else if (findReplaceEnabled && KStandardShortcut::replace().contains(key)) { } else if (findReplaceEnabled && KStandardShortcut::replace().contains(key)) {
if (!parent->isReadOnly()) if (!parent->isReadOnly()) {
parent->slotReplace(); parent->slotReplace();
}
return true; return true;
} else if ( KStandardShortcut::pasteSelection().contains( key ) ) { } else if (KStandardShortcut::pasteSelection().contains(key)) {
QString text = QApplication::clipboard()->text( QClipboard::Selection ); QString text = QApplication::clipboard()->text(QClipboard::Selection);
if ( !text.isEmpty() ) if (!text.isEmpty()) {
parent->insertPlainText( text ); // TODO: check if this is html? (MiB) // TODO: check if this is html? (MiB)
parent->insertPlainText(text);
}
return true; return true;
} }
return false; return false;
} }
static void deleteWord(QTextCursor cursor, QTextCursor::MoveOperation op)
{
cursor.clearSelection();
cursor.movePosition( op, QTextCursor::KeepAnchor );
cursor.removeSelectedText();
}
void KTextEdit::deleteWordBack() void KTextEdit::deleteWordBack()
{ {
deleteWord(textCursor(), QTextCursor::PreviousWord); deleteWord(textCursor(), QTextCursor::PreviousWord);
@ -384,23 +401,26 @@ void KTextEdit::deleteWordForward()
QMenu *KTextEdit::mousePopupMenu() QMenu *KTextEdit::mousePopupMenu()
{ {
QMenu *popup = createStandardContextMenu(); QMenu *popup = createStandardContextMenu();
if (!popup) return 0; if (!popup) {
connect( popup, SIGNAL(triggered(QAction*)), return nullptr;
this, SLOT(menuActivated(QAction*)) ); }
connect(
popup, SIGNAL(triggered(QAction*)),
this, SLOT(menuActivated(QAction*))
);
const bool emptyDocument = document()->isEmpty(); const bool emptyDocument = document()->isEmpty();
if( !isReadOnly() ) if (!isReadOnly()) {
{
popup->addSeparator(); popup->addSeparator();
d->autoSpellCheckAction = popup->addAction( i18n( "Auto Spell Check" ) ); d->autoSpellCheckAction = popup->addAction(i18n("Auto Spell Check"));
d->autoSpellCheckAction->setCheckable( true ); d->autoSpellCheckAction->setCheckable( true );
d->autoSpellCheckAction->setChecked( checkSpellingEnabled() ); d->autoSpellCheckAction->setChecked( checkSpellingEnabled());
popup->addSeparator(); popup->addSeparator();
if (d->showTabAction) { if (d->showTabAction) {
d->allowTab = popup->addAction( i18n("Allow Tabulations") ); d->allowTab = popup->addAction(i18n("Allow Tabulations"));
d->allowTab->setCheckable( true ); d->allowTab->setCheckable(true);
d->allowTab->setChecked( !tabChangesFocus() ); d->allowTab->setChecked(!tabChangesFocus());
} }
} }
@ -436,9 +456,11 @@ void KTextEdit::contextMenuEvent(QContextMenuEvent *event)
QTextCursor cursor = textCursor(); QTextCursor cursor = textCursor();
// Check if the user clicked a selected word // Check if the user clicked a selected word
const bool selectedWordClicked = cursor.hasSelection() && const bool selectedWordClicked = (
cursor.hasSelection() &&
mousePos >= cursor.selectionStart() && mousePos >= cursor.selectionStart() &&
mousePos <= cursor.selectionEnd(); mousePos <= cursor.selectionEnd()
);
// Get the word under the (mouse-)cursor and see if it is misspelled. // 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. // 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); selectedWord = selectedWord.right(selectedWord.size() - 1);
wordSelectCursor.movePosition(QTextCursor::NextCharacter, QTextCursor::MoveAnchor); wordSelectCursor.movePosition(QTextCursor::NextCharacter, QTextCursor::MoveAnchor);
} }
if (selectedWord.endsWith('\'') || selectedWord.endsWith('\"')) if (selectedWord.endsWith('\'') || selectedWord.endsWith('\"')) {
selectedWord.chop(1); selectedWord.chop(1);
}
wordSelectCursor.movePosition(QTextCursor::NextCharacter, wordSelectCursor.movePosition(
QTextCursor::KeepAnchor, selectedWord.size()); QTextCursor::NextCharacter,
QTextCursor::KeepAnchor, selectedWord.size()
);
const bool wordIsMisspelled = isMouseCursorInsideWord && const bool wordIsMisspelled = (
isMouseCursorInsideWord &&
checkSpellingEnabled() && checkSpellingEnabled() &&
!selectedWord.isEmpty() && !selectedWord.isEmpty() &&
highlighter() && highlighter() &&
highlighter()->isWordMisspelled(selectedWord); highlighter()->isWordMisspelled(selectedWord)
);
// If the user clicked a selected word, do nothing. // If the user clicked a selected word, do nothing.
// If the user clicked somewhere else, move the cursor there. // If the user clicked somewhere else, move the cursor there.
// If the user clicked on a misspelled word, select that word. // If the user clicked on a misspelled word, select that word.
// Same behavior as in OpenOffice Writer. // Same behavior as in OpenOffice Writer.
if (!selectedWordClicked) { if (!selectedWordClicked) {
if (wordIsMisspelled) if (wordIsMisspelled) {
setTextCursor(wordSelectCursor); setTextCursor(wordSelectCursor);
else } else {
setTextCursor(cursorAtMouse); setTextCursor(cursorAtMouse);
}
cursor = textCursor(); cursor = textCursor();
} }
@ -501,8 +529,7 @@ void KTextEdit::contextMenuEvent(QContextMenuEvent *event)
if (reps.isEmpty()) { if (reps.isEmpty()) {
QAction *suggestionsAction = menu.addAction(i18n("No suggestions for %1", selectedWord)); QAction *suggestionsAction = menu.addAction(i18n("No suggestions for %1", selectedWord));
suggestionsAction->setEnabled(false); suggestionsAction->setEnabled(false);
} } else {
else {
QStringList::const_iterator end(reps.constEnd()); QStringList::const_iterator end(reps.constEnd());
for (QStringList::const_iterator it = reps.constBegin(); it != end; ++it) { for (QStringList::const_iterator it = reps.constBegin(); it != end; ++it) {
menu.addAction(*it); menu.addAction(*it);
@ -511,8 +538,8 @@ void KTextEdit::contextMenuEvent(QContextMenuEvent *event)
menu.addSeparator(); menu.addSeparator();
QAction *ignoreAction = menu.addAction(i18n("Ignore")); QAction* ignoreAction = menu.addAction(i18n("Ignore"));
QAction *addToDictAction = menu.addAction(i18n("Add to Dictionary")); QAction* addToDictAction = menu.addAction(i18n("Add to Dictionary"));
//Execute the popup inline //Execute the popup inline
const QAction *selectedAction = menu.exec(event->globalPos()); const QAction *selectedAction = menu.exec(event->globalPos());
@ -522,14 +549,11 @@ void KTextEdit::contextMenuEvent(QContextMenuEvent *event)
if (selectedAction == ignoreAction) { if (selectedAction == ignoreAction) {
highlighter()->ignoreWord(selectedWord); highlighter()->ignoreWord(selectedWord);
highlighter()->rehighlight(); highlighter()->rehighlight();
} } else if (selectedAction == addToDictAction) {
else if (selectedAction == addToDictAction) {
highlighter()->addWordToDictionary(selectedWord); highlighter()->addWordToDictionary(selectedWord);
highlighter()->rehighlight(); highlighter()->rehighlight();
} } else {
// Other actions can only be one of the suggested words // Other actions can only be one of the suggested words
else {
const QString replacement = selectedAction->text(); const QString replacement = selectedAction->text();
Q_ASSERT(reps.contains(replacement)); Q_ASSERT(reps.contains(replacement));
cursor.insertText(replacement); cursor.insertText(replacement);
@ -557,36 +581,36 @@ void KTextEdit::setHighlighter(KSpellHighlighter *highLighter)
void KTextEdit::setCheckSpellingEnabled(bool check) void KTextEdit::setCheckSpellingEnabled(bool check)
{ {
emit checkSpellingChanged( check ); emit checkSpellingChanged(check);
if ( check == d->checkSpellingEnabled ) if (check == d->checkSpellingEnabled) {
return; return;
}
// From the above statment we know know that if we're turning checking // 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 // on that we need to create a new highlighter and if we're turning it
// off we should remove the old one. // off we should remove the old one.
d->checkSpellingEnabled = check; d->checkSpellingEnabled = check;
if ( check ) if (check) {
{ if (hasFocus()) {
if ( hasFocus() ) {
createHighlighter(); createHighlighter();
if (!spellCheckingLanguage().isEmpty()) if (!spellCheckingLanguage().isEmpty()) {
setSpellCheckingLanguage(spellCheckingLanguage()); setSpellCheckingLanguage(spellCheckingLanguage());
} }
} }
else } else {
{
delete d->highlighter; delete d->highlighter;
d->highlighter = 0; d->highlighter = nullptr;
} }
} }
void KTextEdit::focusInEvent( QFocusEvent *event ) void KTextEdit::focusInEvent(QFocusEvent *event)
{ {
if ( d->checkSpellingEnabled && !isReadOnly() && !d->highlighter ) if (d->checkSpellingEnabled && !isReadOnly() && !d->highlighter) {
createHighlighter(); createHighlighter();
}
QTextEdit::focusInEvent( event ); QTextEdit::focusInEvent(event);
} }
bool KTextEdit::checkSpellingEnabled() const bool KTextEdit::checkSpellingEnabled() const
@ -594,39 +618,42 @@ bool KTextEdit::checkSpellingEnabled() const
return d->checkSpellingEnabled; return d->checkSpellingEnabled;
} }
void KTextEdit::setReadOnly( bool readOnly ) void KTextEdit::setReadOnly(bool readOnly)
{ {
if ( !readOnly && hasFocus() && d->checkSpellingEnabled && !d->highlighter ) if (!readOnly && hasFocus() && d->checkSpellingEnabled && !d->highlighter) {
createHighlighter(); createHighlighter();
if ( readOnly == isReadOnly() )
return;
if ( readOnly ) {
delete d->highlighter;
d->highlighter = 0;
d->customPalette = testAttribute( Qt::WA_SetPalette );
QPalette p = palette();
QColor color = p.color( QPalette::Disabled, QPalette::Background );
p.setColor( QPalette::Base, color );
p.setColor( QPalette::Background, color );
setPalette( p );
} else {
if ( d->customPalette && testAttribute( Qt::WA_SetPalette ) ) {
QPalette p = palette();
QColor color = p.color( QPalette::Normal, QPalette::Base );
p.setColor( QPalette::Base, color );
p.setColor( QPalette::Background, color );
setPalette( p );
} else
setPalette( QPalette() );
} }
QTextEdit::setReadOnly( readOnly ); if (readOnly == isReadOnly()) {
return;
}
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);
p.setColor(QPalette::Base, color);
p.setColor(QPalette::Background, color);
setPalette(p);
} else {
if (d->customPalette && testAttribute(Qt::WA_SetPalette)) {
QPalette p = palette();
QColor color = p.color(QPalette::Normal, QPalette::Base);
p.setColor(QPalette::Base, color);
p.setColor(QPalette::Background, color);
setPalette(p);
} else {
setPalette(QPalette());
}
}
QTextEdit::setReadOnly(readOnly);
} }
void KTextEdit::highlightWord( int length, int pos ) void KTextEdit::highlightWord(int length, int pos)
{ {
QTextCursor cursor(document()); QTextCursor cursor(document());
cursor.setPosition(pos); cursor.setPosition(pos);
@ -637,15 +664,15 @@ void KTextEdit::highlightWord( int length, int pos )
void KTextEdit::replace() void KTextEdit::replace()
{ {
if ( document()->isEmpty() ) // saves having to track the text changes if (document()->isEmpty()) {
// saves having to track the text changes
return; return;
}
if ( d->repDlg ) { if (d->repDlg) {
KWindowSystem::activateWindow( d->repDlg->winId() ); KWindowSystem::activateWindow(d->repDlg->winId());
} else { } else {
d->repDlg = new KReplaceDialog(this, 0, d->repDlg = new KReplaceDialog(this, 0, QStringList(), QStringList(), false);
QStringList(), QStringList(), false); connect(d->repDlg, SIGNAL(okClicked()), this, SLOT(slotDoReplace()));
connect( d->repDlg, SIGNAL(okClicked()), this, SLOT(slotDoReplace()) );
} }
d->repDlg->show(); d->repDlg->show();
} }
@ -657,7 +684,7 @@ void KTextEdit::slotDoReplace()
return; return;
} }
if(d->repDlg->pattern().isEmpty()) { if (d->repDlg->pattern().isEmpty()) {
delete d->replace; delete d->replace;
d->replace = 0; d->replace = 0;
ensureCursorVisible(); ensureCursorVisible();
@ -673,21 +700,25 @@ void KTextEdit::slotDoReplace()
// Connect highlight signal to code which handles highlighting // Connect highlight signal to code which handles highlighting
// of found text. // of found text.
connect(d->replace, SIGNAL(highlight(QString,int,int)), connect(
this, SLOT(slotFindHighlight(QString,int,int))); 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(findNext()), this, SLOT(slotReplaceNext()));
connect(d->replace, SIGNAL(replace(QString,int,int,int)), connect(
this, SLOT(slotReplaceText(QString,int,int,int))); d->replace, SIGNAL(replace(QString,int,int,int)),
this, SLOT(slotReplaceText(QString,int,int,int))
);
d->repDlg->close(); d->repDlg->close();
slotReplaceNext(); slotReplaceNext();
} }
void KTextEdit::slotReplaceNext() void KTextEdit::slotReplaceNext()
{ {
if (!d->replace) if (!d->replace) {
return; return;
}
d->lastReplacedPosition = -1; d->lastReplacedPosition = -1;
if (!(d->replace->options() & KReplaceDialog::PromptOnReplace)) { if (!(d->replace->options() & KReplaceDialog::PromptOnReplace)) {
@ -697,8 +728,9 @@ void KTextEdit::slotReplaceNext()
KFind::Result res = KFind::NoMatch; KFind::Result res = KFind::NoMatch;
if (d->replace->needData()) if (d->replace->needData()) {
d->replace->setData(toPlainText(), d->repIndex); d->replace->setData(toPlainText(), d->repIndex);
}
res = d->replace->replace(); res = d->replace->replace();
if (!(d->replace->options() & KReplaceDialog::PromptOnReplace)) { if (!(d->replace->options() & KReplaceDialog::PromptOnReplace)) {
textCursor().endEditBlock(); // #48541 textCursor().endEditBlock(); // #48541
@ -716,26 +748,24 @@ void KTextEdit::slotReplaceNext()
if (res == KFind::NoMatch) { if (res == KFind::NoMatch) {
d->replace->displayFinalDialog(); d->replace->displayFinalDialog();
d->replace->disconnect(this); 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; d->replace = 0;
ensureCursorVisible(); ensureCursorVisible();
//or if ( m_replace->shouldRestart() ) { reinit (w/o FromCursor) and call slotReplaceNext(); } // or if ( m_replace->shouldRestart() ) { reinit (w/o FromCursor) and call slotReplaceNext(); }
} else { } else {
//m_replace->closeReplaceNextDialog(); // m_replace->closeReplaceNextDialog();
} }
} }
void KTextEdit::slotDoFind() void KTextEdit::slotDoFind()
{ {
if (!d->findDlg) { if (!d->findDlg) {
// Should really assert() // Should really assert()
return; return;
} }
if( d->findDlg->pattern().isEmpty()) if (d->findDlg->pattern().isEmpty()) {
{
delete d->find; delete d->find;
d->find = 0; d->find = nullptr;
return; return;
} }
delete d->find; delete d->find;
@ -747,8 +777,10 @@ void KTextEdit::slotDoFind()
// Connect highlight signal to code which handles highlighting // Connect highlight signal to code which handles highlighting
// of found text. // of found text.
connect(d->find, SIGNAL(highlight(QString,int,int)), connect(
this, SLOT(slotFindHighlight(QString,int,int))); d->find, SIGNAL(highlight(QString,int,int)),
this, SLOT(slotFindHighlight(QString,int,int))
);
connect(d->find, SIGNAL(findNext()), this, SLOT(slotFindNext())); connect(d->find, SIGNAL(findNext()), this, SLOT(slotFindNext()));
d->findDlg->close(); d->findDlg->close();
@ -756,46 +788,46 @@ void KTextEdit::slotDoFind()
slotFindNext(); slotFindNext();
} }
void KTextEdit::slotFindNext() void KTextEdit::slotFindNext()
{ {
if (!d->find) if (!d->find) {
return; return;
if(document()->isEmpty()) }
{ if (document()->isEmpty()) {
d->find->disconnect(this); d->find->disconnect(this);
d->find->deleteLater(); // we are in a slot connected to m_find, don't delete right away d->find->deleteLater(); // in a slot connected to m_find, don't delete right away
d->find = 0; d->find = nullptr;
return; return;
} }
KFind::Result res = KFind::NoMatch; KFind::Result res = KFind::NoMatch;
if (d->find->needData()) if (d->find->needData()) {
d->find->setData(toPlainText(), d->findIndex); d->find->setData(toPlainText(), d->findIndex);
}
res = d->find->find(); res = d->find->find();
if (res == KFind::NoMatch) { if (res == KFind::NoMatch) {
d->find->displayFinalDialog(); d->find->displayFinalDialog();
d->find->disconnect(this); d->find->disconnect(this);
d->find->deleteLater(); // we are in a slot connected to m_find, don't delete right away d->find->deleteLater(); // in a slot connected to m_find, don't delete right away
d->find = 0; d->find = nullptr;
//or if ( m_find->shouldRestart() ) { reinit (w/o FromCursor) and call slotFindNext(); } // or if ( m_find->shouldRestart() ) { reinit (w/o FromCursor) and call slotFindNext(); }
} else { } else {
//m_find->closeFindNextDialog(); // m_find->closeFindNextDialog();
} }
} }
void KTextEdit::slotFind() void KTextEdit::slotFind()
{ {
if ( document()->isEmpty() ) // saves having to track the text changes if (document()->isEmpty()) {
// saves having to track the text changes
return; return;
}
if ( d->findDlg ) { if (d->findDlg) {
KWindowSystem::activateWindow( d->findDlg->winId() ); KWindowSystem::activateWindow(d->findDlg->winId());
} else { } else {
d->findDlg = new KFindDialog(this); d->findDlg = new KFindDialog(this);
connect( d->findDlg, SIGNAL(okClicked()), this, SLOT(slotDoFind()) ); connect( d->findDlg, SIGNAL(okClicked()), this, SLOT(slotDoFind()));
} }
d->findDlg->show(); d->findDlg->show();
} }
@ -803,64 +835,64 @@ void KTextEdit::slotFind()
void KTextEdit::slotReplace() void KTextEdit::slotReplace()
{ {
if ( document()->isEmpty() ) // saves having to track the text changes if (document()->isEmpty()) {
// saves having to track the text changes
return; return;
}
if ( d->repDlg ) { if (d->repDlg) {
KWindowSystem::activateWindow( d->repDlg->winId() ); KWindowSystem::activateWindow(d->repDlg->winId());
} else { } else {
d->repDlg = new KReplaceDialog(this, 0, d->repDlg = new KReplaceDialog(this, 0, QStringList(), QStringList(), false);
QStringList(), QStringList(), false); connect( d->repDlg, SIGNAL(okClicked()), this, SLOT(slotDoReplace()));
connect( d->repDlg, SIGNAL(okClicked()), this, SLOT(slotDoReplace()) );
} }
d->repDlg->show(); d->repDlg->show();
} }
void KTextEdit::enableFindReplace( bool enabled ) void KTextEdit::enableFindReplace(bool enabled)
{ {
d->findReplaceEnabled = enabled; d->findReplaceEnabled = enabled;
} }
void KTextEdit::showTabAction( bool show ) void KTextEdit::showTabAction(bool show)
{ {
d->showTabAction = show; d->showTabAction = show;
} }
bool KTextEdit::Private::overrideShortcut(const QKeyEvent* event) 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 ) ) { if (KStandardShortcut::copy().contains(key)) {
return true; return true;
} else if ( KStandardShortcut::paste().contains( key ) ) { } else if (KStandardShortcut::paste().contains(key)) {
return true; return true;
} else if ( KStandardShortcut::cut().contains( key ) ) { } else if (KStandardShortcut::cut().contains(key)) {
return true; return true;
} else if ( KStandardShortcut::undo().contains( key ) ) { } else if (KStandardShortcut::undo().contains(key)) {
return true; return true;
} else if ( KStandardShortcut::redo().contains( key ) ) { } else if (KStandardShortcut::redo().contains(key)) {
return true; return true;
} else if ( KStandardShortcut::deleteWordBack().contains( key ) ) { } else if ( KStandardShortcut::deleteWordBack().contains(key) ) {
return true; return true;
} else if ( KStandardShortcut::deleteWordForward().contains( key ) ) { } else if (KStandardShortcut::deleteWordForward().contains(key)) {
return true; return true;
} else if ( KStandardShortcut::backwardWord().contains( key ) ) { } else if (KStandardShortcut::backwardWord().contains(key)) {
return true; return true;
} else if ( KStandardShortcut::forwardWord().contains( key ) ) { } else if (KStandardShortcut::forwardWord().contains(key)) {
return true; return true;
} else if ( KStandardShortcut::next().contains( key ) ) { } else if (KStandardShortcut::next().contains(key)) {
return true; return true;
} else if ( KStandardShortcut::prior().contains( key ) ) { } else if (KStandardShortcut::prior().contains(key)) {
return true; return true;
} else if ( KStandardShortcut::begin().contains( key ) ) { } else if (KStandardShortcut::begin().contains(key)) {
return true; return true;
} else if ( KStandardShortcut::end().contains( key ) ) { } else if (KStandardShortcut::end().contains(key)) {
return true; return true;
} else if ( KStandardShortcut::beginningOfLine().contains( key ) ) { } else if (KStandardShortcut::beginningOfLine().contains(key)) {
return true; return true;
} else if ( KStandardShortcut::endOfLine().contains( key ) ) { } else if (KStandardShortcut::endOfLine().contains(key)) {
return true; return true;
} else if ( KStandardShortcut::pasteSelection().contains( key ) ) { } else if (KStandardShortcut::pasteSelection().contains(key) ) {
return true; return true;
} else if (findReplaceEnabled && KStandardShortcut::find().contains(key)) { } else if (findReplaceEnabled && KStandardShortcut::find().contains(key)) {
return true; return true;
@ -879,11 +911,11 @@ bool KTextEdit::Private::overrideShortcut(const QKeyEvent* event)
return false; return false;
} }
void KTextEdit::keyPressEvent( QKeyEvent *event ) void KTextEdit::keyPressEvent(QKeyEvent *event)
{ {
if (d->handleShortcut(event)) { if (d->handleShortcut(event)) {
event->accept(); event->accept();
}else if (event->modifiers() == Qt::ControlModifier && } else if (event->modifiers() == Qt::ControlModifier &&
(event->key() == Qt::Key_Return || event->key() == Qt::Key_Enter) && (event->key() == Qt::Key_Return || event->key() == Qt::Key_Enter) &&
qobject_cast<KDialog*>(window()) ) { qobject_cast<KDialog*>(window()) ) {
event->ignore(); event->ignore();

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> Copyright (C) 2002 Carsten Pfeiffer <pfeiffer@kde.org>
This library is free software; you can redistribute it and/or This library is free software; you can redistribute it and/or
@ -43,25 +44,25 @@
* @see QTextEdit * @see QTextEdit
* @author Carsten Pfeiffer <pfeiffer@kde.org> * @author Carsten Pfeiffer <pfeiffer@kde.org>
*/ */
class KDEUI_EXPORT KTextEdit : public QTextEdit //krazy:exclude=qclasses class KDEUI_EXPORT KTextEdit : public QTextEdit
{ {
Q_OBJECT Q_OBJECT
Q_PROPERTY( QString clickMessage READ clickMessage WRITE setClickMessage ) Q_PROPERTY(QString clickMessage READ clickMessage WRITE setClickMessage)
Q_PROPERTY( bool checkSpellingEnabled READ checkSpellingEnabled WRITE setCheckSpellingEnabled ) Q_PROPERTY(bool checkSpellingEnabled READ checkSpellingEnabled WRITE setCheckSpellingEnabled)
Q_PROPERTY( QString spellCheckingLanguage READ spellCheckingLanguage WRITE setSpellCheckingLanguage ) Q_PROPERTY(QString spellCheckingLanguage READ spellCheckingLanguage WRITE setSpellCheckingLanguage)
public: public:
/** /**
* Constructs a KTextEdit object. See QTextEdit::QTextEdit * Constructs a KTextEdit object. See QTextEdit::QTextEdit
* for details. * 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 * Constructs a KTextEdit object. See QTextEdit::QTextEdit
* for details. * for details.
*/ */
explicit KTextEdit( QWidget *parent = 0 ); explicit KTextEdit(QWidget *parent = nullptr);
/** /**
* Destroys the KTextEdit object. * Destroys the KTextEdit object.
@ -71,7 +72,7 @@ class KDEUI_EXPORT KTextEdit : public QTextEdit //krazy:exclude=qclasses
/** /**
* Reimplemented to set a proper "deactivated" background color. * Reimplemented to set a proper "deactivated" background color.
*/ */
virtual void setReadOnly( bool readOnly ); virtual void setReadOnly(bool readOnly);
/** /**
* Turns background spell checking for this text edit on or off. * Turns background spell checking for this text edit on or off.
@ -84,7 +85,7 @@ class KDEUI_EXPORT KTextEdit : public QTextEdit //krazy:exclude=qclasses
* @see isReadOnly() * @see isReadOnly()
* @see setReadOnly() * @see setReadOnly()
*/ */
void setCheckSpellingEnabled( bool check ); void setCheckSpellingEnabled(bool check);
/** /**
* Returns true if background spell checking is enabled for this text edit. * Returns true if background spell checking is enabled for this text edit.
@ -104,7 +105,7 @@ class KDEUI_EXPORT KTextEdit : public QTextEdit //krazy:exclude=qclasses
* @param length The length of the selection, in number of characters * @param length The length of the selection, in number of characters
* @param pos The position of the first character of the selection * @param pos The position of the first character of the selection
*/ */
void highlightWord( int length, int pos ); void highlightWord(int length, int pos);
/** /**
* Allows to create a specific highlighter if reimplemented. * Allows to create a specific highlighter if reimplemented.
@ -150,13 +151,13 @@ class KDEUI_EXPORT KTextEdit : public QTextEdit //krazy:exclude=qclasses
* @since 4.1 * @since 4.1
* @todo mark as virtual * @todo mark as virtual
*/ */
QMenu *mousePopupMenu(); QMenu* mousePopupMenu();
/** /**
* Enable find replace action. * Enable find replace action.
* @since 4.1 * @since 4.1
*/ */
void enableFindReplace( bool enabled); void enableFindReplace(bool enabled);
/** /**
* @return the spell checking language which was set by * @return the spell checking language which was set by
@ -180,13 +181,12 @@ class KDEUI_EXPORT KTextEdit : public QTextEdit //krazy:exclude=qclasses
*/ */
QString clickMessage() const; QString clickMessage() const;
/** /**
* @since 4.10 * @since 4.10
*/ */
void showTabAction(bool show); void showTabAction(bool show);
Q_SIGNALS: Q_SIGNALS:
/** /**
* emit signal when we activate or not autospellchecking * emit signal when we activate or not autospellchecking
* *
@ -215,9 +215,9 @@ class KDEUI_EXPORT KTextEdit : public QTextEdit //krazy:exclude=qclasses
* @param p the context menu about to be displayed * @param p the context menu about to be displayed
* @since 4.5 * @since 4.5
*/ */
void aboutToShowContextMenu(QMenu* menu); void aboutToShowContextMenu(QMenu *menu);
public Q_SLOTS: public Q_SLOTS:
/** /**
* Set the spell check language which will be used for highlighting spelling * Set the spell check language which will be used for highlighting spelling
* mistakes and for the spellcheck dialog. * mistakes and for the spellcheck dialog.
@ -234,7 +234,7 @@ class KDEUI_EXPORT KTextEdit : public QTextEdit //krazy:exclude=qclasses
*/ */
void replace(); void replace();
protected Q_SLOTS: protected Q_SLOTS:
/** /**
* @since 4.1 * @since 4.1
*/ */
@ -245,11 +245,11 @@ class KDEUI_EXPORT KTextEdit : public QTextEdit //krazy:exclude=qclasses
void slotFindNext(); void slotFindNext();
void slotReplace(); void slotReplace();
protected: protected:
/** /**
* Reimplemented to catch "delete word" shortcut events. * Reimplemented to catch "delete word" shortcut events.
*/ */
virtual bool event(QEvent*); virtual bool event(QEvent *);
/** /**
* Reimplemented to paint clickMessage. * Reimplemented to paint clickMessage.
@ -259,13 +259,13 @@ class KDEUI_EXPORT KTextEdit : public QTextEdit //krazy:exclude=qclasses
/** /**
* Reimplemented for internal reasons * Reimplemented for internal reasons
*/ */
virtual void keyPressEvent( QKeyEvent* ); virtual void keyPressEvent(QKeyEvent *);
/** /**
* Reimplemented to instantiate a KDictSpellingHighlighter, if * Reimplemented to instantiate a KDictSpellingHighlighter, if
* spellchecking is enabled. * spellchecking is enabled.
*/ */
virtual void focusInEvent( QFocusEvent* ); virtual void focusInEvent(QFocusEvent *);
/** /**
* Deletes a word backwards from the current cursor position, * Deletes a word backwards from the current cursor position,
@ -283,17 +283,17 @@ class KDEUI_EXPORT KTextEdit : public QTextEdit //krazy:exclude=qclasses
* Reimplemented from QTextEdit to add spelling related items * Reimplemented from QTextEdit to add spelling related items
* when appropriate. * when appropriate.
*/ */
virtual void contextMenuEvent( QContextMenuEvent* ); virtual void contextMenuEvent(QContextMenuEvent *);
private: private:
class Private; class Private;
Private *const d; Private *const d;
Q_PRIVATE_SLOT( d, void undoableClear() ) Q_PRIVATE_SLOT( d, void undoableClear() )
Q_PRIVATE_SLOT( d, void toggleAutoSpellCheck() ) Q_PRIVATE_SLOT( d, void toggleAutoSpellCheck() )
Q_PRIVATE_SLOT( d, void slotAllowTab() ) Q_PRIVATE_SLOT( d, void slotAllowTab() )
Q_PRIVATE_SLOT( d, void menuActivated( QAction* ) ) Q_PRIVATE_SLOT( d, void menuActivated(QAction *))
Q_PRIVATE_SLOT( d, void slotFindHighlight(const QString&, int, int)) Q_PRIVATE_SLOT( d, void slotFindHighlight(const QString &, int, int))
Q_PRIVATE_SLOT( d, void slotReplaceText(const QString &, int, int, int)) Q_PRIVATE_SLOT( d, void slotReplaceText(const QString &, int, int, int))
}; };