-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqgsaddattrdialog.cpp
128 lines (110 loc) · 5.02 KB
/
qgsaddattrdialog.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
/***************************************************************************
qgsaddattrdialog.h - description
-------------------
begin : January 2005
copyright : (C) 2005 by Marco Hugentobler
email : [email protected]
***************************************************************************/
/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#include "qgsaddattrdialog.h"
#include "qgsvectorlayer.h"
#include "qgsvectordataprovider.h"
#include "qgslogger.h"
#include <QMessageBox>
QgsAddAttrDialog::QgsAddAttrDialog( QgsVectorLayer *vlayer, QWidget *parent, Qt::WindowFlags fl )
: QDialog( parent, fl )
, mIsShapeFile( vlayer && vlayer->providerType() == QLatin1String( "ogr" ) && vlayer->storageType() == QLatin1String( "ESRI Shapefile" ) )
{
setupUi( this );
if ( !vlayer )
return;
//fill data types into the combo box
const QList< QgsVectorDataProvider::NativeType > &typelist = vlayer->dataProvider()->nativeTypes();
for ( int i = 0; i < typelist.size(); i++ )
{
QgsDebugMsg( QString( "name:%1 type:%2 typeName:%3 length:%4-%5 prec:%6-%7" )
.arg( typelist[i].mTypeDesc )
.arg( typelist[i].mType )
.arg( typelist[i].mTypeName )
.arg( typelist[i].mMinLen ).arg( typelist[i].mMaxLen )
.arg( typelist[i].mMinPrec ).arg( typelist[i].mMaxPrec ) );
mTypeBox->addItem( typelist[i].mTypeDesc );
mTypeBox->setItemData( i, static_cast<int>( typelist[i].mType ), Qt::UserRole );
mTypeBox->setItemData( i, typelist[i].mTypeName, Qt::UserRole + 1 );
mTypeBox->setItemData( i, typelist[i].mMinLen, Qt::UserRole + 2 );
mTypeBox->setItemData( i, typelist[i].mMaxLen, Qt::UserRole + 3 );
mTypeBox->setItemData( i, typelist[i].mMinPrec, Qt::UserRole + 4 );
mTypeBox->setItemData( i, typelist[i].mMaxPrec, Qt::UserRole + 5 );
}
on_mTypeBox_currentIndexChanged( 0 );
if ( mIsShapeFile )
mNameEdit->setMaxLength( 10 );
}
void QgsAddAttrDialog::on_mTypeBox_currentIndexChanged( int idx )
{
mTypeName->setText( mTypeBox->itemData( idx, Qt::UserRole + 1 ).toString() );
mLength->setMinimum( mTypeBox->itemData( idx, Qt::UserRole + 2 ).toInt() );
mLength->setMaximum( mTypeBox->itemData( idx, Qt::UserRole + 3 ).toInt() );
mLength->setVisible( mLength->minimum() < mLength->maximum() );
mLengthLabel->setVisible( mLength->minimum() < mLength->maximum() );
if ( mLength->value() < mLength->minimum() )
mLength->setValue( mLength->minimum() );
if ( mLength->value() > mLength->maximum() )
mLength->setValue( mLength->maximum() );
setPrecisionMinMax();
}
void QgsAddAttrDialog::on_mLength_editingFinished()
{
setPrecisionMinMax();
}
void QgsAddAttrDialog::setPrecisionMinMax()
{
int idx = mTypeBox->currentIndex();
int minPrecType = mTypeBox->itemData( idx, Qt::UserRole + 4 ).toInt();
int maxPrecType = mTypeBox->itemData( idx, Qt::UserRole + 5 ).toInt();
mPrec->setVisible( minPrecType < maxPrecType );
mPrecLabel->setVisible( minPrecType < maxPrecType );
mPrec->setMinimum( minPrecType );
mPrec->setMaximum( qMax( minPrecType, qMin( maxPrecType, mLength->value() ) ) );
}
void QgsAddAttrDialog::accept()
{
if ( mIsShapeFile && mNameEdit->text().toLower() == QLatin1String( "shape" ) )
{
QMessageBox::warning( this, tr( "Warning" ),
tr( "Invalid field name. This field name is reserved and cannot be used." ) );
return;
}
if ( mNameEdit->text().isEmpty() )
{
QMessageBox::warning( this, tr( "Warning" ),
tr( "No name specified. Please specify a name to create a new field." ) );
return;
}
QDialog::accept();
}
QgsField QgsAddAttrDialog::field() const
{
QgsDebugMsg( QString( "idx:%1 name:%2 type:%3 typeName:%4 length:%5 prec:%6 comment:%7" )
.arg( mTypeBox->currentIndex() )
.arg( mNameEdit->text() )
.arg( mTypeBox->currentData( Qt::UserRole ).toInt() )
.arg( mTypeBox->currentData( Qt::UserRole + 1 ).toString() )
.arg( mLength->value() )
.arg( mPrec->value() )
.arg( mCommentEdit->text() ) );
return QgsField(
mNameEdit->text(),
( QVariant::Type ) mTypeBox->currentData( Qt::UserRole ).toInt(),
mTypeBox->currentData( Qt::UserRole + 1 ).toString(),
mLength->value(),
mPrec->value(),
mCommentEdit->text() );
}