yade-dev team mailing list archive
-
yade-dev team
-
Mailing list archive
-
Message #01241
[svn] r1769 - trunk/lib/serialization-qt
Author: cosurgi
Date: 2009-05-09 01:47:08 +0200 (Sat, 09 May 2009)
New Revision: 1769
Modified:
trunk/lib/serialization-qt/QtGUIGenerator.cpp
Log:
sanitize stupid numers in QtGUI, eg. 0.1299999999999 becomes 0.13
Modified: trunk/lib/serialization-qt/QtGUIGenerator.cpp
===================================================================
--- trunk/lib/serialization-qt/QtGUIGenerator.cpp 2009-05-05 20:44:59 UTC (rev 1768)
+++ trunk/lib/serialization-qt/QtGUIGenerator.cpp 2009-05-08 23:47:08 UTC (rev 1769)
@@ -87,7 +87,96 @@
}
}
+std::string sanitize(const std::string num)
+{
+ std::string ret="";
+ int len=num.size();
+ if(len < 7 )
+ return num;
+ // is that a number?
+ try
+ {
+ Real v = boost::lexical_cast<Real>(num);
+ }
+ catch(std::bad_cast&)
+ {
+ return num;
+ }
+ // case .....000001
+ if(num[len-2] == '0' && num[len-3] == '0')
+ {
+ for(int pos=len-4 ; pos>=0 ; --pos)
+ {
+ if(ret.size() != 0 || num[pos] != '0' || num[pos-1] == '.')
+ {
+ ret = num[pos] + ret;
+ }
+ }
+ }
+ else
+ {// case ....9999998
+ if(num[len-2] == '9' && num[len-3] == '9')
+ {
+ for(int pos=len-4 ; pos>=0 ; --pos)
+ {
+ if(ret.size() != 0 || num[pos] != '9')
+ {
+ if(ret.size() == 0)
+ {
+ ret = (unsigned char)((int)(num[pos])+1);
+ }
+ else
+ {
+ ret = num[pos] + ret;
+ }
+ }
+ }
+ }
+ else
+ {// exponential part e-..
+ if(num[len-3] == '-' && num[len-4] == 'e')
+ {
+ std::string exp =std::string("e-")+num[len-2]+num[len-1];
+ std::string mant="";
+ for(int pos=len-5 ; pos>=0 ; --pos)
+ {
+ mant = num[pos] + mant;
+ }
+ ret=sanitize(mant)+exp;
+ }
+ else
+ {// exponential part e..
+ if(num[len-3] == 'e')
+ {
+ std::string exp =std::string("e")+num[len-2]+num[len-1];
+ std::string mant="";
+ for(int pos=len-4 ; pos>=0 ; --pos)
+ {
+ mant = num[pos] + mant;
+ }
+ ret=sanitize(mant)+exp;
+ }
+ else
+ {
+ return num;
+ }
+ }
+ }
+ }
+ Real a = boost::lexical_cast<Real>(ret);
+ Real b = boost::lexical_cast<Real>(num);
+ if(a-b == 0) // the sanitized number and original numer must be exactly the same
+ {
+ return ret;
+ }
+ else
+ {
+ std::cerr << "INFO: sanitize failed: " << a << " != " << b << "\n";
+ return num; // return original numer, since they are different
+ }
+};
+
void QtGUIGenerator::buildGUI(shared_ptr<Serializable> s, QWidget * widget)
{
@@ -142,7 +231,7 @@
else
{
QLineEdit* le = new QLineEdit(widget);
- le->setText(descriptor->strings[i]);
+ le->setText(sanitize(descriptor->strings[i]));
descriptor->widgets.push_back(le);
descriptor->types.push_back(AttributeDescriptor::FLOATING);
}