|
28 | 28 | #include <qgssearchtreenode.h>
|
29 | 29 |
|
30 | 30 | #include "qgisapp.h"
|
| 31 | +#include "qgsaddattrdialog.h" |
| 32 | +#include "qgsdelattrdialog.h" |
31 | 33 | #include "qgssearchquerybuilder.h"
|
32 | 34 | #include "qgslogger.h"
|
33 | 35 | #include "qgsmapcanvas.h"
|
@@ -88,11 +90,18 @@ QgsAttributeTableDialog::QgsAttributeTableDialog( QgsVectorLayer *theLayer, QWid
|
88 | 90 | mInvertSelectionButton->setIcon( getThemeIcon( "/mActionInvertSelection.png" ) );
|
89 | 91 | mToggleEditingButton->setIcon( getThemeIcon( "/mActionToggleEditing.png" ) );
|
90 | 92 | mOpenFieldCalculator->setIcon( getThemeIcon( "/mActionCalculateField.png" ) );
|
| 93 | + mAddAttribute->setIcon( getThemeIcon( "/mActionNewAttribute.png" ) ); |
| 94 | + mRemoveAttribute->setIcon( getThemeIcon( "/mActionDeleteAttribute.png" ) ); |
| 95 | + |
91 | 96 | // toggle editing
|
92 | 97 | bool canChangeAttributes = mLayer->dataProvider()->capabilities() & QgsVectorDataProvider::ChangeAttributeValues;
|
| 98 | + bool canAddAttributes = mLayer->dataProvider()->capabilities() & QgsVectorDataProvider::AddAttributes; |
| 99 | + bool canDeleteAttributes = mLayer->dataProvider()->capabilities() & QgsVectorDataProvider::DeleteAttributes; |
93 | 100 | mToggleEditingButton->setCheckable( true );
|
94 | 101 | mToggleEditingButton->setEnabled( canChangeAttributes );
|
95 | 102 | mOpenFieldCalculator->setEnabled( canChangeAttributes && mLayer->isEditable() );
|
| 103 | + mAddAttribute->setEnabled( canAddAttributes && mLayer->isEditable() ); |
| 104 | + mRemoveAttribute->setEnabled( canDeleteAttributes && mLayer->isEditable() ); |
96 | 105 |
|
97 | 106 | // info from table to application
|
98 | 107 | connect( this, SIGNAL( editingToggled( QgsMapLayer * ) ), QgisApp::instance(), SLOT( toggleEditing( QgsMapLayer * ) ) );
|
@@ -526,7 +535,11 @@ void QgsAttributeTableDialog::editingToggled()
|
526 | 535 | mToggleEditingButton->blockSignals( false );
|
527 | 536 |
|
528 | 537 | bool canChangeAttributes = mLayer->dataProvider()->capabilities() & QgsVectorDataProvider::ChangeAttributeValues;
|
| 538 | + bool canAddAttributes = mLayer->dataProvider()->capabilities() & QgsVectorDataProvider::AddAttributes; |
| 539 | + bool canDeleteAttributes = mLayer->dataProvider()->capabilities() & QgsVectorDataProvider::DeleteAttributes; |
529 | 540 | mOpenFieldCalculator->setEnabled( canChangeAttributes && mLayer->isEditable() );
|
| 541 | + mAddAttribute->setEnabled( canAddAttributes && mLayer->isEditable() ); |
| 542 | + mRemoveAttribute->setEnabled( canDeleteAttributes && mLayer->isEditable() ); |
530 | 543 |
|
531 | 544 | // (probably reload data if user stopped editing - possible revert)
|
532 | 545 | mModel->reload( mModel->index( 0, 0 ), mModel->index( mModel->rowCount(), mModel->columnCount() ) );
|
@@ -555,6 +568,68 @@ void QgsAttributeTableDialog::revert()
|
555 | 568 | mModel->reload( mModel->index( 0, 0 ), mModel->index( mModel->rowCount(), mModel->columnCount() ) );
|
556 | 569 | }
|
557 | 570 |
|
| 571 | +void QgsAttributeTableDialog::on_mAddAttribute_clicked() |
| 572 | +{ |
| 573 | + if ( !mLayer ) |
| 574 | + { |
| 575 | + return; |
| 576 | + } |
| 577 | + |
| 578 | + QgsAddAttrDialog dialog( mLayer->dataProvider(), this ); |
| 579 | + if ( dialog.exec() == QDialog::Accepted ) |
| 580 | + { |
| 581 | + mLayer->beginEditCommand( tr( "Attribute added" ) ); |
| 582 | + if ( mLayer->addAttribute( dialog.field() ) ) |
| 583 | + { |
| 584 | + mLayer->endEditCommand(); |
| 585 | + } |
| 586 | + else |
| 587 | + { |
| 588 | + QMessageBox::critical( 0, tr( "Attribute Error" ), tr( "The attribute could not be added to the layer" ) ); |
| 589 | + mLayer->destroyEditCommand(); |
| 590 | + } |
| 591 | + } |
| 592 | +} |
| 593 | + |
| 594 | +void QgsAttributeTableDialog::on_mRemoveAttribute_clicked() |
| 595 | +{ |
| 596 | + if ( !mLayer ) |
| 597 | + { |
| 598 | + return; |
| 599 | + } |
| 600 | + |
| 601 | + QgsDelAttrDialog dialog( mLayer ); |
| 602 | + if ( dialog.exec() == QDialog::Accepted ) |
| 603 | + { |
| 604 | + QList<int> attributes = dialog.selectedAttributes(); |
| 605 | + if ( attributes.size() < 1 ) |
| 606 | + { |
| 607 | + return; |
| 608 | + } |
| 609 | + |
| 610 | + mLayer->beginEditCommand( tr( "Deleted attribute" ) ); |
| 611 | + bool deleted = false; |
| 612 | + QList<int>::const_iterator it = attributes.constBegin(); |
| 613 | + for ( ; it != attributes.constEnd(); ++it ) |
| 614 | + { |
| 615 | + if ( mLayer->deleteAttribute( *it ) ) |
| 616 | + { |
| 617 | + deleted = true; |
| 618 | + } |
| 619 | + } |
| 620 | + |
| 621 | + if ( deleted ) |
| 622 | + { |
| 623 | + mLayer->endEditCommand(); |
| 624 | + } |
| 625 | + else |
| 626 | + { |
| 627 | + QMessageBox::critical( 0, tr( "Attribute Error" ), tr( "The attribute(s) could not be deleted" ) ); |
| 628 | + mLayer->destroyEditCommand(); |
| 629 | + } |
| 630 | + } |
| 631 | +} |
| 632 | + |
558 | 633 | void QgsAttributeTableDialog::on_mOpenFieldCalculator_clicked()
|
559 | 634 | {
|
560 | 635 | QgsFieldCalculator calc( mLayer );
|
|
0 commit comments