##// 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:

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