Add QTextOption/QTextEdit/QPlainTextEdit::tabStopDistance property

In Qt, we have QTextOption::tabStop, QTextEdit::tabStopWidth and
QPlainTextEdit::tabStopWidth.

Neither are very good names, since the tab stop is neither a
numerical value as in the former, nor does it have any dimensions
that can be measured, as in the latter. Vertical text advances
may also be supported by Qt at some point in the future, at
which point the name would make even less sense.

At the same time, we expose the actual type of the tab stop
distance as floating point in the QTextEdit and QPlainTextEdit
API instead of always rounding it to an int.

To avoid duplicating either of these APIs in Qt Quick, we
introduce tabStopDistance as the common term instead and deprecate
the old names.

[ChangeLog][Text] Introduced tabStopDistance property in
QTextOption, QTextEdit and QPlainTextEdit as replacement for
the inconsistently named tabStop and tabStopWidth properties.
QTextOption::tabStop, QTextEdit::tabStopWidth and
QPlainTextEdit::tabStopWidth have subsequently been deprecated.

Change-Id: Ib7e01387910cddb58adaaaadcd56c0e69edc4bc2
Reviewed-by: Paolo Angelelli <paolo.angelelli@qt.io>
Reviewed-by: Lars Knoll <lars.knoll@qt.io>
This commit is contained in:
Eskil Abrahamsen Blomfeldt 2017-08-01 12:34:29 +02:00 committed by Jędrzej Nowacki
parent b03706d35f
commit 9342a8b843
10 changed files with 118 additions and 26 deletions

View File

@ -7419,7 +7419,7 @@ void qt_format_text(const QFont &fnt, const QRectF &_r,
if (option->flags() & QTextOption::IncludeTrailingSpaces)
tf |= Qt::TextIncludeTrailingSpaces;
if (option->tabStop() >= 0 || !option->tabArray().isEmpty())
if (option->tabStopDistance() >= 0 || !option->tabArray().isEmpty())
tf |= Qt::TextExpandTabs;
}
@ -7536,8 +7536,8 @@ start_lengthVariant:
engine.option = *option;
}
if (engine.option.tabStop() < 0 && tabstops > 0)
engine.option.setTabStop(tabstops);
if (engine.option.tabStopDistance() < 0 && tabstops > 0)
engine.option.setTabStopDistance(tabstops);
if (engine.option.tabs().isEmpty() && ta) {
QList<qreal> tabs;

View File

@ -203,7 +203,7 @@ QTextDocumentPrivate::QTextDocumentPrivate()
inContentsChange = false;
blockCursorAdjustment = false;
defaultTextOption.setTabStop(80); // same as in qtextengine.cpp
defaultTextOption.setTabStopDistance(80); // same as in qtextengine.cpp
defaultTextOption.setWrapMode(QTextOption::WrapAtWordBoundaryOrAnywhere);
defaultCursorMoveStyle = Qt::LogicalMoveStyle;

View File

@ -3043,7 +3043,7 @@ QFixed QTextEngine::calculateTabWidth(int item, QFixed x) const
}
}
}
QFixed tab = QFixed::fromReal(option.tabStop());
QFixed tab = QFixed::fromReal(option.tabStopDistance());
if (tab <= 0)
tab = 80; // default
tab *= dpiScale;

View File

@ -144,7 +144,7 @@ QTextOption &QTextOption::operator=(const QTextOption &o)
Sets the tab positions for the text layout to those specified by
\a tabStops.
\sa tabArray(), setTabStop(), setTabs()
\sa tabArray(), setTabStopDistance(), setTabs()
*/
void QTextOption::setTabArray(const QList<qreal> &tabStops)
{
@ -332,22 +332,45 @@ QList<QTextOption::Tab> QTextOption::tabs() const
\sa flags()
*/
#if QT_DEPRECATED_SINCE(5, 10)
/*!
\fn qreal QTextOption::tabStop() const
\deprecated in Qt 5.10. Use tabStopDistance() instead.
Returns the distance in device units between tab stops.
Convenient function for the above method
\sa setTabStop(), tabArray(), setTabs(), tabs()
\sa setTabStopDistance(), tabArray(), setTabs(), tabs()
*/
/*!
\fn void QTextOption::setTabStop(qreal tabStop)
\deprecated in Qt 5.10. Use setTabStopDistance() instead.
Sets the default distance in device units between tab stops to the value specified
by \a tabStop.
\sa tabStop(), setTabArray(), setTabs(), tabs()
\sa tabStopDistance(), setTabArray(), setTabs(), tabs()
*/
#endif
/*!
\fn qreal QTextOption::tabStopDistance() const
\since 5.10
Returns the distance in device units between tab stops.
\sa setTabStopDistance(), tabArray(), setTabs(), tabs()
*/
/*!
\fn void QTextOption::setTabStopDistance(qreal tabStopDistance)
\since 5.10
Sets the default distance in device units between tab stops to the value specified
by \a tabStopDistance.
\sa tabStopDistance(), setTabArray(), setTabs(), tabs()
*/
/*!
@ -426,7 +449,7 @@ QList<QTextOption::Tab> QTextOption::tabs() const
\fn void setTabs(const QList<Tab> &tabStops)
Set the Tab properties to \a tabStops.
\sa tabStop(), tabs()
\sa tabStopDistance(), tabs()
*/
/*!
@ -434,7 +457,7 @@ QList<QTextOption::Tab> QTextOption::tabs() const
\fn QList<QTextOption::Tab> QTextOption::tabs() const
Returns a list of tab positions defined for the text layout.
\sa tabStop(), setTabs(), setTabStop()
\sa tabStopDistance(), setTabs(), setTabStop()
*/

View File

@ -117,8 +117,13 @@ public:
inline void setFlags(Flags flags);
inline Flags flags() const { return Flags(f); }
inline void setTabStop(qreal tabStop);
inline qreal tabStop() const { return tab; }
#if QT_DEPRECATED_SINCE(5, 10)
QT_DEPRECATED inline void setTabStop(qreal tabStop);
QT_DEPRECATED inline qreal tabStop() const { return tabStopDistance(); }
#endif
inline void setTabStopDistance(qreal tabStopDistance);
inline qreal tabStopDistance() const { return tab; }
void setTabArray(const QList<qreal> &tabStops);
QList<qreal> tabArray() const;
@ -149,7 +154,12 @@ inline void QTextOption::setAlignment(Qt::Alignment aalignment)
inline void QTextOption::setFlags(Flags aflags)
{ f = aflags; }
#if QT_DEPRECATED_SINCE(5, 10)
inline void QTextOption::setTabStop(qreal atabStop)
{ setTabStopDistance(atabStop); }
#endif
inline void QTextOption::setTabStopDistance(qreal atabStop)
{ tab = atabStop; }
QT_END_NAMESPACE

View File

@ -2453,29 +2453,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

View File

@ -74,7 +74,10 @@ class Q_WIDGETS_EXPORT QPlainTextEdit : public QAbstractScrollArea
Q_PROPERTY(bool readOnly READ isReadOnly WRITE setReadOnly)
Q_PROPERTY(QString plainText READ toPlainText WRITE setPlainText NOTIFY textChanged USER true)
Q_PROPERTY(bool overwriteMode READ overwriteMode WRITE setOverwriteMode)
#if QT_DEPRECATED_SINCE(5, 10)
Q_PROPERTY(int tabStopWidth READ tabStopWidth WRITE setTabStopWidth)
#endif
Q_PROPERTY(qreal tabStopDistance READ tabStopDistance WRITE setTabStopDistance)
Q_PROPERTY(int cursorWidth READ cursorWidth WRITE setCursorWidth)
Q_PROPERTY(Qt::TextInteractionFlags textInteractionFlags READ textInteractionFlags WRITE setTextInteractionFlags)
Q_PROPERTY(int blockCount READ blockCount)
@ -168,8 +171,13 @@ public:
bool overwriteMode() const;
void setOverwriteMode(bool overwrite);
int tabStopWidth() const;
void setTabStopWidth(int width);
#if QT_DEPRECATED_SINCE(5, 10)
QT_DEPRECATED int tabStopWidth() const;
QT_DEPRECATED void setTabStopWidth(int width);
#endif
qreal tabStopDistance() const;
void setTabStopDistance(qreal distance);
int cursorWidth() const;
void setCursorWidth(int width);

View File

@ -1954,27 +1954,48 @@ void QTextEdit::setOverwriteMode(bool overwrite)
d->control->setOverwriteMode(overwrite);
}
#if QT_DEPRECATED_SINCE(5, 10)
/*!
\property QTextEdit::tabStopWidth
\brief the tab stop width in pixels
\since 4.1
\deprecated in Qt 5.10. Use tabStopDistance instead.
By default, this property contains a value of 80 pixels.
*/
int QTextEdit::tabStopWidth() const
{
Q_D(const QTextEdit);
return qRound(d->control->document()->defaultTextOption().tabStop());
return qRound(tabStopDistance());
}
void QTextEdit::setTabStopWidth(int width)
{
setTabStopDistance(width);
}
#endif
/*!
\property QTextEdit::tabStopDistance
\brief the tab stop distance in pixels
\since 5.10
By default, this property contains a value of 80 pixels.
*/
qreal QTextEdit::tabStopDistance() const
{
Q_D(const QTextEdit);
return d->control->document()->defaultTextOption().tabStopDistance();
}
void QTextEdit::setTabStopDistance(qreal distance)
{
Q_D(QTextEdit);
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);
}

View File

@ -77,7 +77,10 @@ class Q_WIDGETS_EXPORT QTextEdit : public QAbstractScrollArea
#endif
Q_PROPERTY(QString plainText READ toPlainText WRITE setPlainText DESIGNABLE false)
Q_PROPERTY(bool overwriteMode READ overwriteMode WRITE setOverwriteMode)
#if QT_DEPRECATED_SINCE(5, 10)
Q_PROPERTY(int tabStopWidth READ tabStopWidth WRITE setTabStopWidth)
#endif
Q_PROPERTY(qreal tabStopDistance READ tabStopDistance WRITE setTabStopDistance)
Q_PROPERTY(bool acceptRichText READ acceptRichText WRITE setAcceptRichText)
Q_PROPERTY(int cursorWidth READ cursorWidth WRITE setCursorWidth)
Q_PROPERTY(Qt::TextInteractionFlags textInteractionFlags READ textInteractionFlags WRITE setTextInteractionFlags)
@ -187,8 +190,13 @@ public:
bool overwriteMode() const;
void setOverwriteMode(bool overwrite);
int tabStopWidth() const;
void setTabStopWidth(int width);
#if QT_DEPRECATED_SINCE(5, 10)
QT_DEPRECATED int tabStopWidth() const;
QT_DEPRECATED void setTabStopWidth(int width);
#endif
qreal tabStopDistance() const;
void setTabStopDistance(qreal distance);
int cursorWidth() const;
void setCursorWidth(int width);

View File

@ -1312,7 +1312,7 @@ void tst_QTextLayout::testDefaultTabs()
QCOMPARE(line.cursorToX(31), 480.);
QTextOption option = layout.textOption();
option.setTabStop(90);
option.setTabStopDistance(90);
layout.setTextOption(option);
layout.beginLayout();
line = layout.createLine();
@ -1351,7 +1351,7 @@ void tst_QTextLayout::testTabs()
layout.setCacheEnabled(true);
QTextOption option = layout.textOption();
option.setTabStop(150);
option.setTabStopDistance(150);
layout.setTextOption(option);
layout.beginLayout();