QGIS API Documentation  2.2.0-Valmiera
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups 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  , mListModel( listModel )
20 {
21 }
22 
24 {
25  if ( pos.x() > sIconSize )
26  {
27  return EditElement;
28  }
29  else
30  {
31  return SelectionElement;
32  }
33 }
34 
36 {
37  mFeatureSelectionModel = featureSelectionModel;
38 }
39 
40 void QgsFeatureListViewDelegate::setEditSelectionModel( QItemSelectionModel* editSelectionModel )
41 {
42  mEditSelectionModel = editSelectionModel;
43 }
44 
45 QSize QgsFeatureListViewDelegate::sizeHint( const QStyleOptionViewItem& option, const QModelIndex& index ) const
46 {
47  Q_UNUSED( index )
48  return QSize( option.rect.width(), sIconSize );
49 }
50 
51 void QgsFeatureListViewDelegate::paint( QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index ) const
52 {
53  QString text = index.model()->data( index, Qt::EditRole ).toString();
54  QgsFeatureListModel::FeatureInfo featInfo = index.model()->data( index, Qt::UserRole ).value<QgsFeatureListModel::FeatureInfo>();
55  bool isEdited = mEditSelectionModel->isSelected( mListModel->mapToMaster( index ) );
56 
57  // Icon layout options
58  QStyleOptionViewItem iconOption;
59 
60  QRect iconLayoutBounds( option.rect.x(), option.rect.y(), option.rect.height(), option.rect.height() );
61 
62  QPixmap icon;
63 
64  if ( mFeatureSelectionModel->isSelected( index ) )
65  {
66  // Item is selected
67  icon = QgsApplication::getThemePixmap( "/mIconSelected.svg" );
68  }
69  else
70  {
71  icon = QgsApplication::getThemePixmap( "/mIconDeselected.svg" );
72  }
73 
74  // Text layout options
75  QRect textLayoutBounds( iconLayoutBounds.x() + iconLayoutBounds.width(), option.rect.y(), option.rect.width() - ( iconLayoutBounds.x() + iconLayoutBounds.width() ), option.rect.height() );
76 
77  QStyleOptionViewItem textOption;
78  textOption.state |= QStyle::State_Enabled;
79  if ( isEdited )
80  {
81  textOption.state |= QStyle::State_Selected;
82  }
83 
84  if ( featInfo.isNew )
85  {
86  textOption.font.setStyle( QFont::StyleItalic );
87  textOption.palette.setColor( QPalette::Text, Qt::darkGreen );
88  textOption.palette.setColor( QPalette::HighlightedText, Qt::darkGreen );
89  }
90  else if ( featInfo.isEdited || isEdited )
91  {
92  textOption.font.setStyle( QFont::StyleItalic );
93  textOption.palette.setColor( QPalette::Text, Qt::red );
94  textOption.palette.setColor( QPalette::HighlightedText, Qt::red );
95  }
96 
97  drawDisplay( painter, textOption, textLayoutBounds, text );
98  drawDecoration( painter, iconOption, iconLayoutBounds, icon );
99 }