QGIS API Documentation  2.14.0-Essen
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 nullptr;
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 nullptr;
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 nullptr;
67 
68  int fieldIdx = index.model()->data( index, QgsAttributeTableModel::FieldIndexRole ).toInt();
69 
70  QString widgetType = vl->editFormConfig()->widgetType( fieldIdx );
71  QgsEditorWidgetConfig cfg = vl->editFormConfig()->widgetConfig( fieldIdx );
72  QgsAttributeEditorContext context( masterModel( index.model() )->editorContext(), QgsAttributeEditorContext::Popup );
73  QgsEditorWidgetWrapper* eww = QgsEditorWidgetRegistry::instance()->create( widgetType, vl, fieldIdx, cfg, nullptr, parent, context );
74  QWidget* w = eww->widget();
75 
76  w->setAutoFillBackground( true );
77 
78  eww->setEnabled( !vl->editFormConfig()->readOnly( fieldIdx ) );
79 
80  return w;
81 }
82 
84 {
85  QgsVectorLayer *vl = layer( model );
86  if ( !vl )
87  return;
88 
89  int fieldIdx = model->data( index, QgsAttributeTableModel::FieldIndexRole ).toInt();
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 
109 {
111  if ( !eww )
112  return;
113 
114  eww->setValue( index.model()->data( index, Qt::EditRole ) );
115 }
116 
118 {
119  mFeatureSelectionModel = featureSelectionModel;
120 }
121 
123  const QStyleOptionViewItem & option,
124  const QModelIndex & index ) const
125 {
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 }
qlonglong toLongLong(bool *ok) const
static unsigned index
virtual bool isSelected(QgsFeatureId fid)
Returns the selection status of a given feature id.
void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const override
Overloads the paint method form the QItemDelegate bas class.
void beginEditCommand(const QString &text)
Create edit command for undo/redo operations.
This class contains context information for attribute editor widgets.
Manages an editor widget Widget and wrapper share the same parent.
void save()
bool readOnly(int idx) const
This returns true if the field is manually set to read only or if the field does not support editing ...
virtual void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
QString widgetType(int fieldIdx) const
Get the id for the editor widget used to represent the field at the given index.
A model backed by a QgsVectorLayerCache which is able to provide feature/attribute information to a Q...
QgsEditFormConfig * editFormConfig() const
Get the configuration of the form used to represent this vector layer.
static QgsEditorWidgetRegistry * instance()
This class is a singleton and has therefore to be accessed with this method instead of a constructor...
QString tr(const char *sourceText, const char *disambiguation, int n)
void setFeatureSelectionModel(QgsFeatureSelectionModel *featureSelectionModel)
QWidget * createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const override
Used to create an editor for when the user tries to change the contents of a cell.
QgsVectorLayer * layer() const
Returns the layer this filter acts on.
void drawRect(const QRectF &rectangle)
A widget was opened as a popup (e.g. attribute table editor widget)
void setEditorData(QWidget *editor, const QModelIndex &index) const override
Sets data from model into the editor.
int toInt(bool *ok) const
bool isNull() const
void setPen(const QColor &color)
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.
static QgsEditorWidgetWrapper * fromWidget(QWidget *widget)
Will return a wrapper for a given widget.
QgsEditorWidgetConfig widgetConfig(int fieldIdx) const
Get the configuration for the editor widget used to represent the field at the given index...
virtual void setValue(const QVariant &value)=0
Is called, when the value of the widget needs to be changed.
virtual QVariant data(const QModelIndex &index, int role) const =0
Q_DECL_DEPRECATED bool changeAttributeValue(QgsFeatureId fid, int field, const 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.
void restore()
QgsAttributeTableModel * masterModel() const
Returns the table model this filter is using.
const QAbstractItemModel * model() const
void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const override
Sets data from editor back to model.
QgsVectorLayer * layer() const
Returns the layer this model uses as backend.
virtual QVariant value() const =0
Will be used to access the widget&#39;s value.
qint64 QgsFeatureId
Definition: qgsfeature.h:31
bool isValid() const
void setAutoFillBackground(bool enabled)
QObject * parent() const
Represents a vector layer which manages a vector based data sets.