18#include "moc_qgskeyvaluewidget.cpp"
29 removeButton->setEnabled(
false );
35 mModel.setReadOnly( readOnly );
40void QgsKeyValueModel::setMap(
const QVariantMap &map )
44 for ( QVariantMap::const_iterator it = map.constBegin(); it != map.constEnd(); ++it )
46 mLines.append( Line( it.key(), it.value() ) );
51QVariantMap QgsKeyValueModel::map()
const
54 for ( QVector<Line>::const_iterator it = mLines.constBegin(); it != mLines.constEnd(); ++it )
56 if ( !it->first.isEmpty() )
58 ret[it->first] = it->second;
64QgsKeyValueModel::QgsKeyValueModel( QObject *parent )
65 : QAbstractTableModel( parent )
68int QgsKeyValueModel::rowCount(
const QModelIndex &parent )
const
71 return mLines.count();
74int QgsKeyValueModel::columnCount(
const QModelIndex &parent )
const
80QVariant QgsKeyValueModel::headerData(
int section, Qt::Orientation orientation,
int role )
const
82 if ( orientation == Qt::Horizontal && role == Qt::DisplayRole )
84 return QObject::tr( section == 0 ?
"Key" :
"Value" );
89QVariant QgsKeyValueModel::data(
const QModelIndex &index,
int role )
const
91 if ( index.row() < 0 || index.row() >= mLines.count() || ( role != Qt::DisplayRole && role != Qt::EditRole ) )
95 if ( index.column() == 0 )
96 return mLines.at( index.row() ).first;
97 if ( index.column() == 1 )
98 return mLines.at( index.row() ).second;
102bool QgsKeyValueModel::setData(
const QModelIndex &index,
const QVariant &value,
int role )
107 if ( index.row() < 0 || index.row() >= mLines.count() || role != Qt::EditRole )
111 if ( index.column() == 0 )
113 mLines[index.row()].first = value.toString();
117 mLines[index.row()].second = value;
119 emit dataChanged( index, index );
123Qt::ItemFlags QgsKeyValueModel::flags(
const QModelIndex &index )
const
126 return QAbstractTableModel::flags( index ) | Qt::ItemIsEditable;
128 return QAbstractTableModel::flags( index );
131bool QgsKeyValueModel::insertRows(
int position,
int rows,
const QModelIndex &parent )
137 beginInsertRows( QModelIndex(), position, position + rows - 1 );
138 for (
int i = 0; i < rows; ++i )
140 mLines.insert( position, Line( QString(), QVariant() ) );
146bool QgsKeyValueModel::removeRows(
int position,
int rows,
const QModelIndex &parent )
152 beginRemoveRows( QModelIndex(), position, position + rows - 1 );
153 mLines.remove( position, rows );
158void QgsKeyValueModel::setReadOnly(
bool readOnly )
160 mReadOnly = readOnly;