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/dic-options/PackFieldsListModel.cpp | 101 ++++++++++++++++++++++++++++++++ 1 file changed, 101 insertions(+) create mode 100644 src/dic-options/PackFieldsListModel.cpp (limited to 'src/dic-options/PackFieldsListModel.cpp') diff --git a/src/dic-options/PackFieldsListModel.cpp b/src/dic-options/PackFieldsListModel.cpp new file mode 100644 index 0000000..eb6eb6f --- /dev/null +++ b/src/dic-options/PackFieldsListModel.cpp @@ -0,0 +1,101 @@ +#include "PackFieldsListModel.h" +#include "../dictionary/CardPack.h" +#include "../dictionary/Field.h" + +#include + +int PackFieldsListModel::rowCount( const QModelIndex& /*parent*/ ) const + { + CardPack* pack = m_parent->m_dict.cardPack( m_parentRow ); + if( !pack ) + return 0; + return pack->getFields().size(); + } + +QVariant PackFieldsListModel::data( const QModelIndex &index, int role ) const + { + if (!index.isValid()) + return QVariant(); + if (index.row() >= rowCount()) + return QVariant(); + + CardPack* pack = m_parent->m_dict.cardPack( m_parentRow ); + if( !pack ) + return QVariant(); + const Field* field = pack->getFields().value(index.row()); + switch( role ) + { + case Qt::DisplayRole: + case Qt::EditRole: + return field->name(); + case Qt::FontRole: + if( index.row() == 0 ) + { + QFont font; + font.setBold(true); + return font; + } + else + return QFont(); + default: + return QVariant(); + } + } + +void PackFieldsListModel::changeParentRow( const QModelIndex& aIndex ) + { + m_parentRow = aIndex.row(); + emit layoutChanged(); + } + +bool PackFieldsListModel::setData(const QModelIndex& index, const QVariant& aValue, int role) + { + if( !index.isValid() || role != Qt::EditRole ) + return false; + const Field* field = m_parent->m_dict.field( aValue.toString() ); + if( !field ) + return false; + CardPack* pack = m_parent->m_dict.cardPack( m_parentRow ); + pack->setField( index.row(), field ); + emit dataChanged(index, index); + return true; + } + +bool PackFieldsListModel::insertRows(int position, int rows, const QModelIndex &/*parent*/) + { + beginInsertRows(QModelIndex(), position, position+rows-1); + CardPack* pack = m_parent->m_dict.cardPack( m_parentRow ); + for (int row = 0; row < rows; ++row) + pack->insertField( position, m_parent->m_emptyField ); + endInsertRows(); + return true; + } + +bool PackFieldsListModel::removeRows(int position, int rows, const QModelIndex &/*parent*/) + { + beginRemoveRows(QModelIndex(), position, position+rows-1); + CardPack* pack = m_parent->m_dict.cardPack( m_parentRow ); + for (int row = 0; row < rows; ++row) + pack->removeField( position ); + endRemoveRows(); + return true; + } + +const void* PackFieldsListModel::dataPtr( const QModelIndex& aIndex ) const + { + CardPack* pack = m_parent->m_dict.cardPack( m_parentRow ); + if( !pack ) + return NULL; + const Field* field = pack->getFields().value( aIndex.row() ); + return static_cast( field ); + } + +void PackFieldsListModel::insertPointer( int aPos, void* aData ) + { + beginInsertRows(QModelIndex(), aPos, aPos ); + CardPack* pack = m_parent->m_dict.cardPack( m_parentRow ); + if( !pack ) + return; + pack->insertField( aPos, static_cast( aData ) ); + endInsertRows(); + } -- cgit