QGIS API Documentation  3.16.0-Hannover (43b64b13f3)
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 #include "qgsapplication.h"
22 
23 #include <QMessageBox>
24 
26  : QDialog( parent )
27  , mLayer( layer )
28  , mNameOnly( nameOnly )
29  , mPropertyDefinition( def )
30 {
31  setupUi( this );
33 
34  mType->addItem( QgsApplication::getThemeIcon( "/mIconFieldText.svg" ), tr( "String" ), QgsPropertyDefinition::DataTypeString );
35  mType->addItem( QgsApplication::getThemeIcon( "/mIconFieldFloat.svg" ), tr( "Real" ), QgsPropertyDefinition::DataTypeNumeric );
36  mType->addItem( QgsApplication::getThemeIcon( "/mIconFieldInteger.svg" ), tr( "Integer" ), QgsPropertyDefinition::DataTypeBoolean );
37 
38  mType->setCurrentIndex( mType->findData( def.dataType() ) );
39 
40  if ( mNameOnly )
41  mType->setEnabled( false );
42  else
43  mType->setEnabled( true );
44 }
45 
47 {
49  def.setComment( mName->text() );
50 
51  if ( !mNameOnly )
52  {
53  def.setDataType( static_cast< QgsPropertyDefinition::DataType >( mType->currentData().toInt() ) );
54 
55  def.setOrigin( "user" );
56  def.setName( "custom" );
57  }
58 
59  QString fieldName = QgsAuxiliaryLayer::nameFromProperty( def, true );
60  const int idx = mLayer->fields().lookupField( fieldName );
61  if ( idx >= 0 )
62  {
63  const QString title = tr( "New Auxiliary Field" );
64  const QString msg = tr( "Invalid name. Auxiliary field '%1' already exists." ).arg( fieldName );
65  QMessageBox::critical( this, title, msg, QMessageBox::Ok );
66  }
67  else if ( def.comment().isEmpty() )
68  {
69  const QString title = tr( "New Auxiliary Field" );
70  const QString msg = tr( "Name is a mandatory parameter." );
71  QMessageBox::critical( this, title, msg, QMessageBox::Ok );
72  }
73  else
74  {
75  if ( mLayer->auxiliaryLayer()->addAuxiliaryField( def ) )
76  mPropertyDefinition = def;
77  QDialog::accept();
78  }
79 }
80 
82 {
83  return mPropertyDefinition;
84 }
QgsApplication::getThemeIcon
static QIcon getThemeIcon(const QString &name)
Helper to get a theme icon.
Definition: qgsapplication.cpp:626
QgsPropertyDefinition::DataTypeString
@ DataTypeString
Property requires a string value.
Definition: qgsproperty.h:93
qgsgui.h
QgsPropertyDefinition::DataTypeBoolean
@ DataTypeBoolean
Property requires a boolean value.
Definition: qgsproperty.h:107
QgsNewAuxiliaryFieldDialog::propertyDefinition
QgsPropertyDefinition propertyDefinition() const
Returns the underlying property definition.
Definition: qgsnewauxiliaryfielddialog.cpp:81
qgsnewauxiliaryfielddialog.h
QgsPropertyDefinition::DataTypeNumeric
@ DataTypeNumeric
Property requires a numeric value.
Definition: qgsproperty.h:100
QgsVectorLayer::auxiliaryLayer
QgsAuxiliaryLayer * auxiliaryLayer()
Returns the current auxiliary layer.
Definition: qgsvectorlayer.cpp:5293
qgsauxiliarystorage.h
qgsapplication.h
QgsGui::enableAutoGeometryRestore
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:139
QgsPropertyDefinition::dataType
DataType dataType() const
Returns the allowable field/value data type for the property.
Definition: qgsproperty.h:189
QgsVectorLayer::fields
QgsFields fields() const FINAL
Returns the list of fields of this layer.
Definition: qgsvectorlayer.cpp:3283
QgsPropertyDefinition::setComment
void setComment(const QString &comment)
Sets comment of the property.
Definition: qgsproperty.h:174
QgsPropertyDefinition::DataType
DataType
Valid data types required by property.
Definition: qgsproperty.h:86
QgsNewAuxiliaryFieldDialog::mNameOnly
bool mNameOnly
Definition: qgsnewauxiliaryfielddialog.h:58
QgsPropertyDefinition
Definition for a property.
Definition: qgsproperty.h:48
QgsNewAuxiliaryFieldDialog::mLayer
QgsVectorLayer * mLayer
Definition: qgsnewauxiliaryfielddialog.h:57
QgsNewAuxiliaryFieldDialog::QgsNewAuxiliaryFieldDialog
QgsNewAuxiliaryFieldDialog(const QgsPropertyDefinition &definition, QgsVectorLayer *layer, bool nameOnly=true, QWidget *parent=nullptr)
Constructor.
Definition: qgsnewauxiliaryfielddialog.cpp:25
QgsPropertyDefinition::comment
QString comment() const
Returns the comment of the property.
Definition: qgsproperty.h:169
QgsPropertyDefinition::setName
void setName(const QString &name)
Sets the name of the property.
Definition: qgsproperty.h:145
QgsGui::instance
static QgsGui * instance()
Returns a pointer to the singleton instance.
Definition: qgsgui.cpp:63
QgsVectorLayer
Represents a vector layer which manages a vector based data sets.
Definition: qgsvectorlayer.h:387
QgsNewAuxiliaryFieldDialog::mPropertyDefinition
QgsPropertyDefinition mPropertyDefinition
Definition: qgsnewauxiliaryfielddialog.h:59
QgsAuxiliaryLayer::nameFromProperty
static QString nameFromProperty(const QgsPropertyDefinition &def, bool joined=false)
Returns the name of the auxiliary field for a property definition.
Definition: qgsauxiliarystorage.cpp:366
QgsNewAuxiliaryFieldDialog::accept
void accept() override
Definition: qgsnewauxiliaryfielddialog.cpp:46
QgsAuxiliaryLayer::addAuxiliaryField
bool addAuxiliaryField(const QgsPropertyDefinition &definition)
Adds an auxiliary field for the given property.
Definition: qgsauxiliarystorage.cpp:140
QgsFields::lookupField
int lookupField(const QString &fieldName) const
Looks up field's index from the field name.
Definition: qgsfields.cpp:344
QgsPropertyDefinition::setDataType
void setDataType(DataType type)
Sets the data type.
Definition: qgsproperty.h:184
QgsPropertyDefinition::setOrigin
void setOrigin(const QString &origin)
Sets the origin of the property.
Definition: qgsproperty.h:159