aboutsummaryrefslogtreecommitdiffstats
path: root/src/templates/qquickpageindicator.cpp
diff options
context:
space:
mode:
authorJ-P Nurmi <jpnurmi@theqtcompany.com>2015-09-29 22:03:07 +0200
committerJ-P Nurmi <jpnurmi@theqtcompany.com>2015-09-30 14:32:45 +0000
commit542dd7b77a236fef03e5569e44669fad6db0485d (patch)
treefb039a24e51161b8c0891b64d44945b1d8a77b85 /src/templates/qquickpageindicator.cpp
parent45cf8b973b9160af5b1ef01194cdc820283c0d56 (diff)
Add PageIndicator::interactive
Change-Id: Iecb6d855d1c7710901925097ad4e03006d58309c Reviewed-by: Mitch Curtis <mitch.curtis@theqtcompany.com>
Diffstat (limited to 'src/templates/qquickpageindicator.cpp')
-rw-r--r--src/templates/qquickpageindicator.cpp120
1 files changed, 119 insertions, 1 deletions
diff --git a/src/templates/qquickpageindicator.cpp b/src/templates/qquickpageindicator.cpp
index d7cb40cc..ceb83ba7 100644
--- a/src/templates/qquickpageindicator.cpp
+++ b/src/templates/qquickpageindicator.cpp
@@ -67,15 +67,54 @@ QT_BEGIN_NAMESPACE
class QQuickPageIndicatorPrivate : public QQuickControlPrivate
{
+ Q_DECLARE_PUBLIC(QQuickPageIndicator)
+
public:
- QQuickPageIndicatorPrivate() : count(0), currentIndex(0), delegate(Q_NULLPTR) { }
+ QQuickPageIndicatorPrivate() : count(0), currentIndex(0),
+ interactive(false), delegate(Q_NULLPTR), pressedItem(Q_NULLPTR) { }
+
+ QQuickItem *itemAt(const QPoint &pos) const;
+ void updatePressed(bool pressed, const QPoint &pos = QPoint());
+ void setContextProperty(QQuickItem *item, const QString &name, const QVariant &value);
int count;
int currentIndex;
+ bool interactive;
QQmlComponent *delegate;
+ QQuickItem *pressedItem;
QColor color;
};
+QQuickItem *QQuickPageIndicatorPrivate::itemAt(const QPoint &pos) const
+{
+ Q_Q(const QQuickPageIndicator);
+ if (contentItem) {
+ QPointF mapped = q->mapToItem(contentItem, pos);
+ return contentItem->childAt(mapped.x(), mapped.y());
+ }
+ return Q_NULLPTR;
+}
+
+void QQuickPageIndicatorPrivate::updatePressed(bool pressed, const QPoint &pos)
+{
+ QQuickItem *prevItem = pressedItem;
+ pressedItem = pressed ? itemAt(pos) : Q_NULLPTR;
+ if (prevItem != pressedItem) {
+ setContextProperty(prevItem, QStringLiteral("pressed"), false);
+ setContextProperty(pressedItem, QStringLiteral("pressed"), pressed);
+ }
+}
+
+void QQuickPageIndicatorPrivate::setContextProperty(QQuickItem *item, const QString &name, const QVariant &value)
+{
+ QQmlContext *context = qmlContext(item);
+ if (context && context->isValid()) {
+ context = context->parentContext();
+ if (context && context->isValid())
+ context->setContextProperty(name, value);
+ }
+}
+
QQuickPageIndicator::QQuickPageIndicator(QQuickItem *parent) :
QQuickControl(*(new QQuickPageIndicatorPrivate), parent)
{
@@ -123,10 +162,41 @@ void QQuickPageIndicator::setCurrentIndex(int index)
}
/*!
+ \qmlproperty bool QtQuick.Controls::PageIndicator::interactive
+
+ This property holds whether the control is interactive. An interactive page indicator
+ reacts to presses and automatically changes the \l {currentIndex}{current index}
+ appropriately.
+
+ The default value is \c false.
+*/
+bool QQuickPageIndicator::isInteractive() const
+{
+ Q_D(const QQuickPageIndicator);
+ return d->interactive;
+}
+
+void QQuickPageIndicator::setInteractive(bool interactive)
+{
+ Q_D(QQuickPageIndicator);
+ if (d->interactive != interactive) {
+ d->interactive = interactive;
+ setAcceptedMouseButtons(interactive ? Qt::LeftButton : Qt::NoButton);
+ emit interactiveChanged();
+ }
+}
+
+/*!
\qmlproperty Component QtQuick.Controls::PageIndicator::delegate
This property holds a delegate that presents a page.
+ The following properties are available in the context of each delegate:
+ \table
+ \row \li \b index : int \li The index of the item
+ \row \li \b pressed : bool \li Whether the item is pressed
+ \endtable
+
\sa color
*/
QQmlComponent *QQuickPageIndicator::delegate() const
@@ -166,4 +236,52 @@ void QQuickPageIndicator::setColor(const QColor &color)
}
}
+void QQuickPageIndicator::componentComplete()
+{
+ Q_D(QQuickPageIndicator);
+ QQuickControl::componentComplete();
+ if (d->contentItem) {
+ foreach (QQuickItem *child, d->contentItem->childItems()) {
+ if (!QQuickItemPrivate::get(child)->isTransparentForPositioner())
+ d->setContextProperty(child, QStringLiteral("pressed"), false);
+ }
+ }
+}
+
+void QQuickPageIndicator::mousePressEvent(QMouseEvent *event)
+{
+ Q_D(QQuickPageIndicator);
+ if (d->interactive) {
+ d->updatePressed(true, event->pos());
+ event->accept();
+ }
+}
+
+void QQuickPageIndicator::mouseMoveEvent(QMouseEvent *event)
+{
+ Q_D(QQuickPageIndicator);
+ if (d->interactive) {
+ d->updatePressed(true, event->pos());
+ event->accept();
+ }
+}
+
+void QQuickPageIndicator::mouseReleaseEvent(QMouseEvent *event)
+{
+ Q_D(QQuickPageIndicator);
+ if (d->interactive) {
+ if (d->pressedItem)
+ setCurrentIndex(d->contentItem->childItems().indexOf(d->pressedItem));
+ d->updatePressed(false);
+ event->accept();
+ }
+}
+
+void QQuickPageIndicator::mouseUngrabEvent()
+{
+ Q_D(QQuickPageIndicator);
+ if (d->interactive)
+ d->updatePressed(false);
+}
+
QT_END_NAMESPACE