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/UnusedFieldsListModel.cpp |
Diffstat (limited to 'src/dic-options/UnusedFieldsListModel.cpp')
-rw-r--r-- | src/dic-options/UnusedFieldsListModel.cpp | 111 |
1 files changed, 111 insertions, 0 deletions
diff --git a/src/dic-options/UnusedFieldsListModel.cpp b/src/dic-options/UnusedFieldsListModel.cpp new file mode 100644 index 0000000..3311285 --- /dev/null +++ b/src/dic-options/UnusedFieldsListModel.cpp @@ -0,0 +1,111 @@ +#include "UnusedFieldsListModel.h" +#include "../dictionary/CardPack.h" +#include "../dictionary/Dictionary.h" + +#include <QMimeData> + +UnusedFieldsListModel::UnusedFieldsListModel( DictionaryOptionsDialog* aParent ): + DraggableListModel( aParent ), m_parentRow( 0 ) + { + updateUnusedFields(); + } + + +int UnusedFieldsListModel::rowCount(const QModelIndex& /*parent*/) const + { + return m_unusedFields.size(); + } + +QVariant UnusedFieldsListModel::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: + return m_unusedFields.value(index.row())->name(); + default: + return QVariant(); + } + } + +void UnusedFieldsListModel::updateUnusedFields() + { + beginResetModel(); + m_unusedFields.clear(); + CardPack* pack = m_parent->m_dict.cardPack( m_parentRow ); + if( !pack ) + return; + QList<const Field*> packFields = pack->getFields(); + for( int i=0; i < m_parent->m_dict.fieldsNum(); i++ ) + { + const Field* dictField = m_parent->m_dict.field( i ); + if( !packFields.contains( dictField ) ) + m_unusedFields << dictField; + } + endResetModel(); + } + +void UnusedFieldsListModel::changeParentRow( const QModelIndex& aIndex ) + { + m_parentRow = aIndex.row(); + updateUnusedFields(); + emit layoutChanged(); + } + +bool UnusedFieldsListModel::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; + m_unusedFields[index.row()] = field; + emit dataChanged(index, index); + return true; + } + +bool UnusedFieldsListModel::insertRows(int position, int rows, const QModelIndex &/*parent*/) + { + beginInsertRows(QModelIndex(), position, position+rows-1); + for (int row = 0; row < rows; ++row) + m_unusedFields.insert( position, m_parent->m_emptyField ); + endInsertRows(); + updateUnusedFields(); + return true; + } + +bool UnusedFieldsListModel::removeRows(int position, int rows, const QModelIndex &/*parent*/) + { + beginRemoveRows(QModelIndex(), position, position+rows-1); + for (int row = 0; row < rows; ++row) + m_unusedFields.removeAt( position ); + endRemoveRows(); + updateUnusedFields(); + return true; + } + +const void* UnusedFieldsListModel::dataPtr( const QModelIndex& aIndex ) const + { + const Field* field = m_unusedFields.value( aIndex.row() ); + return static_cast<const void*>( field ); + } + +void UnusedFieldsListModel::insertPointer( int aPos, void* aData ) + { + m_unusedFields.insert( aPos, static_cast<Field*>( aData ) ); + } + +bool UnusedFieldsListModel::dropMimeData(const QMimeData *data, Qt::DropAction action, + int /*row*/, int /*column*/, const QModelIndex &/*parent*/) + { + if (action == Qt::IgnoreAction) + return true; + if (!data->hasFormat("application/octet-stream")) + return false; + return true; + } |