Skip to content

Commit

Permalink
Use dedicated QgsMapLayerComboBox and QgsFieldComboBox classes
Browse files Browse the repository at this point in the history
to handle layer and fields widgets
  • Loading branch information
DelazJ authored and nyalldawson committed Nov 13, 2024
1 parent ec409aa commit a4a9ba6
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 70 deletions.
67 changes: 11 additions & 56 deletions src/gui/qgsattributetypeloaddialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
#include "qgsvectordataprovider.h"
#include "qgsproject.h"
#include "qgsvectorlayer.h"
#include "qgsiconutils.h"

#include <QTableWidgetItem>
#include <QLineEdit>
Expand All @@ -41,15 +40,16 @@ QgsAttributeTypeLoadDialog::QgsAttributeTypeLoadDialog( QgsVectorLayer *vl )
{
setupUi( this );

connect( layerComboBox, static_cast<void ( QComboBox::* )( int )>( &QComboBox::currentIndexChanged ), this, &QgsAttributeTypeLoadDialog::fillComboBoxes );
layerComboBox->setFilters( Qgis::LayerFilter::VectorLayer );
layerComboBox->setCurrentIndex( -1 );

connect( layerComboBox, &QgsMapLayerComboBox::layerChanged, keyComboBox, &QgsFieldComboBox::setLayer );
connect( layerComboBox, &QgsMapLayerComboBox::layerChanged, valueComboBox, &QgsFieldComboBox::setLayer );
connect( keyComboBox, static_cast<void ( QComboBox::* )( int )>( &QComboBox::currentIndexChanged ), this, [ = ]( int index ) { createPreview( index ); } );
connect( valueComboBox, static_cast<void ( QComboBox::* )( int )>( &QComboBox::currentIndexChanged ), this, [ = ]( int index ) { createPreview( index ); } );
connect( previewButton, &QAbstractButton::pressed, this, &QgsAttributeTypeLoadDialog::previewButtonPushed );

fillLayerList();

keyComboBox->setDisabled( true );
valueComboBox->setDisabled( true );
}

void QgsAttributeTypeLoadDialog::setVectorLayer( QgsVectorLayer *layer )
Expand All @@ -62,51 +62,6 @@ void QgsAttributeTypeLoadDialog::previewButtonPushed()
createPreview( valueComboBox->currentIndex(), true );
}

void QgsAttributeTypeLoadDialog::fillLayerList()
{
layerComboBox->blockSignals( true );
layerComboBox->clear();
const auto constMapLayers = QgsProject::instance()->mapLayers();
for ( QgsMapLayer *l : constMapLayers )
{
QgsVectorLayer *vl = qobject_cast< QgsVectorLayer * >( l );
if ( vl )
layerComboBox->addItem( QgsIconUtils::iconForLayer( vl ), vl->name(), vl->id() );
}
layerComboBox->setCurrentIndex( -1 );
layerComboBox->blockSignals( false );
}

void QgsAttributeTypeLoadDialog::fillComboBoxes( int layerIndex )
{
keyComboBox->blockSignals( true );
valueComboBox->blockSignals( true );

//clear comboboxes first
keyComboBox->clear();
valueComboBox->clear();

QgsVectorLayer *vLayer = qobject_cast<QgsVectorLayer *>( layerIndex < 0 ? nullptr : QgsProject::instance()->mapLayer( layerComboBox->itemData( layerIndex ).toString() ) );
if ( vLayer )
{
const QgsFields vLayerFields = vLayer->dataProvider()->fields();
for (int idx = 0; idx < vLayerFields.count(); ++idx )
{
keyComboBox->addItem( vLayerFields.iconForField( idx ), vLayerFields.at( idx ).name(), idx );
valueComboBox->addItem( vLayerFields.iconForField( idx ), vLayerFields.at( idx ).name(), idx );
}
}

keyComboBox->setEnabled( nullptr != vLayer );
valueComboBox->setEnabled( nullptr != vLayer );

keyComboBox->setCurrentIndex( -1 );
valueComboBox->setCurrentIndex( -1 );

keyComboBox->blockSignals( false );
valueComboBox->blockSignals( false );
}

void QgsAttributeTypeLoadDialog::createPreview( int fieldIndex, bool full )
{
previewTableWidget->clearContents();
Expand All @@ -120,9 +75,9 @@ void QgsAttributeTypeLoadDialog::createPreview( int fieldIndex, bool full )
//when nothing is selected there is no reason for preview
return;
}
const int idx = keyComboBox->currentData().toInt();
const int idx2 = valueComboBox->currentData().toInt();
QgsMapLayer *dataLayer = QgsProject::instance()->mapLayer( layerComboBox->currentData().toString() );
const int idx = keyComboBox->currentIndex();
const int idx2 = valueComboBox->currentIndex();
QgsMapLayer *dataLayer = QgsProject::instance()->mapLayer( layerComboBox->currentLayer()->id() );
QgsVectorLayer *vLayer = qobject_cast<QgsVectorLayer *>( dataLayer );
if ( !vLayer )
return;
Expand Down Expand Up @@ -169,9 +124,9 @@ bool QgsAttributeTypeLoadDialog::insertNull()
void QgsAttributeTypeLoadDialog::loadDataToValueMap()
{
mValueMap.clear();
const int idx = keyComboBox->currentData().toInt();
const int idx2 = valueComboBox->currentData().toInt();
QgsMapLayer *dataLayer = QgsProject::instance()->mapLayer( layerComboBox->currentData().toString() );
const int idx = keyComboBox->currentIndex();
const int idx2 = valueComboBox->currentIndex();
QgsMapLayer *dataLayer = QgsProject::instance()->mapLayer( layerComboBox->currentLayer()->id() );
QgsVectorLayer *vLayer = qobject_cast<QgsVectorLayer *>( dataLayer );
if ( !vLayer )
return;
Expand Down
11 changes: 0 additions & 11 deletions src/gui/qgsattributetypeloaddialog.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,6 @@ class GUI_EXPORT QgsAttributeTypeLoadDialog: public QDialog, private Ui::QgsAttr

private slots:

/**
* Slot which reacts to change of selected layer to fill other two comboboxes with correct data
* \param layerIndex index of layer which was selected
*/
void fillComboBoxes( int layerIndex );

/**
* Slot to react to button push or change of selected column for display of preview
* \param fieldIndex indexOfChangedField
Expand All @@ -88,11 +82,6 @@ class GUI_EXPORT QgsAttributeTypeLoadDialog: public QDialog, private Ui::QgsAttr

private:

/**
* Internal function to fill the list of layers
*/
void fillLayerList();

/**
* Function to transfer data from layer to value map used in editing
*/
Expand Down
18 changes: 15 additions & 3 deletions src/ui/qgsattributeloadfrommap.ui
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
</widget>
</item>
<item row="1" column="1" colspan="2">
<widget class="QComboBox" name="layerComboBox"/>
<widget class="QgsMapLayerComboBox" name="layerComboBox"/>
</item>
<item row="5" column="0" colspan="3">
<widget class="QTableWidget" name="previewTableWidget">
Expand Down Expand Up @@ -62,7 +62,7 @@
</widget>
</item>
<item row="2" column="1">
<widget class="QComboBox" name="valueComboBox"/>
<widget class="QgsFieldComboBox" name="valueComboBox"/>
</item>
<item row="2" column="2" rowspan="2">
<widget class="QPushButton" name="previewButton">
Expand All @@ -72,7 +72,7 @@
</widget>
</item>
<item row="3" column="1">
<widget class="QComboBox" name="keyComboBox"/>
<widget class="QgsFieldComboBox" name="keyComboBox"/>
</item>
<item row="4" column="0" colspan="2">
<widget class="QCheckBox" name="nullCheckBox">
Expand All @@ -95,6 +95,18 @@
</item>
</layout>
</widget>
<customwidgets>
<customwidget>
<class>QgsFieldComboBox</class>
<extends>QComboBox</extends>
<header>qgsfieldcombobox.h</header>
</customwidget>
<customwidget>
<class>QgsMapLayerComboBox</class>
<extends>QComboBox</extends>
<header>qgsmaplayercombobox.h</header>
</customwidget>
</customwidgets>
<tabstops>
<tabstop>layerComboBox</tabstop>
<tabstop>valueComboBox</tabstop>
Expand Down

0 comments on commit a4a9ba6

Please sign in to comment.