QGIS API Documentation  2.14.0-Essen
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  QString text = index.model()->data( index, Qt::EditRole ).toString();
62  QgsFeatureListModel::FeatureInfo featInfo = index.model()->data( index, Qt::UserRole ).value<QgsFeatureListModel::FeatureInfo>();
63 
64  bool isEditSelection = mEditSelectionModel && mEditSelectionModel->isSelected( mListModel->mapToMaster( index ) );
65 
66  // Icon layout options
67  QStyleOptionViewItem iconOption;
68 
69  QRect iconLayoutBounds( option.rect.x(), option.rect.y(), option.rect.height(), option.rect.height() );
70 
71  QPixmap icon;
72 
73  if ( mFeatureSelectionModel->isSelected( index ) )
74  {
75  // Item is selected
76  icon = QgsApplication::getThemePixmap( "/mIconSelected.svg" );
77  }
78  else
79  {
80  icon = QgsApplication::getThemePixmap( "/mIconDeselected.svg" );
81  }
82 
83  // Scale up the icon if needed
84  if ( option.rect.height() > sIconSize )
85  {
86  icon = icon.scaledToHeight( option.rect.height(), Qt::SmoothTransformation );
87  }
88 
89 
90  // Text layout options
91  QRect textLayoutBounds( iconLayoutBounds.x() + iconLayoutBounds.width(), option.rect.y(), option.rect.width() - ( iconLayoutBounds.x() + iconLayoutBounds.width() ), option.rect.height() );
92 
93  QStyleOptionViewItem textOption;
94  textOption.state |= QStyle::State_Enabled;
95  if ( isEditSelection )
96  {
97  textOption.state |= QStyle::State_Selected;
98  }
99 
100  if ( featInfo.isNew )
101  {
102  textOption.font.setStyle( QFont::StyleItalic );
103  textOption.palette.setColor( QPalette::Text, Qt::darkGreen );
104  textOption.palette.setColor( QPalette::HighlightedText, Qt::darkGreen );
105  }
106  else if ( featInfo.isEdited || ( mCurrentFeatureEdited && isEditSelection ) )
107  {
108  textOption.font.setStyle( QFont::StyleItalic );
109  textOption.palette.setColor( QPalette::Text, Qt::red );
110  textOption.palette.setColor( QPalette::HighlightedText, Qt::red );
111  }
112 
113  drawDisplay( painter, textOption, textLayoutBounds, text );
114  drawDecoration( painter, iconOption, iconLayoutBounds, icon );
115 }
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
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
const QAbstractItemModel * model() const
bool isSelected(const QModelIndex &index) const
void setEditSelectionModel(QItemSelectionModel *editSelectionModel)
QString toString() const