QGIS API Documentation  3.16.0-Hannover (43b64b13f3)
qgslistwidget.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgslistwidget.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 "qgslistwidget.h"
17 
18 QgsListWidget::QgsListWidget( QVariant::Type subType, QWidget *parent )
19  : QgsTableWidgetBase( parent )
20  , mModel( subType, this )
21  , mSubType( subType )
22 {
23  init( &mModel );
24 }
25 
26 void QgsListWidget::setList( const QVariantList &list )
27 {
28  removeButton->setEnabled( false );
29  mModel.setList( list );
30 }
31 
32 
34 QgsListModel::QgsListModel( QVariant::Type subType, QObject *parent ) :
35  QAbstractTableModel( parent ),
36  mSubType( subType )
37 {
38 }
39 
40 void QgsListModel::setList( const QVariantList &list )
41 {
42  beginResetModel();
43  mLines = list;
44  endResetModel();
45 }
46 
47 QVariantList QgsListModel::list() const
48 {
49  QVariantList result;
50  for ( QVariantList::const_iterator it = mLines.constBegin(); it != mLines.constEnd(); ++it )
51  {
52  QVariant cur = *it;
53  if ( cur.convert( mSubType ) )
54  result.append( cur );
55  }
56  return result;
57 }
58 
59 bool QgsListModel::valid() const
60 {
61  for ( QVariantList::const_iterator it = mLines.constBegin(); it != mLines.constEnd(); ++it )
62  {
63  QVariant cur = *it;
64  if ( !cur.convert( mSubType ) ) return false;
65  }
66  return true;
67 }
68 
69 int QgsListModel::rowCount( const QModelIndex &parent ) const
70 {
71  Q_UNUSED( parent )
72  return mLines.count();
73 }
74 
75 int QgsListModel::columnCount( const QModelIndex &parent ) const
76 {
77  Q_UNUSED( parent )
78  return 1;
79 }
80 
81 QVariant QgsListModel::headerData( int section, Qt::Orientation orientation, int role ) const
82 {
83  if ( orientation == Qt::Horizontal && role == Qt::DisplayRole && section == 0 )
84  {
85  return QObject::tr( "Value" );
86  }
87  return QVariant();
88 }
89 
90 QVariant QgsListModel::data( const QModelIndex &index, int role ) const
91 {
92  if ( index.row() < 0 ||
93  index.row() >= mLines.count() ||
94  ( role != Qt::DisplayRole && role != Qt::EditRole ) ||
95  index.column() != 0 )
96  {
97  return QVariant( mSubType );
98  }
99  return mLines.at( index.row() );
100 }
101 
102 bool QgsListModel::setData( const QModelIndex &index, const QVariant &value, int role )
103 {
104  if ( index.row() < 0 || index.row() >= mLines.count() ||
105  index.column() != 0 || role != Qt::EditRole )
106  {
107  return false;
108  }
109  mLines[index.row()] = value.toString();
110  emit dataChanged( index, index );
111  return true;
112 }
113 
114 Qt::ItemFlags QgsListModel::flags( const QModelIndex &index ) const
115 {
116  return QAbstractTableModel::flags( index ) | Qt::ItemIsEditable;
117 }
118 
119 bool QgsListModel::insertRows( int position, int rows, const QModelIndex &parent )
120 {
121  Q_UNUSED( parent )
122  beginInsertRows( QModelIndex(), position, position + rows - 1 );
123  for ( int i = 0; i < rows; ++i )
124  {
125  mLines.insert( position, QVariant( mSubType ) );
126  }
127  endInsertRows();
128  return true;
129 }
130 
131 bool QgsListModel::removeRows( int position, int rows, const QModelIndex &parent )
132 {
133  Q_UNUSED( parent )
134  beginRemoveRows( QModelIndex(), position, position + rows - 1 );
135  for ( int i = 0; i < rows; ++i )
136  mLines.removeAt( position );
137  endRemoveRows();
138  return true;
139 }
QgsTableWidgetBase::init
void init(QAbstractTableModel *model)
Initialize the table with the given model.
Definition: qgstablewidgetbase.cpp:26
QgsTableWidgetBase
Base widget allowing to edit a collection, using a table.
Definition: qgstablewidgetbase.h:34
qgslistwidget.h
QgsListWidget::list
QVariantList list
Definition: qgslistwidget.h:69
QgsListWidget::QgsListWidget
QgsListWidget(QVariant::Type subType, QWidget *parent=nullptr)
Constructor.
Definition: qgslistwidget.cpp:18
QgsListWidget::setList
void setList(const QVariantList &list)
Set the initial value of the widget.
Definition: qgslistwidget.cpp:26