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); |
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 |