QGIS API Documentation 3.39.0-Master (47f7b3a4989)
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
19 : QgsTableWidgetBase( parent )
20 , mModel( this )
21{
22 init( &mModel );
23}
24
25void QgsKeyValueWidget::setMap( const QVariantMap &map )
26{
27 removeButton->setEnabled( false );
28 mModel.setMap( map );
29}
30
31void QgsKeyValueWidget::setReadOnly( bool readOnly )
32{
33 mModel.setReadOnly( readOnly );
35}
36
38void QgsKeyValueModel::setMap( const QVariantMap &map )
39{
40 beginResetModel();
41 mLines.clear();
42 for ( QVariantMap::const_iterator it = map.constBegin(); it != map.constEnd(); ++it )
43 {
44 mLines.append( Line( it.key(), it.value() ) );
45 }
46 endResetModel();
47}
48
49QVariantMap QgsKeyValueModel::map() const
50{
51 QVariantMap ret;
52 for ( QVector<Line>::const_iterator it = mLines.constBegin(); it != mLines.constEnd(); ++it )
53 {
54 if ( !it->first.isEmpty() )
55 {
56 ret[it->first] = it->second;
57 }
58 }
59 return ret;
60}
61
62QgsKeyValueModel::QgsKeyValueModel( QObject *parent ) :
63 QAbstractTableModel( parent )
64{
65}
66
67int QgsKeyValueModel::rowCount( const QModelIndex &parent ) const
68{
69 Q_UNUSED( parent )
70 return mLines.count();
71}
72
73int QgsKeyValueModel::columnCount( const QModelIndex &parent ) const
74{
75 Q_UNUSED( parent )
76 return 2;
77}
78
79QVariant QgsKeyValueModel::headerData( int section, Qt::Orientation orientation, int role ) const
80{
81 if ( orientation == Qt::Horizontal && role == Qt::DisplayRole )
82 {
83 return QObject::tr( section == 0 ? "Key" : "Value" );
84 }
85 return QVariant();
86}
87
88QVariant QgsKeyValueModel::data( const QModelIndex &index, int role ) const
89{
90 if ( index.row() < 0 ||
91 index.row() >= mLines.count() ||
92 ( role != Qt::DisplayRole && role != Qt::EditRole ) )
93 {
94 return QVariant();
95 }
96 if ( index.column() == 0 )
97 return mLines.at( index.row() ).first;
98 if ( index.column() == 1 )
99 return mLines.at( index.row() ).second;
100 return QVariant();
101}
102
103bool QgsKeyValueModel::setData( const QModelIndex &index, const QVariant &value, int role )
104{
105 if ( mReadOnly )
106 return false;
107
108 if ( index.row() < 0 || index.row() >= mLines.count() || role != Qt::EditRole )
109 {
110 return false;
111 }
112 if ( index.column() == 0 )
113 {
114 mLines[index.row()].first = value.toString();
115 }
116 else
117 {
118 mLines[index.row()].second = value;
119 }
120 emit dataChanged( index, index );
121 return true;
122}
123
124Qt::ItemFlags QgsKeyValueModel::flags( const QModelIndex &index ) const
125{
126 if ( !mReadOnly )
127 return QAbstractTableModel::flags( index ) | Qt::ItemIsEditable;
128 else
129 return QAbstractTableModel::flags( index );
130}
131
132bool QgsKeyValueModel::insertRows( int position, int rows, const QModelIndex &parent )
133{
134 if ( mReadOnly )
135 return false;
136
137 Q_UNUSED( parent )
138 beginInsertRows( QModelIndex(), position, position + rows - 1 );
139 for ( int i = 0; i < rows; ++i )
140 {
141 mLines.insert( position, Line( QString(), QVariant() ) );
142 }
143 endInsertRows();
144 return true;
145}
146
147bool QgsKeyValueModel::removeRows( int position, int rows, const QModelIndex &parent )
148{
149 if ( mReadOnly )
150 return false;
151
152 Q_UNUSED( parent )
153 beginRemoveRows( QModelIndex(), position, position + rows - 1 );
154 mLines.remove( position, rows );
155 endRemoveRows();
156 return true;
157}
158
159void QgsKeyValueModel::setReadOnly( bool readOnly )
160{
161 mReadOnly = readOnly;
162}
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.