src/declarative/graphicsitems/qdeclarativegridview.cpp
changeset 37 758a864f9613
parent 33 3e2da88830cd
equal deleted inserted replaced
36:ef0373b55136 37:758a864f9613
    62 {
    62 {
    63 public:
    63 public:
    64     FxGridItem(QDeclarativeItem *i, QDeclarativeGridView *v) : item(i), view(v) {
    64     FxGridItem(QDeclarativeItem *i, QDeclarativeGridView *v) : item(i), view(v) {
    65         attached = static_cast<QDeclarativeGridViewAttached*>(qmlAttachedPropertiesObject<QDeclarativeGridView>(item));
    65         attached = static_cast<QDeclarativeGridViewAttached*>(qmlAttachedPropertiesObject<QDeclarativeGridView>(item));
    66         if (attached)
    66         if (attached)
    67             attached->m_view = view;
    67             attached->setView(view);
    68     }
    68     }
    69     ~FxGridItem() {}
    69     ~FxGridItem() {}
    70 
    70 
    71     qreal rowPos() const { return (view->flow() == QDeclarativeGridView::LeftToRight ? item->y() : item->x()); }
    71     qreal rowPos() const { return (view->flow() == QDeclarativeGridView::LeftToRight ? item->y() : item->x()); }
    72     qreal colPos() const { return (view->flow() == QDeclarativeGridView::LeftToRight ? item->x() : item->y()); }
    72     qreal colPos() const { return (view->flow() == QDeclarativeGridView::LeftToRight ? item->x() : item->y()); }
  1054 //----------------------------------------------------------------------------
  1054 //----------------------------------------------------------------------------
  1055 
  1055 
  1056 /*!
  1056 /*!
  1057     \qmlclass GridView QDeclarativeGridView
  1057     \qmlclass GridView QDeclarativeGridView
  1058     \since 4.7
  1058     \since 4.7
       
  1059     \ingroup qml-view-elements
       
  1060 
  1059     \inherits Flickable
  1061     \inherits Flickable
  1060     \brief The GridView item provides a grid view of items provided by a model.
  1062     \brief The GridView item provides a grid view of items provided by a model.
  1061 
  1063 
  1062     A GridView displays data from models created from built-in QML elements like ListModel
  1064     A GridView displays data from models created from built-in QML elements like ListModel
  1063     and XmlListModel, or custom model classes defined in C++ that inherit from
  1065     and XmlListModel, or custom model classes defined in C++ that inherit from
  1083     (containing \l Image and \ Text elements) for its delegate. The view will create a new delegate 
  1085     (containing \l Image and \ Text elements) for its delegate. The view will create a new delegate 
  1084     for each item in the model. Notice the delegate is able to access the model's \c name and 
  1086     for each item in the model. Notice the delegate is able to access the model's \c name and 
  1085     \c portrait data directly.
  1087     \c portrait data directly.
  1086 
  1088 
  1087     An improved grid view is shown below. The delegate is visually improved and is moved 
  1089     An improved grid view is shown below. The delegate is visually improved and is moved 
  1088     into a separate \c contactDelegate component. Also, the currently selected item is highlighted
  1090     into a separate \c contactDelegate component.
  1089     with a blue \l Rectangle using the \l highlight property, and \c focus is set to \c true
       
  1090     to enable keyboard navigation for the grid view.
       
  1091     
  1091     
  1092     \snippet doc/src/snippets/declarative/gridview/gridview.qml classdocs advanced
  1092     \snippet doc/src/snippets/declarative/gridview/gridview.qml classdocs advanced
  1093     \image gridview-highlight.png
  1093     \image gridview-highlight.png
       
  1094 
       
  1095     The currently selected item is highlighted with a blue \l Rectangle using the \l highlight property,
       
  1096     and \c focus is set to \c true to enable keyboard navigation for the grid view.
       
  1097     The grid view itself is a focus scope (see \l{qmlfocus#Acquiring Focus and Focus Scopes}{the focus documentation page} for more details).
  1094 
  1098 
  1095     Delegates are instantiated as needed and may be destroyed at any time.
  1099     Delegates are instantiated as needed and may be destroyed at any time.
  1096     State should \e never be stored in a delegate.
  1100     State should \e never be stored in a delegate.
  1097 
  1101 
  1098     \note Views do not enable \e clip automatically.  If the view
  1102     \note Views do not enable \e clip automatically.  If the view
  1274         d->ownModel = true;
  1278         d->ownModel = true;
  1275     }
  1279     }
  1276     if (QDeclarativeVisualDataModel *dataModel = qobject_cast<QDeclarativeVisualDataModel*>(d->model)) {
  1280     if (QDeclarativeVisualDataModel *dataModel = qobject_cast<QDeclarativeVisualDataModel*>(d->model)) {
  1277         dataModel->setDelegate(delegate);
  1281         dataModel->setDelegate(delegate);
  1278         if (isComponentComplete()) {
  1282         if (isComponentComplete()) {
       
  1283             for (int i = 0; i < d->visibleItems.count(); ++i)
       
  1284                 d->releaseItem(d->visibleItems.at(i));
       
  1285             d->visibleItems.clear();
       
  1286             d->releaseItem(d->currentItem);
       
  1287             d->currentItem = 0;
  1279             refill();
  1288             refill();
  1280             d->moveReason = QDeclarativeGridViewPrivate::SetIndex;
  1289             d->moveReason = QDeclarativeGridViewPrivate::SetIndex;
  1281             d->updateCurrent(d->currentIndex);
  1290             d->updateCurrent(d->currentIndex);
  1282             if (d->highlight && d->currentItem) {
  1291             if (d->highlight && d->currentItem) {
  1283                 d->highlight->setPosition(d->currentItem->colPos(), d->currentItem->rowPos());
  1292                 d->highlight->setPosition(d->currentItem->colPos(), d->currentItem->rowPos());
  2201                     pos = trackedPos - d->highlightRangeStart;
  2210                     pos = trackedPos - d->highlightRangeStart;
  2202             } else {
  2211             } else {
  2203                 if (trackedPos < d->startPosition() + d->highlightRangeStart) {
  2212                 if (trackedPos < d->startPosition() + d->highlightRangeStart) {
  2204                     pos = d->startPosition();
  2213                     pos = d->startPosition();
  2205                 } else if (d->trackedItem->endRowPos() > d->endPosition() - d->size() + d->highlightRangeEnd) {
  2214                 } else if (d->trackedItem->endRowPos() > d->endPosition() - d->size() + d->highlightRangeEnd) {
  2206                     pos = d->endPosition() - d->size();
  2215                     pos = d->endPosition() - d->size() + 1;
  2207                     if (pos < d->startPosition())
  2216                     if (pos < d->startPosition())
  2208                         pos = d->startPosition();
  2217                         pos = d->startPosition();
  2209                 } else {
  2218                 } else {
  2210                     if (trackedPos < viewPos + d->highlightRangeStart) {
  2219                     if (trackedPos < viewPos + d->highlightRangeStart) {
  2211                         pos = trackedPos - d->highlightRangeStart;
  2220                         pos = trackedPos - d->highlightRangeStart;
  2215                 }
  2224                 }
  2216             }
  2225             }
  2217         } else {
  2226         } else {
  2218             if (trackedPos < viewPos && d->currentItem->rowPos() < viewPos) {
  2227             if (trackedPos < viewPos && d->currentItem->rowPos() < viewPos) {
  2219                 pos = d->currentItem->rowPos() < trackedPos ? trackedPos : d->currentItem->rowPos();
  2228                 pos = d->currentItem->rowPos() < trackedPos ? trackedPos : d->currentItem->rowPos();
  2220             } else if (d->trackedItem->endRowPos() > viewPos + d->size()
  2229             } else if (d->trackedItem->endRowPos() >= viewPos + d->size()
  2221                 && d->currentItem->endRowPos() > viewPos + d->size()) {
  2230                 && d->currentItem->endRowPos() >= viewPos + d->size()) {
  2222                 if (d->trackedItem->endRowPos() < d->currentItem->endRowPos()) {
  2231                 if (d->trackedItem->endRowPos() <= d->currentItem->endRowPos()) {
  2223                     pos = d->trackedItem->endRowPos() - d->size();
  2232                     pos = d->trackedItem->endRowPos() - d->size() + 1;
  2224                     if (d->rowSize() > d->size())
  2233                     if (d->rowSize() > d->size())
  2225                         pos = trackedPos;
  2234                         pos = trackedPos;
  2226                 } else {
  2235                 } else {
  2227                     pos = d->currentItem->endRowPos() - d->size();
  2236                     pos = d->currentItem->endRowPos() - d->size() + 1;
  2228                     if (d->rowSize() > d->size())
  2237                     if (d->rowSize() > d->size())
  2229                         pos = d->currentItem->rowPos();
  2238                         pos = d->currentItem->rowPos();
  2230                 }
  2239                 }
  2231             }
  2240             }
  2232         }
  2241         }