QGIS API Documentation  2.0.1-Dufour
 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 
22 #include "qgsattributetableview.h"
23 #include "qgsattributetablemodel.h"
26 #include "qgsvectordataprovider.h"
27 #include "qgsattributeeditor.h"
28 #include "qgslogger.h"
29 
30 
31 
32 // TODO: Remove this casting orgy
33 
34 QgsVectorLayer *QgsAttributeTableDelegate::layer( const QAbstractItemModel *model ) const
35 {
36  const QgsAttributeTableModel *tm = qobject_cast<const QgsAttributeTableModel *>( model );
37  if ( tm )
38  return tm->layer();
39 
40  const QgsAttributeTableFilterModel *fm = dynamic_cast<const QgsAttributeTableFilterModel *>( model );
41  if ( fm )
42  return fm->layer();
43 
44  return NULL;
45 }
46 
48  QWidget *parent,
49  const QStyleOptionViewItem &option,
50  const QModelIndex &index ) const
51 {
52  Q_UNUSED( option );
53  QgsVectorLayer *vl = layer( index.model() );
54  if ( !vl )
55  return NULL;
56 
57  int fieldIdx = index.model()->data( index, QgsAttributeTableModel::FieldIndexRole ).toInt();
58 
59  QWidget *w = QgsAttributeEditor::createAttributeEditor( parent, 0, vl, fieldIdx, index.model()->data( index, Qt::EditRole ) );
60 
61  w->setAutoFillBackground( true );
62 
63  if ( parent )
64  {
65  QgsAttributeTableView *tv = dynamic_cast<QgsAttributeTableView *>( parent->parentWidget() );
66  w->setMinimumWidth( tv->columnWidth( index.column() ) );
67 
68  if ( vl->editType( fieldIdx ) == QgsVectorLayer::FileName ||
69  vl->editType( fieldIdx ) == QgsVectorLayer::Calendar )
70  {
71  QLineEdit *le = w->findChild<QLineEdit*>();
72  le->adjustSize();
73  w->setMinimumHeight( le->height()*2 ); // FIXME: there must be a better way to do this
74  }
75  }
76 
77  w->setEnabled( vl->fieldEditable( fieldIdx ) );
78 
79  return w;
80 }
81 
82 void QgsAttributeTableDelegate::setModelData( QWidget *editor, QAbstractItemModel *model, const QModelIndex &index ) const
83 {
84  QgsVectorLayer *vl = layer( model );
85  if ( vl == NULL )
86  return;
87 
88  int fieldIdx = model->data( index, QgsAttributeTableModel::FieldIndexRole ).toInt();
89  QgsFeatureId fid = model->data( index, QgsAttributeTableModel::FeatureIdRole ).toLongLong();
90 
91  QVariant value;
92  if ( !QgsAttributeEditor::retrieveValue( editor, vl, fieldIdx, value ) )
93  return;
94 
95  vl->beginEditCommand( tr( "Attribute changed" ) );
96  vl->changeAttributeValue( fid, fieldIdx, value, true );
97  vl->endEditCommand();
98 }
99 
100 void QgsAttributeTableDelegate::setEditorData( QWidget *editor, const QModelIndex &index ) const
101 {
102  QgsVectorLayer *vl = layer( index.model() );
103  if ( vl == NULL )
104  return;
105 
106  int fieldIdx = index.model()->data( index, QgsAttributeTableModel::FieldIndexRole ).toInt();
107  QgsAttributeEditor::setValue( editor, vl, fieldIdx, index.model()->data( index, Qt::EditRole ) );
108 }
109 
111 {
112  mFeatureSelectionModel = featureSelectionModel;
113 }
114 
115 void QgsAttributeTableDelegate::paint( QPainter * painter,
116  const QStyleOptionViewItem & option,
117  const QModelIndex & index ) const
118 {
119  QgsFeatureId fid = index.model()->data( index, QgsAttributeTableModel::FeatureIdRole ).toLongLong();
120 
121  QStyleOptionViewItem myOpt = option;
122 
123  if ( mFeatureSelectionModel->isSelected( fid ) )
124  myOpt.state |= QStyle::State_Selected;
125 
126  QItemDelegate::paint( painter, myOpt, index );
127 
128  if ( option.state & QStyle::State_HasFocus )
129  {
130  QRect r = option.rect.adjusted( 1, 1, -1, -1 );
131  QPen p( QBrush( QColor( 0, 255, 127 ) ), 2 );
132  painter->save();
133  painter->setPen( p );
134  painter->drawRect( r );
135  painter->restore();
136  }
137 }