|
| 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 <QPainter> |
| 19 | + |
| 20 | +#include "qgsattributetableview.h" |
| 21 | +#include "qgsattributetablemodel.h" |
| 22 | +#include "qgsattributetabledelegate.h" |
| 23 | +#include "qgsvectordataprovider.h" |
| 24 | + |
| 25 | +QWidget * QgsAttributeTableDelegate::createEditor( |
| 26 | + QWidget *parent, |
| 27 | + const QStyleOptionViewItem &option, |
| 28 | + const QModelIndex &index ) const |
| 29 | +{ |
| 30 | + QWidget *editor = QItemDelegate::createEditor( parent, option, index ); |
| 31 | + |
| 32 | + QLineEdit *le = dynamic_cast<QLineEdit*>( editor ); |
| 33 | + if ( !le ) return editor; |
| 34 | + |
| 35 | + const QgsAttributeTableModel* m = dynamic_cast<const QgsAttributeTableModel*>( index.model() ); |
| 36 | + if ( !m ) return editor; |
| 37 | + |
| 38 | + int col = index.column(); |
| 39 | + QVariant::Type type = m->layer()->dataProvider()->fields()[col].type(); |
| 40 | + |
| 41 | + if ( type == QVariant::Int ) |
| 42 | + { |
| 43 | + le->setValidator( new QIntValidator( le ) ); |
| 44 | + } |
| 45 | + else if ( type == QVariant::Double ) |
| 46 | + { |
| 47 | + le->setValidator( new QDoubleValidator( le ) ); |
| 48 | + } |
| 49 | + |
| 50 | + return editor; |
| 51 | +} |
| 52 | + |
| 53 | + |
| 54 | +void QgsAttributeTableDelegate::paint( QPainter * painter, |
| 55 | + const QStyleOptionViewItem & option, |
| 56 | + const QModelIndex & index ) const |
| 57 | +{ |
| 58 | + QItemDelegate::paint( painter, option, index ); |
| 59 | + |
| 60 | + if ( option.state & QStyle::State_HasFocus ) |
| 61 | + { |
| 62 | + QRect r = option.rect.adjusted( 1, 1, -1, -1 ); |
| 63 | + QPen p( QBrush( QColor( 0, 255, 127 ) ), 2 ); |
| 64 | + painter->save(); |
| 65 | + painter->setPen( p ); |
| 66 | + painter->drawRect( r ); |
| 67 | + painter->restore(); |
| 68 | + } |
| 69 | +} |
| 70 | + |
0 commit comments