src/hbwidgets/itemviews/hbgridview.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.h>
       
    26 #include <hbgridview_p.h>
       
    27 #include <hbgridlayout_p.h>
       
    28 #include <hbabstractviewitem.h>
       
    29 #include <hbeffect.h>
       
    30 #include <hbmodeliterator.h>
       
    31 
       
    32 #include <QGraphicsSceneMouseEvent>
       
    33 
       
    34 /*!
       
    35  @beta
       
    36  @hbwidgets
       
    37  \class HbGridView
       
    38  \brief HbGridView implements a grid view. Data is represented by HbGridViewItem.
       
    39  HbGridView is an HbAbstractItemView implementation using the model/view design pattern. 
       
    40 
       
    41  Since HbGridView is derived from HbAbstractItemView it can use QAbstractItemModel-derived implementations
       
    42  (for example QStandardItemModel) as a model.
       
    43 
       
    44  See HbAbstractItemView for emitted signals (touch events & item activation).
       
    45  
       
    46  <b> Scrolling Directions </b>
       
    47  
       
    48  Scrolling can be set to either a \link Qt::Horizontal \endlink or \link Qt::Vertical \endlink direction.
       
    49  Items are arranged according to the scroll direction i.e Qt::Horizontal lays out items 
       
    50  horizontally and Qt::Vertical lays out items vertically.
       
    51 
       
    52  By default the HbScrollArea::ClampingStyle used is BounceBackClamping, the HbScrollArea::ScrollingStyle is PanOrFlick,
       
    53  scrollbars are invisible and recycling and inertia are disabled.
       
    54 
       
    55  The following code snippet demonstrates the use of HbGridView
       
    56  \snippet{ultimatecodesnippet/ultimatecodesnippet.cpp,21}
       
    57  */
       
    58 
       
    59 /*!
       
    60  GridView recycling is in proto state.
       
    61  */
       
    62 
       
    63 /*!    
       
    64   See also  HbAbstractItemView,HbAbstractViewitem,HbGridViewItem,HbScrollArea
       
    65  */
       
    66 
       
    67      
       
    68 /*!
       
    69  Constructs a new HbGridView with \a parent.
       
    70  */
       
    71 HbGridView::HbGridView(QGraphicsItem *parent) :
       
    72     HbAbstractItemView(*new HbGridViewPrivate(), new HbGridItemContainer(), new HbModelIterator(), parent)
       
    73 {
       
    74     Q_D( HbGridView );
       
    75     d->q_ptr = this;
       
    76     d->init();
       
    77 }
       
    78 
       
    79 HbGridView::HbGridView(HbGridViewPrivate &dd, HbAbstractItemContainer *container, QGraphicsItem *parent) :
       
    80     HbAbstractItemView(dd, container, new HbModelIterator(), parent)
       
    81 {
       
    82     Q_D( HbGridView );
       
    83     d->q_ptr = this;
       
    84     d->init();
       
    85 }
       
    86 
       
    87 HbGridView::~HbGridView()
       
    88 {
       
    89 }
       
    90 
       
    91 /*!
       
    92  \property HbGridView::rowCount()
       
    93  \brief Returns the total number of rows in the view.
       
    94  \sa HbGridView::setRowCount()
       
    95  */
       
    96 int HbGridView::rowCount() const
       
    97 {
       
    98     Q_D(const HbGridView);
       
    99     return d->itemContainer()->rowCount();
       
   100 }
       
   101 
       
   102 /*!
       
   103  \property HbGridView::setRowCount()
       
   104  \brief Sets the total number of rows to \a rowCount.
       
   105  \sa HbGridView::rowCount()
       
   106  */
       
   107 void HbGridView::setRowCount(int rowCount)
       
   108 {
       
   109     Q_D(HbGridView);
       
   110 
       
   111     d->mVisibleIndex = d->indexInTheCenter();
       
   112 
       
   113     d->itemContainer()->setRowCount(rowCount);
       
   114 
       
   115     scrollTo(d->mVisibleIndex, HbAbstractItemView::PositionAtCenter);
       
   116 }
       
   117 
       
   118 /*!
       
   119  \property HbGridView::columnCount
       
   120  \brief Returns the total number of columns in the view.
       
   121  \sa HbGridView::setColumnCount()
       
   122  */
       
   123 int HbGridView::columnCount() const
       
   124 {
       
   125     Q_D(const HbGridView);
       
   126     return d->itemContainer()->columnCount();
       
   127 }
       
   128 
       
   129 /*!
       
   130  \property HbGridView::setColumnCount
       
   131  \brief Sets the total number of columns to \a columnCount.
       
   132   \sa HbGridView::columnCount()
       
   133  */
       
   134 void HbGridView::setColumnCount(int columnCount)
       
   135 {
       
   136     Q_D(HbGridView);
       
   137 
       
   138     d->mVisibleIndex = d->indexInTheCenter();
       
   139 
       
   140     d->itemContainer()->setColumnCount(columnCount);
       
   141 
       
   142     scrollTo(d->mVisibleIndex, HbAbstractItemView::PositionAtCenter);
       
   143 }
       
   144 
       
   145 
       
   146 /*!
       
   147  \property HbGridView::iconVisible()
       
   148  \brief Returns true if icons are currently displayed in GridView.
       
   149  \sa HbGridView::setIconVisible()
       
   150  */
       
   151 bool HbGridView::iconVisible() const
       
   152 {
       
   153     Q_D(const HbGridView);
       
   154     return d->mIconVisible;
       
   155 }
       
   156 
       
   157 /*!
       
   158  \property HbGridView::setIconVisible()
       
   159  \brief Set visibility of icons in GridView to \a visible.
       
   160         All view items are updated automatically.
       
   161  \sa HbGridView::iconVisible()
       
   162  */
       
   163 void HbGridView::setIconVisible(bool visible)
       
   164 {
       
   165     Q_D(HbGridView);
       
   166     d->setIconVisible(visible);
       
   167 }
       
   168 
       
   169 /*!
       
   170  \property HbGridView::textVisible()
       
   171  \brief  Returns visibility of labels in GridView.
       
   172  \sa HbGridView::setTextVisible()
       
   173  */
       
   174 bool HbGridView::textVisible() const
       
   175 {
       
   176     Q_D(const HbGridView);
       
   177     return d->mTextVisible;
       
   178 }
       
   179 
       
   180 /*!
       
   181  \property HbGridView::setTextVisible()
       
   182  \brief  Sets visibility of labels in GridView to \a visible.
       
   183          All view items are updated automatically.
       
   184  \sa HbGridView::textVisible()
       
   185  */
       
   186 void HbGridView::setTextVisible(bool visible)
       
   187 {
       
   188     Q_D(HbGridView);
       
   189     d->setTextVisible(visible);
       
   190 }
       
   191 
       
   192 /*!
       
   193  Returns item at \a row and \a column.
       
   194  */
       
   195 HbAbstractViewItem *HbGridView::itemAt(int row, int column) const
       
   196 {
       
   197     Q_D(const HbGridView);
       
   198    if (     row >= 0
       
   199        &&   column >= 0
       
   200        &&   column < d->itemContainer()->columnCount()
       
   201        &&   d->mModelIterator->model()) {
       
   202         int index  = row*d->itemContainer()->columnCount() + column;
       
   203         return d->itemContainer()->itemByIndex(d->mModelIterator->index(index));
       
   204    }
       
   205    return 0;
       
   206 }
       
   207 
       
   208 /*!
       
   209  Returns item at \a index.
       
   210  Ownership of the item is not transferred.
       
   211 
       
   212  \deprecated HbGridView::itemAt(int) const
       
   213      is deprecated. Please use HbAbstractItemView::itemByIndex(const QModelIndex &index) instead.
       
   214 
       
   215  \sa HbAbstractItemView::itemByIndex(const QModelIndex &index)
       
   216 
       
   217  */
       
   218 HbAbstractViewItem *HbGridView::itemAt(int index) const
       
   219 {
       
   220     Q_D(const HbGridView);
       
   221 
       
   222     if (d->mModelIterator->model()) {
       
   223         return d->itemContainer()->itemByIndex(d->mModelIterator->index(index));
       
   224     }
       
   225     return 0;
       
   226 }
       
   227 
       
   228 /*!
       
   229  \reimp
       
   230  */
       
   231 void HbGridView::scrollTo(const QModelIndex &index, ScrollHint hint)
       
   232 {
       
   233     Q_D(HbGridView);
       
   234     // always use container, event if recycling is off and all items are 
       
   235     // in container, but still aditional action is needed -
       
   236     // container::scrollTo is responsible for procesing all
       
   237     // posponed events (DelayedLayoutRequest)
       
   238     if (    d->mModelIterator->model()
       
   239         &&  index.model() == d->mModelIterator->model()) {
       
   240         d->itemContainer()->scrollTo(index, hint);
       
   241         HbAbstractItemView::scrollTo(index, hint);
       
   242     }
       
   243 }
       
   244 
       
   245 /*!
       
   246     \reimp
       
   247 */
       
   248 void HbGridView::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
       
   249 {
       
   250     // overriding HbAbstractitemView's selection behaviour
       
   251     HbScrollArea::mouseMoveEvent( event );
       
   252     event->accept();
       
   253 }
       
   254 
       
   255 /*!
       
   256  \reimp
       
   257  */
       
   258 void HbGridView::orientationAboutToBeChanged()
       
   259 {
       
   260     Q_D(HbGridView);
       
   261     d->mVisibleIndex = d->indexInTheCenter();
       
   262 }
       
   263 
       
   264 /*!
       
   265  \reimp
       
   266  */
       
   267 void HbGridView::orientationChanged(Qt::Orientation newOrientation)
       
   268 {
       
   269     Q_D(HbGridView);
       
   270     d->mContainer->setPos(0,0);
       
   271     d->itemContainer()->orientationChanged(newOrientation);
       
   272 
       
   273     scrollTo(d->mVisibleIndex, HbAbstractItemView::PositionAtCenter);
       
   274 
       
   275     d->mVisibleIndex = QModelIndex();
       
   276 }
       
   277 
       
   278 /*!
       
   279     \reimp
       
   280 */
       
   281 void HbGridView::rowsInserted(const QModelIndex &parent, int start, int end)
       
   282 {
       
   283     Q_D(HbGridView);
       
   284 
       
   285     if (parent == d->mModelIterator->rootIndex()) {
       
   286         HbAbstractItemView::rowsInserted(parent, start, end);
       
   287 
       
   288         bool animate = d->mEnabledAnimations & HbAbstractItemView::Appear ? d->mAnimateItems : false;
       
   289         if (animate) {
       
   290             if (d->mAppearAnimationIndexes.count()) {
       
   291                 d->mAppearAnimationIndexes.clear();
       
   292             }
       
   293 
       
   294             for (int i = start; i <= end; i++) {
       
   295                 QPersistentModelIndex index = d->mModelIterator->index(i, parent);
       
   296                 HbAbstractViewItem *item = itemByIndex(index);
       
   297                 if (item) {
       
   298                     d->mAppearAnimationIndexes.append(index);
       
   299                 }
       
   300             }
       
   301         }
       
   302     }
       
   303 }
       
   304 
       
   305 /*!
       
   306     \reimp
       
   307 */
       
   308 void HbGridView::rowsAboutToBeRemoved(const QModelIndex &parent, int start, int end)
       
   309 {
       
   310     Q_D(HbGridView);
       
   311 
       
   312     if (parent == d->mModelIterator->rootIndex() && d->mModelIterator->model()) {
       
   313         for (int i = start; i <= end; i++) {
       
   314             HbAbstractViewItem* item = d->mContainer->itemByIndex(d->mModelIterator->index(i));
       
   315             if (item) {
       
   316                 bool animate = d->mEnabledAnimations & HbAbstractItemView::Disappear ? d->mAnimateItems : false;
       
   317                 if (animate) {
       
   318                     d->mItemsAboutToBeDeleted.append(item);
       
   319                 }
       
   320             }
       
   321         }
       
   322     }
       
   323 }
       
   324 
       
   325 /*!
       
   326     \reimp
       
   327 */
       
   328 void HbGridView::rowsRemoved(const QModelIndex &parent, int start, int end)
       
   329 {
       
   330     Q_D(HbGridView);
       
   331 
       
   332     if (parent == d->mModelIterator->rootIndex()) {
       
   333         bool animate = d->mEnabledAnimations & HbAbstractItemView::Disappear ? d->mAnimateItems: false;
       
   334         if (animate) {
       
   335             for (int i = 0; i < d->mItemsAboutToBeDeleted.count(); i++) {
       
   336                 QGraphicsItem *item = d->mItemsAboutToBeDeleted.at(i);
       
   337                 HbEffect::cancel(item, "appear");
       
   338                 HbEffect::start(item,
       
   339                                 "gridviewitem", 
       
   340                                 "disappear",
       
   341                                 d->mContainer,
       
   342                                 "animationFinished");    
       
   343             }
       
   344             d->mItemsAboutToBeDeleted.clear();
       
   345         }
       
   346         HbAbstractItemView::rowsRemoved(parent, start, end);
       
   347     }
       
   348 }
       
   349 
       
   350 /*!
       
   351     \reimp 
       
   352 */
       
   353 void HbGridView::dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight)
       
   354 {
       
   355     Q_D(const HbGridView);
       
   356 
       
   357     QList<HbAbstractViewItem *> items = d->mContainer->items();
       
   358 
       
   359     if (!items.isEmpty()
       
   360         && topLeft.parent() == d->mModelIterator->rootIndex()
       
   361         && items.first()->modelIndex().row() <= bottomRight.row()
       
   362         && topLeft.row() <= d->itemContainer()->lastValidItemIndex().row()) {
       
   363         HbAbstractItemView::dataChanged(topLeft, bottomRight);
       
   364     }
       
   365 }
       
   366 
       
   367 /*!
       
   368     This slot is connected to scrollDirectionsChanged signal emitted from HbScrollArea.
       
   369  */
       
   370 void HbGridView::scrollDirectionChanged(Qt::Orientations scrollDirection)
       
   371 {
       
   372     Q_D(HbGridView);
       
   373     // scroll direction changed, calculations need to be done on old value
       
   374     d->mVisibleIndex = d->indexInTheCenter((d->mScrollDirections == Qt::Vertical) 
       
   375         ? Qt::Horizontal : Qt::Vertical);
       
   376     d->mContainer->setPos(0,0);
       
   377     d->itemContainer()->scrollDirectionChanged(scrollDirection);
       
   378 
       
   379     scrollTo(d->mVisibleIndex, HbAbstractItemView::PositionAtCenter);
       
   380 
       
   381     d->mVisibleIndex = QModelIndex();
       
   382 }
       
   383 
       
   384 #include "moc_hbgridview.cpp"