QGIS API Documentation  2.4.0-Chugiak
 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  , mCurrentFeatureEdited( false )
21 {
22 }
23 
25 {
26  if ( pos.x() > sIconSize )
27  {
28  return EditElement;
29  }
30  else
31  {
32  return SelectionElement;
33  }
34 }
35 
37 {
38  mFeatureSelectionModel = featureSelectionModel;
39 }
40 
42 {
43  mCurrentFeatureEdited = state;
44 }
45 
46 void QgsFeatureListViewDelegate::setEditSelectionModel( QItemSelectionModel* editSelectionModel )
47 {
48  mEditSelectionModel = editSelectionModel;
49 }
50 
51 QSize QgsFeatureListViewDelegate::sizeHint( const QStyleOptionViewItem& option, const QModelIndex& index ) const
52 {
53  Q_UNUSED( index )
54  return QSize( option.rect.width(), sIconSize );
55 }
56 
57 void QgsFeatureListViewDelegate::paint( QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index ) const
58 {
59  QString text = index.model()->data( index, Qt::EditRole ).toString();
60  QgsFeatureListModel::FeatureInfo featInfo = index.model()->data( index, Qt::UserRole ).value<QgsFeatureListModel::FeatureInfo>();
61 
62  bool isEditSelection = mEditSelectionModel->isSelected( mListModel->mapToMaster( index ) );
63 
64  // Icon layout options
65  QStyleOptionViewItem iconOption;
66 
67  QRect iconLayoutBounds( option.rect.x(), option.rect.y(), option.rect.height(), option.rect.height() );
68 
69  QPixmap icon;
70 
71  if ( mFeatureSelectionModel->isSelected( index ) )
72  {
73  // Item is selected
74  icon = QgsApplication::getThemePixmap( "/mIconSelected.svg" );
75  }
76  else
77  {
78  icon = QgsApplication::getThemePixmap( "/mIconDeselected.svg" );
79  }
80 
81  // Text layout options
82  QRect textLayoutBounds( iconLayoutBounds.x() + iconLayoutBounds.width(), option.rect.y(), option.rect.width() - ( iconLayoutBounds.x() + iconLayoutBounds.width() ), option.rect.height() );
83 
84  QStyleOptionViewItem textOption;
85  textOption.state |= QStyle::State_Enabled;
86  if ( isEditSelection )
87  {
88  textOption.state |= QStyle::State_Selected;
89  }
90 
91  if ( featInfo.isNew )
92  {
93  textOption.font.setStyle( QFont::StyleItalic );
94  textOption.palette.setColor( QPalette::Text, Qt::darkGreen );
95  textOption.palette.setColor( QPalette::HighlightedText, Qt::darkGreen );
96  }
97  else if ( featInfo.isEdited || ( mCurrentFeatureEdited && isEditSelection ) )
98  {
99  textOption.font.setStyle( QFont::StyleItalic );
100  textOption.palette.setColor( QPalette::Text, Qt::red );
101  textOption.palette.setColor( QPalette::HighlightedText, Qt::red );
102  }
103 
104  drawDisplay( painter, textOption, textLayoutBounds, text );
105  drawDecoration( painter, iconOption, iconLayoutBounds, icon );
106 }
static unsigned index
virtual bool isSelected(QgsFeatureId fid)
Returns the selection status of a given feature id.
virtual QSize sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const
virtual QModelIndex mapToMaster(const QModelIndex &proxyIndex) const
static QIcon icon(QString icon)
static QPixmap getThemePixmap(const QString &theName)
Helper to get a theme icon as a pixmap.
Element positionToElement(const QPoint &pos)
bool mCurrentFeatureEdited
Set to true if the current edit selection has been edited.
void setFeatureSelectionModel(QgsFeatureSelectionModel *featureSelectionModel)
QgsFeatureSelectionModel * mFeatureSelectionModel
virtual void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
QgsFeatureListViewDelegate(QgsFeatureListModel *listModel, QObject *parent=0)
QItemSelectionModel * mEditSelectionModel
void setEditSelectionModel(QItemSelectionModel *editSelectionModel)