QGIS API Documentation 3.41.0-Master (45a0abf3bec)
Loading...
Searching...
No Matches
qgskeyvaluewidget.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgskeyvaluewidget.cpp
3 --------------------------------------
4 Date : 08.2016
5 Copyright : (C) 2016 Patrick Valsecchi
6 Email : patrick.valsecchi@camptocamp.com
7 ***************************************************************************
8 * *
9 * This program is free software; you can redistribute it and/or modify *
10 * it under the terms of the GNU General Public License as published by *
11 * the Free Software Foundation; either version 2 of the License, or *
12 * (at your option) any later version. *
13 * *
14 ***************************************************************************/
15
16#include "qgskeyvaluewidget.h"
17#include "moc_qgskeyvaluewidget.cpp"
18
20 : QgsTableWidgetBase( parent )
21 , mModel( this )
22{
23 init( &mModel );
24}
25
26void QgsKeyValueWidget::setMap( const QVariantMap &map )
27{
28 removeButton->setEnabled( false );
29 mModel.setMap( map );
30}
31
32void QgsKeyValueWidget::setReadOnly( bool readOnly )
33{
34 mModel.setReadOnly( readOnly );
36}
37
39void QgsKeyValueModel::setMap( const QVariantMap &map )
40{
41 beginResetModel();
42 mLines.clear();
43 for ( QVariantMap::const_iterator it = map.constBegin(); it != map.constEnd(); ++it )
44 {
45 mLines.append( Line( it.key(), it.value() ) );
46 }
47 endResetModel();
48}
49
50QVariantMap QgsKeyValueModel::map() const
51{
52 QVariantMap ret;
53 for ( QVector<Line>::const_iterator it = mLines.constBegin(); it != mLines.constEnd(); ++it )
54 {
55 if ( !it->first.isEmpty() )
56 {
57 ret[it->first] = it->second;
58 }
59 }
60 return ret;
61}
62
63QgsKeyValueModel::QgsKeyValueModel( QObject *parent ) :
64 QAbstractTableModel( parent )
65{
66}
67
68int QgsKeyValueModel::rowCount( const QModelIndex &parent ) const
69{
70 Q_UNUSED( parent )
71 return mLines.count();
72}
73
74int QgsKeyValueModel::columnCount( const QModelIndex &parent ) const
75{
76 Q_UNUSED( parent )
77 return 2;
78}
79
80QVariant QgsKeyValueModel::headerData( int section, Qt::Orientation orientation, int role ) const
81{
82 if ( orientation == Qt::Horizontal && role == Qt::DisplayRole )
83 {
84 return QObject::tr( section == 0 ? "Key" : "Value" );
85 }
86 return QVariant();
87}
88
89QVariant QgsKeyValueModel::data( const QModelIndex &index, int role ) const
90{
91 if ( index.row() < 0 ||
92 index.row() >= mLines.count() ||
93 ( role != Qt::DisplayRole && role != Qt::EditRole ) )
94 {
95 return QVariant();
96 }
97 if ( index.column() == 0 )
98 return mLines.at( index.row() ).first;
99 if ( index.column() == 1 )
100 return mLines.at( index.row() ).second;
101 return QVariant();
102}
103
104bool QgsKeyValueModel::setData( const QModelIndex &index, const QVariant &value, int role )
105{
106 if ( mReadOnly )
107 return false;
108
109 if ( index.row() < 0 || index.row() >= mLines.count() || role != Qt::EditRole )
110 {
111 return false;
112 }
113 if ( index.column() == 0 )
114 {
115 mLines[index.row()].first = value.toString();
116 }
117 else
118 {
119 mLines[index.row()].second = value;
120 }
121 emit dataChanged( index, index );
122 return true;
123}
124
125Qt::ItemFlags QgsKeyValueModel::flags( const QModelIndex &index ) const
126{
127 if ( !mReadOnly )
128 return QAbstractTableModel::flags( index ) | Qt::ItemIsEditable;
129 else
130 return QAbstractTableModel::flags( index );
131}
132
133bool QgsKeyValueModel::insertRows( int position, int rows, const QModelIndex &parent )
134{
135 if ( mReadOnly )
136 return false;
137
138 Q_UNUSED( parent )
139 beginInsertRows( QModelIndex(), position, position + rows - 1 );
140 for ( int i = 0; i < rows; ++i )
141 {
142 mLines.insert( position, Line( QString(), QVariant() ) );
143 }
144 endInsertRows();
145 return true;
146}
147
148bool QgsKeyValueModel::removeRows( int position, int rows, const QModelIndex &parent )
149{
150 if ( mReadOnly )
151 return false;
152
153 Q_UNUSED( parent )
154 beginRemoveRows( QModelIndex(), position, position + rows - 1 );
155 mLines.remove( position, rows );
156 endRemoveRows();
157 return true;
158}
159
160void QgsKeyValueModel::setReadOnly( bool readOnly )
161{
162 mReadOnly = readOnly;
163}
QgsKeyValueWidget(QWidget *parent=nullptr)
Constructor.
void setReadOnly(bool readOnly) override
void setMap(const QVariantMap &map)
Set the initial value of the widget.
Base widget allowing to edit a collection, using a table.
void init(QAbstractTableModel *model)
Initialize the table with the given model.
virtual void setReadOnly(bool readOnly)
Sets whether the widget should be shown in a read-only state.