1
/***************************************************************************
2
QgsAttributeTableDelegate.cpp
3
--------------------------------------
5
Copyright : (C) 2009 Vita Cizek
6
Email : weetya (at) gmail.com
7
***************************************************************************
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. *
14
***************************************************************************/
16
#include <QItemDelegate>
21
#include "qgsattributetableview.h"
22
#include "qgsattributetablemodel.h"
23
#include "qgsattributetablefiltermodel.h"
24
#include "qgsattributetabledelegate.h"
25
#include "qgsvectordataprovider.h"
26
#include "qgsattributeeditor.h"
27
#include "qgslogger.h"
29
QgsVectorLayer *QgsAttributeTableDelegate::layer( const QAbstractItemModel *model ) const
31
const QgsAttributeTableModel *tm = qobject_cast<const QgsAttributeTableModel *>( model );
35
const QgsAttributeTableFilterModel *fm = dynamic_cast<const QgsAttributeTableFilterModel *>( model );
42
int QgsAttributeTableDelegate::fieldIdx( const QModelIndex &index ) const
44
const QgsAttributeTableModel *tm = qobject_cast<const QgsAttributeTableModel *>( index.model() );
46
return tm->fieldIdx( index.column() );
48
const QgsAttributeTableFilterModel *fm = dynamic_cast<const QgsAttributeTableFilterModel *>( index.model() );
50
return fm->tableModel()->fieldIdx( index.column() );
56
QWidget *QgsAttributeTableDelegate::createEditor(
58
const QStyleOptionViewItem &option,
59
const QModelIndex &index ) const
61
QgsVectorLayer *vl = layer( index.model() );
65
QWidget *w = QgsAttributeEditor::createAttributeEditor( parent, 0, vl, fieldIdx( index ), index.model()->data( index, Qt::EditRole ) );
69
QgsAttributeTableView *tv = dynamic_cast<QgsAttributeTableView *>( parent->parentWidget() );
70
w->setMinimumWidth( tv->columnWidth( index.column() ) );
72
if ( vl->editType( fieldIdx( index ) ) == QgsVectorLayer::FileName )
74
QLineEdit *le = w->findChild<QLineEdit*>();
76
w->setMinimumHeight( le->height()*2 ); // FIXME: there must be a better way to do this
83
void QgsAttributeTableDelegate::setModelData( QWidget *editor, QAbstractItemModel *model, const QModelIndex &index ) const
85
QgsVectorLayer *vl = layer( model );
90
if ( !QgsAttributeEditor::retrieveValue( editor, vl, fieldIdx( index ), value ) )
93
model->setData( index, value );
96
void QgsAttributeTableDelegate::setEditorData( QWidget *editor, const QModelIndex &index ) const
98
QgsVectorLayer *vl = layer( index.model() );
102
QgsAttributeEditor::setValue( editor, vl, fieldIdx( index ), index.model()->data( index, Qt::EditRole ) );
105
void QgsAttributeTableDelegate::paint( QPainter * painter,
106
const QStyleOptionViewItem & option,
107
const QModelIndex & index ) const
109
QItemDelegate::paint( painter, option, index );
111
if ( option.state & QStyle::State_HasFocus )
113
QRect r = option.rect.adjusted( 1, 1, -1, -1 );
114
QPen p( QBrush( QColor( 0, 255, 127 ) ), 2 );
116
painter->setPen( p );
117
painter->drawRect( r );