QGIS API Documentation 3.40.0-Bratislava (b56115d8743)
Loading...
Searching...
No Matches
qgslistwidget.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgslistwidget.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 "qgslistwidget.h"
17#include "qgsvariantutils.h"
18
19QgsListWidget::QgsListWidget( QMetaType::Type subType, QWidget *parent )
20 : QgsTableWidgetBase( parent )
21 , mModel( subType, this )
22 , mSubType( subType )
23{
24 init( &mModel );
25}
26
27void QgsListWidget::setList( const QVariantList &list )
28{
29 removeButton->setEnabled( false );
30 mModel.setList( list );
31}
32
33void QgsListWidget::setReadOnly( bool readOnly )
34{
35 mModel.setReadOnly( readOnly );
37}
38
39
41QgsListModel::QgsListModel( QMetaType::Type subType, QObject *parent ) :
42 QAbstractTableModel( parent ),
43 mSubType( subType )
44{
45}
46
47void QgsListModel::setList( const QVariantList &list )
48{
49 beginResetModel();
50 mLines = list;
51 endResetModel();
52}
53
54QVariantList QgsListModel::list() const
55{
56 QVariantList result;
57 for ( QVariantList::const_iterator it = mLines.constBegin(); it != mLines.constEnd(); ++it )
58 {
59 QVariant cur = *it;
60 if ( cur.convert( mSubType ) )
61 result.append( cur );
62 }
63 return result;
64}
65
66bool QgsListModel::valid() const
67{
68 for ( QVariantList::const_iterator it = mLines.constBegin(); it != mLines.constEnd(); ++it )
69 {
70 QVariant cur = *it;
71 if ( !cur.convert( mSubType ) ) return false;
72 }
73 return true;
74}
75
76int QgsListModel::rowCount( const QModelIndex &parent ) const
77{
78 Q_UNUSED( parent )
79 return mLines.count();
80}
81
82int QgsListModel::columnCount( const QModelIndex &parent ) const
83{
84 Q_UNUSED( parent )
85 return 1;
86}
87
88QVariant QgsListModel::headerData( int section, Qt::Orientation orientation, int role ) const
89{
90 if ( orientation == Qt::Horizontal && role == Qt::DisplayRole && section == 0 )
91 {
92 return QObject::tr( "Value" );
93 }
94 return QVariant();
95}
96
97QVariant QgsListModel::data( const QModelIndex &index, int role ) const
98{
99 if ( index.row() < 0 ||
100 index.row() >= mLines.count() ||
101 ( role != Qt::DisplayRole && role != Qt::EditRole ) ||
102 index.column() != 0 )
103 {
104 return QgsVariantUtils::createNullVariant( mSubType );
105 }
106 return mLines.at( index.row() );
107}
108
109bool QgsListModel::setData( const QModelIndex &index, const QVariant &value, int role )
110{
111 if ( mReadOnly )
112 return false;
113
114 if ( index.row() < 0 || index.row() >= mLines.count() ||
115 index.column() != 0 || role != Qt::EditRole )
116 {
117 return false;
118 }
119 mLines[index.row()] = value.toString();
120 emit dataChanged( index, index );
121 return true;
122}
123
124Qt::ItemFlags QgsListModel::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 QgsListModel::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, QgsVariantUtils::createNullVariant( mSubType ) );
142 }
143 endInsertRows();
144 return true;
145}
146
147bool QgsListModel::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 for ( int i = 0; i < rows; ++i )
155 mLines.removeAt( position );
156 endRemoveRows();
157 return true;
158}
159
160void QgsListModel::setReadOnly( bool readOnly )
161{
162 mReadOnly = readOnly;
163}
QVariantList list
QgsListWidget(QMetaType::Type subType, QWidget *parent=nullptr)
Constructor.
void setReadOnly(bool readOnly) override
void setList(const QVariantList &list)
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.
static QVariant createNullVariant(QMetaType::Type metaType)
Helper method to properly create a null QVariant from a metaType Returns the created QVariant.