Advertisement
Guest User

Untitled

a guest
Dec 7th, 2017
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. void QListModel::ensureSorted(int column, Qt::SortOrder order, int start, int end)
  2. {
  3.     if (column != 0)
  4.         return;
  5.  
  6.     int count = end - start + 1;
  7.     QVector < QPair<QListWidgetItem*,int> > sorting(count);
  8.     for (int i = 0; i < count; ++i) {
  9.         sorting[i].first = items.at(start + i);
  10.         sorting[i].second = start + i;
  11.     }
  12.  
  13.     LessThan compare = (order == Qt::AscendingOrder ? &itemLessThan : &itemGreaterThan);
  14.     std::sort(sorting.begin(), sorting.end(), compare);
  15.  
  16.     QModelIndexList oldPersistentIndexes = persistentIndexList();
  17.     QModelIndexList newPersistentIndexes = oldPersistentIndexes;
  18.     QList<QListWidgetItem*> tmp = items;
  19.     QList<QListWidgetItem*>::iterator lit = tmp.begin();
  20.     bool changed = false;
  21.     for (int i = 0; i < count; ++i) {
  22.         int oldRow = sorting.at(i).second;
  23.         int tmpitepos = lit - tmp.begin();
  24.         QListWidgetItem *item = tmp.takeAt(oldRow);
  25.         if (tmpitepos > tmp.size())
  26.             --tmpitepos;
  27.         lit = tmp.begin() + tmpitepos;
  28.         lit = sortedInsertionIterator(lit, tmp.end(), order, item);
  29.         int newRow = qMax(lit - tmp.begin(), 0);
  30.         lit = tmp.insert(lit, item);
  31.         if (newRow != oldRow) {
  32.             changed = true;
  33.             for (int j = i + 1; j < count; ++j) {
  34.                 int otherRow = sorting.at(j).second;
  35.                 if (oldRow < otherRow && newRow >= otherRow)
  36.                     --sorting[j].second;
  37.                 else if (oldRow > otherRow && newRow <= otherRow)
  38.                     ++sorting[j].second;
  39.             }
  40.             for (int k = 0; k < newPersistentIndexes.count(); ++k) {
  41.                 QModelIndex pi = newPersistentIndexes.at(k);
  42.                 int oldPersistentRow = pi.row();
  43.                 int newPersistentRow = oldPersistentRow;
  44.                 if (oldPersistentRow == oldRow)
  45.                     newPersistentRow = newRow;
  46.                 else if (oldRow < oldPersistentRow && newRow >= oldPersistentRow)
  47.                     newPersistentRow = oldPersistentRow - 1;
  48.                 else if (oldRow > oldPersistentRow && newRow <= oldPersistentRow)
  49.                     newPersistentRow = oldPersistentRow + 1;
  50.                 if (newPersistentRow != oldPersistentRow)
  51.                     newPersistentIndexes[k] = createIndex(newPersistentRow,
  52.                                                           pi.column(), pi.internalPointer());
  53.             }
  54.         }
  55.     }
  56.  
  57.     if (changed) {
  58.         emit layoutAboutToBeChanged();
  59.         items = tmp;
  60.         changePersistentIndexList(oldPersistentIndexes, newPersistentIndexes);
  61.         emit layoutChanged();
  62.     }
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement