summaryrefslogtreecommitdiff
path: root/src/dic-options/PacksListModel.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/dic-options/PacksListModel.cpp')
-rw-r--r--src/dic-options/PacksListModel.cpp81
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();
+ }