QModelIndex ListModel::moveItemHorizontal(const QModelIndex& index, int direction) { ListItem* item = itemFromIndex(index); if (!item) return index; ListItem* parent = item->parent(); int row = item->row(); if (direction == App::Left) { // reparent as child of parent's parent if (!parent || parent->isRoot()) // already top level item return index; ListItem* newParent = parent->parent(); int newRow = parent->row() + 1; QSqlDatabase db = QSqlDatabase::database(); db.transaction(); if (parent->takeChildDb(row) && item->setParentDb(newParent, newRow)) { db.commit(); if (beginMoveRows(indexFromItem(parent), row, row, indexFromItem(newParent), newRow)) { newParent->insertChild(newRow, parent->takeChild(row)); endMoveRows(); } return indexFromItem(item); } else { db.rollback(); return index; } } else { // move as child of previous sibling ListItem* newParent = parent->child(row - 1); if (!newParent) return index; QSqlDatabase db = QSqlDatabase::database(); db.transaction(); if (parent->takeChildDb(row) && item->setParentDb(newParent, newParent->childCount())) { db.commit(); if (beginMoveRows(indexFromItem(parent), row, row, indexFromItem(newParent), newParent->childCount())) { newParent->appendChild(parent->takeChild(row)); endMoveRows(); } newParent->setExpanded(true); return indexFromItem(item); } else { db.rollback(); return index; } } }
QModelIndex ListModel::parent(const QModelIndex& index) const { if (!index.isValid()) // root return QModelIndex(); ListItem* item = itemFromIndex(index); if (!item) return QModelIndex(); ListItem* parent = item->parent(); if (parent->isRoot()) // top level item return QModelIndex(); return createIndex(parent->row(), 0, parent); }