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