Commit 5b9e19ff authored by Andrew den Exter's avatar Andrew den Exter Committed by Qt by Nokia

Add support for resizing fonts to fit Text dimensions.

This adds a mode where if the content of a Text item doesn't fit
within its bounds the font size is reduced during layout until it
does or a minimum font size is reached.

Task-number: QTBUG-22832
Change-Id: I6198ef03899e2f21b32e313548966ef4b0e3bff1
Reviewed-by: default avatarAndrew den Exter <andrew.den-exter@nokia.com>
Reviewed-by: default avatarYann Bodson <yann.bodson@nokia.com>
parent 0323a56a
This diff is collapsed.
......@@ -63,6 +63,7 @@ class Q_QUICK_PRIVATE_EXPORT QQuickText : public QQuickImplicitSizeItem
Q_ENUMS(TextElideMode)
Q_ENUMS(WrapMode)
Q_ENUMS(LineHeightMode)
Q_ENUMS(FontSizeMode)
Q_PROPERTY(QString text READ text WRITE setText NOTIFY textChanged)
Q_PROPERTY(QFont font READ font WRITE setFont NOTIFY fontChanged)
......@@ -84,6 +85,9 @@ class Q_QUICK_PRIVATE_EXPORT QQuickText : public QQuickImplicitSizeItem
Q_PROPERTY(qreal lineHeight READ lineHeight WRITE setLineHeight NOTIFY lineHeightChanged)
Q_PROPERTY(LineHeightMode lineHeightMode READ lineHeightMode WRITE setLineHeightMode NOTIFY lineHeightModeChanged)
Q_PROPERTY(QUrl baseUrl READ baseUrl WRITE setBaseUrl RESET resetBaseUrl NOTIFY baseUrlChanged)
Q_PROPERTY(int minimumPixelSize READ minimumPixelSize WRITE setMinimumPixelSize NOTIFY minimumPixelSizeChanged)
Q_PROPERTY(int minimumPointSize READ minimumPointSize WRITE setMinimumPointSize NOTIFY minimumPointSizeChanged)
Q_PROPERTY(FontSizeMode fontSizeMode READ fontSizeMode WRITE setFontSizeMode NOTIFY fontSizeModeChanged)
public:
QQuickText(QQuickItem *parent=0);
......@@ -118,6 +122,9 @@ public:
enum LineHeightMode { ProportionalHeight, FixedHeight };
enum FontSizeMode { FixedSize = 0x0, HorizontalFit = 0x01, VerticalFit = 0x02,
Fit = HorizontalFit | VerticalFit };
QString text() const;
void setText(const QString &);
......@@ -163,10 +170,20 @@ public:
LineHeightMode lineHeightMode() const;
void setLineHeightMode(LineHeightMode);
QUrl baseUrl() const;
void setBaseUrl(const QUrl &url);
void resetBaseUrl();
int minimumPixelSize() const;
void setMinimumPixelSize(int size);
int minimumPointSize() const;
void setMinimumPointSize(int size);
FontSizeMode fontSizeMode() const;
void setFontSizeMode(FontSizeMode mode);
virtual void componentComplete();
int resourcesLoading() const; // mainly for testing
......@@ -195,6 +212,9 @@ Q_SIGNALS:
void paintedSizeChanged();
void lineHeightChanged(qreal lineHeight);
void lineHeightModeChanged(LineHeightMode mode);
void fontSizeModeChanged();
void minimumPixelSizeChanged();
void minimumPointSizeChanged();
void effectiveHorizontalAlignmentChanged();
void lineLaidOut(QQuickTextLine *line);
void baseUrlChanged();
......@@ -207,6 +227,8 @@ protected:
virtual QSGNode *updatePaintNode(QSGNode *, UpdatePaintNodeData *);
virtual bool event(QEvent *);
void updatePolish();
private Q_SLOTS:
void q_imagesLoaded();
void triggerPreprocess();
......@@ -231,6 +253,7 @@ public:
QQuickTextLine();
void setLine(QTextLine* line);
void setLineOffset(int offset);
int number() const;
qreal width() const;
......@@ -248,6 +271,7 @@ public:
private:
QTextLine *m_line;
qreal m_height;
int m_lineOffset;
};
QT_END_NAMESPACE
......
......@@ -103,9 +103,12 @@ public:
int lineCount;
int maximumLineCount;
int maximumLineCountValid;
QQuickText::FontSizeMode fontSizeMode;
int minimumPixelSize;
int minimumPointSize;
QPointF elidePos;
static QString elideChar;
static const QChar elideChar;
void markDirty();
bool invalidateImageCache();
......@@ -115,6 +118,7 @@ public:
bool imageCacheDirty:1;
bool updateOnComponentComplete:1;
bool updateLayoutOnPolish:1;
bool richText:1;
bool styledText:1;
bool singleline:1;
......@@ -141,13 +145,13 @@ public:
QQuickTextDocumentWithImageResources *doc;
QRect setupTextLayout();
void setupCustomLineGeometry(QTextLine &line, qreal &height, qreal elideWidth);
void setupCustomLineGeometry(QTextLine &line, qreal &height, int lineOffset = 0);
QPixmap textLayoutImage(bool drawStyle);
void drawTextLayout(QPainter *p, const QPointF &pos, bool drawStyle);
bool isLinkActivatedConnected();
QString anchorAt(const QPointF &pos);
QTextLayout layout;
QTextLayout *elipsisLayout;
QTextLayout *elideLayout;
QQuickTextLine *textLine;
static QPixmap drawOutline(const QPixmap &source, const QPixmap &styleSource);
......
import QtQuick 2.0
Item {
width: 300
height: 200
Rectangle {
anchors.fill: myText
border.width: 1
}
Text {
id: myText
objectName: "myText"
width: 250
height: 41
minimumPointSize: 8
minimumPixelSize: 8
font.pixelSize: 30
font.family: "Helvetica"
}
TextInput { focus: true; objectName: "input" }
}
......@@ -48,12 +48,16 @@ Rectangle {
Item {
id: textpanel
height: 360
width: 440
anchors.fill: parent
anchors.rightMargin: controlpanel.width
Text {
id: textelement
height: parent.height - 20; width: parent.width - 20
anchors.centerIn: parent
anchors.fill: parent;
anchors.margins: 10
text: { textvalue.model.get(textvalue.currentIndex).value }
textFormat: { formatvalue.model.get(formatvalue.currentIndex).value }
......@@ -79,6 +83,8 @@ Rectangle {
smooth: { smoothvalue.model.get(smoothvalue.currentIndex).value }
style: { stylevalue.model.get(stylevalue.currentIndex).value }
styleColor: { stylecolorvalue.model.get(stylecolorvalue.currentIndex).value }
fontSizeMode : { fontsizemodevalue.model.get(fontsizemodevalue.currentIndex).value }
minimumPointSize : { minimumpointsizevalue.model.get(minimumpointsizevalue.currentIndex).value }
Rectangle{ color: "transparent"; border.color: "green"; anchors.fill: parent }
}
......@@ -204,11 +210,11 @@ Rectangle {
ControlView {
id: pixelvalue
controlname: "Pixel"
model: ListModel { ListElement { name: "-1"; value: -1 } ListElement { name: "8"; value: 8 } ListElement { name: "20"; value: 20 } } }
model: ListModel { ListElement { name: "-1"; value: -1 } ListElement { name: "8"; value: 8 } ListElement { name: "20"; value: 20 } ListElement { name: "50"; value: 20 } } }
ControlView {
id: pointvalue
controlname: "Point"
model: ListModel { ListElement { name: "-1"; value: -1 } ListElement { name: "8"; value: 8 } ListElement { name: "20"; value: 20 } } }
model: ListModel { ListElement { name: "-1"; value: -1 } ListElement { name: "8"; value: 8 } ListElement { name: "20"; value: 20 } ListElement { name: "50"; value: 20 } } }
ControlView {
id: strikeoutvalue
controlname: "Strike"
......@@ -267,6 +273,21 @@ Rectangle {
controlname: "Wrap"
model: ListModel { ListElement { name: "None"; value: Text.NoWrap } ListElement { name: "Word"; value: Text.WordWrap }
ListElement { name: "Anywhere"; value: Text.WrapAnywhere } ListElement { name: "Wrap"; value: Text.Wrap } } }
ControlView {
id: fontsizemodevalue
controlname: "FontSize"
model: ListModel { ListElement { name: "FixedSize"; value: Text.FixedSize } ListElement { name: "Horizontal"; value: Text.HorizontalFit }
ListElement { name: "Vertical"; value: Text.VerticalFit } ListElement { name: "Fit"; value: Text.Fit } } }
ControlView {
id: minimumpixelsizevalue
controlname: "MinPixelSize"
model: ListModel { ListElement { name: "8"; value: 8 } ListElement { name: "12"; value: 12 }
ListElement { name: "24"; value: 24 } ListElement { name: "32"; value: 32 } } }
ControlView {
id: minimumpointsizevalue
controlname: "MinPointSize"
model: ListModel { ListElement { name: "8"; value: 8 } ListElement { name: "12"; value: 12 }
ListElement { name: "24"; value: 24 } ListElement { name: "32"; value: 32 } } }
}
}
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment