contentstorage/caclient/src/caitemmodel.cpp
changeset 61 8e5041d13c84
parent 60 f62f87b200ec
child 66 32469d7d46ff
equal deleted inserted replaced
60:f62f87b200ec 61:8e5041d13c84
   704     CACLIENTTEST_FUNC_ENTRY("CaItemModelPrivate::updateItemData");
   704     CACLIENTTEST_FUNC_ENTRY("CaItemModelPrivate::updateItemData");
   705 
   705 
   706     mEntries.updateEntry(id);
   706     mEntries.updateEntry(id);
   707 
   707 
   708     QList<int> ids = mService->getEntryIds(mQuery);
   708     QList<int> ids = mService->getEntryIds(mQuery);
   709     if (mEntries.indexOf(id) >= 0 && ids.indexOf(id)
   709     if (mEntries.indexOf(id) >= 0 
   710             == mEntries.indexOf(id)) {
   710            && ids.indexOf(id) == mEntries.indexOf(id)) {
   711         emit m_q->dataChanged(index(mEntries.indexOf(id)), index(
   711         emit m_q->dataChanged(index(mEntries.indexOf(id)), index(
   712                                   mEntries.indexOf(id)));
   712                                   mEntries.indexOf(id)));
       
   713     } else if (ids.indexOf(id) < 0){
       
   714         removeItem(id);
       
   715     } else if (mEntries.indexOf(id) < 0){
       
   716         addItem(id);  
       
   717     } else if (mParentEntry && id == mParentEntry->id()) {
       
   718         updateParentEntry();
       
   719         m_q->reset();
   713     } else {
   720     } else {
   714         if (mParentEntry && id == mParentEntry->id()) {
   721         updateModel();
   715             updateParentEntry();
       
   716             m_q->reset();
       
   717         } else {
       
   718             updateLayout();
       
   719         }
       
   720     }
   722     }
   721     CACLIENTTEST_FUNC_EXIT("CaItemModelPrivate::updateItemData");
   723     CACLIENTTEST_FUNC_EXIT("CaItemModelPrivate::updateItemData");
   722 }
   724 }
   723 
   725 
   724 /*!
   726 /*!
   749     CACLIENTTEST_FUNC_ENTRY("CaItemModelPrivate::handleAddItems");
   751     CACLIENTTEST_FUNC_ENTRY("CaItemModelPrivate::handleAddItems");
   750 
   752 
   751     int entriesCount = mEntries.count();
   753     int entriesCount = mEntries.count();
   752     if (entriesCount) {
   754     if (entriesCount) {
   753         int lastRow = itemsList.indexOf(mEntries[entriesCount - 1]);
   755         int lastRow = itemsList.indexOf(mEntries[entriesCount - 1]);
   754         if (itemsList.count() == entriesCount && lastRow == (entriesCount
   756         if (itemsList.count() == entriesCount) {
   755                 - 1)) {
       
   756             //count is same and last item is in same position
   757             //count is same and last item is in same position
   757             //so we update whole model
   758             //so we update whole model
   758             updateModel();
   759             bool orderChanged(false);
       
   760             while (entriesCount) {
       
   761                 if (itemsList.indexOf(mEntries[entriesCount - 1]) 
       
   762                         != (entriesCount - 1)) {
       
   763                     orderChanged = true;
       
   764                     break;
       
   765                 }
       
   766                 entriesCount--;
       
   767             }
       
   768             if (orderChanged) {
       
   769                 updateLayout();
       
   770             }
       
   771             else {
       
   772                 updateModel();
       
   773             }
       
   774             
   759         } else if ((itemsList.count() - entriesCount) == 1 && lastRow
   775         } else if ((itemsList.count() - entriesCount) == 1 && lastRow
   760                    == entriesCount) {
   776                    == entriesCount) {
   761             //just one item added - collection
   777             //just one item added - collection
   762             int i = 0;
   778             int i = 0;
   763             for (i = 0; i < entriesCount; i++) {
   779             for (i = 0; i < entriesCount; i++) {
   771             while (i < itemsList.count()) {
   787             while (i < itemsList.count()) {
   772                 addItem(itemsList[i]);
   788                 addItem(itemsList[i]);
   773                 i++;
   789                 i++;
   774             }
   790             }
   775         } else {
   791         } else {
   776             //some items were inserted or reordered,
   792             //some items were inserted
   777             //so we update layout and emit signal with row number
   793             //so we update model and emit signal with row number
   778             //of first moved/added item
   794             //of first moved/added item
   779             //signal is needed to scroll a view to proper position after
   795             //signal is needed to scroll a view to proper position after
   780             //some items were added
   796             //some items were added
   781             updateLayout();
   797             updateModel();
   782             emit m_q->scrollTo(lastRow + 1,
   798             emit m_q->scrollTo(lastRow + 1,
   783                                QAbstractItemView::PositionAtTop);
   799                                QAbstractItemView::PositionAtTop);
   784         }
   800         } 
   785     } else {
   801     } else {
   786         updateModel();
   802         updateModel();
   787     }
   803     }
   788     CACLIENTTEST_FUNC_EXIT("CaItemModelPrivate::handleAddItems");
   804     CACLIENTTEST_FUNC_EXIT("CaItemModelPrivate::handleAddItems");
   789 }
   805 }
   813         m_q->beginRemoveRows(QModelIndex(), mEntries.indexOf(id),
   829         m_q->beginRemoveRows(QModelIndex(), mEntries.indexOf(id),
   814                              mEntries.indexOf(id));
   830                              mEntries.indexOf(id));
   815         mEntries.remove(id);
   831         mEntries.remove(id);
   816         m_q->endRemoveRows();
   832         m_q->endRemoveRows();
   817     } else {
   833     } else {
   818         updateLayout();
   834         updateModel();
   819     }
   835     }
   820     
       
   821     CACLIENTTEST_FUNC_EXIT("CaItemModelPrivate::removeItem");
   836     CACLIENTTEST_FUNC_EXIT("CaItemModelPrivate::removeItem");
   822 }
   837 }
   823 
   838 
   824 /*!
   839 /*!
   825  Removes missing items from model
   840  Removes missing items from model
   841     CACLIENTTEST_FUNC_EXIT("CaItemModelPrivate::removeItems");
   856     CACLIENTTEST_FUNC_EXIT("CaItemModelPrivate::removeItems");
   842 }
   857 }
   843 
   858 
   844 /*!
   859 /*!
   845  Layout update
   860  Layout update
       
   861  NOTE: this method should be called only if the entries get rearranged
       
   862  and do not change their contents!
   846  */
   863  */
   847 void CaItemModelPrivate::updateLayout()
   864 void CaItemModelPrivate::updateLayout()
   848 {
   865 {
   849     CACLIENTTEST_FUNC_ENTRY("CaItemModelPrivate::updateLayout");
   866     CACLIENTTEST_FUNC_ENTRY("CaItemModelPrivate::updateLayout");
   850     m_q->layoutAboutToBeChanged();
   867     m_q->layoutAboutToBeChanged();
       
   868 
       
   869     // get the ID list from before the update
       
   870     QList<int> oldOrderedIdList = mEntries.orderedIdList();
       
   871 
       
   872     // do the update, the entries should only get rearranged
   851     mEntries.updateEntries(mQuery);
   873     mEntries.updateEntries(mQuery);
   852     updateParentEntry();
   874 
       
   875     // get the new ID list after the entries got rearranged
       
   876     QList<int> newOrderedIdList = mEntries.orderedIdList();
       
   877     // this list will contain the new position indices
       
   878     QList<int> newPositionsList;
       
   879     int entry;
       
   880     foreach (entry, oldOrderedIdList) {
       
   881         newPositionsList << newOrderedIdList.indexOf(entry);
       
   882     }
       
   883 
       
   884     // now check which items in the previous persistent index list changed
       
   885     // their positions, make new indices for them and store in the new
       
   886     // persistent index list
       
   887     QModelIndexList oldPersistentIndexList = m_q->persistentIndexList();
       
   888     QModelIndexList newPersistentIndexList;
       
   889     QModelIndex index;
       
   890     foreach (index, oldPersistentIndexList) {
       
   891         newPersistentIndexList <<
       
   892             m_q->createIndex(
       
   893                 newPositionsList.at(index.row()),
       
   894                 0,
       
   895                 index.internalPointer());
       
   896     }
       
   897 
       
   898     m_q->changePersistentIndexList(oldPersistentIndexList, newPersistentIndexList);
       
   899 
   853     m_q->layoutChanged();
   900     m_q->layoutChanged();
   854     CACLIENTTEST_FUNC_EXIT("CaItemModelPrivate::updateLayout");
   901     CACLIENTTEST_FUNC_EXIT("CaItemModelPrivate::updateLayout");
   855 }
   902 }
   856 
   903 
   857 /*!
   904 /*!