src/hbwidgets/itemviews/hbgridview_p.cpp
changeset 0 16d8024aca5e
child 1 f7ac710697a9
equal deleted inserted replaced
-1:000000000000 0:16d8024aca5e
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2008-2010 Nokia Corporation and/or its subsidiary(-ies).
       
     4 ** All rights reserved.
       
     5 ** Contact: Nokia Corporation (developer.feedback@nokia.com)
       
     6 **
       
     7 ** This file is part of the HbWidgets module of the UI Extensions for Mobile.
       
     8 **
       
     9 ** GNU Lesser General Public License Usage
       
    10 ** This file may be used under the terms of the GNU Lesser General Public
       
    11 ** License version 2.1 as published by the Free Software Foundation and
       
    12 ** appearing in the file LICENSE.LGPL included in the packaging of this file.
       
    13 ** Please review the following information to ensure the GNU Lesser General
       
    14 ** Public License version 2.1 requirements will be met:
       
    15 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
       
    16 **
       
    17 ** In addition, as a special exception, Nokia gives you certain additional
       
    18 ** rights.  These rights are described in the Nokia Qt LGPL Exception
       
    19 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
       
    20 **
       
    21 ** If you have questions regarding the use of this file, please contact
       
    22 ** Nokia at developer.feedback@nokia.com.
       
    23 **
       
    24 ****************************************************************************/
       
    25 #include <hbgridview_p.h>
       
    26 #include <hbgridlayout_p.h>
       
    27 #include <hbgriditemcontainer_p.h>
       
    28 #include <hbgridviewitem_p.h>
       
    29 
       
    30 #include <hbgesturefilter.h>
       
    31 #include <hbgridviewitem.h>
       
    32 #include <hbscrollbar.h>
       
    33 
       
    34 #include "hbmodeliterator.h"
       
    35 
       
    36 #include <QDebug>
       
    37 #include <QGraphicsView>
       
    38 
       
    39 
       
    40 const QString KDefaultLayoutOption = "default";
       
    41 
       
    42 HbGridViewPrivate::HbGridViewPrivate() : 
       
    43     mIconVisible(true), 
       
    44     mTextVisible(true)
       
    45 {
       
    46 }
       
    47 
       
    48 HbGridViewPrivate::~HbGridViewPrivate()
       
    49 {
       
    50 }
       
    51 
       
    52 void HbGridViewPrivate::init()
       
    53 {
       
    54     Q_Q(HbGridView);
       
    55     q->setClampingStyle(q->BounceBackClamping);
       
    56     q->setScrollingStyle(q->PanOrFlick);
       
    57     q->setVerticalScrollBarPolicy(HbScrollArea::ScrollBarAlwaysOff);
       
    58     q->setHorizontalScrollBarPolicy(HbScrollArea::ScrollBarAlwaysOff);
       
    59     q->setFrictionEnabled(false);
       
    60     q->setFlag(QGraphicsItem::ItemClipsToShape);
       
    61     QObject::connect(q, SIGNAL(scrollDirectionsChanged(Qt::Orientations)),
       
    62                       q, SLOT(scrollDirectionChanged(Qt::Orientations)));
       
    63     mLayoutOptionName = KDefaultLayoutOption;
       
    64 }
       
    65 
       
    66 /*
       
    67  Utility function to find a item under certain scene pixel
       
    68  */
       
    69 QModelIndex HbGridViewPrivate::itemIndexUnder(const QPointF& point)
       
    70 {
       
    71     Q_Q ( HbGridView );
       
    72     QModelIndex ret = QModelIndex();
       
    73     HbAbstractViewItem* hitItem = 0;
       
    74     QList<QGraphicsItem *> items = q->scene()->items(point);
       
    75     if (!items.isEmpty()) {
       
    76         int count(items.count());
       
    77         for (int current(0); current < count; ++current) {
       
    78             QGraphicsItem *item = items.at(current);
       
    79             hitItem = viewItem(item);
       
    80             if (hitItem && hitItem->modelIndex().isValid()) {
       
    81                 ret = hitItem->modelIndex();
       
    82                 break;
       
    83             }
       
    84         }
       
    85     }
       
    86     return ret;
       
    87 }
       
    88 /*!
       
    89     \reimp
       
    90 */
       
    91 bool HbGridViewPrivate::visible(HbAbstractViewItem* item, bool fullyVisible) const
       
    92 {
       
    93     if (item && item->isVisible()
       
    94         && item->modelIndex().isValid()) {
       
    95         return HbAbstractItemViewPrivate::visible(item, fullyVisible);
       
    96     }
       
    97     return false;
       
    98 }
       
    99 
       
   100 
       
   101 void HbGridViewPrivate::setIconVisible(bool iconVisible)
       
   102 {
       
   103     if (iconVisible != mIconVisible) {
       
   104         mIconVisible = iconVisible;
       
   105         relayout();
       
   106     }
       
   107 }
       
   108 
       
   109 void HbGridViewPrivate::setTextVisible(bool textVisible)
       
   110 {
       
   111     if (textVisible != mTextVisible) {
       
   112         mTextVisible = textVisible;
       
   113         relayout();
       
   114     }
       
   115 }
       
   116 
       
   117 void HbGridViewPrivate::relayout()
       
   118 {
       
   119     QList<HbAbstractViewItem *> items = mContainer->items();
       
   120     foreach (HbAbstractViewItem *item, items) {
       
   121         HbGridViewItem *gridItem = qobject_cast<HbGridViewItem *>(item);
       
   122         if (gridItem) {
       
   123             gridItem->updateChildItems();
       
   124         }
       
   125     }
       
   126 }
       
   127 
       
   128 /*!
       
   129     Overwrites the default scroll area scrollbar updating algorithm when
       
   130     recycling is used. On recycling the scrollbar position & size is calculated
       
   131     using rows and their pixel size is not used.
       
   132 */
       
   133 void HbGridViewPrivate::updateScrollBar(Qt::Orientation orientation)
       
   134 {
       
   135     if (!mContainer->itemRecycling()
       
   136         || mContainer->itemPrototypes().count() != 1 
       
   137         || mContainer->items().count() == 0) {
       
   138         HbScrollAreaPrivate::updateScrollBar(orientation);
       
   139     } else {
       
   140         if (mContainer->layout() && !mContainer->layout()->isActivated()) {
       
   141             mContainer->layout()->activate();
       
   142         }
       
   143         if (orientation == Qt::Vertical) {
       
   144             updateVerticalScrollBar();
       
   145         } else {
       
   146             updateHorizontalScrollBar();
       
   147         }
       
   148     }
       
   149 } 
       
   150 
       
   151 void HbGridViewPrivate::updateHorizontalScrollBar()
       
   152 {    
       
   153     Q_Q(const HbGridView);
       
   154     
       
   155     HbAbstractViewItem *firstItem = mContainer->items().first();
       
   156     qreal uniformItemWidth = firstItem->size().width();
       
   157 
       
   158     int rowCount = q->rowCount();
       
   159     int indexCount = mModelIterator->indexCount();
       
   160 
       
   161     int virtualColumnCount = indexCount / rowCount;
       
   162     int remainder = indexCount % rowCount;
       
   163     if (remainder != 0) {
       
   164         virtualColumnCount++;
       
   165     }
       
   166 
       
   167     int firstItemModelRowNumber = mModelIterator->indexPosition(firstItem->modelIndex());
       
   168     int firstBufferItemRowNumber = firstItemModelRowNumber / rowCount; 
       
   169 
       
   170     QRectF itemRect = itemBoundingRect(firstItem);
       
   171     qreal realLeftBoundary = itemRect.left();   
       
   172     qreal virtualLeftBoundary = realLeftBoundary - (firstBufferItemRowNumber*uniformItemWidth); 
       
   173 
       
   174     qreal containerVirtualWidth = uniformItemWidth *  virtualColumnCount;
       
   175     qreal thumbPosition(0);
       
   176 
       
   177     // The scrollbar "thumb" position is the current position of the contents widget divided
       
   178     // by the difference between the width of the contents widget and the width of the scroll area.
       
   179     // This formula assumes that the "thumb" of the the scroll bar is sized proportionately to
       
   180     // the width of the contents widget.
       
   181     qreal hiddenVirtualWidth = containerVirtualWidth - q->boundingRect().width();
       
   182     if (hiddenVirtualWidth != 0) {
       
   183         thumbPosition = (-virtualLeftBoundary)  / hiddenVirtualWidth;
       
   184     }
       
   185 
       
   186     if (thumbPosition < 0.0)
       
   187         thumbPosition = 0.0;
       
   188     else if (thumbPosition > 1.0)
       
   189         thumbPosition = 1.0;
       
   190 
       
   191     if (mHorizontalScrollBar) {
       
   192         if (containerVirtualWidth!=0) {
       
   193             mHorizontalScrollBar->setPageSize(qBound ( (qreal)0.0,
       
   194                                      q->boundingRect().width() / containerVirtualWidth,
       
   195                                       (qreal)1.0));
       
   196         }
       
   197         mHorizontalScrollBar->setValue(thumbPosition); 
       
   198     }
       
   199 }
       
   200 
       
   201 void HbGridViewPrivate::updateVerticalScrollBar()
       
   202 {
       
   203     Q_Q(const HbGridView);
       
   204 
       
   205     HbAbstractViewItem *firstItem = mContainer->items().first();
       
   206     qreal uniformItemHeight = firstItem->size().height();
       
   207 
       
   208     int columnCount = q->columnCount();
       
   209     int indexCount = mModelIterator->indexCount();
       
   210 
       
   211     int virtualRowCount = indexCount / columnCount;
       
   212     int remainder = indexCount % columnCount;
       
   213     if (remainder != 0) {  //even one item requires the whole row
       
   214         virtualRowCount++;
       
   215     }
       
   216 
       
   217     int firstItemModelRowNumber = mModelIterator->indexPosition(firstItem->modelIndex());
       
   218     int firstBufferItemRowNumber = firstItemModelRowNumber / columnCount; 
       
   219 
       
   220     QRectF itemRect = itemBoundingRect(firstItem);
       
   221     qreal realTopBoundary = itemRect.top();
       
   222     qreal virtualTopBoundary = realTopBoundary - (firstBufferItemRowNumber*uniformItemHeight); 
       
   223 
       
   224     qreal containerVirtualHeight = uniformItemHeight *  virtualRowCount;
       
   225     qreal thumbPosition = 0;
       
   226 
       
   227     // The scrollbar "thumb" position is the current position of the contents widget divided
       
   228     // by the difference between the height of the contents widget and the height of the scroll area.
       
   229     // This formula assumes that the "thumb" of the the scroll bar is sized proportionately to
       
   230     // the height of the contents widget.
       
   231     qreal hiddenVirtualHeight = containerVirtualHeight - q->boundingRect().height();
       
   232     if (hiddenVirtualHeight != 0) {
       
   233         thumbPosition = (-virtualTopBoundary) / hiddenVirtualHeight;
       
   234     } 
       
   235 
       
   236     if (thumbPosition < 0.0)
       
   237         thumbPosition = 0.0;
       
   238     else if (thumbPosition > 1.0)
       
   239         thumbPosition = 1.0;
       
   240 
       
   241     if (mVerticalScrollBar) {
       
   242         if (containerVirtualHeight!=0) {
       
   243             mVerticalScrollBar->setPageSize(qBound ( (qreal)0.0,
       
   244                                      q->boundingRect().height() / containerVirtualHeight,
       
   245                                       (qreal)1.0));
       
   246         }
       
   247         mVerticalScrollBar->setValue(thumbPosition); 
       
   248     }    
       
   249 }
       
   250 
       
   251 void HbGridViewPrivate::setScrollBarMetrics(Qt::Orientation orientation)
       
   252 {   
       
   253     if (!mContainer->itemRecycling()
       
   254         || mContainer->itemPrototypes().count() != 1 
       
   255         || mContainer->items().count() == 0)  {
       
   256         HbScrollAreaPrivate::setScrollBarMetrics(orientation);
       
   257     } else {
       
   258         //We just make sure that the base clas is not called
       
   259         //It set the page size wrongly
       
   260         updateScrollBar(orientation); 
       
   261     }
       
   262 } 
       
   263 
       
   264 void HbGridViewPrivate::setContentPosition( qreal value, Qt::Orientation orientation, bool animate )
       
   265 {
       
   266     Q_Q( HbGridView );
       
   267 
       
   268     if (mContainer->itemRecycling() && mModelIterator->model()) {
       
   269         if (mContainer->layout() && !mContainer->layout()->isActivated()) {
       
   270             mContainer->layout()->activate();
       
   271         }
       
   272 
       
   273         qreal filteredValue = (int)(value * 1000) / 1000.0;        
       
   274 
       
   275         HbAbstractViewItem *firstItem = mContainer->items().first();
       
   276 
       
   277         qreal uniformItemDimension = 0; // width or height of item
       
   278         qreal dimension = 0; // width or height of view
       
   279         int dimensionCount = 0; // rowcount or columncount
       
   280         qreal posInBeginning = 0; // top or left position of first item in buffer
       
   281 
       
   282         if (orientation == Qt::Vertical) {
       
   283             posInBeginning = itemBoundingRect(firstItem).top();
       
   284             uniformItemDimension = firstItem->size().height();
       
   285             dimension = q->boundingRect().height();
       
   286             dimensionCount = q->columnCount();
       
   287         } else {
       
   288             posInBeginning = itemBoundingRect(firstItem).left();
       
   289             uniformItemDimension = firstItem->size().width();
       
   290             dimension = q->boundingRect().width();
       
   291             dimensionCount = q->rowCount();
       
   292         }
       
   293 
       
   294         int indexCount = mModelIterator->indexCount();
       
   295         int virtualCount = indexCount / dimensionCount; // amount of rows/columns in "complete" grid
       
   296         int remainder = indexCount % dimensionCount;
       
   297         if (remainder != 0) {  //even one item requires the whole row
       
   298             virtualCount++;
       
   299         }
       
   300 
       
   301         qreal target = virtualCount * filteredValue;  // target position in "complete" grid (in rows/columns)
       
   302         int virtualItemCount = virtualCount * dimensionCount; // item count when all the "empty" items are also counted in
       
   303         qreal posToBeInView = dimension * filteredValue; 
       
   304 
       
   305         QModelIndex newIndex = mModelIterator->index(qMin((int)(virtualItemCount * filteredValue), indexCount - 1));
       
   306 
       
   307         if (!mContainer->itemByIndex(newIndex)) {
       
   308             //jump
       
   309             int itemsInBuffer = mContainer->items().count();
       
   310 
       
   311             int newBufferStartItem = (int)(virtualItemCount * filteredValue) - qMin(itemsInBuffer - 1, (int)(itemsInBuffer * filteredValue));
       
   312             mContainer->setModelIndexes(mModelIterator->index(newBufferStartItem));
       
   313             int newBufferStartRow = newBufferStartItem / dimensionCount;
       
   314 
       
   315             qreal posToBeInBuffer = ((target - newBufferStartRow) * uniformItemDimension);
       
   316 
       
   317             qreal posToBe = posToBeInView - posToBeInBuffer;
       
   318 
       
   319             if (orientation == Qt::Vertical) {
       
   320                 HbScrollAreaPrivate::setContentPosition(QPointF(0, posToBe)); 
       
   321             } else {
       
   322                 HbScrollAreaPrivate::setContentPosition(QPointF(posToBe, 0)); 
       
   323             }
       
   324         } else {
       
   325             // scroll
       
   326             int firstItemRow = mContainer->items().first()->modelIndex().row() / dimensionCount;
       
   327 
       
   328             qreal posToBeInBuffer = (target - firstItemRow) * uniformItemDimension;
       
   329             
       
   330             qreal posToBe = posToBeInView - posToBeInBuffer;
       
   331 
       
   332             if (orientation == Qt::Vertical) {
       
   333                 q->scrollByAmount(QPointF(0, posInBeginning - posToBe));
       
   334             } else {
       
   335                 q->scrollByAmount(QPointF(posInBeginning - posToBe, 0));
       
   336             }
       
   337         }
       
   338     } else {
       
   339         HbScrollAreaPrivate::setContentPosition(value, orientation, animate);
       
   340     }
       
   341        
       
   342     if (animate) {
       
   343         updateScrollBar(orientation);
       
   344     }
       
   345 }
       
   346 
       
   347 QModelIndex HbGridViewPrivate::indexInTheCenter(Qt::Orientations scrollDirection) const
       
   348 {
       
   349     Q_Q(const HbGridView);
       
   350     QModelIndex centerViewModelIndex; // nearest to center of the screen
       
   351     if (!mContainer->items().isEmpty()) {
       
   352         qreal centerPos;
       
   353         HbGridItemContainer *container = itemContainer();
       
   354         if (scrollDirection == Qt::Vertical) {
       
   355             // calculating row:
       
   356             centerPos = -container->pos().y() + q->boundingRect().height() / 2
       
   357                         // take rather item that is just above of view center instead of bottom one
       
   358                         - container->viewLayout()->effectiveSizeHint(Qt::MinimumSize).height() / 2;
       
   359             // calculate item row
       
   360             centerPos /= container->viewLayout()->effectiveSizeHint(Qt::MinimumSize).height();
       
   361             //calculate item index
       
   362             centerPos *= itemContainer()->columnCount();
       
   363             // go to center column
       
   364             centerPos += itemContainer()->columnCount() / 2;
       
   365         } else {
       
   366             // calculating row:
       
   367             centerPos = -container->pos().x() + q->boundingRect().width() / 2
       
   368                         // take rather item that is just on the left of view center instead of right one
       
   369                         - container->viewLayout()->effectiveSizeHint(Qt::MinimumSize).width() / 2;
       
   370             // calculate buffer row
       
   371             centerPos /= container->viewLayout()->effectiveSizeHint(Qt::MinimumSize).width();
       
   372             //calculate item index
       
   373             centerPos *= itemContainer()->rowCount();
       
   374             // go to center row
       
   375             centerPos += itemContainer()->rowCount() / 2;
       
   376         }
       
   377         int centerIndex = qRound(centerPos);
       
   378         if (centerIndex >= container->items().size()) {
       
   379             centerIndex = container->items().size() / 2;
       
   380         }
       
   381         centerViewModelIndex = container->items().at(centerIndex)->modelIndex();
       
   382     }
       
   383     return centerViewModelIndex;
       
   384 }