SciDAVis  1.D4
String2DoubleFilter.h
Go to the documentation of this file.
1 /***************************************************************************
2  File : String2DoubleFilter.h
3  Project : SciDAVis
4  --------------------------------------------------------------------
5  Copyright : (C) 2007 by Knut Franke
6  Email (use @ for *) : knut.franke*gmx.de
7  Description : Locale-aware conversion filter QString -> double.
8 
9  ***************************************************************************/
10 
11 /***************************************************************************
12  * *
13  * This program is free software; you can redistribute it and/or modify *
14  * it under the terms of the GNU General Public License as published by *
15  * the Free Software Foundation; either version 2 of the License, or *
16  * (at your option) any later version. *
17  * *
18  * This program is distributed in the hope that it will be useful, *
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
21  * GNU General Public License for more details. *
22  * *
23  * You should have received a copy of the GNU General Public License *
24  * along with this program; if not, write to the Free Software *
25  * Foundation, Inc., 51 Franklin Street, Fifth Floor, *
26  * Boston, MA 02110-1301 USA *
27  * *
28  ***************************************************************************/
29 #ifndef STRING2DOUBLE_FILTER_H
30 #define STRING2DOUBLE_FILTER_H
31 
32 #include "../AbstractSimpleFilter.h"
33 #include <QLocale>
34 #include "lib/XmlStreamReader.h"
35 #include <QXmlStreamWriter>
36 #include <QtDebug>
37 
40 {
41  Q_OBJECT
42 
43  public:
45  void setNumericLocale(QLocale locale) { d_numeric_locale = locale; d_use_default_locale = false; }
47 
48  virtual double valueAt(int row) const {
49  if (!d_inputs.value(0)) return 0;
50  if (d_use_default_locale) // we need a new QLocale instance here in case the default changed since the last call
51  return QLocale().toDouble(d_inputs.value(0)->textAt(row));
52  return d_numeric_locale.toDouble(d_inputs.value(0)->textAt(row));
53  }
54  virtual bool isInvalid(int row) const {
55  if (!d_inputs.value(0)) return false;
56  bool ok;
58  QLocale().toDouble(d_inputs.value(0)->textAt(row), &ok);
59  else
60  d_numeric_locale.toDouble(d_inputs.value(0)->textAt(row), &ok);
61  return !ok;
62  }
63  virtual bool isInvalid(Interval<int> i) const {
64  if (!d_inputs.value(0)) return false;
65  QLocale locale;
67  locale = d_numeric_locale;
68  for (int row = i.start(); row <= i.end(); row++) {
69  bool ok;
70  locale.toDouble(d_inputs.value(0)->textAt(row), &ok);
71  if (ok)
72  return false;
73  }
74  return true;
75  }
76  virtual QList< Interval<int> > invalidIntervals() const
77  {
78  IntervalAttribute<bool> validity;
79  if (d_inputs.value(0)) {
80  int rows = d_inputs.value(0)->rowCount();
81  for (int i=0; i<rows; i++)
82  validity.setValue(i, isInvalid(i));
83  }
84  return validity.intervals();
85  }
86 
87 
90 
91  protected:
93  virtual bool inputAcceptable(int, const AbstractColumn *source) {
94  return source->dataType() == SciDAVis::TypeQString;
95  }
96 
97  private:
100 };
101 
102 #endif // ifndef STRING2DOUBLE_FILTER_H
103