17#include "moc_qgskeyvaluewidget.cpp"
28 removeButton->setEnabled(
false );
34 mModel.setReadOnly( readOnly );
39void QgsKeyValueModel::setMap(
const QVariantMap &map )
43 for ( QVariantMap::const_iterator it = map.constBegin(); it != map.constEnd(); ++it )
45 mLines.append( Line( it.key(), it.value() ) );
50QVariantMap QgsKeyValueModel::map()
const
53 for ( QVector<Line>::const_iterator it = mLines.constBegin(); it != mLines.constEnd(); ++it )
55 if ( !it->first.isEmpty() )
57 ret[it->first] = it->second;
63QgsKeyValueModel::QgsKeyValueModel( QObject *parent ) :
64 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 ||
92 index.row() >= mLines.count() ||
93 ( role != Qt::DisplayRole && role != Qt::EditRole ) )
97 if ( index.column() == 0 )
98 return mLines.at( index.row() ).first;
99 if ( index.column() == 1 )
100 return mLines.at( index.row() ).second;
104bool QgsKeyValueModel::setData(
const QModelIndex &index,
const QVariant &value,
int role )
109 if ( index.row() < 0 || index.row() >= mLines.count() || role != Qt::EditRole )
113 if ( index.column() == 0 )
115 mLines[index.row()].first = value.toString();
119 mLines[index.row()].second = value;
121 emit dataChanged( index, index );
125Qt::ItemFlags QgsKeyValueModel::flags(
const QModelIndex &index )
const
128 return QAbstractTableModel::flags( index ) | Qt::ItemIsEditable;
130 return QAbstractTableModel::flags( index );
133bool QgsKeyValueModel::insertRows(
int position,
int rows,
const QModelIndex &parent )
139 beginInsertRows( QModelIndex(), position, position + rows - 1 );
140 for (
int i = 0; i < rows; ++i )
142 mLines.insert( position, Line( QString(), QVariant() ) );
148bool QgsKeyValueModel::removeRows(
int position,
int rows,
const QModelIndex &parent )
154 beginRemoveRows( QModelIndex(), position, position + rows - 1 );
155 mLines.remove( position, rows );
160void QgsKeyValueModel::setReadOnly(
bool readOnly )
162 mReadOnly = readOnly;