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