ganeswidgets/src/hgwidgets_p.cpp
changeset 3 c863538fcbb6
parent 2 49c70dcc3f17
child 5 4fa04caf0f43
--- a/ganeswidgets/src/hgwidgets_p.cpp	Fri May 14 16:57:01 2010 +0300
+++ b/ganeswidgets/src/hgwidgets_p.cpp	Thu May 27 13:59:05 2010 +0300
@@ -15,7 +15,6 @@
 *
 */
 
-#include <QGraphicsLinearLayout>
 #include <QApplication>
 #include <hbscrollbar_p.h>
 #include <hgwidgets/hgwidgets.h>
@@ -29,14 +28,12 @@
 //#include "hgindexfeedback.h"
 
 static const int INITIAL_SCROLLBAR_HIDE_TIMEOUT(4000);
-static const int DEFAULT_BUFFER_SIZE(30);
+static const int DEFAULT_BUFFER_SIZE(25);
 
 HgWidgetPrivate::HgWidgetPrivate() :
-    mLayout(0),
     mContainer(0),
     mBufferManager(0),
     mModel(0),
-    mSelectionModel(0),
     mDefaultSelectionModel(0),
     mScrollBar(0),
     mAbleToScroll(false),
@@ -69,7 +66,7 @@
     q->setFocusPolicy(Qt::StrongFocus);
 
     createScrollBar(container->scrollDirection());
-    
+
     //QObject::connect(&(mScrollBarHideTimer), SIGNAL(timeout()), q, SLOT(_q_hideScrollBars()));
 
     mContainer = container;
@@ -85,10 +82,10 @@
     q->connect(mContainer, SIGNAL(scrollingStarted()), q, SIGNAL(scrollingStarted()));
     q->connect(mContainer, SIGNAL(scrollingEnded()), q, SIGNAL(scrollingEnded()));
     q->connect(mScrollBarHideTimer, SIGNAL(timeout()), q, SLOT(_q_hideScrollBars()));
-    
+
 //    mIndexFeedback = new HgIndexFeedback(q);
 //    mIndexFeedback->setWidget(q);
-    
+
 }
 
 void HgWidgetPrivate::setModel( QAbstractItemModel *model )
@@ -105,23 +102,36 @@
 void HgWidgetPrivate::setSelectionModel(QItemSelectionModel *selectionModel)
 {
     FUNC_LOG;
-    
+
     Q_Q(HgWidget);
 
     if (mContainer) {
+        if (mContainer->selectionModel()) {
+            q->disconnect(mContainer->selectionModel(),
+                SIGNAL(currentChanged(QModelIndex, QModelIndex)),
+                q,
+                SLOT(_q_updateCurrentItem(QModelIndex, QModelIndex)));
+        }
+
+        QModelIndex defaultItem;
+        if (mModel && mModel->rowCount() > 0) {
+            defaultItem = mModel->index(0, 0);
+        }
+
         if (selectionModel == 0) {
             QItemSelectionModel *oldSelectionModel = mDefaultSelectionModel;
             mDefaultSelectionModel = 0;
             mDefaultSelectionModel = new QItemSelectionModel(mModel);
-            mContainer->setSelectionModel(mDefaultSelectionModel);
+            mContainer->setSelectionModel(mDefaultSelectionModel, defaultItem);
             delete oldSelectionModel;
         }
         else if (selectionModel != mContainer->selectionModel()) {
             QItemSelectionModel *oldSelectionModel = mDefaultSelectionModel;
             mDefaultSelectionModel = 0;
-            mContainer->setSelectionModel(selectionModel);
+            mContainer->setSelectionModel(selectionModel, defaultItem);
             delete oldSelectionModel;
         }
+
         if (mContainer->selectionModel()) {
 //            if (mIndexFeedback) {
 //                delete mIndexFeedback;
@@ -129,7 +139,11 @@
 //            }
 //            mIndexFeedback = new HgIndexFeedback(q);
 //            mIndexFeedback->setWidget(q);
-        }            
+
+            q->connect(mContainer->selectionModel(),
+                SIGNAL(currentChanged(QModelIndex, QModelIndex)),
+                SLOT(_q_updateCurrentItem(QModelIndex, QModelIndex)));
+        }
     }
 }
 
@@ -249,7 +263,7 @@
         q->connect(mModel, SIGNAL(rowsMoved(QModelIndex, int, int, QModelIndex, int)),
                            SLOT(_q_moveRows(QModelIndex, int, int, QModelIndex, int)));
         q->connect(mModel, SIGNAL(modelReset()),SLOT(_q_modelReset()));
-        
+
         mContainer->setItemCount(mModel->rowCount(QModelIndex()));
         QList<HgWidgetItem*> items = mContainer->items();
 
@@ -261,14 +275,8 @@
         }
 
         initBufferManager(itemCount);
-        
+
         setSelectionModel(0); // Default
-
-        if (mModel->rowCount() > 0)
-        {
-            setCurrentIndex(mModel->index(0, 0));
-            scrollTo(mModel->index(0, 0));
-        }
     }
 }
 
@@ -410,15 +418,19 @@
 
 void HgWidgetPrivate::setScrollBarPolicy(HgWidget::ScrollBarPolicy policy)
 {
-    mScrollBarPolicy = policy;
+    Q_Q(HgWidget);
+
+    if (mScrollBarPolicy != policy) {
+        mScrollBarPolicy = policy;
 
-    if (mScrollBarPolicy == HgWidget::ScrollBarAlwaysOff &&
-        mScrollBar->isVisible()){
-        mScrollBar->setVisible(false);
-    }
+        if (mScrollBarPolicy == HgWidget::ScrollBarAlwaysOff &&
+            mScrollBar->isVisible()){
+            mScrollBar->setVisible(false);
+        }
 
-    if(policy != HgWidget::ScrollBarAlwaysOff){
-        updateScrollMetrics();
+        if(policy != HgWidget::ScrollBarAlwaysOff){
+            updateScrollMetrics();
+        }
     }
 }
 
@@ -471,7 +483,7 @@
             _q_modelReset();
             return;
         }
-        
+
         mBufferManager->addItems(start, end);
         mContainer->addItems(start, end);
         // re-set model indexes for the items including and after the added indexes
@@ -481,9 +493,6 @@
             items.at(i)->setModelIndex(mModel->index(i, 0, QModelIndex()));
         }
         mBufferManager->flushRequestBuffers();
-        if (oldItemCount == 0 && newItemCount > 0) {
-            setCurrentIndex(mModel->index(0, 0));
-        }
         q->update();
     }
 }
@@ -539,6 +548,8 @@
 
 void HgWidgetPrivate::_q_modelReset()
 {
+    FUNC_LOG;
+
     if (mContainer && mBufferManager) {
         const int oldItemCount = mContainer->itemCount();
         const int newItemCount = mModel->rowCount();
@@ -557,14 +568,20 @@
             for( int i=0; i<itemCount; i++) {
                 items.at(i)->setModelIndex(mModel->index(i, 0, QModelIndex()));
             }
-                        
-            // Buffermanager requests items to be updated. 
+
+            // Buffermanager requests items to be updated.
             mBufferManager->resetBuffer(0, newItemCount);
+            QItemSelectionModel *selectionModel = mContainer->selectionModel();
             if (mModel->rowCount() > 0) {
-                setCurrentIndex(mModel->index(0, 0));
-                scrollTo(mModel->index(0, 0));
+                if (selectionModel && selectionModel->currentIndex().isValid()) {
+                    scrollTo(selectionModel->currentIndex());
+                }
+                else {
+                    setCurrentIndex(mModel->index(0, 0));
+                    scrollTo(mModel->index(0, 0));
+                }
             }
-        }        
+        }
     }
 }
 
@@ -627,8 +644,9 @@
 
 void HgWidgetPrivate::adjustGeometry()
 {
+    FUNC_LOG;
     Q_Q(HgWidget);
-    
+
     QRectF scrollAreaBoundingRect = q->boundingRect();
     if( scrollAreaBoundingRect.isNull() ||
             !scrollAreaBoundingRect.isValid() ||
@@ -645,7 +663,7 @@
 void HgWidgetPrivate::lostForeground()
 {
     if( !mForeground ) return;
-    
+
     mForeground = false;
     QList<HgWidgetItem*> list = mContainer->items();
     foreach(HgWidgetItem* item, list){
@@ -656,7 +674,7 @@
 void HgWidgetPrivate::gainedForeground()
 {
     if( mForeground ) return;
-    
+
     mForeground = true;
     QList<HgWidgetItem*> list = mContainer->items();
     int bufferStart = 0;
@@ -667,6 +685,13 @@
     }
 }
 
+void HgWidgetPrivate::updateCurrentItem(const QModelIndex &currentItem)
+{
+    Q_UNUSED(currentItem);
+
+    // By default do nothing
+}
+
 bool HgWidgetPrivate::getItemOutline(const QModelIndex& index, QPolygonF& points)
 {
     return mContainer->getItemPoints(index.row(), points);
@@ -685,7 +710,6 @@
         if (!mStaticScrollDirection) {
             createScrollBar(orientation);
         }
-        q->repolish();
         adjustGeometry();
     }
 }
@@ -696,6 +720,13 @@
     Q_UNUSED(orientation);
 }
 
+void HgWidgetPrivate::_q_updateCurrentItem(const QModelIndex &current, const QModelIndex &previous)
+{
+    Q_UNUSED(previous);
+
+    updateCurrentItem(current);
+}
+
 Qt::Orientation HgWidgetPrivate::scrollDirection() const
 {
     return mContainer->orientation();
@@ -704,7 +735,7 @@
 void HgWidgetPrivate::createScrollBar(Qt::Orientation orientation)
 {
     Q_Q(HgWidget);
-    
+
     delete mScrollBar;
     mScrollBar = 0;
     mScrollBar = new HbScrollBar(orientation,q);
@@ -712,7 +743,7 @@
         HbStyle::setItemName(mScrollBar, "scrollbar-vertical");
     }
     else {
-        HbStyle::setItemName(mScrollBar, "scrollbar-horizontal");    
+        HbStyle::setItemName(mScrollBar, "scrollbar-horizontal");
     }
 
     mScrollBar->setZValue(q->zValue() + 1);
@@ -761,6 +792,15 @@
         }
     }
     mContainer->itemDataChanged(topLeft, bottomRight);
+
+    if (mContainer->selectionModel()) {
+        QModelIndex currentIndex = mContainer->selectionModel()->currentIndex();
+        if (currentIndex.isValid() &&
+            currentIndex.row() >= topLeft.row() &&
+            currentIndex.row() <= bottomRight.row()) {
+            updateCurrentItem(currentIndex);
+        }
+    }
 }
 
 void HgWidgetPrivate::setItemSizePolicy(HgWidget::ItemSizePolicy policy)