CatalogueTreeModel.cpp
218 lines
| 5.7 KiB
| text/x-c
|
CppLexer
r1228 | #include "Catalogue/CatalogueTreeModel.h" | |||
r1229 | #include <Catalogue/CatalogueTreeItems/CatalogueAbstractTreeItem.h> | |||
r1228 | ||||
r1229 | #include <QMimeData> | |||
r1228 | #include <memory> | |||
r1229 | #include <Common/MimeTypesDef.h> | |||
r1228 | struct CatalogueTreeModel::CatalogueTreeModelPrivate { | |||
r1229 | std::unique_ptr<CatalogueAbstractTreeItem> m_RootItem = nullptr; | |||
r1228 | ||||
r1229 | CatalogueTreeModelPrivate() : m_RootItem{std::make_unique<CatalogueAbstractTreeItem>()} {} | |||
r1228 | }; | |||
CatalogueTreeModel::CatalogueTreeModel(QObject *parent) | ||||
: QAbstractItemModel(parent), impl{spimpl::make_unique_impl<CatalogueTreeModelPrivate>()} | ||||
{ | ||||
} | ||||
r1229 | QModelIndex CatalogueTreeModel::addTopLevelItem(CatalogueAbstractTreeItem *item) | |||
r1228 | { | |||
r1229 | auto nbTopLevelItems = impl->m_RootItem->children().count(); | |||
beginInsertRows(QModelIndex(), nbTopLevelItems, nbTopLevelItems); | ||||
r1228 | impl->m_RootItem->addChild(item); | |||
endInsertRows(); | ||||
r1229 | emit dataChanged(QModelIndex(), QModelIndex()); | |||
r1228 | ||||
r1229 | return index(nbTopLevelItems, 0); | |||
r1228 | } | |||
r1229 | QVector<CatalogueAbstractTreeItem *> CatalogueTreeModel::topLevelItems() const | |||
r1228 | { | |||
r1229 | return impl->m_RootItem->children(); | |||
r1228 | } | |||
r1229 | void CatalogueTreeModel::addChildItem(CatalogueAbstractTreeItem *child, | |||
const QModelIndex &parentIndex) | ||||
r1228 | { | |||
auto parentItem = item(parentIndex); | ||||
r1229 | int c = parentItem->children().count(); | |||
beginInsertRows(parentIndex, c, c); | ||||
r1228 | parentItem->addChild(child); | |||
endInsertRows(); | ||||
r1229 | ||||
r1230 | emit dataChanged(parentIndex, parentIndex); | |||
r1228 | } | |||
r1276 | void CatalogueTreeModel::removeChildItem(CatalogueAbstractTreeItem *child, | |||
const QModelIndex &parentIndex) | ||||
{ | ||||
auto parentItem = item(parentIndex); | ||||
int i = parentItem->children().indexOf(child); | ||||
beginRemoveRows(parentIndex, i, i); | ||||
parentItem->removeChild(child); | ||||
endRemoveRows(); | ||||
emit dataChanged(parentIndex, parentIndex); | ||||
} | ||||
void CatalogueTreeModel::refresh(const QModelIndex &index) | ||||
{ | ||||
emit dataChanged(index, index); | ||||
} | ||||
r1229 | CatalogueAbstractTreeItem *CatalogueTreeModel::item(const QModelIndex &index) const | |||
r1228 | { | |||
r1229 | return static_cast<CatalogueAbstractTreeItem *>(index.internalPointer()); | |||
r1228 | } | |||
r1229 | QModelIndex CatalogueTreeModel::indexOf(CatalogueAbstractTreeItem *item, int column) const | |||
r1228 | { | |||
auto parentItem = item->parent(); | ||||
if (!parentItem) { | ||||
return QModelIndex(); | ||||
} | ||||
r1229 | auto row = parentItem->children().indexOf(item); | |||
r1228 | return createIndex(row, column, item); | |||
} | ||||
QModelIndex CatalogueTreeModel::index(int row, int column, const QModelIndex &parent) const | ||||
{ | ||||
r1229 | if (column > 0) { | |||
int a = 0; | ||||
} | ||||
r1228 | if (!hasIndex(row, column, parent)) { | |||
return QModelIndex(); | ||||
} | ||||
r1229 | CatalogueAbstractTreeItem *parentItem = nullptr; | |||
r1228 | ||||
if (!parent.isValid()) { | ||||
parentItem = impl->m_RootItem.get(); | ||||
} | ||||
else { | ||||
r1229 | parentItem = item(parent); | |||
r1228 | } | |||
r1229 | auto childItem = parentItem->children().value(row); | |||
r1228 | if (childItem) { | |||
return createIndex(row, column, childItem); | ||||
} | ||||
return QModelIndex(); | ||||
} | ||||
QModelIndex CatalogueTreeModel::parent(const QModelIndex &index) const | ||||
{ | ||||
if (!index.isValid()) { | ||||
return QModelIndex(); | ||||
} | ||||
r1229 | auto childItem = item(index); | |||
r1228 | auto parentItem = childItem->parent(); | |||
if (parentItem == nullptr || parentItem->parent() == nullptr) { | ||||
return QModelIndex(); | ||||
} | ||||
r1229 | auto row = parentItem->parent()->children().indexOf(parentItem); | |||
r1228 | return createIndex(row, 0, parentItem); | |||
} | ||||
int CatalogueTreeModel::rowCount(const QModelIndex &parent) const | ||||
{ | ||||
r1229 | CatalogueAbstractTreeItem *parentItem = nullptr; | |||
r1228 | ||||
if (!parent.isValid()) { | ||||
parentItem = impl->m_RootItem.get(); | ||||
} | ||||
else { | ||||
r1229 | parentItem = item(parent); | |||
r1228 | } | |||
r1229 | return parentItem->children().count(); | |||
r1228 | } | |||
int CatalogueTreeModel::columnCount(const QModelIndex &parent) const | ||||
{ | ||||
return (int)Column::Count; | ||||
} | ||||
Qt::ItemFlags CatalogueTreeModel::flags(const QModelIndex &index) const | ||||
{ | ||||
r1229 | auto treeItem = item(index); | |||
if (treeItem) { | ||||
return treeItem->flags(index.column()); | ||||
r1228 | } | |||
return Qt::NoItemFlags; | ||||
} | ||||
QVariant CatalogueTreeModel::data(const QModelIndex &index, int role) const | ||||
{ | ||||
r1229 | auto treeItem = item(index); | |||
if (treeItem) { | ||||
return treeItem->data(index.column(), role); | ||||
r1228 | } | |||
return QModelIndex(); | ||||
} | ||||
bool CatalogueTreeModel::setData(const QModelIndex &index, const QVariant &value, int role) | ||||
{ | ||||
r1229 | auto treeItem = item(index); | |||
if (treeItem) { | ||||
auto result = treeItem->setData(index.column(), role, value); | ||||
r1228 | ||||
r1229 | if (result && index.column() == (int)Column::Name) { | |||
r1228 | emit itemRenamed(index); | |||
} | ||||
r1229 | return result; | |||
r1228 | } | |||
return false; | ||||
} | ||||
r1229 | bool CatalogueTreeModel::canDropMimeData(const QMimeData *data, Qt::DropAction action, int row, | |||
int column, const QModelIndex &parent) const | ||||
{ | ||||
auto draggedIndex = parent; | ||||
auto draggedItem = item(draggedIndex); | ||||
if (draggedItem) { | ||||
return draggedItem->canDropMimeData(data, action); | ||||
} | ||||
return false; | ||||
} | ||||
bool CatalogueTreeModel::dropMimeData(const QMimeData *data, Qt::DropAction action, int row, | ||||
int column, const QModelIndex &parent) | ||||
{ | ||||
r1230 | bool result = false; | |||
auto draggedIndex = parent; | ||||
auto draggedItem = item(draggedIndex); | ||||
if (draggedItem) { | ||||
result = draggedItem->dropMimeData(data, action); | ||||
if (result) { | ||||
emit itemDropped(draggedIndex); | ||||
} | ||||
} | ||||
return result; | ||||
r1229 | } | |||
Qt::DropActions CatalogueTreeModel::supportedDropActions() const | ||||
{ | ||||
return Qt::CopyAction | Qt::MoveAction; | ||||
} | ||||
QStringList CatalogueTreeModel::mimeTypes() const | ||||
{ | ||||
return {MIME_TYPE_EVENT_LIST}; | ||||
} | ||||