Skip to content
Permalink
Browse files

Added button to select attributes from largest geometry when merging …

…features
  • Loading branch information
uclaros authored and nyalldawson committed Mar 15, 2021
1 parent e44f8a7 commit 5dbcb0060cbb791b499c52f2a96c93355f99a0ad
@@ -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>
@@ -0,0 +1 @@
<svg height="16" width="16" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink"><linearGradient id="a" gradientUnits="userSpaceOnUse" x1="4.74" x2="6.5" y1="4.842" y2="10.5"><stop offset="0" stop-color="#eee"/><stop offset="1" stop-color="#aec7e2"/></linearGradient><path d="m.5 6.5c0 12 6 2 9 2 2 0 6 4 6-1 0-8-4.398-3.19-7-4-1.942-.604-8-4-8 3z" fill="url(#a)" fill-rule="evenodd" stroke="#2b3b4d" stroke-linecap="round" stroke-linejoin="round"/></svg>
@@ -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();

@@ -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 );
}
@@ -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 )
{
@@ -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()
@@ -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 );

@@ -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 );

@@ -30,6 +30,9 @@
<property name="text">
<string/>
</property>
<property name="autoDefault">
<bool>false</bool>
</property>
</widget>
</item>
<item>
@@ -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">
@@ -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>
@@ -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">
@@ -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>
@@ -147,6 +199,7 @@
<tabstops>
<tabstop>mTableWidget</tabstop>
<tabstop>mFromSelectedPushButton</tabstop>
<tabstop>mFromLargestPushButton</tabstop>
<tabstop>mSkipAllButton</tabstop>
<tabstop>mRemoveFeatureFromSelectionButton</tabstop>
<tabstop>buttonBox</tabstop>

0 comments on commit 5dbcb00

Please sign in to comment.