QGIS API Documentation 3.30.0-'s-Hertogenbosch (f186b8efe0)
qgssingleitemmodel.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgssingleitemmodel.cpp
3 ---------------
4 begin : May 2022
5 copyright : (C) 2022 by Nyall Dawson
6 email : nyall dot dawson 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
16#include "qgssingleitemmodel.h"
17
18
19QgsSingleItemModel::QgsSingleItemModel( QObject *parent, const QString &text, const QMap< int, QVariant > &data, Qt::ItemFlags flags )
20 : QAbstractItemModel( parent )
21 , mText( text )
22 , mData( data )
23 , mFlags( flags )
24{
25
26}
27
28QgsSingleItemModel::QgsSingleItemModel( QObject *parent, const QList<QMap<int, QVariant> > &columnData, Qt::ItemFlags flags )
29 : QAbstractItemModel( parent )
30 , mColumnData( columnData )
31 , mFlags( flags )
32{
33}
34
35QVariant QgsSingleItemModel::data( const QModelIndex &index, int role ) const
36{
37 if ( index.row() < 0 || index.row() >= rowCount( QModelIndex() ) )
38 return QVariant();
39
40 if ( index.column() < 0 || index.column() >= columnCount( QModelIndex() ) )
41 return QVariant();
42
43 if ( !mColumnData.isEmpty() )
44 {
45 return mColumnData.value( index.column() ).value( role );
46 }
47 else
48 {
49 switch ( role )
50 {
51 case Qt::DisplayRole:
52 return mText;
53
54 case Qt::ToolTipRole:
55 return mData.value( Qt::ToolTipRole, mText );
56
57 default:
58 return mData.value( role );
59 }
60 }
61}
62
63Qt::ItemFlags QgsSingleItemModel::flags( const QModelIndex &index ) const
64{
65 if ( index.isValid() )
66 {
67 return mFlags;
68 }
69 else
70 {
71 return QAbstractItemModel::flags( index );
72 }
73}
74
75QModelIndex QgsSingleItemModel::index( int row, int column, const QModelIndex &parent ) const
76{
77 if ( !hasIndex( row, column, parent ) )
78 return QModelIndex();
79
80 if ( !parent.isValid() )
81 {
82 return createIndex( row, column );
83 }
84
85 return QModelIndex();
86}
87
88QModelIndex QgsSingleItemModel::parent( const QModelIndex & ) const
89{
90 return QModelIndex();
91}
92
93int QgsSingleItemModel::rowCount( const QModelIndex &parent ) const
94{
95 if ( !parent.isValid() )
96 {
97 return 1;
98 }
99 return 0;
100}
101
102int QgsSingleItemModel::columnCount( const QModelIndex & ) const
103{
104 if ( !mColumnData.empty() )
105 return mColumnData.size();
106
107 return 1;
108}
QModelIndex index(int row, int column, const QModelIndex &parent=QModelIndex()) const override
Qt::ItemFlags flags(const QModelIndex &index) const override
int columnCount(const QModelIndex &parent=QModelIndex()) const override
QVariant data(const QModelIndex &index, int role) const override
QModelIndex parent(const QModelIndex &index) const override
QgsSingleItemModel(QObject *parent=nullptr, const QString &text=QString(), const QMap< int, QVariant > &data=QMap< int, QVariant >(), Qt::ItemFlags flags=Qt::NoItemFlags)
Constructor for QgsSingleItemModel with the specified parent object and display text.
int rowCount(const QModelIndex &parent=QModelIndex()) const override