QGIS API Documentation 3.41.0-Master (cea29feecf2)
Loading...
Searching...
No Matches
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#include "moc_qgssingleitemmodel.cpp"
18
19
20QgsSingleItemModel::QgsSingleItemModel( QObject *parent, const QString &text, const QMap< int, QVariant > &data, Qt::ItemFlags flags )
21 : QAbstractItemModel( parent )
22 , mText( text )
23 , mData( data )
24 , mFlags( flags )
25{
26
27}
28
29QgsSingleItemModel::QgsSingleItemModel( QObject *parent, const QList<QMap<int, QVariant> > &columnData, Qt::ItemFlags flags )
30 : QAbstractItemModel( parent )
31 , mColumnData( columnData )
32 , mFlags( flags )
33{
34}
35
36QVariant QgsSingleItemModel::data( const QModelIndex &index, int role ) const
37{
38 if ( index.row() < 0 || index.row() >= rowCount( QModelIndex() ) )
39 return QVariant();
40
41 if ( index.column() < 0 || index.column() >= columnCount( QModelIndex() ) )
42 return QVariant();
43
44 if ( !mColumnData.isEmpty() )
45 {
46 return mColumnData.value( index.column() ).value( role );
47 }
48 else
49 {
50 switch ( role )
51 {
52 case Qt::DisplayRole:
53 return mText;
54
55 case Qt::ToolTipRole:
56 return mData.value( Qt::ToolTipRole, mText );
57
58 default:
59 return mData.value( role );
60 }
61 }
62}
63
64Qt::ItemFlags QgsSingleItemModel::flags( const QModelIndex &index ) const
65{
66 if ( index.isValid() )
67 {
68 return mFlags;
69 }
70 else
71 {
72 return QAbstractItemModel::flags( index );
73 }
74}
75
76QModelIndex QgsSingleItemModel::index( int row, int column, const QModelIndex &parent ) const
77{
78 if ( !hasIndex( row, column, parent ) )
79 return QModelIndex();
80
81 if ( !parent.isValid() )
82 {
83 return createIndex( row, column );
84 }
85
86 return QModelIndex();
87}
88
89QModelIndex QgsSingleItemModel::parent( const QModelIndex & ) const
90{
91 return QModelIndex();
92}
93
94int QgsSingleItemModel::rowCount( const QModelIndex &parent ) const
95{
96 if ( !parent.isValid() )
97 {
98 return 1;
99 }
100 return 0;
101}
102
103int QgsSingleItemModel::columnCount( const QModelIndex & ) const
104{
105 if ( !mColumnData.empty() )
106 return mColumnData.size();
107
108 return 1;
109}
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