+int PlacesItemModel::groupedDropIndex(int index, const PlacesItem* item) const
+{
+ Q_ASSERT(item);
+
+ int dropIndex = index;
+ const PlacesItem::GroupType type = item->groupType();
+
+ const int itemCount = count();
+ if (index < 0) {
+ dropIndex = itemCount;
+ }
+
+ // Search nearest previous item with the same group
+ int previousIndex = -1;
+ for (int i = dropIndex - 1; i >= 0; --i) {
+ if (placesItem(i)->groupType() == type) {
+ previousIndex = i;
+ break;
+ }
+ }
+
+ // Search nearest next item with the same group
+ int nextIndex = -1;
+ for (int i = dropIndex; i < count(); ++i) {
+ if (placesItem(i)->groupType() == type) {
+ nextIndex = i;
+ break;
+ }
+ }
+
+ // Adjust the drop-index to be inserted to the
+ // nearest item with the same group.
+ if (previousIndex >= 0 && nextIndex >= 0) {
+ dropIndex = (dropIndex - previousIndex < nextIndex - dropIndex) ?
+ previousIndex + 1 : nextIndex;
+ } else if (previousIndex >= 0) {
+ dropIndex = previousIndex + 1;
+ } else if (nextIndex >= 0) {
+ dropIndex = nextIndex;
+ }
+
+ return dropIndex;
+}
+