Skip to content

Commit 5c3890b

Browse files
author
timlinux
committedMay 4, 2009
Added missing files
git-svn-id: http://svn.osgeo.org/qgis/trunk@10726 c8812cc2-4d05-0410-92ff-de0c093fc19c
1 parent 921321e commit 5c3890b

8 files changed

+511
-0
lines changed
 
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
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+
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/***************************************************************************
2+
QgsAttributeTableDelegate.h
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+
#ifndef QGSATTRIBUTETABLEDELEGATE_H
17+
#define QGSATTRIBUTETABLEDELEGATE_H
18+
19+
#include <QItemDelegate>
20+
class QPainter;
21+
/** \ingroup app
22+
* A delegate item class for QgsAttributeTable (see Qt documentation for
23+
* QItemDelegate).
24+
*/
25+
26+
class QgsAttributeTableDelegate : public QItemDelegate
27+
{
28+
Q_OBJECT;
29+
public:
30+
/** Constructor */
31+
QgsAttributeTableDelegate( QObject* parent = NULL ) :
32+
QItemDelegate( parent ) {};
33+
/** Used to create an editor for when the user tries to
34+
* change the contents of a cell */
35+
QWidget * createEditor(
36+
QWidget *parent,
37+
const QStyleOptionViewItem &option,
38+
const QModelIndex &index ) const;
39+
/** Overloads the paint method form the QItemDelegate bas class */
40+
void paint(
41+
QPainter * painter,
42+
const QStyleOptionViewItem & option,
43+
const QModelIndex & index ) const;
44+
};
45+
46+
#endif //QGSATTRIBUTETABLEDELEGATE_H
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/***************************************************************************
2+
QgsAttributeTableFilterModel.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 "qgsattributetablefiltermodel.h"
17+
#include "qgsattributetablemodel.h"
18+
#include "qgsvectorlayer.h"
19+
20+
//////////////////
21+
// Filter Model //
22+
//////////////////
23+
24+
void QgsAttributeTableFilterModel::sort( int column, Qt::SortOrder order )
25+
{
26+
(( QgsAttributeTableModel * )sourceModel() )->sort( column, order );
27+
}
28+
29+
QgsAttributeTableFilterModel::QgsAttributeTableFilterModel( QgsVectorLayer* theLayer )
30+
{
31+
mLayer = theLayer;
32+
mHideUnselected = false;
33+
setDynamicSortFilter( true );
34+
}
35+
36+
bool QgsAttributeTableFilterModel::filterAcceptsRow( int sourceRow, const QModelIndex &sourceParent ) const
37+
{
38+
if ( mHideUnselected )
39+
// unreadable? yes, i agree :-)
40+
return mLayer->selectedFeaturesIds().contains((( QgsAttributeTableModel * )sourceModel() )->rowToId( sourceRow ) );
41+
42+
return true;
43+
}
44+
45+
/*
46+
QModelIndex QgsAttributeTableFilterModel::mapFromSource ( const QModelIndex& sourceIndex ) const
47+
{
48+
return sourceIndex;
49+
}
50+
51+
QModelIndex QgsAttributeTableFilterModel::mapToSource ( const QModelIndex& filterIndex ) const
52+
{
53+
return filterIndex;
54+
}
55+
*/
56+
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/***************************************************************************
2+
QgsAttributeTableFilterModel.h - Filter Model for attribute table
3+
-------------------
4+
date : Feb 2009
5+
copyright : Vita Cizek
6+
email : weetya (at) gmail.com
7+
8+
***************************************************************************
9+
* *
10+
* This program is free software; you can redistribute it and/or modify *
11+
* it under the terms of the GNU General Public License as published by *
12+
* the Free Software Foundation; either version 2 of the License, or *
13+
* (at your option) any later version. *
14+
* *
15+
***************************************************************************/
16+
17+
#ifndef QGSATTRIBUTETABLEFILTERMODEL_H
18+
#define QGSATTRIBUTETABLEFILTERMODEL_H
19+
20+
#include <QSortFilterProxyModel>
21+
#include <QModelIndex>
22+
23+
//QGIS Includes
24+
#include "qgsvectorlayer.h" //QgsAttributeList
25+
26+
class QgsAttributeTableFilterModel: public QSortFilterProxyModel
27+
{
28+
public:
29+
QgsAttributeTableFilterModel( QgsVectorLayer* theLayer );
30+
//QModelIndex mapToSource ( const QModelIndex & filterIndex ) const;
31+
//QModelIndex mapFromSource ( const QModelIndex & sourceIndex ) const;
32+
bool mHideUnselected;
33+
virtual void sort( int column, Qt::SortOrder order = Qt::AscendingOrder );
34+
protected:
35+
bool filterAcceptsRow( int sourceRow, const QModelIndex &sourceParent ) const;
36+
private:
37+
QgsVectorLayer* mLayer;
38+
};
39+
40+
#endif
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/***************************************************************************
2+
QgsAttributeTableIdColumnPair.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 "qgsattributetableidcolumnpair.h"
17+
#include "qgsfield.h"
18+
19+
#include <QVariant>
20+
21+
//could be faster when type guessed before sorting
22+
bool QgsAttributeTableIdColumnPair::operator<( const QgsAttributeTableIdColumnPair &b ) const
23+
{
24+
//QVariant thinks gid is a string!
25+
QVariant::Type columnType = columnItem.type();
26+
27+
if ( columnType == QVariant::Int || columnType == QVariant::UInt || columnType == QVariant::LongLong || columnType == QVariant::ULongLong )
28+
return columnItem.toLongLong() < b.columnItem.toLongLong();
29+
30+
if ( columnType == QVariant::Double )
31+
return columnItem.toDouble() < b.columnItem.toDouble();
32+
33+
return columnItem.toString() < b.columnItem.toString();
34+
}
35+
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/***************************************************************************
2+
QgsAttributeTableIdColumnPair.h - Helper class for attribute tables
3+
-------------------
4+
date : Feb 2009
5+
copyright : Vita Cizek
6+
email : weetya (at) gmail.com
7+
8+
***************************************************************************
9+
* *
10+
* This program is free software; you can redistribute it and/or modify *
11+
* it under the terms of the GNU General Public License as published by *
12+
* the Free Software Foundation; either version 2 of the License, or *
13+
* (at your option) any later version. *
14+
* *
15+
***************************************************************************/
16+
17+
#ifndef QGSATTRIBUTETABKEIDCOLUMNPAIR_H
18+
#define QGSATTRIBUTETABKEIDCOLUMNPAIR_H
19+
20+
#include <QVariant>
21+
22+
class QgsAttributeTableIdColumnPair
23+
{
24+
public:
25+
int id;
26+
QVariant columnItem;
27+
28+
bool operator<( const QgsAttributeTableIdColumnPair &b ) const;
29+
};
30+
31+
#endif
Lines changed: 177 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,177 @@
1+
/***************************************************************************
2+
QgsAttributeTableMemoryModel.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 "qgsattributetablememorymodel.h"
17+
#include "qgsattributetablefiltermodel.h"
18+
19+
#include "qgsfield.h"
20+
#include "qgsvectorlayer.h"
21+
#include "qgslogger.h"
22+
23+
#include <QtGui>
24+
#include <QVariant>
25+
26+
/////////////////////
27+
// In-Memory model //
28+
/////////////////////
29+
30+
void QgsAttributeTableMemoryModel::loadLayer()
31+
{
32+
QgsAttributeTableModel::loadLayer();
33+
mLayer->select( mLayer->pendingAllAttributesList(), QgsRectangle(), false );
34+
35+
QgsFeature f;
36+
while ( mLayer->nextFeature( f ) )
37+
mFeatureMap.insert( f.id(), f );
38+
}
39+
40+
QgsAttributeTableMemoryModel::QgsAttributeTableMemoryModel
41+
( QgsVectorLayer *theLayer )
42+
: QgsAttributeTableModel( theLayer )
43+
{
44+
loadLayer();
45+
}
46+
47+
QVariant QgsAttributeTableMemoryModel::data( const QModelIndex &index, int role ) const
48+
{
49+
if ( !index.isValid() || ( role != Qt::TextAlignmentRole && role != Qt::DisplayRole && role != Qt::EditRole ) )
50+
return QVariant();
51+
52+
QVariant::Type fldType = mLayer->pendingFields()[ mAttributes[index.column()] ].type();
53+
bool fldNumeric = ( fldType == QVariant::Int || fldType == QVariant::Double );
54+
55+
if ( role == Qt::TextAlignmentRole )
56+
{
57+
if ( fldNumeric )
58+
return QVariant( Qt::AlignRight );
59+
else
60+
return QVariant( Qt::AlignLeft );
61+
}
62+
63+
// if we don't have the row in current cache, load it from layer first
64+
if ( mLastRowId != rowToId( index.row() ) )
65+
{
66+
//bool res = mLayer->featureAtId(rowToId(index.row()), mFeat, false, true);
67+
bool res = mFeatureMap.contains( rowToId( index.row() ) );
68+
69+
if ( !res )
70+
return QVariant( "ERROR" );
71+
72+
mLastRowId = rowToId( index.row() );
73+
mFeat = mFeatureMap[rowToId( index.row() )];
74+
mLastRow = ( QgsAttributeMap * ) & mFeat.attributeMap();
75+
}
76+
77+
if ( !mLastRow )
78+
return QVariant( "ERROR" );
79+
80+
QVariant &val = ( *mLastRow )[ mAttributes[index.column()] ];
81+
82+
if ( val.isNull() )
83+
{
84+
// if the value is NULL, show that in table, but don't show "NULL" text in editor
85+
if ( role == Qt::EditRole )
86+
return QVariant();
87+
else
88+
return QVariant( "NULL" );
89+
}
90+
91+
// force also numeric data for EditRole to be strings
92+
// otherwise it creates spinboxes instead of line edits
93+
// (probably not what we do want)
94+
if ( fldNumeric && role == Qt::EditRole )
95+
return val.toString();
96+
97+
// convert to QString from some other representation
98+
// this prevents displaying greater numbers in exponential format
99+
return val.toString();
100+
}
101+
102+
bool QgsAttributeTableMemoryModel::setData( const QModelIndex &index, const QVariant &value, int role )
103+
{
104+
if ( !index.isValid() || role != Qt::EditRole )
105+
return false;
106+
107+
if ( !mLayer->isEditable() )
108+
return false;
109+
110+
//bool res = mLayer->featureAtId(rowToId(index.row()), mFeat, false, true);
111+
bool res = mFeatureMap.contains( rowToId( index.row() ) );
112+
113+
if ( res )
114+
{
115+
mLastRowId = rowToId( index.row() );
116+
mFeat = mFeatureMap[rowToId( index.row() )];
117+
mLastRow = ( QgsAttributeMap * ) & mFeat.attributeMap();
118+
119+
120+
// QgsDebugMsg(mFeatureMap[rowToId(index.row())].id());
121+
mFeatureMap[rowToId( index.row() )].changeAttribute( index.column(), value );
122+
// propagate back to the layer
123+
mLayer->changeAttributeValue( rowToId( index.row() ), index.column(), value, true );
124+
}
125+
126+
if ( !mLayer->isModified() )
127+
return false;
128+
129+
emit dataChanged( index, index );
130+
return true;
131+
}
132+
133+
void QgsAttributeTableMemoryModel::featureDeleted( int fid )
134+
{
135+
QgsDebugMsg( "entered." );
136+
mFeatureMap.remove( fid );
137+
QgsAttributeTableModel::featureDeleted( fid );
138+
}
139+
140+
void QgsAttributeTableMemoryModel::featureAdded( int fid )
141+
{
142+
QgsDebugMsg( "entered." );
143+
QgsFeature f;
144+
mLayer->featureAtId( fid, f, false, true );
145+
mFeatureMap.insert( fid, f );
146+
QgsAttributeTableModel::featureAdded( fid );
147+
}
148+
149+
#if 0
150+
void QgsAttributeTableMemoryModel::attributeAdded( int idx )
151+
{
152+
QgsDebugMsg( "entered." );
153+
loadLayer();
154+
reload( index( 0, 0 ), index( rowCount(), columnCount() ) );
155+
}
156+
157+
void QgsAttributeTableMemoryModel::attributeDeleted( int idx )
158+
{
159+
QgsDebugMsg( "entered." );
160+
loadLayer();
161+
reload( index( 0, 0 ), index( rowCount(), columnCount() ) );
162+
}
163+
#endif
164+
165+
void QgsAttributeTableMemoryModel::layerDeleted()
166+
{
167+
QgsDebugMsg( "entered." );
168+
mFeatureMap.clear();
169+
QgsAttributeTableModel::layerDeleted();
170+
}
171+
172+
void QgsAttributeTableMemoryModel::attributeValueChanged( int fid, int idx, const QVariant &value )
173+
{
174+
QgsDebugMsg( "entered." );
175+
mFeatureMap[fid].changeAttribute( idx, value );
176+
reload( index( 0, 0 ), index( rowCount(), columnCount() ) );
177+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/***************************************************************************
2+
QgsAttributeTableMemoryModel.h - Memory Model for attribute table
3+
-------------------
4+
date : Feb 2009
5+
copyright : Vita Cizek
6+
email : weetya (at) gmail.com
7+
8+
***************************************************************************
9+
* *
10+
* This program is free software; you can redistribute it and/or modify *
11+
* it under the terms of the GNU General Public License as published by *
12+
* the Free Software Foundation; either version 2 of the License, or *
13+
* (at your option) any later version. *
14+
* *
15+
***************************************************************************/
16+
17+
#ifndef QGSATTRIBUTETABLEMEMORYMODEL_H
18+
#define QGSATTRIBUTETABLEMEMORYMODEL_H
19+
20+
#include <QAbstractTableModel>
21+
#include <QModelIndex>
22+
#include <QObject>
23+
24+
//QGIS Includes
25+
#include "qgsfeature.h" //QgsAttributeMap
26+
#include "qgsvectorlayer.h" //QgsAttributeList
27+
#include "qgsattributetablemodel.h"
28+
#include "qgsattributetableidcolumnpair.h"
29+
30+
class QgsAttributeTableMemoryModel: public QgsAttributeTableModel
31+
{
32+
Q_OBJECT;
33+
34+
public:
35+
QgsAttributeTableMemoryModel( QgsVectorLayer *theLayer );//, QObject *parent = 0);
36+
37+
protected slots:
38+
virtual void featureDeleted( int fid );
39+
virtual void featureAdded( int fid );
40+
virtual void layerDeleted();
41+
42+
private slots:
43+
//virtual void attributeAdded (int idx);
44+
//virtual void attributeDeleted (int idx);
45+
virtual void attributeValueChanged( int fid, int idx, const QVariant &value );
46+
//virtual void layerModified(bool onlyGeometry);
47+
48+
private:
49+
virtual QVariant data( const QModelIndex &index, int role ) const;
50+
virtual bool setData( const QModelIndex &index, const QVariant &value, int role );
51+
virtual void loadLayer();
52+
53+
QMap<int, QgsFeature> mFeatureMap;
54+
};
55+
56+
#endif //QGSATTRIBUTETABLEMEMORYMODEL_H

0 commit comments

Comments
 (0)
Please sign in to comment.