QPlainTextEdit code cleanup

Replace QObject::connect statements using SIGNAL/SLOT macros with
recent API.
Replace if/elseif cascades with switch statements.
Remove unnecessary blank lines.

Change-Id: Ib813e25530905e01f70ad52da11e69163445eaf8
(cherry picked from commit 2f3f3eb0d4d77743c135d95c792f66a4272903f7)
Reviewed-by: Shawn Rutledge <shawn.rutledge@qt.io>
This commit is contained in:
Axel Spoerl 2023-02-17 12:42:50 +01:00
parent e9efb7b1e2
commit 106ce0bd52

View File

@ -430,10 +430,17 @@ void QPlainTextEditPrivate::_q_cursorPositionChanged()
} }
void QPlainTextEditPrivate::_q_verticalScrollbarActionTriggered(int action) { void QPlainTextEditPrivate::_q_verticalScrollbarActionTriggered(int action) {
if (action == QAbstractSlider::SliderPageStepAdd) {
const auto a = static_cast<QAbstractSlider::SliderAction>(action);
switch (a) {
case QAbstractSlider::SliderPageStepAdd:
pageUpDown(QTextCursor::Down, QTextCursor::MoveAnchor, false); pageUpDown(QTextCursor::Down, QTextCursor::MoveAnchor, false);
} else if (action == QAbstractSlider::SliderPageStepSub) { break;
case QAbstractSlider::SliderPageStepSub:
pageUpDown(QTextCursor::Up, QTextCursor::MoveAnchor, false); pageUpDown(QTextCursor::Up, QTextCursor::MoveAnchor, false);
break;
default:
break;
} }
} }
@ -741,23 +748,28 @@ void QPlainTextEditPrivate::init(const QString &txt)
control->setPalette(q->palette()); control->setPalette(q->palette());
QObject::connect(vbar, SIGNAL(actionTriggered(int)), q, SLOT(_q_verticalScrollbarActionTriggered(int))); QObjectPrivate::connect(vbar, &QAbstractSlider::actionTriggered,
this, &QPlainTextEditPrivate::_q_verticalScrollbarActionTriggered);
QObject::connect(control, SIGNAL(microFocusChanged()), q, SLOT(updateMicroFocus())); QObject::connect(control, &QWidgetTextControl::microFocusChanged, q,
QObject::connect(control, SIGNAL(documentSizeChanged(QSizeF)), q, SLOT(_q_adjustScrollbars())); [q](){q->updateMicroFocus(); });
QObject::connect(control, SIGNAL(blockCountChanged(int)), q, SIGNAL(blockCountChanged(int))); QObjectPrivate::connect(control, &QWidgetTextControl::documentSizeChanged,
QObject::connect(control, SIGNAL(updateRequest(QRectF)), q, SLOT(_q_repaintContents(QRectF))); this, &QPlainTextEditPrivate::_q_adjustScrollbars);
QObject::connect(control, SIGNAL(modificationChanged(bool)), q, SIGNAL(modificationChanged(bool))); QObject::connect(control, &QWidgetTextControl::blockCountChanged,
q, &QPlainTextEdit::blockCountChanged);
QObject::connect(control, SIGNAL(textChanged()), q, SIGNAL(textChanged())); QObjectPrivate::connect(control, &QWidgetTextControl::updateRequest,
QObject::connect(control, SIGNAL(undoAvailable(bool)), q, SIGNAL(undoAvailable(bool))); this, &QPlainTextEditPrivate::_q_repaintContents);
QObject::connect(control, SIGNAL(redoAvailable(bool)), q, SIGNAL(redoAvailable(bool))); QObject::connect(control, &QWidgetTextControl::modificationChanged,
QObject::connect(control, SIGNAL(copyAvailable(bool)), q, SIGNAL(copyAvailable(bool))); q, &QPlainTextEdit::modificationChanged);
QObject::connect(control, SIGNAL(selectionChanged()), q, SIGNAL(selectionChanged())); QObject::connect(control, &QWidgetTextControl::textChanged, q, &QPlainTextEdit::textChanged);
QObject::connect(control, SIGNAL(cursorPositionChanged()), q, SLOT(_q_cursorPositionChanged())); QObject::connect(control, &QWidgetTextControl::undoAvailable, q, &QPlainTextEdit::undoAvailable);
QObject::connect(control, &QWidgetTextControl::redoAvailable, q, &QPlainTextEdit::redoAvailable);
QObject::connect(control, SIGNAL(textChanged()), q, SLOT(_q_updatePlaceholderVisibility())); QObject::connect(control, &QWidgetTextControl::copyAvailable, q, &QPlainTextEdit::copyAvailable);
QObject::connect(control, SIGNAL(textChanged()), q, SLOT(updateMicroFocus())); QObject::connect(control, &QWidgetTextControl::selectionChanged, q, &QPlainTextEdit::selectionChanged);
QObjectPrivate::connect(control, &QWidgetTextControl::cursorPositionChanged,
this, &QPlainTextEditPrivate::_q_cursorPositionChanged);
QObjectPrivate::connect(control, &QWidgetTextControl::textChanged,
this, &QPlainTextEditPrivate::_q_updatePlaceholderVisibility);
QObject::connect(control, &QWidgetTextControl::textChanged, q, [q](){q->updateMicroFocus(); });
// set a null page size initially to avoid any relayouting until the textedit // set a null page size initially to avoid any relayouting until the textedit
// is shown. relayoutDocument() will take care of setting the page size to the // is shown. relayoutDocument() will take care of setting the page size to the
@ -984,8 +996,6 @@ void QPlainTextEditPrivate::_q_adjustScrollbars()
vSliderLength = lineSpacing != 0 ? viewport->height() / lineSpacing : 0; vSliderLength = lineSpacing != 0 ? viewport->height() / lineSpacing : 0;
} }
QSizeF documentSize = documentLayout->documentSize(); QSizeF documentSize = documentLayout->documentSize();
vbar->setRange(0, qMax(0, vmax)); vbar->setRange(0, qMax(0, vmax));
vbar->setPageStep(vSliderLength); vbar->setPageStep(vSliderLength);
@ -1500,9 +1510,10 @@ bool QPlainTextEdit::event(QEvent *e)
{ {
Q_D(QPlainTextEdit); Q_D(QPlainTextEdit);
switch (e->type()) {
#ifndef QT_NO_CONTEXTMENU #ifndef QT_NO_CONTEXTMENU
if (e->type() == QEvent::ContextMenu case QEvent::ContextMenu:
&& static_cast<QContextMenuEvent *>(e)->reason() == QContextMenuEvent::Keyboard) { if (static_cast<QContextMenuEvent *>(e)->reason() == QContextMenuEvent::Keyboard) {
ensureCursorVisible(); ensureCursorVisible();
const QPoint cursorPos = cursorRect().center(); const QPoint cursorPos = cursorRect().center();
QContextMenuEvent ce(QContextMenuEvent::Keyboard, cursorPos, d->viewport->mapToGlobal(cursorPos)); QContextMenuEvent ce(QContextMenuEvent::Keyboard, cursorPos, d->viewport->mapToGlobal(cursorPos));
@ -1511,42 +1522,45 @@ bool QPlainTextEdit::event(QEvent *e)
e->setAccepted(ce.isAccepted()); e->setAccepted(ce.isAccepted());
return result; return result;
} }
break;
#endif // QT_NO_CONTEXTMENU #endif // QT_NO_CONTEXTMENU
if (e->type() == QEvent::ShortcutOverride case QEvent::ShortcutOverride:
|| e->type() == QEvent::ToolTip) { case QEvent::ToolTip:
d->sendControlEvent(e); d->sendControlEvent(e);
} break;
#ifdef QT_KEYPAD_NAVIGATION #ifdef QT_KEYPAD_NAVIGATION
else if (e->type() == QEvent::EnterEditFocus || e->type() == QEvent::LeaveEditFocus) { case QEvent::EnterEditFocus:
case QEvent::LeaveEditFocus:
if (QApplicationPrivate::keypadNavigationEnabled()) if (QApplicationPrivate::keypadNavigationEnabled())
d->sendControlEvent(e); d->sendControlEvent(e);
} break;
#endif #endif
#ifndef QT_NO_GESTURES #ifndef QT_NO_GESTURES
else if (e->type() == QEvent::Gesture) { case QEvent::Gesture:
QGestureEvent *ge = static_cast<QGestureEvent *>(e); if (auto *g = static_cast<QGestureEvent *>(e)->gesture(Qt::PanGesture)) {
QPanGesture *g = static_cast<QPanGesture *>(ge->gesture(Qt::PanGesture)); QPanGesture *panGesture = static_cast<QPanGesture *>(g);
if (g) {
QScrollBar *hBar = horizontalScrollBar(); QScrollBar *hBar = horizontalScrollBar();
QScrollBar *vBar = verticalScrollBar(); QScrollBar *vBar = verticalScrollBar();
if (g->state() == Qt::GestureStarted) if (panGesture->state() == Qt::GestureStarted)
d->originalOffsetY = vBar->value(); d->originalOffsetY = vBar->value();
QPointF offset = g->offset(); QPointF offset = panGesture->offset();
if (!offset.isNull()) { if (!offset.isNull()) {
if (QGuiApplication::isRightToLeft()) if (QGuiApplication::isRightToLeft())
offset.rx() *= -1; offset.rx() *= -1;
// QPlainTextEdit scrolls by lines only in vertical direction // QPlainTextEdit scrolls by lines only in vertical direction
QFontMetrics fm(document()->defaultFont()); QFontMetrics fm(document()->defaultFont());
int lineHeight = fm.height(); int lineHeight = fm.height();
int newX = hBar->value() - g->delta().x(); int newX = hBar->value() - panGesture->delta().x();
int newY = d->originalOffsetY - offset.y()/lineHeight; int newY = d->originalOffsetY - offset.y()/lineHeight;
hBar->setValue(newX); hBar->setValue(newX);
vBar->setValue(newY); vBar->setValue(newY);
} }
} }
return true; return true;
}
#endif // QT_NO_GESTURES #endif // QT_NO_GESTURES
default:
break;
}
return QAbstractScrollArea::event(e); return QAbstractScrollArea::event(e);
} }
@ -2278,21 +2292,29 @@ void QPlainTextEdit::changeEvent(QEvent *e)
{ {
Q_D(QPlainTextEdit); Q_D(QPlainTextEdit);
QAbstractScrollArea::changeEvent(e); QAbstractScrollArea::changeEvent(e);
if (e->type() == QEvent::ApplicationFontChange
|| e->type() == QEvent::FontChange) { switch (e->type()) {
case QEvent::ApplicationFontChange:
case QEvent::FontChange:
d->control->document()->setDefaultFont(font()); d->control->document()->setDefaultFont(font());
} else if (e->type() == QEvent::ActivationChange) { break;
d->control->setPalette(palette()); case QEvent::ActivationChange:
if (!isActiveWindow()) if (!isActiveWindow())
d->autoScrollTimer.stop(); d->autoScrollTimer.stop();
} else if (e->type() == QEvent::EnabledChange) { break;
case QEvent::EnabledChange:
e->setAccepted(isEnabled()); e->setAccepted(isEnabled());
d->control->setPalette(palette()); d->control->setPalette(palette());
d->sendControlEvent(e); d->sendControlEvent(e);
} else if (e->type() == QEvent::PaletteChange) { break;
case QEvent::PaletteChange:
d->control->setPalette(palette()); d->control->setPalette(palette());
} else if (e->type() == QEvent::LayoutDirectionChange) { break;
case QEvent::LayoutDirectionChange:
d->sendControlEvent(e); d->sendControlEvent(e);
break;
default:
break;
} }
} }
@ -2977,12 +2999,17 @@ void QPlainTextEditPrivate::append(const QString &text, Qt::TextFormat format)
bool documentSizeChangedBlocked = documentLayout->priv()->blockDocumentSizeChanged; bool documentSizeChangedBlocked = documentLayout->priv()->blockDocumentSizeChanged;
documentLayout->priv()->blockDocumentSizeChanged = true; documentLayout->priv()->blockDocumentSizeChanged = true;
if (format == Qt::RichText) switch (format) {
case Qt::RichText:
control->appendHtml(text); control->appendHtml(text);
else if (format == Qt::PlainText) break;
case Qt::PlainText:
control->appendPlainText(text); control->appendPlainText(text);
else break;
default:
control->append(text); control->append(text);
break;
}
if (maximumBlockCount > 0) { if (maximumBlockCount > 0) {
if (document->blockCount() > maximumBlockCount) { if (document->blockCount() > maximumBlockCount) {