diff options
Diffstat (limited to 'src/declarative/graphicsitems')
45 files changed, 3218 insertions, 409 deletions
diff --git a/src/declarative/graphicsitems/graphicsitems.pri b/src/declarative/graphicsitems/graphicsitems.pri index ffdeb29..9904274 100644 --- a/src/declarative/graphicsitems/graphicsitems.pri +++ b/src/declarative/graphicsitems/graphicsitems.pri @@ -50,7 +50,12 @@ HEADERS += \ $$PWD/qdeclarativelayoutitem_p.h \ $$PWD/qdeclarativeitemchangelistener_p.h \ $$PWD/qdeclarativegraphicswidget_p.h \ - $$PWD/qdeclarativetextlayout_p.h + $$PWD/qdeclarativetextlayout_p.h \ + $$PWD/qdeclarativepincharea_p.h \ + $$PWD/qdeclarativepincharea_p_p.h \ + $$PWD/qdeclarativeimplicitsizeitem_p.h \ + $$PWD/qdeclarativeimplicitsizeitem_p_p.h + SOURCES += \ $$PWD/qdeclarativeitemsmodule.cpp \ @@ -83,5 +88,7 @@ SOURCES += \ $$PWD/qdeclarativelistview.cpp \ $$PWD/qdeclarativelayoutitem.cpp \ $$PWD/qdeclarativegraphicswidget.cpp \ - $$PWD/qdeclarativetextlayout.cpp + $$PWD/qdeclarativetextlayout.cpp \ + $$PWD/qdeclarativepincharea.cpp \ + $$PWD/qdeclarativeimplicitsizeitem.cpp diff --git a/src/declarative/graphicsitems/qdeclarativeanimatedimage.cpp b/src/declarative/graphicsitems/qdeclarativeanimatedimage.cpp index 4729817..27bb6a2 100644 --- a/src/declarative/graphicsitems/qdeclarativeanimatedimage.cpp +++ b/src/declarative/graphicsitems/qdeclarativeanimatedimage.cpp @@ -86,6 +86,25 @@ QT_BEGIN_NAMESPACE \sa BorderImage, Image */ +/*! + \qmlproperty bool AnimatedImage::cache + \since Quick 1.1 + + Specifies whether the image should be cached. The default value is + true. Setting \a cache to false is useful when dealing with large images, + to make sure that they aren't cached at the expense of small 'ui element' images. +*/ + +/*! + \qmlproperty bool AnimatedImage::mirror + \since Quick 1.1 + + This property holds whether the image should be horizontally inverted + (effectively displaying a mirrored image). + + The default value is false. +*/ + QDeclarativeAnimatedImage::QDeclarativeAnimatedImage(QDeclarativeItem *parent) : QDeclarativeImage(*(new QDeclarativeAnimatedImagePrivate), parent) { @@ -126,7 +145,7 @@ void QDeclarativeAnimatedImage::setPaused(bool pause) \qmlproperty bool AnimatedImage::playing This property holds whether the animated image is playing. - By defaults, this property is true, meaning that the animation + By default, this property is true, meaning that the animation will start playing immediately. */ bool QDeclarativeAnimatedImage::isPlaying() const diff --git a/src/declarative/graphicsitems/qdeclarativeborderimage.cpp b/src/declarative/graphicsitems/qdeclarativeborderimage.cpp index a851864..c03c624 100644 --- a/src/declarative/graphicsitems/qdeclarativeborderimage.cpp +++ b/src/declarative/graphicsitems/qdeclarativeborderimage.cpp @@ -200,6 +200,16 @@ QDeclarativeBorderImage::~QDeclarativeBorderImage() */ /*! + \qmlproperty bool BorderImage::mirror + \since Quick 1.1 + + This property holds whether the image should be horizontally inverted + (effectively displaying a mirrored image). + + The default value is false. +*/ + +/*! \qmlproperty url BorderImage::source This property holds the URL that refers to the source image. @@ -228,6 +238,16 @@ QDeclarativeBorderImage::~QDeclarativeBorderImage() \sa QDeclarativeImageProvider */ + +/*! + \qmlproperty QSize BorderImage::sourceSize + + This property holds the actual width and height of the loaded image. + + In BorderImage, this property is read-only. + + \sa Image::sourceSize +*/ void QDeclarativeBorderImage::setSource(const QUrl &url) { Q_D(QDeclarativeBorderImage); @@ -292,7 +312,12 @@ void QDeclarativeBorderImage::load() } } else { - d->pix.load(qmlEngine(this), d->url, d->async); + QDeclarativePixmap::Options options; + if (d->async) + options |= QDeclarativePixmap::Asynchronous; + if (d->cache) + options |= QDeclarativePixmap::Cache; + d->pix.load(qmlEngine(this), d->url, options); if (d->pix.isLoading()) { d->pix.connectFinished(this, SLOT(requestFinished())); @@ -312,6 +337,7 @@ void QDeclarativeBorderImage::load() d->progress = 1.0; emit statusChanged(d->status); emit progressChanged(d->progress); + requestFinished(); update(); } } @@ -418,7 +444,12 @@ void QDeclarativeBorderImage::setGridScaledImage(const QDeclarativeGridScaledIma d->sciurl = d->url.resolved(QUrl(sci.pixmapUrl())); - d->pix.load(qmlEngine(this), d->sciurl, d->async); + QDeclarativePixmap::Options options; + if (d->async) + options |= QDeclarativePixmap::Asynchronous; + if (d->cache) + options |= QDeclarativePixmap::Cache; + d->pix.load(qmlEngine(this), d->sciurl, options); if (d->pix.isLoading()) { static int thisRequestProgress = -1; @@ -470,6 +501,9 @@ void QDeclarativeBorderImage::requestFinished() setImplicitWidth(impsize.width()); setImplicitHeight(impsize.height()); + if (d->sourcesize.width() != d->pix.width() || d->sourcesize.height() != d->pix.height()) + emit sourceSizeChanged(); + d->progress = 1.0; emit statusChanged(d->status); emit progressChanged(1.0); @@ -519,8 +553,15 @@ void QDeclarativeBorderImage::paint(QPainter *p, const QStyleOptionGraphicsItem bool oldAA = p->testRenderHint(QPainter::Antialiasing); bool oldSmooth = p->testRenderHint(QPainter::SmoothPixmapTransform); + QTransform oldTransform; if (d->smooth) p->setRenderHints(QPainter::Antialiasing | QPainter::SmoothPixmapTransform, d->smooth); + if (d->mirror) { + oldTransform = p->transform(); + QTransform mirror; + mirror.translate(d->width(), 0).scale(-1, 1.0); + p->setWorldTransform(mirror * oldTransform); + } const QDeclarativeScaleGrid *border = d->getScaleGrid(); int left = border->left(); @@ -546,6 +587,8 @@ void QDeclarativeBorderImage::paint(QPainter *p, const QStyleOptionGraphicsItem p->setRenderHint(QPainter::Antialiasing, oldAA); p->setRenderHint(QPainter::SmoothPixmapTransform, oldSmooth); } + if (d->mirror) + p->setWorldTransform(oldTransform); } QT_END_NAMESPACE diff --git a/src/declarative/graphicsitems/qdeclarativeborderimage_p.h b/src/declarative/graphicsitems/qdeclarativeborderimage_p.h index ca1eae8..f4c6594 100644 --- a/src/declarative/graphicsitems/qdeclarativeborderimage_p.h +++ b/src/declarative/graphicsitems/qdeclarativeborderimage_p.h @@ -63,6 +63,9 @@ class Q_AUTOTEST_EXPORT QDeclarativeBorderImage : public QDeclarativeImageBase Q_PROPERTY(TileMode horizontalTileMode READ horizontalTileMode WRITE setHorizontalTileMode NOTIFY horizontalTileModeChanged) Q_PROPERTY(TileMode verticalTileMode READ verticalTileMode WRITE setVerticalTileMode NOTIFY verticalTileModeChanged) + // read-only for BorderImage + Q_PROPERTY(QSize sourceSize READ sourceSize NOTIFY sourceSizeChanged) + public: QDeclarativeBorderImage(QDeclarativeItem *parent=0); ~QDeclarativeBorderImage(); @@ -83,6 +86,7 @@ public: Q_SIGNALS: void horizontalTileModeChanged(); void verticalTileModeChanged(); + void sourceSizeChanged(); protected: virtual void load(); diff --git a/src/declarative/graphicsitems/qdeclarativeflickable.cpp b/src/declarative/graphicsitems/qdeclarativeflickable.cpp index 14388b3..a12a5ed 100644 --- a/src/declarative/graphicsitems/qdeclarativeflickable.cpp +++ b/src/declarative/graphicsitems/qdeclarativeflickable.cpp @@ -876,7 +876,7 @@ void QDeclarativeFlickable::wheelEvent(QGraphicsSceneWheelEvent *event) Q_D(QDeclarativeFlickable); if (!d->interactive) { QDeclarativeItem::wheelEvent(event); - } else if (yflick()) { + } else if (yflick() && event->orientation() == Qt::Vertical) { if (event->delta() > 0) d->vData.velocity = qMax(event->delta() - d->vData.smoothVelocity.value(), qreal(250.0)); else @@ -888,7 +888,7 @@ void QDeclarativeFlickable::wheelEvent(QGraphicsSceneWheelEvent *event) movementStarting(); } event->accept(); - } else if (xflick()) { + } else if (xflick() && event->orientation() == Qt::Horizontal) { if (event->delta() > 0) d->hData.velocity = qMax(event->delta() - d->hData.smoothVelocity.value(), qreal(250.0)); else @@ -1271,6 +1271,60 @@ void QDeclarativeFlickable::setContentHeight(qreal h) d->updateBeginningEnd(); } +/*! + \qmlmethod Flickable::resizeContent(real width, real height, QPointF center) + \preliminary + + Resizes the content to \a width x \a height about \a center. + + \bold {This method was added in QtQuick 1.1.} + + This does not scale the contents of the Flickable - it only resizes the \l contentWidth + and \l contentHeight. + + Resizing the content may result in the content being positioned outside + the bounds of the Flickable. Calling \l returnToBounds() will + move the content back within legal bounds. +*/ +void QDeclarativeFlickable::resizeContent(qreal w, qreal h, QPointF center) +{ + Q_D(QDeclarativeFlickable); + if (w != d->hData.viewSize) { + qreal oldSize = d->hData.viewSize; + setContentWidth(w); + if (center.x() != 0) { + qreal pos = center.x() * w / oldSize; + setContentX(contentX() + pos - center.x()); + } + } + if (h != d->vData.viewSize) { + qreal oldSize = d->vData.viewSize; + setContentHeight(h); + if (center.y() != 0) { + qreal pos = center.y() * h / oldSize; + setContentY(contentY() + pos - center.y()); + } + } +} + +/*! + \qmlmethod Flickable::returnToBounds() + \preliminary + + Ensures the content is within legal bounds. + + \bold {This method was added in QtQuick 1.1.} + + This may be called to ensure that the content is within legal bounds + after manually positioning the content. +*/ +void QDeclarativeFlickable::returnToBounds() +{ + Q_D(QDeclarativeFlickable); + d->fixupX(); + d->fixupY(); +} + qreal QDeclarativeFlickable::vWidth() const { Q_D(const QDeclarativeFlickable); diff --git a/src/declarative/graphicsitems/qdeclarativeflickable_p.h b/src/declarative/graphicsitems/qdeclarativeflickable_p.h index 1359dc8..4fde1d5 100644 --- a/src/declarative/graphicsitems/qdeclarativeflickable_p.h +++ b/src/declarative/graphicsitems/qdeclarativeflickable_p.h @@ -149,6 +149,9 @@ public: FlickableDirection flickableDirection() const; void setFlickableDirection(FlickableDirection); + Q_INVOKABLE Q_REVISION(1) void resizeContent(qreal w, qreal h, QPointF center); + Q_INVOKABLE Q_REVISION(1) void returnToBounds(); + Q_SIGNALS: void contentWidthChanged(); void contentHeightChanged(); diff --git a/src/declarative/graphicsitems/qdeclarativeflipable.cpp b/src/declarative/graphicsitems/qdeclarativeflipable.cpp index 160872c..5fda758 100644 --- a/src/declarative/graphicsitems/qdeclarativeflipable.cpp +++ b/src/declarative/graphicsitems/qdeclarativeflipable.cpp @@ -142,6 +142,7 @@ void QDeclarativeFlipable::setFront(QGraphicsObject *front) d->front->setParentItem(this); if (Back == d->current) d->front->setOpacity(0.); + emit frontChanged(); } QGraphicsObject *QDeclarativeFlipable::back() @@ -165,6 +166,7 @@ void QDeclarativeFlipable::setBack(QGraphicsObject *back) this, SLOT(retransformBack())); connect(back, SIGNAL(heightChanged()), this, SLOT(retransformBack())); + emit backChanged(); } void QDeclarativeFlipable::retransformBack() diff --git a/src/declarative/graphicsitems/qdeclarativeflipable_p.h b/src/declarative/graphicsitems/qdeclarativeflipable_p.h index c294697..4ea2ec0 100644 --- a/src/declarative/graphicsitems/qdeclarativeflipable_p.h +++ b/src/declarative/graphicsitems/qdeclarativeflipable_p.h @@ -60,8 +60,8 @@ class Q_AUTOTEST_EXPORT QDeclarativeFlipable : public QDeclarativeItem Q_OBJECT Q_ENUMS(Side) - Q_PROPERTY(QGraphicsObject *front READ front WRITE setFront) - Q_PROPERTY(QGraphicsObject *back READ back WRITE setBack) + Q_PROPERTY(QGraphicsObject *front READ front WRITE setFront NOTIFY frontChanged) + Q_PROPERTY(QGraphicsObject *back READ back WRITE setBack NOTIFY backChanged) Q_PROPERTY(Side side READ side NOTIFY sideChanged) //### flipAxis //### flipRotation @@ -79,6 +79,8 @@ public: Side side() const; Q_SIGNALS: + void frontChanged(); + void backChanged(); void sideChanged(); private Q_SLOTS: diff --git a/src/declarative/graphicsitems/qdeclarativegridview.cpp b/src/declarative/graphicsitems/qdeclarativegridview.cpp index 9e101b0..8baaa50 100644 --- a/src/declarative/graphicsitems/qdeclarativegridview.cpp +++ b/src/declarative/graphicsitems/qdeclarativegridview.cpp @@ -336,6 +336,7 @@ public: } } + void positionViewAtIndex(int index, int mode); virtual void fixup(AxisData &data, qreal minExtent, qreal maxExtent); virtual void flick(AxisData &data, qreal minExtent, qreal maxExtent, qreal vSize, QDeclarativeTimeLineCallback::Callback fixupCallback, qreal velocity); @@ -1615,6 +1616,8 @@ void QDeclarativeGridView::setFlow(Flow flow) setContentHeight(-1); setFlickableDirection(QDeclarativeFlickable::HorizontalFlick); } + setContentX(0); + setContentY(0); d->clear(); d->updateGrid(); refill(); @@ -1776,6 +1779,9 @@ void QDeclarativeGridView::setFooter(QDeclarativeComponent *footer) Q_D(QDeclarativeGridView); if (d->footerComponent != footer) { if (d->footer) { + if (scene()) + scene()->removeItem(d->footer->item); + d->footer->item->deleteLater(); delete d->footer; d->footer = 0; } @@ -1783,6 +1789,7 @@ void QDeclarativeGridView::setFooter(QDeclarativeComponent *footer) if (isComponentComplete()) { d->updateFooter(); d->updateGrid(); + d->fixupPosition(); } emit footerChanged(); } @@ -1808,6 +1815,9 @@ void QDeclarativeGridView::setHeader(QDeclarativeComponent *header) Q_D(QDeclarativeGridView); if (d->headerComponent != header) { if (d->header) { + if (scene()) + scene()->removeItem(d->header->item); + d->header->item->deleteLater(); delete d->header; d->header = 0; } @@ -1816,6 +1826,7 @@ void QDeclarativeGridView::setHeader(QDeclarativeComponent *header) d->updateHeader(); d->updateFooter(); d->updateGrid(); + d->fixupPosition(); } emit headerChanged(); } @@ -2122,6 +2133,78 @@ void QDeclarativeGridView::moveCurrentIndexRight() } } +void QDeclarativeGridViewPrivate::positionViewAtIndex(int index, int mode) +{ + Q_Q(QDeclarativeGridView); + if (!isValid()) + return; + if (mode < QDeclarativeGridView::Beginning || mode > QDeclarativeGridView::Contain) + return; + + int idx = qMax(qMin(index, model->count()-1), 0); + + if (layoutScheduled) + layout(); + qreal pos = position(); + FxGridItem *item = visibleItem(idx); + qreal maxExtent = flow == QDeclarativeGridView::LeftToRight ? -q->maxYExtent() : -q->maxXExtent(); + if (!item) { + int itemPos = rowPosAt(idx); + // save the currently visible items in case any of them end up visible again + QList<FxGridItem*> oldVisible = visibleItems; + visibleItems.clear(); + visibleIndex = idx - idx % columns; + maxExtent = flow == QDeclarativeGridView::LeftToRight ? -q->maxYExtent() : -q->maxXExtent(); + setPosition(qMin(qreal(itemPos), maxExtent)); + // now release the reference to all the old visible items. + for (int i = 0; i < oldVisible.count(); ++i) + releaseItem(oldVisible.at(i)); + item = visibleItem(idx); + } + if (item) { + qreal itemPos = item->rowPos(); + switch (mode) { + case QDeclarativeGridView::Beginning: + pos = itemPos; + if (index < 0 && header) { + pos -= flow == QDeclarativeGridView::LeftToRight + ? header->item->height() + : header->item->width(); + } + break; + case QDeclarativeGridView::Center: + pos = itemPos - (size() - rowSize())/2; + break; + case QDeclarativeGridView::End: + pos = itemPos - size() + rowSize(); + if (index >= model->count() && footer) { + pos += flow == QDeclarativeGridView::LeftToRight + ? footer->item->height() + : footer->item->width(); + } + break; + case QDeclarativeGridView::Visible: + if (itemPos > pos + size()) + pos = itemPos - size() + rowSize(); + else if (item->endRowPos() < pos) + pos = itemPos; + break; + case QDeclarativeGridView::Contain: + if (item->endRowPos() > pos + size()) + pos = itemPos - size() + rowSize(); + if (itemPos < pos) + pos = itemPos; + } + pos = qMin(pos, maxExtent); + qreal minExtent = flow == QDeclarativeGridView::LeftToRight ? -q->minYExtent() : -q->minXExtent(); + pos = qMax(pos, minExtent); + moveReason = QDeclarativeGridViewPrivate::Other; + q->cancelFlick(); + setPosition(pos); + } + fixupPosition(); +} + /*! \qmlmethod GridView::positionViewAtIndex(int index, PositionMode mode) @@ -2159,58 +2242,42 @@ void QDeclarativeGridView::positionViewAtIndex(int index, int mode) Q_D(QDeclarativeGridView); if (!d->isValid() || index < 0 || index >= d->model->count()) return; - if (mode < Beginning || mode > Contain) + d->positionViewAtIndex(index, mode); +} + +/*! + \qmlmethod GridView::positionViewAtBeginning() + \qmlmethod GridView::positionViewAtEnd() + + Positions the view at the beginning or end, taking into account any header or footer. + + It is not recommended to use \l {Flickable::}{contentX} or \l {Flickable::}{contentY} to position the view + at a particular index. This is unreliable since removing items from the start + of the list does not cause all other items to be repositioned, and because + the actual start of the view can vary based on the size of the delegates. + + \bold Note: methods should only be called after the Component has completed. To position + the view at startup, this method should be called by Component.onCompleted. For + example, to position the view at the end on startup: + + \code + Component.onCompleted: positionViewAtEnd() + \endcode +*/ +void QDeclarativeGridView::positionViewAtBeginning() +{ + Q_D(QDeclarativeGridView); + if (!d->isValid()) return; + d->positionViewAtIndex(-1, Beginning); +} - if (d->layoutScheduled) - d->layout(); - qreal pos = d->position(); - qreal maxExtent = d->flow == QDeclarativeGridView::LeftToRight ? -maxYExtent() : -maxXExtent(); - FxGridItem *item = d->visibleItem(index); - if (!item) { - int itemPos = d->rowPosAt(index); - // save the currently visible items in case any of them end up visible again - QList<FxGridItem*> oldVisible = d->visibleItems; - d->visibleItems.clear(); - d->visibleIndex = index - index % d->columns; - d->setPosition(qMin(qreal(itemPos), maxExtent)); - // now release the reference to all the old visible items. - for (int i = 0; i < oldVisible.count(); ++i) - d->releaseItem(oldVisible.at(i)); - item = d->visibleItem(index); - } - if (item) { - qreal itemPos = item->rowPos(); - switch (mode) { - case Beginning: - pos = itemPos; - break; - case Center: - pos = itemPos - (d->size() - d->rowSize())/2; - break; - case End: - pos = itemPos - d->size() + d->rowSize(); - break; - case Visible: - if (itemPos > pos + d->size()) - pos = itemPos - d->size() + d->rowSize(); - else if (item->endRowPos() < pos) - pos = itemPos; - break; - case Contain: - if (item->endRowPos() > pos + d->size()) - pos = itemPos - d->size() + d->rowSize(); - if (itemPos < pos) - pos = itemPos; - } - pos = qMin(pos, maxExtent); - qreal minExtent = d->flow == QDeclarativeGridView::LeftToRight ? -minYExtent() : -minXExtent(); - pos = qMax(pos, minExtent); - d->moveReason = QDeclarativeGridViewPrivate::Other; - cancelFlick(); - d->setPosition(pos); - } - d->fixupPosition(); +void QDeclarativeGridView::positionViewAtEnd() +{ + Q_D(QDeclarativeGridView); + if (!d->isValid()) + return; + d->positionViewAtIndex(d->model->count(), End); } /*! @@ -2560,12 +2627,8 @@ void QDeclarativeGridView::itemsMoved(int from, int to, int count) return; QHash<int,FxGridItem*> moved; - bool removedBeforeVisible = false; FxGridItem *firstItem = d->firstVisibleItem(); - if (from < to && from < d->visibleIndex && to > d->visibleIndex) - removedBeforeVisible = true; - QList<FxGridItem*>::Iterator it = d->visibleItems.begin(); while (it != d->visibleItems.end()) { FxGridItem *item = *it; @@ -2574,16 +2637,12 @@ void QDeclarativeGridView::itemsMoved(int from, int to, int count) item->index += (to-from); moved.insert(item->index, item); it = d->visibleItems.erase(it); - if (item->rowPos() < firstItem->rowPos()) - removedBeforeVisible = true; } else { if (item->index > from && item->index != -1) { // move everything after the moved items. item->index -= count; if (item->index < d->visibleIndex) d->visibleIndex = item->index; - } else if (item->index != -1) { - removedBeforeVisible = true; } ++it; } diff --git a/src/declarative/graphicsitems/qdeclarativegridview_p.h b/src/declarative/graphicsitems/qdeclarativegridview_p.h index 7dc8954..248b9ef 100644 --- a/src/declarative/graphicsitems/qdeclarativegridview_p.h +++ b/src/declarative/graphicsitems/qdeclarativegridview_p.h @@ -159,6 +159,8 @@ public: Q_INVOKABLE void positionViewAtIndex(int index, int mode); Q_INVOKABLE int indexAt(int x, int y) const; + Q_INVOKABLE Q_REVISION(1) void positionViewAtBeginning(); + Q_INVOKABLE Q_REVISION(1) void positionViewAtEnd(); static QDeclarativeGridViewAttached *qmlAttachedProperties(QObject *); diff --git a/src/declarative/graphicsitems/qdeclarativeimage.cpp b/src/declarative/graphicsitems/qdeclarativeimage.cpp index e90a4df..2c9bde5 100644 --- a/src/declarative/graphicsitems/qdeclarativeimage.cpp +++ b/src/declarative/graphicsitems/qdeclarativeimage.cpp @@ -458,84 +458,102 @@ QRectF QDeclarativeImage::boundingRect() const are always loaded asynchonously. */ +/*! + \qmlproperty bool Image::cache + \since Quick 1.1 + + Specifies whether the image should be cached. The default value is + true. Setting \a cache to false is useful when dealing with large images, + to make sure that they aren't cached at the expense of small 'ui element' images. +*/ + +/*! + \qmlproperty bool Image::mirror + \since Quick 1.1 + + This property holds whether the image should be horizontally inverted + (effectively displaying a mirrored image). + + The default value is false. +*/ + + void QDeclarativeImage::paint(QPainter *p, const QStyleOptionGraphicsItem *, QWidget *) { Q_D(QDeclarativeImage); if (d->pix.pixmap().isNull() ) return; - bool oldAA = p->testRenderHint(QPainter::Antialiasing); - bool oldSmooth = p->testRenderHint(QPainter::SmoothPixmapTransform); - if (d->smooth) - p->setRenderHints(QPainter::Antialiasing | QPainter::SmoothPixmapTransform, d->smooth); + int drawWidth = width(); + int drawHeight = height(); + bool doClip = false; + QTransform transform; + qreal widthScale = width() / qreal(d->pix.width()); + qreal heightScale = height() / qreal(d->pix.height()); if (width() != d->pix.width() || height() != d->pix.height()) { if (d->fillMode >= Tile) { - if (d->fillMode == Tile) { - p->drawTiledPixmap(QRectF(0,0,width(),height()), d->pix); - } else { - qreal widthScale = width() / qreal(d->pix.width()); - qreal heightScale = height() / qreal(d->pix.height()); - - QTransform scale; - if (d->fillMode == TileVertically) { - scale.scale(widthScale, 1.0); - QTransform old = p->transform(); - p->setWorldTransform(scale * old); - p->drawTiledPixmap(QRectF(0,0,d->pix.width(),height()), d->pix); - p->setWorldTransform(old); - } else { - scale.scale(1.0, heightScale); - QTransform old = p->transform(); - p->setWorldTransform(scale * old); - p->drawTiledPixmap(QRectF(0,0,width(),d->pix.height()), d->pix); - p->setWorldTransform(old); - } + if (d->fillMode == TileVertically) { + transform.scale(widthScale, 1.0); + drawWidth = d->pix.width(); + } else if (d->fillMode == TileHorizontally) { + transform.scale(1.0, heightScale); + drawHeight = d->pix.height(); } } else { - qreal widthScale = width() / qreal(d->pix.width()); - qreal heightScale = height() / qreal(d->pix.height()); - - QTransform scale; - if (d->fillMode == PreserveAspectFit) { if (widthScale <= heightScale) { heightScale = widthScale; - scale.translate(0, (height() - heightScale * d->pix.height()) / 2); + transform.translate(0, (height() - heightScale * d->pix.height()) / 2); } else if(heightScale < widthScale) { widthScale = heightScale; - scale.translate((width() - widthScale * d->pix.width()) / 2, 0); + transform.translate((width() - widthScale * d->pix.width()) / 2, 0); } } else if (d->fillMode == PreserveAspectCrop) { if (widthScale < heightScale) { widthScale = heightScale; - scale.translate((width() - widthScale * d->pix.width()) / 2, 0); + transform.translate((width() - widthScale * d->pix.width()) / 2, 0); } else if(heightScale < widthScale) { heightScale = widthScale; - scale.translate(0, (height() - heightScale * d->pix.height()) / 2); + transform.translate(0, (height() - heightScale * d->pix.height()) / 2); } } - if (clip()) { - p->save(); - p->setClipRect(QRectF(0, 0, d->mWidth, d->mHeight), Qt::IntersectClip); - } - scale.scale(widthScale, heightScale); - QTransform old = p->transform(); - p->setWorldTransform(scale * old); - p->drawPixmap(0, 0, d->pix); - p->setWorldTransform(old); - if (clip()) { - p->restore(); - } + transform.scale(widthScale, heightScale); + drawWidth = d->pix.width(); + drawHeight = d->pix.height(); + doClip = clip(); } - } else { - p->drawPixmap(0, 0, d->pix); } + QTransform oldTransform; + bool oldAA = p->testRenderHint(QPainter::Antialiasing); + bool oldSmooth = p->testRenderHint(QPainter::SmoothPixmapTransform); + if (d->smooth) + p->setRenderHints(QPainter::Antialiasing | QPainter::SmoothPixmapTransform, d->smooth); + if (doClip) { + p->save(); + p->setClipRect(QRectF(0, 0, d->mWidth, d->mHeight), Qt::IntersectClip); + } + if (d->mirror) + transform.translate(drawWidth, 0).scale(-1.0, 1.0); + if (!transform.isIdentity()) { + oldTransform = p->transform(); + p->setWorldTransform(transform * oldTransform); + } + + if (d->fillMode >= Tile) + p->drawTiledPixmap(QRectF(0, 0, drawWidth, drawHeight), d->pix); + else + p->drawPixmap(QRectF(0, 0, drawWidth, drawHeight), d->pix, QRectF(0, 0, drawWidth, drawHeight)); + if (d->smooth) { p->setRenderHint(QPainter::Antialiasing, oldAA); p->setRenderHint(QPainter::SmoothPixmapTransform, oldSmooth); } + if (doClip) + p->restore(); + if (!transform.isIdentity()) + p->setWorldTransform(oldTransform); } void QDeclarativeImage::pixmapChange() diff --git a/src/declarative/graphicsitems/qdeclarativeimagebase.cpp b/src/declarative/graphicsitems/qdeclarativeimagebase.cpp index 74e9337..471c87f 100644 --- a/src/declarative/graphicsitems/qdeclarativeimagebase.cpp +++ b/src/declarative/graphicsitems/qdeclarativeimagebase.cpp @@ -48,8 +48,13 @@ QT_BEGIN_NAMESPACE +QDeclarativeImageBase::QDeclarativeImageBase(QDeclarativeItem *parent) + : QDeclarativeImplicitSizeItem(*(new QDeclarativeImageBasePrivate), parent) +{ +} + QDeclarativeImageBase::QDeclarativeImageBase(QDeclarativeImageBasePrivate &dd, QDeclarativeItem *parent) - : QDeclarativeItem(dd, parent) + : QDeclarativeImplicitSizeItem(dd, parent) { } @@ -128,6 +133,44 @@ QSize QDeclarativeImageBase::sourceSize() const return QSize(width != -1 ? width : implicitWidth(), height != -1 ? height : implicitHeight()); } +bool QDeclarativeImageBase::cache() const +{ + Q_D(const QDeclarativeImageBase); + return d->cache; +} + +void QDeclarativeImageBase::setCache(bool cache) +{ + Q_D(QDeclarativeImageBase); + if (d->cache == cache) + return; + + d->cache = cache; + emit cacheChanged(); + if (isComponentComplete()) + load(); +} + +void QDeclarativeImageBase::setMirror(bool mirror) +{ + Q_D(QDeclarativeImageBase); + if (mirror == d->mirror) + return; + + d->mirror = mirror; + + if (isComponentComplete()) + update(); + + emit mirrorChanged(); +} + +bool QDeclarativeImageBase::mirror() const +{ + Q_D(const QDeclarativeImageBase); + return d->mirror; +} + void QDeclarativeImageBase::load() { Q_D(QDeclarativeImageBase); @@ -143,7 +186,12 @@ void QDeclarativeImageBase::load() pixmapChange(); update(); } else { - d->pix.load(qmlEngine(this), d->url, d->explicitSourceSize ? sourceSize() : QSize(), d->async); + QDeclarativePixmap::Options options; + if (d->async) + options |= QDeclarativePixmap::Asynchronous; + if (d->cache) + options |= QDeclarativePixmap::Cache; + d->pix.load(qmlEngine(this), d->url, d->explicitSourceSize ? sourceSize() : QSize(), options); if (d->pix.isLoading()) { d->progress = 0.0; diff --git a/src/declarative/graphicsitems/qdeclarativeimagebase_p.h b/src/declarative/graphicsitems/qdeclarativeimagebase_p.h index 7135170..abee25d 100644 --- a/src/declarative/graphicsitems/qdeclarativeimagebase_p.h +++ b/src/declarative/graphicsitems/qdeclarativeimagebase_p.h @@ -42,14 +42,14 @@ #ifndef QDECLARATIVEIMAGEBASE_H #define QDECLARATIVEIMAGEBASE_H -#include "qdeclarativeitem.h" +#include "qdeclarativeimplicitsizeitem_p.h" QT_BEGIN_HEADER QT_BEGIN_NAMESPACE class QDeclarativeImageBasePrivate; -class Q_AUTOTEST_EXPORT QDeclarativeImageBase : public QDeclarativeItem +class Q_AUTOTEST_EXPORT QDeclarativeImageBase : public QDeclarativeImplicitSizeItem { Q_OBJECT Q_ENUMS(Status) @@ -58,9 +58,12 @@ class Q_AUTOTEST_EXPORT QDeclarativeImageBase : public QDeclarativeItem Q_PROPERTY(QUrl source READ source WRITE setSource NOTIFY sourceChanged) Q_PROPERTY(qreal progress READ progress NOTIFY progressChanged) Q_PROPERTY(bool asynchronous READ asynchronous WRITE setAsynchronous NOTIFY asynchronousChanged) + Q_PROPERTY(bool cache READ cache WRITE setCache NOTIFY cacheChanged REVISION 1) Q_PROPERTY(QSize sourceSize READ sourceSize WRITE setSourceSize NOTIFY sourceSizeChanged) + Q_PROPERTY(bool mirror READ mirror WRITE setMirror NOTIFY mirrorChanged REVISION 1) public: + QDeclarativeImageBase(QDeclarativeItem *parent=0); ~QDeclarativeImageBase(); enum Status { Null, Ready, Loading, Error }; Status status() const; @@ -72,15 +75,23 @@ public: bool asynchronous() const; void setAsynchronous(bool); + bool cache() const; + void setCache(bool); + virtual void setSourceSize(const QSize&); QSize sourceSize() const; + virtual void setMirror(bool mirror); + bool mirror() const; + Q_SIGNALS: void sourceChanged(const QUrl &); void sourceSizeChanged(); void statusChanged(QDeclarativeImageBase::Status); void progressChanged(qreal progress); void asynchronousChanged(); + Q_REVISION(1) void cacheChanged(); + Q_REVISION(1) void mirrorChanged(); protected: virtual void load(); diff --git a/src/declarative/graphicsitems/qdeclarativeimagebase_p_p.h b/src/declarative/graphicsitems/qdeclarativeimagebase_p_p.h index 5d892ad..ab16e66 100644 --- a/src/declarative/graphicsitems/qdeclarativeimagebase_p_p.h +++ b/src/declarative/graphicsitems/qdeclarativeimagebase_p_p.h @@ -53,7 +53,7 @@ // We mean it. // -#include "private/qdeclarativeitem_p.h" +#include "private/qdeclarativeimplicitsizeitem_p_p.h" #include "private/qdeclarativepixmapcache_p.h" #include <QtCore/QPointer> @@ -61,7 +61,7 @@ QT_BEGIN_NAMESPACE class QNetworkReply; -class QDeclarativeImageBasePrivate : public QDeclarativeItemPrivate +class QDeclarativeImageBasePrivate : public QDeclarativeImplicitSizeItemPrivate { Q_DECLARE_PUBLIC(QDeclarativeImageBase) @@ -70,7 +70,9 @@ public: : status(QDeclarativeImageBase::Null), progress(0.0), explicitSourceSize(false), - async(false) + async(false), + cache(true), + mirror(false) { QGraphicsItemPrivate::flags = QGraphicsItemPrivate::flags & ~QGraphicsItem::ItemHasNoContents; } @@ -82,6 +84,8 @@ public: QSize sourcesize; bool explicitSourceSize : 1; bool async : 1; + bool cache : 1; + bool mirror: 1; }; QT_END_NAMESPACE diff --git a/src/declarative/graphicsitems/qdeclarativeimplicitsizeitem.cpp b/src/declarative/graphicsitems/qdeclarativeimplicitsizeitem.cpp new file mode 100644 index 0000000..31ac28f --- /dev/null +++ b/src/declarative/graphicsitems/qdeclarativeimplicitsizeitem.cpp @@ -0,0 +1,92 @@ +/**************************************************************************** +** +** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the QtDeclarative module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the Technology Preview License Agreement accompanying +** this package. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain additional +** rights. These rights are described in the Nokia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** If you have questions regarding the use of this file, please contact +** Nokia at qt-info@nokia.com. +** +** +** +** +** +** +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "private/qdeclarativeimplicitsizeitem_p.h" +#include "private/qdeclarativeimplicitsizeitem_p_p.h" + +QT_BEGIN_NAMESPACE + +void QDeclarativeImplicitSizeItemPrivate::implicitWidthChanged() +{ + Q_Q(QDeclarativeImplicitSizeItem); + emit q->implicitWidthChanged(); +} + +void QDeclarativeImplicitSizeItemPrivate::implicitHeightChanged() +{ + Q_Q(QDeclarativeImplicitSizeItem); + emit q->implicitHeightChanged(); +} + +QDeclarativeImplicitSizeItem::QDeclarativeImplicitSizeItem(QDeclarativeItem *parent) + : QDeclarativeItem(*(new QDeclarativeImplicitSizeItemPrivate), parent) +{ +} + +QDeclarativeImplicitSizeItem::QDeclarativeImplicitSizeItem(QDeclarativeImplicitSizeItemPrivate &dd, QDeclarativeItem *parent) + : QDeclarativeItem(dd, parent) +{ +} + + +void QDeclarativeImplicitSizePaintedItemPrivate::implicitWidthChanged() +{ + Q_Q(QDeclarativeImplicitSizePaintedItem); + emit q->implicitWidthChanged(); +} + +void QDeclarativeImplicitSizePaintedItemPrivate::implicitHeightChanged() +{ + Q_Q(QDeclarativeImplicitSizePaintedItem); + emit q->implicitHeightChanged(); +} + +QDeclarativeImplicitSizePaintedItem::QDeclarativeImplicitSizePaintedItem(QDeclarativeItem *parent) + : QDeclarativePaintedItem(*(new QDeclarativeImplicitSizePaintedItemPrivate), parent) +{ +} + +QDeclarativeImplicitSizePaintedItem::QDeclarativeImplicitSizePaintedItem(QDeclarativeImplicitSizePaintedItemPrivate &dd, QDeclarativeItem *parent) + : QDeclarativePaintedItem(dd, parent) +{ +} + +QT_END_NAMESPACE diff --git a/src/declarative/graphicsitems/qdeclarativeimplicitsizeitem_p.h b/src/declarative/graphicsitems/qdeclarativeimplicitsizeitem_p.h new file mode 100644 index 0000000..1863d95 --- /dev/null +++ b/src/declarative/graphicsitems/qdeclarativeimplicitsizeitem_p.h @@ -0,0 +1,100 @@ +/**************************************************************************** +** +** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the QtDeclarative module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the Technology Preview License Agreement accompanying +** this package. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain additional +** rights. These rights are described in the Nokia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** If you have questions regarding the use of this file, please contact +** Nokia at qt-info@nokia.com. +** +** +** +** +** +** +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef QDECLARATIVEIMPLICITSIZEITEM_H +#define QDECLARATIVEIMPLICITSIZEITEM_H + +#include "qdeclarativepainteditem_p.h" + +QT_BEGIN_HEADER + +QT_BEGIN_NAMESPACE + +class QDeclarativeImplicitSizeItemPrivate; +class Q_AUTOTEST_EXPORT QDeclarativeImplicitSizeItem : public QDeclarativeItem +{ + Q_OBJECT + Q_PROPERTY(qreal implicitWidth READ implicitWidth NOTIFY implicitWidthChanged REVISION 1) + Q_PROPERTY(qreal implicitHeight READ implicitHeight NOTIFY implicitHeightChanged REVISION 1) + +public: + QDeclarativeImplicitSizeItem(QDeclarativeItem *parent = 0); + +protected: + QDeclarativeImplicitSizeItem(QDeclarativeImplicitSizeItemPrivate &dd, QDeclarativeItem *parent); + +Q_SIGNALS: + Q_REVISION(1) void implicitWidthChanged(); + Q_REVISION(1) void implicitHeightChanged(); + +private: + Q_DISABLE_COPY(QDeclarativeImplicitSizeItem) + Q_DECLARE_PRIVATE_D(QGraphicsItem::d_ptr.data(), QDeclarativeImplicitSizeItem) +}; + +class QDeclarativeImplicitSizePaintedItemPrivate; +class Q_AUTOTEST_EXPORT QDeclarativeImplicitSizePaintedItem : public QDeclarativePaintedItem +{ + Q_OBJECT + Q_PROPERTY(qreal implicitWidth READ implicitWidth NOTIFY implicitWidthChanged REVISION 1) + Q_PROPERTY(qreal implicitHeight READ implicitHeight NOTIFY implicitHeightChanged REVISION 1) + +public: + QDeclarativeImplicitSizePaintedItem(QDeclarativeItem *parent = 0); + +protected: + QDeclarativeImplicitSizePaintedItem(QDeclarativeImplicitSizePaintedItemPrivate &dd, QDeclarativeItem *parent); + virtual void drawContents(QPainter *, const QRect &) {}; + +Q_SIGNALS: + Q_REVISION(1) void implicitWidthChanged(); + Q_REVISION(1) void implicitHeightChanged(); + +private: + Q_DISABLE_COPY(QDeclarativeImplicitSizePaintedItem) + Q_DECLARE_PRIVATE_D(QGraphicsItem::d_ptr.data(), QDeclarativeImplicitSizePaintedItem) +}; + +QT_END_NAMESPACE + +QT_END_HEADER + +#endif // QDECLARATIVEIMPLICITSIZEITEM_H diff --git a/src/declarative/graphicsitems/qdeclarativeimplicitsizeitem_p_p.h b/src/declarative/graphicsitems/qdeclarativeimplicitsizeitem_p_p.h new file mode 100644 index 0000000..0bb7163 --- /dev/null +++ b/src/declarative/graphicsitems/qdeclarativeimplicitsizeitem_p_p.h @@ -0,0 +1,90 @@ +/**************************************************************************** +** +** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the QtDeclarative module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the Technology Preview License Agreement accompanying +** this package. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain additional +** rights. These rights are described in the Nokia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** If you have questions regarding the use of this file, please contact +** Nokia at qt-info@nokia.com. +** +** +** +** +** +** +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef QDECLARATIVEIMPLICITSIZEITEM_P_H +#define QDECLARATIVEIMPLICITSIZEITEM_P_H + +// +// W A R N I N G +// ------------- +// +// This file is not part of the Qt API. It exists purely as an +// implementation detail. This header file may change from version to +// version without notice, or even be removed. +// +// We mean it. +// + +#include "private/qdeclarativeitem_p.h" +#include "private/qdeclarativepainteditem_p_p.h" + +QT_BEGIN_NAMESPACE + +class QDeclarativeImplicitSizeItemPrivate : public QDeclarativeItemPrivate +{ + Q_DECLARE_PUBLIC(QDeclarativeImplicitSizeItem) + +public: + QDeclarativeImplicitSizeItemPrivate() + { + } + + virtual void implicitWidthChanged(); + virtual void implicitHeightChanged(); +}; + + +class QDeclarativeImplicitSizePaintedItemPrivate : public QDeclarativePaintedItemPrivate +{ + Q_DECLARE_PUBLIC(QDeclarativeImplicitSizePaintedItem) + +public: + QDeclarativeImplicitSizePaintedItemPrivate() + { + } + + virtual void implicitWidthChanged(); + virtual void implicitHeightChanged(); +}; + +QT_END_NAMESPACE + +#endif // QDECLARATIVEIMPLICITSIZEITEM_P_H diff --git a/src/declarative/graphicsitems/qdeclarativeitem.cpp b/src/declarative/graphicsitems/qdeclarativeitem.cpp index a99d918..ffef61b 100644 --- a/src/declarative/graphicsitems/qdeclarativeitem.cpp +++ b/src/declarative/graphicsitems/qdeclarativeitem.cpp @@ -444,6 +444,11 @@ void QDeclarativeItemKeyFilter::componentComplete() \c KeyNavigation.BeforeItem allows the event to be used for key navigation before the item, rather than after. + If item to which the focus is switching is not enabled or visible, an attempt will + be made to skip this item and focus on the next. This is possible if there are + a chain of items with the same KeyNavigation handler. If multiple items in a row are not enabled + or visible, they will also be skipped. + \sa {Keys}{Keys attached property} */ @@ -452,10 +457,12 @@ void QDeclarativeItemKeyFilter::componentComplete() \qmlproperty Item KeyNavigation::right \qmlproperty Item KeyNavigation::up \qmlproperty Item KeyNavigation::down + \qmlproperty Item KeyNavigation::tab + \qmlproperty Item KeyNavigation::backtab These properties hold the item to assign focus to - when the left, right, up or down cursor keys are - pressed. + when the left, right, up or down cursor keys, or the + tab key are pressed. */ /*! @@ -611,37 +618,37 @@ void QDeclarativeKeyNavigationAttached::keyPressed(QKeyEvent *event, bool post) switch(event->key()) { case Qt::Key_Left: if (d->left) { - d->left->setFocus(true); + setFocusNavigation(d->left, "left"); event->accept(); } break; case Qt::Key_Right: if (d->right) { - d->right->setFocus(true); + setFocusNavigation(d->right, "right"); event->accept(); } break; case Qt::Key_Up: if (d->up) { - d->up->setFocus(true); + setFocusNavigation(d->up, "up"); event->accept(); } break; case Qt::Key_Down: if (d->down) { - d->down->setFocus(true); + setFocusNavigation(d->down, "down"); event->accept(); } break; case Qt::Key_Tab: if (d->tab) { - d->tab->setFocus(true); + setFocusNavigation(d->tab, "tab"); event->accept(); } break; case Qt::Key_Backtab: if (d->backtab) { - d->backtab->setFocus(true); + setFocusNavigation(d->backtab, "backtab"); event->accept(); } break; @@ -700,6 +707,29 @@ void QDeclarativeKeyNavigationAttached::keyReleased(QKeyEvent *event, bool post) if (!event->isAccepted()) QDeclarativeItemKeyFilter::keyReleased(event, post); } +void QDeclarativeKeyNavigationAttached::setFocusNavigation(QDeclarativeItem *currentItem, const char *dir) +{ + QDeclarativeItem *initialItem = currentItem; + bool isNextItem = false; + do { + isNextItem = false; + if (currentItem->isVisible() && currentItem->isEnabled()) { + currentItem->setFocus(true); + } else { + QObject *attached = + qmlAttachedPropertiesObject<QDeclarativeKeyNavigationAttached>(currentItem, false); + if (attached) { + QDeclarativeItem *tempItem = qvariant_cast<QDeclarativeItem*>(attached->property(dir)); + if (tempItem) { + currentItem = tempItem; + isNextItem = true; + } + } + } + } + while (currentItem != initialItem && isNextItem); +} + /*! \qmlclass Keys QDeclarativeKeysAttached \ingroup qml-basic-interaction-elements @@ -1889,11 +1919,26 @@ void QDeclarativeItem::setClip(bool c) /*! \qmlproperty bool Item::visible - Whether the item is visible. By default this is true. + This property holds whether the item is visible. By default this is true. + + Setting this property directly affects the \c visible value of child + items. When set to \c false, the \c visible values of all child items also + become \c false. When set to \c true, the \c visible values of child items + are returned to \c true, unless they have explicitly been set to \c false. - \note visible is not linked to actual visibility; if an item - moves off screen, or the opacity changes to 0, this will - not affect the visible property. + (Because of this flow-on behavior, using the \c visible property may not + have the intended effect if a property binding should only respond to + explicit property changes. In such cases it may be better to use the + \l opacity property instead.) + + Setting this property to \c false automatically causes \l focus to be set + to \c false, and this item will longer receive mouse and keyboard events. + (In contrast, setting the \l opacity to 0 does not affect the \l focus + property and the receiving of key events.) + + \note This property's value is only affected by changes to this property or + the parent's \c visible property. It does not change, for example, if this + item moves off-screen, or if the \l opacity changes to 0. */ @@ -2277,13 +2322,15 @@ void QDeclarativeItem::setBaselineOffset(qreal offset) /*! \qmlproperty real Item::opacity - The opacity of the item. Opacity is specified as a number between 0 - (fully transparent) and 1 (fully opaque). The default is 1. + This property holds the opacity of the item. Opacity is specified as a + number between 0 (fully transparent) and 1 (fully opaque). The default is 1. - Opacity is an \e inherited attribute. That is, the opacity is - also applied individually to child items. In almost all cases this - is what you want, but in some cases (like the following example) - it may produce undesired results. + When this property is set, the specified opacity is also applied + individually to child items. In almost all cases this is what you want, + but in some cases it may produce undesired results. For example in the + second set of rectangles below, the red rectangle has specified an opacity + of 0.5, which affects the opacity of its blue child rectangle even though + the child has not specified an opacity. \table \row @@ -2318,6 +2365,12 @@ void QDeclarativeItem::setBaselineOffset(qreal offset) } \endqml \endtable + + If an item's opacity is set to 0, the item will no longer receive mouse + events, but will continue to receive key events and will retain the keyboard + \l focus if it has been set. (In contrast, setting the \l visible property + to \c false stops both mouse and keyboard events, and also removes focus + from the item.) */ /*! @@ -3056,13 +3109,24 @@ void QDeclarativeItemPrivate::resetWidth() q->setImplicitWidth(q->implicitWidth()); } +void QDeclarativeItemPrivate::implicitWidthChanged() +{ + Q_Q(QDeclarativeItem); + emit q->implicitWidthChanged(); +} + +qreal QDeclarativeItemPrivate::implicitWidth() const +{ + return mImplicitWidth; +} + /*! Returns the width of the item that is implied by other properties that determine the content. */ qreal QDeclarativeItem::implicitWidth() const { Q_D(const QDeclarativeItem); - return d->implicitWidth; + return d->implicitWidth(); } /*! @@ -3072,9 +3136,13 @@ qreal QDeclarativeItem::implicitWidth() const void QDeclarativeItem::setImplicitWidth(qreal w) { Q_D(QDeclarativeItem); - d->implicitWidth = w; - if (d->mWidth == w || widthValid()) + bool changed = w != d->mImplicitWidth; + d->mImplicitWidth = w; + if (d->mWidth == w || widthValid()) { + if (changed) + d->implicitWidthChanged(); return; + } qreal oldWidth = d->mWidth; @@ -3083,6 +3151,9 @@ void QDeclarativeItem::setImplicitWidth(qreal w) geometryChanged(QRectF(x(), y(), width(), height()), QRectF(x(), y(), oldWidth, height())); + + if (changed) + d->implicitWidthChanged(); } /*! @@ -3164,25 +3235,77 @@ void QDeclarativeItemPrivate::resetHeight() q->setImplicitHeight(q->implicitHeight()); } +void QDeclarativeItemPrivate::implicitHeightChanged() +{ + Q_Q(QDeclarativeItem); + emit q->implicitHeightChanged(); +} + +qreal QDeclarativeItemPrivate::implicitHeight() const +{ + return mImplicitHeight; +} + /*! Returns the height of the item that is implied by other properties that determine the content. */ qreal QDeclarativeItem::implicitHeight() const { Q_D(const QDeclarativeItem); - return d->implicitHeight; + return d->implicitHeight(); } /*! + \qmlproperty real Item::implicitWidth + \qmlproperty real Item::implicitHeight + \since Quick 1.1 + + Defines the natural width or height of the Item if no \l width or \l height is specified. + + The default implicit size for most items is 0x0, however some elements have an inherent + implicit size which cannot be overridden, e.g. Image, Text. + + Setting the implicit size is useful for defining components that have a preferred size + based on their content, for example: + + \code + // Label.qml + import QtQuick 1.1 + + Item { + property alias icon: image.source + property alias label: text.text + implicitWidth: text.implicitWidth + image.implicitWidth + implicitHeight: Math.max(text.implicitHeight, image.implicitHeight) + Image { id: image } + Text { + id: text + wrapMode: Text.Wrap + anchors.left: image.right; anchors.right: parent.right + anchors.verticalCenter: parent.verticalCenter + } + } + \endcode + + \bold Note: using implicitWidth of Text or TextEdit and setting the width explicitly + incurs a performance penalty as the text must be laid out twice. +*/ + + +/*! Sets the implied height of the item to \a h. This is the height implied by other properties that determine the content. */ void QDeclarativeItem::setImplicitHeight(qreal h) { Q_D(QDeclarativeItem); - d->implicitHeight = h; - if (d->mHeight == h || heightValid()) + bool changed = h != d->mImplicitHeight; + d->mImplicitHeight = h; + if (d->mHeight == h || heightValid()) { + if (changed) + d->implicitHeightChanged(); return; + } qreal oldHeight = d->mHeight; @@ -3191,6 +3314,9 @@ void QDeclarativeItem::setImplicitHeight(qreal h) geometryChanged(QRectF(x(), y(), width(), height()), QRectF(x(), y(), width(), oldHeight)); + + if (changed) + d->implicitHeightChanged(); } /*! diff --git a/src/declarative/graphicsitems/qdeclarativeitem.h b/src/declarative/graphicsitems/qdeclarativeitem.h index 0dd3937..839b934 100644 --- a/src/declarative/graphicsitems/qdeclarativeitem.h +++ b/src/declarative/graphicsitems/qdeclarativeitem.h @@ -92,6 +92,9 @@ class Q_DECLARATIVE_EXPORT QDeclarativeItem : public QGraphicsObject, public QDe Q_PROPERTY(TransformOrigin transformOrigin READ transformOrigin WRITE setTransformOrigin NOTIFY transformOriginChanged) Q_PROPERTY(QPointF transformOriginPoint READ transformOriginPoint) // transformOriginPoint is read-only for Item Q_PROPERTY(bool smooth READ smooth WRITE setSmooth NOTIFY smoothChanged) + Q_PROPERTY(qreal implicitWidth READ implicitWidth WRITE setImplicitWidth NOTIFY implicitWidthChanged REVISION 1) + Q_PROPERTY(qreal implicitHeight READ implicitHeight WRITE setImplicitHeight NOTIFY implicitHeightChanged REVISION 1) + Q_ENUMS(TransformOrigin) Q_CLASSINFO("DefaultProperty", "data") @@ -161,6 +164,8 @@ Q_SIGNALS: void transformOriginChanged(TransformOrigin); void smoothChanged(bool); void clipChanged(bool); + Q_REVISION(1) void implicitWidthChanged(); + Q_REVISION(1) void implicitHeightChanged(); protected: bool isComponentComplete() const; diff --git a/src/declarative/graphicsitems/qdeclarativeitem_p.h b/src/declarative/graphicsitems/qdeclarativeitem_p.h index 781dd6d..4303c0a 100644 --- a/src/declarative/graphicsitems/qdeclarativeitem_p.h +++ b/src/declarative/graphicsitems/qdeclarativeitem_p.h @@ -126,7 +126,7 @@ public: widthValid(false), heightValid(false), componentComplete(true), keepMouse(false), smooth(false), transformOriginDirty(true), doneEventPreHandler(false), keyHandler(0), - mWidth(0), mHeight(0), implicitWidth(0), implicitHeight(0), hadSubFocusItem(false) + mWidth(0), mHeight(0), mImplicitWidth(0), mImplicitHeight(0), hadSubFocusItem(false) { QGraphicsItemPrivate::acceptedMouseButtons = 0; isDeclarativeItem = 1; @@ -157,6 +157,11 @@ public: void setHeight(qreal); void resetHeight(); + virtual qreal implicitWidth() const; + virtual qreal implicitHeight() const; + virtual void implicitWidthChanged(); + virtual void implicitHeightChanged(); + QDeclarativeListProperty<QObject> data(); QDeclarativeListProperty<QObject> resources(); @@ -272,8 +277,8 @@ public: qreal mWidth; qreal mHeight; - qreal implicitWidth; - qreal implicitHeight; + qreal mImplicitWidth; + qreal mImplicitHeight; bool hadSubFocusItem; @@ -415,6 +420,7 @@ Q_SIGNALS: private: virtual void keyPressed(QKeyEvent *event, bool post); virtual void keyReleased(QKeyEvent *event, bool post); + void setFocusNavigation(QDeclarativeItem *currentItem, const char *dir); }; class QDeclarativeKeysAttachedPrivate : public QObjectPrivate diff --git a/src/declarative/graphicsitems/qdeclarativeitemsmodule.cpp b/src/declarative/graphicsitems/qdeclarativeitemsmodule.cpp index 52703c2..bc4a2d0 100644 --- a/src/declarative/graphicsitems/qdeclarativeitemsmodule.cpp +++ b/src/declarative/graphicsitems/qdeclarativeitemsmodule.cpp @@ -78,6 +78,7 @@ #include "private/qdeclarativewebview_p_p.h" #endif #include "private/qdeclarativeanchors_p.h" +#include "private/qdeclarativepincharea_p.h" static QDeclarativePrivate::AutoParentResult qgraphicsobject_autoParent(QObject *obj, QObject *parent) { @@ -174,6 +175,29 @@ void QDeclarativeItemModule::defineModule() qmlRegisterUncreatableType<QDeclarativeKeyNavigationAttached>("QtQuick",1,0,"KeyNavigation",QDeclarativeKeyNavigationAttached::tr("KeyNavigation is only available via attached properties")); qmlRegisterUncreatableType<QDeclarativeKeysAttached>("QtQuick",1,0,"Keys",QDeclarativeKeysAttached::tr("Keys is only available via attached properties")); + // QtQuick 1.1 items + qmlRegisterType<QDeclarativePinchArea>("QtQuick",1,1,"PinchArea"); + qmlRegisterType<QDeclarativePinch>("QtQuick",1,1,"Pinch"); + qmlRegisterType<QDeclarativePinchEvent>(); + qmlRegisterType<QDeclarativeItem,1>("QtQuick",1,1,"Item"); + qmlRegisterType<QDeclarativeFlickable,1>("QtQuick",1,1,"Flickable"); + qmlRegisterType<QDeclarativeListView,1>("QtQuick",1,1,"ListView"); + qmlRegisterType<QDeclarativeGridView,1>("QtQuick",1,1,"GridView"); + qmlRegisterType<QDeclarativeRow,1>("QtQuick",1,1,"Row"); + qmlRegisterType<QDeclarativeGrid,1>("QtQuick",1,1,"Grid"); + qmlRegisterType<QDeclarativeFlow,1>("QtQuick",1,1,"Flow"); + qmlRegisterType<QDeclarativeRepeater,1>("QtQuick",1,1,"Repeater"); + qmlRegisterType<QDeclarativeText,1>("QtQuick",1,1,"Text"); + qmlRegisterType<QDeclarativeTextEdit,1>("QtQuick",1,1,"TextEdit"); +#ifndef QT_NO_LINEEDIT + qmlRegisterType<QDeclarativeTextInput,1>("QtQuick",1,1,"TextInput"); +#endif + qmlRegisterRevision<QDeclarativeImageBase,1>("QtQuick",1,1); + qmlRegisterRevision<QDeclarativeImplicitSizeItem,0>("QtQuick",1,0); + qmlRegisterRevision<QDeclarativeImplicitSizeItem,1>("QtQuick",1,1); + qmlRegisterRevision<QDeclarativeImplicitSizePaintedItem,0>("QtQuick",1,0); + qmlRegisterRevision<QDeclarativeImplicitSizePaintedItem,1>("QtQuick",1,1); + #ifndef QT_NO_IMPORT_QT47_QML #ifdef QT_NO_MOVIE qmlRegisterTypeNotAvailable("Qt",4,7,"AnimatedImage", diff --git a/src/declarative/graphicsitems/qdeclarativelistview.cpp b/src/declarative/graphicsitems/qdeclarativelistview.cpp index b4b3fa7..075c3af 100644 --- a/src/declarative/graphicsitems/qdeclarativelistview.cpp +++ b/src/declarative/graphicsitems/qdeclarativelistview.cpp @@ -451,6 +451,7 @@ public: void updateHeader(); void updateFooter(); void fixupPosition(); + void positionViewAtIndex(int index, int mode); virtual void fixup(AxisData &data, qreal minExtent, qreal maxExtent); virtual void flick(QDeclarativeFlickablePrivate::AxisData &data, qreal minExtent, qreal maxExtent, qreal vSize, QDeclarativeTimeLineCallback::Callback fixupCallback, qreal velocity); @@ -2231,6 +2232,9 @@ void QDeclarativeListView::setFooter(QDeclarativeComponent *footer) Q_D(QDeclarativeListView); if (d->footerComponent != footer) { if (d->footer) { + if (scene()) + scene()->removeItem(d->footer->item); + d->footer->item->deleteLater(); delete d->footer; d->footer = 0; } @@ -2240,6 +2244,7 @@ void QDeclarativeListView::setFooter(QDeclarativeComponent *footer) if (isComponentComplete()) { d->updateFooter(); d->updateViewport(); + d->fixupPosition(); } emit footerChanged(); } @@ -2265,6 +2270,9 @@ void QDeclarativeListView::setHeader(QDeclarativeComponent *header) Q_D(QDeclarativeListView); if (d->headerComponent != header) { if (d->header) { + if (scene()) + scene()->removeItem(d->header->item); + d->header->item->deleteLater(); delete d->header; d->header = 0; } @@ -2275,6 +2283,7 @@ void QDeclarativeListView::setHeader(QDeclarativeComponent *header) d->updateHeader(); d->updateFooter(); d->updateViewport(); + d->fixupPosition(); } emit headerChanged(); } @@ -2558,6 +2567,78 @@ void QDeclarativeListView::decrementCurrentIndex() } } +void QDeclarativeListViewPrivate::positionViewAtIndex(int index, int mode) +{ + Q_Q(QDeclarativeListView); + if (!isValid()) + return; + if (mode < QDeclarativeListView::Beginning || mode > QDeclarativeListView::Contain) + return; + int idx = qMax(qMin(index, model->count()-1), 0); + + if (layoutScheduled) + layout(); + qreal pos = position(); + FxListItem *item = visibleItem(idx); + qreal maxExtent = orient == QDeclarativeListView::Vertical ? -q->maxYExtent() : -q->maxXExtent(); + if (!item) { + int itemPos = positionAt(idx); + // save the currently visible items in case any of them end up visible again + QList<FxListItem*> oldVisible = visibleItems; + visibleItems.clear(); + visiblePos = itemPos; + visibleIndex = idx; + setPosition(qMin(qreal(itemPos), maxExtent)); + // now release the reference to all the old visible items. + for (int i = 0; i < oldVisible.count(); ++i) + releaseItem(oldVisible.at(i)); + item = visibleItem(idx); + } + if (item) { + const qreal itemPos = item->position(); + switch (mode) { + case QDeclarativeListView::Beginning: + pos = itemPos; + if (index < 0 && header) + pos -= header->size(); + break; + case QDeclarativeListView::Center: + pos = itemPos - (size() - item->size())/2; + break; + case QDeclarativeListView::End: + pos = itemPos - size() + item->size(); + if (index >= model->count() && footer) + pos += footer->size(); + break; + case QDeclarativeListView::Visible: + if (itemPos > pos + size()) + pos = itemPos - size() + item->size(); + else if (item->endPosition() < pos) + pos = itemPos; + break; + case QDeclarativeListView::Contain: + if (item->endPosition() > pos + size()) + pos = itemPos - size() + item->size(); + if (itemPos < pos) + pos = itemPos; + } + pos = qMin(pos, maxExtent); + qreal minExtent = orient == QDeclarativeListView::Vertical ? -q->minYExtent() : -q->minXExtent(); + pos = qMax(pos, minExtent); + moveReason = QDeclarativeListViewPrivate::Other; + q->cancelFlick(); + setPosition(pos); + if (highlight) { + if (autoHighlight) { + highlight->setPosition(currentItem->itemPosition()); + highlight->setSize(currentItem->itemSize()); + } + updateHighlight(); + } + } + fixupPosition(); +} + /*! \qmlmethod ListView::positionViewAtIndex(int index, PositionMode mode) @@ -2596,66 +2677,42 @@ void QDeclarativeListView::positionViewAtIndex(int index, int mode) Q_D(QDeclarativeListView); if (!d->isValid() || index < 0 || index >= d->model->count()) return; - if (mode < Beginning || mode > Contain) + d->positionViewAtIndex(index, mode); +} + +/*! + \qmlmethod ListView::positionViewAtBeginning() + \qmlmethod ListView::positionViewAtEnd() + + Positions the view at the beginning or end, taking into account any header or footer. + + It is not recommended to use \l {Flickable::}{contentX} or \l {Flickable::}{contentY} to position the view + at a particular index. This is unreliable since removing items from the start + of the list does not cause all other items to be repositioned, and because + the actual start of the view can vary based on the size of the delegates. + + \bold Note: methods should only be called after the Component has completed. To position + the view at startup, this method should be called by Component.onCompleted. For + example, to position the view at the end on startup: + + \code + Component.onCompleted: positionViewAtEnd() + \endcode +*/ +void QDeclarativeListView::positionViewAtBeginning() +{ + Q_D(QDeclarativeListView); + if (!d->isValid()) return; + d->positionViewAtIndex(-1, Beginning); +} - if (d->layoutScheduled) - d->layout(); - qreal pos = d->position(); - FxListItem *item = d->visibleItem(index); - qreal maxExtent = d->orient == QDeclarativeListView::Vertical ? -maxYExtent() : -maxXExtent(); - if (!item) { - int itemPos = d->positionAt(index); - // save the currently visible items in case any of them end up visible again - QList<FxListItem*> oldVisible = d->visibleItems; - d->visibleItems.clear(); - d->visiblePos = itemPos; - d->visibleIndex = index; - d->setPosition(qMin(qreal(itemPos), maxExtent)); - // now release the reference to all the old visible items. - for (int i = 0; i < oldVisible.count(); ++i) - d->releaseItem(oldVisible.at(i)); - item = d->visibleItem(index); - } - if (item) { - const qreal itemPos = item->position(); - switch (mode) { - case Beginning: - pos = itemPos; - break; - case Center: - pos = itemPos - (d->size() - item->size())/2; - break; - case End: - pos = itemPos - d->size() + item->size(); - break; - case Visible: - if (itemPos > pos + d->size()) - pos = itemPos - d->size() + item->size(); - else if (item->endPosition() < pos) - pos = itemPos; - break; - case Contain: - if (item->endPosition() > pos + d->size()) - pos = itemPos - d->size() + item->size(); - if (itemPos < pos) - pos = itemPos; - } - pos = qMin(pos, maxExtent); - qreal minExtent = d->orient == QDeclarativeListView::Vertical ? -minYExtent() : -minXExtent(); - pos = qMax(pos, minExtent); - d->moveReason = QDeclarativeListViewPrivate::Other; - cancelFlick(); - d->setPosition(pos); - if (d->highlight) { - if (d->autoHighlight) { - d->highlight->setPosition(d->currentItem->itemPosition()); - d->highlight->setSize(d->currentItem->itemSize()); - } - d->updateHighlight(); - } - } - d->fixupPosition(); +void QDeclarativeListView::positionViewAtEnd() +{ + Q_D(QDeclarativeListView); + if (!d->isValid()) + return; + d->positionViewAtIndex(d->model->count(), End); } /*! diff --git a/src/declarative/graphicsitems/qdeclarativelistview_p.h b/src/declarative/graphicsitems/qdeclarativelistview_p.h index 7ffcb43..10fbf10 100644 --- a/src/declarative/graphicsitems/qdeclarativelistview_p.h +++ b/src/declarative/graphicsitems/qdeclarativelistview_p.h @@ -209,6 +209,8 @@ public: Q_INVOKABLE void positionViewAtIndex(int index, int mode); Q_INVOKABLE int indexAt(int x, int y) const; + Q_INVOKABLE Q_REVISION(1) void positionViewAtBeginning(); + Q_INVOKABLE Q_REVISION(1) void positionViewAtEnd(); public Q_SLOTS: void incrementCurrentIndex(); diff --git a/src/declarative/graphicsitems/qdeclarativeloader.cpp b/src/declarative/graphicsitems/qdeclarativeloader.cpp index ded2be3..6c1f1be 100644 --- a/src/declarative/graphicsitems/qdeclarativeloader.cpp +++ b/src/declarative/graphicsitems/qdeclarativeloader.cpp @@ -48,7 +48,8 @@ QT_BEGIN_NAMESPACE QDeclarativeLoaderPrivate::QDeclarativeLoaderPrivate() - : item(0), component(0), ownComponent(false) + : item(0), component(0), ownComponent(false), updatingSize(false), + itemWidthValid(false), itemHeightValid(false) { } @@ -58,8 +59,13 @@ QDeclarativeLoaderPrivate::~QDeclarativeLoaderPrivate() void QDeclarativeLoaderPrivate::itemGeometryChanged(QDeclarativeItem *resizeItem, const QRectF &newGeometry, const QRectF &oldGeometry) { - if (resizeItem == item) + if (resizeItem == item) { + if (!updatingSize && newGeometry.width() != oldGeometry.width()) + itemWidthValid = true; + if (!updatingSize && newGeometry.height() != oldGeometry.height()) + itemHeightValid = true; _q_updateSize(false); + } QDeclarativeItemChangeListener::itemGeometryChanged(resizeItem, newGeometry, oldGeometry); } @@ -99,6 +105,10 @@ void QDeclarativeLoaderPrivate::initResize() QDeclarativeItemPrivate *p = static_cast<QDeclarativeItemPrivate *>(QGraphicsItemPrivate::get(qmlItem)); p->addItemChangeListener(this, QDeclarativeItemPrivate::Geometry); + // We may override the item's size, so we need to remember + // whether the item provided its own valid size. + itemWidthValid = p->widthValid; + itemHeightValid = p->heightValid; } else if (item && item->isWidget()) { QGraphicsWidget *widget = static_cast<QGraphicsWidget*>(item); widget->installEventFilter(q); @@ -216,7 +226,7 @@ void QDeclarativeLoaderPrivate::initResize() */ QDeclarativeLoader::QDeclarativeLoader(QDeclarativeItem *parent) - : QDeclarativeItem(*(new QDeclarativeLoaderPrivate), parent) + : QDeclarativeImplicitSizeItem(*(new QDeclarativeLoaderPrivate), parent) { Q_D(QDeclarativeLoader); d->flags |= QGraphicsItem::ItemIsFocusScope; @@ -262,6 +272,7 @@ void QDeclarativeLoader::setSource(const QUrl &url) d->clear(); d->source = url; + if (d->source.isEmpty()) { emit sourceChanged(); emit statusChanged(); @@ -272,18 +283,9 @@ void QDeclarativeLoader::setSource(const QUrl &url) d->component = new QDeclarativeComponent(qmlEngine(this), d->source, this); d->ownComponent = true; - if (!d->component->isLoading()) { - d->_q_sourceLoaded(); - } else { - connect(d->component, SIGNAL(statusChanged(QDeclarativeComponent::Status)), - this, SLOT(_q_sourceLoaded())); - connect(d->component, SIGNAL(progressChanged(qreal)), - this, SIGNAL(progressChanged())); - emit statusChanged(); - emit progressChanged(); - emit sourceChanged(); - emit itemChanged(); - } + + if (isComponentComplete()) + d->load(); } /*! @@ -324,6 +326,7 @@ void QDeclarativeLoader::setSourceComponent(QDeclarativeComponent *comp) d->component = comp; d->ownComponent = false; + if (!d->component) { emit sourceChanged(); emit statusChanged(); @@ -332,18 +335,8 @@ void QDeclarativeLoader::setSourceComponent(QDeclarativeComponent *comp) return; } - if (!d->component->isLoading()) { - d->_q_sourceLoaded(); - } else { - connect(d->component, SIGNAL(statusChanged(QDeclarativeComponent::Status)), - this, SLOT(_q_sourceLoaded())); - connect(d->component, SIGNAL(progressChanged(qreal)), - this, SIGNAL(progressChanged())); - emit progressChanged(); - emit sourceChanged(); - emit statusChanged(); - emit itemChanged(); - } + if (isComponentComplete()) + d->load(); } void QDeclarativeLoader::resetSourceComponent() @@ -351,6 +344,27 @@ void QDeclarativeLoader::resetSourceComponent() setSourceComponent(0); } +void QDeclarativeLoaderPrivate::load() +{ + Q_Q(QDeclarativeLoader); + + if (!q->isComponentComplete() || !component) + return; + + if (!component->isLoading()) { + _q_sourceLoaded(); + } else { + QObject::connect(component, SIGNAL(statusChanged(QDeclarativeComponent::Status)), + q, SLOT(_q_sourceLoaded())); + QObject::connect(component, SIGNAL(progressChanged(qreal)), + q, SIGNAL(progressChanged())); + emit q->statusChanged(); + emit q->progressChanged(); + emit q->sourceChanged(); + emit q->itemChanged(); + } +} + void QDeclarativeLoaderPrivate::_q_sourceLoaded() { Q_Q(QDeclarativeLoader); @@ -465,9 +479,10 @@ QDeclarativeLoader::Status QDeclarativeLoader::status() const void QDeclarativeLoader::componentComplete() { + Q_D(QDeclarativeLoader); + QDeclarativeItem::componentComplete(); - if (status() == Ready) - emit loaded(); + d->load(); } @@ -504,13 +519,21 @@ qreal QDeclarativeLoader::progress() const void QDeclarativeLoaderPrivate::_q_updateSize(bool loaderGeometryChanged) { Q_Q(QDeclarativeLoader); - if (!item) + if (!item || updatingSize) return; + + updatingSize = true; if (QDeclarativeItem *qmlItem = qobject_cast<QDeclarativeItem*>(item)) { - q->setImplicitWidth(qmlItem->width()); + if (!itemWidthValid) + q->setImplicitWidth(qmlItem->implicitWidth()); + else + q->setImplicitWidth(qmlItem->width()); if (loaderGeometryChanged && q->widthValid()) qmlItem->setWidth(q->width()); - q->setImplicitHeight(qmlItem->height()); + if (!itemHeightValid) + q->setImplicitHeight(qmlItem->implicitHeight()); + else + q->setImplicitHeight(qmlItem->height()); if (loaderGeometryChanged && q->heightValid()) qmlItem->setHeight(q->height()); } else if (item && item->isWidget()) { @@ -525,6 +548,7 @@ void QDeclarativeLoaderPrivate::_q_updateSize(bool loaderGeometryChanged) if (widget->size() != widgetSize) widget->resize(widgetSize); } + updatingSize = false; } /*! diff --git a/src/declarative/graphicsitems/qdeclarativeloader_p.h b/src/declarative/graphicsitems/qdeclarativeloader_p.h index 1fe7ae7..96b883c 100644 --- a/src/declarative/graphicsitems/qdeclarativeloader_p.h +++ b/src/declarative/graphicsitems/qdeclarativeloader_p.h @@ -42,7 +42,7 @@ #ifndef QDECLARATIVELOADER_H #define QDECLARATIVELOADER_H -#include "qdeclarativeitem.h" +#include "qdeclarativeimplicitsizeitem_p.h" QT_BEGIN_HEADER @@ -51,7 +51,7 @@ QT_BEGIN_NAMESPACE QT_MODULE(Declarative) class QDeclarativeLoaderPrivate; -class Q_AUTOTEST_EXPORT QDeclarativeLoader : public QDeclarativeItem +class Q_AUTOTEST_EXPORT QDeclarativeLoader : public QDeclarativeImplicitSizeItem { Q_OBJECT Q_ENUMS(Status) diff --git a/src/declarative/graphicsitems/qdeclarativeloader_p_p.h b/src/declarative/graphicsitems/qdeclarativeloader_p_p.h index 45ab595..4593dfc 100644 --- a/src/declarative/graphicsitems/qdeclarativeloader_p_p.h +++ b/src/declarative/graphicsitems/qdeclarativeloader_p_p.h @@ -55,13 +55,13 @@ #include "private/qdeclarativeloader_p.h" -#include "private/qdeclarativeitem_p.h" +#include "private/qdeclarativeimplicitsizeitem_p_p.h" #include "private/qdeclarativeitemchangelistener_p.h" QT_BEGIN_NAMESPACE class QDeclarativeContext; -class QDeclarativeLoaderPrivate : public QDeclarativeItemPrivate, public QDeclarativeItemChangeListener +class QDeclarativeLoaderPrivate : public QDeclarativeImplicitSizeItemPrivate, public QDeclarativeItemChangeListener { Q_DECLARE_PUBLIC(QDeclarativeLoader) @@ -72,11 +72,15 @@ public: void itemGeometryChanged(QDeclarativeItem *item, const QRectF &newGeometry, const QRectF &oldGeometry); void clear(); void initResize(); + void load(); QUrl source; QGraphicsObject *item; QDeclarativeComponent *component; bool ownComponent : 1; + bool updatingSize: 1; + bool itemWidthValid : 1; + bool itemHeightValid : 1; void _q_sourceLoaded(); void _q_updateSize(bool loaderGeometryChanged = true); diff --git a/src/declarative/graphicsitems/qdeclarativemousearea.cpp b/src/declarative/graphicsitems/qdeclarativemousearea.cpp index f5186df..ffbae3f 100644 --- a/src/declarative/graphicsitems/qdeclarativemousearea.cpp +++ b/src/declarative/graphicsitems/qdeclarativemousearea.cpp @@ -583,18 +583,22 @@ void QDeclarativeMouseArea::hoverEnterEvent(QGraphicsSceneHoverEvent *event) Q_D(QDeclarativeMouseArea); if (!d->absorb) QDeclarativeItem::hoverEnterEvent(event); - else + else { + d->lastPos = event->pos(); setHovered(true); + QDeclarativeMouseEvent me(d->lastPos.x(), d->lastPos.y(), Qt::NoButton, Qt::NoButton, event->modifiers(), false, false); + emit mousePositionChanged(&me); + } } void QDeclarativeMouseArea::hoverMoveEvent(QGraphicsSceneHoverEvent *event) { Q_D(QDeclarativeMouseArea); if (!d->absorb) { - QDeclarativeItem::hoverEnterEvent(event); + QDeclarativeItem::hoverMoveEvent(event); } else { d->lastPos = event->pos(); - QDeclarativeMouseEvent me(d->lastPos.x(), d->lastPos.y(), Qt::NoButton, d->lastButtons, d->lastModifiers, false, d->longPress); + QDeclarativeMouseEvent me(d->lastPos.x(), d->lastPos.y(), Qt::NoButton, Qt::NoButton, event->modifiers(), false, false); emit mousePositionChanged(&me); me.setX(d->lastPos.x()); me.setY(d->lastPos.y()); @@ -859,15 +863,16 @@ bool QDeclarativeMouseArea::setPressed(bool p) me.setX(d->lastPos.x()); me.setY(d->lastPos.y()); emit mousePositionChanged(&me); + emit pressedChanged(); } else { emit released(&me); me.setX(d->lastPos.x()); me.setY(d->lastPos.y()); + emit pressedChanged(); if (isclick && !d->longPress && !d->doubleClick) emit clicked(&me); } - emit pressedChanged(); return me.isAccepted(); } return false; diff --git a/src/declarative/graphicsitems/qdeclarativepincharea.cpp b/src/declarative/graphicsitems/qdeclarativepincharea.cpp new file mode 100644 index 0000000..eae83f6 --- /dev/null +++ b/src/declarative/graphicsitems/qdeclarativepincharea.cpp @@ -0,0 +1,582 @@ +/**************************************************************************** +** +** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the QtDeclarative module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the Technology Preview License Agreement accompanying +** this package. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain additional +** rights. These rights are described in the Nokia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** If you have questions regarding the use of this file, please contact +** Nokia at qt-info@nokia.com. +** +** +** +** +** +** +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "qdeclarativepincharea_p.h" +#include "qdeclarativepincharea_p_p.h" + +#include <QApplication> +#include <QGraphicsScene> + +#include <float.h> +#include <math.h> + +QT_BEGIN_NAMESPACE + + +/*! + \qmlclass PinchEvent QDeclarativePinchEvent + \ingroup qml-event-elements + \brief The PinchEvent object provides information about a pinch event. + + \bold {The PinchEvent element was added in QtQuick 1.1} + + The \c center, \c startCenter, \c previousCenter properties provide the center position between the two touch points. + + The \c scale and \c previousScale properties provide the scale factor. + + The \c angle, \c previousAngle and \c rotation properties provide the angle between the two points and the amount of rotation. + + The \c point1, \c point2, \c startPoint1, \c startPoint2 properties provide the positions of the touch points. + + The \c accepted property may be set to false in the \c onPinchStarted handler if the gesture should not + be handled. + + \sa PinchArea +*/ + +/*! + \qmlproperty QPointF PinchEvent::center + \qmlproperty QPointF PinchEvent::startCenter + \qmlproperty QPointF PinchEvent::previousCenter + + These properties hold the position of the center point between the two touch points. + + \list + \o \c center is the current center point + \o \c previousCenter is the center point of the previous event. + \o \c startCenter is the center point when the gesture began + \endlist +*/ + +/*! + \qmlproperty real PinchEvent::scale + \qmlproperty real PinchEvent::previousScale + + These properties hold the scale factor determined by the change in distance between the two touch points. + + \list + \o \c scale is the current scale factor. + \o \c previousScale is the scale factor of the previous event. + \endlist + + When a pinch gesture is started, the scale is 1.0. +*/ + +/*! + \qmlproperty real PinchEvent::angle + \qmlproperty real PinchEvent::previousAngle + \qmlproperty real PinchEvent::rotation + + These properties hold the angle between the two touch points. + + \list + \o \c angle is the current angle between the two points in the range -180 to 180. + \o \c previousAngle is the angle of the previous event. + \o \c rotation is the total rotation since the pinch gesture started. + \endlist + + When a pinch gesture is started, the rotation is 0.0. +*/ + +/*! + \qmlproperty QPointF PinchEvent::point1 + \qmlproperty QPointF PinchEvent::startPoint1 + \qmlproperty QPointF PinchEvent::point2 + \qmlproperty QPointF PinchEvent::startPoint2 + + These properties provide the actual touch points generating the pinch. + + \list + \o \c point1 and \c point2 hold the current positions of the points. + \o \c startPoint1 and \c startPoint2 hold the positions of the points when the second point was touched. + \endlist +*/ + +/*! + \qmlproperty bool PinchEvent::accepted + + Setting this property to false in the \c PinchArea::onPinchStarted handler + will result in no further pinch events being generated, and the gesture + ignored. +*/ + +QDeclarativePinch::QDeclarativePinch() + : m_target(0), m_minScale(1.0), m_maxScale(1.0) + , m_minRotation(0.0), m_maxRotation(0.0) + , m_axis(NoDrag), m_xmin(-FLT_MAX), m_xmax(FLT_MAX) + , m_ymin(-FLT_MAX), m_ymax(FLT_MAX), m_active(false) +{ +} + +QDeclarativePinchAreaPrivate::~QDeclarativePinchAreaPrivate() +{ + delete pinch; +} + +/*! + \qmlclass PinchArea QDeclarativePinchArea + \brief The PinchArea item enables simple pinch gesture handling. + \inherits Item + + \bold {The PinchArea element was added in QtQuick 1.1} + + A PinchArea is an invisible item that is typically used in conjunction with + a visible item in order to provide pinch gesture handling for that item. + + The \l enabled property is used to enable and disable pinch handling for + the proxied item. When disabled, the pinch area becomes transparent to + mouse/touch events. + + PinchArea can be used in two ways: + + \list + \o setting a \c pinch.target to provide automatic interaction with an element + \o using the onPinchStarted, onPinchUpdated and onPinchFinished handlers + \endlist + + \sa PinchEvent +*/ + +/*! + \qmlsignal PinchArea::onPinchStarted() + + This handler is called when the pinch area detects that a pinch gesture has started. + + The \l {PinchEvent}{pinch} parameter provides information about the pinch gesture, + including the scale, center and angle of the pinch. + + To ignore this gesture set the \c pinch.accepted property to false. The gesture + will be cancelled and no further events will be sent. +*/ + +/*! + \qmlsignal PinchArea::onPinchUpdated() + + This handler is called when the pinch area detects that a pinch gesture has changed. + + The \l {PinchEvent}{pinch} parameter provides information about the pinch gesture, + including the scale, center and angle of the pinch. +*/ + +/*! + \qmlsignal PinchArea::onPinchFinished() + + This handler is called when the pinch area detects that a pinch gesture has finished. + + The \l {PinchEvent}{pinch} parameter provides information about the pinch gesture, + including the scale, center and angle of the pinch. +*/ + + +/*! + \qmlproperty Item PinchArea::pinch.target + \qmlproperty bool PinchArea::pinch.active + \qmlproperty real PinchArea::pinch.minimumScale + \qmlproperty real PinchArea::pinch.maximumScale + \qmlproperty real PinchArea::pinch.minimumRotation + \qmlproperty real PinchArea::pinch.maximumRotation + \qmlproperty enumeration PinchArea::pinch.dragAxis + \qmlproperty real PinchArea::pinch.minimumX + \qmlproperty real PinchArea::pinch.maximumX + \qmlproperty real PinchArea::pinch.minimumY + \qmlproperty real PinchArea::pinch.maximumY + + \c pinch provides a convenient way to make an item react to pinch gestures. + + \list + \i \c pinch.target specifies the id of the item to drag. + \i \c pinch.active specifies if the target item is currently being dragged. + \i \c pinch.minimumScale and \c pinch.maximumScale limit the range of the Item::scale property. + \i \c pinch.minimumRotation and \c pinch.maximumRotation limit the range of the Item::rotation property. + \i \c pinch.dragAxis specifies whether dragging in not allowed (\c Pinch.NoDrag), can be done horizontally (\c Pinch.XAxis), vertically (\c Pinch.YAxis), or both (\c Pinch.XandYAxis) + \i \c pinch.minimum and \c pinch.maximum limit how far the target can be dragged along the corresponding axes. + \endlist +*/ + +QDeclarativePinchArea::QDeclarativePinchArea(QDeclarativeItem *parent) + : QDeclarativeItem(*(new QDeclarativePinchAreaPrivate), parent) +{ + Q_D(QDeclarativePinchArea); + d->init(); +} + +QDeclarativePinchArea::~QDeclarativePinchArea() +{ +} + +/*! + \qmlproperty bool PinchArea::enabled + This property holds whether the item accepts pinch gestures. + + This property defaults to true. +*/ +bool QDeclarativePinchArea::isEnabled() const +{ + Q_D(const QDeclarativePinchArea); + return d->absorb; +} + +void QDeclarativePinchArea::setEnabled(bool a) +{ + Q_D(QDeclarativePinchArea); + if (a != d->absorb) { + d->absorb = a; + emit enabledChanged(); + } +} + +bool QDeclarativePinchArea::event(QEvent *event) +{ + Q_D(QDeclarativePinchArea); + if (!d->absorb || !isVisible()) + return QDeclarativeItem::event(event); + switch (event->type()) { + case QEvent::TouchBegin: + case QEvent::TouchUpdate: { + QTouchEvent *touch = static_cast<QTouchEvent*>(event); + d->touchPoints.clear(); + for (int i = 0; i < touch->touchPoints().count(); ++i) { + if (!(touch->touchPoints().at(i).state() & Qt::TouchPointReleased)) { + d->touchPoints << touch->touchPoints().at(i); + } + } + updatePinch(); + } + return true; + case QEvent::TouchEnd: + d->touchPoints.clear(); + updatePinch(); + break; + default: + return QDeclarativeItem::event(event); + } + + return QDeclarativeItem::event(event); +} + +void QDeclarativePinchArea::updatePinch() +{ + Q_D(QDeclarativePinchArea); + if (d->touchPoints.count() != 2) { + if (d->inPinch) { + d->stealMouse = false; + setKeepMouseGrab(false); + d->inPinch = false; + const qreal rotationAngle = d->pinchStartAngle - d->pinchLastAngle; + QPointF pinchCenter = mapFromScene(d->sceneLastCenter); + QDeclarativePinchEvent pe(pinchCenter, d->pinchLastScale, d->pinchLastAngle, rotationAngle); + pe.setStartCenter(d->pinchStartCenter); + pe.setPreviousCenter(pinchCenter); + pe.setPreviousAngle(d->pinchLastAngle); + pe.setPreviousScale(d->pinchLastScale); + pe.setStartPoint1(mapFromScene(d->sceneStartPoint1)); + pe.setStartPoint2(mapFromScene(d->sceneStartPoint2)); + pe.setPoint1(d->lastPoint1); + pe.setPoint2(d->lastPoint2); + emit pinchFinished(&pe); + d->pinchStartDist = 0; + if (d->pinch && d->pinch->target()) + d->pinch->setActive(false); + } + return; + } + if (d->touchPoints.at(0).state() & Qt::TouchPointPressed + || d->touchPoints.at(1).state() & Qt::TouchPointPressed) { + d->sceneStartPoint1 = d->touchPoints.at(0).scenePos(); + d->sceneStartPoint2 = d->touchPoints.at(1).scenePos(); + d->inPinch = false; + d->pinchRejected = false; + } else if (!d->pinchRejected){ + QDeclarativeItem *grabber = scene() ? qobject_cast<QDeclarativeItem*>(scene()->mouseGrabberItem()) : 0; + if (grabber == this || !grabber || !grabber->keepMouseGrab()) { + const int dragThreshold = QApplication::startDragDistance(); + QPointF p1 = d->touchPoints.at(0).scenePos(); + QPointF p2 = d->touchPoints.at(1).scenePos(); + qreal dx = p1.x() - p2.x(); + qreal dy = p1.y() - p2.y(); + qreal dist = sqrt(dx*dx + dy*dy); + QPointF sceneCenter = (p1 + p2)/2; + qreal angle = QLineF(p1, p2).angle(); + if (angle > 180) + angle -= 360; + if (!d->inPinch) { + if (qAbs(p1.x()-d->sceneStartPoint1.x()) > dragThreshold + || qAbs(p1.y()-d->sceneStartPoint1.y()) > dragThreshold + || qAbs(p2.x()-d->sceneStartPoint2.x()) > dragThreshold + || qAbs(p2.y()-d->sceneStartPoint2.y()) > dragThreshold) { + d->sceneStartCenter = sceneCenter; + d->sceneLastCenter = sceneCenter; + d->pinchStartCenter = mapFromScene(sceneCenter); + d->pinchStartDist = dist; + d->pinchStartAngle = angle; + d->pinchLastScale = 1.0; + d->pinchLastAngle = angle; + d->lastPoint1 = d->touchPoints.at(0).pos(); + d->lastPoint2 = d->touchPoints.at(1).pos(); + QDeclarativePinchEvent pe(d->pinchStartCenter, 1.0, angle, 0.0); + pe.setStartCenter(d->pinchStartCenter); + pe.setPreviousCenter(d->pinchStartCenter); + pe.setPreviousAngle(d->pinchLastAngle); + pe.setPreviousScale(d->pinchLastScale); + pe.setStartPoint1(mapFromScene(d->sceneStartPoint1)); + pe.setStartPoint2(mapFromScene(d->sceneStartPoint2)); + pe.setPoint1(d->lastPoint1); + pe.setPoint2(d->lastPoint2); + emit pinchStarted(&pe); + if (pe.accepted()) { + d->inPinch = true; + d->stealMouse = true; + QGraphicsScene *s = scene(); + if (s && s->mouseGrabberItem() != this) + grabMouse(); + setKeepMouseGrab(true); + if (d->pinch && d->pinch->target()) { + d->pinchStartPos = pinch()->target()->pos(); + d->pinchStartScale = d->pinch->target()->scale(); + d->pinchStartRotation = d->pinch->target()->rotation(); + d->pinch->setActive(true); + } + } else { + d->pinchRejected = true; + } + } + } else if (d->pinchStartDist > 0) { + qreal scale = dist / d->pinchStartDist; + qreal rotationAngle = d->pinchStartAngle - angle; + if (rotationAngle > 180) + rotationAngle -= 360; + QPointF pinchCenter = mapFromScene(sceneCenter); + QDeclarativePinchEvent pe(pinchCenter, scale, angle, rotationAngle); + pe.setStartCenter(d->pinchStartCenter); + pe.setPreviousCenter(mapFromScene(d->sceneLastCenter)); + pe.setPreviousAngle(d->pinchLastAngle); + pe.setPreviousScale(d->pinchLastScale); + pe.setStartPoint1(mapFromScene(d->sceneStartPoint1)); + pe.setStartPoint2(mapFromScene(d->sceneStartPoint2)); + pe.setPoint1(d->touchPoints.at(0).pos()); + pe.setPoint2(d->touchPoints.at(1).pos()); + d->pinchLastScale = scale; + d->sceneLastCenter = sceneCenter; + d->pinchLastAngle = angle; + d->lastPoint1 = d->touchPoints.at(0).pos(); + d->lastPoint2 = d->touchPoints.at(1).pos(); + emit pinchUpdated(&pe); + if (d->pinch && d->pinch->target()) { + qreal s = d->pinchStartScale * scale; + s = qMin(qMax(pinch()->minimumScale(),s), pinch()->maximumScale()); + pinch()->target()->setScale(s); + QPointF pos = sceneCenter - d->sceneStartCenter + d->pinchStartPos; + if (pinch()->axis() & QDeclarativePinch::XAxis) { + qreal x = pos.x(); + if (x < pinch()->xmin()) + x = pinch()->xmin(); + else if (x > pinch()->xmax()) + x = pinch()->xmax(); + pinch()->target()->setX(x); + } + if (pinch()->axis() & QDeclarativePinch::YAxis) { + qreal y = pos.y(); + if (y < pinch()->ymin()) + y = pinch()->ymin(); + else if (y > pinch()->ymax()) + y = pinch()->ymax(); + pinch()->target()->setY(y); + } + if (d->pinchStartRotation >= pinch()->minimumRotation() + && d->pinchStartRotation <= pinch()->maximumRotation()) { + qreal r = rotationAngle + d->pinchStartRotation; + r = qMin(qMax(pinch()->minimumRotation(),r), pinch()->maximumRotation()); + pinch()->target()->setRotation(r); + } + } + } + } + } +} + +void QDeclarativePinchArea::mousePressEvent(QGraphicsSceneMouseEvent *event) +{ + Q_D(QDeclarativePinchArea); + d->stealMouse = false; + if (!d->absorb) + QDeclarativeItem::mousePressEvent(event); + else { + setKeepMouseGrab(false); + event->setAccepted(true); + } +} + +void QDeclarativePinchArea::mouseMoveEvent(QGraphicsSceneMouseEvent *event) +{ + Q_D(QDeclarativePinchArea); + if (!d->absorb) { + QDeclarativeItem::mouseMoveEvent(event); + return; + } +} + +void QDeclarativePinchArea::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) +{ + Q_D(QDeclarativePinchArea); + d->stealMouse = false; + if (!d->absorb) { + QDeclarativeItem::mouseReleaseEvent(event); + } else { + QGraphicsScene *s = scene(); + if (s && s->mouseGrabberItem() == this) + ungrabMouse(); + setKeepMouseGrab(false); + } +} + +bool QDeclarativePinchArea::sceneEvent(QEvent *event) +{ + bool rv = QDeclarativeItem::sceneEvent(event); + if (event->type() == QEvent::UngrabMouse) { + setKeepMouseGrab(false); + } + return rv; +} + +bool QDeclarativePinchArea::sendMouseEvent(QGraphicsSceneMouseEvent *event) +{ + Q_D(QDeclarativePinchArea); + QGraphicsSceneMouseEvent mouseEvent(event->type()); + QRectF myRect = mapToScene(QRectF(0, 0, width(), height())).boundingRect(); + + QGraphicsScene *s = scene(); + QDeclarativeItem *grabber = s ? qobject_cast<QDeclarativeItem*>(s->mouseGrabberItem()) : 0; + bool stealThisEvent = d->stealMouse; + if ((stealThisEvent || myRect.contains(event->scenePos().toPoint())) && (!grabber || !grabber->keepMouseGrab())) { + mouseEvent.setAccepted(false); + for (int i = 0x1; i <= 0x10; i <<= 1) { + if (event->buttons() & i) { + Qt::MouseButton button = Qt::MouseButton(i); + mouseEvent.setButtonDownPos(button, mapFromScene(event->buttonDownPos(button))); + } + } + mouseEvent.setScenePos(event->scenePos()); + mouseEvent.setLastScenePos(event->lastScenePos()); + mouseEvent.setPos(mapFromScene(event->scenePos())); + mouseEvent.setLastPos(mapFromScene(event->lastScenePos())); + + switch(mouseEvent.type()) { + case QEvent::GraphicsSceneMouseMove: + mouseMoveEvent(&mouseEvent); + break; + case QEvent::GraphicsSceneMousePress: + mousePressEvent(&mouseEvent); + break; + case QEvent::GraphicsSceneMouseRelease: + mouseReleaseEvent(&mouseEvent); + break; + default: + break; + } + grabber = qobject_cast<QDeclarativeItem*>(s->mouseGrabberItem()); + if (grabber && stealThisEvent && !grabber->keepMouseGrab() && grabber != this) + grabMouse(); + + return stealThisEvent; + } + if (mouseEvent.type() == QEvent::GraphicsSceneMouseRelease) { + d->stealMouse = false; + if (s && s->mouseGrabberItem() == this) + ungrabMouse(); + setKeepMouseGrab(false); + } + return false; +} + +bool QDeclarativePinchArea::sceneEventFilter(QGraphicsItem *i, QEvent *e) +{ + Q_D(QDeclarativePinchArea); + if (!d->absorb || !isVisible()) + return QDeclarativeItem::sceneEventFilter(i, e); + switch (e->type()) { + case QEvent::GraphicsSceneMousePress: + case QEvent::GraphicsSceneMouseMove: + case QEvent::GraphicsSceneMouseRelease: + return sendMouseEvent(static_cast<QGraphicsSceneMouseEvent *>(e)); + break; + case QEvent::TouchBegin: + case QEvent::TouchUpdate: { + QTouchEvent *touch = static_cast<QTouchEvent*>(e); + d->touchPoints.clear(); + for (int i = 0; i < touch->touchPoints().count(); ++i) + if (!(touch->touchPoints().at(i).state() & Qt::TouchPointReleased)) + d->touchPoints << touch->touchPoints().at(i); + updatePinch(); + } + return d->inPinch; + case QEvent::TouchEnd: + d->touchPoints.clear(); + updatePinch(); + break; + default: + break; + } + + return QDeclarativeItem::sceneEventFilter(i, e); +} + +void QDeclarativePinchArea::geometryChanged(const QRectF &newGeometry, + const QRectF &oldGeometry) +{ + QDeclarativeItem::geometryChanged(newGeometry, oldGeometry); +} + +QVariant QDeclarativePinchArea::itemChange(GraphicsItemChange change, + const QVariant &value) +{ + return QDeclarativeItem::itemChange(change, value); +} + +QDeclarativePinch *QDeclarativePinchArea::pinch() +{ + Q_D(QDeclarativePinchArea); + if (!d->pinch) + d->pinch = new QDeclarativePinch; + return d->pinch; +} + + +QT_END_NAMESPACE diff --git a/src/declarative/graphicsitems/qdeclarativepincharea_p.h b/src/declarative/graphicsitems/qdeclarativepincharea_p.h new file mode 100644 index 0000000..6d04708 --- /dev/null +++ b/src/declarative/graphicsitems/qdeclarativepincharea_p.h @@ -0,0 +1,308 @@ +/**************************************************************************** +** +** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the QtDeclarative module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the Technology Preview License Agreement accompanying +** this package. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain additional +** rights. These rights are described in the Nokia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** If you have questions regarding the use of this file, please contact +** Nokia at qt-info@nokia.com. +** +** +** +** +** +** +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef QDECLARATIVEPINCHAREA_H +#define QDECLARATIVEPINCHAREA_H + +#include <qdeclarativeitem.h> + +QT_BEGIN_HEADER + +QT_BEGIN_NAMESPACE + +QT_MODULE(Declarative) + +class Q_AUTOTEST_EXPORT QDeclarativePinch : public QObject +{ + Q_OBJECT + + Q_ENUMS(Axis) + Q_PROPERTY(QGraphicsObject *target READ target WRITE setTarget RESET resetTarget) + Q_PROPERTY(qreal minimumScale READ minimumScale WRITE setMinimumScale NOTIFY minimumScaleChanged) + Q_PROPERTY(qreal maximumScale READ maximumScale WRITE setMaximumScale NOTIFY maximumScaleChanged) + Q_PROPERTY(qreal minimumRotation READ minimumRotation WRITE setMinimumRotation NOTIFY minimumRotationChanged) + Q_PROPERTY(qreal maximumRotation READ maximumRotation WRITE setMaximumRotation NOTIFY maximumRotationChanged) + Q_PROPERTY(Axis dragAxis READ axis WRITE setAxis NOTIFY dragAxisChanged) + Q_PROPERTY(qreal minimumX READ xmin WRITE setXmin NOTIFY minimumXChanged) + Q_PROPERTY(qreal maximumX READ xmax WRITE setXmax NOTIFY maximumXChanged) + Q_PROPERTY(qreal minimumY READ ymin WRITE setYmin NOTIFY minimumYChanged) + Q_PROPERTY(qreal maximumY READ ymax WRITE setYmax NOTIFY maximumYChanged) + Q_PROPERTY(bool active READ active NOTIFY activeChanged) + +public: + QDeclarativePinch(); + + QGraphicsObject *target() const { return m_target; } + void setTarget(QGraphicsObject *target) { + if (target == m_target) + return; + m_target = target; + emit targetChanged(); + } + void resetTarget() { + if (!m_target) + return; + m_target = 0; + emit targetChanged(); + } + + qreal minimumScale() const { return m_minScale; } + void setMinimumScale(qreal s) { + if (s == m_minScale) + return; + m_minScale = s; + emit minimumScaleChanged(); + } + qreal maximumScale() const { return m_maxScale; } + void setMaximumScale(qreal s) { + if (s == m_maxScale) + return; + m_maxScale = s; + emit maximumScaleChanged(); + } + + qreal minimumRotation() const { return m_minRotation; } + void setMinimumRotation(qreal r) { + if (r == m_minRotation) + return; + m_minRotation = r; + emit minimumRotationChanged(); + } + qreal maximumRotation() const { return m_maxRotation; } + void setMaximumRotation(qreal r) { + if (r == m_maxRotation) + return; + m_maxRotation = r; + emit maximumRotationChanged(); + } + + enum Axis { NoDrag=0x00, XAxis=0x01, YAxis=0x02, XandYAxis=0x03 }; + Axis axis() const { return m_axis; } + void setAxis(Axis a) { + if (a == m_axis) + return; + m_axis = a; + emit dragAxisChanged(); + } + + qreal xmin() const { return m_xmin; } + void setXmin(qreal x) { + if (x == m_xmin) + return; + m_xmin = x; + emit minimumXChanged(); + } + qreal xmax() const { return m_xmax; } + void setXmax(qreal x) { + if (x == m_xmax) + return; + m_xmax = x; + emit maximumXChanged(); + } + qreal ymin() const { return m_ymin; } + void setYmin(qreal y) { + if (y == m_ymin) + return; + m_ymin = y; + emit minimumYChanged(); + } + qreal ymax() const { return m_ymax; } + void setYmax(qreal y) { + if (y == m_ymax) + return; + m_ymax = y; + emit maximumYChanged(); + } + + bool active() const { return m_active; } + void setActive(bool a) { + if (a == m_active) + return; + m_active = a; + emit activeChanged(); + } + +signals: + void targetChanged(); + void minimumScaleChanged(); + void maximumScaleChanged(); + void minimumRotationChanged(); + void maximumRotationChanged(); + void dragAxisChanged(); + void minimumXChanged(); + void maximumXChanged(); + void minimumYChanged(); + void maximumYChanged(); + void activeChanged(); + +private: + QGraphicsObject *m_target; + qreal m_minScale; + qreal m_maxScale; + qreal m_minRotation; + qreal m_maxRotation; + Axis m_axis; + qreal m_xmin; + qreal m_xmax; + qreal m_ymin; + qreal m_ymax; + bool m_active; +}; + +class Q_AUTOTEST_EXPORT QDeclarativePinchEvent : public QObject +{ + Q_OBJECT + + Q_PROPERTY(QPointF center READ center) + Q_PROPERTY(QPointF startCenter READ startCenter) + Q_PROPERTY(QPointF previousCenter READ previousCenter) + Q_PROPERTY(qreal scale READ scale) + Q_PROPERTY(qreal previousScale READ previousScale) + Q_PROPERTY(qreal angle READ angle) + Q_PROPERTY(qreal previousAngle READ previousAngle) + Q_PROPERTY(qreal rotation READ rotation) + Q_PROPERTY(QPointF point1 READ point1) + Q_PROPERTY(QPointF startPoint1 READ startPoint1) + Q_PROPERTY(QPointF point2 READ point2) + Q_PROPERTY(QPointF startPoint2 READ startPoint2) + Q_PROPERTY(bool accepted READ accepted WRITE setAccepted) + +public: + QDeclarativePinchEvent(QPointF c, qreal s, qreal a, qreal r) + : QObject(), m_center(c), m_scale(s), m_angle(a), m_rotation(r), m_accepted(true) {} + + QPointF center() const { return m_center; } + QPointF startCenter() const { return m_startCenter; } + void setStartCenter(QPointF c) { m_startCenter = c; } + QPointF previousCenter() const { return m_lastCenter; } + void setPreviousCenter(QPointF c) { m_lastCenter = c; } + qreal scale() const { return m_scale; } + qreal previousScale() const { return m_lastScale; } + void setPreviousScale(qreal s) { m_lastScale = s; } + qreal angle() const { return m_angle; } + qreal previousAngle() const { return m_lastAngle; } + void setPreviousAngle(qreal a) { m_lastAngle = a; } + qreal rotation() const { return m_rotation; } + QPointF point1() const { return m_point1; } + void setPoint1(QPointF p) { m_point1 = p; } + QPointF startPoint1() const { return m_startPoint1; } + void setStartPoint1(QPointF p) { m_startPoint1 = p; } + QPointF point2() const { return m_point2; } + void setPoint2(QPointF p) { m_point2 = p; } + QPointF startPoint2() const { return m_startPoint2; } + void setStartPoint2(QPointF p) { m_startPoint2 = p; } + + bool accepted() const { return m_accepted; } + void setAccepted(bool a) { m_accepted = a; } + +private: + QPointF m_center; + QPointF m_startCenter; + QPointF m_lastCenter; + qreal m_scale; + qreal m_lastScale; + qreal m_angle; + qreal m_lastAngle; + qreal m_rotation; + QPointF m_point1; + QPointF m_point2; + QPointF m_startPoint1; + QPointF m_startPoint2; + bool m_accepted; +}; + + +class QDeclarativeMouseEvent; +class QDeclarativePinchAreaPrivate; +class Q_AUTOTEST_EXPORT QDeclarativePinchArea : public QDeclarativeItem +{ + Q_OBJECT + + Q_PROPERTY(bool enabled READ isEnabled WRITE setEnabled NOTIFY enabledChanged) + Q_PROPERTY(QDeclarativePinch *pinch READ pinch CONSTANT) + +public: + QDeclarativePinchArea(QDeclarativeItem *parent=0); + ~QDeclarativePinchArea(); + + bool isEnabled() const; + void setEnabled(bool); + + QDeclarativePinch *pinch(); + +Q_SIGNALS: + void enabledChanged(); + void pinchStarted(QDeclarativePinchEvent *pinch); + void pinchUpdated(QDeclarativePinchEvent *pinch); + void pinchFinished(QDeclarativePinchEvent *pinch); + +protected: + void mousePressEvent(QGraphicsSceneMouseEvent *event); + void mouseReleaseEvent(QGraphicsSceneMouseEvent *event); + void mouseMoveEvent(QGraphicsSceneMouseEvent *event); + bool sceneEvent(QEvent *); + bool sendMouseEvent(QGraphicsSceneMouseEvent *event); + bool sceneEventFilter(QGraphicsItem *i, QEvent *e); + bool event(QEvent *); + + virtual void geometryChanged(const QRectF &newGeometry, + const QRectF &oldGeometry); + virtual QVariant itemChange(GraphicsItemChange change, const QVariant& value); + +private: + void updatePinch(); + void handlePress(); + void handleRelease(); + +private: + Q_DISABLE_COPY(QDeclarativePinchArea) + Q_DECLARE_PRIVATE_D(QGraphicsItem::d_ptr.data(), QDeclarativePinchArea) +}; + +QT_END_NAMESPACE + +QML_DECLARE_TYPE(QDeclarativePinch) +QML_DECLARE_TYPE(QDeclarativePinchEvent) +QML_DECLARE_TYPE(QDeclarativePinchArea) + +QT_END_HEADER + +#endif // QDECLARATIVEPINCHAREA_H diff --git a/src/declarative/graphicsitems/qdeclarativepincharea_p_p.h b/src/declarative/graphicsitems/qdeclarativepincharea_p_p.h new file mode 100644 index 0000000..5641e35 --- /dev/null +++ b/src/declarative/graphicsitems/qdeclarativepincharea_p_p.h @@ -0,0 +1,109 @@ +/**************************************************************************** +** +** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the QtDeclarative module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the Technology Preview License Agreement accompanying +** this package. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain additional +** rights. These rights are described in the Nokia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** If you have questions regarding the use of this file, please contact +** Nokia at qt-info@nokia.com. +** +** +** +** +** +** +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef QDECLARATIVEPINCHAREA_P_H +#define QDECLARATIVEPINCHAREA_P_H + +// +// W A R N I N G +// ------------- +// +// This file is not part of the Qt API. It exists purely as an +// implementation detail. This header file may change from version to +// version without notice, or even be removed. +// +// We mean it. +// + +#include <qdatetime.h> +#include <qbasictimer.h> +#include <qevent.h> +#include <qgraphicssceneevent.h> +#include "qdeclarativeitem_p.h" + +QT_BEGIN_NAMESPACE + +class QDeclarativePinch; +class QDeclarativePinchAreaPrivate : public QDeclarativeItemPrivate +{ + Q_DECLARE_PUBLIC(QDeclarativePinchArea) +public: + QDeclarativePinchAreaPrivate() + : absorb(true), stealMouse(false), inPinch(false) + , pinchRejected(false), pinch(0), pinchStartDist(0) + { + } + + ~QDeclarativePinchAreaPrivate(); + + void init() + { + Q_Q(QDeclarativePinchArea); + q->setAcceptedMouseButtons(Qt::LeftButton); + q->setAcceptTouchEvents(true); + q->setFiltersChildEvents(true); + } + + bool absorb : 1; + bool stealMouse : 1; + bool inPinch : 1; + bool pinchRejected : 1; + QDeclarativePinch *pinch; + QPointF sceneStartPoint1; + QPointF sceneStartPoint2; + QPointF lastPoint1; + QPointF lastPoint2; + qreal pinchStartDist; + qreal pinchStartScale; + qreal pinchLastScale; + qreal pinchStartRotation; + qreal pinchStartAngle; + qreal pinchLastAngle; + QPointF sceneStartCenter; + QPointF pinchStartCenter; + QPointF sceneLastCenter; + QPointF pinchStartPos; + QList<QTouchEvent::TouchPoint> touchPoints; +}; + +QT_END_NAMESPACE + +#endif // QDECLARATIVEPINCHAREA_P_H diff --git a/src/declarative/graphicsitems/qdeclarativepositioners.cpp b/src/declarative/graphicsitems/qdeclarativepositioners.cpp index e0bd2ff..27a1301 100644 --- a/src/declarative/graphicsitems/qdeclarativepositioners.cpp +++ b/src/declarative/graphicsitems/qdeclarativepositioners.cpp @@ -111,14 +111,14 @@ void QDeclarativeBasePositioner::graphicsWidgetGeometryChanged() Note that the subclass is responsible for adding the spacing in between items. */ QDeclarativeBasePositioner::QDeclarativeBasePositioner(PositionerType at, QDeclarativeItem *parent) - : QDeclarativeItem(*(new QDeclarativeBasePositionerPrivate), parent) + : QDeclarativeImplicitSizeItem(*(new QDeclarativeBasePositionerPrivate), parent) { Q_D(QDeclarativeBasePositioner); d->init(at); } QDeclarativeBasePositioner::QDeclarativeBasePositioner(QDeclarativeBasePositionerPrivate &dd, PositionerType at, QDeclarativeItem *parent) - : QDeclarativeItem(dd, parent) + : QDeclarativeImplicitSizeItem(dd, parent) { Q_D(QDeclarativeBasePositioner); d->init(at); @@ -385,7 +385,7 @@ void QDeclarativeBasePositioner::finishApplyTransitions() Items with a width or height of 0 will not be positioned. - \sa Row, Grid, Flow, {declarative/positioners}{Positioners example} + \sa Row, Grid, Flow, {declarative/positioners/addandremove}{Positioners example} */ /*! \qmlproperty Transition Column::add @@ -425,7 +425,7 @@ void QDeclarativeBasePositioner::finishApplyTransitions() } \endqml - \sa add, {declarative/positioners}{Positioners example} + \sa add, {declarative/positioners/addandremove}{Positioners example} */ /*! \qmlproperty int Column::spacing @@ -528,7 +528,7 @@ void QDeclarativeColumn::reportConflictingAnchors() Items with a width or height of 0 will not be positioned. - \sa Column, Grid, Flow, {declarative/positioners}{Positioners example} + \sa Column, Grid, Flow, {declarative/positioners/addandremove}{Positioners example} */ /*! \qmlproperty Transition Row::add @@ -567,7 +567,7 @@ void QDeclarativeColumn::reportConflictingAnchors() } \endqml - \sa add, {declarative/positioners}{Positioners example} + \sa add, {declarative/positioners/addandremove}{Positioners example} */ /*! \qmlproperty int Row::spacing @@ -578,21 +578,55 @@ void QDeclarativeColumn::reportConflictingAnchors() \sa Grid::spacing */ QDeclarativeRow::QDeclarativeRow(QDeclarativeItem *parent) -: QDeclarativeBasePositioner(Horizontal, parent) +: QDeclarativeBasePositioner(Horizontal, parent), m_layoutDirection(Qt::LeftToRight) { } +/*! + \qmlproperty enumeration Row::layoutDirection + This property holds the layoutDirection of the row. + + Possible values: + + \list + \o Qt.LeftToRight (default) - Items are laid out from left to right. If the width of the row is explicitly set, + the left anchor remains to the left of the row. + \o Qt.RightToLeft - Items are laid out from right to left. If the width of the row is explicitly set, + the right anchor remains to the right of the row. + \endlist + + \sa Grid::layoutDirection, Flow::layoutDirection, {declarative/positioners/layoutdirection}{Layout directions example} +*/ +Qt::LayoutDirection QDeclarativeRow::layoutDirection() const +{ + return m_layoutDirection; +} + +void QDeclarativeRow::setLayoutDirection(Qt::LayoutDirection layoutDirection) +{ + if (m_layoutDirection != layoutDirection) { + m_layoutDirection = layoutDirection; + prePositioning(); + emit layoutDirectionChanged(); + } +} + void QDeclarativeRow::doPositioning(QSizeF *contentSize) { int hoffset = 0; + QList<int> hoffsets; for (int ii = 0; ii < positionedItems.count(); ++ii) { const PositionedItem &child = positionedItems.at(ii); if (!child.item || !child.isVisible) continue; - if(child.item->x() != hoffset) - positionX(hoffset, child); + if(m_layoutDirection == Qt::LeftToRight){ + if(child.item->x() != hoffset) + positionX(hoffset, child); + }else{ + hoffsets << hoffset; + } contentSize->setHeight(qMax(contentSize->height(), QGraphicsItemPrivate::get(child.item)->height())); @@ -601,6 +635,26 @@ void QDeclarativeRow::doPositioning(QSizeF *contentSize) } contentSize->setWidth(hoffset - spacing()); + + if(m_layoutDirection == Qt::LeftToRight) + return; + + //Right to Left layout + int end = 0; + if(!widthValid()) + end = contentSize->width(); + else + end = width(); + + int acc = 0; + for (int ii = 0; ii < positionedItems.count(); ++ii) { + const PositionedItem &child = positionedItems.at(ii); + if (!child.item || !child.isVisible) + continue; + hoffset = end - hoffsets[acc++] - QGraphicsItemPrivate::get(child.item)->width(); + if(child.item->x() != hoffset) + positionX(hoffset, child); + } } void QDeclarativeRow::reportConflictingAnchors() @@ -678,7 +732,7 @@ void QDeclarativeRow::reportConflictingAnchors() Items with a width or height of 0 will not be positioned. - \sa Flow, Row, Column, {declarative/positioners}{Positioners example} + \sa Flow, Row, Column, {declarative/positioners/addandremove}{Positioners example} */ /*! \qmlproperty Transition Grid::add @@ -716,7 +770,7 @@ void QDeclarativeRow::reportConflictingAnchors() } \endqml - \sa add, {declarative/positioners}{Positioners example} + \sa add, {declarative/positioners/addandremove}{Positioners example} */ /*! \qmlproperty int Grid::spacing @@ -736,7 +790,7 @@ void QDeclarativeRow::reportConflictingAnchors() \sa rows, columns */ QDeclarativeGrid::QDeclarativeGrid(QDeclarativeItem *parent) : - QDeclarativeBasePositioner(Both, parent), m_rows(-1), m_columns(-1), m_flow(LeftToRight) + QDeclarativeBasePositioner(Both, parent), m_rows(-1), m_columns(-1), m_flow(LeftToRight), m_layoutDirection(Qt::LeftToRight) { } @@ -784,7 +838,7 @@ void QDeclarativeGrid::setRows(const int rows) \list \o Grid.LeftToRight (default) - Items are positioned next to - to each other from left to right, then wrapped to the next line. + each other in the \l layoutDirection, then wrapped to the next line. \o Grid.TopToBottom - Items are positioned next to each other from top to bottom, then wrapped to the next column. \endlist @@ -803,6 +857,37 @@ void QDeclarativeGrid::setFlow(Flow flow) } } +/*! + \qmlproperty enumeration Grid::layoutDirection + This property holds the layout direction of the layout. + + Possible values are: + + \list + \o Qt.LeftToRight (default) - Items are positioned beginning + from the top, left anchor. The flow direction is dependent + on the \l Grid::flow property. + \o Qt.RightToLeft - Items are positioned beginning from the + top, right anchor. The flow direction is dependent on the + \l Grid::flow property. + \endlist + + \sa Flow::layoutDirection, Row::layoutDirection, {declarative/positioners/layoutdirection}{Layout directions example} +*/ +Qt::LayoutDirection QDeclarativeGrid::layoutDirection() const +{ + return m_layoutDirection; +} + +void QDeclarativeGrid::setLayoutDirection(Qt::LayoutDirection layoutDirection) +{ + if (m_layoutDirection != layoutDirection) { + m_layoutDirection = layoutDirection; + prePositioning(); + emit layoutDirectionChanged(); + } +} + void QDeclarativeGrid::doPositioning(QSizeF *contentSize) { @@ -825,6 +910,9 @@ void QDeclarativeGrid::doPositioning(QSizeF *contentSize) c = (numVisible+(m_rows-1))/m_rows; } + if(r==0 || c==0) + return; //Nothing to do + QList<int> maxColWidth; QList<int> maxRowHeight; int childIndex =0; @@ -855,7 +943,7 @@ void QDeclarativeGrid::doPositioning(QSizeF *contentSize) if (i==0) maxColWidth << 0; - if (childIndex == positionedItems.count()) + if (childIndex == visibleItems.count()) break; const PositionedItem &child = visibleItems.at(childIndex++); @@ -868,40 +956,71 @@ void QDeclarativeGrid::doPositioning(QSizeF *contentSize) } } + int widthSum = 0; + for(int j=0; j < maxColWidth.size(); j++){ + if(j) + widthSum += spacing(); + widthSum += maxColWidth[j]; + } + + int heightSum = 0; + for(int i=0; i < maxRowHeight.size(); i++){ + if(i) + heightSum += spacing(); + heightSum += maxRowHeight[i]; + } + + contentSize->setHeight(heightSum); + contentSize->setWidth(widthSum); + + int end = 0; + if(widthValid()) + end = width(); + else + end = widthSum; + int xoffset=0; + if(m_layoutDirection == Qt::RightToLeft) + xoffset=end; int yoffset=0; int curRow =0; int curCol =0; for (int i = 0; i < visibleItems.count(); ++i) { const PositionedItem &child = visibleItems.at(i); - if((child.item->x()!=xoffset)||(child.item->y()!=yoffset)){ - positionX(xoffset, child); + int childXOffset = xoffset; + if(m_layoutDirection == Qt::RightToLeft) + childXOffset -= QGraphicsItemPrivate::get(child.item)->width(); + if((child.item->x()!=childXOffset)||(child.item->y()!=yoffset)){ + positionX(childXOffset, child); positionY(yoffset, child); } if (m_flow == LeftToRight) { - contentSize->setWidth(qMax(contentSize->width(), xoffset + QGraphicsItemPrivate::get(child.item)->width())); - contentSize->setHeight(yoffset + maxRowHeight[curRow]); - - xoffset+=maxColWidth[curCol]+spacing(); + if(m_layoutDirection == Qt::LeftToRight) + xoffset+=maxColWidth[curCol]+spacing(); + else + xoffset-=maxColWidth[curCol]+spacing(); curCol++; curCol%=c; if (!curCol){ yoffset+=maxRowHeight[curRow]+spacing(); - xoffset=0; + if(m_layoutDirection == Qt::LeftToRight) + xoffset=0; + else + xoffset=end; curRow++; if (curRow>=r) break; } } else { - contentSize->setHeight(qMax(contentSize->height(), yoffset + QGraphicsItemPrivate::get(child.item)->height())); - contentSize->setWidth(xoffset + maxColWidth[curCol]); - yoffset+=maxRowHeight[curRow]+spacing(); curRow++; curRow%=r; if (!curRow){ - xoffset+=maxColWidth[curCol]+spacing(); + if(m_layoutDirection == Qt::LeftToRight) + xoffset+=maxColWidth[curCol]+spacing(); + else + xoffset-=maxColWidth[curCol]+spacing(); yoffset=0; curCol++; if (curCol>=c) @@ -977,7 +1096,7 @@ void QDeclarativeGrid::reportConflictingAnchors() Items with a width or height of 0 will not be positioned. - \sa Column, Row, Grid, {declarative/positioners}{Positioners example} + \sa Column, Row, Grid, {declarative/positioners/addandremove}{Positioners example} */ /*! \qmlproperty Transition Flow::add @@ -1016,7 +1135,7 @@ void QDeclarativeGrid::reportConflictingAnchors() } \endqml - \sa add, {declarative/positioners}{Positioners example} + \sa add, {declarative/positioners/addandremove}{Positioners example} */ /*! \qmlproperty int Flow::spacing @@ -1033,10 +1152,12 @@ class QDeclarativeFlowPrivate : public QDeclarativeBasePositionerPrivate public: QDeclarativeFlowPrivate() - : QDeclarativeBasePositionerPrivate(), flow(QDeclarativeFlow::LeftToRight) + : QDeclarativeBasePositionerPrivate(), flow(QDeclarativeFlow::LeftToRight), + layoutDirection(Qt::LeftToRight) {} QDeclarativeFlow::Flow flow; + Qt::LayoutDirection layoutDirection; }; QDeclarativeFlow::QDeclarativeFlow(QDeclarativeItem *parent) @@ -1055,7 +1176,7 @@ QDeclarativeFlow::QDeclarativeFlow(QDeclarativeItem *parent) \list \o Flow.LeftToRight (default) - Items are positioned next to - to each other from left to right until the width of the Flow + to each other according to the \l layoutDirection until the width of the Flow is exceeded, then wrapped to the next line. \o Flow.TopToBottom - Items are positioned next to each other from top to bottom until the height of the Flow is exceeded, @@ -1078,6 +1199,40 @@ void QDeclarativeFlow::setFlow(Flow flow) } } +/*! + \qmlproperty enumeration Flow::layoutDirection + This property holds the layout direction of the layout. + + Possible values are: + + \list + \o Qt.LeftToRight (default) - Items are positioned beginning + from the top, left anchor. The flow direction is dependent + on the \l Flow::flow property. + \o Qt.RightToLeft - Items are positioned beginning from the + top, right anchor. The flow direction is dependent on the + \l Flow::flow property. + \endlist + + \sa Grid::layoutDirection, Row::layoutDirection, {declarative/positioners/layoutdirection}{Layout directions example} +*/ + +Qt::LayoutDirection QDeclarativeFlow::layoutDirection() const +{ + Q_D(const QDeclarativeFlow); + return d->layoutDirection; +} + +void QDeclarativeFlow::setLayoutDirection(Qt::LayoutDirection layoutDirection) +{ + Q_D(QDeclarativeFlow); + if (d->layoutDirection != layoutDirection) { + d->layoutDirection = layoutDirection; + prePositioning(); + emit layoutDirectionChanged(); + } +} + void QDeclarativeFlow::doPositioning(QSizeF *contentSize) { Q_D(QDeclarativeFlow); @@ -1085,6 +1240,7 @@ void QDeclarativeFlow::doPositioning(QSizeF *contentSize) int hoffset = 0; int voffset = 0; int linemax = 0; + QList<int> hoffsets; for (int i = 0; i < positionedItems.count(); ++i) { const PositionedItem &child = positionedItems.at(i); @@ -1106,10 +1262,14 @@ void QDeclarativeFlow::doPositioning(QSizeF *contentSize) } } - if(child.item->x() != hoffset || child.item->y() != voffset){ - positionX(hoffset, child); - positionY(voffset, child); + if(d->layoutDirection == Qt::LeftToRight){ + if(child.item->x() != hoffset) + positionX(hoffset, child); + }else{ + hoffsets << hoffset; } + if(child.item->y() != voffset) + positionY(voffset, child); contentSize->setWidth(qMax(contentSize->width(), hoffset + childPrivate->width())); contentSize->setHeight(qMax(contentSize->height(), voffset + childPrivate->height())); @@ -1124,6 +1284,24 @@ void QDeclarativeFlow::doPositioning(QSizeF *contentSize) linemax = qMax(linemax, qCeil(childPrivate->width())); } } + + if(d->layoutDirection == Qt::LeftToRight) + return; + + int end; + if(widthValid()) + end = width(); + else + end = contentSize->width(); + int acc = 0; + for (int i = 0; i < positionedItems.count(); ++i) { + const PositionedItem &child = positionedItems.at(i); + if (!child.item || !child.isVisible) + continue; + hoffset = end - hoffsets[acc++] - QGraphicsItemPrivate::get(child.item)->width(); + if(child.item->x() != hoffset) + positionX(hoffset, child); + } } void QDeclarativeFlow::reportConflictingAnchors() diff --git a/src/declarative/graphicsitems/qdeclarativepositioners_p.h b/src/declarative/graphicsitems/qdeclarativepositioners_p.h index 9efbeb9..55d8fa1 100644 --- a/src/declarative/graphicsitems/qdeclarativepositioners_p.h +++ b/src/declarative/graphicsitems/qdeclarativepositioners_p.h @@ -42,7 +42,7 @@ #ifndef QDECLARATIVELAYOUTS_H #define QDECLARATIVELAYOUTS_H -#include "qdeclarativeitem.h" +#include "qdeclarativeimplicitsizeitem_p.h" #include <private/qdeclarativestate_p.h> #include <private/qpodvector_p.h> @@ -57,7 +57,7 @@ QT_BEGIN_NAMESPACE QT_MODULE(Declarative) class QDeclarativeBasePositionerPrivate; -class Q_DECLARATIVE_PRIVATE_EXPORT QDeclarativeBasePositioner : public QDeclarativeItem +class Q_DECLARATIVE_PRIVATE_EXPORT QDeclarativeBasePositioner : public QDeclarativeImplicitSizeItem { Q_OBJECT @@ -129,12 +129,21 @@ private: class Q_AUTOTEST_EXPORT QDeclarativeRow: public QDeclarativeBasePositioner { Q_OBJECT + Q_PROPERTY(Qt::LayoutDirection layoutDirection READ layoutDirection WRITE setLayoutDirection NOTIFY layoutDirectionChanged REVISION 1) public: QDeclarativeRow(QDeclarativeItem *parent=0); + + Qt::LayoutDirection layoutDirection() const; + void setLayoutDirection (Qt::LayoutDirection); + +Q_SIGNALS: + Q_REVISION(1) void layoutDirectionChanged(); + protected: virtual void doPositioning(QSizeF *contentSize); virtual void reportConflictingAnchors(); private: + Qt::LayoutDirection m_layoutDirection; Q_DISABLE_COPY(QDeclarativeRow) }; @@ -144,6 +153,7 @@ class Q_AUTOTEST_EXPORT QDeclarativeGrid : public QDeclarativeBasePositioner Q_PROPERTY(int rows READ rows WRITE setRows NOTIFY rowsChanged) Q_PROPERTY(int columns READ columns WRITE setColumns NOTIFY columnsChanged) Q_PROPERTY(Flow flow READ flow WRITE setFlow NOTIFY flowChanged) + Q_PROPERTY(Qt::LayoutDirection layoutDirection READ layoutDirection WRITE setLayoutDirection NOTIFY layoutDirectionChanged REVISION 1) public: QDeclarativeGrid(QDeclarativeItem *parent=0); @@ -159,10 +169,14 @@ public: Flow flow() const; void setFlow(Flow); + Qt::LayoutDirection layoutDirection() const; + void setLayoutDirection (Qt::LayoutDirection); + Q_SIGNALS: void rowsChanged(); void columnsChanged(); void flowChanged(); + Q_REVISION(1) void layoutDirectionChanged(); protected: virtual void doPositioning(QSizeF *contentSize); @@ -172,6 +186,7 @@ private: int m_rows; int m_columns; Flow m_flow; + Qt::LayoutDirection m_layoutDirection; Q_DISABLE_COPY(QDeclarativeGrid) }; @@ -179,6 +194,7 @@ class QDeclarativeFlowPrivate; class Q_AUTOTEST_EXPORT QDeclarativeFlow: public QDeclarativeBasePositioner { Q_OBJECT + Q_PROPERTY(Qt::LayoutDirection layoutDirection READ layoutDirection WRITE setLayoutDirection NOTIFY layoutDirectionChanged REVISION 1) Q_PROPERTY(Flow flow READ flow WRITE setFlow NOTIFY flowChanged) public: QDeclarativeFlow(QDeclarativeItem *parent=0); @@ -188,8 +204,12 @@ public: Flow flow() const; void setFlow(Flow); + Qt::LayoutDirection layoutDirection() const; + void setLayoutDirection (Qt::LayoutDirection); + Q_SIGNALS: void flowChanged(); + Q_REVISION(1) void layoutDirectionChanged(); protected: virtual void doPositioning(QSizeF *contentSize); diff --git a/src/declarative/graphicsitems/qdeclarativepositioners_p_p.h b/src/declarative/graphicsitems/qdeclarativepositioners_p_p.h index c6535a7..df105c6 100644 --- a/src/declarative/graphicsitems/qdeclarativepositioners_p_p.h +++ b/src/declarative/graphicsitems/qdeclarativepositioners_p_p.h @@ -55,7 +55,7 @@ #include "private/qdeclarativepositioners_p.h" -#include "private/qdeclarativeitem_p.h" +#include "private/qdeclarativeimplicitsizeitem_p_p.h" #include <qdeclarativestate_p.h> #include <qdeclarativetransitionmanager_p_p.h> @@ -67,7 +67,7 @@ #include <QDebug> QT_BEGIN_NAMESPACE -class QDeclarativeBasePositionerPrivate : public QDeclarativeItemPrivate, public QDeclarativeItemChangeListener +class QDeclarativeBasePositionerPrivate : public QDeclarativeImplicitSizeItemPrivate, public QDeclarativeItemChangeListener { Q_DECLARE_PUBLIC(QDeclarativeBasePositioner) diff --git a/src/declarative/graphicsitems/qdeclarativerepeater.cpp b/src/declarative/graphicsitems/qdeclarativerepeater.cpp index 7aa30e5..8455513 100644 --- a/src/declarative/graphicsitems/qdeclarativerepeater.cpp +++ b/src/declarative/graphicsitems/qdeclarativerepeater.cpp @@ -84,43 +84,23 @@ QDeclarativeRepeaterPrivate::~QDeclarativeRepeaterPrivate() \image repeater-simple.png - The \l model of a Repeater can be any of the supported \l {qmlmodels}{Data Models}. + A Repeater's \l model can be any of the supported \l {qmlmodels}{data models}. + Additionally, like delegates for other views, a Repeater delegate can access + its index within the repeater, as well as the model data relevant to the + delegate. See the \l delegate property documentation for details. Items instantiated by the Repeater are inserted, in order, as children of the Repeater's parent. The insertion starts immediately after the repeater's position in its parent stacking list. This allows a Repeater to be used inside a layout. For example, the following Repeater's items are stacked between a red rectangle and a blue rectangle: - + \snippet doc/src/snippets/declarative/repeaters/repeater.qml layout \image repeater.png - \section2 The \c index and \c modelData properties - - The index of a delegate is exposed as an accessible \c index property in the delegate. - Properties of the model are also available depending upon the type of \l {qmlmodels}{Data Model}. - - Here is a Repeater that uses the \c index property inside the instantiated items: - - \table - \row - \o \snippet doc/src/snippets/declarative/repeaters/repeater.qml index - \o \image repeater-index.png - \endtable - - Here is another Repeater that uses the \c modelData property to reference the data for a - particular index: - - \table - \row - \o \snippet doc/src/snippets/declarative/repeaters/repeater.qml modeldata - \o \image repeater-modeldata.png - \endtable - - - A Repeater item owns all items it instantiates. Removing or dynamically destroying + \note A Repeater item owns all items it instantiates. Removing or dynamically destroying an item created by a Repeater results in unpredictable behavior. @@ -146,6 +126,27 @@ QDeclarativeRepeaterPrivate::~QDeclarativeRepeaterPrivate() \endcode */ +/*! + \qmlsignal Repeater::onItemAdded(int index, Item item) + \since Quick 1.1 + + This handler is called when an item is added to the repeater. The \a index + parameter holds the index at which the item has been inserted within the + repeater, and the \a item parameter holds the \l Item that has been added. +*/ + +/*! + \qmlsignal Repeater::onItemRemoved(int index, Item item) + \since Quick 1.1 + + This handler is called when an item is removed from the repeater. The \a index + parameter holds the index at which the item was removed from the repeater, + and the \a item parameter holds the \l Item that was removed. + + Do not keep a reference to \a item if it was created by this repeater, as + in these cases it will be deleted shortly after the handler is called. +*/ + QDeclarativeRepeater::QDeclarativeRepeater(QDeclarativeItem *parent) : QDeclarativeItem(*(new QDeclarativeRepeaterPrivate), parent) { @@ -160,26 +161,16 @@ QDeclarativeRepeater::~QDeclarativeRepeater() The model providing data for the repeater. - This property can be set to any of the following: + This property can be set to any of the supported \l {qmlmodels}{data models}: \list - \o A number that indicates the number of delegates to be created + \o A number that indicates the number of delegates to be created by the repeater \o A model (e.g. a ListModel item, or a QAbstractItemModel subclass) \o A string list \o An object list \endlist - In each case, the data element and the index is exposed to each instantiated - component. The index is always exposed as an accessible \c index property. - In the case of an object or string list, the data element (of type string - or object) is available as the \c modelData property. In the case of a Qt model, - all roles are available as named properties just like in the view classes. - - As a special case the model can also be merely a number. In this case it will - create that many instances of the component. They will also be assigned an index - based on the order they are created. - - Models can also be created directly in QML, using a \l{ListModel} or \l{XmlListModel}. + The type of model affects the properties that are exposed to the \l delegate. \sa {qmlmodels}{Data Models} */ @@ -243,8 +234,33 @@ void QDeclarativeRepeater::setModel(const QVariant &model) \default The delegate provides a template defining each item instantiated by the repeater. - The index is exposed as an accessible \c index property. Properties of the - model are also available depending upon the type of \l {qmlmodels}{Data Model}. + + Delegates are exposed to a read-only \c index property that indicates the index + of the delegate within the repeater. For example, the following \l Text delegate + displays the index of each repeated item: + + \table + \row + \o \snippet doc/src/snippets/declarative/repeaters/repeater.qml index + \o \image repeater-index.png + \endtable + + If the \l model is a \l{QStringList-based model}{string list} or + \l{QObjectList-based model}{object list}, the delegate is also exposed to + a read-only \c modelData property that holds the string or object data. For + example: + + \table + \row + \o \snippet doc/src/snippets/declarative/repeaters/repeater.qml modeldata + \o \image repeater-modeldata.png + \endtable + + If the \l model is a model object (such as a \l ListModel) the delegate + can access all model roles as named properties, in the same way that delegates + do for view classes like ListView. + + \sa {QML Data Models} */ QDeclarativeComponent *QDeclarativeRepeater::delegate() const { @@ -288,6 +304,21 @@ int QDeclarativeRepeater::count() const return 0; } +/*! + \qmlmethod Item Repeater::itemAt(index) + \since Quick 1.1 + + Returns the \l Item that has been created at the given \a index, or \c null + if no item exists at \a index. +*/ +QDeclarativeItem *QDeclarativeRepeater::itemAt(int index) const +{ + Q_D(const QDeclarativeRepeater); + if (index >= 0 && index < d->deletables.count()) + return d->deletables[index]; + return 0; + +} void QDeclarativeRepeater::componentComplete() { @@ -309,8 +340,13 @@ QVariant QDeclarativeRepeater::itemChange(GraphicsItemChange change, void QDeclarativeRepeater::clear() { Q_D(QDeclarativeRepeater); + bool complete = isComponentComplete(); + if (d->model) { - foreach (QDeclarativeItem *item, d->deletables) { + while (d->deletables.count() > 0) { + QDeclarativeItem *item = d->deletables.takeLast(); + if (complete) + emit itemRemoved(d->deletables.count()-1, item); d->model->release(item); } } @@ -335,6 +371,7 @@ void QDeclarativeRepeater::regenerate() item->setParentItem(parentItem()); item->stackBefore(this); d->deletables << item; + emit itemAdded(ii, item); } } } @@ -355,6 +392,7 @@ void QDeclarativeRepeater::itemsInserted(int index, int count) else item->stackBefore(this); d->deletables.insert(modelIndex, item); + emit itemAdded(modelIndex, item); } } emit countChanged(); @@ -367,6 +405,7 @@ void QDeclarativeRepeater::itemsRemoved(int index, int count) return; while (count--) { QDeclarativeItem *item = d->deletables.takeAt(index); + emit itemRemoved(index, item); if (item) d->model->release(item); else diff --git a/src/declarative/graphicsitems/qdeclarativerepeater_p.h b/src/declarative/graphicsitems/qdeclarativerepeater_p.h index 73b50c2..9cb4e30 100644 --- a/src/declarative/graphicsitems/qdeclarativerepeater_p.h +++ b/src/declarative/graphicsitems/qdeclarativerepeater_p.h @@ -72,10 +72,16 @@ public: int count() const; + Q_INVOKABLE Q_REVISION(1) QDeclarativeItem *itemAt(int index) const; + Q_SIGNALS: void modelChanged(); void delegateChanged(); void countChanged(); + + Q_REVISION(1) void itemAdded(int index, QDeclarativeItem *item); + Q_REVISION(1) void itemRemoved(int index, QDeclarativeItem *item); + private: void clear(); void regenerate(); diff --git a/src/declarative/graphicsitems/qdeclarativetext.cpp b/src/declarative/graphicsitems/qdeclarativetext.cpp index d56035e..8f1d531 100644 --- a/src/declarative/graphicsitems/qdeclarativetext.cpp +++ b/src/declarative/graphicsitems/qdeclarativetext.cpp @@ -41,6 +41,7 @@ #include "private/qdeclarativetext_p.h" #include "private/qdeclarativetext_p_p.h" +#include <private/qtextdocumentlayout_p.h> #include <qdeclarativestyledtext_p.h> #include <qdeclarativeinfo.h> #include <qdeclarativepixmapcache_p.h> @@ -54,6 +55,7 @@ #include <QPainter> #include <QAbstractTextDocumentLayout> #include <qmath.h> +#include <limits.h> QT_BEGIN_NAMESPACE @@ -82,14 +84,25 @@ private: static QSet<QUrl> errors; }; +class QDeclarativeTextDocumentLayout : public QTextDocumentLayout +{ + Q_OBJECT +public: + QDeclarativeTextDocumentLayout(QTextDocument *doc); + void setLineHeight(qreal lineHeight, QDeclarativeText::LineHeightMode mode); +}; + DEFINE_BOOL_CONFIG_OPTION(enableImageCache, QML_ENABLE_TEXT_IMAGE_CACHE); +QString QDeclarativeTextPrivate::elideChar = QString(0x2026); + QDeclarativeTextPrivate::QDeclarativeTextPrivate() : color((QRgb)0), style(QDeclarativeText::Normal), hAlign(QDeclarativeText::AlignLeft), vAlign(QDeclarativeText::AlignTop), elideMode(QDeclarativeText::ElideNone), - format(QDeclarativeText::AutoText), wrapMode(QDeclarativeText::NoWrap), imageCacheDirty(true), - updateOnComponentComplete(true), richText(false), singleline(false), cacheAllTextAsImage(true), - internalWidthUpdate(false), doc(0) + format(QDeclarativeText::AutoText), wrapMode(QDeclarativeText::NoWrap), lineHeight(1), lineHeightMode(QDeclarativeText::MultiplyHeight), + lineCount(1), truncated(false), maximumLineCount(INT_MAX), + maximumLineCountValid(false), imageCacheDirty(true), updateOnComponentComplete(true), richText(false), singleline(false), + cacheAllTextAsImage(true), internalWidthUpdate(false), requireImplicitWidth(false), naturalWidth(0), doc(0) { cacheAllTextAsImage = enableImageCache(); QGraphicsItemPrivate::acceptedMouseButtons = Qt::LeftButton; @@ -172,10 +185,31 @@ void QTextDocumentWithImageResources::setText(const QString &text) QSet<QUrl> QTextDocumentWithImageResources::errors; +QDeclarativeTextDocumentLayout::QDeclarativeTextDocumentLayout(QTextDocument *doc) + : QTextDocumentLayout(doc) { +} + +void QDeclarativeTextDocumentLayout::setLineHeight(qreal lineHeight, QDeclarativeText::LineHeightMode mode = QDeclarativeText::MultiplyHeight) +{ + QTextDocumentLayout::setLineHeight(lineHeight, QTextDocumentLayout::LineHeightMode(mode)); +} + QDeclarativeTextPrivate::~QDeclarativeTextPrivate() { } +qreal QDeclarativeTextPrivate::implicitWidth() const +{ + if (!requireImplicitWidth) { + // We don't calculate implicitWidth unless it is required. + // We need to force a size update now to ensure implicitWidth is calculated + QDeclarativeTextPrivate *me = const_cast<QDeclarativeTextPrivate*>(this); + me->requireImplicitWidth = true; + me->updateSize(); + } + return mImplicitWidth; +} + void QDeclarativeTextPrivate::updateLayout() { Q_Q(QDeclarativeText); @@ -192,15 +226,24 @@ void QDeclarativeTextPrivate::updateLayout() QString tmp = text; tmp.replace(QLatin1Char('\n'), QChar::LineSeparator); singleline = !tmp.contains(QChar::LineSeparator); - if (singleline && elideMode != QDeclarativeText::ElideNone && q->widthValid()) { + if (singleline && !maximumLineCountValid && elideMode != QDeclarativeText::ElideNone && q->widthValid()) { QFontMetrics fm(font); tmp = fm.elidedText(tmp,(Qt::TextElideMode)elideMode,q->width()); // XXX still worth layout...? + if (tmp != text && !truncated) { + truncated = true; + emit q->truncatedChanged(); + } } layout.setText(tmp); } else { singleline = false; QDeclarativeStyledText::parse(text, layout); } + } else { + ensureDoc(); + QDeclarativeTextDocumentLayout *layout = new QDeclarativeTextDocumentLayout(doc); + layout->setLineHeight(lineHeight, lineHeightMode); + doc->setDocumentLayout(layout); } updateSize(); @@ -215,12 +258,20 @@ void QDeclarativeTextPrivate::updateSize() return; } + if (!requireImplicitWidth) { + emit q->implicitWidthChanged(); + // if the implicitWidth is used, then updateSize() has already been called (recursively) + if (requireImplicitWidth) + return; + } + invalidateImageCache(); QFontMetrics fm(font); if (text.isEmpty()) { q->setImplicitWidth(0); q->setImplicitHeight(fm.height()); + paintedSize = QSize(0, fm.height()); emit q->paintedSizeChanged(); q->update(); return; @@ -241,9 +292,14 @@ void QDeclarativeTextPrivate::updateSize() singleline = false; // richtext can't elide or be optimized for single-line case ensureDoc(); doc->setDefaultFont(font); - QTextOption option((Qt::Alignment)int(hAlign | vAlign)); + QTextOption option; + option.setAlignment((Qt::Alignment)int(hAlign | vAlign)); option.setWrapMode(QTextOption::WrapMode(wrapMode)); doc->setDefaultTextOption(option); + if (requireImplicitWidth && q->widthValid()) { + doc->setTextWidth(-1); + naturalWidth = doc->idealWidth(); + } if (wrapMode != QDeclarativeText::NoWrap && q->widthValid()) doc->setTextWidth(q->width()); else @@ -268,10 +324,16 @@ void QDeclarativeTextPrivate::updateSize() //### need to comfirm cost of always setting these for richText internalWidthUpdate = true; - q->setImplicitWidth(size.width()); + if (!q->widthValid()) + q->setImplicitWidth(size.width()); + else if (requireImplicitWidth) + q->setImplicitWidth(naturalWidth); internalWidthUpdate = false; q->setImplicitHeight(size.height()); - emit q->paintedSizeChanged(); + if (paintedSize != size) { + paintedSize = size; + emit q->paintedSizeChanged(); + } q->update(); } @@ -291,25 +353,107 @@ QSize QDeclarativeTextPrivate::setupTextLayout() qreal height = 0; qreal widthUsed = 0; qreal lineWidth = 0; + int visibleTextLength = 0; + int visibleCount = 0; //set manual width if ((wrapMode != QDeclarativeText::NoWrap || elideMode != QDeclarativeText::ElideNone) && q->widthValid()) lineWidth = q->width(); QTextOption textOption = layout.textOption(); + if (hAlign == QDeclarativeText::AlignJustify) + textOption.setAlignment(Qt::Alignment(hAlign)); textOption.setWrapMode(QTextOption::WrapMode(wrapMode)); layout.setTextOption(textOption); - layout.beginLayout(); - forever { - QTextLine line = layout.createLine(); - if (!line.isValid()) - break; + QDeclarativeText::HAlignment hAlignment = hAlign; + if(text.isRightToLeft()) { + if ((hAlign == QDeclarativeText::AlignLeft) || (hAlign == QDeclarativeText::AlignJustify)) { + hAlignment = QDeclarativeText::AlignRight; + } else if (hAlign == QDeclarativeText::AlignRight) { + hAlignment = QDeclarativeText::AlignLeft; + } else { + hAlignment = hAlign; + } + } + + bool elideText = false; + bool truncate = false; + + QFontMetrics fm(layout.font()); + qreal elideWidth = fm.width(elideChar); + elidePos = QPointF(); - if (lineWidth) + if (requireImplicitWidth && q->widthValid()) { + // requires an extra layout + layout.beginLayout(); + forever { + QTextLine line = layout.createLine(); + if (!line.isValid()) + break; + } + layout.endLayout(); + naturalWidth = 0; + for (int i = 0; i < layout.lineCount(); ++i) { + QTextLine line = layout.lineAt(i); + naturalWidth = qMax(naturalWidth, line.naturalTextWidth()); + } + } + + if (maximumLineCountValid) { + layout.beginLayout(); + if (!lineWidth) + lineWidth = INT_MAX; + int y = 0; + int linesLeft = maximumLineCount; + while (linesLeft > 0) { + QTextLine line = layout.createLine(); + if (!line.isValid()) + break; + + visibleCount++; line.setLineWidth(lineWidth); + visibleTextLength += line.textLength(); + + if (--linesLeft == 0) { + if (visibleTextLength < text.length()) { + truncate = true; + if (elideMode==QDeclarativeText::ElideRight && q->widthValid()) { + // Need to correct for alignment + line.setLineWidth(lineWidth-elideWidth); + int x = line.naturalTextWidth(); + if (hAlignment == QDeclarativeText::AlignRight) { + x = q->width()-elideWidth; + } else if (hAlignment == QDeclarativeText::AlignHCenter) { + x = (q->width()+line.naturalTextWidth()-elideWidth)/2; + } + elidePos = QPointF(x, y + fm.ascent()); + elideText = true; + } + } + } + + y += line.height(); + } + layout.endLayout(); + + //Update truncated + if (truncated != truncate) { + truncated = truncate; + emit q->truncatedChanged(); + } + } else { + layout.beginLayout(); + forever { + QTextLine line = layout.createLine(); + if (!line.isValid()) + break; + visibleCount++; + if (lineWidth) + line.setLineWidth(lineWidth); + } + layout.endLayout(); } - layout.endLayout(); for (int i = 0; i < layout.lineCount(); ++i) { QTextLine line = layout.lineAt(i); @@ -317,26 +461,38 @@ QSize QDeclarativeTextPrivate::setupTextLayout() } qreal layoutWidth = q->widthValid() ? q->width() : widthUsed; + if (!q->widthValid()) + naturalWidth = layoutWidth; qreal x = 0; for (int i = 0; i < layout.lineCount(); ++i) { QTextLine line = layout.lineAt(i); line.setPosition(QPointF(0, height)); - height += line.height(); + height += (lineHeightMode == QDeclarativeText::PixelHeight) ? lineHeight : line.height() * lineHeight; if (!cacheAllTextAsImage) { - if (hAlign == QDeclarativeText::AlignLeft) { + if ((hAlignment == QDeclarativeText::AlignLeft) || (hAlignment == QDeclarativeText::AlignJustify)) { x = 0; - } else if (hAlign == QDeclarativeText::AlignRight) { + } else if (hAlignment == QDeclarativeText::AlignRight) { x = layoutWidth - line.naturalTextWidth(); - } else if (hAlign == QDeclarativeText::AlignHCenter) { + if (elideText && i == layout.lineCount()-1) + x -= elideWidth; // Correct for when eliding multilines + } else if (hAlignment == QDeclarativeText::AlignHCenter) { x = (layoutWidth - line.naturalTextWidth()) / 2; + if (elideText && i == layout.lineCount()-1) + x -= elideWidth/2; // Correct for when eliding multilines } line.setPosition(QPointF(x, line.y())); } } - return layout.boundingRect().toAlignedRect().size(); + //Update the number of visible lines + if (lineCount != visibleCount) { + lineCount = visibleCount; + emit q->lineCountChanged(); + } + + return QSize(qCeil(widthUsed), qCeil(height)); } /*! @@ -351,7 +507,7 @@ QPixmap QDeclarativeTextPrivate::textLayoutImage(bool drawStyle) qreal x = 0; for (int i = 0; i < layout.lineCount(); ++i) { QTextLine line = layout.lineAt(i); - if (hAlign == QDeclarativeText::AlignLeft) { + if ((hAlign == QDeclarativeText::AlignLeft) || (hAlign == QDeclarativeText::AlignJustify)) { x = 0; } else if (hAlign == QDeclarativeText::AlignRight) { x = size.width() - line.naturalTextWidth(); @@ -389,7 +545,9 @@ void QDeclarativeTextPrivate::drawTextLayout(QPainter *painter, const QPointF &p else painter->setPen(color); painter->setFont(font); - layout.draw(painter, pos); + layout.draw(painter, pos); + if (!elidePos.isNull()) + painter->drawText(elidePos, elideChar); } /*! @@ -595,7 +753,7 @@ QPixmap QDeclarativeTextPrivate::drawOutline(const QPixmap &source, const QPixma \sa {declarative/text/fonts}{Fonts example} */ QDeclarativeText::QDeclarativeText(QDeclarativeItem *parent) - : QDeclarativeItem(*(new QDeclarativeTextPrivate), parent) + : QDeclarativeImplicitSizeItem(*(new QDeclarativeTextPrivate), parent) { } @@ -928,8 +1086,8 @@ void QDeclarativeText::setStyleColor(const QColor &color) Sets the horizontal and vertical alignment of the text within the Text items width and height. By default, the text is top-left aligned. - The valid values for \c horizontalAlignment are \c Text.AlignLeft, \c Text.AlignRight and - \c Text.AlignHCenter. The valid values for \c verticalAlignment are \c Text.AlignTop, \c Text.AlignBottom + The valid values for \c horizontalAlignment are \c Text.AlignLeft, \c Text.AlignRight, \c Text.AlignHCenter and + \c Text.AlignJustify. The valid values for \c verticalAlignment are \c Text.AlignTop, \c Text.AlignBottom and \c Text.AlignVCenter. Note that for a single line of text, the size of the text is the area of the text. In this common case, @@ -1007,6 +1165,76 @@ void QDeclarativeText::setWrapMode(WrapMode mode) emit wrapModeChanged(); } +/*! + \qmlproperty int Text::lineCount + + Returns the number of lines visible in the text item. + + This property is not supported for rich text. + + \sa maximumLineCount +*/ +int QDeclarativeText::lineCount() const +{ + Q_D(const QDeclarativeText); + return d->lineCount; +} + +/*! + \qmlproperty bool Text::truncated + + Returns true if the text has been truncated due to \l maximumLineCount + or \l elide. + + This property is not supported for rich text. + + \sa maximumLineCount, elide +*/ +bool QDeclarativeText::truncated() const +{ + Q_D(const QDeclarativeText); + return d->truncated; +} + +/*! + \qmlproperty int Text::maximumLineCount + + Set this property to limit the number of lines that the text item will show. + If elide is set to Text.ElideRight, the text will be elided appropriately. + By default, this is the value of the largest possible integer. + + This property is not supported for rich text. + + \sa lineCount, elide +*/ +int QDeclarativeText::maximumLineCount() const +{ + Q_D(const QDeclarativeText); + return d->maximumLineCount; +} + +void QDeclarativeText::setMaximumLineCount(int lines) +{ + Q_D(QDeclarativeText); + + d->maximumLineCountValid = lines==INT_MAX ? false : true; + if (d->maximumLineCount != lines) { + d->maximumLineCount = lines; + d->updateLayout(); + emit maximumLineCountChanged(); + } +} + +void QDeclarativeText::resetMaximumLineCount() +{ + Q_D(QDeclarativeText); + setMaximumLineCount(INT_MAX); + d->elidePos = QPointF(); + if (d->truncated != false) { + d->truncated = false; + emit truncatedChanged(); + } +} /*! \qmlproperty enumeration Text::textFormat @@ -1094,7 +1322,7 @@ void QDeclarativeText::setTextFormat(TextFormat format) Set this property to elide parts of the text fit to the Text item's width. The text will only elide if an explicit width has been set. - This property cannot be used with multi-line text or with rich text. + This property cannot be used with rich text. Eliding can be: \list @@ -1104,6 +1332,9 @@ void QDeclarativeText::setTextFormat(TextFormat format) \o Text.ElideRight \endlist + If this property is set to Text.ElideRight, it can be used with multiline + text. The text will only elide if maximumLineCount has been set. + If the text is a multi-length string, and the mode is not \c Text.ElideNone, the first string that fits will be used, otherwise the last will be elided. @@ -1147,6 +1378,7 @@ QRectF QDeclarativeText::boundingRect() const switch (d->hAlign) { case AlignLeft: + case AlignJustify: x = 0; break; case AlignRight: @@ -1180,7 +1412,7 @@ void QDeclarativeText::geometryChanged(const QRectF &newGeometry, const QRectF & && (d->wrapMode != QDeclarativeText::NoWrap || d->elideMode != QDeclarativeText::ElideNone || d->hAlign != QDeclarativeText::AlignLeft)) { - if (d->singleline && d->elideMode != QDeclarativeText::ElideNone && widthValid()) { + if ((d->singleline || d->maximumLineCountValid) && d->elideMode != QDeclarativeText::ElideNone && widthValid()) { // We need to re-elide d->updateLayout(); } else { @@ -1200,7 +1432,8 @@ void QDeclarativeText::geometryChanged(const QRectF &newGeometry, const QRectF & */ qreal QDeclarativeText::paintedWidth() const { - return implicitWidth(); + Q_D(const QDeclarativeText); + return d->paintedSize.width(); } /*! @@ -1211,7 +1444,64 @@ qreal QDeclarativeText::paintedWidth() const */ qreal QDeclarativeText::paintedHeight() const { - return implicitHeight(); + Q_D(const QDeclarativeText); + return d->paintedSize.height(); +} + +/*! + \qmlproperty real Text::lineHeight + + Sets the line height for the text. + The value can be in pixels or a multiplier depending on lineHeightMode. + + The default value is a multiplier of 1.0. + The line height must be a positive value. +*/ +qreal QDeclarativeText::lineHeight() const +{ + Q_D(const QDeclarativeText); + return d->lineHeight; +} + +void QDeclarativeText::setLineHeight(qreal lineHeight) +{ + Q_D(QDeclarativeText); + + if ((d->lineHeight == lineHeight) || (lineHeight < 0.0)) + return; + + d->lineHeight = lineHeight; + d->updateLayout(); + emit lineHeightChanged(lineHeight); +} + +/*! + \qmlproperty enumeration Text::lineHeightMode + + This property determines how the line height is specified. + The possible values are: + + \list + \o Text.MultiplyHeight (default) - specifies a line height multiplier, + \o Text.PixelHeight - specifies the line height in pixels. + \endlist +*/ +QDeclarativeText::LineHeightMode QDeclarativeText::lineHeightMode() const +{ + Q_D(const QDeclarativeText); + return d->lineHeightMode; +} + +void QDeclarativeText::setLineHeightMode(LineHeightMode mode) +{ + Q_D(QDeclarativeText); + if (mode == d->lineHeightMode) + return; + + d->lineHeightMode = mode; + d->updateLayout(); + + emit lineHeightModeChanged(mode); } /*! diff --git a/src/declarative/graphicsitems/qdeclarativetext_p.h b/src/declarative/graphicsitems/qdeclarativetext_p.h index abc45f9..f3697d5 100644 --- a/src/declarative/graphicsitems/qdeclarativetext_p.h +++ b/src/declarative/graphicsitems/qdeclarativetext_p.h @@ -43,7 +43,7 @@ #define QDECLARATIVETEXT_H #include <QtGui/qtextoption.h> -#include "qdeclarativeitem.h" +#include "qdeclarativeimplicitsizeitem_p.h" #include <private/qdeclarativeglobal_p.h> @@ -53,7 +53,7 @@ QT_BEGIN_NAMESPACE QT_MODULE(Declarative) class QDeclarativeTextPrivate; -class Q_DECLARATIVE_PRIVATE_EXPORT QDeclarativeText : public QDeclarativeItem +class Q_DECLARATIVE_PRIVATE_EXPORT QDeclarativeText : public QDeclarativeImplicitSizeItem { Q_OBJECT Q_ENUMS(HAlignment) @@ -62,6 +62,7 @@ class Q_DECLARATIVE_PRIVATE_EXPORT QDeclarativeText : public QDeclarativeItem Q_ENUMS(TextFormat) Q_ENUMS(TextElideMode) Q_ENUMS(WrapMode) + Q_ENUMS(LineHeightMode) Q_PROPERTY(QString text READ text WRITE setText NOTIFY textChanged) Q_PROPERTY(QFont font READ font WRITE setFont NOTIFY fontChanged) @@ -71,10 +72,16 @@ class Q_DECLARATIVE_PRIVATE_EXPORT QDeclarativeText : public QDeclarativeItem Q_PROPERTY(HAlignment horizontalAlignment READ hAlign WRITE setHAlign NOTIFY horizontalAlignmentChanged) Q_PROPERTY(VAlignment verticalAlignment READ vAlign WRITE setVAlign NOTIFY verticalAlignmentChanged) Q_PROPERTY(WrapMode wrapMode READ wrapMode WRITE setWrapMode NOTIFY wrapModeChanged) + Q_PROPERTY(int lineCount READ lineCount NOTIFY lineCountChanged REVISION 1) + Q_PROPERTY(bool truncated READ truncated NOTIFY truncatedChanged REVISION 1) + Q_PROPERTY(int maximumLineCount READ maximumLineCount WRITE setMaximumLineCount NOTIFY maximumLineCountChanged RESET resetMaximumLineCount REVISION 1) + Q_PROPERTY(TextFormat textFormat READ textFormat WRITE setTextFormat NOTIFY textFormatChanged) Q_PROPERTY(TextElideMode elide READ elideMode WRITE setElideMode NOTIFY elideModeChanged) //### elideMode? Q_PROPERTY(qreal paintedWidth READ paintedWidth NOTIFY paintedSizeChanged) Q_PROPERTY(qreal paintedHeight READ paintedHeight NOTIFY paintedSizeChanged) + Q_PROPERTY(qreal lineHeight READ lineHeight WRITE setLineHeight NOTIFY lineHeightChanged REVISION 1) + Q_PROPERTY(LineHeightMode lineHeightMode READ lineHeightMode WRITE setLineHeightMode NOTIFY lineHeightModeChanged REVISION 1) public: QDeclarativeText(QDeclarativeItem *parent=0); @@ -82,7 +89,8 @@ public: enum HAlignment { AlignLeft = Qt::AlignLeft, AlignRight = Qt::AlignRight, - AlignHCenter = Qt::AlignHCenter }; + AlignHCenter = Qt::AlignHCenter, + AlignJustify = Qt::AlignJustify }; // ### VERSIONING: Only in QtQuick 1.1 enum VAlignment { AlignTop = Qt::AlignTop, AlignBottom = Qt::AlignBottom, AlignVCenter = Qt::AlignVCenter }; @@ -106,6 +114,8 @@ public: Wrap = QTextOption::WrapAtWordBoundaryOrAnywhere }; + enum LineHeightMode { MultiplyHeight, PixelHeight }; + QString text() const; void setText(const QString &); @@ -130,12 +140,25 @@ public: WrapMode wrapMode() const; void setWrapMode(WrapMode w); + int lineCount() const; + bool truncated() const; + + int maximumLineCount() const; + void setMaximumLineCount(int lines); + void resetMaximumLineCount(); + TextFormat textFormat() const; void setTextFormat(TextFormat format); TextElideMode elideMode() const; void setElideMode(TextElideMode); + qreal lineHeight() const; + void setLineHeight(qreal lineHeight); + + LineHeightMode lineHeightMode() const; + void setLineHeightMode(LineHeightMode); + void paint(QPainter *, const QStyleOptionGraphicsItem *, QWidget *); virtual void componentComplete(); @@ -157,9 +180,14 @@ Q_SIGNALS: void horizontalAlignmentChanged(HAlignment alignment); void verticalAlignmentChanged(VAlignment alignment); void wrapModeChanged(); + Q_REVISION(1) void lineCountChanged(); + Q_REVISION(1) void truncatedChanged(); + Q_REVISION(1) void maximumLineCountChanged(); void textFormatChanged(TextFormat textFormat); void elideModeChanged(TextElideMode mode); void paintedSizeChanged(); + Q_REVISION(1) void lineHeightChanged(qreal lineHeight); + Q_REVISION(1) void lineHeightModeChanged(LineHeightMode mode); protected: void mousePressEvent(QGraphicsSceneMouseEvent *event); diff --git a/src/declarative/graphicsitems/qdeclarativetext_p_p.h b/src/declarative/graphicsitems/qdeclarativetext_p_p.h index e749bc9..36ae123 100644 --- a/src/declarative/graphicsitems/qdeclarativetext_p_p.h +++ b/src/declarative/graphicsitems/qdeclarativetext_p_p.h @@ -54,7 +54,7 @@ // #include "qdeclarativeitem.h" -#include "private/qdeclarativeitem_p.h" +#include "private/qdeclarativeimplicitsizeitem_p_p.h" #include "private/qdeclarativetextlayout_p.h" #include <qdeclarative.h> @@ -66,7 +66,7 @@ QT_BEGIN_NAMESPACE class QTextLayout; class QTextDocumentWithImageResources; -class QDeclarativeTextPrivate : public QDeclarativeItemPrivate +class Q_AUTOTEST_EXPORT QDeclarativeTextPrivate : public QDeclarativeImplicitSizeItemPrivate { Q_DECLARE_PUBLIC(QDeclarativeText) public: @@ -89,6 +89,15 @@ public: QDeclarativeText::TextElideMode elideMode; QDeclarativeText::TextFormat format; QDeclarativeText::WrapMode wrapMode; + qreal lineHeight; + QDeclarativeText::LineHeightMode lineHeightMode; + int lineCount; + bool truncated; + int maximumLineCount; + int maximumLineCountValid; + QPointF elidePos; + + static QString elideChar; void invalidateImageCache(); void checkImageCache(); @@ -100,9 +109,12 @@ public: bool singleline:1; bool cacheAllTextAsImage:1; bool internalWidthUpdate:1; + bool requireImplicitWidth:1; QSize layedOutTextSize; - + QSize paintedSize; + qreal naturalWidth; + virtual qreal implicitWidth() const; void ensureDoc(); QPixmap textDocumentImage(bool drawStyle); QTextDocumentWithImageResources *doc; diff --git a/src/declarative/graphicsitems/qdeclarativetextedit.cpp b/src/declarative/graphicsitems/qdeclarativetextedit.cpp index 19c0f2d..39f1465 100644 --- a/src/declarative/graphicsitems/qdeclarativetextedit.cpp +++ b/src/declarative/graphicsitems/qdeclarativetextedit.cpp @@ -51,6 +51,7 @@ #include <QTextLayout> #include <QTextLine> #include <QTextDocument> +#include <QTextObject> #include <QGraphicsSceneMouseEvent> #include <QDebug> #include <QPainter> @@ -103,8 +104,16 @@ TextEdit { \sa Text, TextInput, {declarative/text/textselection}{Text Selection example} */ +/*! + \qmlsignal TextEdit::onLinkActivated(string link) + \since Quick 1.1 + + This handler is called when the user clicks on a link embedded in the text. + The link must be in rich text or HTML format and the + \a link string provides access to the particular link. +*/ QDeclarativeTextEdit::QDeclarativeTextEdit(QDeclarativeItem *parent) -: QDeclarativePaintedItem(*(new QDeclarativeTextEditPrivate), parent) +: QDeclarativeImplicitSizePaintedItem(*(new QDeclarativeTextEditPrivate), parent) { Q_D(QDeclarativeTextEdit); d->init(); @@ -457,6 +466,7 @@ void QDeclarativeTextEdit::setSelectedTextColor(const QColor &color) \o TextEdit.AlignLeft (default) \o TextEdit.AlignRight \o TextEdit.AlignHCenter + \o TextEdit.AlignJustify \endlist Valid values for \c verticalAlignment are: @@ -533,6 +543,17 @@ void QDeclarativeTextEdit::setWrapMode(WrapMode mode) } /*! + \qmlproperty int TextEdit::lineCount + + Returns the total number of lines in the textEdit item. +*/ +int QDeclarativeTextEdit::lineCount() const +{ + Q_D(const QDeclarativeTextEdit); + return d->lineCount; +} + +/*! \qmlproperty real TextEdit::paintedWidth Returns the width of the text, including the width past the width @@ -540,7 +561,8 @@ void QDeclarativeTextEdit::setWrapMode(WrapMode mode) */ qreal QDeclarativeTextEdit::paintedWidth() const { - return implicitWidth(); + Q_D(const QDeclarativeTextEdit); + return d->paintedSize.width(); } /*! @@ -551,7 +573,8 @@ qreal QDeclarativeTextEdit::paintedWidth() const */ qreal QDeclarativeTextEdit::paintedHeight() const { - return implicitHeight(); + Q_D(const QDeclarativeTextEdit); + return d->paintedSize.height(); } /*! @@ -585,37 +608,101 @@ int QDeclarativeTextEdit::positionAt(int x, int y) const return r; } +void QDeclarativeTextEdit::moveCursorSelection(int pos) +{ + //Note that this is the same as setCursorPosition but with the KeepAnchor flag set + Q_D(QDeclarativeTextEdit); + QTextCursor cursor = d->control->textCursor(); + if (cursor.position() == pos) + return; + cursor.setPosition(pos, QTextCursor::KeepAnchor); + d->control->setTextCursor(cursor); +} + /*! - \qmlmethod int TextEdit::moveCursorSelection(int pos) + \qmlmethod void TextEdit::moveCursorSelection(int position, SelectionMode mode = TextEdit.SelectCharacters) + \since Quick 1.1 - Moves the cursor to \a position and updates the selection accordingly. - (To only move the cursor, set the \l cursorPosition property.) + Moves the cursor to \a position and updates the selection according to the optional \a mode + parameter. (To only move the cursor, set the \l cursorPosition property.) When this method is called it additionally sets either the selectionStart or the selectionEnd (whichever was at the previous cursor position) to the specified position. This allows you to easily extend and contract the selected text range. + The selection mode specifies whether the selection is updated on a per character or a per word + basis. If not specified the selection mode will default to TextEdit.SelectCharacters. + + \list + \o TextEdit.SelectCharacters - Sets either the selectionStart or selectionEnd (whichever was at + the previous cursor position) to the specified position. + \o TextEdit.SelectWords - Sets the selectionStart and selectionEnd to include all + words between the specified postion and the previous cursor position. Words partially in the + range are included. + \endlist + For example, take this sequence of calls: \code cursorPosition = 5 - moveCursorSelection(9) - moveCursorSelection(7) + moveCursorSelection(9, TextEdit.SelectCharacters) + moveCursorSelection(7, TextEdit.SelectCharacters) \endcode This moves the cursor to position 5, extend the selection end from 5 to 9 and then retract the selection end from 9 to 7, leaving the text from position 5 to 7 selected (the 6th and 7th characters). + + The same sequence with TextEdit.SelectWords will extend the selection start to a word boundary + before or on position 5 and extend the selection end to a word boundary on or past position 9. */ -void QDeclarativeTextEdit::moveCursorSelection(int pos) +void QDeclarativeTextEdit::moveCursorSelection(int pos, SelectionMode mode) { - //Note that this is the same as setCursorPosition but with the KeepAnchor flag set Q_D(QDeclarativeTextEdit); QTextCursor cursor = d->control->textCursor(); if (cursor.position() == pos) return; - cursor.setPosition(pos, QTextCursor::KeepAnchor); + if (mode == SelectCharacters) { + cursor.setPosition(pos, QTextCursor::KeepAnchor); + } else if (cursor.anchor() < pos || (cursor.anchor() == pos && cursor.position() < pos)) { + if (cursor.anchor() > cursor.position()) { + cursor.setPosition(cursor.anchor(), QTextCursor::MoveAnchor); + cursor.movePosition(QTextCursor::StartOfWord, QTextCursor::KeepAnchor); + if (cursor.position() == cursor.anchor()) + cursor.movePosition(QTextCursor::PreviousWord, QTextCursor::MoveAnchor); + else + cursor.setPosition(cursor.position(), QTextCursor::MoveAnchor); + } else { + cursor.setPosition(cursor.anchor(), QTextCursor::MoveAnchor); + cursor.movePosition(QTextCursor::StartOfWord, QTextCursor::MoveAnchor); + } + + cursor.setPosition(pos, QTextCursor::KeepAnchor); + cursor.movePosition(QTextCursor::StartOfWord, QTextCursor::KeepAnchor); + if (cursor.position() != pos) + cursor.movePosition(QTextCursor::EndOfWord, QTextCursor::KeepAnchor); + } else if (cursor.anchor() > pos || (cursor.anchor() == pos && cursor.position() > pos)) { + if (cursor.anchor() < cursor.position()) { + cursor.setPosition(cursor.anchor(), QTextCursor::MoveAnchor); + cursor.movePosition(QTextCursor::EndOfWord, QTextCursor::MoveAnchor); + } else { + cursor.setPosition(cursor.anchor(), QTextCursor::MoveAnchor); + cursor.movePosition(QTextCursor::PreviousCharacter, QTextCursor::KeepAnchor); + cursor.movePosition(QTextCursor::EndOfWord, QTextCursor::KeepAnchor); + if (cursor.position() != cursor.anchor()) { + cursor.setPosition(cursor.anchor(), QTextCursor::MoveAnchor); + cursor.movePosition(QTextCursor::EndOfWord, QTextCursor::MoveAnchor); + } + } + + cursor.setPosition(pos, QTextCursor::KeepAnchor); + cursor.movePosition(QTextCursor::EndOfWord, QTextCursor::KeepAnchor); + if (cursor.position() != pos) { + cursor.movePosition(QTextCursor::PreviousCharacter, QTextCursor::KeepAnchor); + cursor.movePosition(QTextCursor::StartOfWord, QTextCursor::KeepAnchor); + } + } d->control->setTextCursor(cursor); } @@ -661,7 +748,7 @@ void QDeclarativeTextEdit::setCursorPosition(int pos) if (pos < 0 || pos > d->text.length()) return; QTextCursor cursor = d->control->textCursor(); - if (cursor.position() == pos) + if (cursor.position() == pos && cursor.anchor() == pos) return; cursor.setPosition(pos); d->control->setTextCursor(cursor); @@ -894,6 +981,35 @@ void QDeclarativeTextEdit::setSelectByMouse(bool on) } +/*! + \qmlproperty enum TextEdit::mouseSelectionMode + \since Quick 1.1 + + Specifies how text should be selected using a mouse. + + \list + \o TextEdit.SelectCharacters - The selection is updated with individual characters. (Default) + \o TextEdit.SelectWords - The selection is updated with whole words. + \endlist + + This property only applies when \l selectByMouse is true. +*/ + +QDeclarativeTextEdit::SelectionMode QDeclarativeTextEdit::mouseSelectionMode() const +{ + Q_D(const QDeclarativeTextEdit); + return d->mouseSelectionMode; +} + +void QDeclarativeTextEdit::setMouseSelectionMode(SelectionMode mode) +{ + Q_D(QDeclarativeTextEdit); + if (d->mouseSelectionMode != mode) { + d->mouseSelectionMode = mode; + d->control->setWordSelectionEnabled(mode == SelectWords); + emit mouseSelectionModeChanged(mode); + } +} /*! \qmlproperty bool TextEdit::readOnly @@ -909,12 +1025,13 @@ void QDeclarativeTextEdit::setReadOnly(bool r) if (r == isReadOnly()) return; + setFlag(QGraphicsItem::ItemAcceptsInputMethod, !r); - Qt::TextInteractionFlags flags = Qt::NoTextInteraction; + Qt::TextInteractionFlags flags = Qt::LinksAccessibleByMouse; if (r) { - flags = Qt::TextSelectableByMouse; + flags = flags | Qt::TextSelectableByMouse; } else { - flags = Qt::TextEditorInteraction; + flags = flags | Qt::TextEditorInteraction; } d->control->setTextInteractionFlags(flags); if (!r) @@ -1012,6 +1129,19 @@ void QDeclarativeTextEditPrivate::focusChanged(bool hasFocus) } /*! + \qmlmethod void TextEdit::deselect() + + Removes active text selection. +*/ +void QDeclarativeTextEdit::deselect() +{ + Q_D(QDeclarativeTextEdit); + QTextCursor c = d->control->textCursor(); + c.clearSelection(); + d->control->setTextCursor(c); +} + +/*! \qmlmethod void TextEdit::selectAll() Causes all text to be selected. @@ -1252,6 +1382,21 @@ void QDeclarativeTextEdit::updateImgCache(const QRectF &rf) filtering at the beginning of the animation and reenable it at the conclusion. */ +/*! + \qmlproperty bool TextEdit::canPaste + + Returns true if the TextEdit is writable and the content of the clipboard is + suitable for pasting into the TextEdit. + + \since QtQuick 1.1 +*/ + +bool QDeclarativeTextEdit::canPaste() const +{ + Q_D(const QDeclarativeTextEdit); + return d->canPaste; +} + void QDeclarativeTextEditPrivate::init() { Q_Q(QDeclarativeTextEdit); @@ -1263,6 +1408,8 @@ void QDeclarativeTextEditPrivate::init() control = new QTextControl(q); control->setIgnoreUnusedNavigationEvents(true); + control->setTextInteractionFlags(control->textInteractionFlags() | Qt::LinksAccessibleByMouse); + control->setDragEnabled(false); // QTextControl follows the default text color // defined by the platform, declarative text @@ -1281,6 +1428,11 @@ void QDeclarativeTextEditPrivate::init() QObject::connect(control, SIGNAL(cursorPositionChanged()), q, SLOT(updateSelectionMarkers())); QObject::connect(control, SIGNAL(cursorPositionChanged()), q, SIGNAL(cursorPositionChanged())); QObject::connect(control, SIGNAL(cursorPositionChanged()), q, SIGNAL(cursorRectangleChanged())); + QObject::connect(control, SIGNAL(linkActivated(QString)), q, SIGNAL(linkActivated(QString))); +#ifndef QT_NO_CLIPBOARD + QObject::connect(q, SIGNAL(readOnlyChanged(bool)), q, SLOT(q_canPasteChanged())); + QObject::connect(QApplication::clipboard(), SIGNAL(dataChanged()), q, SLOT(q_canPasteChanged())); +#endif document = control->document(); document->setDefaultFont(font); @@ -1295,6 +1447,7 @@ void QDeclarativeTextEdit::q_textChanged() Q_D(QDeclarativeTextEdit); d->text = text(); updateSize(); + updateTotalLines(); updateMicroFocus(); emit textChanged(d->text); } @@ -1356,6 +1509,17 @@ QRectF QDeclarativeTextEdit::boundingRect() const return r.translated(0,d->yoff); } +qreal QDeclarativeTextEditPrivate::implicitWidth() const +{ + Q_Q(const QDeclarativeTextEdit); + if (!requireImplicitWidth) { + // We don't calculate implicitWidth unless it is required. + // We need to force a size update now to ensure implicitWidth is calculated + const_cast<QDeclarativeTextEditPrivate*>(this)->requireImplicitWidth = true; + const_cast<QDeclarativeTextEdit*>(q)->updateSize(); + } + return mImplicitWidth; +} //### we should perhaps be a bit smarter here -- depending on what has changed, we shouldn't // need to do all the calculations each time @@ -1363,16 +1527,27 @@ void QDeclarativeTextEdit::updateSize() { Q_D(QDeclarativeTextEdit); if (isComponentComplete()) { - QFontMetrics fm = QFontMetrics(d->font); - int dy = height(); + qreal naturalWidth = d->mImplicitWidth; // ### assumes that if the width is set, the text will fill to edges // ### (unless wrap is false, then clipping will occur) if (widthValid()) { + if (!d->requireImplicitWidth) { + emit implicitWidthChanged(); + // if the implicitWidth is used, then updateSize() has already been called (recursively) + if (d->requireImplicitWidth) + return; + } + if (d->requireImplicitWidth) { + d->document->setTextWidth(-1); + naturalWidth = d->document->idealWidth(); + } if (d->document->textWidth() != width()) d->document->setTextWidth(width()); } else { d->document->setTextWidth(-1); } + QFontMetrics fm = QFontMetrics(d->font); + int dy = height(); dy -= (int)d->document->size().height(); int nyoff; @@ -1397,12 +1572,15 @@ void QDeclarativeTextEdit::updateSize() if (!widthValid() && d->document->textWidth() != newWidth) d->document->setTextWidth(newWidth); // ### Text does not align if width is not set (QTextDoc bug) // ### Setting the implicitWidth triggers another updateSize(), and unless there are bindings nothing has changed. - setImplicitWidth(newWidth); + if (!widthValid()) + setImplicitWidth(newWidth); + else if (d->requireImplicitWidth) + setImplicitWidth(naturalWidth); qreal newHeight = d->document->isEmpty() ? fm.height() : (int)d->document->size().height(); setImplicitHeight(newHeight); - setContentsSize(QSize(newWidth, newHeight)); - + d->paintedSize = QSize(newWidth, newHeight); + setContentsSize(d->paintedSize); emit paintedSizeChanged(); } else { d->dirty = true; @@ -1410,6 +1588,26 @@ void QDeclarativeTextEdit::updateSize() emit update(); } +void QDeclarativeTextEdit::updateTotalLines() +{ + Q_D(QDeclarativeTextEdit); + + int subLines = 0; + + for (QTextBlock it = d->document->begin(); it != d->document->end(); it = it.next()) { + QTextLayout *layout = it.layout(); + if (!layout) + continue; + subLines += layout->lineCount()-1; + } + + int newTotalLines = d->document->lineCount() + subLines; + if (d->lineCount != newTotalLines) { + d->lineCount = newTotalLines; + emit lineCountChanged(); + } +} + void QDeclarativeTextEditPrivate::updateDefaultTextOption() { QTextOption opt = document->defaultTextOption(); @@ -1538,4 +1736,13 @@ void QDeclarativeTextEdit::focusInEvent(QFocusEvent *event) QDeclarativePaintedItem::focusInEvent(event); } +void QDeclarativeTextEdit::q_canPasteChanged() +{ + Q_D(QDeclarativeTextEdit); + bool old = d->canPaste; + d->canPaste = d->control->canPaste(); + if(old!=d->canPaste) + emit canPasteChanged(); +} + QT_END_NAMESPACE diff --git a/src/declarative/graphicsitems/qdeclarativetextedit_p.h b/src/declarative/graphicsitems/qdeclarativetextedit_p.h index cff55f0..7785a7a 100644 --- a/src/declarative/graphicsitems/qdeclarativetextedit_p.h +++ b/src/declarative/graphicsitems/qdeclarativetextedit_p.h @@ -43,7 +43,7 @@ #define QDECLARATIVETEXTEDIT_H #include "private/qdeclarativetext_p.h" -#include "private/qdeclarativepainteditem_p.h" +#include "private/qdeclarativeimplicitsizeitem_p.h" #include <QtGui/qtextdocument.h> #include <QtGui/qtextoption.h> @@ -58,13 +58,14 @@ QT_MODULE(Declarative) class QDeclarativeTextEditPrivate; -class Q_AUTOTEST_EXPORT QDeclarativeTextEdit : public QDeclarativePaintedItem +class Q_AUTOTEST_EXPORT QDeclarativeTextEdit : public QDeclarativeImplicitSizePaintedItem { Q_OBJECT Q_ENUMS(VAlignment) Q_ENUMS(HAlignment) Q_ENUMS(TextFormat) Q_ENUMS(WrapMode) + Q_ENUMS(SelectionMode) Q_PROPERTY(QString text READ text WRITE setText NOTIFY textChanged) Q_PROPERTY(QColor color READ color WRITE setColor NOTIFY colorChanged) @@ -74,6 +75,7 @@ class Q_AUTOTEST_EXPORT QDeclarativeTextEdit : public QDeclarativePaintedItem Q_PROPERTY(HAlignment horizontalAlignment READ hAlign WRITE setHAlign NOTIFY horizontalAlignmentChanged) Q_PROPERTY(VAlignment verticalAlignment READ vAlign WRITE setVAlign NOTIFY verticalAlignmentChanged) Q_PROPERTY(WrapMode wrapMode READ wrapMode WRITE setWrapMode NOTIFY wrapModeChanged) + Q_PROPERTY(int lineCount READ lineCount NOTIFY lineCountChanged REVISION 1) Q_PROPERTY(qreal paintedWidth READ paintedWidth NOTIFY paintedSizeChanged) Q_PROPERTY(qreal paintedHeight READ paintedHeight NOTIFY paintedSizeChanged) Q_PROPERTY(TextFormat textFormat READ textFormat WRITE setTextFormat NOTIFY textFormatChanged) @@ -90,6 +92,8 @@ class Q_AUTOTEST_EXPORT QDeclarativeTextEdit : public QDeclarativePaintedItem Q_PROPERTY(qreal textMargin READ textMargin WRITE setTextMargin NOTIFY textMarginChanged) Q_PROPERTY(Qt::InputMethodHints inputMethodHints READ inputMethodHints WRITE setInputMethodHints) Q_PROPERTY(bool selectByMouse READ selectByMouse WRITE setSelectByMouse NOTIFY selectByMouseChanged) + Q_PROPERTY(SelectionMode mouseSelectionMode READ mouseSelectionMode WRITE setMouseSelectionMode NOTIFY mouseSelectionModeChanged REVISION 1) + Q_PROPERTY(bool canPaste READ canPaste NOTIFY canPasteChanged REVISION 1) public: QDeclarativeTextEdit(QDeclarativeItem *parent=0); @@ -97,7 +101,8 @@ public: enum HAlignment { AlignLeft = Qt::AlignLeft, AlignRight = Qt::AlignRight, - AlignHCenter = Qt::AlignHCenter + AlignHCenter = Qt::AlignHCenter, + AlignJustify = Qt::AlignJustify // ### VERSIONING: Only in QtQuick 1.1 }; enum VAlignment { @@ -119,6 +124,11 @@ public: Wrap = QTextOption::WrapAtWordBoundaryOrAnywhere }; + enum SelectionMode { + SelectCharacters, + SelectWords + }; + Q_INVOKABLE void openSoftwareInputPanel(); Q_INVOKABLE void closeSoftwareInputPanel(); @@ -149,6 +159,8 @@ public: WrapMode wrapMode() const; void setWrapMode(WrapMode w); + int lineCount() const; + bool isCursorVisible() const; void setCursorVisible(bool on); @@ -175,6 +187,11 @@ public: bool selectByMouse() const; void setSelectByMouse(bool); + SelectionMode mouseSelectionMode() const; + void setMouseSelectionMode(SelectionMode mode); + + bool canPaste() const; + virtual void componentComplete(); /* FROM EDIT */ @@ -194,6 +211,7 @@ public: Q_INVOKABLE QRectF positionToRectangle(int) const; Q_INVOKABLE int positionAt(int x, int y) const; Q_INVOKABLE void moveCursorSelection(int pos); + Q_INVOKABLE Q_REVISION(1) void moveCursorSelection(int pos, SelectionMode mode); QRectF boundingRect() const; @@ -212,6 +230,7 @@ Q_SIGNALS: void horizontalAlignmentChanged(HAlignment alignment); void verticalAlignmentChanged(VAlignment alignment); void wrapModeChanged(); + void lineCountChanged(); void textFormatChanged(TextFormat textFormat); void readOnlyChanged(bool isReadOnly); void cursorVisibleChanged(bool isCursorVisible); @@ -220,11 +239,15 @@ Q_SIGNALS: void persistentSelectionChanged(bool isPersistentSelection); void textMarginChanged(qreal textMargin); void selectByMouseChanged(bool selectByMouse); + Q_REVISION(1) void mouseSelectionModeChanged(SelectionMode mode); + Q_REVISION(1) void linkActivated(const QString &link); + Q_REVISION(1) void canPasteChanged(); public Q_SLOTS: void selectAll(); void selectWord(); void select(int start, int end); + Q_REVISION(1) void deselect(); #ifndef QT_NO_CLIPBOARD void cut(); void copy(); @@ -237,9 +260,11 @@ private Q_SLOTS: void updateSelectionMarkers(); void moveCursorDelegate(); void loadCursorDelegate(); + void q_canPasteChanged(); private: void updateSize(); + void updateTotalLines(); protected: virtual void geometryChanged(const QRectF &newGeometry, diff --git a/src/declarative/graphicsitems/qdeclarativetextedit_p_p.h b/src/declarative/graphicsitems/qdeclarativetextedit_p_p.h index 3a45541..111cc02 100644 --- a/src/declarative/graphicsitems/qdeclarativetextedit_p_p.h +++ b/src/declarative/graphicsitems/qdeclarativetextedit_p_p.h @@ -54,7 +54,7 @@ // #include "qdeclarativeitem.h" -#include "private/qdeclarativepainteditem_p_p.h" +#include "private/qdeclarativeimplicitsizeitem_p_p.h" #include <qdeclarative.h> @@ -62,7 +62,7 @@ QT_BEGIN_NAMESPACE class QTextLayout; class QTextDocument; class QTextControl; -class QDeclarativeTextEditPrivate : public QDeclarativePaintedItemPrivate +class QDeclarativeTextEditPrivate : public QDeclarativeImplicitSizePaintedItemPrivate { Q_DECLARE_PUBLIC(QDeclarativeTextEdit) @@ -70,10 +70,10 @@ public: QDeclarativeTextEditPrivate() : color("black"), hAlign(QDeclarativeTextEdit::AlignLeft), vAlign(QDeclarativeTextEdit::AlignTop), imgDirty(true), dirty(false), richText(false), cursorVisible(false), focusOnPress(true), - showInputPanelOnFocus(true), clickCausedFocus(false), persistentSelection(true), textMargin(0.0), - lastSelectionStart(0), lastSelectionEnd(0), cursorComponent(0), cursor(0), + showInputPanelOnFocus(true), clickCausedFocus(false), persistentSelection(true), requireImplicitWidth(false), + textMargin(0.0), lastSelectionStart(0), lastSelectionEnd(0), cursorComponent(0), cursor(0), format(QDeclarativeTextEdit::AutoText), document(0), wrapMode(QDeclarativeTextEdit::NoWrap), - selectByMouse(false), + mouseSelectionMode(QDeclarativeTextEdit::SelectCharacters), selectByMouse(false), canPaste(false), yoff(0) { #ifdef Q_OS_SYMBIAN @@ -88,6 +88,7 @@ public: void updateDefaultTextOption(); void relayoutDocument(); void updateSelection(); + qreal implicitWidth() const; void focusChanged(bool); QString text; @@ -110,6 +111,7 @@ public: bool showInputPanelOnFocus : 1; bool clickCausedFocus : 1; bool persistentSelection : 1; + bool requireImplicitWidth:1; qreal textMargin; int lastSelectionStart; int lastSelectionEnd; @@ -119,8 +121,12 @@ public: QTextDocument *document; QTextControl *control; QDeclarativeTextEdit::WrapMode wrapMode; + QDeclarativeTextEdit::SelectionMode mouseSelectionMode; + int lineCount; bool selectByMouse; + bool canPaste; int yoff; + QSize paintedSize; }; QT_END_NAMESPACE diff --git a/src/declarative/graphicsitems/qdeclarativetextinput.cpp b/src/declarative/graphicsitems/qdeclarativetextinput.cpp index 6f02471..6f7d3ff 100644 --- a/src/declarative/graphicsitems/qdeclarativetextinput.cpp +++ b/src/declarative/graphicsitems/qdeclarativetextinput.cpp @@ -50,6 +50,8 @@ #include <QApplication> #include <QFontMetrics> #include <QPainter> +#include <QTextBoundaryFinder> +#include <qstyle.h> #ifndef QT_NO_LINEEDIT @@ -75,7 +77,7 @@ QT_BEGIN_NAMESPACE \sa TextEdit, Text, {declarative/text/textselection}{Text Selection example} */ QDeclarativeTextInput::QDeclarativeTextInput(QDeclarativeItem* parent) - : QDeclarativePaintedItem(*(new QDeclarativeTextInputPrivate), parent) + : QDeclarativeImplicitSizePaintedItem(*(new QDeclarativeTextInputPrivate), parent) { Q_D(QDeclarativeTextInput); d->init(); @@ -353,6 +355,16 @@ void QDeclarativeTextInput::setHAlign(HAlignment align) emit horizontalAlignmentChanged(d->hAlign); } +/*! + \qmlproperty bool TextInput::readOnly + + Sets whether user input can modify the contents of the TextInput. + + If readOnly is set to true, then user input will not affect the text + property. Any bindings or attempts to set the text property will still + work. +*/ + bool QDeclarativeTextInput::isReadOnly() const { Q_D(const QDeclarativeTextInput); @@ -365,11 +377,20 @@ void QDeclarativeTextInput::setReadOnly(bool ro) if (d->control->isReadOnly() == ro) return; + setFlag(QGraphicsItem::ItemAcceptsInputMethod, !ro); d->control->setReadOnly(ro); emit readOnlyChanged(ro); } +/*! + \qmlproperty int TextInput::maximumLength + The maximum permitted length of the text in the TextInput. + + If the text is too long, it is truncated at the limit. + + By default, this property contains a value of 32767. +*/ int QDeclarativeTextInput::maxLength() const { Q_D(const QDeclarativeTextInput); @@ -593,6 +614,11 @@ void QDeclarativeTextInput::setAutoScroll(bool b) \ingroup qml-basic-visual-elements This element provides a validator for integer values. + + IntValidator uses the \l {QLocale::setDefault()}{default locale} to interpret the number and + will accept locale specific digits, group separators, and positive and negative signs. In + addition, IntValidator is always guaranteed to accept a number formatted according to the "C" + locale. */ /*! \qmlproperty int IntValidator::top @@ -1002,7 +1028,7 @@ void QDeclarativeTextInput::mouseMoveEvent(QGraphicsSceneMouseEvent *event) { Q_D(QDeclarativeTextInput); if (d->selectByMouse) { - d->control->moveCursor(d->xToPos(event->pos().x()), true); + moveCursorSelection(d->xToPos(event->pos().x()), d->mouseSelectionMode); event->setAccepted(true); } else { QDeclarativePaintedItem::mouseMoveEvent(event); @@ -1075,10 +1101,11 @@ void QDeclarativeTextInputPrivate::updateHorizontalScroll() int cix = qRound(control->cursorToX()); QRect br(q->boundingRect().toRect()); int widthUsed = calculateTextWidth(); + Qt::Alignment va = QStyle::visualAlignment(control->layoutDirection(), QFlag(Qt::Alignment(hAlign))); if (autoScroll) { if (widthUsed <= br.width()) { // text fits in br; use hscroll for alignment - switch (hAlign & ~(Qt::AlignAbsolute|Qt::AlignVertical_Mask)) { + switch (va & ~(Qt::AlignAbsolute|Qt::AlignVertical_Mask)) { case Qt::AlignRight: hscroll = widthUsed - br.width() - 1; break; @@ -1102,12 +1129,17 @@ void QDeclarativeTextInputPrivate::updateHorizontalScroll() hscroll = widthUsed - br.width() + 1; } } else { - if(hAlign == QDeclarativeTextInput::AlignRight){ + switch (va & ~(Qt::AlignAbsolute|Qt::AlignVertical_Mask)) { + case Qt::AlignRight: hscroll = q->width() - widthUsed; - }else if(hAlign == QDeclarativeTextInput::AlignHCenter){ + break; + case Qt::AlignHCenter: hscroll = (q->width() - widthUsed) / 2; - } else { + break; + default: + // Left hscroll = 0; + break; } } } @@ -1169,6 +1201,17 @@ QVariant QDeclarativeTextInput::inputMethodQuery(Qt::InputMethodQuery property) } /*! + \qmlmethod void TextInput::deselect() + + Removes active text selection. +*/ +void QDeclarativeTextInput::deselect() +{ + Q_D(QDeclarativeTextInput); + d->control->deselect(); +} + +/*! \qmlmethod void TextInput::selectAll() Causes all text to be selected. @@ -1211,7 +1254,8 @@ void QDeclarativeTextInput::copy() void QDeclarativeTextInput::paste() { Q_D(QDeclarativeTextInput); - d->control->paste(); + if(!d->control->isReadOnly()) + d->control->paste(); } #endif // QT_NO_CLIPBOARD @@ -1310,35 +1354,137 @@ void QDeclarativeTextInput::setSelectByMouse(bool on) } } +/*! + \qmlproperty enum TextInput::mouseSelectionMode + \since Quick 1.1 + + Specifies how text should be selected using a mouse. + + \list + \o TextInput.SelectCharacters - The selection is updated with individual characters. (Default) + \o TextInput.SelectWords - The selection is updated with whole words. + \endlist + + This property only applies when \l selectByMouse is true. +*/ + +QDeclarativeTextInput::SelectionMode QDeclarativeTextInput::mouseSelectionMode() const +{ + Q_D(const QDeclarativeTextInput); + return d->mouseSelectionMode; +} + +void QDeclarativeTextInput::setMouseSelectionMode(SelectionMode mode) +{ + Q_D(QDeclarativeTextInput); + if (d->mouseSelectionMode != mode) { + d->mouseSelectionMode = mode; + emit mouseSelectionModeChanged(mode); + } +} + +bool QDeclarativeTextInput::canPaste() const +{ + Q_D(const QDeclarativeTextInput); + return d->canPaste; +} + +void QDeclarativeTextInput::moveCursorSelection(int position) +{ + Q_D(QDeclarativeTextInput); + d->control->moveCursor(position, true); + d->updateHorizontalScroll(); +} /*! - \qmlmethod void TextInput::moveCursorSelection(int position) + \qmlmethod void TextInput::moveCursorSelection(int position, SelectionMode mode = TextInput.SelectCharacters) + \since Quick 1.1 - Moves the cursor to \a position and updates the selection accordingly. - (To only move the cursor, set the \l cursorPosition property.) + Moves the cursor to \a position and updates the selection according to the optional \a mode + parameter. (To only move the cursor, set the \l cursorPosition property.) When this method is called it additionally sets either the selectionStart or the selectionEnd (whichever was at the previous cursor position) to the specified position. This allows you to easily extend and contract the selected text range. + The selection mode specifies whether the selection is updated on a per character or a per word + basis. If not specified the selection mode will default to TextInput.SelectCharacters. + + \list + \o TextEdit.SelectCharacters - Sets either the selectionStart or selectionEnd (whichever was at + the previous cursor position) to the specified position. + \o TextEdit.SelectWords - Sets the selectionStart and selectionEnd to include all + words between the specified postion and the previous cursor position. Words partially in the + range are included. + \endlist + For example, take this sequence of calls: \code cursorPosition = 5 - moveCursorSelection(9) - moveCursorSelection(7) + moveCursorSelection(9, TextInput.SelectCharacters) + moveCursorSelection(7, TextInput.SelectCharacters) \endcode This moves the cursor to position 5, extend the selection end from 5 to 9 and then retract the selection end from 9 to 7, leaving the text from position 5 to 7 selected (the 6th and 7th characters). + + The same sequence with TextInput.SelectWords will extend the selection start to a word boundary + before or on position 5 and extend the selection end to a word boundary on or past position 9. */ -void QDeclarativeTextInput::moveCursorSelection(int position) +void QDeclarativeTextInput::moveCursorSelection(int pos, SelectionMode mode) { Q_D(QDeclarativeTextInput); - d->control->moveCursor(position, true); - d->updateHorizontalScroll(); + + if (mode == SelectCharacters) { + d->control->moveCursor(pos, true); + } else if (pos != d->control->cursor()){ + const int cursor = d->control->cursor(); + int anchor; + if (!d->control->hasSelectedText()) + anchor = d->control->cursor(); + else if (d->control->selectionStart() == d->control->cursor()) + anchor = d->control->selectionEnd(); + else + anchor = d->control->selectionStart(); + + if (anchor < pos || (anchor == pos && cursor < pos)) { + QTextBoundaryFinder finder(QTextBoundaryFinder::Word, d->control->text()); + finder.setPosition(anchor); + + const QTextBoundaryFinder::BoundaryReasons reasons = finder.boundaryReasons(); + if (!(reasons & QTextBoundaryFinder::StartWord) + || ((reasons & QTextBoundaryFinder::EndWord) && anchor > cursor)) { + finder.toPreviousBoundary(); + } + anchor = finder.position(); + + finder.setPosition(pos); + if (!finder.isAtBoundary()) + finder.toNextBoundary(); + + d->control->setSelection(anchor, finder.position() - anchor); + } else if (anchor > pos || (anchor == pos && cursor > pos)) { + QTextBoundaryFinder finder(QTextBoundaryFinder::Word, d->control->text()); + finder.setPosition(anchor); + + const QTextBoundaryFinder::BoundaryReasons reasons = finder.boundaryReasons(); + if (!(reasons & QTextBoundaryFinder::EndWord) + || ((reasons & QTextBoundaryFinder::StartWord) && anchor < cursor)) { + finder.toNextBoundary(); + } + + anchor = finder.position(); + + finder.setPosition(pos); + if (!finder.isAtBoundary()) + finder.toPreviousBoundary(); + + d->control->setSelection(anchor, finder.position() - anchor); + } + } } /*! @@ -1474,6 +1620,12 @@ void QDeclarativeTextInputPrivate::init() q, SIGNAL(accepted())); q->connect(control, SIGNAL(updateNeeded(QRect)), q, SLOT(updateRect(QRect))); +#ifndef QT_NO_CLIPBOARD + q->connect(q, SIGNAL(readOnlyChanged(bool)), + q, SLOT(q_canPasteChanged())); + q->connect(QApplication::clipboard(), SIGNAL(dataChanged()), + q, SLOT(q_canPasteChanged())); +#endif // QT_NO_CLIPBOARD q->updateSize(); oldValidity = control->hasAcceptableInput(); lastSelectionStart = 0; @@ -1575,6 +1727,17 @@ void QDeclarativeTextInput::updateSize(bool needsRedraw) } } +void QDeclarativeTextInput::q_canPasteChanged() +{ + Q_D(QDeclarativeTextInput); + bool old = d->canPaste; +#ifndef QT_NO_CLIPBOARD + d->canPaste = !d->control->isReadOnly() && QApplication::clipboard()->text().length() != 0; +#endif + if(d->canPaste != old) + emit canPasteChanged(); +} + QT_END_NAMESPACE #endif // QT_NO_LINEEDIT diff --git a/src/declarative/graphicsitems/qdeclarativetextinput_p.h b/src/declarative/graphicsitems/qdeclarativetextinput_p.h index abea0e8..63d0e53 100644 --- a/src/declarative/graphicsitems/qdeclarativetextinput_p.h +++ b/src/declarative/graphicsitems/qdeclarativetextinput_p.h @@ -43,7 +43,7 @@ #define QDECLARATIVETEXTINPUT_H #include "private/qdeclarativetext_p.h" -#include "private/qdeclarativepainteditem_p.h" +#include "private/qdeclarativeimplicitsizeitem_p.h" #include <QGraphicsSceneMouseEvent> #include <QIntValidator> @@ -58,11 +58,12 @@ QT_MODULE(Declarative) class QDeclarativeTextInputPrivate; class QValidator; -class Q_AUTOTEST_EXPORT QDeclarativeTextInput : public QDeclarativePaintedItem +class Q_AUTOTEST_EXPORT QDeclarativeTextInput : public QDeclarativeImplicitSizePaintedItem { Q_OBJECT Q_ENUMS(HAlignment) Q_ENUMS(EchoMode) + Q_ENUMS(SelectionMode) Q_PROPERTY(QString text READ text WRITE setText NOTIFY textChanged) Q_PROPERTY(QColor color READ color WRITE setColor NOTIFY colorChanged) @@ -94,6 +95,8 @@ class Q_AUTOTEST_EXPORT QDeclarativeTextInput : public QDeclarativePaintedItem Q_PROPERTY(QString displayText READ displayText NOTIFY displayTextChanged) Q_PROPERTY(bool autoScroll READ autoScroll WRITE setAutoScroll NOTIFY autoScrollChanged) Q_PROPERTY(bool selectByMouse READ selectByMouse WRITE setSelectByMouse NOTIFY selectByMouseChanged) + Q_PROPERTY(SelectionMode mouseSelectionMode READ mouseSelectionMode WRITE setMouseSelectionMode NOTIFY mouseSelectionModeChanged REVISION 1) + Q_PROPERTY(bool canPaste READ canPaste NOTIFY canPasteChanged REVISION 1) public: QDeclarativeTextInput(QDeclarativeItem* parent=0); @@ -112,10 +115,16 @@ public: AlignHCenter = Qt::AlignHCenter }; + enum SelectionMode { + SelectCharacters, + SelectWords + }; + //Auxilliary functions needed to control the TextInput from QML Q_INVOKABLE int positionAt(int x) const; Q_INVOKABLE QRectF positionToRectangle(int pos) const; Q_INVOKABLE void moveCursorSelection(int pos); + Q_INVOKABLE Q_REVISION(1) void moveCursorSelection(int pos, SelectionMode mode); Q_INVOKABLE void openSoftwareInputPanel(); Q_INVOKABLE void closeSoftwareInputPanel(); @@ -184,12 +193,16 @@ public: bool selectByMouse() const; void setSelectByMouse(bool); + SelectionMode mouseSelectionMode() const; + void setMouseSelectionMode(SelectionMode mode); + bool hasAcceptableInput() const; void drawContents(QPainter *p,const QRect &r); QVariant inputMethodQuery(Qt::InputMethodQuery property) const; QRectF boundingRect() const; + bool canPaste() const; Q_SIGNALS: void textChanged(); @@ -216,6 +229,8 @@ Q_SIGNALS: void activeFocusOnPressChanged(bool activeFocusOnPress); void autoScrollChanged(bool autoScroll); void selectByMouseChanged(bool selectByMouse); + Q_REVISION(1) void mouseSelectionModeChanged(SelectionMode mode); + Q_REVISION(1) void canPasteChanged(); protected: virtual void geometryChanged(const QRectF &newGeometry, @@ -234,6 +249,7 @@ public Q_SLOTS: void selectAll(); void selectWord(); void select(int start, int end); + Q_REVISION(1) void deselect(); #ifndef QT_NO_CLIPBOARD void cut(); void copy(); @@ -248,6 +264,7 @@ private Q_SLOTS: void moveCursor(); void cursorPosChanged(); void updateRect(const QRect &r = QRect()); + void q_canPasteChanged(); private: Q_DECLARE_PRIVATE_D(QGraphicsItem::d_ptr.data(), QDeclarativeTextInput) diff --git a/src/declarative/graphicsitems/qdeclarativetextinput_p_p.h b/src/declarative/graphicsitems/qdeclarativetextinput_p_p.h index 16827b3..7a0086e 100644 --- a/src/declarative/graphicsitems/qdeclarativetextinput_p_p.h +++ b/src/declarative/graphicsitems/qdeclarativetextinput_p_p.h @@ -44,7 +44,7 @@ #include "private/qdeclarativetextinput_p.h" -#include "private/qdeclarativepainteditem_p_p.h" +#include "private/qdeclarativeimplicitsizeitem_p_p.h" #include <qdeclarative.h> @@ -66,16 +66,17 @@ QT_BEGIN_NAMESPACE -class QDeclarativeTextInputPrivate : public QDeclarativePaintedItemPrivate +class Q_AUTOTEST_EXPORT QDeclarativeTextInputPrivate : public QDeclarativeImplicitSizePaintedItemPrivate { Q_DECLARE_PUBLIC(QDeclarativeTextInput) public: QDeclarativeTextInputPrivate() : control(new QLineControl(QString())), color((QRgb)0), style(QDeclarativeText::Normal), styleColor((QRgb)0), hAlign(QDeclarativeTextInput::AlignLeft), + mouseSelectionMode(QDeclarativeTextInput::SelectCharacters), hscroll(0), oldScroll(0), focused(false), focusOnPress(true), showInputPanelOnFocus(true), clickCausedFocus(false), cursorVisible(false), - autoScroll(true), selectByMouse(false) + autoScroll(true), selectByMouse(false), canPaste(false) { #ifdef Q_OS_SYMBIAN if (QSysInfo::symbianVersion() == QSysInfo::SV_SF_1 || QSysInfo::symbianVersion() == QSysInfo::SV_SF_3) { @@ -114,6 +115,7 @@ public: QDeclarativeText::TextStyle style; QColor styleColor; QDeclarativeTextInput::HAlignment hAlign; + QDeclarativeTextInput::SelectionMode mouseSelectionMode; QPointer<QDeclarativeComponent> cursorComponent; QPointer<QDeclarativeItem> cursorItem; @@ -131,6 +133,11 @@ public: bool cursorVisible; bool autoScroll; bool selectByMouse; + bool canPaste; + + static inline QDeclarativeTextInputPrivate *get(QDeclarativeTextInput *t) { + return t->d_func(); + } }; QT_END_NAMESPACE diff --git a/src/declarative/graphicsitems/qdeclarativevisualitemmodel.cpp b/src/declarative/graphicsitems/qdeclarativevisualitemmodel.cpp index 065a388..a8082f8 100644 --- a/src/declarative/graphicsitems/qdeclarativevisualitemmodel.cpp +++ b/src/declarative/graphicsitems/qdeclarativevisualitemmodel.cpp @@ -557,8 +557,9 @@ QDeclarativeVisualDataModelData::~QDeclarativeVisualDataModelData() void QDeclarativeVisualDataModelData::ensureProperties() { QDeclarativeVisualDataModelPrivate *modelPriv = QDeclarativeVisualDataModelPrivate::get(m_model); - if (modelPriv->m_metaDataCacheable && !modelPriv->m_metaDataCreated) { - modelPriv->createMetaData(); + if (modelPriv->m_metaDataCacheable) { + if (!modelPriv->m_metaDataCreated) + modelPriv->createMetaData(); if (modelPriv->m_metaDataCreated) m_meta->setCached(true); } @@ -1076,7 +1077,7 @@ QDeclarativeItem *QDeclarativeVisualDataModel::item(int index, const QByteArray } else { delete data; delete ctxt; - qmlInfo(this, d->m_delegate->errors()) << "Error creating delgate"; + qmlInfo(this, d->m_delegate->errors()) << "Error creating delegate"; } } QDeclarativeItem *item = qobject_cast<QDeclarativeItem *>(nobj); |