14
14
***************************************************************************/
15
15
16
16
#include " qgsattributetablemodel.h"
17
- // #include "qgsattributetableview .h"
17
+ #include " qgsattributetablefiltermodel .h"
18
18
19
- #include " qgsvectordataprovider.h"
20
19
#include " qgsfield.h"
21
20
#include " qgsvectorlayer.h"
21
+ #include " qgslogger.h"
22
22
23
23
#include < QtGui>
24
24
#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
- */
79
25
80
26
// //////////////////////////
81
27
// QgsAttributeTableModel //
@@ -336,7 +282,7 @@ QVariant QgsAttributeTableModel::headerData( int section, Qt::Orientation orient
336
282
void QgsAttributeTableModel::sort ( int column, Qt::SortOrder order )
337
283
{
338
284
QgsAttributeMap row;
339
- idColumnPair pair;
285
+ QgsAttributeTableIdColumnPair pair;
340
286
QgsAttributeList attrs;
341
287
QgsFeature f;
342
288
@@ -360,14 +306,14 @@ void QgsAttributeTableModel::sort( int column, Qt::SortOrder order )
360
306
if ( order == Qt::AscendingOrder )
361
307
qStableSort ( mSortList .begin (), mSortList .end () );
362
308
else
363
- qStableSort ( mSortList .begin (), mSortList .end (), qGreater<idColumnPair >() );
309
+ qStableSort ( mSortList .begin (), mSortList .end (), qGreater<QgsAttributeTableIdColumnPair >() );
364
310
365
311
// recalculate id<->row maps
366
312
mRowIdMap .clear ();
367
313
mIdRowMap .clear ();
368
314
369
315
int i = 0 ;
370
- QList<idColumnPair >::Iterator it;
316
+ QList<QgsAttributeTableIdColumnPair >::Iterator it;
371
317
for ( it = mSortList .begin (); it != mSortList .end (); ++it, ++i )
372
318
{
373
319
mRowIdMap .insert ( i, it->id );
@@ -491,155 +437,3 @@ void QgsAttributeTableModel::incomingChangeLayout()
491
437
emit layoutAboutToBeChanged ();
492
438
}
493
439
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
- }
0 commit comments