Skip to content

Commit 3c94180

Browse files
author
jef
committed
use integer and double validators on attribute dialog and table, fixes #933
git-svn-id: http://svn.osgeo.org/qgis/trunk/qgis@8356 c8812cc2-4d05-0410-92ff-de0c093fc19c
1 parent 893913e commit 3c94180

4 files changed

+75
-13
lines changed

src/app/qgsattributedialog.cpp

+33-7
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
#include <QTableWidgetItem>
2323
#include <QSettings>
24+
#include <QLineEdit>
2425

2526
QgsAttributeDialog::QgsAttributeDialog(const QgsFieldMap& fields, const QgsAttributeMap& attributes)
2627
: QDialog(),
@@ -44,10 +45,24 @@ QgsAttributeDialog::QgsAttributeDialog(const QgsFieldMap& fields, const QgsAttri
4445
mTable->setItem(index, 0, myFieldItem);
4546

4647
// set attribute value
47-
4848
QTableWidgetItem * myValueItem = new QTableWidgetItem((*it).toString());
4949
mTable->setItem(index, 1, myValueItem);
5050

51+
QLineEdit *le = new QLineEdit();
52+
53+
le->setFrame(false);
54+
55+
if( fields[it.key()].type()==QVariant::Int )
56+
{
57+
le->setValidator( new QIntValidator(le) );
58+
}
59+
else if( fields[it.key()].type()==QVariant::Double )
60+
{
61+
le->setValidator( new QDoubleValidator(le) );
62+
}
63+
64+
mTable->setCellWidget(index, 1, le);
65+
5166
++index;
5267
}
5368

@@ -67,11 +82,7 @@ QgsAttributeDialog::~QgsAttributeDialog()
6782

6883
QString QgsAttributeDialog::value(int row)
6984
{
70-
QString val = mTable->item(row,1)->text();
71-
if(val=="NULL")
72-
return QString::null;
73-
else
74-
return mTable->item(row,1)->text();
85+
return static_cast<QLineEdit*>(mTable->cellWidget(row,1))->text();
7586
}
7687

7788
bool QgsAttributeDialog::isDirty(int row)
@@ -89,7 +100,22 @@ bool QgsAttributeDialog::queryAttributes(const QgsFieldMap& fields, QgsFeature&
89100
int i=0;
90101
for (QgsAttributeMap::const_iterator it = featureAttributes.begin(); it != featureAttributes.end(); ++it)
91102
{
92-
f.changeAttribute(it.key(), QVariant(attdialog.value(i++)) );
103+
QString value = attdialog.value(i++);
104+
105+
switch( fields[it.key()].type() )
106+
{
107+
case QVariant::Int:
108+
f.changeAttribute(it.key(), value=="" ? QVariant( QString::null ) : QVariant( value.toInt() ) );
109+
break;
110+
111+
case QVariant::Double:
112+
f.changeAttribute(it.key(), value=="" ? QVariant( QString::null ) : QVariant( value.toDouble() ) );
113+
break;
114+
115+
default:
116+
f.changeAttribute(it.key(), value=="NULL" ? QVariant(QString::null) : QVariant(value) );
117+
break;
118+
}
93119
}
94120
return true;
95121
}

src/app/qgsattributedialog.h

+3-3
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,6 @@ class QgsAttributeDialog: public QDialog, private Ui::QgsAttributeDialogBase
3838

3939
~QgsAttributeDialog();
4040

41-
/** Returns the field value of a row */
42-
QString value(int row);
43-
4441
/** Returns if the field value of a row was edited since this dialog opened */
4542
bool isDirty(int row);
4643

@@ -67,6 +64,9 @@ class QgsAttributeDialog: public QDialog, private Ui::QgsAttributeDialogBase
6764
private:
6865
QString _settingsPath;
6966

67+
/** Returns the field value of a row */
68+
QString value(int row);
69+
7070
std::vector<bool> mRowIsDirty;
7171
};
7272

src/app/qgsattributetable.cpp

+37-3
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
#include <QClipboard>
2525
#include <QAction>
2626
#include <QMenu>
27+
#include <QLineEdit>
28+
#include <QValidator>
2729

2830
#include "qgsattributetable.h"
2931
#include "qgsfeature.h"
@@ -59,6 +61,22 @@ QgsAttributeTable::~QgsAttributeTable()
5961
{
6062
}
6163

64+
QWidget *QgsAttributeTable::createEditor(int row, int col, bool initFromCell ) const
65+
{
66+
QLineEdit *le = static_cast<QLineEdit*>(Q3Table::createEditor(row, col, initFromCell));
67+
68+
if( mFields[col-1].type()==QVariant::Int )
69+
{
70+
le->setValidator( new QIntValidator(le) );
71+
}
72+
else if( mFields[col-1].type()==QVariant::Double )
73+
{
74+
le->setValidator( new QDoubleValidator(le) );
75+
}
76+
77+
return le;
78+
}
79+
6280
void QgsAttributeTable::columnClicked(int col)
6381
{
6482
QApplication::setOverrideCursor(Qt::waitCursor);
@@ -461,7 +479,10 @@ bool QgsAttributeTable::commitChanges(QgsVectorLayer* layer)
461479
fieldIndex = provider->indexFromFieldName(record_it.key());
462480
if(fieldIndex != -1)
463481
{
464-
if( record_it.value()=="NULL" )
482+
if( record_it.value()=="NULL" ||
483+
( record_it.value().isEmpty() &&
484+
(provider->fields()[fieldIndex].type()==QVariant::Int ||
485+
provider->fields()[fieldIndex].type()==QVariant::Double) ) )
465486
newAttMap.insert(fieldIndex, QVariant(QString::null) );
466487
else
467488
newAttMap.insert(fieldIndex, record_it.value());
@@ -573,9 +594,22 @@ void QgsAttributeTable::putFeatureInTable(int row, QgsFeature& fet)
573594
QgsAttributeMap::const_iterator it;
574595
int h = 1;
575596
for (it = attr.begin(); it != attr.end(); ++it)
576-
{
597+
{
598+
QString value;
599+
577600
// get the field values
578-
setText(row, h++, it->isNull() ? "NULL" : it->toString());
601+
if( it->isNull() )
602+
{
603+
if( mFields[h-1].type()==QVariant::Int || mFields[h-1].type()==QVariant::Double )
604+
value="";
605+
else
606+
value="NULL";
607+
} else {
608+
value = it->toString();
609+
}
610+
611+
clearCellWidget(row, h);
612+
setText(row, h++, value);
579613
}
580614
}
581615

src/app/qgsattributetable.h

+2
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,8 @@ class QgsAttributeTable:public Q3Table
157157
Also, mLastSelectedRows is updated*/
158158
bool checkSelectionChanges();
159159

160+
virtual QWidget *createEditor(int row, int col, bool initFromCell ) const;
161+
160162
signals:
161163
/**Is emitted when a row was selected*/
162164
void selected(int, bool);

0 commit comments

Comments
 (0)