Skip to content

Commit

Permalink
Added button to select attributes from largest geometry when merging …
Browse files Browse the repository at this point in the history
…features
  • Loading branch information
uclaros authored and nyalldawson committed Mar 15, 2021
1 parent e44f8a7 commit 5dbcb00
Show file tree
Hide file tree
Showing 5 changed files with 122 additions and 12 deletions.
1 change: 1 addition & 0 deletions images/images.qrc
Expand Up @@ -309,6 +309,7 @@
<file>themes/default/mActionFilter2.svg</file>
<file>themes/default/mActionFolder.svg</file>
<file>themes/default/mActionFormAnnotation.svg</file>
<file>themes/default/mActionFromLargestFeature.svg</file>
<file>themes/default/mActionFromSelectedFeature.svg</file>
<file>themes/default/mActionFormView.svg</file>
<file>themes/default/mActionFullCumulativeCutStretch.svg</file>
Expand Down
1 change: 1 addition & 0 deletions images/themes/default/mActionFromLargestFeature.svg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
70 changes: 61 additions & 9 deletions src/app/qgsmergeattributesdialog.cpp
Expand Up @@ -63,6 +63,7 @@ QgsMergeAttributesDialog::QgsMergeAttributesDialog( const QgsFeatureList &featur
QgsGui::enableAutoGeometryRestore( this );

connect( mFromSelectedPushButton, &QPushButton::clicked, this, &QgsMergeAttributesDialog::mFromSelectedPushButton_clicked );
connect( mFromLargestPushButton, &QPushButton::clicked, this, &QgsMergeAttributesDialog::mFromLargestPushButton_clicked );
connect( mRemoveFeatureFromSelectionButton, &QPushButton::clicked, this, &QgsMergeAttributesDialog::mRemoveFeatureFromSelectionButton_clicked );
createTableWidgetContents();

Expand All @@ -75,8 +76,13 @@ QgsMergeAttributesDialog::QgsMergeAttributesDialog( const QgsFeatureList &featur
mTableWidget->setSelectionMode( QAbstractItemView::SingleSelection );

mFromSelectedPushButton->setIcon( QgsApplication::getThemeIcon( QStringLiteral( "mActionFromSelectedFeature.svg" ) ) );
mFromLargestPushButton->setIcon( QgsApplication::getThemeIcon( QStringLiteral( "mActionFromLargestFeature.svg" ) ) );
mRemoveFeatureFromSelectionButton->setIcon( QgsApplication::getThemeIcon( QStringLiteral( "mActionRemoveSelectedFeature.svg" ) ) );

mFromLargestPushButton->setEnabled( mVectorLayer->geometryType() == QgsWkbTypes::LineGeometry ||
mVectorLayer->geometryType() == QgsWkbTypes::PolygonGeometry );
mTakeLargestAttributesLabel->setEnabled( mFromLargestPushButton->isEnabled() );

connect( mSkipAllButton, &QAbstractButton::clicked, this, &QgsMergeAttributesDialog::setAllToSkip );
connect( mTableWidget, &QTableWidget::cellChanged, this, &QgsMergeAttributesDialog::tableWidgetCellChanged );
}
Expand Down Expand Up @@ -389,6 +395,24 @@ QVariant QgsMergeAttributesDialog::featureAttribute( QgsFeatureId featureId, int
return QVariant( mVectorLayer->fields().at( fieldIdx ).type() );
}

void QgsMergeAttributesDialog::setAllAttributesFromFeature( QgsFeatureId featureId )
{
for ( int i = 0; i < mTableWidget->columnCount(); ++i )
{
QComboBox *currentComboBox = qobject_cast<QComboBox *>( mTableWidget->cellWidget( 0, i ) );
if ( !currentComboBox )
continue;

if ( mVectorLayer->fields().at( i ).constraints().constraints() & QgsFieldConstraints::ConstraintUnique )
{
currentComboBox->setCurrentIndex( currentComboBox->findData( QStringLiteral( "skip" ) ) );
}
else
{
currentComboBox->setCurrentIndex( currentComboBox->findData( QStringLiteral( "f%1" ).arg( FID_TO_STRING( featureId ) ) ) );
}
}
}

QVariant QgsMergeAttributesDialog::calcStatistic( int col, QgsStatisticalSummary::Statistic stat )
{
Expand Down Expand Up @@ -460,21 +484,49 @@ void QgsMergeAttributesDialog::mFromSelectedPushButton_clicked()
return;
}

for ( int i = 0; i < mTableWidget->columnCount(); ++i )
{
QComboBox *currentComboBox = qobject_cast<QComboBox *>( mTableWidget->cellWidget( 0, i ) );
if ( !currentComboBox )
continue;
setAllAttributesFromFeature( featureId );
}

if ( mVectorLayer->fields().at( i ).constraints().constraints() & QgsFieldConstraints::ConstraintUnique )
void QgsMergeAttributesDialog::mFromLargestPushButton_clicked()
{
QgsFeatureId featureId;
double maxValue = 0;

switch ( mVectorLayer->geometryType() )
{
case QgsWkbTypes::LineGeometry:
{
currentComboBox->setCurrentIndex( currentComboBox->findData( QStringLiteral( "skip" ) ) );
QgsFeatureList::const_iterator f_it = mFeatureList.constBegin();
for ( ; f_it != mFeatureList.constEnd(); ++f_it )
{
double featureLength = f_it->geometry().length();
if ( featureLength > maxValue )
{
featureId = f_it->id();
maxValue = featureLength;
}
}
break;
}
else
case QgsWkbTypes::PolygonGeometry:
{
currentComboBox->setCurrentIndex( currentComboBox->findData( QStringLiteral( "f%1" ).arg( FID_TO_STRING( featureId ) ) ) );
QgsFeatureList::const_iterator f_it = mFeatureList.constBegin();
for ( ; f_it != mFeatureList.constEnd(); ++f_it )
{
double featureArea = f_it->geometry().area();
if ( featureArea > maxValue )
{
featureId = f_it->id();
maxValue = featureArea;
}
}
break;
}
default:
return;
}
if ( maxValue > 0 )
setAllAttributesFromFeature( featureId );
}

void QgsMergeAttributesDialog::mRemoveFeatureFromSelectionButton_clicked()
Expand Down
3 changes: 3 additions & 0 deletions src/app/qgsmergeattributesdialog.h
Expand Up @@ -65,6 +65,7 @@ class APP_EXPORT QgsMergeAttributesDialog: public QDialog, private Ui::QgsMergeA
void comboValueChanged( const QString &text );
void selectedRowChanged();
void mFromSelectedPushButton_clicked();
void mFromLargestPushButton_clicked();
void mRemoveFeatureFromSelectionButton_clicked();
void tableWidgetCellChanged( int row, int column );

Expand All @@ -83,6 +84,8 @@ class APP_EXPORT QgsMergeAttributesDialog: public QDialog, private Ui::QgsMergeA
void refreshMergedValue( int col );
//! Inserts the attribute value of a specific feature into the row of merged attributes
QVariant featureAttribute( QgsFeatureId featureId, int fieldIdx );
//! Inserts all attribute values of a specific feature into the row of merged attributes
void setAllAttributesFromFeature( QgsFeatureId featureId );
//! Appends the values of the features for the final value
QVariant concatenationAttribute( int col );

Expand Down
59 changes: 56 additions & 3 deletions src/ui/qgsmergeattributesdialogbase.ui
Expand Up @@ -30,6 +30,9 @@
<property name="text">
<string/>
</property>
<property name="autoDefault">
<bool>false</bool>
</property>
</widget>
</item>
<item>
Expand Down Expand Up @@ -57,7 +60,53 @@
</item>
</layout>
</item>
<item row="3" column="0">
<item row="2" column="0">
<layout class="QHBoxLayout" name="horizontalLayout_4">
<item>
<widget class="QPushButton" name="mFromLargestPushButton">
<property name="toolTip">
<string>Take all attributes from the Polygon with the largest area or Line with the longest length</string>
</property>
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="text">
<string/>
</property>
<property name="autoDefault">
<bool>false</bool>
</property>
</widget>
</item>
<item>
<widget class="QLabel" name="mTakeLargestAttributesLabel">
<property name="text">
<string>Take attributes from largest geometry</string>
</property>
<property name="buddy">
<cstring>mFromLargestPushButton</cstring>
</property>
</widget>
</item>
<item>
<spacer name="horizontalSpacer_4">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
</layout>
</item>
<item row="4" column="0">
<layout class="QHBoxLayout" name="horizontalLayout_2">
<item>
<widget class="QPushButton" name="mRemoveFeatureFromSelectionButton">
Expand Down Expand Up @@ -91,7 +140,7 @@
</item>
</layout>
</item>
<item row="4" column="0">
<item row="5" column="0">
<widget class="QDialogButtonBox" name="buttonBox">
<property name="orientation">
<enum>Qt::Horizontal</enum>
Expand All @@ -101,7 +150,7 @@
</property>
</widget>
</item>
<item row="2" column="0">
<item row="3" column="0">
<layout class="QHBoxLayout" name="horizontalLayout_3">
<item>
<widget class="QPushButton" name="mSkipAllButton">
Expand All @@ -115,6 +164,9 @@
<iconset resource="../../images/images.qrc">
<normaloff>:/images/themes/default/mActionSelectPan.svg</normaloff>:/images/themes/default/mActionSelectPan.svg</iconset>
</property>
<property name="autoDefault">
<bool>false</bool>
</property>
</widget>
</item>
<item>
Expand Down Expand Up @@ -147,6 +199,7 @@
<tabstops>
<tabstop>mTableWidget</tabstop>
<tabstop>mFromSelectedPushButton</tabstop>
<tabstop>mFromLargestPushButton</tabstop>
<tabstop>mSkipAllButton</tabstop>
<tabstop>mRemoveFeatureFromSelectionButton</tabstop>
<tabstop>buttonBox</tabstop>
Expand Down

0 comments on commit 5dbcb00

Please sign in to comment.