~ubuntu-branches/ubuntu/maverick/scidavis/maverick

« back to all changes in this revision

Viewing changes to scidavis/src/future/core/datatypes/String2DayOfWeekFilter.h

  • Committer: Bazaar Package Importer
  • Author(s): Ruben Molina
  • Date: 2009-09-06 11:34:04 UTC
  • Revision ID: james.westby@ubuntu.com-20090906113404-4awaey82l3686w4q
Tags: upstream-0.2.3
ImportĀ upstreamĀ versionĀ 0.2.3

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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:
 
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
                        if (input_value.isEmpty()) return QDateTime();
 
61
                        bool ok;
 
62
                        int day_value = input_value.toInt(&ok);
 
63
                        if(!ok)
 
64
                        {
 
65
#if QT_VERSION <= 0x040300  
 
66
                                // workaround for Qt bug #171920
 
67
                                QDate temp = QDate(1900,1,1);
 
68
                                for(int i=1; i<=7; i++)
 
69
                                        if( (input_value.toLower() == QDate::longDayName(i).toLower())
 
70
                                                        || (input_value.toLower() == QDate::shortDayName(i).toLower()) )
 
71
                                        {
 
72
                                                temp = QDate(1900,1,i);
 
73
                                                break;
 
74
                                        }
 
75
 
 
76
#else 
 
77
                                QDate temp = QDate::fromString(input_value, "ddd");
 
78
                                if(!temp.isValid())
 
79
                                        temp = QDate::fromString(input_value, "dddd");
 
80
#endif
 
81
                                if(!temp.isValid())
 
82
                                        return QDateTime();
 
83
                                else
 
84
                                        day_value = temp.dayOfWeek();
 
85
                        }
 
86
 
 
87
                        // Don't use Julian days here since support for years < 1 is bad
 
88
                        // Use 1900-01-01 instead (a Monday)
 
89
                        QDate result_date = QDate(1900,1,1).addDays(day_value - 1);
 
90
                        QTime result_time = QTime(0,0,0,0);
 
91
                        return QDateTime(result_date, result_time);
 
92
                }
 
93
                virtual bool isInvalid(int row) const { 
 
94
                        const AbstractColumn *col = d_inputs.value(0);
 
95
                        if (!col) return false;
 
96
                        return !(dateTimeAt(row).isValid()) || col->isInvalid(row);
 
97
                }
 
98
                virtual bool isInvalid(Interval<int> i) const {
 
99
                        if (!d_inputs.value(0)) return false;
 
100
                        for (int row = i.start(); row <= i.end(); row++) {
 
101
                                if (!isInvalid(row))
 
102
                                        return false;
 
103
                        }
 
104
                        return true;
 
105
                }
 
106
                virtual QList< Interval<int> > invalidIntervals() const 
 
107
                {
 
108
                        IntervalAttribute<bool> validity;
 
109
                        if (d_inputs.value(0)) {
 
110
                                int rows = d_inputs.value(0)->rowCount();
 
111
                                for (int i=0; i<rows; i++) 
 
112
                                        validity.setValue(i, isInvalid(i));
 
113
                        }
 
114
                        return validity.intervals();
 
115
                }
 
116
 
 
117
                //! Return the data type of the column
 
118
                virtual SciDAVis::ColumnDataType dataType() const { return SciDAVis::TypeQDateTime; }
 
119
 
 
120
        protected:
 
121
                virtual bool inputAcceptable(int, const AbstractColumn *source) {
 
122
                        return source->dataType() == SciDAVis::TypeQString;
 
123
                }
 
124
};
 
125
 
 
126
#endif // ifndef STRING2DAYOFWEEK_FILTER_H
 
127