QGIS API Documentation 3.41.0-Master (cea29feecf2)
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 "moc_qgslistwidget.cpp"
18#include "qgsvariantutils.h"
19
20QgsListWidget::QgsListWidget( QMetaType::Type subType, QWidget *parent )
21 : QgsTableWidgetBase( parent )
22 , mModel( subType, this )
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 ), mSubType( subType )
43{
44}
45
46void QgsListModel::setList( const QVariantList &list )
47{
48 beginResetModel();
49 mLines = list;
50 endResetModel();
51}
52
53QVariantList QgsListModel::list() const
54{
55 QVariantList result;
56 for ( QVariantList::const_iterator it = mLines.constBegin(); it != mLines.constEnd(); ++it )
57 {
58 QVariant cur = *it;
59 if ( cur.convert( mSubType ) )
60 result.append( cur );
61 }
62 return result;
63}
64
65bool QgsListModel::valid() const
66{
67 for ( QVariantList::const_iterator it = mLines.constBegin(); it != mLines.constEnd(); ++it )
68 {
69 QVariant cur = *it;
70 if ( !cur.convert( mSubType ) )
71 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 || index.row() >= mLines.count() || ( role != Qt::DisplayRole && role != Qt::EditRole ) || index.column() != 0 )
100 {
101 return QgsVariantUtils::createNullVariant( mSubType );
102 }
103 return mLines.at( index.row() );
104}
105
106bool QgsListModel::setData( const QModelIndex &index, const QVariant &value, int role )
107{
108 if ( mReadOnly )
109 return false;
110
111 if ( index.row() < 0 || index.row() >= mLines.count() || index.column() != 0 || role != Qt::EditRole )
112 {
113 return false;
114 }
115 mLines[index.row()] = value.toString();
116 emit dataChanged( index, index );
117 return true;
118}
119
120Qt::ItemFlags QgsListModel::flags( const QModelIndex &index ) const
121{
122 if ( !mReadOnly )
123 return QAbstractTableModel::flags( index ) | Qt::ItemIsEditable;
124 else
125 return QAbstractTableModel::flags( index );
126}
127
128bool QgsListModel::insertRows( int position, int rows, const QModelIndex &parent )
129{
130 if ( mReadOnly )
131 return false;
132
133 Q_UNUSED( parent )
134 beginInsertRows( QModelIndex(), position, position + rows - 1 );
135 for ( int i = 0; i < rows; ++i )
136 {
137 mLines.insert( position, QgsVariantUtils::createNullVariant( mSubType ) );
138 }
139 endInsertRows();
140 return true;
141}
142
143bool QgsListModel::removeRows( int position, int rows, const QModelIndex &parent )
144{
145 if ( mReadOnly )
146 return false;
147
148 Q_UNUSED( parent )
149 beginRemoveRows( QModelIndex(), position, position + rows - 1 );
150 for ( int i = 0; i < rows; ++i )
151 mLines.removeAt( position );
152 endRemoveRows();
153 return true;
154}
155
156void QgsListModel::setReadOnly( bool readOnly )
157{
158 mReadOnly = readOnly;
159}
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.