diff options
author | Jedidiah Barber <contact@jedbarber.id.au> | 2021-07-14 11:49:10 +1200 |
---|---|---|
committer | Jedidiah Barber <contact@jedbarber.id.au> | 2021-07-14 11:49:10 +1200 |
commit | d24f813f3f2a05c112e803e4256b53535895fc98 (patch) | |
tree | 601e6ae9a1cd44bcfdcf91739a5ca36aedd827c9 /src/dic-options/PacksListModel.cpp |
Diffstat (limited to 'src/dic-options/PacksListModel.cpp')
-rw-r--r-- | src/dic-options/PacksListModel.cpp | 81 |
1 files changed, 81 insertions, 0 deletions
diff --git a/src/dic-options/PacksListModel.cpp b/src/dic-options/PacksListModel.cpp new file mode 100644 index 0000000..c2dfc31 --- /dev/null +++ b/src/dic-options/PacksListModel.cpp @@ -0,0 +1,81 @@ +#include "PacksListModel.h" +#include "../dictionary/CardPack.h" + +QVariant PacksListModel::data( const QModelIndex &index, int role ) const + { + if (!index.isValid()) + return QVariant(); + if (index.row() >= rowCount()) + return QVariant(); + + switch( role ) + { + case Qt::DisplayRole: + case Qt::EditRole: + { + const CardPack* pack = m_parent->m_dict.cardPack( index.row() ); + if( !pack ) + return QVariant(); + return pack->id(); + } + default: + return QVariant(); + } + } + +bool PacksListModel::setData(const QModelIndex& index, const QVariant& aValue, int role) + { + if( !index.isValid() || role != Qt::EditRole ) + return false; + m_parent->m_dict.cardPack( index.row() )->setName( aValue.toString() ); + emit dataChanged(index, index); + return true; + } + +Qt::ItemFlags PacksListModel::flags(const QModelIndex &index) const + { + Qt::ItemFlags defaultFlags = DraggableListModel::flags(index); + return defaultFlags; + } + +bool PacksListModel::insertRows(int position, int rows, const QModelIndex &/*parent*/) + { + beginInsertRows(QModelIndex(), position, position+rows-1); + for (int i = 0; i < rows; i++) + { + CardPack* newPack = new CardPack( &(m_parent->m_dict) ); + newPack->addField( m_parent->m_dict.field(0) ); + m_parent->m_dict.insertPack( position + i, newPack ); + } + endInsertRows(); + return true; + } + +bool PacksListModel::removeRows(int position, int rows, const QModelIndex &/*parent*/) + { + beginRemoveRows(QModelIndex(), position, position+rows-1); + for (int row = 0; row < rows; ++row) + m_parent->m_dict.removePack( position ); + endRemoveRows(); + return true; + } + +const void* PacksListModel::dataPtr( const QModelIndex& aIndex ) const + { + CardPack* pack = m_parent->m_dict.cardPack( aIndex.row() ); + return static_cast<const void*>( pack ); + } + +void PacksListModel::insertPointer( int aPos, void* aData ) + { + beginInsertRows(QModelIndex(), aPos, aPos ); + m_parent->m_dict.insertPack( aPos, static_cast<CardPack*>( aData ) ); + endInsertRows(); + } + +void PacksListModel::removePack( int aPos ) + { + beginRemoveRows(QModelIndex(), aPos, aPos); + m_parent->m_dict.destroyPack( aPos ); + endRemoveRows(); + } |