diff options
author | Carl Schwan <carl@carlschwan.eu> | 2024-04-10 16:30:42 +0200 |
---|---|---|
committer | Shawn Rutledge <shawn.rutledge@qt.io> | 2024-05-07 12:28:59 -0700 |
commit | ab2f4ef8fad82fd07dac8768b8dcb94b11b271b6 (patch) | |
tree | b24d4c9f6e65150566b195a545794386b30114cc /src/gui/text/qcssparser.cpp | |
parent | fb2ea1e52a03f87e041a61b936183175df37b5bf (diff) |
QTextDocument: Add support for responsive images
Add support for the max-width css attribute in image. This allows images
to be responsive: it adapts their size to the size of the QTextDocument
so that they never grow bigger than the QTextDocument pageSize.
This is implemented for the image handler used in QTextEdit and other
QtWidget text related classes.
[ChangeLog][QtGui][CSS] The max-width style can now be applied to
<img/> to set the maximum width in pixels or percentage.
Task-number: QTBUG-12283
Change-Id: Ic94e16279a1240ab4a509823de59dc0bfc920bb9
Reviewed-by: Shawn Rutledge <shawn.rutledge@qt.io>
Diffstat (limited to 'src/gui/text/qcssparser.cpp')
-rw-r--r-- | src/gui/text/qcssparser.cpp | 12 |
1 files changed, 12 insertions, 0 deletions
diff --git a/src/gui/text/qcssparser.cpp b/src/gui/text/qcssparser.cpp index e5815ffce2..916e96ea63 100644 --- a/src/gui/text/qcssparser.cpp +++ b/src/gui/text/qcssparser.cpp @@ -12,6 +12,7 @@ #include <qfontmetrics.h> #include <qbrush.h> #include <qimagereader.h> +#include <qtextformat.h> #include <algorithm> @@ -396,6 +397,8 @@ LengthData ValueExtractor::lengthValue(const Value& v) if (data.unit != LengthData::None) s.chop(2); + else if (v.type == Value::Percentage) + data.unit = LengthData::Percent; data.number = s.toDouble(); return data; @@ -409,6 +412,15 @@ static int lengthValueFromData(const LengthData& data, const QFont& f) return qRound(qBound(double(INT_MIN) + 0.1, scale * data.number, double(INT_MAX))); } +QTextLength ValueExtractor::textLength(const Declaration &decl) +{ + const LengthData data = lengthValue(decl.d->values.at(0)); + if (data.unit == LengthData::Percent) + return QTextLength(QTextLength::PercentageLength, data.number); + + return QTextLength(QTextLength::FixedLength, lengthValueFromData(data, f)); +} + int ValueExtractor::lengthValue(const Declaration &decl) { if (decl.d->parsed.isValid()) |