diff options
Diffstat (limited to 'src/templates')
-rw-r--r-- | src/templates/qquickpageindicator.cpp | 120 | ||||
-rw-r--r-- | src/templates/qquickpageindicator_p.h | 12 |
2 files changed, 131 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 diff --git a/src/templates/qquickpageindicator_p.h b/src/templates/qquickpageindicator_p.h index 856ce39b..e1c865dd 100644 --- a/src/templates/qquickpageindicator_p.h +++ b/src/templates/qquickpageindicator_p.h @@ -61,6 +61,7 @@ class Q_QUICKTEMPLATES_EXPORT QQuickPageIndicator : public QQuickControl Q_OBJECT Q_PROPERTY(int count READ count WRITE setCount NOTIFY countChanged FINAL) Q_PROPERTY(int currentIndex READ currentIndex WRITE setCurrentIndex NOTIFY currentIndexChanged FINAL) + Q_PROPERTY(bool interactive READ isInteractive WRITE setInteractive NOTIFY interactiveChanged FINAL) Q_PROPERTY(QQmlComponent *delegate READ delegate WRITE setDelegate NOTIFY delegateChanged FINAL) Q_PROPERTY(QColor color READ color WRITE setColor NOTIFY colorChanged FINAL) @@ -73,6 +74,9 @@ public: int currentIndex() const; void setCurrentIndex(int index); + bool isInteractive() const; + void setInteractive(bool interactive); + QQmlComponent *delegate() const; void setDelegate(QQmlComponent *delegate); @@ -82,9 +86,17 @@ public: Q_SIGNALS: void countChanged(); void currentIndexChanged(); + void interactiveChanged(); void delegateChanged(); void colorChanged(); +protected: + void componentComplete() Q_DECL_OVERRIDE; + void mousePressEvent(QMouseEvent *event) Q_DECL_OVERRIDE; + void mouseMoveEvent(QMouseEvent *event) Q_DECL_OVERRIDE; + void mouseReleaseEvent(QMouseEvent *event) Q_DECL_OVERRIDE; + void mouseUngrabEvent() Q_DECL_OVERRIDE; + private: Q_DISABLE_COPY(QQuickPageIndicator) Q_DECLARE_PRIVATE(QQuickPageIndicator) |