QGIS API Documentation  2.5.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 
21 #include "qgsattributeeditor.h"
24 #include "qgsattributetablemodel.h"
25 #include "qgsattributetableview.h"
28 #include "qgslogger.h"
29 #include "qgsvectordataprovider.h"
30 
31 
32 
33 // TODO: Remove this casting orgy
34 
35 QgsVectorLayer *QgsAttributeTableDelegate::layer( const QAbstractItemModel *model ) const
36 {
37  const QgsAttributeTableModel *tm = qobject_cast<const QgsAttributeTableModel *>( model );
38  if ( tm )
39  return tm->layer();
40 
41  const QgsAttributeTableFilterModel *fm = dynamic_cast<const QgsAttributeTableFilterModel *>( model );
42  if ( fm )
43  return fm->layer();
44 
45  return NULL;
46 }
47 
49  QWidget *parent,
50  const QStyleOptionViewItem &option,
51  const QModelIndex &index ) const
52 {
53  Q_UNUSED( option );
54  QgsVectorLayer *vl = layer( index.model() );
55  if ( !vl )
56  return NULL;
57 
58  int fieldIdx = index.model()->data( index, QgsAttributeTableModel::FieldIndexRole ).toInt();
59 
60  QString widgetType = vl->editorWidgetV2( fieldIdx );
61  QgsEditorWidgetConfig cfg = vl->editorWidgetV2Config( fieldIdx );
62  QgsEditorWidgetWrapper* eww = QgsEditorWidgetRegistry::instance()->create( widgetType, vl, fieldIdx, cfg, 0, parent );
63  QWidget* w = eww->widget();
64 
65  w->setAutoFillBackground( true );
66 
67  eww->setEnabled( vl->fieldEditable( fieldIdx ) );
68 
69  return w;
70 }
71 
72 void QgsAttributeTableDelegate::setModelData( QWidget *editor, QAbstractItemModel *model, const QModelIndex &index ) const
73 {
74  QgsVectorLayer *vl = layer( model );
75  if ( vl == NULL )
76  return;
77 
78  int fieldIdx = model->data( index, QgsAttributeTableModel::FieldIndexRole ).toInt();
79  QgsFeatureId fid = model->data( index, QgsAttributeTableModel::FeatureIdRole ).toLongLong();
80  QVariant oldValue = model->data( index, Qt::EditRole );
81 
82  QVariant newValue;
84  if ( !eww )
85  return;
86 
87  newValue = eww->value();
88 
89  if ( oldValue != newValue && newValue.isValid() )
90  {
91  vl->beginEditCommand( tr( "Attribute changed" ) );
92  vl->changeAttributeValue( fid, fieldIdx, newValue, oldValue );
93  vl->endEditCommand();
94  }
95 }
96 
97 void QgsAttributeTableDelegate::setEditorData( QWidget *editor, const QModelIndex &index ) const
98 {
100  if ( !eww )
101  return;
102 
103  eww->setValue( index.model()->data( index, Qt::EditRole ) );
104 }
105 
107 {
108  mFeatureSelectionModel = featureSelectionModel;
109 }
110 
111 void QgsAttributeTableDelegate::paint( QPainter * painter,
112  const QStyleOptionViewItem & option,
113  const QModelIndex & index ) const
114 {
115  QgsFeatureId fid = index.model()->data( index, QgsAttributeTableModel::FeatureIdRole ).toLongLong();
116 
117  QStyleOptionViewItem myOpt = option;
118 
119  if ( index.model()->data( index, Qt::EditRole ).isNull() )
120  {
121  myOpt.font.setItalic( true );
122  myOpt.palette.setColor( QPalette::Text, QColor( "gray" ) );
123  }
124 
125  if ( mFeatureSelectionModel->isSelected( fid ) )
126  myOpt.state |= QStyle::State_Selected;
127 
128  QItemDelegate::paint( painter, myOpt, index );
129 
130  if ( option.state & QStyle::State_HasFocus )
131  {
132  QRect r = option.rect.adjusted( 1, 1, -1, -1 );
133  QPen p( QBrush( QColor( 0, 255, 127 ) ), 2 );
134  painter->save();
135  painter->setPen( p );
136  painter->drawRect( r );
137  painter->restore();
138  }
139 }
const QgsEditorWidgetConfig editorWidgetV2Config(int fieldIdx) const
Get the configuration for the editor widget used to represent the field at the given index...
static unsigned index
bool fieldEditable(int idx)
is edit widget editable
virtual bool isSelected(QgsFeatureId fid)
Returns the selection status of a given feature id.
Manages an editor widget Widget and wrapper share the same parent.
void beginEditCommand(QString text)
Create edit command for undo/redo operations.
virtual QVariant value()=0
Will be used to access the widget's value.
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...
static QgsEditorWidgetRegistry * instance()
This class is a singleton and has therefore to be accessed with this method instead of a constructor...
void setFeatureSelectionModel(QgsFeatureSelectionModel *featureSelectionModel)
QgsVectorLayer * layer() const
Returns the layer this filter acts on.
QgsVectorLayer * layer(const QAbstractItemModel *model) const
QgsEditorWidgetWrapper * create(const QString &widgetId, QgsVectorLayer *vl, int fieldIdx, const QgsEditorWidgetConfig &config, QWidget *editor, QWidget *parent, const QgsAttributeEditorContext context=QgsAttributeEditorContext())
Create an attribute editor widget wrapper of a given type for a given field.
const QString editorWidgetV2(int fieldIdx) const
Get the id for the editor widget used to represent the field at the given index.
static QgsEditorWidgetWrapper * fromWidget(QWidget *widget)
Will return a wrapper for a given widget.
Q_DECL_DEPRECATED bool changeAttributeValue(QgsFeatureId fid, int field, QVariant value, bool emitSignal)
Changes an attribute value (but does not commit it)
virtual void setValue(const QVariant &value)=0
Is called, when the value of the widget needs to be changed.
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.
void setEnabled(bool enabled)
Is used to enable or disable the edit functionality of the managed 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.
QMap< QString, QVariant > QgsEditorWidgetConfig
Holds a set of configuration parameters for a editor widget wrapper.
qint64 QgsFeatureId
Definition: qgsfeature.h:30
QWidget * widget()
Access the widget managed by this wrapper.
Represents a vector layer which manages a vector based data sets.
#define tr(sourceText)