QGIS API Documentation  2.3.0-Master
 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 }
static unsigned index
bool fieldEditable(int idx)
is edit widget editable
Provides a table view of features of a QgsVectorLayer.
virtual bool isSelected(QgsFeatureId fid)
Returns the selection status of a given feature id.
static bool retrieveValue(QWidget *widget, QgsVectorLayer *vl, int idx, QVariant &value)
void beginEditCommand(QString text)
Create edit command for undo/redo operations.
QWidget * createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const
Used to create an editor for when the user tries to change the contents of a cell.
A model backed by a QgsVectorLayerCache which is able to provide feature/attribute information to a Q...
void setFeatureSelectionModel(QgsFeatureSelectionModel *featureSelectionModel)
QgsVectorLayer * layer() const
Returns the layer this filter acts on.
QgsVectorLayer * layer(const QAbstractItemModel *model) const
EditType editType(int idx)
get edit type
static bool setValue(QWidget *widget, QgsVectorLayer *vl, int idx, const QVariant &value)
Q_DECL_DEPRECATED bool changeAttributeValue(QgsFeatureId fid, int field, QVariant value, bool emitSignal)
Changes an attribute value (but does not commit it)
void endEditCommand()
Finish edit command and add it to undo/redo stack.
QgsFeatureSelectionModel * mFeatureSelectionModel
void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
Sets data from editor back to model.
void setEditorData(QWidget *editor, const QModelIndex &index) const
Sets data from model into the editor.
static QWidget * createAttributeEditor(QWidget *parent, QWidget *editor, QgsVectorLayer *vl, int idx, const QVariant &value, QMap< int, QWidget * > &proxyWidgets)
Creates or prepares a attribute editor widget.
QgsVectorLayer * layer() const
Returns the layer this model uses as backend.
void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
Overloads the paint method form the QItemDelegate bas class.
qint64 QgsFeatureId
Definition: qgsfeature.h:30
Represents a vector layer which manages a vector based data sets.
#define tr(sourceText)