summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorMarius Bugge Monsen <mmonsen@trolltech.com>2009-08-31 19:00:58 +0200
committerMarius Bugge Monsen <mmonsen@trolltech.com>2009-08-31 19:00:58 +0200
commitabc2891bc4f3147d9e305eeaa8d16671b309d111 (patch)
treee6a950f431e9479de68e9b261cbd85b4ad311146 /src
parentb8c4757e74264525e6c0db77cded83af2457d390 (diff)
Implement functionality to ensure that the current item is always visible. Needs testing.
Diffstat (limited to 'src')
-rw-r--r--src/qgraphicslistview.cpp77
-rw-r--r--src/qgraphicslistview.h5
-rw-r--r--src/qlistcontroller.cpp43
-rw-r--r--src/qlistcontroller.h5
-rw-r--r--src/qlistcontroller_p.h1
-rw-r--r--src/qlistwidgetng.cpp19
-rw-r--r--src/qlistwidgetng.h1
-rw-r--r--src/qlistwidgetng_p.h1
8 files changed, 80 insertions, 72 deletions
diff --git a/src/qgraphicslistview.cpp b/src/qgraphicslistview.cpp
index 169db3e..5f10627 100644
--- a/src/qgraphicslistview.cpp
+++ b/src/qgraphicslistview.cpp
@@ -1154,69 +1154,40 @@ int QtGraphicsListView::firstIndex() const
}
/*!
- Sets the offset to ensure that the item represented
- by the given \a index is visible in the view.
- Note that this function is potentially slow.
-
- \sa setOffset()
+ Returns an offset value that will ensure that the given \a index
+ is visible in the view.
*/
-void QtGraphicsListView::setOffsetToEnsureIndexIsVisible(int index)
+qreal QtGraphicsListView::offsetToEnsureIndexIsVisible(int index) const
{
- Q_D(QtGraphicsListView);
+ Q_D(const QtGraphicsListView);
const QRectF item = itemGeometry(index); // potentially slow
const QRectF view = geometry();
- if (!view.contains(item)) {
- if (orientation() == Qt::Vertical) {
- if (item.top() < view.top())
- setOffset(d->offset + item.top());
- else if (item.bottom() > view.bottom())
- setOffset(d->offset + item.bottom() - view.height());
- } else {
- if (item.left() < view.left())
- setOffset(d->offset + item.x());
- else if (item.right() > view.right())
- setOffset(d->offset + item.right() - view.width());
- }
+ if (orientation() == Qt::Vertical) {
+ if (item.bottom() < view.top())
+ return d->offset + item.top();
+ else if (item.top() > view.bottom())
+ return d->offset + item.bottom() - view.height();
+ } else {
+ if (item.right() < view.left())
+ return d->offset + item.x();
+ else if (item.left() > view.right())
+ return d->offset + item.right() - view.width();
}
+ return d->offset;
}
/*!
+ Returns an first index value that will ensure that the given \a index
+ is visible in the view.
*/
-void QtGraphicsListView::setFirstIndexToEnsureIndexIsVisible(int index)
+int QtGraphicsListView::firstIndexToEnsureIndexIsVisible(int index) const
{
- // ### this breaks views with a different layout
-#if 0
- Q_D(QtGraphicsListView);
- // ### cache
- if (index > d->firstIndex) {
- const QSizeF constraint = (d->orientation == Qt::Horizontal
- ? QSizeF(-1, size().height())
- : QSizeF(size().width(), -1));
- QStyleOptionViewItemV4 option;
- initStyleOption(&option);
- if (orientation() == Qt::Vertical) {
- qreal height = size().height() + d->offset;
- for (; index >= 0; --index) {
- initStyleOption(&option, index);
- height -= d->itemSizeHint(&option, index, constraint).height();
- if (height < 0)
- break;
- }
- } else {
- qreal width = size().width() + d->offset;
- for (; index >= 0; --index) {
- initStyleOption(&option, index);
- width -= d->itemSizeHint(&option, index, constraint).width();
- if (width < 0)
- break;
- }
- }
- }
- if (index > -1)
- setFirstIndex(index);
-#else
- Q_UNUSED(index);
-#endif
+ Q_D(const QtGraphicsListView);
+ if (index < d->firstIndex)
+ return index;
+ else if (index >= (d->firstIndex + d->items.count()))
+ return qBound(0, index - d->items.count() + 1, maximumFirstIndex()); // slow
+ return d->firstIndex;
}
/*!
diff --git a/src/qgraphicslistview.h b/src/qgraphicslistview.h
index bbca758..17829c6 100644
--- a/src/qgraphicslistview.h
+++ b/src/qgraphicslistview.h
@@ -146,11 +146,12 @@ public:
QGraphicsObject *itemForIndex(int index) const;
+ qreal offsetToEnsureIndexIsVisible(int index) const;
+ int firstIndexToEnsureIndexIsVisible(int index) const;
+
public Q_SLOTS:
void setFirstIndex(int index);
void setOffset(qreal offset);
- void setOffsetToEnsureIndexIsVisible(int index);
- void setFirstIndexToEnsureIndexIsVisible(int index);
void updateLayout();
Q_SIGNALS:
diff --git a/src/qlistcontroller.cpp b/src/qlistcontroller.cpp
index f1dbd9e..27bcd7c 100644
--- a/src/qlistcontroller.cpp
+++ b/src/qlistcontroller.cpp
@@ -105,6 +105,21 @@ void QtListControllerPrivate::_q_offsetChanged(qreal offset)
}
/*!
+ \internal
+ */
+void QtListControllerPrivate::_q_currentChanged(int current, int previous)
+{
+ Q_UNUSED(previous);
+ if (!view || !model)
+ return;
+ // ensure current is visible
+ if (scrollPerItem)
+ view->setFirstIndex(view->firstIndexToEnsureIndexIsVisible(current));
+ else
+ view->setOffset(view->offsetToEnsureIndexIsVisible(current));
+}
+
+/*!
\internal
*/
void QtListControllerPrivate::_q_animationFinished()
@@ -164,6 +179,11 @@ void QtListControllerPrivate::animateFirstIndex(int index)
*/
/*!
+ \enum SelectionBehavior
+ \brief controls how selection is done in the list
+*/
+
+/*!
Constructs a list controller with the given \a parent.
*/
QtListController::QtListController(QObject *parent)
@@ -250,16 +270,18 @@ void QtListController::setSelectionManager(QtListSelectionManager *selectionMana
Q_D(QtListController);
if (d->selectionManager == selectionManager)
return;
- if (d->selectionManager)
+ if (d->selectionManager) {
disconnect(d->selectionManager, SIGNAL(destroyed()), this, SLOT(_q_selectionsDestroyed()));
+ disconnect(d->selectionManager, SIGNAL(currentChanged(int,int)), this, SLOT(_q_currentChanged(int,int)));
+ }
QtListSelectionManager *old = d->selectionManager;
-
d->selectionManager = selectionManager;
if (d->selectionManager) {
d->selectionManager->setModel(d->model);
connect(d->selectionManager, SIGNAL(destroyed()), this, SLOT(_q_selectionsDestroyed()));
+ connect(d->selectionManager, SIGNAL(currentChanged(int,int)), this, SLOT(_q_currentChanged(int,int)));
}
if (d->view)
@@ -382,11 +404,18 @@ void QtListController::setScrollValue(qreal value)
Q_D(QtListController);
if (!d->view)
return;
- if (d->scrollPerItem)
- //d->animateFirstIndex(value);
- d->view->setFirstIndex(static_cast<int>(value));
- else
+ if (d->scrollPerItem) {
+ int index = static_cast<int>(value);
+ if (d->view->firstIndex() == index)
+ return;
+ //d->animateFirstIndex(index);
+ d->view->setFirstIndex(index);
+ } else {
+ if (d->view->offset() == value)
+ return;
d->view->setOffset(value);
+ }
+ emit scrollValueChanged(value);
}
/*!
@@ -516,7 +545,7 @@ bool QtListController::keyPressEvent(QKeyEvent *event)
return true;
}
} else { // no modifier
- //d->selectionManager->endAnchoredSelection();
+ d->selectionManager->endAnchoredSelection();
d->selectionManager->clearSelections();
d->selectionManager->setCurrentItem(index);
switch (d->behavior) {
diff --git a/src/qlistcontroller.h b/src/qlistcontroller.h
index 889efef..aea8664 100644
--- a/src/qlistcontroller.h
+++ b/src/qlistcontroller.h
@@ -131,8 +131,9 @@ private:
Q_PRIVATE_SLOT(d_func(), void _q_modelDestroyed())
Q_PRIVATE_SLOT(d_func(), void _q_viewDestroyed())
Q_PRIVATE_SLOT(d_func(), void _q_selectionsDestroyed())
- Q_PRIVATE_SLOT(d_func(), void _q_firstIndexChanged(int index))
- Q_PRIVATE_SLOT(d_func(), void _q_offsetChanged(qreal offset))
+ Q_PRIVATE_SLOT(d_func(), void _q_firstIndexChanged(int))
+ Q_PRIVATE_SLOT(d_func(), void _q_offsetChanged(qreal))
+ Q_PRIVATE_SLOT(d_func(), void _q_currentChanged(int,int))
Q_PRIVATE_SLOT(d_func(), void _q_animationFinished())
};
diff --git a/src/qlistcontroller_p.h b/src/qlistcontroller_p.h
index 80c8990..a58b0f6 100644
--- a/src/qlistcontroller_p.h
+++ b/src/qlistcontroller_p.h
@@ -58,6 +58,7 @@ public:
void _q_selectionsDestroyed();
void _q_firstIndexChanged(int index);
void _q_offsetChanged(qreal offset);
+ void _q_currentChanged(int current, int previous);
void _q_animationFinished();
void animateFirstIndex(int index);
diff --git a/src/qlistwidgetng.cpp b/src/qlistwidgetng.cpp
index 1a70187..8ef2c6b 100644
--- a/src/qlistwidgetng.cpp
+++ b/src/qlistwidgetng.cpp
@@ -141,14 +141,8 @@ void QtListWidgetNGPrivate::_q_modelChanged(QtListModelInterface *current, QtLis
void QtListWidgetNGPrivate::_q_selectionManagerChanged(QtListSelectionManager *current, QtListSelectionManager *previous)
{
- if (previous) {
- QObject::disconnect(previous, SIGNAL(currentChanged(int,int)),
- controller->view(), SLOT(setFirstIndexToEnsureIndexIsVisible(int)));
- }
- if (current) {
- QObject::connect(current, SIGNAL(currentChanged(int,int)),
- controller->view(), SLOT(setFirstIndexToEnsureIndexIsVisible(int)));
- }
+ Q_UNUSED(current);
+ Q_UNUSED(previous);
}
void QtListWidgetNGPrivate::_q_viewChanged(QtGraphicsListView *current, QtGraphicsListView *previous)
@@ -156,15 +150,24 @@ void QtListWidgetNGPrivate::_q_viewChanged(QtGraphicsListView *current, QtGraphi
Q_Q(QtListWidgetNG);
if (previous) {
q->scene()->removeItem(previous);
+ QObject::disconnect(previous, SIGNAL(firstIndexChanged(int)), q, SLOT(_q_firstIndexChanged(int)));
}
if (current) {
q->scene()->addItem(current);
q->scene()->setActiveWindow(current);
current->grabKeyboard();
+ QObject::connect(current, SIGNAL(firstIndexChanged(int)), q, SLOT(_q_firstIndexChanged(int)));
}
//_q_updateGeometries();
}
+void QtListWidgetNGPrivate::_q_firstIndexChanged(int index)
+{
+ Q_Q(QtListWidgetNG);
+ if (q->verticalScrollBar()->value() != index)
+ q->verticalScrollBar()->setValue(index);
+}
+
void QtListWidgetNGPrivate::_q_updateGeometries()
{
Q_Q(QtListWidgetNG);
diff --git a/src/qlistwidgetng.h b/src/qlistwidgetng.h
index 5167992..eca1912 100644
--- a/src/qlistwidgetng.h
+++ b/src/qlistwidgetng.h
@@ -71,6 +71,7 @@ private:
Q_PRIVATE_SLOT(d_func(), void _q_modelChanged(QtListModelInterface *current, QtListModelInterface *previous))
Q_PRIVATE_SLOT(d_func(), void _q_selectionManagerChanged(QtListSelectionManager *current, QtListSelectionManager *previous))
Q_PRIVATE_SLOT(d_func(), void _q_viewChanged(QtGraphicsListView *current, QtGraphicsListView *previous))
+ Q_PRIVATE_SLOT(d_func(), void _q_firstIndexChanged(int index))
};
QT_END_NAMESPACE
diff --git a/src/qlistwidgetng_p.h b/src/qlistwidgetng_p.h
index 294deb1..38ffd41 100644
--- a/src/qlistwidgetng_p.h
+++ b/src/qlistwidgetng_p.h
@@ -59,6 +59,7 @@ public:
void _q_modelChanged(QtListModelInterface *current, QtListModelInterface *previous);
void _q_selectionManagerChanged(QtListSelectionManager *current, QtListSelectionManager *previous);
void _q_viewChanged(QtGraphicsListView *current, QtGraphicsListView *previous);
+ void _q_firstIndexChanged(int index);
void _q_updateGeometries();
void initialize();