QGIS API Documentation  2.8.2-Wien
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
qgsattributetabledelegate.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  QgsAttributeTableDelegate.cpp
3  --------------------------------------
4  Date : Feb 2009
5  Copyright : (C) 2009 Vita Cizek
6  Email : weetya (at) gmail.com
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  ***************************************************************************/
15 
16 #include <QItemDelegate>
17 #include <QLineEdit>
18 #include <QComboBox>
19 #include <QPainter>
20 
21 #include "qgsattributeeditor.h"
24 #include "qgsattributetablemodel.h"
25 #include "qgsattributetableview.h"
28 #include "qgslogger.h"
29 #include "qgsvectordataprovider.h"
30 
31 
32 QgsVectorLayer* QgsAttributeTableDelegate::layer( const QAbstractItemModel *model )
33 {
34  const QgsAttributeTableModel *tm = qobject_cast<const QgsAttributeTableModel *>( model );
35  if ( tm )
36  return tm->layer();
37 
38  const QgsAttributeTableFilterModel *fm = dynamic_cast<const QgsAttributeTableFilterModel *>( model );
39  if ( fm )
40  return fm->layer();
41 
42  return 0;
43 }
44 
45 const QgsAttributeTableModel* QgsAttributeTableDelegate::masterModel( const QAbstractItemModel* model )
46 {
47  const QgsAttributeTableModel *tm = qobject_cast<const QgsAttributeTableModel *>( model );
48  if ( tm )
49  return tm;
50 
51  const QgsAttributeTableFilterModel *fm = dynamic_cast<const QgsAttributeTableFilterModel *>( model );
52  if ( fm )
53  return fm->masterModel();
54 
55  return 0;
56 }
57 
59  QWidget *parent,
60  const QStyleOptionViewItem &option,
61  const QModelIndex &index ) const
62 {
63  Q_UNUSED( option );
64  QgsVectorLayer *vl = layer( index.model() );
65  if ( !vl )
66  return NULL;
67 
68  int fieldIdx = index.model()->data( index, QgsAttributeTableModel::FieldIndexRole ).toInt();
69 
70  QString widgetType = vl->editorWidgetV2( fieldIdx );
71  QgsEditorWidgetConfig cfg = vl->editorWidgetV2Config( fieldIdx );
72  QgsAttributeEditorContext context( masterModel( index.model() )->editorContext(), QgsAttributeEditorContext::Popup );
73  QgsEditorWidgetWrapper* eww = QgsEditorWidgetRegistry::instance()->create( widgetType, vl, fieldIdx, cfg, 0, parent, context );
74  QWidget* w = eww->widget();
75 
76  w->setAutoFillBackground( true );
77 
78  eww->setEnabled( vl->fieldEditable( fieldIdx ) );
79 
80  return w;
81 }
82 
83 void QgsAttributeTableDelegate::setModelData( QWidget *editor, QAbstractItemModel *model, const QModelIndex &index ) const
84 {
85  QgsVectorLayer *vl = layer( model );
86  if ( vl == NULL )
87  return;
88 
89  int fieldIdx = model->data( index, QgsAttributeTableModel::FieldIndexRole ).toInt();
90  QgsFeatureId fid = model->data( index, QgsAttributeTableModel::FeatureIdRole ).toLongLong();
91  QVariant oldValue = model->data( index, Qt::EditRole );
92 
93  QVariant newValue;
95  if ( !eww )
96  return;
97 
98  newValue = eww->value();
99 
100  if (( oldValue != newValue && newValue.isValid() ) || oldValue.isNull() != newValue.isNull() )
101  {
102  vl->beginEditCommand( tr( "Attribute changed" ) );
103  vl->changeAttributeValue( fid, fieldIdx, newValue, oldValue );
104  vl->endEditCommand();
105  }
106 }
107 
108 void QgsAttributeTableDelegate::setEditorData( QWidget *editor, const QModelIndex &index ) const
109 {
111  if ( !eww )
112  return;
113 
114  eww->setValue( index.model()->data( index, Qt::EditRole ) );
115 }
116 
118 {
119  mFeatureSelectionModel = featureSelectionModel;
120 }
121 
122 void QgsAttributeTableDelegate::paint( QPainter * painter,
123  const QStyleOptionViewItem & option,
124  const QModelIndex & index ) const
125 {
126  QgsFeatureId fid = index.model()->data( index, QgsAttributeTableModel::FeatureIdRole ).toLongLong();
127 
128  QStyleOptionViewItem myOpt = option;
129 
130  if ( index.model()->data( index, Qt::EditRole ).isNull() )
131  {
132  myOpt.font.setItalic( true );
133  myOpt.palette.setColor( QPalette::Text, QColor( "gray" ) );
134  }
135 
136  if ( mFeatureSelectionModel && mFeatureSelectionModel->isSelected( fid ) )
137  myOpt.state |= QStyle::State_Selected;
138 
139  QItemDelegate::paint( painter, myOpt, index );
140 
141  if ( option.state & QStyle::State_HasFocus )
142  {
143  QRect r = option.rect.adjusted( 1, 1, -1, -1 );
144  QPen p( QBrush( QColor( 0, 255, 127 ) ), 2 );
145  painter->save();
146  painter->setPen( p );
147  painter->drawRect( r );
148  painter->restore();
149  }
150 }