QGIS API Documentation
qgsfeaturelistviewdelegate.cpp
Go to the documentation of this file.
2 #include "qgsvectorlayer.h"
4 #include "qgsfeaturelistmodel.h"
5 #include "qgsapplication.h"
8 
9 #include <QHBoxLayout>
10 #include <QPushButton>
11 #include <QLabel>
12 #include <QApplication>
13 #include <QMouseEvent>
14 #include <QObject>
15 
17  : QItemDelegate( parent )
18  , mFeatureSelectionModel( nullptr )
19  , mEditSelectionModel( nullptr )
20  , mListModel( listModel )
21  , mCurrentFeatureEdited( false )
22 {
23 }
24 
26 {
27  if ( pos.x() > sIconSize )
28  {
29  return EditElement;
30  }
31  else
32  {
33  return SelectionElement;
34  }
35 }
36 
38 {
39  mFeatureSelectionModel = featureSelectionModel;
40 }
41 
43 {
44  mCurrentFeatureEdited = state;
45 }
46 
48 {
49  mEditSelectionModel = editSelectionModel;
50 }
51 
53 {
54  Q_UNUSED( index )
55  int height = sIconSize;
56  return QSize( option.rect.width(), qMax( height, option.fontMetrics.height() ) );
57 }
58 
60 {
61  static QPixmap selectedIcon;
62  if ( selectedIcon.isNull() )
63  selectedIcon = QgsApplication::getThemePixmap( "/mIconSelected.svg" );
64  static QPixmap deselectedIcon;
65  if ( deselectedIcon.isNull() )
66  deselectedIcon = QgsApplication::getThemePixmap( "/mIconDeselected.svg" );
67 
68  QString text = index.model()->data( index, Qt::EditRole ).toString();
69  QgsFeatureListModel::FeatureInfo featInfo = index.model()->data( index, Qt::UserRole ).value<QgsFeatureListModel::FeatureInfo>();
70 
71  bool isEditSelection = mEditSelectionModel && mEditSelectionModel->isSelected( mListModel->mapToMaster( index ) );
72 
73  // Icon layout options
74  QStyleOptionViewItem iconOption;
75 
76  QRect iconLayoutBounds( option.rect.x(), option.rect.y(), option.rect.height(), option.rect.height() );
77 
78  QPixmap icon = mFeatureSelectionModel->isSelected( index ) ? selectedIcon : deselectedIcon;
79 
80  // Scale up the icon if needed
81  if ( option.rect.height() > sIconSize )
82  {
83  icon = icon.scaledToHeight( option.rect.height(), Qt::SmoothTransformation );
84  }
85 
86  // Text layout options
87  QRect textLayoutBounds( iconLayoutBounds.x() + iconLayoutBounds.width(), option.rect.y(), option.rect.width() - ( iconLayoutBounds.x() + iconLayoutBounds.width() ), option.rect.height() );
88 
89  QStyleOptionViewItem textOption;
90  textOption.state |= QStyle::State_Enabled;
91  if ( isEditSelection )
92  {
93  textOption.state |= QStyle::State_Selected;
94  }
95 
96  if ( featInfo.isNew )
97  {
98  textOption.font.setStyle( QFont::StyleItalic );
99  textOption.palette.setColor( QPalette::Text, Qt::darkGreen );
100  textOption.palette.setColor( QPalette::HighlightedText, Qt::darkGreen );
101  }
102  else if ( featInfo.isEdited || ( mCurrentFeatureEdited && isEditSelection ) )
103  {
104  textOption.font.setStyle( QFont::StyleItalic );
105  textOption.palette.setColor( QPalette::Text, Qt::red );
106  textOption.palette.setColor( QPalette::HighlightedText, Qt::red );
107  }
108 
109  drawDisplay( painter, textOption, textLayoutBounds, text );
110  drawDecoration( painter, iconOption, iconLayoutBounds, icon );
111 }
static unsigned index
virtual bool isSelected(QgsFeatureId fid)
Returns the selection status of a given feature id.
virtual void drawDecoration(QPainter *painter, const QStyleOptionViewItem &option, const QRect &rect, const QPixmap &pixmap) const
virtual QModelIndex mapToMaster(const QModelIndex &proxyIndex) const
T value() const
static QPixmap getThemePixmap(const QString &theName)
Helper to get a theme icon as a pixmap.
int x() const
QPixmap scaledToHeight(int height, Qt::TransformationMode mode) const
virtual void drawDisplay(QPainter *painter, const QStyleOptionViewItem &option, const QRect &rect, const QString &text) const
void setFeatureSelectionModel(QgsFeatureSelectionModel *featureSelectionModel)
virtual QSize sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const override
virtual void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const override
QgsFeatureListViewDelegate(QgsFeatureListModel *listModel, QObject *parent=nullptr)
virtual QVariant data(const QModelIndex &index, int role) const =0
bool isNull() const
const QAbstractItemModel * model() const
bool isSelected(const QModelIndex &index) const
void setEditSelectionModel(QItemSelectionModel *editSelectionModel)
QString toString() const