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