QGIS API Documentation 3.41.0-Master (cea29feecf2)
Loading...
Searching...
No Matches
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
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 "moc_qgsnewauxiliaryfielddialog.cpp"
20#include "qgsauxiliarystorage.h"
21#include "qgsgui.h"
22#include "qgsapplication.h"
23#include "qgsvariantutils.h"
24
25#include <QMessageBox>
26
28 : QDialog( parent )
29 , mLayer( layer )
30 , mNameOnly( nameOnly )
31 , mPropertyDefinition( def )
32{
33 setupUi( this );
35
36 mType->addItem( QgsFields::iconForFieldType( QMetaType::Type::QString ), QgsVariantUtils::typeToDisplayString( QMetaType::Type::QString ), QgsPropertyDefinition::DataTypeString );
37 mType->addItem( QgsFields::iconForFieldType( QMetaType::Type::Double ), QgsVariantUtils::typeToDisplayString( QMetaType::Type::Double ), QgsPropertyDefinition::DataTypeNumeric );
38 mType->addItem( QgsFields::iconForFieldType( QMetaType::Type::Int ), tr( "Integer" ), QgsPropertyDefinition::DataTypeBoolean );
39
40 mType->setCurrentIndex( mType->findData( def.dataType() ) );
41
42 if ( mNameOnly )
43 mType->setEnabled( false );
44 else
45 mType->setEnabled( true );
46}
47
49{
51 def.setComment( mName->text() );
52
53 if ( !mNameOnly )
54 {
55 def.setDataType( static_cast<QgsPropertyDefinition::DataType>( mType->currentData().toInt() ) );
56
57 def.setOrigin( "user" );
58 def.setName( "custom" );
59 }
60
61 const QString fieldName = QgsAuxiliaryLayer::nameFromProperty( def, true );
62 const int idx = mLayer->fields().lookupField( fieldName );
63 if ( idx >= 0 )
64 {
65 const QString title = tr( "New Auxiliary Field" );
66 const QString msg = tr( "Invalid name. Auxiliary field '%1' already exists." ).arg( fieldName );
67 QMessageBox::critical( this, title, msg, QMessageBox::Ok );
68 }
69 else if ( def.comment().isEmpty() )
70 {
71 const QString title = tr( "New Auxiliary Field" );
72 const QString msg = tr( "Name is a mandatory parameter." );
73 QMessageBox::critical( this, title, msg, QMessageBox::Ok );
74 }
75 else
76 {
79 QDialog::accept();
80 }
81}
82
static QString nameFromProperty(const QgsPropertyDefinition &def, bool joined=false)
Returns the name of the auxiliary field for a property definition.
bool addAuxiliaryField(const QgsPropertyDefinition &definition)
Adds an auxiliary field for the given property.
static QIcon iconForFieldType(QMetaType::Type type, QMetaType::Type subType=QMetaType::Type::UnknownType, const QString &typeString=QString())
Returns an icon corresponding to a field type.
Q_INVOKABLE int lookupField(const QString &fieldName) const
Looks up field's index from the field name.
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:210
QgsPropertyDefinition mPropertyDefinition
QgsPropertyDefinition propertyDefinition() const
Returns the underlying property definition.
QgsNewAuxiliaryFieldDialog(const QgsPropertyDefinition &definition, QgsVectorLayer *layer, bool nameOnly=true, QWidget *parent=nullptr)
Constructor.
Definition for a property.
Definition qgsproperty.h:45
QString comment() const
Returns the comment of the property.
DataType dataType() const
Returns the allowable field/value data type for the property.
void setOrigin(const QString &origin)
Sets the origin of the property.
void setDataType(DataType type)
Sets the data type.
void setName(const QString &name)
Sets the name of the property.
void setComment(const QString &comment)
Sets comment of the property.
DataType
Valid data types required by property.
Definition qgsproperty.h:83
@ DataTypeString
Property requires a string value.
Definition qgsproperty.h:90
@ DataTypeBoolean
Property requires a boolean value.
@ DataTypeNumeric
Property requires a numeric value.
Definition qgsproperty.h:97
static QString typeToDisplayString(QMetaType::Type type, QMetaType::Type subType=QMetaType::Type::UnknownType)
Returns a user-friendly translated string representing a QVariant type.
Represents a vector layer which manages a vector based data sets.
QgsAuxiliaryLayer * auxiliaryLayer()
Returns the current auxiliary layer.