QModelIndex TaskItemModel::parent(const QModelIndex &index) const { if (!index.isValid()) { return QModelIndex(); } TaskItem *childTaskItem = static_cast<TaskItem *>(index.internalPointer()); TaskItem *parentTaskItem = childTaskItem->parent(); if (parentTaskItem == m_taskManager->rootItem()) { return QModelIndex(); } return createIndex(parentTaskItem->calcIndex(), 0, parentTaskItem); }
QModelIndex TreeModel::paste(const QModelIndex &index) { if (!index.isValid() || !cutItem) return index; TaskItem *sibling = itemForIndex(index); Q_ASSERT(sibling); TaskItem *parent = sibling->parent(); Q_ASSERT(parent); int row = parent->rowOfChild(sibling) + 1; beginInsertRows(index.parent(), row, row); parent->insertChild(row, cutItem); TaskItem *child = cutItem; cutItem = 0; endInsertRows(); return createIndex(row, 0, child); }
QModelIndex TreeModel::demote(const QModelIndex &index) { if (!index.isValid()) return index; TaskItem *item = itemForIndex(index); Q_ASSERT(item); TaskItem *parent = item->parent(); Q_ASSERT(parent); int row = parent->rowOfChild(item); if (row == 0) return index; // No preceding sibling to move this under TaskItem *child = parent->takeChild(row); Q_ASSERT(child == item); TaskItem *sibling = parent->childAt(row - 1); Q_ASSERT(sibling); sibling->addChild(child); QModelIndex newIndex = createIndex(sibling->childCount() - 1, 0, child); emit dataChanged(newIndex, newIndex); return newIndex; }