QGIS API Documentation  2.11.0-Master
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
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( NULL )
19  , mEditSelectionModel( NULL )
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  return QSize( option.rect.width(), sIconSize );
56 }
57 
59 {
60  QString text = index.model()->data( index, Qt::EditRole ).toString();
61  QgsFeatureListModel::FeatureInfo featInfo = index.model()->data( index, Qt::UserRole ).value<QgsFeatureListModel::FeatureInfo>();
62 
63  bool isEditSelection = mEditSelectionModel && mEditSelectionModel->isSelected( mListModel->mapToMaster( index ) );
64 
65  // Icon layout options
66  QStyleOptionViewItem iconOption;
67 
68  QRect iconLayoutBounds( option.rect.x(), option.rect.y(), option.rect.height(), option.rect.height() );
69 
70  QPixmap icon;
71 
72  if ( mFeatureSelectionModel->isSelected( index ) )
73  {
74  // Item is selected
75  icon = QgsApplication::getThemePixmap( "/mIconSelected.svg" );
76  }
77  else
78  {
79  icon = QgsApplication::getThemePixmap( "/mIconDeselected.svg" );
80  }
81 
82  // Text layout options
83  QRect textLayoutBounds( iconLayoutBounds.x() + iconLayoutBounds.width(), option.rect.y(), option.rect.width() - ( iconLayoutBounds.x() + iconLayoutBounds.width() ), option.rect.height() );
84 
85  QStyleOptionViewItem textOption;
86  textOption.state |= QStyle::State_Enabled;
87  if ( isEditSelection )
88  {
89  textOption.state |= QStyle::State_Selected;
90  }
91 
92  if ( featInfo.isNew )
93  {
94  textOption.font.setStyle( QFont::StyleItalic );
95  textOption.palette.setColor( QPalette::Text, Qt::darkGreen );
96  textOption.palette.setColor( QPalette::HighlightedText, Qt::darkGreen );
97  }
98  else if ( featInfo.isEdited || ( mCurrentFeatureEdited && isEditSelection ) )
99  {
100  textOption.font.setStyle( QFont::StyleItalic );
101  textOption.palette.setColor( QPalette::Text, Qt::red );
102  textOption.palette.setColor( QPalette::HighlightedText, Qt::red );
103  }
104 
105  drawDisplay( painter, textOption, textLayoutBounds, text );
106  drawDecoration( painter, iconOption, iconLayoutBounds, icon );
107 }
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
Element positionToElement(const QPoint &pos)
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=0)
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