diff options
author | Mitch Curtis <mitch.curtis@theqtcompany.com> | 2015-09-16 16:01:33 +0200 |
---|---|---|
committer | Mitch Curtis <mitch.curtis@theqtcompany.com> | 2015-09-17 11:41:22 +0000 |
commit | 7c5c9d1fb8bff09d955489f65098f3f63c53bef5 (patch) | |
tree | 73bd900bc161a532031b263e2ba5723439513a52 /src/templates/qquickcontainer.cpp | |
parent | 7e6973ad91efd62a3f4e4bb6592dd8e04e7225c0 (diff) |
Move currentIndex, add/insert/removeItem to QQuickContainer
Change-Id: I6bb2adb4de972bb62f0e72c80cc50cf90964631f
Reviewed-by: J-P Nurmi <jpnurmi@theqtcompany.com>
Diffstat (limited to 'src/templates/qquickcontainer.cpp')
-rw-r--r-- | src/templates/qquickcontainer.cpp | 68 |
1 files changed, 67 insertions, 1 deletions
diff --git a/src/templates/qquickcontainer.cpp b/src/templates/qquickcontainer.cpp index 76a4e192..ce417c57 100644 --- a/src/templates/qquickcontainer.cpp +++ b/src/templates/qquickcontainer.cpp @@ -48,7 +48,7 @@ QT_BEGIN_NAMESPACE \internal */ -QQuickContainerPrivate::QQuickContainerPrivate() : contentModel(Q_NULLPTR) +QQuickContainerPrivate::QQuickContainerPrivate() : contentModel(Q_NULLPTR), currentIndex(-1) { } @@ -85,19 +85,53 @@ void QQuickContainerPrivate::insertItem(int index, QQuickItem *item) QQuickItemPrivate::get(item)->addItemChangeListener(this, QQuickItemPrivate::Destroyed | QQuickItemPrivate::Parent); contentData.append(item); contentModel->insert(index, item); + + itemInserted(index, item); + + if (contentModel->count() == 1 && currentIndex == -1) { + Q_Q(QQuickContainer); + q->setCurrentIndex(index); + } +} + +void QQuickContainerPrivate::itemInserted(int, QQuickItem *) +{ } void QQuickContainerPrivate::moveItem(int from, int to) { contentModel->move(from, to); + itemMoved(from, to); +} + +void QQuickContainerPrivate::itemMoved(int, int) +{ } void QQuickContainerPrivate::removeItem(int index, QQuickItem *item) { + Q_Q(QQuickContainer); + bool currentChanged = false; + if (index == currentIndex) { + q->setCurrentIndex(currentIndex - 1); + } else if (index < currentIndex) { + --currentIndex; + currentChanged = true; + } + QQuickItemPrivate::get(item)->removeItemChangeListener(this, QQuickItemPrivate::Destroyed | QQuickItemPrivate::Parent); item->setParentItem(Q_NULLPTR); contentData.removeOne(item); contentModel->remove(index); + + itemRemoved(item); + + if (currentChanged) + emit q->currentIndexChanged(); +} + +void QQuickContainerPrivate::itemRemoved(QQuickItem *) +{ } void QQuickContainerPrivate::itemChildAdded(QQuickItem *, QQuickItem *child) @@ -355,6 +389,38 @@ QQmlListProperty<QQuickItem> QQuickContainer::contentChildren() QQuickContainerPrivate::contentChildren_clear); } +/*! + \qmlproperty int QtQuickControls2::Container::currentIndex + + TODO +*/ +int QQuickContainer::currentIndex() const +{ + Q_D(const QQuickContainer); + return d->currentIndex; +} + +void QQuickContainer::setCurrentIndex(int index) +{ + Q_D(QQuickContainer); + if (d->currentIndex != index) { + d->currentIndex = index; + emit currentIndexChanged(); + emit currentItemChanged(); + } +} + +/*! + \qmlproperty Item QtQuickControls2::Container::currentItem + + TODO +*/ +QQuickItem *QQuickContainer::currentItem() const +{ + Q_D(const QQuickContainer); + return itemAt(d->currentIndex); +} + void QQuickContainer::itemChange(ItemChange change, const ItemChangeData &data) { Q_D(QQuickContainer); |