QGIS API Documentation 3.99.0-Master (2fe06baccd8)
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
18#include "moc_qgskeyvaluewidget.cpp"
19
21 : QgsTableWidgetBase( parent )
22 , mModel( this )
23{
24 init( &mModel );
25}
26
27void QgsKeyValueWidget::setMap( const QVariantMap &map )
28{
29 removeButton->setEnabled( false );
30 mModel.setMap( map );
31}
32
33void QgsKeyValueWidget::setReadOnly( bool readOnly )
34{
35 mModel.setReadOnly( readOnly );
37}
38
40void QgsKeyValueModel::setMap( const QVariantMap &map )
41{
42 beginResetModel();
43 mLines.clear();
44 for ( QVariantMap::const_iterator it = map.constBegin(); it != map.constEnd(); ++it )
45 {
46 mLines.append( Line( it.key(), it.value() ) );
47 }
48 endResetModel();
49}
50
51QVariantMap QgsKeyValueModel::map() const
52{
53 QVariantMap ret;
54 for ( QVector<Line>::const_iterator it = mLines.constBegin(); it != mLines.constEnd(); ++it )
55 {
56 if ( !it->first.isEmpty() )
57 {
58 ret[it->first] = it->second;
59 }
60 }
61 return ret;
62}
63
64QgsKeyValueModel::QgsKeyValueModel( QObject *parent )
65 : QAbstractTableModel( parent )
66{
67}
68
69int QgsKeyValueModel::rowCount( const QModelIndex &parent ) const
70{
71 Q_UNUSED( parent )
72 return mLines.count();
73}
74
75int QgsKeyValueModel::columnCount( const QModelIndex &parent ) const
76{
77 Q_UNUSED( parent )
78 return 2;
79}
80
81QVariant QgsKeyValueModel::headerData( int section, Qt::Orientation orientation, int role ) const
82{
83 if ( orientation == Qt::Horizontal && role == Qt::DisplayRole )
84 {
85 return QObject::tr( section == 0 ? "Key" : "Value" );
86 }
87 return QVariant();
88}
89
90QVariant QgsKeyValueModel::data( const QModelIndex &index, int role ) const
91{
92 if ( index.row() < 0 || index.row() >= mLines.count() || ( 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.
void init(QAbstractTableModel *model)
Initialize the table with the given model.
QgsTableWidgetBase(QWidget *parent)
Constructor.
virtual void setReadOnly(bool readOnly)
Sets whether the widget should be shown in a read-only state.