From 72a3f170fbb1eaab9b293a54224e60f861ad5be3 Mon Sep 17 00:00:00 2001 From: Alan Alpert Date: Tue, 22 Sep 2009 16:51:52 +1000 Subject: Fixing and refactoring of QFxLineInput updating Still not working ideally. --- src/declarative/fx/qfxtextinput.cpp | 51 +++++++++++++++++++++++-------------- src/declarative/fx/qfxtextinput.h | 4 +-- 2 files changed, 34 insertions(+), 21 deletions(-) diff --git a/src/declarative/fx/qfxtextinput.cpp b/src/declarative/fx/qfxtextinput.cpp index f38a551..16ad185 100644 --- a/src/declarative/fx/qfxtextinput.cpp +++ b/src/declarative/fx/qfxtextinput.cpp @@ -131,9 +131,7 @@ void QFxTextInput::setFont(const QFont &font) d->cursorItem->setHeight(QFontMetrics(d->font).height()); moveCursor(); } - //updateSize(); - updateAll();//TODO: Only necessary updates - update(); + updateSize(); } /*! @@ -272,7 +270,7 @@ void QFxTextInput::setCursorVisible(bool on) return; d->cursorVisible = on; d->control->setCursorBlinkPeriod(on?QApplication::cursorFlashTime():0); - updateAll();//TODO: Only update cursor rect + //d->control should emit the cursor update regions } /*! @@ -593,14 +591,17 @@ bool QFxTextInput::event(QEvent* ev) { Q_D(QFxTextInput); //Anything we don't deal with ourselves, pass to the control + bool handled = false; switch(ev->type()){ case QEvent::KeyPress: case QEvent::GraphicsSceneMousePress: break; default: - return d->control->processEvent(ev); + handled = d->control->processEvent(ev); } - return false; + if(!handled) + return QFxPaintedItem::event(ev); + return true; } void QFxTextInput::geometryChanged(const QRectF &newGeometry, @@ -667,13 +668,13 @@ void QFxTextInputPrivate::init() q, SLOT(q_textChanged())); q->connect(control, SIGNAL(accepted()), q, SIGNAL(accepted())); - q->connect(control, SIGNAL(updateNeeded(const QRect &)), - // q, SLOT(dirtyCache(const QRect &))); - q, SLOT(updateAll())); + q->connect(control, SIGNAL(updateNeeded(QRect)), + q, SLOT(updateRect(QRect))); q->connect(control, SIGNAL(cursorPositionChanged(int,int)), - q, SLOT(updateAll())); + q, SLOT(updateRect()));//TODO: Only update rect between pos's q->connect(control, SIGNAL(selectionChanged()), - q, SLOT(updateAll())); + q, SLOT(updateRect()));//TODO: Only update rect in selection + //Note that above TODOs are waiting, pending getting the updateRect working right q->updateSize(); oldValidity = control->hasAcceptableInput(); lastSelectionStart = 0; @@ -719,7 +720,7 @@ void QFxTextInput::selectionChanged() void QFxTextInput::q_textChanged() { Q_D(QFxTextInput); - updateAll(); + updateSize(); emit textChanged(); if(hasAcceptableInput() != d->oldValidity){ d->oldValidity = hasAcceptableInput(); @@ -727,21 +728,33 @@ void QFxTextInput::q_textChanged() } } -//### Please replace this function with proper updating -void QFxTextInput::updateAll() +void QFxTextInput::updateRect(const QRect &r) { - clearCache(); - updateSize(); + //### Only update that rect. + if(r == QRect()) + clearCache(); + //else + //dirtyCache(r);//Not working yet update(); } -void QFxTextInput::updateSize() +void QFxTextInput::updateSize(bool needsRedraw) { Q_D(QFxTextInput); + int w = width(); + int h = height(); setImplicitHeight(d->control->height()); //d->control->width() is width of the line, not of the text - setImplicitWidth(d->control->naturalTextWidth()+2); - setContentsSize(QSize(width(), height())); + //And naturalTextWidth() is the text, not including the cursor + int cursorWidth = d->control->cursorWidth(); + if(d->cursorItem) + cursorWidth = d->cursorItem->width(); + setImplicitWidth(d->control->naturalTextWidth()+2+cursorWidth); + setContentsSize(QSize(width(), height()));//Repaints if changed + if(w==width() && h==height() && needsRedraw){ + clearCache(); + update(); + } } QT_END_NAMESPACE diff --git a/src/declarative/fx/qfxtextinput.h b/src/declarative/fx/qfxtextinput.h index 4fa4100..b1e8b14 100644 --- a/src/declarative/fx/qfxtextinput.h +++ b/src/declarative/fx/qfxtextinput.h @@ -184,13 +184,13 @@ public Q_SLOTS: void selectAll(); private Q_SLOTS: - void updateSize(); + void updateSize(bool needsRedraw = true); void q_textChanged(); void selectionChanged(); - void updateAll(); void createCursor(); void moveCursor(); void cursorPosChanged(); + void updateRect(const QRect &r = QRect()); private: Q_DECLARE_PRIVATE_D(QGraphicsItem::d_ptr.data(), QFxTextInput); -- cgit v0.12 From 1c011c7f70791e0a4a38bd9f8bb1988cd604305c Mon Sep 17 00:00:00 2001 From: Alan Alpert Date: Wed, 23 Sep 2009 10:57:49 +1000 Subject: Fix cursor drawing in QFxTextInput and QFxTextEdit Fixes a bug in QFxPaintedItem that was plaguing them both. Optimizes the painting of he blinking cursor in QFxTextInput (don't redraw whole thing each blink) Fixes cursor drawing and lack thereof bugs with QFxTextEdit. Task-number: QT-669 --- src/declarative/fx/qfxpainteditem.cpp | 3 ++- src/declarative/fx/qfxtextedit.cpp | 5 +++++ src/declarative/fx/qfxtextinput.cpp | 17 +++++++++-------- 3 files changed, 16 insertions(+), 9 deletions(-) diff --git a/src/declarative/fx/qfxpainteditem.cpp b/src/declarative/fx/qfxpainteditem.cpp index 5471e1b..85924e0 100644 --- a/src/declarative/fx/qfxpainteditem.cpp +++ b/src/declarative/fx/qfxpainteditem.cpp @@ -101,7 +101,7 @@ void QFxPaintedItem::dirtyCache(const QRect& rect) QFxPaintedItemPrivate::ImageCacheItem *c = d->imagecache[i]; QRect isect = (c->area & rect) | c->dirty; if (isect == c->area && !inpaint) { - d->imagecache.removeAt(i); + delete d->imagecache.takeAt(i); } else { c->dirty = isect; ++i; @@ -231,6 +231,7 @@ void QFxPaintedItem::paint(QPainter *p, const QStyleOptionGraphicsItem *, QWidge QPainter qp(&d->imagecache[i]->image); qp.setRenderHints(QPainter::HighQualityAntialiasing | QPainter::Antialiasing | QPainter::TextAntialiasing | QPainter::SmoothPixmapTransform, d->smooth); qp.translate(-area.x(), -area.y()); + qp.eraseRect(d->imagecache[i]->dirty); if (d->fillColor.isValid()) qp.fillRect(d->imagecache[i]->dirty,d->fillColor); qp.setClipRect(d->imagecache[i]->dirty); diff --git a/src/declarative/fx/qfxtextedit.cpp b/src/declarative/fx/qfxtextedit.cpp index d1bafd6..58b5c8e 100644 --- a/src/declarative/fx/qfxtextedit.cpp +++ b/src/declarative/fx/qfxtextedit.cpp @@ -1047,6 +1047,11 @@ void QFxTextEdit::updateSize() //### need to comfirm cost of always setting these int newWidth = (int)d->document->idealWidth(); d->document->setTextWidth(newWidth); // ### QTextDoc> Alignment will not work unless textWidth is set. Does Text need this line as well? + int cursorWidth = 1; + if(d->cursor) + cursorWidth = d->cursor->width(); + newWidth += cursorWidth; + newWidth += 3;// ### Need a better way of ensuring cursor is in width setImplicitWidth(newWidth); setImplicitHeight(d->text.isEmpty() ? fm.height() : (int)d->document->size().height()); diff --git a/src/declarative/fx/qfxtextinput.cpp b/src/declarative/fx/qfxtextinput.cpp index 16ad185..b7b155a 100644 --- a/src/declarative/fx/qfxtextinput.cpp +++ b/src/declarative/fx/qfxtextinput.cpp @@ -625,7 +625,9 @@ void QFxTextInput::drawContents(QPainter *p, const QRect &r) flags |= QLineControl::DrawSelections; } - d->control->draw(p, QPoint(0,0), r, flags); + QPoint offset = QPoint(0,0); + QRect clipRect = r; + d->control->draw(p, offset, clipRect, flags); p->restore(); } @@ -674,7 +676,7 @@ void QFxTextInputPrivate::init() q, SLOT(updateRect()));//TODO: Only update rect between pos's q->connect(control, SIGNAL(selectionChanged()), q, SLOT(updateRect()));//TODO: Only update rect in selection - //Note that above TODOs are waiting, pending getting the updateRect working right + //Note that above TODOs probably aren't that big a savings q->updateSize(); oldValidity = control->hasAcceptableInput(); lastSelectionStart = 0; @@ -730,11 +732,10 @@ void QFxTextInput::q_textChanged() void QFxTextInput::updateRect(const QRect &r) { - //### Only update that rect. if(r == QRect()) clearCache(); - //else - //dirtyCache(r);//Not working yet + else + dirtyCache(r); update(); } @@ -744,12 +745,12 @@ void QFxTextInput::updateSize(bool needsRedraw) int w = width(); int h = height(); setImplicitHeight(d->control->height()); - //d->control->width() is width of the line, not of the text - //And naturalTextWidth() is the text, not including the cursor int cursorWidth = d->control->cursorWidth(); if(d->cursorItem) cursorWidth = d->cursorItem->width(); - setImplicitWidth(d->control->naturalTextWidth()+2+cursorWidth); + //### Is QFontMetrics too slow? + QFontMetricsF fm(d->font); + setImplicitWidth(fm.width(d->control->displayText())+cursorWidth); setContentsSize(QSize(width(), height()));//Repaints if changed if(w==width() && h==height() && needsRedraw){ clearCache(); -- cgit v0.12