Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Merge pull request #38383 from suricactus/new_feat_constraint
Allow adding new features via attribute form
  • Loading branch information
m-kuhn committed Sep 4, 2020
2 parents d998bb3 + 718c09f commit a51c201
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 2 deletions.
50 changes: 48 additions & 2 deletions src/app/qgsattributetabledialog.cpp
Expand Up @@ -17,6 +17,7 @@
#include <QMessageBox>
#include <QGridLayout>
#include <QDialogButtonBox>
#include <QMenu>

#include "qgsattributetabledialog.h"
#include "qgsattributetablemodel.h"
Expand Down Expand Up @@ -111,6 +112,8 @@ QgsAttributeTableDialog::QgsAttributeTableDialog( QgsVectorLayer *layer, QgsAttr
connect( mActionDeleteSelected, &QAction::triggered, this, &QgsAttributeTableDialog::mActionDeleteSelected_triggered );
connect( mMainView, &QgsDualView::currentChanged, this, &QgsAttributeTableDialog::mMainView_currentChanged );
connect( mActionAddFeature, &QAction::triggered, this, &QgsAttributeTableDialog::mActionAddFeature_triggered );
connect( mActionAddFeatureViaAttributeTable, &QAction::triggered, this, &QgsAttributeTableDialog::mActionAddFeatureViaAttributeTable_triggered );
connect( mActionAddFeatureViaAttributeForm, &QAction::triggered, this, &QgsAttributeTableDialog::mActionAddFeatureViaAttributeForm_triggered );
connect( mActionExpressionSelect, &QAction::triggered, this, &QgsAttributeTableDialog::mActionExpressionSelect_triggered );
connect( mMainView, &QgsDualView::showContextMenuExternally, this, &QgsAttributeTableDialog::showContextMenu );

Expand All @@ -123,6 +126,16 @@ QgsAttributeTableDialog::QgsAttributeTableDialog( QgsVectorLayer *layer, QgsAttr
mActionPasteFeatures->setShortcuts( QKeySequence::Paste );
mActionPasteFeatures->setShortcutContext( Qt::WidgetWithChildrenShortcut );

QgsSettings settings;

mActionAddFeature->setMenu( new QMenu( mActionAddFeature->parentWidget() ) );
mActionAddFeature->menu()->addAction( mActionAddFeatureViaAttributeTable );
mActionAddFeature->menu()->addAction( mActionAddFeatureViaAttributeForm );
mActionAddFeature->setIcon(
settings.value( QStringLiteral( "/qgis/attributeTableLastAddFeatureMethod" ) ) == QStringLiteral( "attributeForm" )
? mActionAddFeatureViaAttributeForm->icon()
: mActionAddFeatureViaAttributeTable->icon() );

const QgsFields fields = mLayer->fields();
for ( const QgsField &field : fields )
{
Expand All @@ -138,8 +151,6 @@ QgsAttributeTableDialog::QgsAttributeTableDialog( QgsVectorLayer *layer, QgsAttr
layout()->setContentsMargins( 0, 0, 0, 0 );
static_cast< QGridLayout * >( layout() )->setVerticalSpacing( 0 );

QgsSettings settings;

int size = settings.value( QStringLiteral( "/qgis/iconSize" ), 16 ).toInt();
if ( size > 32 )
{
Expand Down Expand Up @@ -586,10 +597,24 @@ void QgsAttributeTableDialog::mActionReload_triggered()
}

void QgsAttributeTableDialog::mActionAddFeature_triggered()
{
QgsSettings s;

if ( s.value( QStringLiteral( "/qgis/attributeTableLastAddFeatureMethod" ) ) == QStringLiteral( "attributeForm" ) )
mActionAddFeatureViaAttributeForm_triggered();
else
mActionAddFeatureViaAttributeTable_triggered();
}

void QgsAttributeTableDialog::mActionAddFeatureViaAttributeTable_triggered()
{
if ( !mLayer->isEditable() )
return;

QgsSettings s;
s.setValue( QStringLiteral( "/qgis/attributeTableLastAddFeatureMethod" ), QStringLiteral( "attributeTable" ) );
mActionAddFeature->setIcon( mActionAddFeatureViaAttributeTable->icon() );

QgsAttributeTableModel *masterModel = mMainView->masterModel();

QgsFeature f;
Expand All @@ -601,6 +626,27 @@ void QgsAttributeTableDialog::mActionAddFeature_triggered()
}
}

void QgsAttributeTableDialog::mActionAddFeatureViaAttributeForm_triggered()
{
if ( !mLayer->isEditable() )
return;

QgsSettings s;
s.setValue( QStringLiteral( "/qgis/attributeTableLastAddFeatureMethod" ), QStringLiteral( "attributeForm" ) );
mActionAddFeature->setIcon( mActionAddFeatureViaAttributeForm->icon() );

QgsFeature f;

QgsFeatureAction action( tr( "Feature Added" ), f, mLayer, QString(), -1, this );
QgsAttributeTableModel *masterModel = mMainView->masterModel();

if ( action.addFeature() )
{
masterModel->reload( masterModel->index( 0, 0 ), masterModel->index( masterModel->rowCount() - 1, masterModel->columnCount() - 1 ) );
}
}


void QgsAttributeTableDialog::mActionExpressionSelect_triggered()
{
QgsExpressionSelectionDialog *dlg = new QgsExpressionSelectionDialog( mLayer );
Expand Down
2 changes: 2 additions & 0 deletions src/app/qgsattributetabledialog.h
Expand Up @@ -171,6 +171,8 @@ class APP_EXPORT QgsAttributeTableDialog : public QDialog, private Ui::QgsAttrib
* add feature
*/
void mActionAddFeature_triggered();
void mActionAddFeatureViaAttributeTable_triggered();
void mActionAddFeatureViaAttributeForm_triggered();

void mActionExpressionSelect_triggered();

Expand Down
21 changes: 21 additions & 0 deletions src/ui/qgsattributetabledialog.ui
Expand Up @@ -532,6 +532,27 @@
<string>Organize Columns</string>
</property>
</action>
<action name="mActionAddFeatureViaAttributeTable">
<property name="icon">
<iconset resource="../../images/images.qrc">
<normaloff>:/images/themes/default/mActionNewTableRow.svg</normaloff>:/images/themes/default/mActionNewTableRow.svg</iconset>
</property>
<property name="text">
<string>Add feature via attribute table</string>
</property>
</action>
<action name="mActionAddFeatureViaAttributeForm">
<property name="icon">
<iconset resource="../../images/images.qrc">
<normaloff>:/images/themes/default/mIconFormSelect.svg</normaloff>:/images/themes/default/mIconFormSelect.svg</iconset>
</property>
<property name="text">
<string>Add feature via attribute form</string>
</property>
<property name="toolTip">
<string>Add feature via attribute form</string>
</property>
</action>
</widget>
<customwidgets>
<customwidget>
Expand Down

0 comments on commit a51c201

Please sign in to comment.