From d24f813f3f2a05c112e803e4256b53535895fc98 Mon Sep 17 00:00:00 2001 From: Jedidiah Barber Date: Wed, 14 Jul 2021 11:49:10 +1200 Subject: Initial mirror commit --- src/main-view/PacksTreeModel.cpp | 93 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 93 insertions(+) create mode 100644 src/main-view/PacksTreeModel.cpp (limited to 'src/main-view/PacksTreeModel.cpp') diff --git a/src/main-view/PacksTreeModel.cpp b/src/main-view/PacksTreeModel.cpp new file mode 100644 index 0000000..55b8c34 --- /dev/null +++ b/src/main-view/PacksTreeModel.cpp @@ -0,0 +1,93 @@ +#include "PacksTreeModel.h" +#include "AppModel.h" +#include "../dictionary/TreeItem.h" + +PacksTreeModel::PacksTreeModel( AppModel* aAppModel, QObject* aParent ): + QAbstractItemModel( aParent ), m_appModel( aAppModel ) + { + } + +QVariant PacksTreeModel::data(const QModelIndex &index, int role) const + { + if( !index.isValid() ) + return QVariant(); + if( role != Qt::DisplayRole ) + return QVariant(); + TreeItem* item = static_cast( index.internalPointer() ); + return item->data( index.column() ); + } + +Qt::ItemFlags PacksTreeModel::flags(const QModelIndex &index) const + { + if( !index.isValid() ) + return 0; + if( !index.parent().isValid() ) // First level item = dictionary + return Qt::ItemIsEnabled; + else // pack + return Qt::ItemIsEnabled | Qt::ItemIsSelectable; + } + +QVariant PacksTreeModel::headerData(int section, Qt::Orientation orientation, int role) const + { + if( orientation == Qt::Horizontal && role == Qt::DisplayRole ) + switch( section ) + { + case 0: return tr("Card pack"); break; + case 1: return tr("Sched"); break; + case 2: return tr("New"); break; + } + return QVariant(); + } + +QModelIndex PacksTreeModel::index(int row, int column, const QModelIndex &parent) const + { + if( !hasIndex(row, column, parent) ) + return QModelIndex(); + if( !parent.isValid() ) // First level item = dictionary + { + Dictionary* dic = m_appModel->dictionary( row ); + if( dic ) + return createIndex( row, column, dic ); + else + return QModelIndex(); + } + TreeItem* parentItem = static_cast( parent.internalPointer() ); + const TreeItem* childItem = parentItem->child( row ); + if( childItem ) + return createIndex( row, column, (void*)childItem ); + else + return QModelIndex(); + } + +QModelIndex PacksTreeModel::parent(const QModelIndex &index) const + { + if( !index.isValid() ) + return QModelIndex(); + TreeItem* childItem = static_cast( index.internalPointer() ); + if( !childItem ) + return QModelIndex(); + const TreeItem* parentItem = childItem->parent(); + + if( parentItem ) + return createIndex( parentItem->row(), 0, (void*)parentItem ); + else + return QModelIndex(); // The root item + } + +int PacksTreeModel::rowCount(const QModelIndex &parent) const + { + if( parent.column() > 0 ) // Only the first column may have children + return 0; + if( !parent.isValid() ) // Root item + return m_appModel->dictionariesNum(); + TreeItem* parentItem = static_cast( parent.internalPointer() ); + return parentItem->childCount(); + } + +void PacksTreeModel::updateData() // TODO: Suspicious method, just reveals protected methods + { + beginResetModel(); + endResetModel(); + } + + -- cgit