Unverified Commit c34499fa authored by Don Gagne's avatar Don Gagne Committed by GitHub

Merge pull request #8702 from airmap/object_list_move

Allow to move/reorder objects within QmlObjectListModel
parents 8151a6cd ea213069
......@@ -125,6 +125,22 @@ bool QmlObjectListModel::removeRows(int position, int rows, const QModelIndex& p
return true;
}
void QmlObjectListModel::move(int from, int to)
{
if(0 <= from && from < count() && 0 <= to && to < count() && from != to) {
// Workaround to allow move item to the bottom. Done according to
// beginMoveRows() documentation and implementation specificity:
// https://doc.qt.io/qt-5/qabstractitemmodel.html#beginMoveRows
// (see 3rd picture explanation there)
if(from == to - 1) {
to = from++;
}
beginMoveRows(QModelIndex(), from, from, QModelIndex(), to);
_objectList.move(from, to);
endMoveRows();
}
}
QObject* QmlObjectListModel::operator[](int index)
{
if (index < 0 || index >= _objectList.count()) {
......
......@@ -46,6 +46,9 @@ public:
bool contains (QObject* object) { return _objectList.indexOf(object) != -1; }
int indexOf (QObject* object) { return _objectList.indexOf(object); }
/// Moves an item to a new position
void move(int from, int to);
QObject* operator[] (int i);
const QObject* operator[] (int i) const;
template<class T> T value (int index) { return qobject_cast<T>(_objectList[index]); }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment