QGIS API Documentation  3.14.0-Pi (9f7028fd23)
qgsquickvaluerelationlistmodel.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsquickvaluerelationlistmodel.cpp
3  --------------------------------------
4  Date : March 2020
5  Copyright : (C) 2020 by Martin Dobias
6  Email : wonder dot sk at gmail dot com
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 
17 
18 #include "qgslogger.h"
19 
20 
22  : QAbstractListModel( parent )
23 {
24 }
25 
26 void QgsQuickValueRelationListModel::populate( const QVariantMap &config, const QgsFeature &formFeature )
27 {
28  beginResetModel();
29  mCache = QgsValueRelationFieldFormatter::createCache( config, formFeature );
30  endResetModel();
31 }
32 
34 {
35  if ( row < 0 || row >= mCache.count() )
36  {
37  QgsDebugMsg( "keyForRow: access outside of range " + QString::number( row ) );
38  return QVariant();
39  }
40  return mCache[row].key;
41 }
42 
43 int QgsQuickValueRelationListModel::rowForKey( const QVariant &key ) const
44 {
45  for ( int i = 0; i < mCache.count(); ++i )
46  {
47  if ( mCache[i].key == key )
48  return i;
49  }
50  QgsDebugMsg( "rowForKey: key not found: " + key.toString() );
51  return -1;
52 }
53 
54 int QgsQuickValueRelationListModel::rowCount( const QModelIndex & ) const
55 {
56  return mCache.count();
57 }
58 
59 QVariant QgsQuickValueRelationListModel::data( const QModelIndex &index, int role ) const
60 {
61  if ( !index.isValid() )
62  return QVariant();
63 
64  int row = index.row();
65  if ( row < 0 || row >= mCache.count() )
66  return QVariant();
67 
68  if ( role == Qt::DisplayRole )
69  return mCache[row].value;
70 
71  return QVariant();
72 }
QgsQuickValueRelationListModel::populate
Q_INVOKABLE void populate(const QVariantMap &config, const QgsFeature &formFeature=QgsFeature())
Populates the model from vector layer's widget configuration.
Definition: qgsquickvaluerelationlistmodel.cpp:26
qgsquickvaluerelationlistmodel.h
QgsQuickValueRelationListModel::keyForRow
Q_INVOKABLE QVariant keyForRow(int row) const
Returns key for the given rown number (invalid variant if outside of the valid range)
Definition: qgsquickvaluerelationlistmodel.cpp:33
QgsDebugMsg
#define QgsDebugMsg(str)
Definition: qgslogger.h:38
QgsQuickValueRelationListModel::rowForKey
Q_INVOKABLE int rowForKey(const QVariant &key) const
Returns row number.
Definition: qgsquickvaluerelationlistmodel.cpp:43
QgsValueRelationFieldFormatter::createCache
QVariant createCache(QgsVectorLayer *layer, int fieldIndex, const QVariantMap &config) const override
Create a cache for a given field.
Definition: qgsvaluerelationfieldformatter.cpp:116
QgsQuickValueRelationListModel::QgsQuickValueRelationListModel
QgsQuickValueRelationListModel(QObject *parent=nullptr)
Constructs an empty list model.
Definition: qgsquickvaluerelationlistmodel.cpp:21
QgsQuickValueRelationListModel::data
QVariant data(const QModelIndex &index, int role=Qt::DisplayRole) const override
Definition: qgsquickvaluerelationlistmodel.cpp:59
QgsFeature
Definition: qgsfeature.h:55
qgslogger.h
QgsQuickValueRelationListModel::rowCount
int rowCount(const QModelIndex &) const override
Definition: qgsquickvaluerelationlistmodel.cpp:54