diff options
Diffstat (limited to 'src/widgets/widgets/qplaintextedit.cpp')
-rw-r--r-- | src/widgets/widgets/qplaintextedit.cpp | 75 |
1 files changed, 59 insertions, 16 deletions
diff --git a/src/widgets/widgets/qplaintextedit.cpp b/src/widgets/widgets/qplaintextedit.cpp index 6b51388990..e66b702ffd 100644 --- a/src/widgets/widgets/qplaintextedit.cpp +++ b/src/widgets/widgets/qplaintextedit.cpp @@ -364,7 +364,7 @@ void QPlainTextDocumentLayout::layoutBlock(const QTextBlock &block) int extraMargin = 0; if (option.flags() & QTextOption::AddSpaceForLineAndParagraphSeparators) { QFontMetrics fm(block.charFormat().font()); - extraMargin += fm.width(QChar(0x21B5)); + extraMargin += fm.horizontalAdvance(QChar(0x21B5)); } tl->beginLayout(); qreal availableWidth = d->width; @@ -747,7 +747,8 @@ QPlainTextEditPrivate::QPlainTextEditPrivate() tabChangesFocus(false), lineWrap(QPlainTextEdit::WidgetWidth), wordWrap(QTextOption::WrapAtWordBoundaryOrAnywhere), - clickCausedFocus(0),topLine(0),topLineFracture(0), + clickCausedFocus(0), placeholderVisible(1), + topLine(0), topLineFracture(0), pageUpDownLastCursorYIsValid(false) { showCursorOnInitialShow = true; @@ -784,6 +785,7 @@ void QPlainTextEditPrivate::init(const QString &txt) QObject::connect(control, SIGNAL(selectionChanged()), q, SIGNAL(selectionChanged())); QObject::connect(control, SIGNAL(cursorPositionChanged()), q, SLOT(_q_cursorPositionChanged())); + QObject::connect(control, SIGNAL(textChanged()), q, SLOT(_q_textChanged())); QObject::connect(control, SIGNAL(textChanged()), q, SLOT(updateMicroFocus())); // set a null page size initially to avoid any relayouting until the textedit @@ -816,6 +818,24 @@ void QPlainTextEditPrivate::init(const QString &txt) #endif } +void QPlainTextEditPrivate::_q_textChanged() +{ + Q_Q(QPlainTextEdit); + + // We normally only repaint the part of view that contains text in the + // document that has changed (in _q_repaintContents). But the placeholder + // text is not a part of the document, but is drawn on separately. So whenever + // we either show or hide the placeholder text, we issue a full update. + bool placeholderCurrentyVisible = placeholderVisible; + + placeholderVisible = !placeholderText.isEmpty() + && q->document()->isEmpty() + && q->firstVisibleBlock().layout()->preeditAreaText().isEmpty(); + + if (placeholderCurrentyVisible != placeholderVisible) + viewport->update(); +} + void QPlainTextEditPrivate::_q_repaintContents(const QRectF &contentsRect) { Q_Q(QPlainTextEdit); @@ -1881,6 +1901,7 @@ static void fillBackground(QPainter *p, const QRectF &rect, QBrush brush, const */ void QPlainTextEdit::paintEvent(QPaintEvent *e) { + Q_D(QPlainTextEdit); QPainter painter(viewport()); Q_ASSERT(qobject_cast<QPlainTextDocumentLayout*>(document()->documentLayout())); @@ -1903,6 +1924,15 @@ void QPlainTextEdit::paintEvent(QPaintEvent *e) er.setRight(qMin(er.right(), maxX)); painter.setClipRect(er); + if (d->placeholderVisible) { + QColor col = d->control->palette().text().color(); + col.setAlpha(128); + painter.setPen(col); + painter.setClipRect(e->rect()); + const int margin = int(document()->documentMargin()); + QRectF textRect = viewportRect.adjusted(margin, margin, 0, 0); + painter.drawText(textRect, Qt::AlignTop | Qt::TextWordWrap, placeholderText()); + } QAbstractTextDocumentLayout::PaintContext context = getPaintContext(); @@ -1977,17 +2007,8 @@ void QPlainTextEdit::paintEvent(QPaintEvent *e) } } + layout->draw(&painter, offset, selections, er); - if (!placeholderText().isEmpty() && document()->isEmpty() && layout->preeditAreaText().isEmpty()) { - Q_D(QPlainTextEdit); - QColor col = d->control->palette().text().color(); - col.setAlpha(128); - painter.setPen(col); - const int margin = int(document()->documentMargin()); - painter.drawText(r.adjusted(margin, 0, 0, 0), Qt::AlignTop | Qt::TextWordWrap, placeholderText()); - } else { - layout->draw(&painter, offset, selections, er); - } if ((drawCursor && !drawCursorAsBlock) || (editable && context.cursorPosition < -1 && !layout->preeditAreaText().isEmpty())) { @@ -2453,29 +2474,51 @@ void QPlainTextEdit::setOverwriteMode(bool overwrite) d->control->setOverwriteMode(overwrite); } +#if QT_DEPRECATED_SINCE(5, 10) /*! \property QPlainTextEdit::tabStopWidth \brief the tab stop width in pixels + \deprecated in Qt 5.10. Use tabStopDistance instead. By default, this property contains a value of 80. */ int QPlainTextEdit::tabStopWidth() const { - Q_D(const QPlainTextEdit); - return qRound(d->control->document()->defaultTextOption().tabStop()); + return qRound(tabStopDistance()); } void QPlainTextEdit::setTabStopWidth(int width) { + setTabStopDistance(width); +} +#endif + +/*! + \property QPlainTextEdit::tabStopDistance + \brief the tab stop distance in pixels + \since 5.10 + + By default, this property contains a value of 80. +*/ + +qreal QPlainTextEdit::tabStopDistance() const +{ + Q_D(const QPlainTextEdit); + return d->control->document()->defaultTextOption().tabStopDistance(); +} + +void QPlainTextEdit::setTabStopDistance(qreal distance) +{ Q_D(QPlainTextEdit); QTextOption opt = d->control->document()->defaultTextOption(); - if (opt.tabStop() == width || width < 0) + if (opt.tabStopDistance() == distance || distance < 0) return; - opt.setTabStop(width); + opt.setTabStopDistance(distance); d->control->document()->setDefaultTextOption(opt); } + /*! \property QPlainTextEdit::cursorWidth |