QGIS API Documentation  2.18.21-Las Palmas (9fba24a)
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  , mFeatureSelectionModel( nullptr )
33  , mEditSelectionModel( nullptr )
34  , mListModel( listModel )
35  , mCurrentFeatureEdited( false )
36 {
37 }
38 
40 {
41  if ( pos.x() > sIconSize )
42  {
43  return EditElement;
44  }
45  else
46  {
47  return SelectionElement;
48  }
49 }
50 
52 {
53  mFeatureSelectionModel = featureSelectionModel;
54 }
55 
57 {
58  mCurrentFeatureEdited = state;
59 }
60 
62 {
63  mEditSelectionModel = editSelectionModel;
64 }
65 
67 {
68  Q_UNUSED( index )
69  int height = sIconSize;
70  return QSize( option.rect.width(), qMax( height, option.fontMetrics.height() ) );
71 }
72 
74 {
75  static QPixmap selectedIcon;
76  if ( selectedIcon.isNull() )
77  selectedIcon = QgsApplication::getThemePixmap( "/mIconSelected.svg" );
78  static QPixmap deselectedIcon;
79  if ( deselectedIcon.isNull() )
80  deselectedIcon = QgsApplication::getThemePixmap( "/mIconDeselected.svg" );
81 
82  QString text = index.model()->data( index, Qt::EditRole ).toString();
83  QgsFeatureListModel::FeatureInfo featInfo = index.model()->data( index, Qt::UserRole ).value<QgsFeatureListModel::FeatureInfo>();
84 
85  bool isEditSelection = mEditSelectionModel && mEditSelectionModel->isSelected( mListModel->mapToMaster( index ) );
86 
87  // Icon layout options
88  QStyleOptionViewItem iconOption;
89 
90  QRect iconLayoutBounds( option.rect.x(), option.rect.y(), option.rect.height(), option.rect.height() );
91 
92  QPixmap icon = mFeatureSelectionModel->isSelected( index ) ? selectedIcon : deselectedIcon;
93 
94  // Scale up the icon if needed
95  if ( option.rect.height() > sIconSize )
96  {
97  icon = icon.scaledToHeight( option.rect.height(), Qt::SmoothTransformation );
98  }
99 
100  // Text layout options
101  QRect textLayoutBounds( iconLayoutBounds.x() + iconLayoutBounds.width(), option.rect.y(), option.rect.width() - ( iconLayoutBounds.x() + iconLayoutBounds.width() ), option.rect.height() );
102 
103  QStyleOptionViewItem textOption;
104  textOption.state |= QStyle::State_Enabled;
105  if ( isEditSelection )
106  {
107  textOption.state |= QStyle::State_Selected;
108  }
109 
110  if ( featInfo.isNew )
111  {
112  textOption.font.setStyle( QFont::StyleItalic );
113  textOption.palette.setColor( QPalette::Text, Qt::darkGreen );
114  textOption.palette.setColor( QPalette::HighlightedText, Qt::darkGreen );
115  }
116  else if ( featInfo.isEdited || ( mCurrentFeatureEdited && isEditSelection ) )
117  {
118  textOption.font.setStyle( QFont::StyleItalic );
119  textOption.palette.setColor( QPalette::Text, Qt::red );
120  textOption.palette.setColor( QPalette::HighlightedText, Qt::red );
121  }
122 
123  drawDisplay( painter, textOption, textLayoutBounds, text );
124  drawDecoration( painter, iconOption, iconLayoutBounds, icon );
125 }
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
T value() const
static QPixmap getThemePixmap(const QString &theName)
Helper to get a theme icon as a pixmap.
virtual QModelIndex mapToMaster(const QModelIndex &proxyIndex) const
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