Skip to content

Commit

Permalink
Properly update layer settings when an auxiliary field is deleted
Browse files Browse the repository at this point in the history
  • Loading branch information
pblottiere committed Oct 9, 2017
1 parent b6752df commit 4fa3400
Show file tree
Hide file tree
Showing 11 changed files with 119 additions and 26 deletions.
13 changes: 13 additions & 0 deletions python/core/qgsauxiliarystorage.sip
Expand Up @@ -211,6 +211,19 @@ class QgsAuxiliaryLayer : QgsVectorLayer
:rtype: int
%End

int propertyFromField( int index ) const;
%Docstring
Returns the underlying property key for the field index. The key may be
a PAL, diagram or symbology property according to the underlying
property definition of the field. The key -1 is returned if an error
happened.

\param index The index of the field

.. versionadded:: 3.0
:rtype: int
%End

static int createProperty( QgsPalLayerSettings::Property property, const QString &providerId, QgsVectorLayer *vlayer );
%Docstring
Create if necessary a new auxiliary field for a PAL property and
Expand Down
7 changes: 0 additions & 7 deletions python/gui/qgspropertyoverridebutton.sip
Expand Up @@ -180,13 +180,6 @@ class QgsPropertyOverrideButton: QToolButton
%Docstring
Updates list of fields.

.. versionadded:: 3.0
%End

void updateGui();
%Docstring
Updates data defined button icon.

.. versionadded:: 3.0
%End

Expand Down
25 changes: 16 additions & 9 deletions src/app/qgslabelinggui.cpp
Expand Up @@ -50,6 +50,8 @@ void QgsLabelingGui::registerDataDefinedButton( QgsPropertyOverrideButton *butto
connect( button, &QgsPropertyOverrideButton::changed, this, &QgsLabelingGui::updateProperty );
connect( button, &QgsPropertyOverrideButton::createAuxiliaryField, this, &QgsLabelingGui::createAuxiliaryField );
button->registerExpressionContextGenerator( this );

mButtons[key] = button;
}

void QgsLabelingGui::updateProperty()
Expand Down Expand Up @@ -611,15 +613,6 @@ void QgsLabelingGui::updateUi()
{
chkMergeLines->setToolTip( QString() );
}

mCoordXDDBtn->updateFieldLists();
mCoordXDDBtn->updateGui();

mCoordYDDBtn->updateFieldLists();
mCoordYDDBtn->updateGui();

mCoordRotationDDBtn->updateFieldLists();
mCoordRotationDDBtn->updateGui();
}

void QgsLabelingGui::createAuxiliaryField()
Expand Down Expand Up @@ -653,3 +646,17 @@ void QgsLabelingGui::createAuxiliaryField()

emit auxiliaryFieldCreated();
}

void QgsLabelingGui::deactivateField( QgsPalLayerSettings::Property key )
{
if ( mButtons.contains( key ) )
{
QgsPropertyOverrideButton *button = mButtons[ key ];
QgsProperty p = button->toProperty();
p.setField( QString() );
p.setActive( false );
button->updateFieldLists();
button->setToProperty( p );
mDataDefinedProperties.setProperty( key, p );
}
}
12 changes: 12 additions & 0 deletions src/app/qgslabelinggui.h
Expand Up @@ -43,6 +43,16 @@ class APP_EXPORT QgsLabelingGui : public QgsTextFormatWidget, private QgsExpress

void setLayer( QgsMapLayer *layer );

/**
* Deactivate a field from data defined properties and update the
* corresponding button.
*
* \param key The property key to deactivate
*
* \since QGIS 3.0
*/
void deactivateField( QgsPalLayerSettings::Property key );

signals:

void auxiliaryFieldCreated();
Expand All @@ -68,6 +78,8 @@ class APP_EXPORT QgsLabelingGui : public QgsTextFormatWidget, private QgsExpress
void populateDataDefinedButtons();
void registerDataDefinedButton( QgsPropertyOverrideButton *button, QgsPalLayerSettings::Property key );

QMap<QgsPalLayerSettings::Property, QgsPropertyOverrideButton *> mButtons;

private slots:

void updateProperty();
Expand Down
5 changes: 5 additions & 0 deletions src/app/qgslabelingwidget.cpp
Expand Up @@ -40,6 +40,11 @@ QgsLabelingWidget::QgsLabelingWidget( QgsVectorLayer *layer, QgsMapCanvas *canva
setLayer( layer );
}

QgsLabelingGui *QgsLabelingWidget::labelingGui()
{
return qobject_cast<QgsLabelingGui *>( mWidget );
}

void QgsLabelingWidget::resetSettings()
{
if ( mOldSettings )
Expand Down
7 changes: 7 additions & 0 deletions src/app/qgslabelingwidget.h
Expand Up @@ -38,6 +38,13 @@ class QgsLabelingWidget : public QgsMapLayerConfigWidget, private Ui::QgsLabelin
public:
QgsLabelingWidget( QgsVectorLayer *layer, QgsMapCanvas *canvas, QWidget *parent = nullptr );

/**
* Returns the labeling gui widget or a nullptr if none.
*
* \since QGIS 3.0
*/
QgsLabelingGui *labelingGui();

public slots:
void setLayer( QgsMapLayer *layer );
//! save config to layer
Expand Down
32 changes: 29 additions & 3 deletions src/app/qgsvectorlayerproperties.cpp
Expand Up @@ -57,6 +57,7 @@
#include "qgsstyle.h"
#include "qgsauxiliarystorage.h"
#include "qgsnewauxiliarylayerdialog.h"
#include "qgslabelinggui.h"

#include "layertree/qgslayertreelayer.h"
#include "qgslayertree.h"
Expand Down Expand Up @@ -1555,7 +1556,7 @@ void QgsVectorLayerProperties::updateAuxiliaryStoragePage( bool reset )

if ( reset && labelingDialog )
{
labelingDialog->resetSettings(); // update data defined buttons
labelingDialog->setLayer( mLayer );
}
}

Expand Down Expand Up @@ -1608,6 +1609,15 @@ void QgsVectorLayerProperties::onAuxiliaryLayerDelete()
{
QApplication::setOverrideCursor( Qt::WaitCursor );
QgsDataSourceUri uri( alayer->source() );

// delete each attribute to correctly update layer settings and data
// defined buttons
while ( alayer->auxiliaryFields().size() > 0 )
{
QgsAuxiliaryField aField = alayer->auxiliaryFields()[0];
deleteAuxiliaryField( alayer->fields().indexOf( aField.name() ) );
}

mLayer->setAuxiliaryLayer(); // remove auxiliary layer
QgsAuxiliaryStorage::deleteTable( uri );
QApplication::restoreOverrideCursor();
Expand Down Expand Up @@ -1659,11 +1669,27 @@ void QgsVectorLayerProperties::onAuxiliaryLayerDeleteField()
if ( reply == QMessageBox::Yes )
{
QApplication::setOverrideCursor( Qt::WaitCursor );
mLayer->auxiliaryLayer()->deleteAttribute( index );
deleteAuxiliaryField( index );
mLayer->triggerRepaint();
QApplication::restoreOverrideCursor();
}
}

void QgsVectorLayerProperties::deleteAuxiliaryField( int index )
{
if ( !mLayer->auxiliaryLayer() )
return;

int key = mLayer->auxiliaryLayer()->propertyFromField( index );
if ( mLayer->auxiliaryLayer()->deleteAttribute( index ) )
{
mLayer->updateFields();

// immediately deactivate data defined button
if ( labelingDialog && labelingDialog->labelingGui() )
labelingDialog->labelingGui()->deactivateField( ( QgsPalLayerSettings::Property ) key );

updateAuxiliaryStoragePage( true );
mFieldsPropertiesDialog->init();
mLayer->triggerRepaint();
}
}
1 change: 1 addition & 0 deletions src/app/qgsvectorlayerproperties.h
Expand Up @@ -217,6 +217,7 @@ class APP_EXPORT QgsVectorLayerProperties : public QgsOptionsDialogBase, private
void addJoinToTreeWidget( const QgsVectorLayerJoinInfo &join, const int insertIndex = -1 );

void updateAuxiliaryStoragePage( bool reset = false );
void deleteAuxiliaryField( int index );

QgsExpressionContext mContext;

Expand Down
23 changes: 23 additions & 0 deletions src/core/qgsauxiliarystorage.cpp
Expand Up @@ -405,6 +405,29 @@ bool QgsAuxiliaryLayer::isHiddenProperty( int index ) const
return hidden;
}

int QgsAuxiliaryLayer::propertyFromField( int index ) const
{
int p = -1;
QgsAuxiliaryField aField( fields().field( index ) );
QgsPropertyDefinition aDef = aField.propertyDefinition();

if ( aDef.origin().compare( "labeling" ) == 0 )
{
const QgsPropertiesDefinition defs = QgsPalLayerSettings::propertyDefinitions();
QgsPropertiesDefinition::const_iterator it = defs.constBegin();
for ( ; it != defs.constEnd(); ++it )
{
if ( it->name().compare( aDef.name(), Qt::CaseInsensitive ) == 0 )
{
p = it.key();
break;
}
}
}

return p;
}

int QgsAuxiliaryLayer::indexOfProperty( const QgsPropertyDefinition &def ) const
{
return fields().indexOf( QgsAuxiliaryField::nameFromProperty( def ) );
Expand Down
12 changes: 12 additions & 0 deletions src/core/qgsauxiliarystorage.h
Expand Up @@ -228,6 +228,18 @@ class CORE_EXPORT QgsAuxiliaryLayer : public QgsVectorLayer
*/
int indexOfProperty( const QgsPropertyDefinition &definition ) const;

/**
* Returns the underlying property key for the field index. The key may be
* a PAL, diagram or symbology property according to the underlying
* property definition of the field. The key -1 is returned if an error
* happened.
*
* \param index The index of the field
*
* \since QGIS 3.0
*/
int propertyFromField( int index ) const;

/**
* Create if necessary a new auxiliary field for a PAL property and
* activate this property in settings.
Expand Down
8 changes: 1 addition & 7 deletions src/gui/qgspropertyoverridebutton.h
Expand Up @@ -192,13 +192,6 @@ class GUI_EXPORT QgsPropertyOverrideButton: public QToolButton
*/
void updateFieldLists();

/**
* Updates data defined button icon.
*
* \since QGIS 3.0
*/
void updateGui();

/**
* Sets a symbol which can be used for previews inside the widget or in any dialog created
* by the widget. If not specified, a default created symbol will be used instead.
Expand Down Expand Up @@ -232,6 +225,7 @@ class GUI_EXPORT QgsPropertyOverrideButton: public QToolButton
void showDescriptionDialog();
void showExpressionDialog();
void showAssistant();
void updateGui();

/**
* Sets the active status, emitting the activated signal where necessary (but never emitting the changed signal!).
Expand Down

0 comments on commit 4fa3400

Please sign in to comment.