contentstorage/caclient/src/caitemmodel.cpp
changeset 92 782e3408c2ab
parent 89 1e87eb3b400f
child 94 dbb8300717f7
equal deleted inserted replaced
91:a6d55a2e75be 92:782e3408c2ab
   380  ...
   380  ...
   381  delete entry;
   381  delete entry;
   382  \endcode
   382  \endcode
   383 
   383 
   384  */
   384  */
   385 CaEntry *CaItemModel::entry(const QModelIndex &index) const
   385 QSharedPointer<CaEntry> CaItemModel::entry(const QModelIndex &index) const
   386 {
   386 {
   387     return m_d->entry(index);
   387     return m_d->entry(index);
   388 }
   388 }
   389 
   389 
   390 /*!
   390 /*!
   392  \param query needed to create model
   392  \param query needed to create model
   393  \param pointer to public implementation object connected
   393  \param pointer to public implementation object connected
   394  */
   394  */
   395 CaItemModelPrivate::CaItemModelPrivate(const CaQuery &query,
   395 CaItemModelPrivate::CaItemModelPrivate(const CaQuery &query,
   396                                        CaItemModel *itemModelPublic) :
   396                                        CaItemModel *itemModelPublic) :
   397     QObject(), m_q(itemModelPublic), mParentEntry(0), mQuery(query),
   397     QObject(), m_q(itemModelPublic), mParentEntry(), mQuery(query),
   398     mService(CaService::instance()), mEntries(mService), mNotifier(NULL),
   398     mService(CaService::instance()), mEntries(mService), mNotifier(NULL),
   399     mSize(defaultIconSize), mSecondLineVisibility(true)
   399     mSize(defaultIconSize), mSecondLineVisibility(true)
   400 {
   400 {
   401     updateModel();
   401     updateModel();
   402     setAutoUpdate(true);
   402     setAutoUpdate(true);
   406  Destructor
   406  Destructor
   407  */
   407  */
   408 CaItemModelPrivate::~CaItemModelPrivate()
   408 CaItemModelPrivate::~CaItemModelPrivate()
   409 {
   409 {
   410     mEntries.clear();
   410     mEntries.clear();
   411     delete mParentEntry;
       
   412     delete mNotifier;
   411     delete mNotifier;
   413 }
   412 }
   414 
   413 
   415 /*!
   414 /*!
   416  Returns count of rows in model
   415  Returns count of rows in model
   464             variant = qVariantFromValue(entry(modelIndex)->flags());
   463             variant = qVariantFromValue(entry(modelIndex)->flags());
   465             break;
   464             break;
   466         case CaItemModel::TextRole:
   465         case CaItemModel::TextRole:
   467             variant = QVariant(entry(modelIndex)->text());
   466             variant = QVariant(entry(modelIndex)->text());
   468             break;
   467             break;
       
   468         case CaItemModel::FullTextRole:
       
   469             variant = QVariant(entry(modelIndex)->text() + QString(" ")
       
   470                 + entry(modelIndex)->description());
       
   471             break;
   469         default:
   472         default:
   470             variant = QVariant(QVariant::Invalid);
   473             variant = QVariant(QVariant::Invalid);
   471         }
   474         }
   472     }
   475     }
   473     CACLIENTTEST_FUNC_EXIT("CaItemModelPrivate::data");
   476     CACLIENTTEST_FUNC_EXIT("CaItemModelPrivate::data");
   548 /*!
   551 /*!
   549  Returns an entry from model
   552  Returns an entry from model
   550  \param modelIndex index of entry in model
   553  \param modelIndex index of entry in model
   551  \retval pointer to an entry
   554  \retval pointer to an entry
   552  */
   555  */
   553 CaEntry *CaItemModelPrivate::entry(const QModelIndex &modelIndex) const
   556 QSharedPointer<CaEntry> CaItemModelPrivate::entry(const QModelIndex &modelIndex) const
   554 {
   557 {
   555     if (modelIndex.column() == 1) {
   558     if (modelIndex.column() == 1) {
   556         return mParentEntry;
   559         return mParentEntry;
   557     } else {
   560     } else {
   558         return mEntries.at(modelIndex.row());
   561         return mEntries.at(modelIndex.row());
   685 void CaItemModelPrivate::updateParentEntry()
   688 void CaItemModelPrivate::updateParentEntry()
   686 {
   689 {
   687     CACLIENTTEST_FUNC_ENTRY("CaItemModelPrivate::updateParentEntry");
   690     CACLIENTTEST_FUNC_ENTRY("CaItemModelPrivate::updateParentEntry");
   688 
   691 
   689     if (mQuery.parentId()) {
   692     if (mQuery.parentId()) {
   690         delete mParentEntry;
       
   691         mParentEntry = mService->getEntry(mQuery.parentId());
   693         mParentEntry = mService->getEntry(mQuery.parentId());
   692     }
   694     }
   693 
   695 
   694     CACLIENTTEST_FUNC_EXIT("CaItemModelPrivate::updateParentEntry");
   696     CACLIENTTEST_FUNC_EXIT("CaItemModelPrivate::updateParentEntry");
   695 
   697 
   704     CACLIENTTEST_FUNC_ENTRY("CaItemModelPrivate::updateItemData");
   706     CACLIENTTEST_FUNC_ENTRY("CaItemModelPrivate::updateItemData");
   705 
   707 
   706     mEntries.updateEntry(id);
   708     mEntries.updateEntry(id);
   707 
   709 
   708     QList<int> ids = mService->getEntryIds(mQuery);
   710     QList<int> ids = mService->getEntryIds(mQuery);
   709     if (mEntries.indexOf(id) >= 0 
   711     if (mEntries.indexOf(id) >= 0
   710            && ids.indexOf(id) == mEntries.indexOf(id)) {
   712            && ids.indexOf(id) == mEntries.indexOf(id)) {
   711         emit m_q->dataChanged(index(mEntries.indexOf(id)), index(
   713         emit m_q->dataChanged(index(mEntries.indexOf(id)), index(
   712                                   mEntries.indexOf(id)));
   714                                   mEntries.indexOf(id)));
   713     } else if (ids.indexOf(id) < 0){
   715     } else if (ids.indexOf(id) < 0){
   714         removeItem(id);
   716         removeItem(id);
   715     } else if (mEntries.indexOf(id) < 0){
   717     } else if (mEntries.indexOf(id) < 0){
   716         addItem(id);  
   718         addItem(id);
   717     } else if (mParentEntry && id == mParentEntry->id()) {
   719     } else if (mParentEntry && id == mParentEntry->id()) {
   718         updateParentEntry();
   720         updateParentEntry();
   719         m_q->reset();
   721         m_q->reset();
   720     } else {
   722     } else {
   721         updateModel();
   723         updateModel();
   741     }
   743     }
   742     CACLIENTTEST_FUNC_EXIT("CaItemModelPrivate::addItem");
   744     CACLIENTTEST_FUNC_EXIT("CaItemModelPrivate::addItem");
   743 }
   745 }
   744 
   746 
   745 /*!
   747 /*!
       
   748  Adds new item block to model
       
   749  Use in cases when inserting / appending an adjacent block of items
       
   750  \param itemsList list of adjacent items
       
   751  */
       
   752 void CaItemModelPrivate::addItemBlock(const QList<int> &itemsList)
       
   753 {
       
   754     CACLIENTTEST_FUNC_ENTRY("CaItemModelPrivate::addItemBlock");
       
   755     if (!itemsList.isEmpty()) {
       
   756         int firstRow = itemRow(itemsList.first());
       
   757         int lastRow = itemRow(itemsList.last());
       
   758         m_q->beginInsertRows(QModelIndex(), firstRow, lastRow);
       
   759         for (int i = 0; i < itemsList.count(); ++i) {
       
   760             mEntries.insert(firstRow + i, itemsList.at(i));
       
   761         }
       
   762         m_q->endInsertRows();
       
   763         emit m_q->scrollTo(firstRow, QAbstractItemView::PositionAtTop);
       
   764     }
       
   765     CACLIENTTEST_FUNC_EXIT("CaItemModelPrivate::addItemBlock");
       
   766 }
       
   767 
       
   768 /*!
   746  Adds new items to model
   769  Adds new items to model
   747  \param itemsList current items list
   770  \param itemsList current items list
   748  */
   771  */
   749 void CaItemModelPrivate::handleAddItems(QList<int> &itemsList)
   772 void CaItemModelPrivate::handleAddItems(const QList<int> &itemsList)
   750 {
   773 {
   751     CACLIENTTEST_FUNC_ENTRY("CaItemModelPrivate::handleAddItems");
   774     CACLIENTTEST_FUNC_ENTRY("CaItemModelPrivate::handleAddItems");
   752 
   775     const int oldItemCount(mEntries.count());
   753     int entriesCount = mEntries.count();
   776     if (oldItemCount) {
   754     if (entriesCount) {
   777         const int newItemCount(itemsList.count());
   755         int lastRow = itemsList.indexOf(mEntries[entriesCount - 1]);
   778         if (newItemCount == oldItemCount) {
   756         if (itemsList.count() == entriesCount) {
   779             // count is the same - check if item order changed
   757             //count is same and last item is in same position
   780             if (itemsList == mEntries.orderedIdList()) {
   758             //so we update whole model
   781                 // assume that if the order has not changed
   759             bool orderChanged(false);
   782                 // it had to be the secondary lines
   760             while (entriesCount) {
   783                 updateModel();
   761                 if (itemsList.indexOf(mEntries[entriesCount - 1]) 
   784             } else {
   762                         != (entriesCount - 1)) {
       
   763                     orderChanged = true;
       
   764                     break;
       
   765                 }
       
   766                 entriesCount--;
       
   767             }
       
   768             if (orderChanged) {
       
   769                 updateLayout();
   785                 updateLayout();
   770             }
   786             }
   771             else {
   787         } else {
   772                 updateModel();
       
   773             }
       
   774             
       
   775         } else if ((itemsList.count() - entriesCount) == 1 && lastRow
       
   776                    == entriesCount) {
       
   777             //just one item added - collection
       
   778             int i = 0;
   788             int i = 0;
   779             for (i = 0; i < entriesCount; i++) {
   789             QList<int> oldList = mEntries.orderedIdList();
   780                 if (itemsList[i] != mEntries[i]) {
   790             //we loop through items to find first added
   781                     addItem(itemsList[i]);
   791             while (i < oldList.count()) {
   782                     updateLayout();
   792                 if (oldList[i] != itemsList[i]) {
   783                     emit m_q->scrollTo(i,
   793                     oldList.takeAt(i);
   784                                        QAbstractItemView::PositionAtTop);
   794                 } else {
       
   795                     ++i;
   785                 }
   796                 }
   786             }
   797             }
   787             while (i < itemsList.count()) {
       
   788                 addItem(itemsList[i]);
       
   789                 i++;
       
   790             }
       
   791         } else {
       
   792             //some items were inserted
       
   793             //so we update model and emit signal with row number
       
   794             //of first moved/added item
       
   795             //signal is needed to scroll a view to proper position after
       
   796             //some items were added
       
   797             updateModel();
   798             updateModel();
   798             emit m_q->scrollTo(lastRow + 1,
   799             //i is the index of first added item
   799                                QAbstractItemView::PositionAtTop);
   800             emit m_q->scrollTo(i, QAbstractItemView::PositionAtTop);
   800         } 
   801         }
   801     } else {
   802     } else {
   802         updateModel();
   803         // items added to an empty list - add all as a single block
       
   804         addItemBlock(itemsList);
   803     }
   805     }
   804     CACLIENTTEST_FUNC_EXIT("CaItemModelPrivate::handleAddItems");
   806     CACLIENTTEST_FUNC_EXIT("CaItemModelPrivate::handleAddItems");
   805 }
   807 }
   806 
   808 
   807 /*!
   809 /*!
   888     QModelIndexList newPersistentIndexList;
   890     QModelIndexList newPersistentIndexList;
   889     QModelIndex index;
   891     QModelIndex index;
   890     foreach (index, oldPersistentIndexList) {
   892     foreach (index, oldPersistentIndexList) {
   891         newPersistentIndexList <<
   893         newPersistentIndexList <<
   892             m_q->createIndex(
   894             m_q->createIndex(
   893                 newPositionsList.at(index.row()),
   895                 newPositionsList.at(index.row()), 0, index.internalPointer());
   894                 0,
   896     }
   895                 index.internalPointer());
   897     m_q->changePersistentIndexList(
   896     }
   898         oldPersistentIndexList, newPersistentIndexList);
   897 
       
   898     m_q->changePersistentIndexList(oldPersistentIndexList, newPersistentIndexList);
       
   899 
   899 
   900     m_q->layoutChanged();
   900     m_q->layoutChanged();
   901     CACLIENTTEST_FUNC_EXIT("CaItemModelPrivate::updateLayout");
   901     CACLIENTTEST_FUNC_EXIT("CaItemModelPrivate::updateLayout");
   902 }
   902 }
   903 
   903