QGIS API Documentation  3.26.3-Buenos Aires (65e4edfdad)
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 #include "qgsvariantutils.h"
23 
24 #include <QMessageBox>
25 
27  : QDialog( parent )
28  , mLayer( layer )
29  , mNameOnly( nameOnly )
30  , mPropertyDefinition( def )
31 {
32  setupUi( this );
34 
35  mType->addItem( QgsFields::iconForFieldType( QVariant::String ), QgsVariantUtils::typeToDisplayString( QVariant::String ), QgsPropertyDefinition::DataTypeString );
36  mType->addItem( QgsFields::iconForFieldType( QVariant::Double ), QgsVariantUtils::typeToDisplayString( QVariant::Double ), QgsPropertyDefinition::DataTypeNumeric );
37  mType->addItem( QgsFields::iconForFieldType( QVariant::Int ), tr( "Integer" ), QgsPropertyDefinition::DataTypeBoolean );
38 
39  mType->setCurrentIndex( mType->findData( def.dataType() ) );
40 
41  if ( mNameOnly )
42  mType->setEnabled( false );
43  else
44  mType->setEnabled( true );
45 }
46 
48 {
50  def.setComment( mName->text() );
51 
52  if ( !mNameOnly )
53  {
54  def.setDataType( static_cast< QgsPropertyDefinition::DataType >( mType->currentData().toInt() ) );
55 
56  def.setOrigin( "user" );
57  def.setName( "custom" );
58  }
59 
60  const QString fieldName = QgsAuxiliaryLayer::nameFromProperty( def, true );
61  const int idx = mLayer->fields().lookupField( fieldName );
62  if ( idx >= 0 )
63  {
64  const QString title = tr( "New Auxiliary Field" );
65  const QString msg = tr( "Invalid name. Auxiliary field '%1' already exists." ).arg( fieldName );
66  QMessageBox::critical( this, title, msg, QMessageBox::Ok );
67  }
68  else if ( def.comment().isEmpty() )
69  {
70  const QString title = tr( "New Auxiliary Field" );
71  const QString msg = tr( "Name is a mandatory parameter." );
72  QMessageBox::critical( this, title, msg, QMessageBox::Ok );
73  }
74  else
75  {
76  if ( mLayer->auxiliaryLayer()->addAuxiliaryField( def ) )
77  mPropertyDefinition = def;
78  QDialog::accept();
79  }
80 }
81 
83 {
84  return mPropertyDefinition;
85 }
QgsPropertyDefinition::DataTypeString
@ DataTypeString
Property requires a string value.
Definition: qgsproperty.h:92
qgsgui.h
QgsPropertyDefinition::DataTypeBoolean
@ DataTypeBoolean
Property requires a boolean value.
Definition: qgsproperty.h:106
QgsNewAuxiliaryFieldDialog::propertyDefinition
QgsPropertyDefinition propertyDefinition() const
Returns the underlying property definition.
Definition: qgsnewauxiliaryfielddialog.cpp:82
qgsnewauxiliaryfielddialog.h
QgsPropertyDefinition::DataTypeNumeric
@ DataTypeNumeric
Property requires a numeric value.
Definition: qgsproperty.h:99
QgsVectorLayer::auxiliaryLayer
QgsAuxiliaryLayer * auxiliaryLayer()
Returns the current auxiliary layer.
Definition: qgsvectorlayer.cpp:5520
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:180
QgsPropertyDefinition::dataType
DataType dataType() const
Returns the allowable field/value data type for the property.
Definition: qgsproperty.h:188
QgsVectorLayer::fields
QgsFields fields() const FINAL
Returns the list of fields of this layer.
Definition: qgsvectorlayer.cpp:3436
QgsPropertyDefinition::setComment
void setComment(const QString &comment)
Sets comment of the property.
Definition: qgsproperty.h:173
QgsPropertyDefinition::DataType
DataType
Valid data types required by property.
Definition: qgsproperty.h:84
QgsNewAuxiliaryFieldDialog::mNameOnly
bool mNameOnly
Definition: qgsnewauxiliaryfielddialog.h:71
qgsvariantutils.h
QgsPropertyDefinition
Definition for a property.
Definition: qgsproperty.h:46
QgsNewAuxiliaryFieldDialog::mLayer
QgsVectorLayer * mLayer
Definition: qgsnewauxiliaryfielddialog.h:70
QgsFields::iconForFieldType
static QIcon iconForFieldType(QVariant::Type type, QVariant::Type subType=QVariant::Type::Invalid)
Returns an icon corresponding to a field type.
Definition: qgsfields.cpp:294
QgsNewAuxiliaryFieldDialog::QgsNewAuxiliaryFieldDialog
QgsNewAuxiliaryFieldDialog(const QgsPropertyDefinition &definition, QgsVectorLayer *layer, bool nameOnly=true, QWidget *parent=nullptr)
Constructor.
Definition: qgsnewauxiliaryfielddialog.cpp:26
QgsPropertyDefinition::comment
QString comment() const
Returns the comment of the property.
Definition: qgsproperty.h:168
QgsPropertyDefinition::setName
void setName(const QString &name)
Sets the name of the property.
Definition: qgsproperty.h:144
QgsVariantUtils::typeToDisplayString
static QString typeToDisplayString(QVariant::Type type, QVariant::Type subType=QVariant::Type::Invalid)
Returns a user-friendly translated string representing a QVariant type.
Definition: qgsvariantutils.cpp:19
QgsVectorLayer
Represents a vector layer which manages a vector based data sets.
Definition: qgsvectorlayer.h:391
QgsNewAuxiliaryFieldDialog::mPropertyDefinition
QgsPropertyDefinition mPropertyDefinition
Definition: qgsnewauxiliaryfielddialog.h:72
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:440
QgsNewAuxiliaryFieldDialog::accept
void accept() override
Definition: qgsnewauxiliaryfielddialog.cpp:47
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:349
QgsPropertyDefinition::setDataType
void setDataType(DataType type)
Sets the data type.
Definition: qgsproperty.h:183
QgsPropertyDefinition::setOrigin
void setOrigin(const QString &origin)
Sets the origin of the property.
Definition: qgsproperty.h:158