/
qgsattributetabledialog.cpp
executable file
·847 lines (713 loc) · 26.2 KB
/
qgsattributetabledialog.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
/***************************************************************************
QgsAttributeTableDialog.cpp
-------------------
date : Feb 2009
copyright : Vita Cizek
email : weetya (at) gmail.com
***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#include <QtGui>
#include "qgsattributetabledialog.h"
#include "qgsattributetablemodel.h"
#include "qgsattributetablefiltermodel.h"
#include "qgsattributetableview.h"
#include <qgsapplication.h>
#include <qgsvectordataprovider.h>
#include <qgsvectorlayer.h>
#include <qgssearchstring.h>
#include <qgssearchtreenode.h>
#include "qgisapp.h"
#include "qgsaddattrdialog.h"
#include "qgsdelattrdialog.h"
#include "qgssearchquerybuilder.h"
#include "qgslogger.h"
#include "qgsmapcanvas.h"
#include "qgsfieldcalculator.h"
#include "qgsfeatureaction.h"
#include "qgsattributeaction.h"
class QgsAttributeTableDock : public QDockWidget
{
public:
QgsAttributeTableDock( const QString & title, QWidget * parent = 0, Qt::WindowFlags flags = 0 )
: QDockWidget( title, parent, flags )
{
setObjectName( "AttributeTable" ); // set object name so the position can be saved
}
virtual void closeEvent( QCloseEvent * ev )
{
Q_UNUSED( ev );
deleteLater();
}
};
QgsAttributeTableDialog::QgsAttributeTableDialog( QgsVectorLayer *theLayer, QWidget *parent, Qt::WindowFlags flags )
: QDialog( parent, flags ), mDock( NULL )
{
mLayer = theLayer;
setupUi( this );
setAttribute( Qt::WA_DeleteOnClose );
QSettings settings;
restoreGeometry( settings.value( "/Windows/BetterAttributeTable/geometry" ).toByteArray() );
// extent has to be set before the model is created
QgsAttributeTableModel::setCurrentExtent( QgisApp::instance()->mapCanvas()->extent() );
connect( QgisApp::instance()->mapCanvas(), SIGNAL( extentsChanged() ), this, SLOT( updateExtent() ) );
mView->setLayer( mLayer );
mFilterModel = ( QgsAttributeTableFilterModel * ) mView->model();
mModel = ( QgsAttributeTableModel * )(( QgsAttributeTableFilterModel * )mView->model() )->sourceModel();
mQuery = query;
mColumnBox = columnBox;
columnBoxInit();
bool myDockFlag = settings.value( "/qgis/dockAttributeTable", false ).toBool();
if ( myDockFlag )
{
mDock = new QgsAttributeTableDock( tr( "Attribute table - %1 (%n Feature(s))", "feature count", mModel->rowCount() ).arg( mLayer->name() ), QgisApp::instance() );
mDock->setAllowedAreas( Qt::BottomDockWidgetArea | Qt::TopDockWidgetArea );
mDock->setWidget( this );
connect( this, SIGNAL( destroyed() ), mDock, SLOT( close() ) );
QgisApp::instance()->addDockWidget( Qt::BottomDockWidgetArea, mDock );
}
updateTitle();
mRemoveSelectionButton->setIcon( QgisApp::getThemeIcon( "/mActionUnselectAttributes.png" ) );
mSelectedToTopButton->setIcon( QgisApp::getThemeIcon( "/mActionSelectedToTop.png" ) );
mCopySelectedRowsButton->setIcon( QgisApp::getThemeIcon( "/mActionCopySelected.png" ) );
mZoomMapToSelectedRowsButton->setIcon( QgisApp::getThemeIcon( "/mActionZoomToSelected.png" ) );
mInvertSelectionButton->setIcon( QgisApp::getThemeIcon( "/mActionInvertSelection.png" ) );
mToggleEditingButton->setIcon( QgisApp::getThemeIcon( "/mActionToggleEditing.png" ) );
mSaveEditsButton->setIcon( QgisApp::getThemeIcon( "/mActionSaveEdits.png" ) );
mDeleteSelectedButton->setIcon( QgisApp::getThemeIcon( "/mActionDeleteSelected.png" ) );
mOpenFieldCalculator->setIcon( QgisApp::getThemeIcon( "/mActionCalculateField.png" ) );
mAddAttribute->setIcon( QgisApp::getThemeIcon( "/mActionNewAttribute.png" ) );
mRemoveAttribute->setIcon( QgisApp::getThemeIcon( "/mActionDeleteAttribute.png" ) );
// toggle editing
bool canChangeAttributes = mLayer->dataProvider()->capabilities() & QgsVectorDataProvider::ChangeAttributeValues;
bool canDeleteFeatures = mLayer->dataProvider()->capabilities() & QgsVectorDataProvider::DeleteFeatures;
bool canAddAttributes = mLayer->dataProvider()->capabilities() & QgsVectorDataProvider::AddAttributes;
bool canDeleteAttributes = mLayer->dataProvider()->capabilities() & QgsVectorDataProvider::DeleteAttributes;
bool canAddFeatures = mLayer->dataProvider()->capabilities() & QgsVectorDataProvider::AddFeatures;
mToggleEditingButton->setCheckable( true );
mToggleEditingButton->setChecked( mLayer->isEditable() );
mToggleEditingButton->setEnabled( canChangeAttributes && !mLayer->isReadOnly() );
mSaveEditsButton->setEnabled( canChangeAttributes && mLayer->isEditable() );
mOpenFieldCalculator->setEnabled( canChangeAttributes && mLayer->isEditable() );
mDeleteSelectedButton->setEnabled( canDeleteFeatures && mLayer->isEditable() );
mAddAttribute->setEnabled( canAddAttributes && mLayer->isEditable() );
mRemoveAttribute->setEnabled( canDeleteAttributes && mLayer->isEditable() );
mAddFeature->setEnabled( canAddFeatures && mLayer->isEditable() && mLayer->geometryType() == QGis::NoGeometry );
mAddFeature->setHidden( !canAddFeatures || mLayer->geometryType() != QGis::NoGeometry );
// info from table to application
connect( this, SIGNAL( editingToggled( QgsMapLayer * ) ), QgisApp::instance(), SLOT( toggleEditing( QgsMapLayer * ) ) );
connect( this, SIGNAL( saveEdits( QgsMapLayer * ) ), QgisApp::instance(), SLOT( saveEdits( QgsMapLayer * ) ) );
// info from layer to table
connect( mLayer, SIGNAL( editingStarted() ), this, SLOT( editingToggled() ) );
connect( mLayer, SIGNAL( editingStopped() ), this, SLOT( editingToggled() ) );
connect( searchButton, SIGNAL( clicked() ), this, SLOT( search() ) );
connect( mAddFeature, SIGNAL( clicked() ), this, SLOT( addFeature() ) );
connect( mLayer, SIGNAL( selectionChanged() ), this, SLOT( updateSelectionFromLayer() ) );
connect( mLayer, SIGNAL( layerDeleted() ), this, SLOT( close() ) );
connect( mView->verticalHeader(), SIGNAL( sectionClicked( int ) ), this, SLOT( updateRowSelection( int ) ) );
connect( mView->verticalHeader(), SIGNAL( sectionPressed( int ) ), this, SLOT( updateRowPressed( int ) ) );
connect( mModel, SIGNAL( modelChanged() ), this, SLOT( updateSelection() ) );
connect( mView, SIGNAL( willShowContextMenu( QMenu*, QModelIndex ) ), this, SLOT( viewWillShowContextMenu( QMenu*, QModelIndex ) ) );
mLastClickedHeaderIndex = 0;
mSelectionModel = new QItemSelectionModel( mFilterModel );
updateSelectionFromLayer();
//make sure to show all recs on first load
on_cbxShowSelectedOnly_toggled( false );
}
QgsAttributeTableDialog::~QgsAttributeTableDialog()
{
delete mSelectionModel;
}
void QgsAttributeTableDialog::updateTitle()
{
setWindowTitle( tr( "Attribute table - %1 :: %n / %2 feature(s) selected",
"feature count",
mView->selectionModel()->selectedRows().size()
)
.arg( mLayer->name() )
.arg( mModel->rowCount() )
);
}
void QgsAttributeTableDialog::closeEvent( QCloseEvent* event )
{
QDialog::closeEvent( event );
if ( mDock == NULL )
{
QSettings settings;
settings.setValue( "/Windows/BetterAttributeTable/geometry", saveGeometry() );
}
}
void QgsAttributeTableDialog::showAdvanced()
{
mMenuActions->exec( QCursor::pos() );
}
void QgsAttributeTableDialog::on_mSelectedToTopButton_clicked()
{
int freeIndex = 0;
//QgsFeatureIds fids = mSelectedFeatures;
//QgsFeatureIds::Iterator it = fids.begin();
mModel->incomingChangeLayout();
QgsFeatureIds::Iterator it = mSelectedFeatures.begin();
for ( ; it != mSelectedFeatures.end(); ++it, ++freeIndex )
{
QModelIndex sourceIndex = mFilterModel->mapToSource( mFilterModel->index( freeIndex, 0 ) );
mModel->swapRows( mModel->rowToId( sourceIndex.row() ), *it );
}
#if 0
while ( it != fids.end() )
{ //map!!!!
//mModel->swapRows(mModel->rowToId(freeIndex), *it);
//QModelIndex index = mFilterModel->mapFromSource(mModel->index(mModel->idToRow(*it), 0));
QModelIndex sourceIndex = mFilterModel->mapToSource( mFilterModel->index( freeIndex, 0 ) );
mModel->swapRows( mModel->rowToId( sourceIndex.row() ), *it );
//mModel->swapRows(freeIndex, *it);
if ( fids.empty() )
break;
else
++it;
++freeIndex;
}
#endif
// just select proper rows
//mModel->reload(mModel->index(0,0), mModel->index(mModel->rowCount(), mModel->columnCount()));
//mModel->changeLayout();
mModel->resetModel();
updateSelection();
}
void QgsAttributeTableDialog::on_mCopySelectedRowsButton_clicked()
{
QgisApp::instance()->editCopy( mLayer );
}
void QgsAttributeTableDialog::on_mZoomMapToSelectedRowsButton_clicked()
{
QgisApp::instance()->mapCanvas()->zoomToSelected( mLayer );
}
void QgsAttributeTableDialog::on_mInvertSelectionButton_clicked()
{
mLayer->invertSelection();
}
void QgsAttributeTableDialog::on_mRemoveSelectionButton_clicked()
{
mLayer->removeSelection();
}
void QgsAttributeTableDialog::on_mDeleteSelectedButton_clicked()
{
QgisApp::instance()->deleteSelected( mLayer );
}
void QgsAttributeTableDialog::on_cbxShowSelectedOnly_toggled( bool theFlag )
{
mFilterModel->setHideUnselected( theFlag );
mFilterModel->invalidate();
//TODO: weird
//mModel->changeLayout();
updateSelection();
}
void QgsAttributeTableDialog::columnBoxInit()
{
QgsFieldMap fieldMap = mLayer->pendingFields();
QgsFieldMap::Iterator it = fieldMap.begin();
for ( ; it != fieldMap.end(); ++it )
if ( mLayer->editType( it.key() ) != QgsVectorLayer::Hidden )
mColumnBox->addItem( it.value().name() );
mColumnBox->setCurrentIndex( mColumnBox->findText( mLayer->displayField() ) );
}
int QgsAttributeTableDialog::columnBoxColumnId()
{
QgsFieldMap fieldMap = mLayer->pendingFields();
QgsFieldMap::Iterator it = fieldMap.begin();
for ( ; it != fieldMap.end(); ++it )
if ( it.value().name() == mColumnBox->currentText() )
return it.key();
return 0;
}
void QgsAttributeTableDialog::updateSelection()
{
QModelIndex index;
QItemSelection selection;
QgsFeatureIds::Iterator it = mSelectedFeatures.begin();
for ( ; it != mSelectedFeatures.end(); ++it )
{
QModelIndex leftUpIndex = mFilterModel->mapFromSource( mModel->index( mModel->idToRow( *it ), 0 ) );
QModelIndex rightBottomIndex = mFilterModel->mapFromSource( mModel->index( mModel->idToRow( *it ), mModel->columnCount() - 1 ) );
selection.append( QItemSelectionRange( leftUpIndex, rightBottomIndex ) );
//selection.append(QItemSelectionRange(leftUpIndex, leftUpIndex));
}
mSelectionModel->select( selection, QItemSelectionModel::ClearAndSelect );// | QItemSelectionModel::Columns);
mView->setSelectionModel( mSelectionModel );
updateTitle();
#if 0
for ( int i = 0; i < mModel->rowCount(); ++i )
{
int id = mModel->rowToId( i );
QgsDebugMsg( id );
}
QgsDebugMsg( "--------------" );
#endif
}
void QgsAttributeTableDialog::updateRowPressed( int index )
{
mView->setSelectionMode( QAbstractItemView::ExtendedSelection );
mIndexPressed = index;
}
void QgsAttributeTableDialog::updateRowSelection( int index )
{
bool bDrag;
if ( mIndexPressed == index )
bDrag = false;
else
bDrag = true;
QString key = "";
if ( QApplication::keyboardModifiers() == Qt::ControlModifier )
key = "Control";
else if ( QApplication::keyboardModifiers() == Qt::ShiftModifier )
key = "Shift";
int first, last;
if ( bDrag )
{
if ( mIndexPressed < index )
{
first = mIndexPressed;
mLastClickedHeaderIndex = last = index;
}
else
{
last = mIndexPressed;
mLastClickedHeaderIndex = first = index;
}
updateRowSelection( first, last, 3 );
updateTitle();
mView->setSelectionMode( QAbstractItemView::NoSelection );
return;
}
else // No drag
{
if ( key == "Shift" )
{
QgsDebugMsg( "shift" );
// get the first and last index of the rows to be selected/deselected
first = last = 0;
if ( index > mLastClickedHeaderIndex )
{
first = mLastClickedHeaderIndex;
last = index;
}
else if ( index == mLastClickedHeaderIndex )
{
// row was selected and now it is shift-clicked
first = last = index;
}
else
{
first = index;
last = mLastClickedHeaderIndex;
}
// for all the rows update the selection, without starting a new selection
if ( first <= last )
updateRowSelection( first, last, 1 );
}
else if ( key == "Control" )
{
QgsDebugMsg( "ctrl" );
// update the single row selection, without starting a new selection
updateRowSelection( index, index, 2 );
// the next shift would start from here
mLastClickedHeaderIndex = index;
}
else // Single click
{
// Start a new selection if the row was not selected
updateRowSelection( index, index, 0 );
// the next shift would start from here
mLastClickedHeaderIndex = index;
}
}
mView->setSelectionMode( QAbstractItemView::NoSelection );
updateTitle();
}
void QgsAttributeTableDialog::updateRowSelection( int first, int last, int clickType )
{
// clickType= 0:Single click, 1:Shift, 2:Ctrl, 3: Dragged click
disconnect( mLayer, SIGNAL( selectionChanged() ), this, SLOT( updateSelectionFromLayer() ) );
// Id must be mapped to table/view row
QModelIndex index = mFilterModel->mapToSource( mFilterModel->index( first, 0 ) );
QgsFeatureId fid = mModel->rowToId( index.row() );
bool wasSelected = mSelectedFeatures.contains( fid );
// new selection should be created
if ( clickType == 0 ) // Single click
{
if ( mSelectedFeatures.size() == 1 && wasSelected ) // One item selected
return; // Click over a selected item doesn't do anything
mView->setCurrentIndex( mFilterModel->index( first, 0 ) );
mView->selectRow( first );
mSelectedFeatures.clear();
mSelectedFeatures.insert( fid );
mLayer->removeSelection();
mLayer->select( fid );
connect( mLayer, SIGNAL( selectionChanged() ), this, SLOT( updateSelectionFromLayer() ) );
return;
}
else if ( clickType == 1 ) // Shift
{
QgsFeatureIds newSelection;
for ( int i = first; i <= last; ++i )
{
if ( i >= first )
{
// Id must be mapped to table/view row
index = mFilterModel->mapToSource( mFilterModel->index( i, 0 ) );
fid = mModel->rowToId( index.row() );
}
newSelection.insert( fid );
}
// Remove items in mSelectedFeatures if they aren't in mNewSelection
QgsFeatureIds::Iterator it = mSelectedFeatures.begin();
while ( it != mSelectedFeatures.end() )
{
if ( !newSelection.contains( *it ) )
{
it = mSelectedFeatures.erase( it );
}
else
{
++it;
}
}
// Append the other fids in range first-last to mSelectedFeatures
QgsFeatureIds::Iterator itNew = newSelection.begin();
for ( ; itNew != newSelection.end(); ++itNew )
{
if ( !mSelectedFeatures.contains( *itNew ) )
{
mSelectedFeatures.insert( *itNew );
}
}
}
else if ( clickType == 2 ) // Ctrl
{
// existing selection should be updated
if ( wasSelected )
mSelectedFeatures.remove( fid );
else
mSelectedFeatures.insert( fid );
}
else if ( clickType == 3 ) // Dragged click
{
QgsFeatureIds newSelection;
for ( int i = first; i <= last; ++i )
{
if ( i >= first )
{
// Id must be mapped to table/view row
index = mFilterModel->mapToSource( mFilterModel->index( i, 0 ) );
fid = mModel->rowToId( index.row() );
}
newSelection.insert( fid );
}
// Remove items in mSelectedFeatures if they aren't in mNewSelection
QgsFeatureIds::Iterator it = mSelectedFeatures.begin();
while ( it != mSelectedFeatures.end() )
{
if ( !newSelection.contains( *it ) )
{
it = mSelectedFeatures.erase( it );
}
else
{
++it;
}
}
// Append the other fids in range first-last to mSelectedFeatures
QgsFeatureIds::Iterator itNew = newSelection.begin();
for ( ; itNew != newSelection.end(); ++itNew )
{
if ( !mSelectedFeatures.contains( *itNew ) )
{
mSelectedFeatures.insert( *itNew );
}
}
}
updateSelection();
mLayer->setSelectedFeatures( mSelectedFeatures );
connect( mLayer, SIGNAL( selectionChanged() ), this, SLOT( updateSelectionFromLayer() ) );
}
void QgsAttributeTableDialog::updateSelectionFromLayer()
{
QgsDebugMsg( "updateFromLayer" );
mSelectedFeatures = mLayer->selectedFeaturesIds();
if ( cbxShowSelectedOnly->isChecked() )
mFilterModel->invalidate();
updateSelection();
}
void QgsAttributeTableDialog::doSearch( QString searchString )
{
// parse search string and build parsed tree
QgsSearchString search;
if ( !search.setString( searchString ) )
{
QMessageBox::critical( this, tr( "Search string parsing error" ), search.parserErrorMsg() );
return;
}
QgsSearchTreeNode* searchTree = search.tree();
if ( searchTree == NULL )
{
QMessageBox::information( this, tr( "Search results" ), tr( "You've supplied an empty search string." ) );
return;
}
// TODO: fetch only necessary columns
// QStringList columns = searchTree->referencedColumns();
bool fetchGeom = searchTree->needsGeometry();
QApplication::setOverrideCursor( Qt::WaitCursor );
mSelectedFeatures.clear();
if ( cbxSearchSelectedOnly->isChecked() )
{
QgsFeatureList selectedFeatures = mLayer->selectedFeatures();
for ( QgsFeatureList::Iterator it = selectedFeatures.begin(); it != selectedFeatures.end(); ++it )
{
if ( searchTree->checkAgainst( mLayer->pendingFields(), *it ) )
mSelectedFeatures << it->id();
// check if there were errors during evaluating
if ( searchTree->hasError() )
break;
}
}
else
{
mLayer->select( mLayer->pendingAllAttributesList(), QgsRectangle(), fetchGeom );
QgsFeature f;
while ( mLayer->nextFeature( f ) )
{
if ( searchTree->checkAgainst( mLayer->pendingFields(), f ) )
mSelectedFeatures << f.id();
// check if there were errors during evaluating
if ( searchTree->hasError() )
break;
}
}
QApplication::restoreOverrideCursor();
if ( searchTree->hasError() )
{
QMessageBox::critical( this, tr( "Error during search" ), searchTree->errorMsg() );
return;
}
mLayer->setSelectedFeatures( mSelectedFeatures );
QString str;
QWidget *w = mDock ? qobject_cast<QWidget*>( mDock ) : qobject_cast<QWidget*>( this );
if ( mSelectedFeatures.size() )
{
w->setWindowTitle( tr( "Attribute table - %1 (%n matching features)", "matching features", mSelectedFeatures.size() ).arg( mLayer->name() ) );
}
else
{
w->setWindowTitle( tr( "Attribute table - %1 (No matching features)" ).arg( mLayer->name() ) );
}
mView->setFocus();
}
void QgsAttributeTableDialog::search()
{
QString fieldName = mColumnBox->currentText();
const QgsFieldMap& flds = mLayer->pendingFields();
int fldIndex = mLayer->fieldNameIndex( fieldName );
QVariant::Type fldType = flds[fldIndex].type();
bool numeric = ( fldType == QVariant::Int || fldType == QVariant::Double );
QString sensString = "ILIKE";
if ( cbxCaseSensitive->isChecked() )
{
sensString = "LIKE";
}
QSettings settings;
QString nullValue = settings.value( "qgis/nullValue", "NULL" ).toString();
QString str;
if ( mQuery->displayText() == nullValue )
{
str = QString( "%1 IS NULL" ).arg( QgsSearchTreeNode::quotedColumnRef( fieldName ) );
}
else
{
str = QString( "%1 %2 '%3'" )
.arg( QgsSearchTreeNode::quotedColumnRef( fieldName ) )
.arg( numeric ? "=" : sensString )
.arg( numeric
? mQuery->displayText().replace( "'", "''" )
:
"%" + mQuery->displayText().replace( "'", "''" ) + "%" ); // escape quotes
}
doSearch( str );
}
void QgsAttributeTableDialog::on_mAdvancedSearchButton_clicked()
{
QgsSearchQueryBuilder dlg( mLayer, this );
dlg.setSearchString( mQuery->displayText() );
if ( dlg.exec() == QDialog::Accepted )
doSearch( dlg.searchString() );
}
void QgsAttributeTableDialog::on_mToggleEditingButton_toggled()
{
emit editingToggled( mLayer );
}
void QgsAttributeTableDialog::on_mSaveEditsButton_clicked()
{
emit saveEdits( mLayer );
}
void QgsAttributeTableDialog::editingToggled()
{
mToggleEditingButton->blockSignals( true );
mToggleEditingButton->setChecked( mLayer->isEditable() );
mSaveEditsButton->setEnabled( mLayer->isEditable() );
mToggleEditingButton->blockSignals( false );
bool canChangeAttributes = mLayer->dataProvider()->capabilities() & QgsVectorDataProvider::ChangeAttributeValues;
bool canDeleteFeatures = mLayer->dataProvider()->capabilities() & QgsVectorDataProvider::DeleteFeatures;
bool canAddAttributes = mLayer->dataProvider()->capabilities() & QgsVectorDataProvider::AddAttributes;
bool canDeleteAttributes = mLayer->dataProvider()->capabilities() & QgsVectorDataProvider::DeleteAttributes;
bool canAddFeatures = mLayer->dataProvider()->capabilities() & QgsVectorDataProvider::AddFeatures;
mOpenFieldCalculator->setEnabled( canChangeAttributes && mLayer->isEditable() );
mDeleteSelectedButton->setEnabled( canDeleteFeatures && mLayer->isEditable() );
mAddAttribute->setEnabled( canAddAttributes && mLayer->isEditable() );
mRemoveAttribute->setEnabled( canDeleteAttributes && mLayer->isEditable() );
mAddFeature->setEnabled( canAddFeatures && mLayer->isEditable() && mLayer->geometryType() == QGis::NoGeometry );
// (probably reload data if user stopped editing - possible revert)
mModel->reload( mModel->index( 0, 0 ), mModel->index( mModel->rowCount(), mModel->columnCount() ) );
// not necessary to set table read only if layer is not editable
// because model always reflects actual state when returning item flags
}
// not used now
void QgsAttributeTableDialog::startEditing()
{
mLayer->startEditing();
}
// not used now
void QgsAttributeTableDialog::submit()
{
mLayer->commitChanges();
}
// not used now
void QgsAttributeTableDialog::revert()
{
mLayer->rollBack();
mModel->revert();
mModel->reload( mModel->index( 0, 0 ), mModel->index( mModel->rowCount(), mModel->columnCount() ) );
}
void QgsAttributeTableDialog::on_mAddAttribute_clicked()
{
if ( !mLayer )
{
return;
}
QgsAddAttrDialog dialog( mLayer, this );
if ( dialog.exec() == QDialog::Accepted )
{
mLayer->beginEditCommand( tr( "Attribute added" ) );
if ( mLayer->addAttribute( dialog.field() ) )
{
mLayer->endEditCommand();
}
else
{
QMessageBox::critical( 0, tr( "Attribute Error" ), tr( "The attribute could not be added to the layer" ) );
mLayer->destroyEditCommand();
}
// update model - a field has been added or updated
mModel->reload( mModel->index( 0, 0 ), mModel->index( mModel->rowCount(), mModel->columnCount() ) );
}
}
void QgsAttributeTableDialog::on_mRemoveAttribute_clicked()
{
if ( !mLayer )
{
return;
}
QgsDelAttrDialog dialog( mLayer );
if ( dialog.exec() == QDialog::Accepted )
{
QList<int> attributes = dialog.selectedAttributes();
if ( attributes.size() < 1 )
{
return;
}
mLayer->beginEditCommand( tr( "Deleted attribute" ) );
bool deleted = false;
QList<int>::const_iterator it = attributes.constBegin();
for ( ; it != attributes.constEnd(); ++it )
{
if ( mLayer->deleteAttribute( *it ) )
{
deleted = true;
}
}
if ( deleted )
{
mLayer->endEditCommand();
}
else
{
QMessageBox::critical( 0, tr( "Attribute Error" ), tr( "The attribute(s) could not be deleted" ) );
mLayer->destroyEditCommand();
}
// update model - a field has been added or updated
mModel->reload( mModel->index( 0, 0 ), mModel->index( mModel->rowCount(), mModel->columnCount() ) );
}
}
void QgsAttributeTableDialog::on_mOpenFieldCalculator_clicked()
{
QgsFieldCalculator calc( mLayer );
if ( calc.exec() == QDialog::Accepted )
{
int col = mModel->fieldCol( calc.changedAttributeId() );
if ( col >= 0 )
{
mModel->reload( mModel->index( 0, col ),
mModel->index( mModel->rowCount(), col ) );
}
}
}
void QgsAttributeTableDialog::addFeature()
{
if ( !mLayer->isEditable() )
return;
QgsFeature f;
QgsFeatureAction action( tr( "Geometryless feature added" ), f, mLayer, -1, this );
if ( action.addFeature() )
{
mModel->reload( mModel->index( 0, 0 ), mModel->index( mModel->rowCount(), mModel->columnCount() ) );
}
}
void QgsAttributeTableDialog::updateExtent()
{
// let the model know about the new extent (we may be showing only features from current extent)
QgsAttributeTableModel::setCurrentExtent( QgisApp::instance()->mapCanvas()->extent() );
}
void QgsAttributeTableDialog::viewWillShowContextMenu( QMenu* menu, QModelIndex atIndex )
{
if ( mLayer->actions()->size() != 0 )
{
QAction *a = menu->addAction( tr( "Run action" ) );
a->setEnabled( false );
for ( int i = 0; i < mLayer->actions()->size(); i++ )
{
const QgsAction &action = mLayer->actions()->at( i );
if ( !action.runable() )
continue;
QgsAttributeTableAction *a = new QgsAttributeTableAction( action.name(), mView, mModel, i, atIndex );
menu->addAction( action.name(), a, SLOT( execute() ) );
}
}
QgsAttributeTableAction *a = new QgsAttributeTableAction( tr( "Open form" ), mView, mModel, -1, atIndex );
menu->addAction( tr( "Open form" ), a, SLOT( featureForm() ) );
}
void QgsAttributeTableAction::execute()
{
mModel->executeAction( mAction, mFieldIdx );
}
void QgsAttributeTableAction::featureForm()
{
QgsFeature f = mModel->feature( mFieldIdx );
QgsFeatureAction action( tr( "Attributes changed" ), f, mModel->layer(), -1, this );
if ( mModel->layer()->isEditable() )
action.editFeature();
else
action.viewFeatureForm();
}