Skip to content

Commit fc0ebb7

Browse files
committedOct 9, 2017
Allow user to create its own auxiliary field
1 parent 4e10a11 commit fc0ebb7

File tree

7 files changed

+76
-7
lines changed

7 files changed

+76
-7
lines changed
 

‎python/core/qgsproperty.sip

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,11 @@ class QgsPropertyDefinition
139139
:rtype: str
140140
%End
141141

142+
void setDataType( DataType type );
143+
%Docstring
144+
Sets the data type
145+
%End
146+
142147
DataType dataType() const;
143148
%Docstring
144149
Returns the allowable field/value data type for the property.

‎src/app/qgsvectorlayerproperties.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@
5757
#include "qgsstyle.h"
5858
#include "qgsauxiliarystorage.h"
5959
#include "qgsnewauxiliarylayerdialog.h"
60+
#include "qgsnewauxiliaryfielddialog.h"
6061
#include "qgslabelinggui.h"
6162
#include "qgssymbollayer.h"
6263

@@ -90,6 +91,7 @@ QgsVectorLayerProperties::QgsVectorLayerProperties(
9091
, mAuxiliaryLayerActionDelete( nullptr )
9192
, mAuxiliaryLayerActionExport( nullptr )
9293
, mAuxiliaryLayerActionDeleteField( nullptr )
94+
, mAuxiliaryLayerActionAddField( nullptr )
9395
{
9496
setupUi( this );
9597
connect( mLayerOrigNameLineEdit, &QLineEdit::textEdited, this, &QgsVectorLayerProperties::mLayerOrigNameLineEdit_textEdited );
@@ -382,6 +384,7 @@ QgsVectorLayerProperties::QgsVectorLayerProperties(
382384
mAuxiliaryStorageActions->setMenu( menu );
383385

384386
connect( mAuxiliaryStorageFieldsDeleteBtn, &QPushButton::clicked, this, &QgsVectorLayerProperties::onAuxiliaryLayerDeleteField );
387+
connect( mAuxiliaryStorageFieldsAddBtn, &QPushButton::clicked, this, &QgsVectorLayerProperties::onAuxiliaryLayerAddField );
385388

386389
updateAuxiliaryStoragePage();
387390
}
@@ -1679,6 +1682,19 @@ void QgsVectorLayerProperties::onAuxiliaryLayerDeleteField()
16791682
}
16801683
}
16811684

1685+
void QgsVectorLayerProperties::onAuxiliaryLayerAddField()
1686+
{
1687+
QgsAuxiliaryLayer *alayer = mLayer->auxiliaryLayer();
1688+
if ( !alayer )
1689+
return;
1690+
1691+
QgsNewAuxiliaryFieldDialog dlg( QgsPropertyDefinition(), mLayer, false );
1692+
if ( dlg.exec() == QDialog::Accepted )
1693+
{
1694+
updateAuxiliaryStoragePage();
1695+
}
1696+
}
1697+
16821698
void QgsVectorLayerProperties::deleteAuxiliaryField( int index )
16831699
{
16841700
if ( !mLayer->auxiliaryLayer() )

‎src/app/qgsvectorlayerproperties.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,8 @@ class APP_EXPORT QgsVectorLayerProperties : public QgsOptionsDialogBase, private
164164

165165
void onAuxiliaryLayerDeleteField();
166166

167+
void onAuxiliaryLayerAddField();
168+
167169
void onAuxiliaryLayerExport();
168170

169171
private:
@@ -235,6 +237,7 @@ class APP_EXPORT QgsVectorLayerProperties : public QgsOptionsDialogBase, private
235237
QAction *mAuxiliaryLayerActionDelete = nullptr;
236238
QAction *mAuxiliaryLayerActionExport = nullptr;
237239
QAction *mAuxiliaryLayerActionDeleteField = nullptr;
240+
QAction *mAuxiliaryLayerActionAddField = nullptr;
238241

239242
private slots:
240243
void openPanel( QgsPanelWidget *panel );

‎src/core/qgsauxiliarystorage.cpp

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,8 @@ QgsAuxiliaryField::QgsAuxiliaryField( const QgsField &f )
8181
if ( p.name().compare( propertyName, Qt::CaseInsensitive ) == 0 )
8282
{
8383
def = p;
84+
if ( parts.size() == 3 )
85+
def.setComment( parts[2] );
8486
break;
8587
}
8688
}
@@ -93,6 +95,8 @@ QgsAuxiliaryField::QgsAuxiliaryField( const QgsField &f )
9395
if ( p.name().compare( propertyName, Qt::CaseInsensitive ) == 0 )
9496
{
9597
def = p;
98+
if ( parts.size() == 3 )
99+
def.setComment( parts[2] );
96100
break;
97101
}
98102
}
@@ -105,17 +109,19 @@ QgsAuxiliaryField::QgsAuxiliaryField( const QgsField &f )
105109
if ( p.name().compare( propertyName, Qt::CaseInsensitive ) == 0 )
106110
{
107111
def = p;
112+
if ( parts.size() == 3 )
113+
def.setComment( parts[2] );
108114
break;
109115
}
110116
}
111117
}
112-
113-
if ( parts.size() == 3 )
118+
else if ( origin.compare( "user", Qt::CaseInsensitive ) == 0 )
114119
{
115-
def.setComment( parts[2] );
120+
def.setOrigin( "user" );
121+
def.setComment( propertyName );
116122
}
117123

118-
if ( !def.name().isEmpty() )
124+
if ( !def.name().isEmpty() || !def.comment().isEmpty() )
119125
{
120126
init( def );
121127
setTypeName( f.typeName() );
@@ -125,7 +131,7 @@ QgsAuxiliaryField::QgsAuxiliaryField( const QgsField &f )
125131

126132
void QgsAuxiliaryField::init( const QgsPropertyDefinition &def )
127133
{
128-
if ( !def.name().isEmpty() )
134+
if ( !def.name().isEmpty() || !def.comment().isEmpty() )
129135
{
130136
QVariant::Type type;
131137
QString typeName;
@@ -167,7 +173,10 @@ bool QgsAuxiliaryLayer::clear()
167173

168174
QString QgsAuxiliaryField::nameFromProperty( const QgsPropertyDefinition &def, bool joined )
169175
{
170-
QString fieldName = QString( "%1_%2" ).arg( def.origin(), def.name().toLower() );
176+
QString fieldName = def.origin();
177+
178+
if ( !def.name().isEmpty() )
179+
fieldName = QString( "%1_%2" ).arg( fieldName, def.name().toLower() );
171180

172181
if ( !def.comment().isEmpty() )
173182
fieldName = QString( "%1_%2" ).arg( fieldName ).arg( def.comment() );
@@ -253,7 +262,7 @@ bool QgsAuxiliaryLayer::exists( const QgsPropertyDefinition &definition ) const
253262

254263
bool QgsAuxiliaryLayer::addAuxiliaryField( const QgsPropertyDefinition &definition )
255264
{
256-
if ( definition.name().isEmpty() || exists( definition ) )
265+
if ( ( definition.name().isEmpty() && definition.comment().isEmpty() ) || exists( definition ) )
257266
return false;
258267

259268
const QgsAuxiliaryField af( definition );

‎src/core/qgsproperty.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,11 @@ class CORE_EXPORT QgsPropertyDefinition
172172
*/
173173
QString helpText() const { return mHelpText; }
174174

175+
/**
176+
* Sets the data type
177+
*/
178+
void setDataType( DataType type ) { mTypes = type; }
179+
175180
/**
176181
* Returns the allowable field/value data type for the property.
177182
*/

‎src/gui/qgsnewauxiliaryfielddialog.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,13 +47,33 @@ QgsNewAuxiliaryFieldDialog::QgsNewAuxiliaryFieldDialog( const QgsPropertyDefinit
4747

4848
if ( mNameOnly )
4949
mType->setEnabled( false );
50+
else
51+
mType->setEnabled( true );
5052
}
5153

5254
void QgsNewAuxiliaryFieldDialog::accept()
5355
{
5456
QgsPropertyDefinition def = mPropertyDefinition;
5557
def.setComment( mName->text() );
5658

59+
if ( !mNameOnly )
60+
{
61+
if ( mType->currentText().compare( tr( "String" ) ) == 0 )
62+
{
63+
def.setDataType( QgsPropertyDefinition::DataTypeString );
64+
}
65+
else if ( mType->currentText().compare( tr( "Numeric" ) ) == 0 )
66+
{
67+
def.setDataType( QgsPropertyDefinition::DataTypeNumeric );
68+
}
69+
else
70+
{
71+
def.setDataType( QgsPropertyDefinition::DataTypeBoolean );
72+
}
73+
74+
def.setOrigin( "user" );
75+
}
76+
5777
QString fieldName = QgsAuxiliaryField::nameFromProperty( def, true );
5878
const int idx = mLayer->fields().lookupField( fieldName );
5979
if ( idx >= 0 )

‎src/ui/qgsvectorlayerpropertiesbase.ui

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2153,6 +2153,17 @@ border-radius: 2px;</string>
21532153
</property>
21542154
</spacer>
21552155
</item>
2156+
<item>
2157+
<widget class="QPushButton" name="mAuxiliaryStorageFieldsAddBtn">
2158+
<property name="text">
2159+
<string/>
2160+
</property>
2161+
<property name="icon">
2162+
<iconset resource="../../images/images.qrc">
2163+
<normaloff>:/images/themes/default/symbologyAdd.svg</normaloff>:/images/themes/default/symbologyAdd.svg</iconset>
2164+
</property>
2165+
</widget>
2166+
</item>
21562167
<item>
21572168
<widget class="QPushButton" name="mAuxiliaryStorageFieldsDeleteBtn">
21582169
<property name="text">

0 commit comments

Comments
 (0)
Please sign in to comment.