~valavanisalex/ubuntu/maverick/scidavis/fix-604811

« back to all changes in this revision

Viewing changes to scidavis/src/future/core/datatypes/String2DateTimeFilter.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                 : String2DateTimeFilter.h
 
3
    Project              : SciDAVis
 
4
    --------------------------------------------------------------------
 
5
    Copyright            : (C) 2007 by Tilman Benkert,
 
6
                           Knut Franke
 
7
    Email (use @ for *)  : thzs*gmx.net, knut.franke*gmx.de
 
8
    Description          : Conversion filter QString -> QDateTime.
 
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 STRING2DATE_TIME_FILTER_H
 
31
#define STRING2DATE_TIME_FILTER_H
 
32
 
 
33
#include "core/AbstractSimpleFilter.h"
 
34
#include <QDateTime>
 
35
#include <QDate>
 
36
#include <QTime>
 
37
 
 
38
/**
 
39
 * \brief Conversion filter QString -> QDateTime.
 
40
 *
 
41
 * The standard use of this filter is explicitly specifiying the date/time format of the strings
 
42
 * on the input, either in the constructor or via setFormat().
 
43
 * However, if the input fails to comply to this format, String2DateTimeFilter
 
44
 * tries to guess the format, using internal lists of common date and time formats (#date_formats
 
45
 * and #time_formats).
 
46
 */
 
47
class String2DateTimeFilter : public AbstractSimpleFilter
 
48
{
 
49
        Q_OBJECT
 
50
 
 
51
        public:
 
52
                //! Standard constructor.
 
53
                explicit String2DateTimeFilter(QString format="yyyy-MM-dd hh:mm:ss.zzz") : d_format(format) {}
 
54
                //! Set the format string to be used for conversion.
 
55
                void setFormat(const QString& format);
 
56
                //! Return the format string
 
57
                /**
 
58
                 * The default format string is "yyyy-MM-dd hh:mm:ss.zzz".
 
59
                 * \sa QDate::toString()
 
60
                 */
 
61
                QString format() const { return d_format; }
 
62
 
 
63
                //! Return the data type of the column
 
64
                virtual SciDAVis::ColumnDataType dataType() const { return SciDAVis::TypeQDateTime; }
 
65
 
 
66
                //! \name XML related functions
 
67
                //@{
 
68
                virtual void writeExtraAttributes(QXmlStreamWriter * writer) const;
 
69
                virtual bool load(XmlStreamReader * reader);
 
70
                //@}
 
71
 
 
72
        signals:
 
73
                void formatChanged();
 
74
 
 
75
        private:
 
76
                friend class String2DateTimeFilterSetFormatCmd;
 
77
                //! The format string.
 
78
                QString d_format;
 
79
 
 
80
                static const char * date_formats[];
 
81
                static const char * time_formats[];
 
82
 
 
83
        public:
 
84
                virtual QDateTime dateTimeAt(int row) const;
 
85
                virtual QDate dateAt(int row) const { return dateTimeAt(row).date(); }
 
86
                virtual QTime timeAt(int row) const { return dateTimeAt(row).time(); }
 
87
                virtual bool isInvalid(int row) const { 
 
88
                        const AbstractColumn *col = d_inputs.value(0);
 
89
                        if (!col) return false;
 
90
                        return !(dateTimeAt(row).isValid()) || col->isInvalid(row);
 
91
                }
 
92
                virtual bool isInvalid(Interval<int> i) const {
 
93
                        if (!d_inputs.value(0)) return false;
 
94
                        for (int row = i.start(); row <= i.end(); row++) {
 
95
                                if (!isInvalid(row))
 
96
                                        return false;
 
97
                        }
 
98
                        return true;
 
99
                }
 
100
                virtual QList< Interval<int> > invalidIntervals() const 
 
101
                {
 
102
                        IntervalAttribute<bool> validity;
 
103
                        if (d_inputs.value(0)) {
 
104
                                int rows = d_inputs.value(0)->rowCount();
 
105
                                for (int i=0; i<rows; i++) 
 
106
                                        validity.setValue(i, isInvalid(i));
 
107
                        }
 
108
                        return validity.intervals();
 
109
                }
 
110
 
 
111
        protected:
 
112
                //! Using typed ports: only string inputs are accepted.
 
113
                virtual bool inputAcceptable(int, const AbstractColumn *source) {
 
114
                        return source->dataType() == SciDAVis::TypeQString;
 
115
                }
 
116
};
 
117
 
 
118
class String2DateTimeFilterSetFormatCmd : public QUndoCommand
 
119
{
 
120
        public:
 
121
                String2DateTimeFilterSetFormatCmd(String2DateTimeFilter* target, const QString &new_format);
 
122
 
 
123
                virtual void redo();
 
124
                virtual void undo();
 
125
 
 
126
        private:
 
127
                String2DateTimeFilter* d_target;
 
128
                QString d_other_format;
 
129
};
 
130
 
 
131
#endif // ifndef STRING2DATE_TIME_FILTER_H
 
132