1 /*************************************************************************** 2 File : String2DayOfWeekFilter.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 days of the week (either numeric or "Mon" 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 STRING2DAYOFWEEK_FILTER_H 31 #define STRING2DAYOFWEEK_FILTER_H 32 33 #include "../AbstractSimpleFilter.h" 34 #include <QDateTime> 35 #include <math.h> 36 #include "lib/XmlStreamReader.h" 37 #include <QXmlStreamWriter> 38 39 //! Conversion filter String -> QDateTime, interpreting the input as days of the week (either numeric or "Mon" etc). 40 class String2DayOfWeekFilter : public AbstractSimpleFilter 41 { 42 Q_OBJECT 43 44 public: dateAt(int row)45 virtual QDate dateAt(int row) const { return dateTimeAt(row).date(); } 46 timeAt(int row)47 virtual QTime timeAt(int row) const { return dateTimeAt(row).time(); } 48 dateTimeAt(int row)49 virtual QDateTime dateTimeAt(int row) const 50 { 51 if (!d_inputs.value(0)) 52 return QDateTime(); 53 54 QString input_value = d_inputs.value(0)->textAt(row); 55 if (input_value.isEmpty()) 56 return QDateTime(); 57 bool ok; 58 int day_value = input_value.toInt(&ok); 59 if (!ok) { 60 #if QT_VERSION <= 0x040300 61 // workaround for Qt bug #171920 62 QDate temp = QDate(1900, 1, 1); 63 for (int i = 1; i <= 7; i++) 64 if ((input_value.toLower() == QDate::longDayName(i).toLower()) 65 || (input_value.toLower() == QDate::shortDayName(i).toLower())) { 66 temp = QDate(1900, 1, i); 67 break; 68 } 69 70 #else 71 QDate temp = QDate::fromString(input_value, "ddd"); 72 if (!temp.isValid()) 73 temp = QDate::fromString(input_value, "dddd"); 74 #endif 75 if (!temp.isValid()) 76 return QDateTime(); 77 else 78 day_value = temp.dayOfWeek(); 79 } 80 81 // Don't use Julian days here since support for years < 1 is bad 82 // Use 1900-01-01 instead (a Monday) 83 QDate result_date = QDate(1900, 1, 1).addDays(day_value - 1); 84 QTime result_time = QTime(0, 0, 0, 0); 85 return QDateTime(result_date, result_time); 86 } isInvalid(int row)87 virtual bool isInvalid(int row) const 88 { 89 const AbstractColumn *col = d_inputs.value(0); 90 if (!col) 91 return false; 92 return !(dateTimeAt(row).isValid()) || col->isInvalid(row); 93 } isInvalid(Interval<int> i)94 virtual bool isInvalid(Interval<int> i) const 95 { 96 if (!d_inputs.value(0)) 97 return false; 98 for (int row = i.start(); row <= i.end(); row++) { 99 if (!isInvalid(row)) 100 return false; 101 } 102 return true; 103 } invalidIntervals()104 virtual QList<Interval<int>> invalidIntervals() const 105 { 106 IntervalAttribute<bool> validity; 107 if (d_inputs.value(0)) { 108 int rows = d_inputs.value(0)->rowCount(); 109 for (int i = 0; i < rows; i++) 110 validity.setValue(i, isInvalid(i)); 111 } 112 return validity.intervals(); 113 } 114 115 //! Return the data type of the column dataType()116 virtual SciDAVis::ColumnDataType dataType() const { return SciDAVis::TypeQDateTime; } 117 118 protected: inputAcceptable(int,const AbstractColumn * source)119 virtual bool inputAcceptable(int, const AbstractColumn *source) 120 { 121 return source->dataType() == SciDAVis::TypeQString; 122 } 123 }; 124 125 #endif // ifndef STRING2DAYOFWEEK_FILTER_H 126