QGIS API Documentation  3.6.0-Noosa (5873452)
qgsfeaturelistviewdelegate.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsfeaturelistviewdelegate.cpp
3  ---------------------
4  begin : February 2013
5  copyright : (C) 2013 by Matthias Kuhn
6  email : matthias at opengis dot ch
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  ***************************************************************************/
16 #include "qgsvectorlayer.h"
17 #include "qgsattributetablemodel.h"
18 #include "qgsfeaturelistmodel.h"
19 #include "qgsapplication.h"
22 
23 #include <QHBoxLayout>
24 #include <QPushButton>
25 #include <QLabel>
26 #include <QApplication>
27 #include <QMouseEvent>
28 #include <QObject>
29 
31  : QItemDelegate( parent )
32  , mListModel( listModel )
33  , mCurrentFeatureEdited( false )
34 {
35 }
36 
38 {
39  if ( pos.x() > ICON_SIZE )
40  {
41  return EditElement;
42  }
43  else
44  {
45  return SelectionElement;
46  }
47 }
48 
50 {
51  mFeatureSelectionModel = featureSelectionModel;
52 }
53 
55 {
56  mCurrentFeatureEdited = state;
57 }
58 
59 void QgsFeatureListViewDelegate::setEditSelectionModel( QItemSelectionModel *editSelectionModel )
60 {
61  mEditSelectionModel = editSelectionModel;
62 }
63 
64 QSize QgsFeatureListViewDelegate::sizeHint( const QStyleOptionViewItem &option, const QModelIndex &index ) const
65 {
66  Q_UNUSED( index )
67  int height = ICON_SIZE;
68  return QSize( option.rect.width(), std::max( height, option.fontMetrics.height() ) );
69 }
70 
71 void QgsFeatureListViewDelegate::paint( QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index ) const
72 {
73  static QPixmap sSelectedIcon;
74  if ( sSelectedIcon.isNull() )
75  sSelectedIcon = QgsApplication::getThemePixmap( QStringLiteral( "/mIconSelected.svg" ) );
76  static QPixmap sDeselectedIcon;
77  if ( sDeselectedIcon.isNull() )
78  sDeselectedIcon = QgsApplication::getThemePixmap( QStringLiteral( "/mIconDeselected.svg" ) );
79 
80  QString text = index.model()->data( index, Qt::EditRole ).toString();
81  QgsFeatureListModel::FeatureInfo featInfo = index.model()->data( index, Qt::UserRole ).value<QgsFeatureListModel::FeatureInfo>();
82 
83  bool isEditSelection = mEditSelectionModel && mEditSelectionModel->isSelected( mListModel->mapToMaster( index ) );
84 
85  // Icon layout options
86  QStyleOptionViewItem iconOption;
87 
88  QRect iconLayoutBounds( option.rect.x(), option.rect.y(), option.rect.height(), option.rect.height() );
89 
90  QPixmap icon = mFeatureSelectionModel->isSelected( index ) ? sSelectedIcon : sDeselectedIcon;
91 
92  // Scale up the icon if needed
93  if ( option.rect.height() > ICON_SIZE )
94  {
95  icon = icon.scaledToHeight( option.rect.height(), Qt::SmoothTransformation );
96  }
97 
98  // Text layout options
99  QRect textLayoutBounds( iconLayoutBounds.x() + iconLayoutBounds.width(), option.rect.y(), option.rect.width() - ( iconLayoutBounds.x() + iconLayoutBounds.width() ), option.rect.height() );
100 
101  QStyleOptionViewItem textOption = option;
102  textOption.state |= QStyle::State_Enabled;
103  if ( isEditSelection )
104  {
105  textOption.state |= QStyle::State_Selected;
106  }
107 
108  if ( featInfo.isNew )
109  {
110  textOption.font.setStyle( QFont::StyleItalic );
111  textOption.palette.setColor( QPalette::Text, Qt::darkGreen );
112  textOption.palette.setColor( QPalette::HighlightedText, Qt::darkGreen );
113  }
114  else if ( featInfo.isEdited || ( mCurrentFeatureEdited && isEditSelection ) )
115  {
116  textOption.font.setStyle( QFont::StyleItalic );
117  textOption.palette.setColor( QPalette::Text, Qt::red );
118  textOption.palette.setColor( QPalette::HighlightedText, Qt::red );
119  }
120 
121  drawDisplay( painter, textOption, textLayoutBounds, text );
122  drawDecoration( painter, iconOption, iconLayoutBounds, icon );
123 }
virtual bool isSelected(QgsFeatureId fid)
Returns the selection status of a given feature id.
static QPixmap getThemePixmap(const QString &name)
Helper to get a theme icon as a pixmap.
virtual QModelIndex mapToMaster(const QModelIndex &proxyIndex) const
bool isEdited
True if feature has been edited.
void setFeatureSelectionModel(QgsFeatureSelectionModel *featureSelectionModel)
QSize sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const override
void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const override
QgsFeatureListViewDelegate(QgsFeatureListModel *listModel, QObject *parent=nullptr)
bool isNew
True if feature is a newly added feature.
void setEditSelectionModel(QItemSelectionModel *editSelectionModel)