Skip to content

Commit

Permalink
Format number strings with field precision for display in attribute t…
Browse files Browse the repository at this point in the history
…able, attribute dialog and editor
  • Loading branch information
mhugent committed Mar 16, 2013
1 parent 3703910 commit 6361d19
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 9 deletions.
10 changes: 5 additions & 5 deletions src/app/qgsidentifyresultsdialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -341,13 +341,13 @@ void QgsIdentifyResultsDialog::addFeature( QgsVectorLayer *vlayer, const QgsFeat
if ( i >= fields.count() )
continue;

QTreeWidgetItem *attrItem = new QTreeWidgetItem( QStringList() << QString::number( i ) << attrs[i].toString() );
QString value = fields[i].displayString( attrs[i] );
QTreeWidgetItem *attrItem = new QTreeWidgetItem( QStringList() << QString::number( i ) << value );

attrItem->setData( 0, Qt::DisplayRole, vlayer->attributeDisplayName( i ) );
attrItem->setData( 0, Qt::UserRole, fields[i].name() );
attrItem->setData( 0, Qt::UserRole + 1, i );

QVariant value = attrs[i];
attrItem->setData( 1, Qt::UserRole, value );

switch ( vlayer->editType( i ) )
Expand All @@ -358,12 +358,12 @@ void QgsIdentifyResultsDialog::addFeature( QgsVectorLayer *vlayer, const QgsFeat
continue;

case QgsVectorLayer::ValueMap:
value = vlayer->valueMap( i ).key( value.toString(), QString( "(%1)" ).arg( value.toString() ) );
value = vlayer->valueMap( i ).key( value, QString( "(%1)" ).arg( value ) );
break;

case QgsVectorLayer::Calendar:
if ( value.canConvert( QVariant::Date ) )
value = value.toDate().toString( vlayer->dateFormat( i ) );
if ( attrs[i].canConvert( QVariant::Date ) )
value = attrs[i].toDate().toString( vlayer->dateFormat( i ) );
break;

default:
Expand Down
14 changes: 14 additions & 0 deletions src/core/qgsfield.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -110,3 +110,17 @@ void QgsField::setComment( const QString & comment )
{
mComment = comment;
}

QString QgsField::displayString( const QVariant& v ) const
{
switch ( mType )
{
case QVariant::Double:
if ( mPrecision > 0 )
{
return QString::number( v.toDouble(), 'f', mPrecision );
}
default:
return v.toString();
}
}
3 changes: 3 additions & 0 deletions src/core/qgsfield.h
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,9 @@ class CORE_EXPORT QgsField
*/
void setComment( const QString & comment );

/**Formats string for display*/
QString displayString( const QVariant& v ) const;

private:

//! Name
Expand Down
6 changes: 3 additions & 3 deletions src/gui/attributetable/qgsattributetablemodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -605,8 +605,9 @@ QVariant QgsAttributeTableModel::data( const QModelIndex &index, int role ) cons
return role == Qt::DisplayRole ? rowId : QVariant();

int fieldId = mAttributes[ index.column()];
const QgsField& field = mLayer->pendingFields()[ fieldId ];

QVariant::Type fldType = mLayer->pendingFields()[ fieldId ].type();
QVariant::Type fldType = field.type();
bool fldNumeric = ( fldType == QVariant::Int || fldType == QVariant::Double );

if ( role == Qt::TextAlignmentRole )
Expand Down Expand Up @@ -656,8 +657,7 @@ QVariant QgsAttributeTableModel::data( const QModelIndex &index, int role ) cons
}
}


return val.toString();
return field.displayString( val );
}

bool QgsAttributeTableModel::setData( const QModelIndex &index, const QVariant &value, int role )
Expand Down
2 changes: 1 addition & 1 deletion src/gui/qgsattributeeditor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1002,7 +1002,7 @@ bool QgsAttributeEditor::setValue( QWidget *editor, QgsVectorLayer *vl, int idx,
}
else
{
text = value.toString();
text = field.displayString( value );
}

if ( le )
Expand Down

0 comments on commit 6361d19

Please sign in to comment.