SciDAVis  1.D4
String2MonthFilter.h
Go to the documentation of this file.
1 /***************************************************************************
2  File : String2MonthFilter.h
3  Project : SciDAVis
4  --------------------------------------------------------------------
5  Copyright : (C) 2007 by Knut Franke, Tilman Benkert
6  Email (use @ for *) : knut.franke*gmx.de, thzs*gmx.net
7  Description : Conversion filter String -> QDateTime, interpreting
8  the input as months of the year (either numeric or "Jan" etc).
9 
10  ***************************************************************************/
11 
12 /***************************************************************************
13  * *
14  * This program is free software; you can redistribute it and/or modify *
15  * it under the terms of the GNU General Public License as published by *
16  * the Free Software Foundation; either version 2 of the License, or *
17  * (at your option) any later version. *
18  * *
19  * This program is distributed in the hope that it will be useful, *
20  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
21  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
22  * GNU General Public License for more details. *
23  * *
24  * You should have received a copy of the GNU General Public License *
25  * along with this program; if not, write to the Free Software *
26  * Foundation, Inc., 51 Franklin Street, Fifth Floor, *
27  * Boston, MA 02110-1301 USA *
28  * *
29  ***************************************************************************/
30 #ifndef STRING2MONTH_FILTER_H
31 #define STRING2MONTH_FILTER_H
32 
33 #include "../AbstractSimpleFilter.h"
34 #include <QDateTime>
35 #include <math.h>
36 #include "lib/XmlStreamReader.h"
37 #include <QXmlStreamWriter>
38 
41 {
42  Q_OBJECT
43 
44  public:
45  virtual QDate dateAt(int row) const
46  {
47  return dateTimeAt(row).date();
48  }
49 
50  virtual QTime timeAt(int row) const
51  {
52  return dateTimeAt(row).time();
53  }
54 
55  virtual QDateTime dateTimeAt(int row) const
56  {
57  if (!d_inputs.value(0)) return QDateTime();
58 
59  QString input_value = d_inputs.value(0)->textAt(row);
60  bool ok;
61  int month_value = input_value.toInt(&ok);
62  if(!ok)
63  {
64  QDate temp = QDate::fromString(input_value, "MMM");
65  if(!temp.isValid())
66  temp = QDate::fromString(input_value, "MMMM");
67  if(!temp.isValid())
68  return QDateTime();
69  else
70  month_value = temp.month();
71  }
72 
73  // Don't use Julian days here since support for years < 1 is bad
74  // Use 1900-01-01 instead
75  QDate result_date = QDate(1900,1,1).addMonths(month_value - 1);
76  QTime result_time = QTime(0,0,0,0);
77  return QDateTime(result_date, result_time);
78  }
79  virtual bool isInvalid(int row) const {
80  const AbstractColumn *col = d_inputs.value(0);
81  if (!col) return false;
82  return !(dateTimeAt(row).isValid()) || col->isInvalid(row);
83  }
84  virtual bool isInvalid(Interval<int> i) const {
85  if (!d_inputs.value(0)) return false;
86  for (int row = i.start(); row <= i.end(); row++) {
87  if (!isInvalid(row))
88  return false;
89  }
90  return true;
91  }
92  virtual QList< Interval<int> > invalidIntervals() const
93  {
94  IntervalAttribute<bool> validity;
95  if (d_inputs.value(0)) {
96  int rows = d_inputs.value(0)->rowCount();
97  for (int i=0; i<rows; i++)
98  validity.setValue(i, isInvalid(i));
99  }
100  return validity.intervals();
101  }
102 
105 
106  protected:
107  virtual bool inputAcceptable(int, const AbstractColumn *source) {
108  return source->dataType() == SciDAVis::TypeQString;
109  }
110 };
111 
112 #endif // ifndef STRING2MONTH_FILTER_H
113