Skip to content
Permalink
Browse files

use integer and double validators on attribute dialog and table, fixes

…#933

git-svn-id: http://svn.osgeo.org/qgis/trunk@8356 c8812cc2-4d05-0410-92ff-de0c093fc19c
  • Loading branch information
jef committed Apr 17, 2008
1 parent e2ea490 commit ae5d5ec517eaebaf4e905cdd92ab195c3be3ff37
Showing with 75 additions and 13 deletions.
  1. +33 −7 src/app/qgsattributedialog.cpp
  2. +3 −3 src/app/qgsattributedialog.h
  3. +37 −3 src/app/qgsattributetable.cpp
  4. +2 −0 src/app/qgsattributetable.h
@@ -21,6 +21,7 @@

#include <QTableWidgetItem>
#include <QSettings>
#include <QLineEdit>

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

// set attribute value

QTableWidgetItem * myValueItem = new QTableWidgetItem((*it).toString());
mTable->setItem(index, 1, myValueItem);

QLineEdit *le = new QLineEdit();

le->setFrame(false);

if( fields[it.key()].type()==QVariant::Int )
{
le->setValidator( new QIntValidator(le) );
}
else if( fields[it.key()].type()==QVariant::Double )
{
le->setValidator( new QDoubleValidator(le) );
}

mTable->setCellWidget(index, 1, le);

++index;
}

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

QString QgsAttributeDialog::value(int row)
{
QString val = mTable->item(row,1)->text();
if(val=="NULL")
return QString::null;
else
return mTable->item(row,1)->text();
return static_cast<QLineEdit*>(mTable->cellWidget(row,1))->text();
}

bool QgsAttributeDialog::isDirty(int row)
@@ -89,7 +100,22 @@ bool QgsAttributeDialog::queryAttributes(const QgsFieldMap& fields, QgsFeature&
int i=0;
for (QgsAttributeMap::const_iterator it = featureAttributes.begin(); it != featureAttributes.end(); ++it)
{
f.changeAttribute(it.key(), QVariant(attdialog.value(i++)) );
QString value = attdialog.value(i++);

switch( fields[it.key()].type() )
{
case QVariant::Int:
f.changeAttribute(it.key(), value=="" ? QVariant( QString::null ) : QVariant( value.toInt() ) );
break;

case QVariant::Double:
f.changeAttribute(it.key(), value=="" ? QVariant( QString::null ) : QVariant( value.toDouble() ) );
break;

default:
f.changeAttribute(it.key(), value=="NULL" ? QVariant(QString::null) : QVariant(value) );
break;
}
}
return true;
}
@@ -38,9 +38,6 @@ class QgsAttributeDialog: public QDialog, private Ui::QgsAttributeDialogBase

~QgsAttributeDialog();

/** Returns the field value of a row */
QString value(int row);

/** Returns if the field value of a row was edited since this dialog opened */
bool isDirty(int row);

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

/** Returns the field value of a row */
QString value(int row);

std::vector<bool> mRowIsDirty;
};

@@ -24,6 +24,8 @@
#include <QClipboard>
#include <QAction>
#include <QMenu>
#include <QLineEdit>
#include <QValidator>

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

QWidget *QgsAttributeTable::createEditor(int row, int col, bool initFromCell ) const
{
QLineEdit *le = static_cast<QLineEdit*>(Q3Table::createEditor(row, col, initFromCell));

if( mFields[col-1].type()==QVariant::Int )
{
le->setValidator( new QIntValidator(le) );
}
else if( mFields[col-1].type()==QVariant::Double )
{
le->setValidator( new QDoubleValidator(le) );
}

return le;
}

void QgsAttributeTable::columnClicked(int col)
{
QApplication::setOverrideCursor(Qt::waitCursor);
@@ -461,7 +479,10 @@ bool QgsAttributeTable::commitChanges(QgsVectorLayer* layer)
fieldIndex = provider->indexFromFieldName(record_it.key());
if(fieldIndex != -1)
{
if( record_it.value()=="NULL" )
if( record_it.value()=="NULL" ||
( record_it.value().isEmpty() &&
(provider->fields()[fieldIndex].type()==QVariant::Int ||
provider->fields()[fieldIndex].type()==QVariant::Double) ) )
newAttMap.insert(fieldIndex, QVariant(QString::null) );
else
newAttMap.insert(fieldIndex, record_it.value());
@@ -573,9 +594,22 @@ void QgsAttributeTable::putFeatureInTable(int row, QgsFeature& fet)
QgsAttributeMap::const_iterator it;
int h = 1;
for (it = attr.begin(); it != attr.end(); ++it)
{
{
QString value;

// get the field values
setText(row, h++, it->isNull() ? "NULL" : it->toString());
if( it->isNull() )
{
if( mFields[h-1].type()==QVariant::Int || mFields[h-1].type()==QVariant::Double )
value="";
else
value="NULL";
} else {
value = it->toString();
}

clearCellWidget(row, h);
setText(row, h++, value);
}
}

@@ -157,6 +157,8 @@ class QgsAttributeTable:public Q3Table
Also, mLastSelectedRows is updated*/
bool checkSelectionChanges();

virtual QWidget *createEditor(int row, int col, bool initFromCell ) const;

signals:
/**Is emitted when a row was selected*/
void selected(int, bool);

0 comments on commit ae5d5ec

Please sign in to comment.