##// END OF EJS Templates
Updates model after an event has been created through the colored zone
Updates model after an event has been created through the colored zone

File last commit:

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