2011-04-27 12:13:26 +00:00
|
|
|
/****************************************************************************
|
|
|
|
**
|
|
|
|
** 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$
|
|
|
|
** GNU Lesser General Public License Usage
|
2011-07-07 12:52:03 +00:00
|
|
|
** 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.
|
2011-04-27 12:13:26 +00:00
|
|
|
**
|
|
|
|
** In addition, as a special exception, Nokia gives you certain additional
|
2011-07-07 12:52:03 +00:00
|
|
|
** rights. These rights are described in the Nokia Qt LGPL Exception
|
2011-04-27 12:13:26 +00:00
|
|
|
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
|
|
|
|
**
|
2011-07-07 12:52:03 +00:00
|
|
|
** GNU General Public License Usage
|
|
|
|
** Alternatively, this file may be used under the terms of the GNU General
|
|
|
|
** Public License version 3.0 as published by the Free Software Foundation
|
|
|
|
** and appearing in the file LICENSE.GPL included in the packaging of this
|
|
|
|
** file. Please review the following information to ensure the GNU General
|
|
|
|
** Public License version 3.0 requirements will be met:
|
|
|
|
** http://www.gnu.org/copyleft/gpl.html.
|
|
|
|
**
|
|
|
|
** Other Usage
|
|
|
|
** Alternatively, this file may be used in accordance with the terms and
|
|
|
|
** conditions contained in a signed written agreement between you and Nokia.
|
2011-04-27 12:13:26 +00:00
|
|
|
**
|
|
|
|
**
|
|
|
|
**
|
|
|
|
**
|
|
|
|
**
|
|
|
|
** $QT_END_LICENSE$
|
|
|
|
**
|
|
|
|
****************************************************************************/
|
|
|
|
|
2011-10-14 08:51:42 +00:00
|
|
|
#include "qquickpathview_p.h"
|
|
|
|
#include "qquickpathview_p_p.h"
|
|
|
|
#include "qquickcanvas.h"
|
2011-04-27 12:13:26 +00:00
|
|
|
|
|
|
|
#include <private/qdeclarativestate_p.h>
|
|
|
|
#include <private/qdeclarativeopenmetaobject_p.h>
|
|
|
|
#include <private/qlistmodelinterface_p.h>
|
2011-09-01 05:24:34 +00:00
|
|
|
#include <private/qdeclarativechangeset_p.h>
|
2011-04-27 12:13:26 +00:00
|
|
|
|
|
|
|
#include <QtGui/qevent.h>
|
2011-08-30 14:48:57 +00:00
|
|
|
#include <QtGui/qevent.h>
|
2011-08-30 11:17:00 +00:00
|
|
|
#include <QtGui/qguiapplication.h>
|
|
|
|
#include <QtGui/qstylehints.h>
|
2011-04-27 12:13:26 +00:00
|
|
|
#include <QtCore/qmath.h>
|
|
|
|
#include <math.h>
|
|
|
|
|
|
|
|
QT_BEGIN_NAMESPACE
|
|
|
|
|
|
|
|
inline qreal qmlMod(qreal x, qreal y)
|
|
|
|
{
|
|
|
|
#ifdef QT_USE_MATH_H_FLOATS
|
2011-10-17 11:03:58 +00:00
|
|
|
if (sizeof(qreal) == sizeof(float))
|
2011-04-27 12:13:26 +00:00
|
|
|
return fmodf(float(x), float(y));
|
|
|
|
else
|
|
|
|
#endif
|
|
|
|
return fmod(x, y);
|
|
|
|
}
|
|
|
|
|
|
|
|
static QDeclarativeOpenMetaObjectType *qPathViewAttachedType = 0;
|
|
|
|
|
2011-10-14 08:51:42 +00:00
|
|
|
QQuickPathViewAttached::QQuickPathViewAttached(QObject *parent)
|
2011-04-27 12:13:26 +00:00
|
|
|
: QObject(parent), m_percent(-1), m_view(0), m_onPath(false), m_isCurrent(false)
|
|
|
|
{
|
|
|
|
if (qPathViewAttachedType) {
|
|
|
|
m_metaobject = new QDeclarativeOpenMetaObject(this, qPathViewAttachedType);
|
|
|
|
m_metaobject->setCached(true);
|
|
|
|
} else {
|
|
|
|
m_metaobject = new QDeclarativeOpenMetaObject(this);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-10-14 08:51:42 +00:00
|
|
|
QQuickPathViewAttached::~QQuickPathViewAttached()
|
2011-04-27 12:13:26 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2011-10-14 08:51:42 +00:00
|
|
|
QVariant QQuickPathViewAttached::value(const QByteArray &name) const
|
2011-04-27 12:13:26 +00:00
|
|
|
{
|
|
|
|
return m_metaobject->value(name);
|
|
|
|
}
|
2011-10-14 08:51:42 +00:00
|
|
|
void QQuickPathViewAttached::setValue(const QByteArray &name, const QVariant &val)
|
2011-04-27 12:13:26 +00:00
|
|
|
{
|
|
|
|
m_metaobject->setValue(name, val);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-10-14 08:51:42 +00:00
|
|
|
void QQuickPathViewPrivate::init()
|
2011-04-27 12:13:26 +00:00
|
|
|
{
|
2011-10-14 08:51:42 +00:00
|
|
|
Q_Q(QQuickPathView);
|
2011-04-27 12:13:26 +00:00
|
|
|
offset = 0;
|
|
|
|
q->setAcceptedMouseButtons(Qt::LeftButton);
|
2011-10-14 08:51:42 +00:00
|
|
|
q->setFlag(QQuickItem::ItemIsFocusScope);
|
2011-04-27 12:13:26 +00:00
|
|
|
q->setFiltersChildMouseEvents(true);
|
2011-09-22 03:14:00 +00:00
|
|
|
FAST_CONNECT(&tl, SIGNAL(updated()), q, SLOT(ticked()))
|
2011-04-27 12:13:26 +00:00
|
|
|
lastPosTime.invalidate();
|
2011-09-22 03:14:00 +00:00
|
|
|
FAST_CONNECT(&tl, SIGNAL(completed()), q, SLOT(movementEnding()))
|
2011-04-27 12:13:26 +00:00
|
|
|
}
|
|
|
|
|
2011-10-14 08:51:42 +00:00
|
|
|
QQuickItem *QQuickPathViewPrivate::getItem(int modelIndex, bool onPath)
|
2011-04-27 12:13:26 +00:00
|
|
|
{
|
2011-10-14 08:51:42 +00:00
|
|
|
Q_Q(QQuickPathView);
|
2011-04-27 12:13:26 +00:00
|
|
|
requestedIndex = modelIndex;
|
2011-10-14 08:51:42 +00:00
|
|
|
QQuickItem *item = model->item(modelIndex, false);
|
2011-04-27 12:13:26 +00:00
|
|
|
if (item) {
|
|
|
|
if (!attType) {
|
|
|
|
// pre-create one metatype to share with all attached objects
|
2011-10-14 08:51:42 +00:00
|
|
|
attType = new QDeclarativeOpenMetaObjectType(&QQuickPathViewAttached::staticMetaObject, qmlEngine(q));
|
2011-10-17 11:03:58 +00:00
|
|
|
foreach (const QString &attr, path->attributes())
|
2011-04-27 12:13:26 +00:00
|
|
|
attType->createProperty(attr.toUtf8());
|
|
|
|
}
|
|
|
|
qPathViewAttachedType = attType;
|
2011-10-14 08:51:42 +00:00
|
|
|
QQuickPathViewAttached *att = static_cast<QQuickPathViewAttached *>(qmlAttachedPropertiesObject<QQuickPathView>(item));
|
2011-04-27 12:13:26 +00:00
|
|
|
qPathViewAttachedType = 0;
|
|
|
|
if (att) {
|
|
|
|
att->m_view = q;
|
2011-08-01 03:09:10 +00:00
|
|
|
att->setOnPath(onPath);
|
2011-04-27 12:13:26 +00:00
|
|
|
}
|
2011-11-17 01:23:52 +00:00
|
|
|
QDeclarative_setParent_noEvent(item, q);
|
2011-04-27 12:13:26 +00:00
|
|
|
item->setParentItem(q);
|
2011-10-14 08:51:42 +00:00
|
|
|
QQuickItemPrivate *itemPrivate = QQuickItemPrivate::get(item);
|
|
|
|
itemPrivate->addItemChangeListener(this, QQuickItemPrivate::Geometry);
|
2011-04-27 12:13:26 +00:00
|
|
|
}
|
|
|
|
requestedIndex = -1;
|
|
|
|
return item;
|
|
|
|
}
|
|
|
|
|
2011-10-14 08:51:42 +00:00
|
|
|
void QQuickPathViewPrivate::releaseItem(QQuickItem *item)
|
2011-04-27 12:13:26 +00:00
|
|
|
{
|
|
|
|
if (!item || !model)
|
|
|
|
return;
|
2011-10-14 08:51:42 +00:00
|
|
|
QQuickItemPrivate *itemPrivate = QQuickItemPrivate::get(item);
|
|
|
|
itemPrivate->removeItemChangeListener(this, QQuickItemPrivate::Geometry);
|
2011-04-27 12:13:26 +00:00
|
|
|
if (model->release(item) == 0) {
|
|
|
|
// item was not destroyed, and we no longer reference it.
|
2011-10-14 08:51:42 +00:00
|
|
|
if (QQuickPathViewAttached *att = attached(item))
|
2011-04-27 12:13:26 +00:00
|
|
|
att->setOnPath(false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-10-14 08:51:42 +00:00
|
|
|
QQuickPathViewAttached *QQuickPathViewPrivate::attached(QQuickItem *item)
|
2011-04-27 12:13:26 +00:00
|
|
|
{
|
2011-10-14 08:51:42 +00:00
|
|
|
return static_cast<QQuickPathViewAttached *>(qmlAttachedPropertiesObject<QQuickPathView>(item, false));
|
2011-04-27 12:13:26 +00:00
|
|
|
}
|
|
|
|
|
2011-10-14 08:51:42 +00:00
|
|
|
void QQuickPathViewPrivate::clear()
|
2011-04-27 12:13:26 +00:00
|
|
|
{
|
2011-08-01 03:09:10 +00:00
|
|
|
if (currentItem) {
|
|
|
|
releaseItem(currentItem);
|
|
|
|
currentItem = 0;
|
|
|
|
}
|
2011-04-27 12:13:26 +00:00
|
|
|
for (int i=0; i<items.count(); i++){
|
2011-10-14 08:51:42 +00:00
|
|
|
QQuickItem *p = items[i];
|
2011-04-27 12:13:26 +00:00
|
|
|
releaseItem(p);
|
|
|
|
}
|
|
|
|
items.clear();
|
|
|
|
}
|
|
|
|
|
2011-10-14 08:51:42 +00:00
|
|
|
void QQuickPathViewPrivate::updateMappedRange()
|
2011-04-27 12:13:26 +00:00
|
|
|
{
|
|
|
|
if (model && pathItems != -1 && pathItems < modelCount)
|
|
|
|
mappedRange = qreal(pathItems)/modelCount;
|
|
|
|
else
|
|
|
|
mappedRange = 1.0;
|
|
|
|
}
|
|
|
|
|
2011-10-14 08:51:42 +00:00
|
|
|
qreal QQuickPathViewPrivate::positionOfIndex(qreal index) const
|
2011-04-27 12:13:26 +00:00
|
|
|
{
|
|
|
|
qreal pos = -1.0;
|
|
|
|
|
|
|
|
if (model && index >= 0 && index < modelCount) {
|
|
|
|
qreal start = 0.0;
|
2011-10-14 08:51:42 +00:00
|
|
|
if (haveHighlightRange && highlightRangeMode != QQuickPathView::NoHighlightRange)
|
2011-04-27 12:13:26 +00:00
|
|
|
start = highlightRangeStart;
|
|
|
|
qreal globalPos = index + offset;
|
|
|
|
globalPos = qmlMod(globalPos, qreal(modelCount)) / modelCount;
|
|
|
|
if (pathItems != -1 && pathItems < modelCount) {
|
|
|
|
globalPos += start * mappedRange;
|
|
|
|
globalPos = qmlMod(globalPos, 1.0);
|
|
|
|
if (globalPos < mappedRange)
|
|
|
|
pos = globalPos / mappedRange;
|
|
|
|
} else {
|
|
|
|
pos = qmlMod(globalPos + start, 1.0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return pos;
|
|
|
|
}
|
|
|
|
|
2011-10-14 08:51:42 +00:00
|
|
|
void QQuickPathViewPrivate::createHighlight()
|
2011-04-27 12:13:26 +00:00
|
|
|
{
|
2011-10-14 08:51:42 +00:00
|
|
|
Q_Q(QQuickPathView);
|
2011-04-27 12:13:26 +00:00
|
|
|
if (!q->isComponentComplete())
|
|
|
|
return;
|
|
|
|
|
|
|
|
bool changed = false;
|
|
|
|
if (highlightItem) {
|
2011-05-30 06:35:52 +00:00
|
|
|
highlightItem->setParentItem(0);
|
|
|
|
highlightItem->deleteLater();
|
2011-04-27 12:13:26 +00:00
|
|
|
highlightItem = 0;
|
|
|
|
changed = true;
|
|
|
|
}
|
|
|
|
|
2011-10-14 08:51:42 +00:00
|
|
|
QQuickItem *item = 0;
|
2011-04-27 12:13:26 +00:00
|
|
|
if (highlightComponent) {
|
2011-10-14 00:20:08 +00:00
|
|
|
QDeclarativeContext *creationContext = highlightComponent->creationContext();
|
|
|
|
QDeclarativeContext *highlightContext = new QDeclarativeContext(
|
|
|
|
creationContext ? creationContext : qmlContext(q));
|
2011-04-27 12:13:26 +00:00
|
|
|
QObject *nobj = highlightComponent->create(highlightContext);
|
|
|
|
if (nobj) {
|
|
|
|
QDeclarative_setParent_noEvent(highlightContext, nobj);
|
2011-10-14 08:51:42 +00:00
|
|
|
item = qobject_cast<QQuickItem *>(nobj);
|
2011-04-27 12:13:26 +00:00
|
|
|
if (!item)
|
|
|
|
delete nobj;
|
|
|
|
} else {
|
|
|
|
delete highlightContext;
|
|
|
|
}
|
|
|
|
} else {
|
2011-10-14 08:51:42 +00:00
|
|
|
item = new QQuickItem;
|
2011-04-27 12:13:26 +00:00
|
|
|
}
|
|
|
|
if (item) {
|
|
|
|
QDeclarative_setParent_noEvent(item, q);
|
|
|
|
item->setParentItem(q);
|
|
|
|
highlightItem = item;
|
|
|
|
changed = true;
|
|
|
|
}
|
|
|
|
if (changed)
|
|
|
|
emit q->highlightItemChanged();
|
|
|
|
}
|
|
|
|
|
2011-10-14 08:51:42 +00:00
|
|
|
void QQuickPathViewPrivate::updateHighlight()
|
2011-04-27 12:13:26 +00:00
|
|
|
{
|
2011-10-14 08:51:42 +00:00
|
|
|
Q_Q(QQuickPathView);
|
2011-04-27 12:13:26 +00:00
|
|
|
if (!q->isComponentComplete() || !isValid())
|
|
|
|
return;
|
|
|
|
if (highlightItem) {
|
2011-10-14 08:51:42 +00:00
|
|
|
if (haveHighlightRange && highlightRangeMode == QQuickPathView::StrictlyEnforceRange) {
|
2011-04-27 12:13:26 +00:00
|
|
|
updateItem(highlightItem, highlightRangeStart);
|
|
|
|
} else {
|
|
|
|
qreal target = currentIndex;
|
|
|
|
|
|
|
|
offsetAdj = 0.0;
|
|
|
|
tl.reset(moveHighlight);
|
|
|
|
moveHighlight.setValue(highlightPosition);
|
|
|
|
|
|
|
|
const int duration = highlightMoveDuration;
|
|
|
|
|
|
|
|
if (target - highlightPosition > modelCount/2) {
|
|
|
|
highlightUp = false;
|
|
|
|
qreal distance = modelCount - target + highlightPosition;
|
|
|
|
tl.move(moveHighlight, 0.0, QEasingCurve(QEasingCurve::InQuad), int(duration * highlightPosition / distance));
|
|
|
|
tl.set(moveHighlight, modelCount-0.01);
|
|
|
|
tl.move(moveHighlight, target, QEasingCurve(QEasingCurve::OutQuad), int(duration * (modelCount-target) / distance));
|
|
|
|
} else if (target - highlightPosition <= -modelCount/2) {
|
|
|
|
highlightUp = true;
|
|
|
|
qreal distance = modelCount - highlightPosition + target;
|
|
|
|
tl.move(moveHighlight, modelCount-0.01, QEasingCurve(QEasingCurve::InQuad), int(duration * (modelCount-highlightPosition) / distance));
|
|
|
|
tl.set(moveHighlight, 0.0);
|
|
|
|
tl.move(moveHighlight, target, QEasingCurve(QEasingCurve::OutQuad), int(duration * target / distance));
|
|
|
|
} else {
|
|
|
|
highlightUp = highlightPosition - target < 0;
|
|
|
|
tl.move(moveHighlight, target, QEasingCurve(QEasingCurve::InOutQuad), duration);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-10-14 08:51:42 +00:00
|
|
|
void QQuickPathViewPrivate::setHighlightPosition(qreal pos)
|
2011-04-27 12:13:26 +00:00
|
|
|
{
|
|
|
|
if (pos != highlightPosition) {
|
|
|
|
qreal start = 0.0;
|
|
|
|
qreal end = 1.0;
|
2011-10-14 08:51:42 +00:00
|
|
|
if (haveHighlightRange && highlightRangeMode != QQuickPathView::NoHighlightRange) {
|
2011-04-27 12:13:26 +00:00
|
|
|
start = highlightRangeStart;
|
|
|
|
end = highlightRangeEnd;
|
|
|
|
}
|
|
|
|
|
|
|
|
qreal range = qreal(modelCount);
|
|
|
|
// calc normalized position of highlight relative to offset
|
|
|
|
qreal relativeHighlight = qmlMod(pos + offset, range) / range;
|
|
|
|
|
|
|
|
if (!highlightUp && relativeHighlight > end * mappedRange) {
|
|
|
|
qreal diff = 1.0 - relativeHighlight;
|
|
|
|
setOffset(offset + diff * range);
|
|
|
|
} else if (highlightUp && relativeHighlight >= (end - start) * mappedRange) {
|
|
|
|
qreal diff = relativeHighlight - (end - start) * mappedRange;
|
|
|
|
setOffset(offset - diff * range - 0.00001);
|
|
|
|
}
|
|
|
|
|
|
|
|
highlightPosition = pos;
|
|
|
|
qreal pathPos = positionOfIndex(pos);
|
|
|
|
updateItem(highlightItem, pathPos);
|
2011-10-14 08:51:42 +00:00
|
|
|
if (QQuickPathViewAttached *att = attached(highlightItem))
|
2011-04-27 12:13:26 +00:00
|
|
|
att->setOnPath(pathPos != -1.0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-10-14 08:51:42 +00:00
|
|
|
void QQuickPathView::pathUpdated()
|
2011-04-27 12:13:26 +00:00
|
|
|
{
|
2011-10-14 08:51:42 +00:00
|
|
|
Q_D(QQuickPathView);
|
|
|
|
QList<QQuickItem*>::iterator it = d->items.begin();
|
2011-04-27 12:13:26 +00:00
|
|
|
while (it != d->items.end()) {
|
2011-10-14 08:51:42 +00:00
|
|
|
QQuickItem *item = *it;
|
|
|
|
if (QQuickPathViewAttached *att = d->attached(item))
|
2011-04-27 12:13:26 +00:00
|
|
|
att->m_percent = -1;
|
|
|
|
++it;
|
|
|
|
}
|
|
|
|
refill();
|
|
|
|
}
|
|
|
|
|
2011-10-14 08:51:42 +00:00
|
|
|
void QQuickPathViewPrivate::updateItem(QQuickItem *item, qreal percent)
|
2011-04-27 12:13:26 +00:00
|
|
|
{
|
2011-10-14 08:51:42 +00:00
|
|
|
if (QQuickPathViewAttached *att = attached(item)) {
|
2011-04-27 12:13:26 +00:00
|
|
|
if (qFuzzyCompare(att->m_percent, percent))
|
|
|
|
return;
|
|
|
|
att->m_percent = percent;
|
2011-10-17 11:03:58 +00:00
|
|
|
foreach (const QString &attr, path->attributes())
|
2011-04-27 12:13:26 +00:00
|
|
|
att->setValue(attr.toUtf8(), path->attributeAt(attr, percent));
|
|
|
|
}
|
|
|
|
QPointF pf = path->pointAt(percent);
|
|
|
|
item->setX(qRound(pf.x() - item->width()/2));
|
|
|
|
item->setY(qRound(pf.y() - item->height()/2));
|
|
|
|
}
|
|
|
|
|
2011-10-14 08:51:42 +00:00
|
|
|
void QQuickPathViewPrivate::regenerate()
|
2011-04-27 12:13:26 +00:00
|
|
|
{
|
2011-10-14 08:51:42 +00:00
|
|
|
Q_Q(QQuickPathView);
|
2011-04-27 12:13:26 +00:00
|
|
|
if (!q->isComponentComplete())
|
|
|
|
return;
|
|
|
|
|
|
|
|
clear();
|
|
|
|
|
|
|
|
if (!isValid())
|
|
|
|
return;
|
|
|
|
|
|
|
|
firstIndex = -1;
|
|
|
|
updateMappedRange();
|
|
|
|
q->refill();
|
|
|
|
}
|
|
|
|
|
2011-08-08 08:03:31 +00:00
|
|
|
/*!
|
2011-10-14 08:51:42 +00:00
|
|
|
\qmlclass PathView QQuickPathView
|
2011-08-08 08:03:31 +00:00
|
|
|
\inqmlmodule QtQuick 2
|
|
|
|
\ingroup qml-view-elements
|
|
|
|
\brief The PathView element lays out model-provided items on a path.
|
|
|
|
\inherits Item
|
|
|
|
|
|
|
|
A PathView displays data from models created from built-in QML elements like ListModel
|
|
|
|
and XmlListModel, or custom model classes defined in C++ that inherit from
|
|
|
|
QAbstractListModel.
|
|
|
|
|
|
|
|
The view has a \l model, which defines the data to be displayed, and
|
|
|
|
a \l delegate, which defines how the data should be displayed.
|
|
|
|
The \l delegate is instantiated for each item on the \l path.
|
|
|
|
The items may be flicked to move them along the path.
|
|
|
|
|
|
|
|
For example, if there is a simple list model defined in a file \c ContactModel.qml like this:
|
|
|
|
|
|
|
|
\snippet doc/src/snippets/declarative/pathview/ContactModel.qml 0
|
|
|
|
|
|
|
|
This data can be represented as a PathView, like this:
|
|
|
|
|
|
|
|
\snippet doc/src/snippets/declarative/pathview/pathview.qml 0
|
|
|
|
|
|
|
|
\image pathview.gif
|
|
|
|
|
|
|
|
(Note the above example uses PathAttribute to scale and modify the
|
|
|
|
opacity of the items as they rotate. This additional code can be seen in the
|
|
|
|
PathAttribute documentation.)
|
|
|
|
|
|
|
|
PathView does not automatically handle keyboard navigation. This is because
|
|
|
|
the keys to use for navigation will depend upon the shape of the path. Navigation
|
|
|
|
can be added quite simply by setting \c focus to \c true and calling
|
|
|
|
\l decrementCurrentIndex() or \l incrementCurrentIndex(), for example to navigate
|
|
|
|
using the left and right arrow keys:
|
|
|
|
|
|
|
|
\qml
|
|
|
|
PathView {
|
|
|
|
// ...
|
|
|
|
focus: true
|
|
|
|
Keys.onLeftPressed: decrementCurrentIndex()
|
|
|
|
Keys.onRightPressed: incrementCurrentIndex()
|
|
|
|
}
|
|
|
|
\endqml
|
|
|
|
|
|
|
|
The path view itself is a focus scope (see \l{qmlfocus#Acquiring Focus and Focus Scopes}{the focus documentation page} for more details).
|
|
|
|
|
|
|
|
Delegates are instantiated as needed and may be destroyed at any time.
|
|
|
|
State should \e never be stored in a delegate.
|
|
|
|
|
|
|
|
PathView attaches a number of properties to the root item of the delegate, for example
|
|
|
|
\c {PathView.isCurrentItem}. In the following example, the root delegate item can access
|
|
|
|
this attached property directly as \c PathView.isCurrentItem, while the child
|
|
|
|
\c nameText object must refer to this property as \c wrapper.PathView.isCurrentItem.
|
|
|
|
|
|
|
|
\snippet doc/src/snippets/declarative/pathview/pathview.qml 1
|
|
|
|
|
|
|
|
\bold Note that views do not enable \e clip automatically. If the view
|
|
|
|
is not clipped by another item or the screen, it will be necessary
|
|
|
|
to set \e {clip: true} in order to have the out of view items clipped
|
|
|
|
nicely.
|
|
|
|
|
|
|
|
\sa Path, {declarative/modelviews/pathview}{PathView example}
|
|
|
|
*/
|
|
|
|
|
2011-10-14 08:51:42 +00:00
|
|
|
QQuickPathView::QQuickPathView(QQuickItem *parent)
|
|
|
|
: QQuickItem(*(new QQuickPathViewPrivate), parent)
|
2011-04-27 12:13:26 +00:00
|
|
|
{
|
2011-10-14 08:51:42 +00:00
|
|
|
Q_D(QQuickPathView);
|
2011-04-27 12:13:26 +00:00
|
|
|
d->init();
|
|
|
|
}
|
|
|
|
|
2011-10-14 08:51:42 +00:00
|
|
|
QQuickPathView::~QQuickPathView()
|
2011-04-27 12:13:26 +00:00
|
|
|
{
|
2011-10-14 08:51:42 +00:00
|
|
|
Q_D(QQuickPathView);
|
2011-04-27 12:13:26 +00:00
|
|
|
d->clear();
|
|
|
|
if (d->attType)
|
|
|
|
d->attType->release();
|
|
|
|
if (d->ownModel)
|
|
|
|
delete d->model;
|
|
|
|
}
|
|
|
|
|
2011-08-08 08:03:31 +00:00
|
|
|
/*!
|
2011-08-16 23:20:22 +00:00
|
|
|
\qmlattachedproperty PathView QtQuick2::PathView::view
|
2011-08-08 08:03:31 +00:00
|
|
|
This attached property holds the view that manages this delegate instance.
|
|
|
|
|
|
|
|
It is attached to each instance of the delegate.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*!
|
2011-08-16 23:20:22 +00:00
|
|
|
\qmlattachedproperty bool QtQuick2::PathView::onPath
|
2011-08-08 08:03:31 +00:00
|
|
|
This attached property holds whether the item is currently on the path.
|
|
|
|
|
|
|
|
If a pathItemCount has been set, it is possible that some items may
|
|
|
|
be instantiated, but not considered to be currently on the path.
|
|
|
|
Usually, these items would be set invisible, for example:
|
|
|
|
|
|
|
|
\qml
|
|
|
|
Component {
|
|
|
|
Rectangle {
|
|
|
|
visible: PathView.onPath
|
|
|
|
// ...
|
|
|
|
}
|
|
|
|
}
|
|
|
|
\endqml
|
|
|
|
|
|
|
|
It is attached to each instance of the delegate.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*!
|
2011-08-16 23:20:22 +00:00
|
|
|
\qmlattachedproperty bool QtQuick2::PathView::isCurrentItem
|
2011-08-08 08:03:31 +00:00
|
|
|
This attached property is true if this delegate is the current item; otherwise false.
|
|
|
|
|
|
|
|
It is attached to each instance of the delegate.
|
|
|
|
|
|
|
|
This property may be used to adjust the appearance of the current item.
|
|
|
|
|
|
|
|
\snippet doc/src/snippets/declarative/pathview/pathview.qml 1
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*!
|
|
|
|
\qmlproperty model QtQuick2::PathView::model
|
|
|
|
This property holds the model providing data for the view.
|
|
|
|
|
|
|
|
The model provides a set of data that is used to create the items for the view.
|
|
|
|
For large or dynamic datasets the model is usually provided by a C++ model object.
|
|
|
|
Models can also be created directly in QML, using the ListModel element.
|
|
|
|
|
|
|
|
\sa {qmlmodels}{Data Models}
|
|
|
|
*/
|
2011-10-14 08:51:42 +00:00
|
|
|
QVariant QQuickPathView::model() const
|
2011-04-27 12:13:26 +00:00
|
|
|
{
|
2011-10-14 08:51:42 +00:00
|
|
|
Q_D(const QQuickPathView);
|
2011-04-27 12:13:26 +00:00
|
|
|
return d->modelVariant;
|
|
|
|
}
|
|
|
|
|
2011-10-14 08:51:42 +00:00
|
|
|
void QQuickPathView::setModel(const QVariant &model)
|
2011-04-27 12:13:26 +00:00
|
|
|
{
|
2011-10-14 08:51:42 +00:00
|
|
|
Q_D(QQuickPathView);
|
2011-04-27 12:13:26 +00:00
|
|
|
if (d->modelVariant == model)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (d->model) {
|
2011-09-01 05:24:34 +00:00
|
|
|
disconnect(d->model, SIGNAL(modelUpdated(QDeclarativeChangeSet,bool)),
|
|
|
|
this, SLOT(modelUpdated(QDeclarativeChangeSet,bool)));
|
2011-10-14 08:51:42 +00:00
|
|
|
disconnect(d->model, SIGNAL(createdItem(int,QQuickItem*)), this, SLOT(createdItem(int,QQuickItem*)));
|
2011-04-27 12:13:26 +00:00
|
|
|
for (int i=0; i<d->items.count(); i++){
|
2011-10-14 08:51:42 +00:00
|
|
|
QQuickItem *p = d->items[i];
|
2011-11-09 05:34:01 +00:00
|
|
|
d->releaseItem(p);
|
2011-04-27 12:13:26 +00:00
|
|
|
}
|
|
|
|
d->items.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
d->modelVariant = model;
|
|
|
|
QObject *object = qvariant_cast<QObject*>(model);
|
2011-10-14 08:51:42 +00:00
|
|
|
QQuickVisualModel *vim = 0;
|
|
|
|
if (object && (vim = qobject_cast<QQuickVisualModel *>(object))) {
|
2011-04-27 12:13:26 +00:00
|
|
|
if (d->ownModel) {
|
|
|
|
delete d->model;
|
|
|
|
d->ownModel = false;
|
|
|
|
}
|
|
|
|
d->model = vim;
|
|
|
|
} else {
|
|
|
|
if (!d->ownModel) {
|
2011-10-14 08:51:42 +00:00
|
|
|
d->model = new QQuickVisualDataModel(qmlContext(this));
|
2011-04-27 12:13:26 +00:00
|
|
|
d->ownModel = true;
|
2011-10-05 06:03:12 +00:00
|
|
|
if (isComponentComplete())
|
2011-10-14 08:51:42 +00:00
|
|
|
static_cast<QQuickVisualDataModel *>(d->model.data())->componentComplete();
|
2011-04-27 12:13:26 +00:00
|
|
|
}
|
2011-10-14 08:51:42 +00:00
|
|
|
if (QQuickVisualDataModel *dataModel = qobject_cast<QQuickVisualDataModel*>(d->model))
|
2011-04-27 12:13:26 +00:00
|
|
|
dataModel->setModel(model);
|
|
|
|
}
|
|
|
|
d->modelCount = 0;
|
|
|
|
if (d->model) {
|
2011-09-01 05:24:34 +00:00
|
|
|
connect(d->model, SIGNAL(modelUpdated(QDeclarativeChangeSet,bool)),
|
|
|
|
this, SLOT(modelUpdated(QDeclarativeChangeSet,bool)));
|
2011-10-14 08:51:42 +00:00
|
|
|
connect(d->model, SIGNAL(createdItem(int,QQuickItem*)), this, SLOT(createdItem(int,QQuickItem*)));
|
2011-04-27 12:13:26 +00:00
|
|
|
d->modelCount = d->model->count();
|
|
|
|
if (d->model->count())
|
|
|
|
d->offset = qmlMod(d->offset, qreal(d->model->count()));
|
|
|
|
if (d->offset < 0)
|
|
|
|
d->offset = d->model->count() + d->offset;
|
|
|
|
}
|
|
|
|
d->regenerate();
|
2011-07-28 10:11:52 +00:00
|
|
|
if (d->currentIndex < d->modelCount)
|
|
|
|
setOffset(qmlMod(d->modelCount - d->currentIndex, d->modelCount));
|
|
|
|
else
|
|
|
|
d->fixOffset();
|
2011-04-27 12:13:26 +00:00
|
|
|
emit countChanged();
|
|
|
|
emit modelChanged();
|
|
|
|
}
|
|
|
|
|
2011-08-08 08:03:31 +00:00
|
|
|
/*!
|
|
|
|
\qmlproperty int QtQuick2::PathView::count
|
|
|
|
This property holds the number of items in the model.
|
|
|
|
*/
|
2011-10-14 08:51:42 +00:00
|
|
|
int QQuickPathView::count() const
|
2011-04-27 12:13:26 +00:00
|
|
|
{
|
2011-10-14 08:51:42 +00:00
|
|
|
Q_D(const QQuickPathView);
|
2011-04-27 12:13:26 +00:00
|
|
|
return d->model ? d->modelCount : 0;
|
|
|
|
}
|
|
|
|
|
2011-08-08 08:03:31 +00:00
|
|
|
/*!
|
|
|
|
\qmlproperty Path QtQuick2::PathView::path
|
|
|
|
This property holds the path used to lay out the items.
|
|
|
|
For more information see the \l Path documentation.
|
|
|
|
*/
|
2011-10-14 08:51:42 +00:00
|
|
|
QDeclarativePath *QQuickPathView::path() const
|
2011-04-27 12:13:26 +00:00
|
|
|
{
|
2011-10-14 08:51:42 +00:00
|
|
|
Q_D(const QQuickPathView);
|
2011-04-27 12:13:26 +00:00
|
|
|
return d->path;
|
|
|
|
}
|
|
|
|
|
2011-10-14 08:51:42 +00:00
|
|
|
void QQuickPathView::setPath(QDeclarativePath *path)
|
2011-04-27 12:13:26 +00:00
|
|
|
{
|
2011-10-14 08:51:42 +00:00
|
|
|
Q_D(QQuickPathView);
|
2011-04-27 12:13:26 +00:00
|
|
|
if (d->path == path)
|
|
|
|
return;
|
|
|
|
if (d->path)
|
|
|
|
disconnect(d->path, SIGNAL(changed()), this, SLOT(pathUpdated()));
|
|
|
|
d->path = path;
|
|
|
|
connect(d->path, SIGNAL(changed()), this, SLOT(pathUpdated()));
|
|
|
|
if (d->isValid() && isComponentComplete()) {
|
|
|
|
d->clear();
|
|
|
|
if (d->attType) {
|
|
|
|
d->attType->release();
|
|
|
|
d->attType = 0;
|
|
|
|
}
|
|
|
|
d->regenerate();
|
|
|
|
}
|
|
|
|
emit pathChanged();
|
|
|
|
}
|
|
|
|
|
2011-08-08 08:03:31 +00:00
|
|
|
/*!
|
|
|
|
\qmlproperty int QtQuick2::PathView::currentIndex
|
|
|
|
This property holds the index of the current item.
|
|
|
|
*/
|
2011-10-14 08:51:42 +00:00
|
|
|
int QQuickPathView::currentIndex() const
|
2011-04-27 12:13:26 +00:00
|
|
|
{
|
2011-10-14 08:51:42 +00:00
|
|
|
Q_D(const QQuickPathView);
|
2011-04-27 12:13:26 +00:00
|
|
|
return d->currentIndex;
|
|
|
|
}
|
|
|
|
|
2011-10-14 08:51:42 +00:00
|
|
|
void QQuickPathView::setCurrentIndex(int idx)
|
2011-04-27 12:13:26 +00:00
|
|
|
{
|
2011-10-14 08:51:42 +00:00
|
|
|
Q_D(QQuickPathView);
|
2011-04-27 12:13:26 +00:00
|
|
|
if (d->model && d->modelCount)
|
|
|
|
idx = qAbs(idx % d->modelCount);
|
|
|
|
if (d->model && idx != d->currentIndex) {
|
2011-08-01 03:09:10 +00:00
|
|
|
if (d->currentItem) {
|
2011-10-14 08:51:42 +00:00
|
|
|
if (QQuickPathViewAttached *att = d->attached(d->currentItem))
|
2011-08-01 03:09:10 +00:00
|
|
|
att->setIsCurrentItem(false);
|
|
|
|
d->releaseItem(d->currentItem);
|
2011-04-27 12:13:26 +00:00
|
|
|
}
|
|
|
|
d->currentItem = 0;
|
2011-10-14 08:51:42 +00:00
|
|
|
d->moveReason = QQuickPathViewPrivate::SetIndex;
|
2011-04-27 12:13:26 +00:00
|
|
|
d->currentIndex = idx;
|
|
|
|
if (d->modelCount) {
|
|
|
|
int itemIndex = (idx - d->firstIndex + d->modelCount) % d->modelCount;
|
|
|
|
if (itemIndex < d->items.count()) {
|
2011-08-01 03:09:10 +00:00
|
|
|
d->currentItem = d->model->item(d->currentIndex, true);
|
2011-04-27 12:13:26 +00:00
|
|
|
d->currentItem->setFocus(true);
|
2011-10-14 08:51:42 +00:00
|
|
|
if (QQuickPathViewAttached *att = d->attached(d->currentItem))
|
2011-04-27 12:13:26 +00:00
|
|
|
att->setIsCurrentItem(true);
|
2011-08-01 03:09:10 +00:00
|
|
|
} else {
|
|
|
|
d->currentItem = d->getItem(d->currentIndex, false);
|
|
|
|
d->updateItem(d->currentItem, d->currentIndex < d->firstIndex ? 0.0 : 1.0);
|
2011-10-14 08:51:42 +00:00
|
|
|
if (QQuickPathViewAttached *att = d->attached(d->currentItem))
|
2011-08-01 03:09:10 +00:00
|
|
|
att->setIsCurrentItem(true);
|
|
|
|
if (d->model->completePending())
|
|
|
|
d->model->completeItem();
|
2011-04-27 12:13:26 +00:00
|
|
|
}
|
2011-10-14 08:51:42 +00:00
|
|
|
if (d->haveHighlightRange && d->highlightRangeMode == QQuickPathView::StrictlyEnforceRange)
|
2011-08-01 03:09:10 +00:00
|
|
|
d->snapToCurrent();
|
2011-04-27 12:13:26 +00:00
|
|
|
d->currentItemOffset = d->positionOfIndex(d->currentIndex);
|
|
|
|
d->updateHighlight();
|
|
|
|
}
|
|
|
|
emit currentIndexChanged();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-10-14 08:51:42 +00:00
|
|
|
QQuickItem *QQuickPathView::currentItem() const
|
2011-08-01 03:09:10 +00:00
|
|
|
{
|
2011-10-14 08:51:42 +00:00
|
|
|
Q_D(const QQuickPathView);
|
2011-08-01 03:09:10 +00:00
|
|
|
return d->currentItem;
|
|
|
|
}
|
|
|
|
|
2011-08-08 08:03:31 +00:00
|
|
|
/*!
|
|
|
|
\qmlmethod QtQuick2::PathView::incrementCurrentIndex()
|
|
|
|
|
|
|
|
Increments the current index.
|
|
|
|
|
|
|
|
\bold Note: methods should only be called after the Component has completed.
|
|
|
|
*/
|
2011-10-14 08:51:42 +00:00
|
|
|
void QQuickPathView::incrementCurrentIndex()
|
2011-04-27 12:13:26 +00:00
|
|
|
{
|
2011-10-14 08:51:42 +00:00
|
|
|
Q_D(QQuickPathView);
|
|
|
|
d->moveDirection = QQuickPathViewPrivate::Positive;
|
2011-04-27 12:13:26 +00:00
|
|
|
setCurrentIndex(currentIndex()+1);
|
|
|
|
}
|
|
|
|
|
2011-08-08 08:03:31 +00:00
|
|
|
/*!
|
|
|
|
\qmlmethod QtQuick2::PathView::decrementCurrentIndex()
|
|
|
|
|
|
|
|
Decrements the current index.
|
|
|
|
|
|
|
|
\bold Note: methods should only be called after the Component has completed.
|
|
|
|
*/
|
2011-10-14 08:51:42 +00:00
|
|
|
void QQuickPathView::decrementCurrentIndex()
|
2011-04-27 12:13:26 +00:00
|
|
|
{
|
2011-10-14 08:51:42 +00:00
|
|
|
Q_D(QQuickPathView);
|
2011-04-27 12:13:26 +00:00
|
|
|
if (d->model && d->modelCount) {
|
|
|
|
int idx = currentIndex()-1;
|
|
|
|
if (idx < 0)
|
|
|
|
idx = d->modelCount - 1;
|
2011-10-14 08:51:42 +00:00
|
|
|
d->moveDirection = QQuickPathViewPrivate::Negative;
|
2011-04-27 12:13:26 +00:00
|
|
|
setCurrentIndex(idx);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-08-08 08:03:31 +00:00
|
|
|
/*!
|
|
|
|
\qmlproperty real QtQuick2::PathView::offset
|
|
|
|
|
|
|
|
The offset specifies how far along the path the items are from their initial positions.
|
|
|
|
This is a real number that ranges from 0.0 to the count of items in the model.
|
|
|
|
*/
|
2011-10-14 08:51:42 +00:00
|
|
|
qreal QQuickPathView::offset() const
|
2011-04-27 12:13:26 +00:00
|
|
|
{
|
2011-10-14 08:51:42 +00:00
|
|
|
Q_D(const QQuickPathView);
|
2011-04-27 12:13:26 +00:00
|
|
|
return d->offset;
|
|
|
|
}
|
|
|
|
|
2011-10-14 08:51:42 +00:00
|
|
|
void QQuickPathView::setOffset(qreal offset)
|
2011-04-27 12:13:26 +00:00
|
|
|
{
|
2011-10-14 08:51:42 +00:00
|
|
|
Q_D(QQuickPathView);
|
2011-04-27 12:13:26 +00:00
|
|
|
d->setOffset(offset);
|
|
|
|
d->updateCurrent();
|
|
|
|
}
|
|
|
|
|
2011-10-14 08:51:42 +00:00
|
|
|
void QQuickPathViewPrivate::setOffset(qreal o)
|
2011-04-27 12:13:26 +00:00
|
|
|
{
|
2011-10-14 08:51:42 +00:00
|
|
|
Q_Q(QQuickPathView);
|
2011-04-27 12:13:26 +00:00
|
|
|
if (offset != o) {
|
|
|
|
if (isValid() && q->isComponentComplete()) {
|
|
|
|
offset = qmlMod(o, qreal(modelCount));
|
|
|
|
if (offset < 0)
|
|
|
|
offset += qreal(modelCount);
|
|
|
|
q->refill();
|
|
|
|
} else {
|
|
|
|
offset = o;
|
|
|
|
}
|
|
|
|
emit q->offsetChanged();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-10-14 08:51:42 +00:00
|
|
|
void QQuickPathViewPrivate::setAdjustedOffset(qreal o)
|
2011-04-27 12:13:26 +00:00
|
|
|
{
|
|
|
|
setOffset(o+offsetAdj);
|
|
|
|
}
|
|
|
|
|
2011-08-08 08:03:31 +00:00
|
|
|
/*!
|
|
|
|
\qmlproperty Component QtQuick2::PathView::highlight
|
|
|
|
This property holds the component to use as the highlight.
|
|
|
|
|
|
|
|
An instance of the highlight component will be created for each view.
|
|
|
|
The geometry of the resultant component instance will be managed by the view
|
|
|
|
so as to stay with the current item.
|
|
|
|
|
|
|
|
The below example demonstrates how to make a simple highlight. Note the use
|
|
|
|
of the \l{PathView::onPath}{PathView.onPath} attached property to ensure that
|
|
|
|
the highlight is hidden when flicked away from the path.
|
|
|
|
|
|
|
|
\qml
|
|
|
|
Component {
|
|
|
|
Rectangle {
|
|
|
|
visible: PathView.onPath
|
|
|
|
// ...
|
|
|
|
}
|
|
|
|
}
|
|
|
|
\endqml
|
|
|
|
|
|
|
|
\sa highlightItem, highlightRangeMode
|
|
|
|
*/
|
|
|
|
|
2011-10-14 08:51:42 +00:00
|
|
|
QDeclarativeComponent *QQuickPathView::highlight() const
|
2011-04-27 12:13:26 +00:00
|
|
|
{
|
2011-10-14 08:51:42 +00:00
|
|
|
Q_D(const QQuickPathView);
|
2011-04-27 12:13:26 +00:00
|
|
|
return d->highlightComponent;
|
|
|
|
}
|
|
|
|
|
2011-10-14 08:51:42 +00:00
|
|
|
void QQuickPathView::setHighlight(QDeclarativeComponent *highlight)
|
2011-04-27 12:13:26 +00:00
|
|
|
{
|
2011-10-14 08:51:42 +00:00
|
|
|
Q_D(QQuickPathView);
|
2011-04-27 12:13:26 +00:00
|
|
|
if (highlight != d->highlightComponent) {
|
|
|
|
d->highlightComponent = highlight;
|
|
|
|
d->createHighlight();
|
|
|
|
d->updateHighlight();
|
|
|
|
emit highlightChanged();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-08-08 08:03:31 +00:00
|
|
|
/*!
|
|
|
|
\qmlproperty Item QtQuick2::PathView::highlightItem
|
|
|
|
|
|
|
|
\c highlightItem holds the highlight item, which was created
|
|
|
|
from the \l highlight component.
|
|
|
|
|
|
|
|
\sa highlight
|
|
|
|
*/
|
2011-10-14 08:51:42 +00:00
|
|
|
QQuickItem *QQuickPathView::highlightItem()
|
2011-04-27 12:13:26 +00:00
|
|
|
{
|
2011-10-14 08:51:42 +00:00
|
|
|
Q_D(const QQuickPathView);
|
2011-04-27 12:13:26 +00:00
|
|
|
return d->highlightItem;
|
|
|
|
}
|
2011-08-08 08:03:31 +00:00
|
|
|
/*!
|
|
|
|
\qmlproperty real QtQuick2::PathView::preferredHighlightBegin
|
|
|
|
\qmlproperty real QtQuick2::PathView::preferredHighlightEnd
|
|
|
|
\qmlproperty enumeration QtQuick2::PathView::highlightRangeMode
|
|
|
|
|
|
|
|
These properties set the preferred range of the highlight (current item)
|
|
|
|
within the view. The preferred values must be in the range 0.0-1.0.
|
|
|
|
|
|
|
|
If highlightRangeMode is set to \e PathView.NoHighlightRange
|
|
|
|
|
|
|
|
If highlightRangeMode is set to \e PathView.ApplyRange the view will
|
|
|
|
attempt to maintain the highlight within the range, however
|
|
|
|
the highlight can move outside of the range at the ends of the path
|
|
|
|
or due to a mouse interaction.
|
|
|
|
|
|
|
|
If highlightRangeMode is set to \e PathView.StrictlyEnforceRange the highlight will never
|
|
|
|
move outside of the range. This means that the current item will change
|
|
|
|
if a keyboard or mouse action would cause the highlight to move
|
|
|
|
outside of the range.
|
|
|
|
|
|
|
|
Note that this is the correct way to influence where the
|
|
|
|
current item ends up when the view moves. For example, if you want the
|
|
|
|
currently selected item to be in the middle of the path, then set the
|
|
|
|
highlight range to be 0.5,0.5 and highlightRangeMode to PathView.StrictlyEnforceRange.
|
|
|
|
Then, when the path scrolls,
|
|
|
|
the currently selected item will be the item at that position. This also applies to
|
|
|
|
when the currently selected item changes - it will scroll to within the preferred
|
|
|
|
highlight range. Furthermore, the behaviour of the current item index will occur
|
|
|
|
whether or not a highlight exists.
|
|
|
|
|
|
|
|
The default value is \e PathView.StrictlyEnforceRange.
|
|
|
|
|
|
|
|
Note that a valid range requires preferredHighlightEnd to be greater
|
|
|
|
than or equal to preferredHighlightBegin.
|
|
|
|
*/
|
2011-10-14 08:51:42 +00:00
|
|
|
qreal QQuickPathView::preferredHighlightBegin() const
|
2011-04-27 12:13:26 +00:00
|
|
|
{
|
2011-10-14 08:51:42 +00:00
|
|
|
Q_D(const QQuickPathView);
|
2011-04-27 12:13:26 +00:00
|
|
|
return d->highlightRangeStart;
|
|
|
|
}
|
|
|
|
|
2011-10-14 08:51:42 +00:00
|
|
|
void QQuickPathView::setPreferredHighlightBegin(qreal start)
|
2011-04-27 12:13:26 +00:00
|
|
|
{
|
2011-10-14 08:51:42 +00:00
|
|
|
Q_D(QQuickPathView);
|
2011-04-27 12:13:26 +00:00
|
|
|
if (d->highlightRangeStart == start || start < 0 || start > 1.0)
|
|
|
|
return;
|
|
|
|
d->highlightRangeStart = start;
|
|
|
|
d->haveHighlightRange = d->highlightRangeMode != NoHighlightRange && d->highlightRangeStart <= d->highlightRangeEnd;
|
|
|
|
refill();
|
|
|
|
emit preferredHighlightBeginChanged();
|
|
|
|
}
|
|
|
|
|
2011-10-14 08:51:42 +00:00
|
|
|
qreal QQuickPathView::preferredHighlightEnd() const
|
2011-04-27 12:13:26 +00:00
|
|
|
{
|
2011-10-14 08:51:42 +00:00
|
|
|
Q_D(const QQuickPathView);
|
2011-04-27 12:13:26 +00:00
|
|
|
return d->highlightRangeEnd;
|
|
|
|
}
|
|
|
|
|
2011-10-14 08:51:42 +00:00
|
|
|
void QQuickPathView::setPreferredHighlightEnd(qreal end)
|
2011-04-27 12:13:26 +00:00
|
|
|
{
|
2011-10-14 08:51:42 +00:00
|
|
|
Q_D(QQuickPathView);
|
2011-04-27 12:13:26 +00:00
|
|
|
if (d->highlightRangeEnd == end || end < 0 || end > 1.0)
|
|
|
|
return;
|
|
|
|
d->highlightRangeEnd = end;
|
|
|
|
d->haveHighlightRange = d->highlightRangeMode != NoHighlightRange && d->highlightRangeStart <= d->highlightRangeEnd;
|
|
|
|
refill();
|
|
|
|
emit preferredHighlightEndChanged();
|
|
|
|
}
|
|
|
|
|
2011-10-14 08:51:42 +00:00
|
|
|
QQuickPathView::HighlightRangeMode QQuickPathView::highlightRangeMode() const
|
2011-04-27 12:13:26 +00:00
|
|
|
{
|
2011-10-14 08:51:42 +00:00
|
|
|
Q_D(const QQuickPathView);
|
2011-04-27 12:13:26 +00:00
|
|
|
return d->highlightRangeMode;
|
|
|
|
}
|
|
|
|
|
2011-10-14 08:51:42 +00:00
|
|
|
void QQuickPathView::setHighlightRangeMode(HighlightRangeMode mode)
|
2011-04-27 12:13:26 +00:00
|
|
|
{
|
2011-10-14 08:51:42 +00:00
|
|
|
Q_D(QQuickPathView);
|
2011-04-27 12:13:26 +00:00
|
|
|
if (d->highlightRangeMode == mode)
|
|
|
|
return;
|
|
|
|
d->highlightRangeMode = mode;
|
|
|
|
d->haveHighlightRange = d->highlightRangeMode != NoHighlightRange && d->highlightRangeStart <= d->highlightRangeEnd;
|
|
|
|
emit highlightRangeModeChanged();
|
|
|
|
}
|
|
|
|
|
2011-08-08 08:03:31 +00:00
|
|
|
/*!
|
|
|
|
\qmlproperty int QtQuick2::PathView::highlightMoveDuration
|
|
|
|
This property holds the move animation duration of the highlight delegate.
|
|
|
|
|
|
|
|
If the highlightRangeMode is StrictlyEnforceRange then this property
|
|
|
|
determines the speed that the items move along the path.
|
|
|
|
|
|
|
|
The default value for the duration is 300ms.
|
|
|
|
*/
|
2011-10-14 08:51:42 +00:00
|
|
|
int QQuickPathView::highlightMoveDuration() const
|
2011-04-27 12:13:26 +00:00
|
|
|
{
|
2011-10-14 08:51:42 +00:00
|
|
|
Q_D(const QQuickPathView);
|
2011-04-27 12:13:26 +00:00
|
|
|
return d->highlightMoveDuration;
|
|
|
|
}
|
|
|
|
|
2011-10-14 08:51:42 +00:00
|
|
|
void QQuickPathView::setHighlightMoveDuration(int duration)
|
2011-04-27 12:13:26 +00:00
|
|
|
{
|
2011-10-14 08:51:42 +00:00
|
|
|
Q_D(QQuickPathView);
|
2011-04-27 12:13:26 +00:00
|
|
|
if (d->highlightMoveDuration == duration)
|
|
|
|
return;
|
|
|
|
d->highlightMoveDuration = duration;
|
|
|
|
emit highlightMoveDurationChanged();
|
|
|
|
}
|
|
|
|
|
2011-08-08 08:03:31 +00:00
|
|
|
/*!
|
|
|
|
\qmlproperty real QtQuick2::PathView::dragMargin
|
|
|
|
This property holds the maximum distance from the path that initiate mouse dragging.
|
|
|
|
|
|
|
|
By default the path can only be dragged by clicking on an item. If
|
|
|
|
dragMargin is greater than zero, a drag can be initiated by clicking
|
|
|
|
within dragMargin pixels of the path.
|
|
|
|
*/
|
2011-10-14 08:51:42 +00:00
|
|
|
qreal QQuickPathView::dragMargin() const
|
2011-04-27 12:13:26 +00:00
|
|
|
{
|
2011-10-14 08:51:42 +00:00
|
|
|
Q_D(const QQuickPathView);
|
2011-04-27 12:13:26 +00:00
|
|
|
return d->dragMargin;
|
|
|
|
}
|
|
|
|
|
2011-10-14 08:51:42 +00:00
|
|
|
void QQuickPathView::setDragMargin(qreal dragMargin)
|
2011-04-27 12:13:26 +00:00
|
|
|
{
|
2011-10-14 08:51:42 +00:00
|
|
|
Q_D(QQuickPathView);
|
2011-04-27 12:13:26 +00:00
|
|
|
if (d->dragMargin == dragMargin)
|
|
|
|
return;
|
|
|
|
d->dragMargin = dragMargin;
|
|
|
|
emit dragMarginChanged();
|
|
|
|
}
|
|
|
|
|
2011-08-08 08:03:31 +00:00
|
|
|
/*!
|
|
|
|
\qmlproperty real QtQuick2::PathView::flickDeceleration
|
|
|
|
This property holds the rate at which a flick will decelerate.
|
|
|
|
|
|
|
|
The default is 100.
|
|
|
|
*/
|
2011-10-14 08:51:42 +00:00
|
|
|
qreal QQuickPathView::flickDeceleration() const
|
2011-04-27 12:13:26 +00:00
|
|
|
{
|
2011-10-14 08:51:42 +00:00
|
|
|
Q_D(const QQuickPathView);
|
2011-04-27 12:13:26 +00:00
|
|
|
return d->deceleration;
|
|
|
|
}
|
|
|
|
|
2011-10-14 08:51:42 +00:00
|
|
|
void QQuickPathView::setFlickDeceleration(qreal dec)
|
2011-04-27 12:13:26 +00:00
|
|
|
{
|
2011-10-14 08:51:42 +00:00
|
|
|
Q_D(QQuickPathView);
|
2011-04-27 12:13:26 +00:00
|
|
|
if (d->deceleration == dec)
|
|
|
|
return;
|
|
|
|
d->deceleration = dec;
|
|
|
|
emit flickDecelerationChanged();
|
|
|
|
}
|
|
|
|
|
2011-08-08 08:03:31 +00:00
|
|
|
/*!
|
|
|
|
\qmlproperty bool QtQuick2::PathView::interactive
|
|
|
|
|
|
|
|
A user cannot drag or flick a PathView that is not interactive.
|
|
|
|
|
|
|
|
This property is useful for temporarily disabling flicking. This allows
|
|
|
|
special interaction with PathView's children.
|
|
|
|
*/
|
2011-10-14 08:51:42 +00:00
|
|
|
bool QQuickPathView::isInteractive() const
|
2011-04-27 12:13:26 +00:00
|
|
|
{
|
2011-10-14 08:51:42 +00:00
|
|
|
Q_D(const QQuickPathView);
|
2011-04-27 12:13:26 +00:00
|
|
|
return d->interactive;
|
|
|
|
}
|
|
|
|
|
2011-10-14 08:51:42 +00:00
|
|
|
void QQuickPathView::setInteractive(bool interactive)
|
2011-04-27 12:13:26 +00:00
|
|
|
{
|
2011-10-14 08:51:42 +00:00
|
|
|
Q_D(QQuickPathView);
|
2011-04-27 12:13:26 +00:00
|
|
|
if (interactive != d->interactive) {
|
|
|
|
d->interactive = interactive;
|
|
|
|
if (!interactive)
|
|
|
|
d->tl.clear();
|
|
|
|
emit interactiveChanged();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-08-08 08:03:31 +00:00
|
|
|
/*!
|
|
|
|
\qmlproperty bool QtQuick2::PathView::moving
|
|
|
|
|
|
|
|
This property holds whether the view is currently moving
|
|
|
|
due to the user either dragging or flicking the view.
|
|
|
|
*/
|
2011-10-14 08:51:42 +00:00
|
|
|
bool QQuickPathView::isMoving() const
|
2011-04-27 12:13:26 +00:00
|
|
|
{
|
2011-10-14 08:51:42 +00:00
|
|
|
Q_D(const QQuickPathView);
|
2011-04-27 12:13:26 +00:00
|
|
|
return d->moving;
|
|
|
|
}
|
|
|
|
|
2011-08-08 08:03:31 +00:00
|
|
|
/*!
|
|
|
|
\qmlproperty bool QtQuick2::PathView::flicking
|
|
|
|
|
|
|
|
This property holds whether the view is currently moving
|
|
|
|
due to the user flicking the view.
|
|
|
|
*/
|
2011-10-14 08:51:42 +00:00
|
|
|
bool QQuickPathView::isFlicking() const
|
2011-04-27 12:13:26 +00:00
|
|
|
{
|
2011-10-14 08:51:42 +00:00
|
|
|
Q_D(const QQuickPathView);
|
2011-04-27 12:13:26 +00:00
|
|
|
return d->flicking;
|
|
|
|
}
|
|
|
|
|
2011-08-08 08:03:31 +00:00
|
|
|
/*!
|
|
|
|
\qmlsignal QtQuick2::PathView::onMovementStarted()
|
|
|
|
|
|
|
|
This handler is called when the view begins moving due to user
|
|
|
|
interaction.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*!
|
|
|
|
\qmlsignal QtQuick2::PathView::onMovementEnded()
|
|
|
|
|
|
|
|
This handler is called when the view stops moving due to user
|
|
|
|
interaction. If a flick was generated, this handler will
|
|
|
|
be triggered once the flick stops. If a flick was not
|
|
|
|
generated, the handler will be triggered when the
|
|
|
|
user stops dragging - i.e. a mouse or touch release.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*!
|
|
|
|
\qmlsignal QtQuick2::PathView::onFlickStarted()
|
|
|
|
|
|
|
|
This handler is called when the view is flicked. A flick
|
|
|
|
starts from the point that the mouse or touch is released,
|
|
|
|
while still in motion.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*!
|
|
|
|
\qmlsignal QtQuick2::PathView::onFlickEnded()
|
|
|
|
|
|
|
|
This handler is called when the view stops moving due to a flick.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*!
|
|
|
|
\qmlproperty Component QtQuick2::PathView::delegate
|
|
|
|
|
|
|
|
The delegate provides a template defining each item instantiated by the view.
|
|
|
|
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}.
|
|
|
|
|
|
|
|
The number of elements in the delegate has a direct effect on the
|
|
|
|
flicking performance of the view when pathItemCount is specified. If at all possible, place functionality
|
|
|
|
that is not needed for the normal display of the delegate in a \l Loader which
|
|
|
|
can load additional elements when needed.
|
|
|
|
|
|
|
|
Note that the PathView will layout the items based on the size of the root
|
|
|
|
item in the delegate.
|
|
|
|
|
|
|
|
Here is an example delegate:
|
|
|
|
\snippet doc/src/snippets/declarative/pathview/pathview.qml 1
|
|
|
|
*/
|
2011-10-14 08:51:42 +00:00
|
|
|
QDeclarativeComponent *QQuickPathView::delegate() const
|
2011-04-27 12:13:26 +00:00
|
|
|
{
|
2011-10-14 08:51:42 +00:00
|
|
|
Q_D(const QQuickPathView);
|
2011-04-27 12:13:26 +00:00
|
|
|
if (d->model) {
|
2011-10-14 08:51:42 +00:00
|
|
|
if (QQuickVisualDataModel *dataModel = qobject_cast<QQuickVisualDataModel*>(d->model))
|
2011-04-27 12:13:26 +00:00
|
|
|
return dataModel->delegate();
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-10-14 08:51:42 +00:00
|
|
|
void QQuickPathView::setDelegate(QDeclarativeComponent *delegate)
|
2011-04-27 12:13:26 +00:00
|
|
|
{
|
2011-10-14 08:51:42 +00:00
|
|
|
Q_D(QQuickPathView);
|
2011-04-27 12:13:26 +00:00
|
|
|
if (delegate == this->delegate())
|
|
|
|
return;
|
|
|
|
if (!d->ownModel) {
|
2011-10-14 08:51:42 +00:00
|
|
|
d->model = new QQuickVisualDataModel(qmlContext(this));
|
2011-04-27 12:13:26 +00:00
|
|
|
d->ownModel = true;
|
|
|
|
}
|
2011-10-14 08:51:42 +00:00
|
|
|
if (QQuickVisualDataModel *dataModel = qobject_cast<QQuickVisualDataModel*>(d->model)) {
|
2011-05-30 06:35:52 +00:00
|
|
|
int oldCount = dataModel->count();
|
2011-04-27 12:13:26 +00:00
|
|
|
dataModel->setDelegate(delegate);
|
|
|
|
d->modelCount = dataModel->count();
|
|
|
|
d->regenerate();
|
2011-05-30 06:35:52 +00:00
|
|
|
if (oldCount != dataModel->count())
|
|
|
|
emit countChanged();
|
2011-04-27 12:13:26 +00:00
|
|
|
emit delegateChanged();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-08-08 08:03:31 +00:00
|
|
|
/*!
|
|
|
|
\qmlproperty int QtQuick2::PathView::pathItemCount
|
|
|
|
This property holds the number of items visible on the path at any one time.
|
|
|
|
*/
|
2011-10-14 08:51:42 +00:00
|
|
|
int QQuickPathView::pathItemCount() const
|
2011-04-27 12:13:26 +00:00
|
|
|
{
|
2011-10-14 08:51:42 +00:00
|
|
|
Q_D(const QQuickPathView);
|
2011-04-27 12:13:26 +00:00
|
|
|
return d->pathItems;
|
|
|
|
}
|
|
|
|
|
2011-10-14 08:51:42 +00:00
|
|
|
void QQuickPathView::setPathItemCount(int i)
|
2011-04-27 12:13:26 +00:00
|
|
|
{
|
2011-10-14 08:51:42 +00:00
|
|
|
Q_D(QQuickPathView);
|
2011-04-27 12:13:26 +00:00
|
|
|
if (i == d->pathItems)
|
|
|
|
return;
|
|
|
|
if (i < 1)
|
|
|
|
i = 1;
|
|
|
|
d->pathItems = i;
|
|
|
|
d->updateMappedRange();
|
|
|
|
if (d->isValid() && isComponentComplete()) {
|
|
|
|
d->regenerate();
|
|
|
|
}
|
|
|
|
emit pathItemCountChanged();
|
|
|
|
}
|
|
|
|
|
2011-10-14 08:51:42 +00:00
|
|
|
QPointF QQuickPathViewPrivate::pointNear(const QPointF &point, qreal *nearPercent) const
|
2011-04-27 12:13:26 +00:00
|
|
|
{
|
|
|
|
//XXX maybe do recursively at increasing resolution.
|
|
|
|
qreal mindist = 1e10; // big number
|
|
|
|
QPointF nearPoint = path->pointAt(0);
|
|
|
|
qreal nearPc = 0;
|
|
|
|
for (qreal i=1; i < 1000; i++) {
|
|
|
|
QPointF pt = path->pointAt(i/1000.0);
|
|
|
|
QPointF diff = pt - point;
|
|
|
|
qreal dist = diff.x()*diff.x() + diff.y()*diff.y();
|
|
|
|
if (dist < mindist) {
|
|
|
|
nearPoint = pt;
|
|
|
|
nearPc = i;
|
|
|
|
mindist = dist;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (nearPercent)
|
|
|
|
*nearPercent = nearPc / 1000.0;
|
|
|
|
|
|
|
|
return nearPoint;
|
|
|
|
}
|
|
|
|
|
2011-10-14 08:51:42 +00:00
|
|
|
void QQuickPathView::mousePressEvent(QMouseEvent *event)
|
2011-04-27 12:13:26 +00:00
|
|
|
{
|
2011-10-14 08:51:42 +00:00
|
|
|
Q_D(QQuickPathView);
|
2011-04-27 12:13:26 +00:00
|
|
|
if (d->interactive) {
|
|
|
|
d->handleMousePressEvent(event);
|
|
|
|
event->accept();
|
|
|
|
} else {
|
2011-10-14 08:51:42 +00:00
|
|
|
QQuickItem::mousePressEvent(event);
|
2011-04-27 12:13:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-10-14 08:51:42 +00:00
|
|
|
void QQuickPathViewPrivate::handleMousePressEvent(QMouseEvent *event)
|
2011-04-27 12:13:26 +00:00
|
|
|
{
|
2011-10-14 08:51:42 +00:00
|
|
|
Q_Q(QQuickPathView);
|
2011-04-27 12:13:26 +00:00
|
|
|
if (!interactive || !items.count())
|
|
|
|
return;
|
2011-08-30 14:48:57 +00:00
|
|
|
QPointF scenePoint = q->mapToScene(event->localPos());
|
2011-04-27 12:13:26 +00:00
|
|
|
int idx = 0;
|
|
|
|
for (; idx < items.count(); ++idx) {
|
|
|
|
QRectF rect = items.at(idx)->boundingRect();
|
|
|
|
rect = items.at(idx)->mapRectToScene(rect);
|
|
|
|
if (rect.contains(scenePoint))
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (idx == items.count() && dragMargin == 0.) // didn't click on an item
|
|
|
|
return;
|
|
|
|
|
2011-08-30 14:48:57 +00:00
|
|
|
startPoint = pointNear(event->localPos(), &startPc);
|
2011-04-27 12:13:26 +00:00
|
|
|
if (idx == items.count()) {
|
2011-08-30 14:48:57 +00:00
|
|
|
qreal distance = qAbs(event->localPos().x() - startPoint.x()) + qAbs(event->localPos().y() - startPoint.y());
|
2011-04-27 12:13:26 +00:00
|
|
|
if (distance > dragMargin)
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (tl.isActive() && flicking)
|
|
|
|
stealMouse = true; // If we've been flicked then steal the click.
|
|
|
|
else
|
|
|
|
stealMouse = false;
|
|
|
|
|
|
|
|
lastElapsed = 0;
|
|
|
|
lastDist = 0;
|
2011-10-14 08:51:42 +00:00
|
|
|
QQuickItemPrivate::start(lastPosTime);
|
2011-04-27 12:13:26 +00:00
|
|
|
tl.clear();
|
|
|
|
}
|
|
|
|
|
2011-10-14 08:51:42 +00:00
|
|
|
void QQuickPathView::mouseMoveEvent(QMouseEvent *event)
|
2011-04-27 12:13:26 +00:00
|
|
|
{
|
2011-10-14 08:51:42 +00:00
|
|
|
Q_D(QQuickPathView);
|
2011-04-27 12:13:26 +00:00
|
|
|
if (d->interactive) {
|
|
|
|
d->handleMouseMoveEvent(event);
|
|
|
|
if (d->stealMouse)
|
|
|
|
setKeepMouseGrab(true);
|
|
|
|
event->accept();
|
|
|
|
} else {
|
2011-10-14 08:51:42 +00:00
|
|
|
QQuickItem::mouseMoveEvent(event);
|
2011-04-27 12:13:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-10-14 08:51:42 +00:00
|
|
|
void QQuickPathViewPrivate::handleMouseMoveEvent(QMouseEvent *event)
|
2011-04-27 12:13:26 +00:00
|
|
|
{
|
2011-10-14 08:51:42 +00:00
|
|
|
Q_Q(QQuickPathView);
|
2011-04-27 12:13:26 +00:00
|
|
|
if (!interactive || !lastPosTime.isValid())
|
|
|
|
return;
|
|
|
|
|
|
|
|
qreal newPc;
|
2011-08-30 14:48:57 +00:00
|
|
|
QPointF pathPoint = pointNear(event->localPos(), &newPc);
|
2011-04-27 12:13:26 +00:00
|
|
|
if (!stealMouse) {
|
|
|
|
QPointF delta = pathPoint - startPoint;
|
2011-08-30 11:17:00 +00:00
|
|
|
if (qAbs(delta.x()) > qApp->styleHints()->startDragDistance() || qAbs(delta.y()) > qApp->styleHints()->startDragDistance()) {
|
2011-04-27 12:13:26 +00:00
|
|
|
stealMouse = true;
|
|
|
|
startPc = newPc;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (stealMouse) {
|
2011-10-14 08:51:42 +00:00
|
|
|
moveReason = QQuickPathViewPrivate::Mouse;
|
2011-04-27 12:13:26 +00:00
|
|
|
qreal diff = (newPc - startPc)*modelCount*mappedRange;
|
|
|
|
if (diff) {
|
|
|
|
q->setOffset(offset + diff);
|
|
|
|
|
|
|
|
if (diff > modelCount/2)
|
|
|
|
diff -= modelCount;
|
|
|
|
else if (diff < -modelCount/2)
|
|
|
|
diff += modelCount;
|
|
|
|
|
2011-10-14 08:51:42 +00:00
|
|
|
lastElapsed = QQuickItemPrivate::restart(lastPosTime);
|
2011-04-27 12:13:26 +00:00
|
|
|
lastDist = diff;
|
|
|
|
startPc = newPc;
|
|
|
|
}
|
|
|
|
if (!moving) {
|
|
|
|
moving = true;
|
|
|
|
emit q->movingChanged();
|
|
|
|
emit q->movementStarted();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-10-14 08:51:42 +00:00
|
|
|
void QQuickPathView::mouseReleaseEvent(QMouseEvent *event)
|
2011-04-27 12:13:26 +00:00
|
|
|
{
|
2011-10-14 08:51:42 +00:00
|
|
|
Q_D(QQuickPathView);
|
2011-04-27 12:13:26 +00:00
|
|
|
if (d->interactive) {
|
|
|
|
d->handleMouseReleaseEvent(event);
|
|
|
|
event->accept();
|
|
|
|
ungrabMouse();
|
|
|
|
} else {
|
2011-10-14 08:51:42 +00:00
|
|
|
QQuickItem::mouseReleaseEvent(event);
|
2011-04-27 12:13:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-10-14 08:51:42 +00:00
|
|
|
void QQuickPathViewPrivate::handleMouseReleaseEvent(QMouseEvent *)
|
2011-04-27 12:13:26 +00:00
|
|
|
{
|
2011-10-14 08:51:42 +00:00
|
|
|
Q_Q(QQuickPathView);
|
2011-04-27 12:13:26 +00:00
|
|
|
stealMouse = false;
|
|
|
|
q->setKeepMouseGrab(false);
|
|
|
|
if (!interactive || !lastPosTime.isValid())
|
|
|
|
return;
|
|
|
|
|
2011-10-14 08:51:42 +00:00
|
|
|
qreal elapsed = qreal(lastElapsed + QQuickItemPrivate::elapsed(lastPosTime)) / 1000.;
|
2011-04-27 12:13:26 +00:00
|
|
|
qreal velocity = elapsed > 0. ? lastDist / elapsed : 0;
|
|
|
|
if (model && modelCount && qAbs(velocity) > 1.) {
|
|
|
|
qreal count = pathItems == -1 ? modelCount : pathItems;
|
|
|
|
if (qAbs(velocity) > count * 2) // limit velocity
|
|
|
|
velocity = (velocity > 0 ? count : -count) * 2;
|
|
|
|
// Calculate the distance to be travelled
|
|
|
|
qreal v2 = velocity*velocity;
|
|
|
|
qreal accel = deceleration/10;
|
|
|
|
// + 0.25 to encourage moving at least one item in the flick direction
|
|
|
|
qreal dist = qMin(qreal(modelCount-1), qreal(v2 / (accel * 2.0) + 0.25));
|
2011-10-14 08:51:42 +00:00
|
|
|
if (haveHighlightRange && highlightRangeMode == QQuickPathView::StrictlyEnforceRange) {
|
2011-04-27 12:13:26 +00:00
|
|
|
// round to nearest item.
|
|
|
|
if (velocity > 0.)
|
|
|
|
dist = qRound(dist + offset) - offset;
|
|
|
|
else
|
|
|
|
dist = qRound(dist - offset) + offset;
|
|
|
|
// Calculate accel required to stop on item boundary
|
|
|
|
if (dist <= 0.) {
|
|
|
|
dist = 0.;
|
|
|
|
accel = 0.;
|
|
|
|
} else {
|
|
|
|
accel = v2 / (2.0f * qAbs(dist));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
offsetAdj = 0.0;
|
|
|
|
moveOffset.setValue(offset);
|
|
|
|
tl.accel(moveOffset, velocity, accel, dist);
|
|
|
|
tl.callback(QDeclarativeTimeLineCallback(&moveOffset, fixOffsetCallback, this));
|
|
|
|
if (!flicking) {
|
|
|
|
flicking = true;
|
|
|
|
emit q->flickingChanged();
|
|
|
|
emit q->flickStarted();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
fixOffset();
|
|
|
|
}
|
|
|
|
|
|
|
|
lastPosTime.invalidate();
|
|
|
|
if (!tl.isActive())
|
|
|
|
q->movementEnding();
|
|
|
|
}
|
|
|
|
|
2011-10-14 08:51:42 +00:00
|
|
|
bool QQuickPathView::sendMouseEvent(QMouseEvent *event)
|
2011-04-27 12:13:26 +00:00
|
|
|
{
|
2011-10-14 08:51:42 +00:00
|
|
|
Q_D(QQuickPathView);
|
2011-04-27 12:13:26 +00:00
|
|
|
QRectF myRect = mapRectToScene(QRectF(0, 0, width(), height()));
|
2011-10-14 08:51:42 +00:00
|
|
|
QQuickCanvas *c = canvas();
|
|
|
|
QQuickItem *grabber = c ? c->mouseGrabberItem() : 0;
|
2011-04-27 12:13:26 +00:00
|
|
|
bool stealThisEvent = d->stealMouse;
|
2011-08-30 14:48:57 +00:00
|
|
|
if ((stealThisEvent || myRect.contains(event->windowPos())) && (!grabber || !grabber->keepMouseGrab())) {
|
|
|
|
QMouseEvent mouseEvent(event->type(), mapFromScene(event->windowPos()), event->windowPos(), event->screenPos(),
|
|
|
|
event->button(), event->buttons(), event->modifiers());
|
2011-04-27 12:13:26 +00:00
|
|
|
mouseEvent.setAccepted(false);
|
|
|
|
|
2011-10-17 11:03:58 +00:00
|
|
|
switch (mouseEvent.type()) {
|
2011-08-30 14:48:57 +00:00
|
|
|
case QEvent::MouseMove:
|
2011-04-27 12:13:26 +00:00
|
|
|
d->handleMouseMoveEvent(&mouseEvent);
|
|
|
|
break;
|
2011-08-30 14:48:57 +00:00
|
|
|
case QEvent::MouseButtonPress:
|
2011-04-27 12:13:26 +00:00
|
|
|
d->handleMousePressEvent(&mouseEvent);
|
|
|
|
stealThisEvent = d->stealMouse; // Update stealThisEvent in case changed by function call above
|
|
|
|
break;
|
2011-08-30 14:48:57 +00:00
|
|
|
case QEvent::MouseButtonRelease:
|
2011-04-27 12:13:26 +00:00
|
|
|
d->handleMouseReleaseEvent(&mouseEvent);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
grabber = c->mouseGrabberItem();
|
|
|
|
if (grabber && stealThisEvent && !grabber->keepMouseGrab() && grabber != this)
|
|
|
|
grabMouse();
|
|
|
|
|
|
|
|
return d->stealMouse;
|
|
|
|
} else if (d->lastPosTime.isValid()) {
|
|
|
|
d->lastPosTime.invalidate();
|
2011-08-01 05:49:33 +00:00
|
|
|
d->fixOffset();
|
2011-04-27 12:13:26 +00:00
|
|
|
}
|
2011-08-30 14:48:57 +00:00
|
|
|
if (event->type() == QEvent::MouseButtonRelease)
|
2011-04-27 12:13:26 +00:00
|
|
|
d->stealMouse = false;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2011-10-14 08:51:42 +00:00
|
|
|
bool QQuickPathView::childMouseEventFilter(QQuickItem *i, QEvent *e)
|
2011-04-27 12:13:26 +00:00
|
|
|
{
|
2011-10-14 08:51:42 +00:00
|
|
|
Q_D(QQuickPathView);
|
2011-04-27 12:13:26 +00:00
|
|
|
if (!isVisible() || !d->interactive)
|
2011-10-14 08:51:42 +00:00
|
|
|
return QQuickItem::childMouseEventFilter(i, e);
|
2011-04-27 12:13:26 +00:00
|
|
|
|
|
|
|
switch (e->type()) {
|
2011-08-30 14:48:57 +00:00
|
|
|
case QEvent::MouseButtonPress:
|
|
|
|
case QEvent::MouseMove:
|
|
|
|
case QEvent::MouseButtonRelease:
|
|
|
|
return sendMouseEvent(static_cast<QMouseEvent *>(e));
|
2011-04-27 12:13:26 +00:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2011-10-14 08:51:42 +00:00
|
|
|
return QQuickItem::childMouseEventFilter(i, e);
|
2011-04-27 12:13:26 +00:00
|
|
|
}
|
|
|
|
|
2011-10-14 08:51:42 +00:00
|
|
|
void QQuickPathView::mouseUngrabEvent()
|
2011-08-01 05:49:33 +00:00
|
|
|
{
|
2011-10-14 08:51:42 +00:00
|
|
|
Q_D(QQuickPathView);
|
2011-08-01 05:49:33 +00:00
|
|
|
if (d->stealMouse) {
|
|
|
|
// if our mouse grab has been removed (probably by a Flickable),
|
|
|
|
// fix our state
|
|
|
|
d->stealMouse = false;
|
|
|
|
setKeepMouseGrab(false);
|
|
|
|
d->lastPosTime.invalidate();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-10-14 08:51:42 +00:00
|
|
|
void QQuickPathView::updatePolish()
|
2011-04-27 12:13:26 +00:00
|
|
|
{
|
2011-10-14 08:51:42 +00:00
|
|
|
QQuickItem::updatePolish();
|
2011-04-27 12:13:26 +00:00
|
|
|
refill();
|
|
|
|
}
|
|
|
|
|
2011-10-14 08:51:42 +00:00
|
|
|
void QQuickPathView::componentComplete()
|
2011-04-27 12:13:26 +00:00
|
|
|
{
|
2011-10-14 08:51:42 +00:00
|
|
|
Q_D(QQuickPathView);
|
2011-10-05 06:03:12 +00:00
|
|
|
if (d->model && d->ownModel)
|
2011-10-14 08:51:42 +00:00
|
|
|
static_cast<QQuickVisualDataModel *>(d->model.data())->componentComplete();
|
2011-10-05 06:03:12 +00:00
|
|
|
|
2011-10-14 08:51:42 +00:00
|
|
|
QQuickItem::componentComplete();
|
2011-10-05 06:03:12 +00:00
|
|
|
|
2011-04-27 12:13:26 +00:00
|
|
|
d->createHighlight();
|
|
|
|
// It is possible that a refill has already happended to to Path
|
|
|
|
// bindings being handled in the componentComplete(). If so
|
|
|
|
// don't do it again.
|
|
|
|
if (d->items.count() == 0 && d->model) {
|
|
|
|
d->modelCount = d->model->count();
|
|
|
|
d->regenerate();
|
|
|
|
}
|
|
|
|
d->updateHighlight();
|
2011-10-05 06:03:12 +00:00
|
|
|
|
|
|
|
if (d->modelCount)
|
|
|
|
emit countChanged();
|
2011-04-27 12:13:26 +00:00
|
|
|
}
|
|
|
|
|
2011-10-14 08:51:42 +00:00
|
|
|
void QQuickPathView::refill()
|
2011-04-27 12:13:26 +00:00
|
|
|
{
|
2011-10-14 08:51:42 +00:00
|
|
|
Q_D(QQuickPathView);
|
2011-04-27 12:13:26 +00:00
|
|
|
if (!d->isValid() || !isComponentComplete())
|
|
|
|
return;
|
|
|
|
|
|
|
|
d->layoutScheduled = false;
|
|
|
|
bool currentVisible = false;
|
|
|
|
|
|
|
|
// first move existing items and remove items off path
|
|
|
|
int idx = d->firstIndex;
|
2011-10-14 08:51:42 +00:00
|
|
|
QList<QQuickItem*>::iterator it = d->items.begin();
|
2011-04-27 12:13:26 +00:00
|
|
|
while (it != d->items.end()) {
|
|
|
|
qreal pos = d->positionOfIndex(idx);
|
2011-10-14 08:51:42 +00:00
|
|
|
QQuickItem *item = *it;
|
2011-04-27 12:13:26 +00:00
|
|
|
if (pos >= 0.0) {
|
|
|
|
d->updateItem(item, pos);
|
|
|
|
if (idx == d->currentIndex) {
|
|
|
|
currentVisible = true;
|
|
|
|
d->currentItemOffset = pos;
|
|
|
|
}
|
|
|
|
++it;
|
|
|
|
} else {
|
2011-09-22 04:34:57 +00:00
|
|
|
// qDebug() << "release";
|
2011-04-27 12:13:26 +00:00
|
|
|
d->updateItem(item, 1.0);
|
|
|
|
d->releaseItem(item);
|
|
|
|
if (it == d->items.begin()) {
|
|
|
|
if (++d->firstIndex >= d->modelCount)
|
|
|
|
d->firstIndex = 0;
|
|
|
|
}
|
|
|
|
it = d->items.erase(it);
|
|
|
|
}
|
|
|
|
++idx;
|
|
|
|
if (idx >= d->modelCount)
|
|
|
|
idx = 0;
|
|
|
|
}
|
|
|
|
if (!d->items.count())
|
|
|
|
d->firstIndex = -1;
|
|
|
|
|
|
|
|
if (d->modelCount) {
|
|
|
|
// add items to beginning and end
|
|
|
|
int count = d->pathItems == -1 ? d->modelCount : qMin(d->pathItems, d->modelCount);
|
|
|
|
if (d->items.count() < count) {
|
|
|
|
int idx = qRound(d->modelCount - d->offset) % d->modelCount;
|
|
|
|
qreal startPos = 0.0;
|
2011-10-14 08:51:42 +00:00
|
|
|
if (d->haveHighlightRange && d->highlightRangeMode != QQuickPathView::NoHighlightRange)
|
2011-04-27 12:13:26 +00:00
|
|
|
startPos = d->highlightRangeStart;
|
|
|
|
if (d->firstIndex >= 0) {
|
|
|
|
startPos = d->positionOfIndex(d->firstIndex);
|
|
|
|
idx = (d->firstIndex + d->items.count()) % d->modelCount;
|
|
|
|
}
|
|
|
|
qreal pos = d->positionOfIndex(idx);
|
|
|
|
while ((pos > startPos || !d->items.count()) && d->items.count() < count) {
|
2011-09-22 04:34:57 +00:00
|
|
|
// qDebug() << "append" << idx;
|
2011-10-14 08:51:42 +00:00
|
|
|
QQuickItem *item = d->getItem(idx);
|
2011-04-27 12:13:26 +00:00
|
|
|
if (d->model->completePending())
|
|
|
|
item->setZ(idx+1);
|
|
|
|
if (d->currentIndex == idx) {
|
|
|
|
currentVisible = true;
|
|
|
|
d->currentItemOffset = pos;
|
|
|
|
}
|
|
|
|
if (d->items.count() == 0)
|
|
|
|
d->firstIndex = idx;
|
|
|
|
d->items.append(item);
|
|
|
|
d->updateItem(item, pos);
|
|
|
|
if (d->model->completePending())
|
|
|
|
d->model->completeItem();
|
|
|
|
++idx;
|
|
|
|
if (idx >= d->modelCount)
|
|
|
|
idx = 0;
|
|
|
|
pos = d->positionOfIndex(idx);
|
|
|
|
}
|
|
|
|
|
|
|
|
idx = d->firstIndex - 1;
|
|
|
|
if (idx < 0)
|
|
|
|
idx = d->modelCount - 1;
|
|
|
|
pos = d->positionOfIndex(idx);
|
2011-09-22 04:34:57 +00:00
|
|
|
while ((pos >= 0.0 && pos < startPos) && d->items.count() < count) {
|
|
|
|
// qDebug() << "prepend" << idx;
|
2011-10-14 08:51:42 +00:00
|
|
|
QQuickItem *item = d->getItem(idx);
|
2011-04-27 12:13:26 +00:00
|
|
|
if (d->model->completePending())
|
|
|
|
item->setZ(idx+1);
|
|
|
|
if (d->currentIndex == idx) {
|
|
|
|
currentVisible = true;
|
|
|
|
d->currentItemOffset = pos;
|
|
|
|
}
|
|
|
|
d->items.prepend(item);
|
|
|
|
d->updateItem(item, pos);
|
|
|
|
if (d->model->completePending())
|
|
|
|
d->model->completeItem();
|
|
|
|
d->firstIndex = idx;
|
|
|
|
idx = d->firstIndex - 1;
|
|
|
|
if (idx < 0)
|
|
|
|
idx = d->modelCount - 1;
|
|
|
|
pos = d->positionOfIndex(idx);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-08-01 03:09:10 +00:00
|
|
|
if (!currentVisible) {
|
2011-04-27 12:13:26 +00:00
|
|
|
d->currentItemOffset = 1.0;
|
2011-08-01 03:09:10 +00:00
|
|
|
if (d->currentItem) {
|
2011-10-14 08:51:42 +00:00
|
|
|
if (QQuickPathViewAttached *att = d->attached(d->currentItem))
|
2011-08-01 03:09:10 +00:00
|
|
|
att->setOnPath(false);
|
|
|
|
} else if (d->currentIndex >= 0 && d->currentIndex < d->modelCount) {
|
|
|
|
d->currentItem = d->getItem(d->currentIndex, false);
|
|
|
|
d->updateItem(d->currentItem, d->currentIndex < d->firstIndex ? 0.0 : 1.0);
|
2011-10-14 08:51:42 +00:00
|
|
|
if (QQuickPathViewAttached *att = d->attached(d->currentItem))
|
2011-08-01 03:09:10 +00:00
|
|
|
att->setIsCurrentItem(true);
|
|
|
|
if (d->model->completePending())
|
|
|
|
d->model->completeItem();
|
|
|
|
}
|
|
|
|
} else if (!d->currentItem) {
|
|
|
|
d->currentItem = d->model->item(d->currentIndex, true);
|
|
|
|
d->currentItem->setFocus(true);
|
2011-10-14 08:51:42 +00:00
|
|
|
if (QQuickPathViewAttached *att = d->attached(d->currentItem))
|
2011-08-01 03:09:10 +00:00
|
|
|
att->setIsCurrentItem(true);
|
|
|
|
}
|
2011-04-27 12:13:26 +00:00
|
|
|
|
2011-10-14 08:51:42 +00:00
|
|
|
if (d->highlightItem && d->haveHighlightRange && d->highlightRangeMode == QQuickPathView::StrictlyEnforceRange) {
|
2011-04-27 12:13:26 +00:00
|
|
|
d->updateItem(d->highlightItem, d->highlightRangeStart);
|
2011-10-14 08:51:42 +00:00
|
|
|
if (QQuickPathViewAttached *att = d->attached(d->highlightItem))
|
2011-04-27 12:13:26 +00:00
|
|
|
att->setOnPath(true);
|
2011-10-14 08:51:42 +00:00
|
|
|
} else if (d->highlightItem && d->moveReason != QQuickPathViewPrivate::SetIndex) {
|
2011-04-27 12:13:26 +00:00
|
|
|
d->updateItem(d->highlightItem, d->currentItemOffset);
|
2011-10-14 08:51:42 +00:00
|
|
|
if (QQuickPathViewAttached *att = d->attached(d->highlightItem))
|
2011-04-27 12:13:26 +00:00
|
|
|
att->setOnPath(currentVisible);
|
|
|
|
}
|
|
|
|
while (d->itemCache.count())
|
|
|
|
d->releaseItem(d->itemCache.takeLast());
|
|
|
|
}
|
|
|
|
|
2011-10-14 08:51:42 +00:00
|
|
|
void QQuickPathView::modelUpdated(const QDeclarativeChangeSet &changeSet, bool reset)
|
2011-04-27 12:13:26 +00:00
|
|
|
{
|
2011-10-14 08:51:42 +00:00
|
|
|
Q_D(QQuickPathView);
|
2011-09-01 05:24:34 +00:00
|
|
|
if (!d->model || !d->model->isValid() || !d->path || !isComponentComplete())
|
2011-04-27 12:13:26 +00:00
|
|
|
return;
|
|
|
|
|
2011-09-01 05:24:34 +00:00
|
|
|
if (reset) {
|
|
|
|
d->modelCount = d->model->count();
|
2011-04-27 12:13:26 +00:00
|
|
|
d->regenerate();
|
2011-09-01 05:24:34 +00:00
|
|
|
emit countChanged();
|
|
|
|
return;
|
2011-04-27 12:13:26 +00:00
|
|
|
}
|
|
|
|
|
2011-09-01 05:24:34 +00:00
|
|
|
if (changeSet.removes().isEmpty() && changeSet.inserts().isEmpty())
|
2011-04-27 12:13:26 +00:00
|
|
|
return;
|
|
|
|
|
2011-09-01 05:24:34 +00:00
|
|
|
const int modelCount = d->modelCount;
|
|
|
|
int moveId = -1;
|
|
|
|
int moveOffset;
|
2011-04-27 12:13:26 +00:00
|
|
|
bool currentChanged = false;
|
2011-09-01 05:24:34 +00:00
|
|
|
bool changedOffset = false;
|
|
|
|
bool removed = false;
|
|
|
|
bool inserted = false;
|
|
|
|
foreach (const QDeclarativeChangeSet::Remove &r, changeSet.removes()) {
|
|
|
|
removed = true;
|
|
|
|
if (moveId == -1 && d->currentIndex >= r.index + r.count) {
|
|
|
|
d->currentIndex -= r.count;
|
|
|
|
currentChanged = true;
|
|
|
|
} else if (moveId == -1 && d->currentIndex >= r.index && d->currentIndex < r.index + r.count) {
|
|
|
|
// current item has been removed.
|
|
|
|
d->currentIndex = qMin(r.index, d->modelCount - r.count - 1);
|
|
|
|
if (r.isMove()) {
|
|
|
|
moveId = r.moveId;
|
|
|
|
moveOffset = d->currentIndex - r.index;
|
|
|
|
} else if (d->currentItem) {
|
2011-10-14 08:51:42 +00:00
|
|
|
if (QQuickPathViewAttached *att = d->attached(d->currentItem))
|
2011-09-01 05:24:34 +00:00
|
|
|
att->setIsCurrentItem(true);
|
|
|
|
d->releaseItem(d->currentItem);
|
|
|
|
d->currentItem = 0;
|
|
|
|
}
|
|
|
|
currentChanged = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (r.index > d->currentIndex) {
|
|
|
|
if (d->offset >= r.count) {
|
|
|
|
changedOffset = true;
|
|
|
|
d->offset -= r.count;
|
|
|
|
d->offsetAdj -= r.count;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
d->modelCount -= r.count;
|
|
|
|
}
|
|
|
|
foreach (const QDeclarativeChangeSet::Insert &i, changeSet.inserts()) {
|
|
|
|
inserted = true;
|
|
|
|
if (d->modelCount) {
|
|
|
|
if (moveId == -1 && i.index <= d->currentIndex) {
|
|
|
|
d->currentIndex += i.count;
|
2011-11-09 05:34:01 +00:00
|
|
|
currentChanged = true;
|
2011-09-01 05:24:34 +00:00
|
|
|
} else if (d->offset != 0) {
|
2011-11-09 05:34:01 +00:00
|
|
|
if (moveId != -1 && moveId == i.moveId) {
|
2011-09-01 05:24:34 +00:00
|
|
|
d->currentIndex = i.index + moveOffset;
|
2011-11-09 05:34:01 +00:00
|
|
|
currentChanged = true;
|
|
|
|
}
|
2011-09-01 05:24:34 +00:00
|
|
|
d->offset += i.count;
|
|
|
|
d->offsetAdj += i.count;
|
|
|
|
}
|
2011-04-27 12:13:26 +00:00
|
|
|
}
|
2011-09-01 05:24:34 +00:00
|
|
|
d->modelCount += i.count;
|
2011-04-27 12:13:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
d->itemCache += d->items;
|
|
|
|
d->items.clear();
|
|
|
|
|
|
|
|
if (!d->modelCount) {
|
|
|
|
while (d->itemCache.count())
|
|
|
|
d->releaseItem(d->itemCache.takeLast());
|
|
|
|
d->offset = 0;
|
|
|
|
changedOffset = true;
|
|
|
|
d->tl.reset(d->moveOffset);
|
2011-09-01 05:24:34 +00:00
|
|
|
} else if (removed) {
|
2011-04-27 12:13:26 +00:00
|
|
|
d->regenerate();
|
2011-11-09 05:34:01 +00:00
|
|
|
if (!d->flicking && !d->moving && d->haveHighlightRange && d->highlightRangeMode == QQuickPathView::StrictlyEnforceRange) {
|
|
|
|
qreal targetOffset = qmlMod(d->modelCount - d->currentIndex, d->modelCount);
|
|
|
|
if (targetOffset != d->offset)
|
|
|
|
d->tl.set(d->moveOffset, targetOffset);
|
|
|
|
}
|
2011-09-01 05:24:34 +00:00
|
|
|
} else if (inserted) {
|
|
|
|
d->firstIndex = -1;
|
|
|
|
d->updateMappedRange();
|
|
|
|
d->scheduleLayout();
|
2011-11-09 05:34:01 +00:00
|
|
|
if (!d->flicking && !d->moving && d->haveHighlightRange && d->highlightRangeMode == QQuickPathView::StrictlyEnforceRange) {
|
|
|
|
qreal targetOffset = qmlMod(d->modelCount - d->currentIndex, d->modelCount);
|
|
|
|
if (targetOffset != d->offset)
|
|
|
|
d->tl.set(d->moveOffset, targetOffset);
|
|
|
|
}
|
2011-04-27 12:13:26 +00:00
|
|
|
}
|
|
|
|
if (changedOffset)
|
|
|
|
emit offsetChanged();
|
|
|
|
if (currentChanged)
|
|
|
|
emit currentIndexChanged();
|
2011-09-01 05:24:34 +00:00
|
|
|
if (d->modelCount != modelCount)
|
|
|
|
emit countChanged();
|
2011-04-27 12:13:26 +00:00
|
|
|
}
|
|
|
|
|
2011-10-14 08:51:42 +00:00
|
|
|
void QQuickPathView::createdItem(int index, QQuickItem *item)
|
2011-04-27 12:13:26 +00:00
|
|
|
{
|
2011-10-14 08:51:42 +00:00
|
|
|
Q_D(QQuickPathView);
|
2011-04-27 12:13:26 +00:00
|
|
|
if (d->requestedIndex != index) {
|
|
|
|
if (!d->attType) {
|
|
|
|
// pre-create one metatype to share with all attached objects
|
2011-10-14 08:51:42 +00:00
|
|
|
d->attType = new QDeclarativeOpenMetaObjectType(&QQuickPathViewAttached::staticMetaObject, qmlEngine(this));
|
2011-10-17 11:03:58 +00:00
|
|
|
foreach (const QString &attr, d->path->attributes())
|
2011-04-27 12:13:26 +00:00
|
|
|
d->attType->createProperty(attr.toUtf8());
|
|
|
|
}
|
|
|
|
qPathViewAttachedType = d->attType;
|
2011-10-14 08:51:42 +00:00
|
|
|
QQuickPathViewAttached *att = static_cast<QQuickPathViewAttached *>(qmlAttachedPropertiesObject<QQuickPathView>(item));
|
2011-04-27 12:13:26 +00:00
|
|
|
qPathViewAttachedType = 0;
|
|
|
|
if (att) {
|
|
|
|
att->m_view = this;
|
|
|
|
att->setOnPath(false);
|
|
|
|
}
|
2011-11-17 01:23:52 +00:00
|
|
|
QDeclarative_setParent_noEvent(item, this);
|
2011-04-27 12:13:26 +00:00
|
|
|
item->setParentItem(this);
|
|
|
|
d->updateItem(item, index < d->firstIndex ? 0.0 : 1.0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-10-14 08:51:42 +00:00
|
|
|
void QQuickPathView::destroyingItem(QQuickItem *item)
|
2011-04-27 12:13:26 +00:00
|
|
|
{
|
|
|
|
Q_UNUSED(item);
|
|
|
|
}
|
|
|
|
|
2011-10-14 08:51:42 +00:00
|
|
|
void QQuickPathView::ticked()
|
2011-04-27 12:13:26 +00:00
|
|
|
{
|
2011-10-14 08:51:42 +00:00
|
|
|
Q_D(QQuickPathView);
|
2011-04-27 12:13:26 +00:00
|
|
|
d->updateCurrent();
|
|
|
|
}
|
|
|
|
|
2011-10-14 08:51:42 +00:00
|
|
|
void QQuickPathView::movementEnding()
|
2011-04-27 12:13:26 +00:00
|
|
|
{
|
2011-10-14 08:51:42 +00:00
|
|
|
Q_D(QQuickPathView);
|
2011-04-27 12:13:26 +00:00
|
|
|
if (d->flicking) {
|
|
|
|
d->flicking = false;
|
|
|
|
emit flickingChanged();
|
|
|
|
emit flickEnded();
|
|
|
|
}
|
|
|
|
if (d->moving && !d->stealMouse) {
|
|
|
|
d->moving = false;
|
|
|
|
emit movingChanged();
|
|
|
|
emit movementEnded();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// find the item closest to the snap position
|
2011-10-14 08:51:42 +00:00
|
|
|
int QQuickPathViewPrivate::calcCurrentIndex()
|
2011-04-27 12:13:26 +00:00
|
|
|
{
|
|
|
|
int current = -1;
|
|
|
|
if (modelCount && model && items.count()) {
|
|
|
|
offset = qmlMod(offset, modelCount);
|
|
|
|
if (offset < 0)
|
|
|
|
offset += modelCount;
|
|
|
|
current = qRound(qAbs(qmlMod(modelCount - offset, modelCount)));
|
|
|
|
current = current % modelCount;
|
|
|
|
}
|
|
|
|
|
|
|
|
return current;
|
|
|
|
}
|
|
|
|
|
2011-10-14 08:51:42 +00:00
|
|
|
void QQuickPathViewPrivate::updateCurrent()
|
2011-04-27 12:13:26 +00:00
|
|
|
{
|
2011-10-14 08:51:42 +00:00
|
|
|
Q_Q(QQuickPathView);
|
2011-04-27 12:13:26 +00:00
|
|
|
if (moveReason != Mouse)
|
|
|
|
return;
|
2011-10-14 08:51:42 +00:00
|
|
|
if (!modelCount || !haveHighlightRange || highlightRangeMode != QQuickPathView::StrictlyEnforceRange)
|
2011-04-27 12:13:26 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
int idx = calcCurrentIndex();
|
|
|
|
if (model && idx != currentIndex) {
|
2011-08-01 03:09:10 +00:00
|
|
|
if (currentItem) {
|
2011-10-14 08:51:42 +00:00
|
|
|
if (QQuickPathViewAttached *att = attached(currentItem))
|
2011-08-01 03:09:10 +00:00
|
|
|
att->setIsCurrentItem(false);
|
|
|
|
releaseItem(currentItem);
|
2011-04-27 12:13:26 +00:00
|
|
|
}
|
|
|
|
currentIndex = idx;
|
|
|
|
currentItem = 0;
|
2011-08-01 03:09:10 +00:00
|
|
|
int itemIndex = (idx - firstIndex + modelCount) % modelCount;
|
2011-04-27 12:13:26 +00:00
|
|
|
if (itemIndex < items.count()) {
|
2011-08-01 03:09:10 +00:00
|
|
|
currentItem = model->item(currentIndex, true);
|
2011-04-27 12:13:26 +00:00
|
|
|
currentItem->setFocus(true);
|
2011-10-14 08:51:42 +00:00
|
|
|
if (QQuickPathViewAttached *att = attached(currentItem))
|
2011-04-27 12:13:26 +00:00
|
|
|
att->setIsCurrentItem(true);
|
2011-08-01 03:09:10 +00:00
|
|
|
} else if (currentIndex >= 0 && currentIndex < modelCount) {
|
|
|
|
currentItem = getItem(currentIndex, false);
|
|
|
|
updateItem(currentItem, currentIndex < firstIndex ? 0.0 : 1.0);
|
2011-10-14 08:51:42 +00:00
|
|
|
if (QQuickPathViewAttached *att = attached(currentItem))
|
2011-08-01 03:09:10 +00:00
|
|
|
att->setIsCurrentItem(true);
|
|
|
|
if (model->completePending())
|
|
|
|
model->completeItem();
|
2011-04-27 12:13:26 +00:00
|
|
|
}
|
|
|
|
emit q->currentIndexChanged();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-10-14 08:51:42 +00:00
|
|
|
void QQuickPathViewPrivate::fixOffsetCallback(void *d)
|
2011-04-27 12:13:26 +00:00
|
|
|
{
|
2011-10-14 08:51:42 +00:00
|
|
|
((QQuickPathViewPrivate *)d)->fixOffset();
|
2011-04-27 12:13:26 +00:00
|
|
|
}
|
|
|
|
|
2011-10-14 08:51:42 +00:00
|
|
|
void QQuickPathViewPrivate::fixOffset()
|
2011-04-27 12:13:26 +00:00
|
|
|
{
|
2011-10-14 08:51:42 +00:00
|
|
|
Q_Q(QQuickPathView);
|
2011-04-27 12:13:26 +00:00
|
|
|
if (model && items.count()) {
|
2011-10-14 08:51:42 +00:00
|
|
|
if (haveHighlightRange && highlightRangeMode == QQuickPathView::StrictlyEnforceRange) {
|
2011-04-27 12:13:26 +00:00
|
|
|
int curr = calcCurrentIndex();
|
|
|
|
if (curr != currentIndex)
|
|
|
|
q->setCurrentIndex(curr);
|
|
|
|
else
|
|
|
|
snapToCurrent();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-10-14 08:51:42 +00:00
|
|
|
void QQuickPathViewPrivate::snapToCurrent()
|
2011-04-27 12:13:26 +00:00
|
|
|
{
|
|
|
|
if (!model || modelCount <= 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
qreal targetOffset = qmlMod(modelCount - currentIndex, modelCount);
|
|
|
|
|
2011-11-09 05:34:01 +00:00
|
|
|
if (offset == targetOffset)
|
|
|
|
return;
|
|
|
|
|
2011-04-27 12:13:26 +00:00
|
|
|
moveReason = Other;
|
|
|
|
offsetAdj = 0.0;
|
|
|
|
tl.reset(moveOffset);
|
|
|
|
moveOffset.setValue(offset);
|
|
|
|
|
|
|
|
const int duration = highlightMoveDuration;
|
|
|
|
|
2011-11-21 23:28:40 +00:00
|
|
|
if (!duration) {
|
|
|
|
tl.set(moveOffset, targetOffset);
|
|
|
|
} else if (moveDirection == Positive || (moveDirection == Shortest && targetOffset - offset > modelCount/2)) {
|
2011-04-27 12:13:26 +00:00
|
|
|
qreal distance = modelCount - targetOffset + offset;
|
|
|
|
if (targetOffset > moveOffset) {
|
|
|
|
tl.move(moveOffset, 0.0, QEasingCurve(QEasingCurve::InQuad), int(duration * offset / distance));
|
|
|
|
tl.set(moveOffset, modelCount);
|
|
|
|
tl.move(moveOffset, targetOffset, QEasingCurve(offset == 0.0 ? QEasingCurve::InOutQuad : QEasingCurve::OutQuad), int(duration * (modelCount-targetOffset) / distance));
|
|
|
|
} else {
|
|
|
|
tl.move(moveOffset, targetOffset, QEasingCurve(QEasingCurve::InOutQuad), duration);
|
|
|
|
}
|
|
|
|
} else if (moveDirection == Negative || targetOffset - offset <= -modelCount/2) {
|
|
|
|
qreal distance = modelCount - offset + targetOffset;
|
|
|
|
if (targetOffset < moveOffset) {
|
|
|
|
tl.move(moveOffset, modelCount, QEasingCurve(targetOffset == 0 ? QEasingCurve::InOutQuad : QEasingCurve::InQuad), int(duration * (modelCount-offset) / distance));
|
|
|
|
tl.set(moveOffset, 0.0);
|
|
|
|
tl.move(moveOffset, targetOffset, QEasingCurve(QEasingCurve::OutQuad), int(duration * targetOffset / distance));
|
|
|
|
} else {
|
|
|
|
tl.move(moveOffset, targetOffset, QEasingCurve(QEasingCurve::InOutQuad), duration);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
tl.move(moveOffset, targetOffset, QEasingCurve(QEasingCurve::InOutQuad), duration);
|
|
|
|
}
|
|
|
|
moveDirection = Shortest;
|
|
|
|
}
|
|
|
|
|
2011-10-14 08:51:42 +00:00
|
|
|
QQuickPathViewAttached *QQuickPathView::qmlAttachedProperties(QObject *obj)
|
2011-04-27 12:13:26 +00:00
|
|
|
{
|
2011-10-14 08:51:42 +00:00
|
|
|
return new QQuickPathViewAttached(obj);
|
2011-04-27 12:13:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
QT_END_NAMESPACE
|
|
|
|
|