QModelIndex ListModel::moveItemVertical(const QModelIndex& index, App::Direction direction) { ListItem* item = itemFromIndex(index); if (!item) return index; int row = item->row(); if (direction == App::Up && row == 0) return index; ListItem* parent = item->parent(); if (direction == App::Down && row == (parent->childCount() - 1)) return index; ListItem* otherItem = parent->child(direction == App::Up ? row - 1 : row + 1); int downRow = direction == App::Down ? row : row - 1; // the row that is being moved down QSqlDatabase db = QSqlDatabase::database(); db.transaction(); if (item->setRow(direction == App::Down ? row + 1 : row - 1) && otherItem->setRow(row)) { db.commit(); if (beginMoveRows(index.parent(), downRow, downRow, index.parent(), downRow + 2)) { parent->moveChild(downRow); endMoveRows(); return indexFromItem(item); } } else db.rollback(); return index; }
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::index(int row, int column, const QModelIndex& parent) const { if (row < 0 || column < 0) return QModelIndex(); ListItem* parentItem = itemFromIndex(parent); if (!parentItem || row >= parentItem->childCount()) return QModelIndex(); ListItem* childItem = parentItem->child(row); if (!childItem) return QModelIndex(); return createIndex(row, column, childItem); }
QModelIndex TagMngrListModel::index(int row, int column, const QModelIndex& parent) const { if (!hasIndex(row, column, parent)) return QModelIndex(); ListItem* parentItem = 0; if (!parent.isValid()) parentItem = d->rootItem; else parentItem = static_cast<ListItem*>(parent.internalPointer()); ListItem* const childItem = parentItem->child(row); if (childItem) return createIndex(row, column, childItem); else return QModelIndex(); }