QGIS API Documentation  2.2.0-Valmiera
 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  QVariant oldValue = model->data( index, Qt::EditRole );
91 
92  QVariant newValue;
93  if ( !QgsAttributeEditor::retrieveValue( editor, vl, fieldIdx, newValue ) )
94  return;
95 
96  vl->beginEditCommand( tr( "Attribute changed" ) );
97  vl->changeAttributeValue( fid, fieldIdx, newValue, oldValue );
98  vl->endEditCommand();
99 }
100 
101 void QgsAttributeTableDelegate::setEditorData( QWidget *editor, const QModelIndex &index ) const
102 {
103  QgsVectorLayer *vl = layer( index.model() );
104  if ( vl == NULL )
105  return;
106 
107  int fieldIdx = index.model()->data( index, QgsAttributeTableModel::FieldIndexRole ).toInt();
108  QgsAttributeEditor::setValue( editor, vl, fieldIdx, index.model()->data( index, Qt::EditRole ) );
109 }
110 
112 {
113  mFeatureSelectionModel = featureSelectionModel;
114 }
115 
116 void QgsAttributeTableDelegate::paint( QPainter * painter,
117  const QStyleOptionViewItem & option,
118  const QModelIndex & index ) const
119 {
120  QgsFeatureId fid = index.model()->data( index, QgsAttributeTableModel::FeatureIdRole ).toLongLong();
121 
122  QStyleOptionViewItem myOpt = option;
123 
124  if ( mFeatureSelectionModel->isSelected( fid ) )
125  myOpt.state |= QStyle::State_Selected;
126 
127  QItemDelegate::paint( painter, myOpt, index );
128 
129  if ( option.state & QStyle::State_HasFocus )
130  {
131  QRect r = option.rect.adjusted( 1, 1, -1, -1 );
132  QPen p( QBrush( QColor( 0, 255, 127 ) ), 2 );
133  painter->save();
134  painter->setPen( p );
135  painter->drawRect( r );
136  painter->restore();
137  }
138 }