~ubuntu-branches/ubuntu/wily/qgis/wily

« back to all changes in this revision

Viewing changes to src/app/attributetable/qgsattributetabledelegate.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Johan Van de Wauw
  • Date: 2010-07-11 20:23:24 UTC
  • mfrom: (3.1.4 squeeze)
  • Revision ID: james.westby@ubuntu.com-20100711202324-5ktghxa7hracohmr
Tags: 1.4.0+12730-3ubuntu1
* Merge from Debian unstable (LP: #540941).
* Fix compilation issues with QT 4.7
* Add build-depends on libqt4-webkit-dev 

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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 "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"
 
28
 
 
29
QgsVectorLayer *QgsAttributeTableDelegate::layer( const QAbstractItemModel *model ) const
 
30
{
 
31
  const QgsAttributeTableModel *tm = qobject_cast<const QgsAttributeTableModel *>( model );
 
32
  if ( tm )
 
33
    return tm->layer();
 
34
 
 
35
  const QgsAttributeTableFilterModel *fm = dynamic_cast<const QgsAttributeTableFilterModel *>( model );
 
36
  if ( fm )
 
37
    return fm->layer();
 
38
 
 
39
  return NULL;
 
40
}
 
41
 
 
42
int QgsAttributeTableDelegate::fieldIdx( const QModelIndex &index ) const
 
43
{
 
44
  const QgsAttributeTableModel *tm = qobject_cast<const QgsAttributeTableModel *>( index.model() );
 
45
  if ( tm )
 
46
    return tm->fieldIdx( index.column() );
 
47
 
 
48
  const QgsAttributeTableFilterModel *fm = dynamic_cast<const QgsAttributeTableFilterModel *>( index.model() );
 
49
  if ( fm )
 
50
    return fm->tableModel()->fieldIdx( index.column() );
 
51
 
 
52
  return -1;
 
53
}
 
54
 
 
55
 
 
56
QWidget *QgsAttributeTableDelegate::createEditor(
 
57
  QWidget *parent,
 
58
  const QStyleOptionViewItem &option,
 
59
  const QModelIndex &index ) const
 
60
{
 
61
  QgsVectorLayer *vl = layer( index.model() );
 
62
  if ( !vl )
 
63
    return NULL;
 
64
 
 
65
  QWidget *w = QgsAttributeEditor::createAttributeEditor( parent, 0, vl, fieldIdx( index ), index.model()->data( index, Qt::EditRole ) );
 
66
 
 
67
  if ( parent )
 
68
  {
 
69
    QgsAttributeTableView *tv = dynamic_cast<QgsAttributeTableView *>( parent->parentWidget() );
 
70
    w->setMinimumWidth( tv->columnWidth( index.column() ) );
 
71
 
 
72
    if ( vl->editType( fieldIdx( index ) ) == QgsVectorLayer::FileName )
 
73
    {
 
74
      QLineEdit *le = w->findChild<QLineEdit*>();
 
75
      le->adjustSize();
 
76
      w->setMinimumHeight( le->height()*2 ); // FIXME: there must be a better way to do this
 
77
    }
 
78
  }
 
79
 
 
80
  return w;
 
81
}
 
82
 
 
83
void QgsAttributeTableDelegate::setModelData( QWidget *editor, QAbstractItemModel *model, const QModelIndex &index ) const
 
84
{
 
85
  QgsVectorLayer *vl = layer( model );
 
86
  if ( vl == NULL )
 
87
    return;
 
88
 
 
89
  QVariant value;
 
90
  if ( !QgsAttributeEditor::retrieveValue( editor, vl, fieldIdx( index ), value ) )
 
91
    return;
 
92
 
 
93
  model->setData( index, value );
 
94
}
 
95
 
 
96
void QgsAttributeTableDelegate::setEditorData( QWidget *editor, const QModelIndex &index ) const
 
97
{
 
98
  QgsVectorLayer *vl = layer( index.model() );
 
99
  if ( vl == NULL )
 
100
    return;
 
101
 
 
102
  QgsAttributeEditor::setValue( editor, vl, fieldIdx( index ), index.model()->data( index, Qt::EditRole ) );
 
103
}
 
104
 
 
105
void QgsAttributeTableDelegate::paint( QPainter * painter,
 
106
                                       const QStyleOptionViewItem & option,
 
107
                                       const QModelIndex & index ) const
 
108
{
 
109
  QItemDelegate::paint( painter, option, index );
 
110
 
 
111
  if ( option.state & QStyle::State_HasFocus )
 
112
  {
 
113
    QRect r = option.rect.adjusted( 1, 1, -1, -1 );
 
114
    QPen p( QBrush( QColor( 0, 255, 127 ) ), 2 );
 
115
    painter->save();
 
116
    painter->setPen( p );
 
117
    painter->drawRect( r );
 
118
    painter->restore();
 
119
  }
 
120
}