##// END OF EJS Templates
Fix merge problem
Fix merge problem

File last commit:

r1230:964109cb8b70
r1242:3731b9185cc1
Show More
CatalogueTreeModel.cpp
201 lines | 5.2 KiB | text/x-c | CppLexer
/ gui / src / Catalogue / CatalogueTreeModel.cpp
Refactoring of catalogue display using a QTreeView and a custom model based on QTreeWidgetItem
r1228 #include "Catalogue/CatalogueTreeModel.h"
Refactoring of catalogue: use a custom item class
r1229 #include <Catalogue/CatalogueTreeItems/CatalogueAbstractTreeItem.h>
Refactoring of catalogue display using a QTreeView and a custom model based on QTreeWidgetItem
r1228
Refactoring of catalogue: use a custom item class
r1229 #include <QMimeData>
Refactoring of catalogue display using a QTreeView and a custom model based on QTreeWidgetItem
r1228 #include <memory>
Refactoring of catalogue: use a custom item class
r1229 #include <Common/MimeTypesDef.h>
Refactoring of catalogue display using a QTreeView and a custom model based on QTreeWidgetItem
r1228 struct CatalogueTreeModel::CatalogueTreeModelPrivate {
Refactoring of catalogue: use a custom item class
r1229 std::unique_ptr<CatalogueAbstractTreeItem> m_RootItem = nullptr;
Refactoring of catalogue display using a QTreeView and a custom model based on QTreeWidgetItem
r1228
Refactoring of catalogue: use a custom item class
r1229 CatalogueTreeModelPrivate() : m_RootItem{std::make_unique<CatalogueAbstractTreeItem>()} {}
Refactoring of catalogue display using a QTreeView and a custom model based on QTreeWidgetItem
r1228 };
CatalogueTreeModel::CatalogueTreeModel(QObject *parent)
: QAbstractItemModel(parent), impl{spimpl::make_unique_impl<CatalogueTreeModelPrivate>()}
{
}
Refactoring of catalogue: use a custom item class
r1229 QModelIndex CatalogueTreeModel::addTopLevelItem(CatalogueAbstractTreeItem *item)
Refactoring of catalogue display using a QTreeView and a custom model based on QTreeWidgetItem
r1228 {
Refactoring of catalogue: use a custom item class
r1229 auto nbTopLevelItems = impl->m_RootItem->children().count();
beginInsertRows(QModelIndex(), nbTopLevelItems, nbTopLevelItems);
Refactoring of catalogue display using a QTreeView and a custom model based on QTreeWidgetItem
r1228 impl->m_RootItem->addChild(item);
endInsertRows();
Refactoring of catalogue: use a custom item class
r1229 emit dataChanged(QModelIndex(), QModelIndex());
Refactoring of catalogue display using a QTreeView and a custom model based on QTreeWidgetItem
r1228
Refactoring of catalogue: use a custom item class
r1229 return index(nbTopLevelItems, 0);
Refactoring of catalogue display using a QTreeView and a custom model based on QTreeWidgetItem
r1228 }
Refactoring of catalogue: use a custom item class
r1229 QVector<CatalogueAbstractTreeItem *> CatalogueTreeModel::topLevelItems() const
Refactoring of catalogue display using a QTreeView and a custom model based on QTreeWidgetItem
r1228 {
Refactoring of catalogue: use a custom item class
r1229 return impl->m_RootItem->children();
Refactoring of catalogue display using a QTreeView and a custom model based on QTreeWidgetItem
r1228 }
Refactoring of catalogue: use a custom item class
r1229 void CatalogueTreeModel::addChildItem(CatalogueAbstractTreeItem *child,
const QModelIndex &parentIndex)
Refactoring of catalogue display using a QTreeView and a custom model based on QTreeWidgetItem
r1228 {
auto parentItem = item(parentIndex);
Refactoring of catalogue: use a custom item class
r1229 int c = parentItem->children().count();
beginInsertRows(parentIndex, c, c);
Refactoring of catalogue display using a QTreeView and a custom model based on QTreeWidgetItem
r1228 parentItem->addChild(child);
endInsertRows();
Refactoring of catalogue: use a custom item class
r1229
Drop of events on a catalogue
r1230 emit dataChanged(parentIndex, parentIndex);
Refactoring of catalogue display using a QTreeView and a custom model based on QTreeWidgetItem
r1228 }
Refactoring of catalogue: use a custom item class
r1229 CatalogueAbstractTreeItem *CatalogueTreeModel::item(const QModelIndex &index) const
Refactoring of catalogue display using a QTreeView and a custom model based on QTreeWidgetItem
r1228 {
Refactoring of catalogue: use a custom item class
r1229 return static_cast<CatalogueAbstractTreeItem *>(index.internalPointer());
Refactoring of catalogue display using a QTreeView and a custom model based on QTreeWidgetItem
r1228 }
Refactoring of catalogue: use a custom item class
r1229 QModelIndex CatalogueTreeModel::indexOf(CatalogueAbstractTreeItem *item, int column) const
Refactoring of catalogue display using a QTreeView and a custom model based on QTreeWidgetItem
r1228 {
auto parentItem = item->parent();
if (!parentItem) {
return QModelIndex();
}
Refactoring of catalogue: use a custom item class
r1229 auto row = parentItem->children().indexOf(item);
Refactoring of catalogue display using a QTreeView and a custom model based on QTreeWidgetItem
r1228 return createIndex(row, column, item);
}
QModelIndex CatalogueTreeModel::index(int row, int column, const QModelIndex &parent) const
{
Refactoring of catalogue: use a custom item class
r1229 if (column > 0) {
int a = 0;
}
Refactoring of catalogue display using a QTreeView and a custom model based on QTreeWidgetItem
r1228 if (!hasIndex(row, column, parent)) {
return QModelIndex();
}
Refactoring of catalogue: use a custom item class
r1229 CatalogueAbstractTreeItem *parentItem = nullptr;
Refactoring of catalogue display using a QTreeView and a custom model based on QTreeWidgetItem
r1228
if (!parent.isValid()) {
parentItem = impl->m_RootItem.get();
}
else {
Refactoring of catalogue: use a custom item class
r1229 parentItem = item(parent);
Refactoring of catalogue display using a QTreeView and a custom model based on QTreeWidgetItem
r1228 }
Refactoring of catalogue: use a custom item class
r1229 auto childItem = parentItem->children().value(row);
Refactoring of catalogue display using a QTreeView and a custom model based on QTreeWidgetItem
r1228 if (childItem) {
return createIndex(row, column, childItem);
}
return QModelIndex();
}
QModelIndex CatalogueTreeModel::parent(const QModelIndex &index) const
{
if (!index.isValid()) {
return QModelIndex();
}
Refactoring of catalogue: use a custom item class
r1229 auto childItem = item(index);
Refactoring of catalogue display using a QTreeView and a custom model based on QTreeWidgetItem
r1228 auto parentItem = childItem->parent();
if (parentItem == nullptr || parentItem->parent() == nullptr) {
return QModelIndex();
}
Refactoring of catalogue: use a custom item class
r1229 auto row = parentItem->parent()->children().indexOf(parentItem);
Refactoring of catalogue display using a QTreeView and a custom model based on QTreeWidgetItem
r1228 return createIndex(row, 0, parentItem);
}
int CatalogueTreeModel::rowCount(const QModelIndex &parent) const
{
Refactoring of catalogue: use a custom item class
r1229 CatalogueAbstractTreeItem *parentItem = nullptr;
Refactoring of catalogue display using a QTreeView and a custom model based on QTreeWidgetItem
r1228
if (!parent.isValid()) {
parentItem = impl->m_RootItem.get();
}
else {
Refactoring of catalogue: use a custom item class
r1229 parentItem = item(parent);
Refactoring of catalogue display using a QTreeView and a custom model based on QTreeWidgetItem
r1228 }
Refactoring of catalogue: use a custom item class
r1229 return parentItem->children().count();
Refactoring of catalogue display using a QTreeView and a custom model based on QTreeWidgetItem
r1228 }
int CatalogueTreeModel::columnCount(const QModelIndex &parent) const
{
return (int)Column::Count;
}
Qt::ItemFlags CatalogueTreeModel::flags(const QModelIndex &index) const
{
Refactoring of catalogue: use a custom item class
r1229 auto treeItem = item(index);
if (treeItem) {
return treeItem->flags(index.column());
Refactoring of catalogue display using a QTreeView and a custom model based on QTreeWidgetItem
r1228 }
return Qt::NoItemFlags;
}
QVariant CatalogueTreeModel::data(const QModelIndex &index, int role) const
{
Refactoring of catalogue: use a custom item class
r1229 auto treeItem = item(index);
if (treeItem) {
return treeItem->data(index.column(), role);
Refactoring of catalogue display using a QTreeView and a custom model based on QTreeWidgetItem
r1228 }
return QModelIndex();
}
bool CatalogueTreeModel::setData(const QModelIndex &index, const QVariant &value, int role)
{
Refactoring of catalogue: use a custom item class
r1229 auto treeItem = item(index);
if (treeItem) {
auto result = treeItem->setData(index.column(), role, value);
Refactoring of catalogue display using a QTreeView and a custom model based on QTreeWidgetItem
r1228
Refactoring of catalogue: use a custom item class
r1229 if (result && index.column() == (int)Column::Name) {
Refactoring of catalogue display using a QTreeView and a custom model based on QTreeWidgetItem
r1228 emit itemRenamed(index);
}
Refactoring of catalogue: use a custom item class
r1229 return result;
Refactoring of catalogue display using a QTreeView and a custom model based on QTreeWidgetItem
r1228 }
return false;
}
Refactoring of catalogue: use a custom item class
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)
{
Drop of events on a catalogue
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;
Refactoring of catalogue: use a custom item class
r1229 }
Qt::DropActions CatalogueTreeModel::supportedDropActions() const
{
return Qt::CopyAction | Qt::MoveAction;
}
QStringList CatalogueTreeModel::mimeTypes() const
{
return {MIME_TYPE_EVENT_LIST};
}