QGIS API Documentation  3.0.2-Girona (307d082)
qgsnewauxiliaryfielddialog.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsnewauxiliaryfielddialog.cpp - description
3  -------------------
4  begin : Sept 05, 2017
5  copyright : (C) 2017 by Paul Blottiere
6  email : [email protected]
7  ***************************************************************************/
8 
9 /***************************************************************************
10  * *
11  * This program is free software; you can redistribute it and/or modify *
12  * it under the terms of the GNU General Public License as published by *
13  * the Free Software Foundation; either version 2 of the License, or *
14  * (at your option) any later version. *
15  * *
16  ***************************************************************************/
17 
19 #include "qgsauxiliarystorage.h"
20 
21 #include <QMessageBox>
22 
24  : QDialog( parent )
25  , mLayer( layer )
26  , mNameOnly( nameOnly )
27  , mPropertyDefinition( def )
28 {
29  setupUi( this );
30 
31  mType->addItem( tr( "String" ) );
32  mType->addItem( tr( "Real" ) );
33  mType->addItem( tr( "Integer" ) );
34 
35  switch ( def.dataType() )
36  {
38  mType->setCurrentIndex( mType->findText( tr( "String" ) ) );
39  break;
41  mType->setCurrentIndex( mType->findText( tr( "Real" ) ) );
42  break;
44  mType->setCurrentIndex( mType->findText( tr( "Integer" ) ) );
45  break;
46  }
47 
48  if ( mNameOnly )
49  mType->setEnabled( false );
50  else
51  mType->setEnabled( true );
52 }
53 
55 {
57  def.setComment( mName->text() );
58 
59  if ( !mNameOnly )
60  {
61  if ( mType->currentText().compare( tr( "String" ) ) == 0 )
62  {
64  }
65  else if ( mType->currentText().compare( tr( "Real" ) ) == 0 )
66  {
68  }
69  else
70  {
72  }
73 
74  def.setOrigin( "user" );
75  def.setName( "custom" );
76  }
77 
78  QString fieldName = QgsAuxiliaryLayer::nameFromProperty( def, true );
79  const int idx = mLayer->fields().lookupField( fieldName );
80  if ( idx >= 0 )
81  {
82  const QString title = tr( "New Auxiliary Field" );
83  const QString msg = tr( "Invalid name. Auxiliary field '%1' already exists." ).arg( fieldName );
84  QMessageBox::critical( this, title, msg, QMessageBox::Ok );
85  }
86  else if ( def.comment().isEmpty() )
87  {
88  const QString title = tr( "New Auxiliary Field" );
89  const QString msg = tr( "Name is a mandatory parameter." );
90  QMessageBox::critical( this, title, msg, QMessageBox::Ok );
91  }
92  else
93  {
94  if ( mLayer->auxiliaryLayer()->addAuxiliaryField( def ) )
95  mPropertyDefinition = def;
96  QDialog::accept();
97  }
98 }
99 
101 {
102  return mPropertyDefinition;
103 }
int lookupField(const QString &fieldName) const
Look up field&#39;s index from the field name.
Definition: qgsfields.cpp:299
QgsPropertyDefinition mPropertyDefinition
QgsNewAuxiliaryFieldDialog(const QgsPropertyDefinition &definition, QgsVectorLayer *layer, bool nameOnly=true, QWidget *parent=nullptr)
Constructor.
void setName(const QString &name)
Sets the name of the property.
Definition: qgsproperty.h:143
DataType dataType() const
Returns the allowable field/value data type for the property.
Definition: qgsproperty.h:187
QgsFields fields() const override
Returns the list of fields of this layer.
Property requires a boolean value.
Definition: qgsproperty.h:105
Property requires a numeric value.
Definition: qgsproperty.h:98
QgsAuxiliaryLayer * auxiliaryLayer()
Returns the current auxiliary layer.
void setDataType(DataType type)
Sets the data type.
Definition: qgsproperty.h:182
Definition for a property.
Definition: qgsproperty.h:46
QString comment() const
Returns the comment of the property.
Definition: qgsproperty.h:167
Property requires a string value.
Definition: qgsproperty.h:91
void setOrigin(const QString &origin)
Sets the origin of the property.
Definition: qgsproperty.h:157
static QString nameFromProperty(const QgsPropertyDefinition &def, bool joined=false)
Returns the name of the auxiliary field for a property definition.
void setComment(const QString &comment)
Sets comment of the property.
Definition: qgsproperty.h:172
bool addAuxiliaryField(const QgsPropertyDefinition &definition)
Adds an auxiliary field for the given property.
Represents a vector layer which manages a vector based data sets.
QgsPropertyDefinition propertyDefinition() const
Returns the underlying property definition.