Reduce QQuickTextEdit memory usage.
authorAndrew den Exter <andrew.den-exter@nokia.com>
Tue, 21 Feb 2012 03:31:19 +0000 (13:31 +1000)
committerQt by Nokia <qt-info@nokia.com>
Thu, 23 Feb 2012 00:19:51 +0000 (01:19 +0100)
Remove unnecessary members from QQuickTextEditPrivate and
QQuickTextControlPrivate and re-order and pack to reduce padding for
alignment.

Change-Id: I14f5e3fc01646d02745f095c2a4b168cd675745d
Reviewed-by: Yann Bodson <yann.bodson@nokia.com>
src/quick/items/qquicktextcontrol.cpp
src/quick/items/qquicktextcontrol_p.h
src/quick/items/qquicktextcontrol_p_p.h
src/quick/items/qquicktextedit.cpp
src/quick/items/qquicktextedit_p_p.h
tests/auto/qtquick2/qquicktextedit/tst_qquicktextedit.cpp

index a763626..9a61312 100644 (file)
@@ -97,13 +97,17 @@ static QTextLine currentTextLine(const QTextCursor &cursor)
 }
 
 QQuickTextControlPrivate::QQuickTextControlPrivate()
-    : doc(0), cursorOn(false), cursorIsFocusIndicator(false),
+    : doc(0),
+      preeditCursor(0),
       interactionFlags(Qt::TextEditorInteraction),
+      cursorOn(false),
+      cursorIsFocusIndicator(false),
       mousePressed(false),
-      lastSelectionState(false), ignoreAutomaticScrollbarAdjustement(false),
+      lastSelectionState(false),
+      ignoreAutomaticScrollbarAdjustement(false),
       overwriteMode(false),
       acceptRichText(true),
-      preeditCursor(0), hideCursor(false),
+      hideCursor(false),
       hasFocus(false),
       isEnabled(true),
       hadSelectionOnMousePress(false),
@@ -298,7 +302,6 @@ void QQuickTextControlPrivate::setContent(Qt::TextFormat format, const QString &
             doc = document;
             clearDocument = false;
         } else {
-            palette = QGuiApplication::palette();
             doc = new QTextDocument(q);
         }
         _q_documentLayoutChanged();
@@ -631,18 +634,6 @@ QQuickTextControl::~QQuickTextControl()
 {
 }
 
-void QQuickTextControl::setView(QObject *view)
-{
-    Q_D(QQuickTextControl);
-    d->contextObject = view;
-}
-
-QObject *QQuickTextControl::view() const
-{
-    Q_D(const QQuickTextControl);
-    return d->contextObject;
-}
-
 QTextDocument *QQuickTextControl::document() const
 {
     Q_D(const QQuickTextControl);
@@ -1418,7 +1409,7 @@ bool QQuickTextControlPrivate::sendMouseEventToInputContext(QMouseEvent *e, cons
 
     Q_UNUSED(e);
 
-    if (contextObject && isPreediting()) {
+    if (isPreediting()) {
         QTextLayout *layout = cursor.block().layout();
         int cursorPos = q->hitTest(pos, Qt::FuzzyHit) - cursor.position();
 
@@ -1908,81 +1899,12 @@ QString QQuickTextControl::toHtml() const
 }
 #endif
 
-QPalette QQuickTextControl::palette() const
-{
-    Q_D(const QQuickTextControl);
-    return d->palette;
-}
-
-void QQuickTextControl::setPalette(const QPalette &pal)
-{
-    Q_D(QQuickTextControl);
-    d->palette = pal;
-}
-
 bool QQuickTextControl::cursorOn() const
 {
     Q_D(const QQuickTextControl);
     return d->cursorOn;
 }
 
-QAbstractTextDocumentLayout::PaintContext QQuickTextControl::getPaintContext() const
-{
-    Q_D(const QQuickTextControl);
-
-    QAbstractTextDocumentLayout::PaintContext ctx;
-
-    ctx.palette = d->palette;
-    if (d->cursorOn && d->isEnabled) {
-        if (d->hideCursor)
-            ctx.cursorPosition = -1;
-        else if (d->preeditCursor != 0)
-            ctx.cursorPosition = - (d->preeditCursor + 2);
-        else
-            ctx.cursorPosition = d->cursor.position();
-    }
-
-    if (d->cursor.hasSelection()) {
-        QAbstractTextDocumentLayout::Selection selection;
-        selection.cursor = d->cursor;
-        if (0 && d->cursorIsFocusIndicator) {
-#if 0
-            // ###
-            QStyleOption opt;
-            opt.palette = ctx.palette;
-            QStyleHintReturnVariant ret;
-            QStyle *style = QGuiApplication::style();
-            if (widget)
-                style = widget->style();
-            style->styleHint(QStyle::SH_TextControl_FocusIndicatorTextCharFormat, &opt, widget, &ret);
-            selection.format = qvariant_cast<QTextFormat>(ret.variant).toCharFormat();
-#endif
-        } else {
-            QPalette::ColorGroup cg = d->hasFocus ? QPalette::Active : QPalette::Inactive;
-            selection.format.setBackground(ctx.palette.brush(cg, QPalette::Highlight));
-            selection.format.setForeground(ctx.palette.brush(cg, QPalette::HighlightedText));
-            if (fullWidthSelection)
-                selection.format.setProperty(QTextFormat::FullWidthSelection, true);
-        }
-        ctx.selections.append(selection);
-    }
-
-    return ctx;
-}
-
-void QQuickTextControl::drawContents(QPainter *p, const QRectF &rect)
-{
-    Q_D(QQuickTextControl);
-    p->save();
-    QAbstractTextDocumentLayout::PaintContext ctx = getPaintContext();
-    if (rect.isValid())
-        p->setClipRect(rect, Qt::IntersectClip);
-    ctx.clip = rect;
-
-    d->doc->documentLayout()->draw(p, ctx);
-    p->restore();
-}
-
 int QQuickTextControl::hitTest(const QPointF &point, Qt::HitTestAccuracy accuracy) const
 {
     Q_D(const QQuickTextControl);
index e7beefd..97ecdc4 100644 (file)
@@ -90,9 +90,6 @@ public:
     explicit QQuickTextControl(QTextDocument *doc, QObject *parent = 0);
     virtual ~QQuickTextControl();
 
-    void setView(QObject *view);
-    QObject *view() const;
-
     QTextDocument *document() const;
 
     void setTextCursor(const QTextCursor &cursor);
@@ -127,9 +124,6 @@ public:
     qreal textWidth() const;
     QSizeF size() const;
 
-    void setIgnoreUnusedNavigationEvents(bool ignore);
-    bool ignoreUnusedNavigationEvents() const;
-
     void moveCursor(QTextCursor::MoveOperation op, QTextCursor::MoveMode mode = QTextCursor::MoveAnchor);
 
     bool canPaste() const;
@@ -178,16 +172,10 @@ Q_SIGNALS:
     void linkHovered(const QString &);
 
 public:
-    // control properties
-    QPalette palette() const;
-    void setPalette(const QPalette &pal);
-
     virtual void processEvent(QEvent *e, const QMatrix &matrix);
     void processEvent(QEvent *e, const QPointF &coordinateOffset = QPointF());
 
     // control methods
-    void drawContents(QPainter *painter, const QRectF &rect = QRectF());
-
     void setFocus(bool focus, Qt::FocusReason = Qt::OtherFocusReason);
 
     virtual QVariant inputMethodQuery(Qt::InputMethodQuery property) const;
index daf2f8c..44bc002 100644 (file)
@@ -132,49 +132,40 @@ public:
     bool isPreediting() const;
     void commitPreedit();
 
-    QTextDocument *doc;
-    bool cursorOn;
-    QTextCursor cursor;
-    bool cursorIsFocusIndicator;
-    QTextCharFormat lastCharFormat;
-
-    Qt::TextInteractionFlags interactionFlags;
-
-    QBasicTimer cursorBlinkTimer;
-    QBasicTimer trippleClickTimer;
     QPointF trippleClickPoint;
+    QPointF mousePressPos;
 
-    bool mousePressed;
-
-    QPoint mousePressPos;
-
-    QPointer<QObject> contextObject;
-
-    bool lastSelectionState;
-
-    bool ignoreAutomaticScrollbarAdjustement;
+    QTextCharFormat lastCharFormat;
 
+    QTextDocument *doc;
+    QTextCursor cursor;
     QTextCursor selectedWordOnDoubleClick;
     QTextCursor selectedBlockOnTrippleClick;
+    QString tentativeCommit;
+    QString highlightedAnchor; // Anchor below cursor
+    QString anchorOnMousePress;
+    QString linkToCopy;
 
-    bool overwriteMode;
-    bool acceptRichText;
+    QBasicTimer cursorBlinkTimer;
+    QBasicTimer trippleClickTimer;
 
     int preeditCursor;
-    bool hideCursor; // used to hide the cursor in the preedit area
-    QString tentativeCommit;
-
-    QPalette palette;
-    bool hasFocus;
-    bool isEnabled;
 
-    QString highlightedAnchor; // Anchor below cursor
-    QString anchorOnMousePress;
-    bool hadSelectionOnMousePress;
+    Qt::TextInteractionFlags interactionFlags;
 
-    bool wordSelectionEnabled;
+    bool cursorOn : 1;
+    bool cursorIsFocusIndicator : 1;
+    bool mousePressed : 1;
+    bool lastSelectionState : 1;
+    bool ignoreAutomaticScrollbarAdjustement : 1;
+    bool overwriteMode : 1;
+    bool acceptRichText : 1;
+    bool hideCursor : 1; // used to hide the cursor in the preedit area
+    bool hasFocus : 1;
+    bool isEnabled : 1;
+    bool hadSelectionOnMousePress : 1;
+    bool wordSelectionEnabled : 1;
 
-    QString linkToCopy;
     void _q_copyLink();
     void _q_updateBlock(const QTextBlock &);
     void _q_documentLayoutChanged();
index 6f3c32d..0626471 100644 (file)
@@ -399,9 +399,6 @@ void QQuickTextEdit::setColor(const QColor &color)
         return;
 
     d->color = color;
-    QPalette pal = d->control->palette();
-    pal.setColor(QPalette::Text, color);
-    d->control->setPalette(pal);
     updateDocument();
     emit colorChanged(d->color);
 }
@@ -424,9 +421,6 @@ void QQuickTextEdit::setSelectionColor(const QColor &color)
         return;
 
     d->selectionColor = color;
-    QPalette pal = d->control->palette();
-    pal.setColor(QPalette::Highlight, color);
-    d->control->setPalette(pal);
     updateDocument();
     emit selectionColorChanged(d->selectionColor);
 }
@@ -449,9 +443,6 @@ void QQuickTextEdit::setSelectedTextColor(const QColor &color)
         return;
 
     d->selectedTextColor = color;
-    QPalette pal = d->control->palette();
-    pal.setColor(QPalette::HighlightedText, color);
-    d->control->setPalette(pal);
     updateDocument();
     emit selectedTextColorChanged(d->selectedTextColor);
 }
@@ -1634,10 +1625,8 @@ QSGNode *QQuickTextEdit::updatePaintNode(QSGNode *oldNode, UpdatePaintNodeData *
 
         QRectF bounds = boundingRect();
 
-        QColor selectionColor = d->control->palette().color(QPalette::Highlight);
-        QColor selectedTextColor = d->control->palette().color(QPalette::HighlightedText);
         node->addTextDocument(bounds.topLeft(), d->document, d->color, QQuickText::Normal, QColor(),
-                              selectionColor, selectedTextColor, selectionStart(),
+                              d->selectionColor, d->selectedTextColor, selectionStart(),
                               selectionEnd() - 1);  // selectionEnd() returns first char after
                                                     // selection
 
@@ -1756,31 +1745,21 @@ void QQuickTextEditPrivate::init()
     document = new QQuickTextDocumentWithImageResources(q);
 
     control = new QQuickTextControl(document, q);
-    control->setView(q);
     control->setTextInteractionFlags(Qt::LinksAccessibleByMouse | Qt::TextSelectableByKeyboard | Qt::TextEditable);
     control->setAcceptRichText(false);
     control->setCursorIsFocusIndicator(true);
 
-    // QQuickTextControl follows the default text color
-    // defined by the platform, declarative text
-    // should be black by default
-    QPalette pal = control->palette();
-    if (pal.color(QPalette::Text) != color) {
-        pal.setColor(QPalette::Text, color);
-        control->setPalette(pal);
-    }
-
-    QObject::connect(control, SIGNAL(updateRequest(QRectF)), q, SLOT(updateDocument()));
-    QObject::connect(control, SIGNAL(updateCursorRequest()), q, SLOT(updateCursor()));
-    QObject::connect(control, SIGNAL(textChanged()), q, SLOT(q_textChanged()));
-    QObject::connect(control, SIGNAL(selectionChanged()), q, SIGNAL(selectionChanged()));
-    QObject::connect(control, SIGNAL(selectionChanged()), q, SLOT(updateSelectionMarkers()));
-    QObject::connect(control, SIGNAL(cursorPositionChanged()), q, SLOT(updateSelectionMarkers()));
-    QObject::connect(control, SIGNAL(cursorPositionChanged()), q, SIGNAL(cursorPositionChanged()));
-    QObject::connect(control, SIGNAL(cursorRectangleChanged()), q, SLOT(moveCursorDelegate()));
-    QObject::connect(control, SIGNAL(linkActivated(QString)), q, SIGNAL(linkActivated(QString)));
+    FAST_CONNECT(control, SIGNAL(updateRequest(QRectF)), q, SLOT(updateDocument()));
+    FAST_CONNECT(control, SIGNAL(updateCursorRequest()), q, SLOT(updateCursor()));
+    FAST_CONNECT(control, SIGNAL(textChanged()), q, SLOT(q_textChanged()));
+    FAST_CONNECT(control, SIGNAL(selectionChanged()), q, SIGNAL(selectionChanged()));
+    FAST_CONNECT(control, SIGNAL(selectionChanged()), q, SLOT(updateSelectionMarkers()));
+    FAST_CONNECT(control, SIGNAL(cursorPositionChanged()), q, SLOT(updateSelectionMarkers()));
+    FAST_CONNECT(control, SIGNAL(cursorPositionChanged()), q, SIGNAL(cursorPositionChanged()));
+    FAST_CONNECT(control, SIGNAL(cursorRectangleChanged()), q, SLOT(moveCursorDelegate()));
+    FAST_CONNECT(control, SIGNAL(linkActivated(QString)), q, SIGNAL(linkActivated(QString)));
 #ifndef QT_NO_CLIPBOARD
-    QObject::connect(QGuiApplication::clipboard(), SIGNAL(dataChanged()), q, SLOT(q_canPasteChanged()));
+    FAST_CONNECT(QGuiApplication::clipboard(), SIGNAL(dataChanged()), q, SLOT(q_canPasteChanged()));
 #endif
     FAST_CONNECT(document, SIGNAL(undoAvailable(bool)), q, SIGNAL(canUndoChanged()));
     FAST_CONNECT(document, SIGNAL(redoAvailable(bool)), q, SIGNAL(canRedoChanged()));
index d69e24f..1497c20 100644 (file)
@@ -68,15 +68,17 @@ class QQuickTextEditPrivate : public QQuickImplicitSizeItemPrivate
 
 public:
     QQuickTextEditPrivate()
-      : color("black"), hAlign(QQuickTextEdit::AlignLeft), vAlign(QQuickTextEdit::AlignTop),
-      documentDirty(true), dirty(false), richText(false), cursorVisible(false), focusOnPress(true),
-      persistentSelection(false), requireImplicitWidth(false), selectByMouse(false), canPaste(false),
-      canPasteValid(false), hAlignImplicit(true), rightToLeftText(false),
-      textCached(false),
-      textMargin(0.0), lastSelectionStart(0), lastSelectionEnd(0), cursorComponent(0), cursor(0),
-      format(QQuickTextEdit::PlainText), document(0), wrapMode(QQuickTextEdit::NoWrap),
-      mouseSelectionMode(QQuickTextEdit::SelectCharacters),
-      lineCount(0), yoff(0), inputMethodHints(Qt::ImhNone), updateType(UpdatePaintNode)
+        : color(QRgb(0xFF000000)), selectionColor(QRgb(0xFF000080)), selectedTextColor(QRgb(0xFFFFFFFF))
+        , textMargin(0.0), font(sourceFont), cursorComponent(0), cursor(0), document(0), control(0)
+        , lastSelectionStart(0), lastSelectionEnd(0), lineCount(0), yoff(0)
+        , hAlign(QQuickTextEdit::AlignLeft), vAlign(QQuickTextEdit::AlignTop)
+        , format(QQuickTextEdit::PlainText), wrapMode(QQuickTextEdit::NoWrap)
+        , mouseSelectionMode(QQuickTextEdit::SelectCharacters), inputMethodHints(Qt::ImhNone)
+        , updateType(UpdatePaintNode)
+        , documentDirty(true), dirty(false), richText(false), cursorVisible(false)
+        , focusOnPress(true), persistentSelection(false), requireImplicitWidth(false)
+        , selectByMouse(false), canPaste(false), canPasteValid(false), hAlignImplicit(true)
+        , rightToLeftText(false), textCached(false)
     {
     }
 
@@ -92,17 +94,42 @@ public:
     void mirrorChange();
     qreal getImplicitWidth() const;
 
+    QColor color;
+    QColor selectionColor;
+    QColor selectedTextColor;
+
+    QSize contentSize;
+
+    qreal textMargin;
+
     QString text;
     QUrl baseUrl;
-    QFont font;
     QFont sourceFont;
-    QColor  color;
-    QColor  selectionColor;
-    QColor  selectedTextColor;
-    QString style;
-    QColor  styleColor;
+    QFont font;
+
+    QDeclarativeComponent* cursorComponent;
+    QQuickItem* cursor;
+    QQuickTextDocumentWithImageResources *document;
+    QQuickTextControl *control;
+
+    int lastSelectionStart;
+    int lastSelectionEnd;
+    int lineCount;
+    int yoff;
+
+    enum UpdateType {
+        UpdateNone,
+        UpdateOnlyPreprocess,
+        UpdatePaintNode
+    };
+
     QQuickTextEdit::HAlignment hAlign;
     QQuickTextEdit::VAlignment vAlign;
+    QQuickTextEdit::TextFormat format;
+    QQuickTextEdit::WrapMode wrapMode;
+    QQuickTextEdit::SelectionMode mouseSelectionMode;
+    Qt::InputMethodHints inputMethodHints;
+    UpdateType updateType;
 
     bool documentDirty : 1;
     bool dirty : 1;
@@ -117,28 +144,6 @@ public:
     bool hAlignImplicit:1;
     bool rightToLeftText:1;
     bool textCached:1;
-
-    qreal textMargin;
-    int lastSelectionStart;
-    int lastSelectionEnd;
-    QDeclarativeComponent* cursorComponent;
-    QQuickItem* cursor;
-    QQuickTextEdit::TextFormat format;
-    QQuickTextDocumentWithImageResources *document;
-    QQuickTextControl *control;
-    QQuickTextEdit::WrapMode wrapMode;
-    QQuickTextEdit::SelectionMode mouseSelectionMode;
-    int lineCount;
-    int yoff;
-    QSize contentSize;
-    Qt::InputMethodHints inputMethodHints;
-
-    enum UpdateType {
-        UpdateNone,
-        UpdateOnlyPreprocess,
-        UpdatePaintNode
-    };
-    UpdateType updateType;
 };
 
 QT_END_NAMESPACE
index 41c8546..62b85b8 100644 (file)
@@ -903,10 +903,7 @@ void tst_qquicktextedit::color()
         QVERIFY(textEditObject);
         QVERIFY(textEditPrivate);
         QVERIFY(textEditPrivate->control);
-
-        QPalette pal = textEditPrivate->control->palette();
         QCOMPARE(textEditPrivate->color, QColor("black"));
-        QCOMPARE(textEditPrivate->color, pal.color(QPalette::Text));
     }
     //test normal
     for (int i = 0; i < colorStrings.size(); i++)