QGIS API Documentation  3.4.15-Madeira (e83d02e274)
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 #include "qgsgui.h"
21 
22 #include <QMessageBox>
23 
25  : QDialog( parent )
26  , mLayer( layer )
27  , mNameOnly( nameOnly )
28  , mPropertyDefinition( def )
29 {
30  setupUi( this );
32 
33  mType->addItem( tr( "String" ) );
34  mType->addItem( tr( "Real" ) );
35  mType->addItem( tr( "Integer" ) );
36 
37  switch ( def.dataType() )
38  {
40  mType->setCurrentIndex( mType->findText( tr( "String" ) ) );
41  break;
43  mType->setCurrentIndex( mType->findText( tr( "Real" ) ) );
44  break;
46  mType->setCurrentIndex( mType->findText( tr( "Integer" ) ) );
47  break;
48  }
49 
50  if ( mNameOnly )
51  mType->setEnabled( false );
52  else
53  mType->setEnabled( true );
54 }
55 
57 {
59  def.setComment( mName->text() );
60 
61  if ( !mNameOnly )
62  {
63  if ( mType->currentText().compare( tr( "String" ) ) == 0 )
64  {
66  }
67  else if ( mType->currentText().compare( tr( "Real" ) ) == 0 )
68  {
70  }
71  else
72  {
74  }
75 
76  def.setOrigin( "user" );
77  def.setName( "custom" );
78  }
79 
80  QString fieldName = QgsAuxiliaryLayer::nameFromProperty( def, true );
81  const int idx = mLayer->fields().lookupField( fieldName );
82  if ( idx >= 0 )
83  {
84  const QString title = tr( "New Auxiliary Field" );
85  const QString msg = tr( "Invalid name. Auxiliary field '%1' already exists." ).arg( fieldName );
86  QMessageBox::critical( this, title, msg, QMessageBox::Ok );
87  }
88  else if ( def.comment().isEmpty() )
89  {
90  const QString title = tr( "New Auxiliary Field" );
91  const QString msg = tr( "Name is a mandatory parameter." );
92  QMessageBox::critical( this, title, msg, QMessageBox::Ok );
93  }
94  else
95  {
96  if ( mLayer->auxiliaryLayer()->addAuxiliaryField( def ) )
97  mPropertyDefinition = def;
98  QDialog::accept();
99  }
100 }
101 
103 {
104  return mPropertyDefinition;
105 }
QgsPropertyDefinition mPropertyDefinition
QgsNewAuxiliaryFieldDialog(const QgsPropertyDefinition &definition, QgsVectorLayer *layer, bool nameOnly=true, QWidget *parent=nullptr)
Constructor.
DataType dataType() const
Returns the allowable field/value data type for the property.
Definition: qgsproperty.h:187
static QgsGui * instance()
Returns a pointer to the singleton instance.
Definition: qgsgui.cpp:47
void setName(const QString &name)
Sets the name of the property.
Definition: qgsproperty.h:143
QgsFields fields() const FINAL
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
int lookupField(const QString &fieldName) const
Looks up field&#39;s index from the field name.
Definition: qgsfields.cpp:320
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
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.
QgsPropertyDefinition propertyDefinition() const
Returns the underlying property definition.
void setComment(const QString &comment)
Sets comment of the property.
Definition: qgsproperty.h:172
static void enableAutoGeometryRestore(QWidget *widget, const QString &key=QString())
Register the widget to allow its position to be automatically saved and restored when open and closed...
Definition: qgsgui.cpp:98
QString comment() const
Returns the comment of the property.
Definition: qgsproperty.h:167
bool addAuxiliaryField(const QgsPropertyDefinition &definition)
Adds an auxiliary field for the given property.
Represents a vector layer which manages a vector based data sets.