QGIS API Documentation 3.41.0-Master (57ec4277f5e)
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
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 || index.row() >= mLines.count() || ( role != Qt::DisplayRole && role != Qt::EditRole ) )
92 {
93 return QVariant();
94 }
95 if ( index.column() == 0 )
96 return mLines.at( index.row() ).first;
97 if ( index.column() == 1 )
98 return mLines.at( index.row() ).second;
99 return QVariant();
100}
101
102bool QgsKeyValueModel::setData( const QModelIndex &index, const QVariant &value, int role )
103{
104 if ( mReadOnly )
105 return false;
106
107 if ( index.row() < 0 || index.row() >= mLines.count() || role != Qt::EditRole )
108 {
109 return false;
110 }
111 if ( index.column() == 0 )
112 {
113 mLines[index.row()].first = value.toString();
114 }
115 else
116 {
117 mLines[index.row()].second = value;
118 }
119 emit dataChanged( index, index );
120 return true;
121}
122
123Qt::ItemFlags QgsKeyValueModel::flags( const QModelIndex &index ) const
124{
125 if ( !mReadOnly )
126 return QAbstractTableModel::flags( index ) | Qt::ItemIsEditable;
127 else
128 return QAbstractTableModel::flags( index );
129}
130
131bool QgsKeyValueModel::insertRows( int position, int rows, const QModelIndex &parent )
132{
133 if ( mReadOnly )
134 return false;
135
136 Q_UNUSED( parent )
137 beginInsertRows( QModelIndex(), position, position + rows - 1 );
138 for ( int i = 0; i < rows; ++i )
139 {
140 mLines.insert( position, Line( QString(), QVariant() ) );
141 }
142 endInsertRows();
143 return true;
144}
145
146bool QgsKeyValueModel::removeRows( int position, int rows, const QModelIndex &parent )
147{
148 if ( mReadOnly )
149 return false;
150
151 Q_UNUSED( parent )
152 beginRemoveRows( QModelIndex(), position, position + rows - 1 );
153 mLines.remove( position, rows );
154 endRemoveRows();
155 return true;
156}
157
158void QgsKeyValueModel::setReadOnly( bool readOnly )
159{
160 mReadOnly = readOnly;
161}
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.