QGIS API Documentation  3.16.0-Hannover (43b64b13f3)
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  const bool isEditSelection = mEditSelectionModel && mEditSelectionModel->isSelected( mListModel->mapToMaster( index ) );
74 
75  QStyleOptionViewItem textOption = option;
76  textOption.state |= QStyle::State_Enabled;
77  if ( isEditSelection )
78  {
79  textOption.state |= QStyle::State_Selected;
80  }
81  drawBackground( painter, textOption, index );
82 
83 
84  static QPixmap sSelectedIcon;
85  if ( sSelectedIcon.isNull() )
86  sSelectedIcon = QgsApplication::getThemePixmap( QStringLiteral( "/mIconSelected.svg" ) );
87  static QPixmap sDeselectedIcon;
88  if ( sDeselectedIcon.isNull() )
89  sDeselectedIcon = QgsApplication::getThemePixmap( QStringLiteral( "/mIconDeselected.svg" ) );
90 
91  QString text = index.model()->data( index, Qt::EditRole ).toString();
92  const QgsFeatureListModel::FeatureInfo featInfo = index.model()->data( index, QgsFeatureListModel::Role::FeatureInfoRole ).value<QgsFeatureListModel::FeatureInfo>();
93 
94  // Icon layout options
95  QStyleOptionViewItem iconOption;
96 
97  QRect iconLayoutBounds( option.rect.x(), option.rect.y(), option.rect.height(), option.rect.height() );
98 
99  QPixmap icon = mFeatureSelectionModel->isSelected( index ) ? sSelectedIcon : sDeselectedIcon;
100 
101  // Scale up the icon if needed
102  if ( option.rect.height() > ICON_SIZE )
103  {
104  icon = icon.scaledToHeight( option.rect.height(), Qt::SmoothTransformation );
105  }
106 
107  drawDecoration( painter, iconOption, iconLayoutBounds, icon );
108 
109  // if conditional style also has an icon, draw that too
110  const QVariant conditionalIcon = index.model()->data( index, Qt::DecorationRole );
111  if ( conditionalIcon.isValid() )
112  {
113  const QPixmap pixmap = conditionalIcon.value< QPixmap >();
114 #if QT_VERSION < QT_VERSION_CHECK(5, 11, 0)
115  iconLayoutBounds.moveLeft( iconLayoutBounds.x() + icon.width() + QFontMetrics( textOption.font ).width( 'X' ) );
116 #else
117  iconLayoutBounds.moveLeft( iconLayoutBounds.x() + icon.width() + QFontMetrics( textOption.font ).horizontalAdvance( 'X' ) );
118 #endif
119  iconLayoutBounds.setTop( option.rect.y() + ( option.rect.height() - pixmap.height() ) / 2.0 );
120  iconLayoutBounds.setHeight( pixmap.height() );
121  drawDecoration( painter, iconOption, iconLayoutBounds, pixmap );
122  }
123 
124  // Text layout options
125  QRect textLayoutBounds( iconLayoutBounds.x() + iconLayoutBounds.width(), option.rect.y(), option.rect.width() - ( iconLayoutBounds.x() + iconLayoutBounds.width() ), option.rect.height() );
126 
127  // start with font and foreground color from model's FontRole
128  const QVariant font = index.model()->data( index, Qt::FontRole );
129  if ( font.isValid() )
130  {
131  textOption.font = font.value< QFont >();
132  }
133 #if QT_VERSION < QT_VERSION_CHECK(5, 13, 0)
134  const QVariant textColor = index.model()->data( index, Qt::TextColorRole );
135 #else
136  const QVariant textColor = index.model()->data( index, Qt::ForegroundRole );
137 #endif
138  if ( textColor.isValid() )
139  {
140  textOption.palette.setColor( QPalette::Text, textColor.value< QColor >() );
141  }
142 
143  if ( featInfo.isNew )
144  {
145  textOption.font.setStyle( QFont::StyleItalic );
146  textOption.palette.setColor( QPalette::Text, Qt::darkGreen );
147  textOption.palette.setColor( QPalette::HighlightedText, Qt::darkGreen );
148  }
149  else if ( featInfo.isEdited || ( mCurrentFeatureEdited && isEditSelection ) )
150  {
151  textOption.font.setStyle( QFont::StyleItalic );
152  textOption.palette.setColor( QPalette::Text, Qt::red );
153  textOption.palette.setColor( QPalette::HighlightedText, Qt::red );
154  }
155 
156  drawDisplay( painter, textOption, textLayoutBounds, text );
157 }
QgsFeatureListViewDelegate::setFeatureSelectionModel
void setFeatureSelectionModel(QgsFeatureSelectionModel *featureSelectionModel)
Definition: qgsfeaturelistviewdelegate.cpp:49
QgsFeatureSelectionModel
Definition: qgsfeatureselectionmodel.h:32
QgsFeatureSelectionModel::isSelected
virtual bool isSelected(QgsFeatureId fid)
Returns the selection status of a given feature id.
Definition: qgsfeatureselectionmodel.cpp:53
QgsFeatureListModel::mapToMaster
virtual QModelIndex mapToMaster(const QModelIndex &proxyIndex) const
Definition: qgsfeaturelistmodel.cpp:310
qgsfeaturelistmodel.h
QgsFeatureListViewDelegate::QgsFeatureListViewDelegate
QgsFeatureListViewDelegate(QgsFeatureListModel *listModel, QObject *parent=nullptr)
Definition: qgsfeaturelistviewdelegate.cpp:30
QgsFeatureListViewDelegate::setCurrentFeatureEdited
void setCurrentFeatureEdited(bool state)
Definition: qgsfeaturelistviewdelegate.cpp:54
QgsApplication::getThemePixmap
static QPixmap getThemePixmap(const QString &name, const QColor &foreColor=QColor(), const QColor &backColor=QColor(), int size=16)
Helper to get a theme icon as a pixmap.
Definition: qgsapplication.cpp:721
qgsapplication.h
QgsFeatureListModel::FeatureInfo::isEdited
bool isEdited
True if feature has been edited.
Definition: qgsfeaturelistmodel.h:56
QgsFeatureListViewDelegate::paint
void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const override
Definition: qgsfeaturelistviewdelegate.cpp:71
QgsFeatureListViewDelegate::SelectionElement
@ SelectionElement
Definition: qgsfeaturelistviewdelegate.h:41
QgsFeatureListModel::FeatureInfo::isNew
bool isNew
True if feature is a newly added feature.
Definition: qgsfeaturelistmodel.h:53
QgsFeatureListViewDelegate::ICON_SIZE
static const int ICON_SIZE
Definition: qgsfeaturelistviewdelegate.h:36
qgsfeaturelistviewdelegate.h
qgsattributetablemodel.h
QgsFeatureListModel::FeatureInfo
Definition: qgsfeaturelistmodel.h:44
QgsFeatureListViewDelegate::positionToElement
Element positionToElement(QPoint pos)
Definition: qgsfeaturelistviewdelegate.cpp:37
qgsvectorlayer.h
qgsfeatureselectionmodel.h
QgsFeatureListViewDelegate::setEditSelectionModel
void setEditSelectionModel(QItemSelectionModel *editSelectionModel)
Definition: qgsfeaturelistviewdelegate.cpp:59
QgsFeatureListViewDelegate::sizeHint
QSize sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const override
Definition: qgsfeaturelistviewdelegate.cpp:64
QgsFeatureListModel
Definition: qgsfeaturelistmodel.h:39
QgsFeatureListViewDelegate::Element
Element
Definition: qgsfeaturelistviewdelegate.h:39
QgsFeatureListViewDelegate::EditElement
@ EditElement
Definition: qgsfeaturelistviewdelegate.h:40
qgsvectorlayereditbuffer.h