diff options
Diffstat (limited to 'src/dic-options/PackFieldsListModel.cpp')
-rw-r--r-- | src/dic-options/PackFieldsListModel.cpp | 101 |
1 files changed, 101 insertions, 0 deletions
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 <QMimeData> + +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<const void*>( 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<Field*>( aData ) ); + endInsertRows(); + } |