[app] Added positionViewAtIndex method to BooksListWatcher

It allows to avoid binding loops
This commit is contained in:
Slava Monich 2016-11-24 14:16:46 +02:00
parent 6900ed151d
commit 1a8aad3139
2 changed files with 49 additions and 3 deletions

View file

@ -38,6 +38,7 @@
#define LISTVIEW_CONTENT_X "contentX"
#define LISTVIEW_CONTENT_Y "contentY"
#define LISTVIEW_INDEX_AT "indexAt"
#define LISTVIEW_POSITION_VIEW_AT_INDEX "positionViewAtIndex"
BooksListWatcher::BooksListWatcher(QObject* aParent) :
QObject(aParent),
@ -45,6 +46,8 @@ BooksListWatcher::BooksListWatcher(QObject* aParent) :
iContentX(0),
iContentY(0),
iListView(NULL),
iCenterMode(-1),
iPositionIsChanging(false),
iResizeTimer(new QTimer(this))
{
iResizeTimer->setSingleShot(true);
@ -58,6 +61,7 @@ void BooksListWatcher::setListView(QQuickItem* aView)
const QSize oldSize(iSize);
if (iListView) iListView->disconnect(this);
iListView = aView;
iCenterMode = -1;
if (iListView) {
connect(iListView,
SIGNAL(widthChanged()),
@ -110,6 +114,38 @@ qreal BooksListWatcher::getRealProperty(const char *name, qreal defaultValue)
return defaultValue;
}
void BooksListWatcher::positionViewAtIndex(int aIndex)
{
if (iListView) {
HDEBUG(aIndex);
if (iCenterMode < 0) {
bool ok = false;
const QMetaObject* metaObject = iListView->metaObject();
if (metaObject) {
int index = metaObject->indexOfEnumerator("PositionMode");
if (index >= 0) {
QMetaEnum metaEnum = metaObject->enumerator(index);
int value = metaEnum.keyToValue("Center", &ok);
if (ok) {
iCenterMode = value;
HDEBUG("Center =" << iCenterMode);
}
}
}
HASSERT(ok);
if (!ok) {
// This is what it normally is
iCenterMode = 1;
}
}
iPositionIsChanging = true;
QMetaObject::invokeMethod(iListView, LISTVIEW_POSITION_VIEW_AT_INDEX,
Q_ARG(int,aIndex), Q_ARG(int,iCenterMode));
iPositionIsChanging = false;
updateCurrentIndex();
}
}
qreal BooksListWatcher::contentX()
{
return getRealProperty(LISTVIEW_CONTENT_X);
@ -193,18 +229,24 @@ void BooksListWatcher::onContentXChanged()
{
HASSERT(sender() == iListView);
iContentX = contentX();
if (!iPositionIsChanging) {
updateCurrentIndex();
}
}
void BooksListWatcher::onContentYChanged()
{
HASSERT(sender() == iListView);
iContentY = contentY();
if (!iPositionIsChanging) {
updateCurrentIndex();
}
}
void BooksListWatcher::onContentSizeChanged()
{
HASSERT(sender() == iListView);
if (!iPositionIsChanging) {
updateCurrentIndex();
}
}

View file

@ -59,6 +59,8 @@ public:
QQuickItem* listView() const { return iListView; }
void setListView(QQuickItem* aView);
Q_INVOKABLE void positionViewAtIndex(int aIndex);
private:
qreal contentX();
qreal contentY();
@ -88,6 +90,8 @@ private:
qreal iContentX;
qreal iContentY;
QQuickItem* iListView;
int iCenterMode;
bool iPositionIsChanging;
QTimer* iResizeTimer;
};