Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
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
  • Loading branch information
jef committed Apr 17, 2008
1 parent 893913e commit 3c94180
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 13 deletions.
40 changes: 33 additions & 7 deletions src/app/qgsattributedialog.cpp
Expand Up @@ -21,6 +21,7 @@

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

QgsAttributeDialog::QgsAttributeDialog(const QgsFieldMap& fields, const QgsAttributeMap& attributes)
: QDialog(),
Expand All @@ -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;
}

Expand All @@ -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)
Expand All @@ -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;
}
Expand Down
6 changes: 3 additions & 3 deletions src/app/qgsattributedialog.h
Expand Up @@ -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);

Expand All @@ -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;
};

Expand Down
40 changes: 37 additions & 3 deletions src/app/qgsattributetable.cpp
Expand Up @@ -24,6 +24,8 @@
#include <QClipboard>
#include <QAction>
#include <QMenu>
#include <QLineEdit>
#include <QValidator>

#include "qgsattributetable.h"
#include "qgsfeature.h"
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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());
Expand Down Expand Up @@ -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);
}
}

Expand Down
2 changes: 2 additions & 0 deletions src/app/qgsattributetable.h
Expand Up @@ -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);
Expand Down

0 comments on commit 3c94180

Please sign in to comment.