Quantum GIS API Documentation  master-693a1fe
src/gui/attributetable/qgsattributetabledelegate.cpp
Go to the documentation of this file.
00001 /***************************************************************************
00002      QgsAttributeTableDelegate.cpp
00003      --------------------------------------
00004     Date                 : Feb 2009
00005     Copyright            : (C) 2009 Vita Cizek
00006     Email                : weetya (at) gmail.com
00007  ***************************************************************************
00008  *                                                                         *
00009  *   This program is free software; you can redistribute it and/or modify  *
00010  *   it under the terms of the GNU General Public License as published by  *
00011  *   the Free Software Foundation; either version 2 of the License, or     *
00012  *   (at your option) any later version.                                   *
00013  *                                                                         *
00014  ***************************************************************************/
00015 
00016 #include <QItemDelegate>
00017 #include <QLineEdit>
00018 #include <QComboBox>
00019 #include <QPainter>
00020 
00021 #include "qgsfeatureselectionmodel.h"
00022 #include "qgsattributetableview.h"
00023 #include "qgsattributetablemodel.h"
00024 #include "qgsattributetablefiltermodel.h"
00025 #include "qgsattributetabledelegate.h"
00026 #include "qgsvectordataprovider.h"
00027 #include "qgsattributeeditor.h"
00028 #include "qgslogger.h"
00029 
00030 
00031 
00032 // TODO: Remove this casting orgy
00033 
00034 QgsVectorLayer *QgsAttributeTableDelegate::layer( const QAbstractItemModel *model ) const
00035 {
00036   const QgsAttributeTableModel *tm = qobject_cast<const QgsAttributeTableModel *>( model );
00037   if ( tm )
00038     return tm->layer();
00039 
00040   const QgsAttributeTableFilterModel *fm = dynamic_cast<const QgsAttributeTableFilterModel *>( model );
00041   if ( fm )
00042     return fm->layer();
00043 
00044   return NULL;
00045 }
00046 
00047 QWidget *QgsAttributeTableDelegate::createEditor(
00048   QWidget *parent,
00049   const QStyleOptionViewItem &option,
00050   const QModelIndex &index ) const
00051 {
00052   Q_UNUSED( option );
00053   QgsVectorLayer *vl = layer( index.model() );
00054   if ( !vl )
00055     return NULL;
00056 
00057   int fieldIdx = index.model()->data( index, QgsAttributeTableModel::FieldIndexRole ).toInt();
00058 
00059   QWidget *w = QgsAttributeEditor::createAttributeEditor( parent, 0, vl, fieldIdx, index.model()->data( index, Qt::EditRole ) );
00060 
00061   w->setAutoFillBackground( true );
00062 
00063   if ( parent )
00064   {
00065     QgsAttributeTableView *tv = dynamic_cast<QgsAttributeTableView *>( parent->parentWidget() );
00066     w->setMinimumWidth( tv->columnWidth( index.column() ) );
00067 
00068     if ( vl->editType( fieldIdx ) == QgsVectorLayer::FileName ||
00069          vl->editType( fieldIdx ) == QgsVectorLayer::Calendar )
00070     {
00071       QLineEdit *le = w->findChild<QLineEdit*>();
00072       le->adjustSize();
00073       w->setMinimumHeight( le->height()*2 ); // FIXME: there must be a better way to do this
00074     }
00075   }
00076 
00077   return w;
00078 }
00079 
00080 void QgsAttributeTableDelegate::setModelData( QWidget *editor, QAbstractItemModel *model, const QModelIndex &index ) const
00081 {
00082   QgsVectorLayer *vl = layer( model );
00083   if ( vl == NULL )
00084     return;
00085 
00086   int fieldIdx = model->data( index, QgsAttributeTableModel::FieldIndexRole ).toInt();
00087   QgsFeatureId fid = model->data( index, QgsAttributeTableModel::FeatureIdRole ).toInt();
00088 
00089   QVariant value;
00090   if ( !QgsAttributeEditor::retrieveValue( editor, vl, fieldIdx, value ) )
00091     return;
00092 
00093   vl->beginEditCommand( tr( "Attribute changed" ) );
00094   vl->changeAttributeValue( fid, fieldIdx, value, true );
00095   vl->endEditCommand();
00096 }
00097 
00098 void QgsAttributeTableDelegate::setEditorData( QWidget *editor, const QModelIndex &index ) const
00099 {
00100   QgsVectorLayer *vl = layer( index.model() );
00101   if ( vl == NULL )
00102     return;
00103 
00104   int fieldIdx = index.model()->data( index, QgsAttributeTableModel::FieldIndexRole ).toInt();
00105   QgsAttributeEditor::setValue( editor, vl, fieldIdx, index.model()->data( index, Qt::EditRole ) );
00106 }
00107 
00108 void QgsAttributeTableDelegate::setFeatureSelectionModel( QgsFeatureSelectionModel *featureSelectionModel )
00109 {
00110   mFeatureSelectionModel = featureSelectionModel;
00111 }
00112 
00113 void QgsAttributeTableDelegate::paint( QPainter * painter,
00114                                        const QStyleOptionViewItem & option,
00115                                        const QModelIndex & index ) const
00116 {
00117   QgsFeatureId fid = index.model()->data( index, QgsAttributeTableModel::FeatureIdRole ).toInt();
00118 
00119   QStyleOptionViewItem myOpt = option;
00120 
00121   if ( mFeatureSelectionModel->isSelected( fid ) )
00122     myOpt.state |= QStyle::State_Selected;
00123 
00124   QItemDelegate::paint( painter, myOpt, index );
00125 
00126   if ( option.state & QStyle::State_HasFocus )
00127   {
00128     QRect r = option.rect.adjusted( 1, 1, -1, -1 );
00129     QPen p( QBrush( QColor( 0, 255, 127 ) ), 2 );
00130     painter->save();
00131     painter->setPen( p );
00132     painter->drawRect( r );
00133     painter->restore();
00134   }
00135 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Defines