Skip to content

Commit d97c101

Browse files
author
timlinux
committed
Refactoring to follow the 1 class :: 1 file rule. Also renamed mem model to memorymodel to comply with coding standards
git-svn-id: http://svn.osgeo.org/qgis/trunk/qgis@10718 c8812cc2-4d05-0410-92ff-de0c093fc19c
1 parent 1c864ca commit d97c101

6 files changed

+26
-325
lines changed

src/app/CMakeLists.txt

+7-1
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,11 @@ SET(QGIS_APP_SRCS
8888

8989
attributetable/qgsattributetabledialog.cpp
9090
attributetable/qgsattributetablemodel.cpp
91+
attributetable/qgsattributetablememorymodel.cpp
9192
attributetable/qgsattributetableview.cpp
93+
attributetable/qgsattributetablefiltermodel.cpp
94+
attributetable/qgsattributetableidcolumnpair.cpp
95+
attributetable/qgsattributetabledelegate.cpp
9296
)
9397

9498

@@ -157,8 +161,10 @@ SET (QGIS_APP_MOC_HDRS
157161
ogr/qgsopenvectorlayerdialog.h
158162
ogr/qgsnewogrconnection.h
159163

160-
attributetable/qgsattributetabledialog.h
161164
attributetable/qgsattributetablemodel.h
165+
attributetable/qgsattributetablememorymodel.h
166+
attributetable/qgsattributetabledialog.h
167+
attributetable/qgsattributetabledelegate.h
162168
)
163169

164170
IF (POSTGRES_FOUND)

src/app/attributetable/qgsattributetabledialog.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
#include "qgsattributetabledialog.h"
2020
#include "qgsattributetablemodel.h"
21+
#include "qgsattributetablefiltermodel.h"
2122
#include "qgsattributetableview.h"
2223

2324
#include <qgsapplication.h>

src/app/attributetable/qgsattributetablemodel.cpp

+5-211
Original file line numberDiff line numberDiff line change
@@ -14,68 +14,14 @@
1414
***************************************************************************/
1515

1616
#include "qgsattributetablemodel.h"
17-
//#include "qgsattributetableview.h"
17+
#include "qgsattributetablefiltermodel.h"
1818

19-
#include "qgsvectordataprovider.h"
2019
#include "qgsfield.h"
2120
#include "qgsvectorlayer.h"
21+
#include "qgslogger.h"
2222

2323
#include <QtGui>
2424
#include <QVariant>
25-
#include <QtAlgorithms>
26-
#include "qgslogger.h"
27-
28-
//could be faster when type guessed before sorting
29-
bool idColumnPair::operator<( const idColumnPair &b ) const
30-
{
31-
//QVariat thinks gid is a string!
32-
QVariant::Type columnType = columnItem.type();
33-
34-
if ( columnType == QVariant::Int || columnType == QVariant::UInt || columnType == QVariant::LongLong || columnType == QVariant::ULongLong )
35-
return columnItem.toLongLong() < b.columnItem.toLongLong();
36-
37-
if ( columnType == QVariant::Double )
38-
return columnItem.toDouble() < b.columnItem.toDouble();
39-
40-
return columnItem.toString() < b.columnItem.toString();
41-
}
42-
43-
//////////////////
44-
// Filter Model //
45-
//////////////////
46-
47-
void QgsAttributeTableFilterModel::sort( int column, Qt::SortOrder order )
48-
{
49-
(( QgsAttributeTableModel * )sourceModel() )->sort( column, order );
50-
}
51-
52-
QgsAttributeTableFilterModel::QgsAttributeTableFilterModel( QgsVectorLayer* theLayer )
53-
{
54-
mLayer = theLayer;
55-
mHideUnselected = false;
56-
setDynamicSortFilter( true );
57-
}
58-
59-
bool QgsAttributeTableFilterModel::filterAcceptsRow( int sourceRow, const QModelIndex &sourceParent ) const
60-
{
61-
if ( mHideUnselected )
62-
// unreadable? yes, i agree :-)
63-
return mLayer->selectedFeaturesIds().contains((( QgsAttributeTableModel * )sourceModel() )->rowToId( sourceRow ) );
64-
65-
return true;
66-
}
67-
68-
/*
69-
QModelIndex QgsAttributeTableFilterModel::mapFromSource ( const QModelIndex& sourceIndex ) const
70-
{
71-
return sourceIndex;
72-
}
73-
74-
QModelIndex QgsAttributeTableFilterModel::mapToSource ( const QModelIndex& filterIndex ) const
75-
{
76-
return filterIndex;
77-
}
78-
*/
7925

8026
////////////////////////////
8127
// QgsAttributeTableModel //
@@ -336,7 +282,7 @@ QVariant QgsAttributeTableModel::headerData( int section, Qt::Orientation orient
336282
void QgsAttributeTableModel::sort( int column, Qt::SortOrder order )
337283
{
338284
QgsAttributeMap row;
339-
idColumnPair pair;
285+
QgsAttributeTableIdColumnPair pair;
340286
QgsAttributeList attrs;
341287
QgsFeature f;
342288

@@ -360,14 +306,14 @@ void QgsAttributeTableModel::sort( int column, Qt::SortOrder order )
360306
if ( order == Qt::AscendingOrder )
361307
qStableSort( mSortList.begin(), mSortList.end() );
362308
else
363-
qStableSort( mSortList.begin(), mSortList.end(), qGreater<idColumnPair>() );
309+
qStableSort( mSortList.begin(), mSortList.end(), qGreater<QgsAttributeTableIdColumnPair>() );
364310

365311
// recalculate id<->row maps
366312
mRowIdMap.clear();
367313
mIdRowMap.clear();
368314

369315
int i = 0;
370-
QList<idColumnPair>::Iterator it;
316+
QList<QgsAttributeTableIdColumnPair>::Iterator it;
371317
for ( it = mSortList.begin(); it != mSortList.end(); ++it, ++i )
372318
{
373319
mRowIdMap.insert( i, it->id );
@@ -491,155 +437,3 @@ void QgsAttributeTableModel::incomingChangeLayout()
491437
emit layoutAboutToBeChanged();
492438
}
493439

494-
/////////////////////
495-
// In-Memory model //
496-
/////////////////////
497-
498-
void QgsAttributeTableMemModel::loadLayer()
499-
{
500-
QgsAttributeTableModel::loadLayer();
501-
mLayer->select( mLayer->pendingAllAttributesList(), QgsRectangle(), false );
502-
503-
QgsFeature f;
504-
while ( mLayer->nextFeature( f ) )
505-
mFeatureMap.insert( f.id(), f );
506-
}
507-
508-
QgsAttributeTableMemModel::QgsAttributeTableMemModel
509-
( QgsVectorLayer *theLayer )
510-
: QgsAttributeTableModel( theLayer )
511-
{
512-
loadLayer();
513-
}
514-
515-
QVariant QgsAttributeTableMemModel::data( const QModelIndex &index, int role ) const
516-
{
517-
if ( !index.isValid() || ( role != Qt::TextAlignmentRole && role != Qt::DisplayRole && role != Qt::EditRole ) )
518-
return QVariant();
519-
520-
QVariant::Type fldType = mLayer->pendingFields()[ mAttributes[index.column()] ].type();
521-
bool fldNumeric = ( fldType == QVariant::Int || fldType == QVariant::Double );
522-
523-
if ( role == Qt::TextAlignmentRole )
524-
{
525-
if ( fldNumeric )
526-
return QVariant( Qt::AlignRight );
527-
else
528-
return QVariant( Qt::AlignLeft );
529-
}
530-
531-
// if we don't have the row in current cache, load it from layer first
532-
if ( mLastRowId != rowToId( index.row() ) )
533-
{
534-
//bool res = mLayer->featureAtId(rowToId(index.row()), mFeat, false, true);
535-
bool res = mFeatureMap.contains( rowToId( index.row() ) );
536-
537-
if ( !res )
538-
return QVariant( "ERROR" );
539-
540-
mLastRowId = rowToId( index.row() );
541-
mFeat = mFeatureMap[rowToId( index.row() )];
542-
mLastRow = ( QgsAttributeMap * ) & mFeat.attributeMap();
543-
}
544-
545-
if ( !mLastRow )
546-
return QVariant( "ERROR" );
547-
548-
QVariant &val = ( *mLastRow )[ mAttributes[index.column()] ];
549-
550-
if ( val.isNull() )
551-
{
552-
// if the value is NULL, show that in table, but don't show "NULL" text in editor
553-
if ( role == Qt::EditRole )
554-
return QVariant();
555-
else
556-
return QVariant( "NULL" );
557-
}
558-
559-
// force also numeric data for EditRole to be strings
560-
// otherwise it creates spinboxes instead of line edits
561-
// (probably not what we do want)
562-
if ( fldNumeric && role == Qt::EditRole )
563-
return val.toString();
564-
565-
// convert to QString from some other representation
566-
// this prevents displaying greater numbers in exponential format
567-
return val.toString();
568-
}
569-
570-
bool QgsAttributeTableMemModel::setData( const QModelIndex &index, const QVariant &value, int role )
571-
{
572-
if ( !index.isValid() || role != Qt::EditRole )
573-
return false;
574-
575-
if ( !mLayer->isEditable() )
576-
return false;
577-
578-
//bool res = mLayer->featureAtId(rowToId(index.row()), mFeat, false, true);
579-
bool res = mFeatureMap.contains( rowToId( index.row() ) );
580-
581-
if ( res )
582-
{
583-
mLastRowId = rowToId( index.row() );
584-
mFeat = mFeatureMap[rowToId( index.row() )];
585-
mLastRow = ( QgsAttributeMap * ) & mFeat.attributeMap();
586-
587-
588-
// QgsDebugMsg(mFeatureMap[rowToId(index.row())].id());
589-
mFeatureMap[rowToId( index.row() )].changeAttribute( index.column(), value );
590-
// propagate back to the layer
591-
mLayer->changeAttributeValue( rowToId( index.row() ), index.column(), value, true );
592-
}
593-
594-
if ( !mLayer->isModified() )
595-
return false;
596-
597-
emit dataChanged( index, index );
598-
return true;
599-
}
600-
601-
void QgsAttributeTableMemModel::featureDeleted( int fid )
602-
{
603-
QgsDebugMsg( "entered." );
604-
mFeatureMap.remove( fid );
605-
QgsAttributeTableModel::featureDeleted( fid );
606-
}
607-
608-
void QgsAttributeTableMemModel::featureAdded( int fid )
609-
{
610-
QgsDebugMsg( "entered." );
611-
QgsFeature f;
612-
mLayer->featureAtId( fid, f, false, true );
613-
mFeatureMap.insert( fid, f );
614-
QgsAttributeTableModel::featureAdded( fid );
615-
}
616-
617-
#if 0
618-
void QgsAttributeTableMemModel::attributeAdded( int idx )
619-
{
620-
QgsDebugMsg( "entered." );
621-
loadLayer();
622-
reload( index( 0, 0 ), index( rowCount(), columnCount() ) );
623-
}
624-
625-
void QgsAttributeTableMemModel::attributeDeleted( int idx )
626-
{
627-
QgsDebugMsg( "entered." );
628-
loadLayer();
629-
reload( index( 0, 0 ), index( rowCount(), columnCount() ) );
630-
}
631-
#endif
632-
633-
void QgsAttributeTableMemModel::layerDeleted()
634-
{
635-
QgsDebugMsg( "entered." );
636-
mFeatureMap.clear();
637-
QgsAttributeTableModel::layerDeleted();
638-
}
639-
640-
void QgsAttributeTableMemModel::attributeValueChanged( int fid, int idx, const QVariant &value )
641-
{
642-
QgsDebugMsg( "entered." );
643-
mFeatureMap[fid].changeAttribute( idx, value );
644-
reload( index( 0, 0 ), index( rowCount(), columnCount() ) );
645-
}

src/app/attributetable/qgsattributetablemodel.h

+4-54
Original file line numberDiff line numberDiff line change
@@ -14,42 +14,17 @@
1414
* *
1515
***************************************************************************/
1616

17-
#ifndef QGSATTRIBUTETABKEMODEL_H
18-
#define QGSATTRIBUTETABKEMODEL_H
17+
#ifndef QGSATTRIBUTETABLEMODEL_H
18+
#define QGSATTRIBUTETABLEMODEL_H
1919

2020
#include <QAbstractTableModel>
21-
#include <QSortFilterProxyModel>
2221
#include <QModelIndex>
2322
#include <QObject>
2423

2524
//QGIS Includes
26-
#include "qgis.h"
2725
#include "qgsfeature.h" //QgsAttributeMap
2826
#include "qgsvectorlayer.h" //QgsAttributeList
29-
30-
class idColumnPair
31-
{
32-
public:
33-
int id;
34-
QVariant columnItem;
35-
36-
bool operator<( const idColumnPair &b ) const;
37-
};
38-
39-
class QgsAttributeTableFilterModel: public QSortFilterProxyModel
40-
{
41-
public:
42-
QgsAttributeTableFilterModel( QgsVectorLayer* theLayer );
43-
//QModelIndex mapToSource ( const QModelIndex & filterIndex ) const;
44-
//QModelIndex mapFromSource ( const QModelIndex & sourceIndex ) const;
45-
bool mHideUnselected;
46-
virtual void sort( int column, Qt::SortOrder order = Qt::AscendingOrder );
47-
protected:
48-
bool filterAcceptsRow( int sourceRow, const QModelIndex &sourceParent ) const;
49-
private:
50-
QgsVectorLayer* mLayer;
51-
};
52-
27+
#include "qgsattributetableidcolumnpair.h"
5328

5429
class QgsAttributeTableModel: public QAbstractTableModel
5530
{
@@ -102,7 +77,7 @@ class QgsAttributeTableModel: public QAbstractTableModel
10277
mutable QgsAttributeMap *mLastRow;
10378
QgsAttributeList mAttributes;
10479

105-
QList<idColumnPair> mSortList;
80+
QList<QgsAttributeTableIdColumnPair> mSortList;
10681
QMap<int, int> mIdRowMap;
10782
QMap<int, int> mRowIdMap;
10883

@@ -111,30 +86,5 @@ class QgsAttributeTableModel: public QAbstractTableModel
11186

11287
};
11388

114-
class QgsAttributeTableMemModel: public QgsAttributeTableModel
115-
{
116-
Q_OBJECT
117-
118-
public:
119-
QgsAttributeTableMemModel( QgsVectorLayer *theLayer );//, QObject *parent = 0);
120-
121-
protected slots:
122-
virtual void featureDeleted( int fid );
123-
virtual void featureAdded( int fid );
124-
virtual void layerDeleted();
125-
126-
private slots:
127-
//virtual void attributeAdded (int idx);
128-
//virtual void attributeDeleted (int idx);
129-
virtual void attributeValueChanged( int fid, int idx, const QVariant &value );
130-
//virtual void layerModified(bool onlyGeometry);
131-
132-
private:
133-
virtual QVariant data( const QModelIndex &index, int role ) const;
134-
virtual bool setData( const QModelIndex &index, const QVariant &value, int role );
135-
virtual void loadLayer();
136-
137-
QMap<int, QgsFeature> mFeatureMap;
138-
};
13989

14090
#endif

0 commit comments

Comments
 (0)