scidavis  2.3.0
About: SciDAVis is a free application for Scientific Data Analysis and Visualization (a fork off of QtiPlot).
  Fossies Dox: scidavis-2.3.0.tar.gz  ("unofficial" and yet experimental doxygen-generated source code documentation)  

String2DateTimeFilter.cpp
Go to the documentation of this file.
1 
12 
30 #include "String2DateTimeFilter.h"
31 #include <QStringList>
32 #include "lib/XmlStreamReader.h"
33 #include <QXmlStreamWriter>
34 
35 const char * String2DateTimeFilter::date_formats[] = {
36  "yyyy-M-d", // ISO 8601 w/ and w/o leading zeros
37  "yyyy/M/d",
38  "d/M/yyyy", // European style day/month order (this order seems to be used in more countries than the US style M/d/yyyy)
39  "d/M/yy",
40  "d-M-yyyy",
41  "d-M-yy",
42  "d.M.yyyy", // German style
43  "d.M.yy",
44  "M/yyyy",
45  "d.M.", // German form w/o year
46  "yyyyMMdd",
47  0
48 };
49 
50 const char * String2DateTimeFilter::time_formats[] = {
51  "h",
52  "h ap",
53  "h:mm",
54  "h:mm ap",
55  "h:mm:ss",
56  "h:mm:ss.zzz",
57  "h:mm:ss:zzz",
58  "mm:ss.zzz",
59  "hmmss",
60  0
61 };
62 
63 QDateTime String2DateTimeFilter::dateTimeAt(int row) const
64 {
65  if (!d_inputs.value(0)) return QDateTime();
66  QString input_value = d_inputs.value(0)->textAt(row);
67  if (input_value.isEmpty()) return QDateTime();
68 
69  // first try the selected format string d_format
70  QDateTime result = QDateTime::fromString(input_value, d_format);
71  if(result.isValid())
72  return result;
73 
74  // fallback:
75  // try other format strings built from date_formats and time_formats
76  // comma and space are valid separators between date and time
77  QStringList strings = input_value.simplified().split(",", QString::SkipEmptyParts);
78  if(strings.size() == 1) strings = strings.at(0).split(" ", QString::SkipEmptyParts);
79 
80  if(strings.size() < 1)
81  return result; // invalid date/time from first attempt
82 
83  QDate date_result;
84  QTime time_result;
85 
86  QString date_string = strings.at(0).trimmed();
87  QString time_string;
88  if(strings.size() > 1)
89  time_string = strings.at(1).trimmed();
90  else
91  time_string = date_string;
92 
93  // try to find a valid date
94  for (const char **format = date_formats; *format != 0; format++) {
95  date_result = QDate::fromString(date_string, *format);
96  if (date_result.isValid())
97  break;
98  }
99  // try to find a valid time
100  for (const char **format = time_formats; *format != 0; format++) {
101  time_result = QTime::fromString(time_string, *format);
102  if (time_result.isValid())
103  break;
104  }
105 
106  if(!date_result.isValid() && time_result.isValid())
107  date_result.setDate(1900,1,1); // this is what QDateTime does e.g. for
108  // QDateTime::fromString("00:00","hh:mm");
109  return QDateTime(date_result, time_result);
110 }
111 
112 void String2DateTimeFilter::writeExtraAttributes(QXmlStreamWriter * writer) const
113 {
114  writer->writeAttribute("format", format());
115 }
116 
118 {
119  QXmlStreamAttributes attribs = reader->attributes();
120  QString str = attribs.value(reader->namespaceUri().toString(), "format").toString();
121 
122  if (AbstractSimpleFilter::load(reader))
123  setFormat(str);
124  else
125  return false;
126 
127  return !reader->hasError();
128 }
129 
130 void String2DateTimeFilter::setFormat(const QString& format)
131 {
133 }
134 
136  : d_target(target), d_other_format(new_format)
137 {
138  if(d_target->parentAspect())
139  setText(QObject::tr("%1: set date-time format to %2").arg(d_target->parentAspect()->name()).arg(new_format));
140  else
141  setText(QObject::tr("set date-time format to %1").arg(new_format));
142 }
143 
145 {
146  QString tmp = d_target->d_format;
148  d_other_format = tmp;
149  emit d_target->formatChanged();
150 }
151 
153 {
154  redo();
155 }
156 
String2DateTimeFilter::date_formats
static const char * date_formats[]
Definition: String2DateTimeFilter.h:80
String2DateTimeFilterSetFormatCmd::undo
virtual void undo()
Definition: String2DateTimeFilter.cpp:152
String2DateTimeFilter::writeExtraAttributes
virtual void writeExtraAttributes(QXmlStreamWriter *writer) const
Override this in derived classes if they have other attributes than filter_name.
Definition: String2DateTimeFilter.cpp:112
AbstractAspect::exec
void exec(QUndoCommand *command)
Execute the given command, pushing it on the undoStack() if available.
Definition: AbstractAspect.cpp:203
String2DateTimeFilter::dateTimeAt
virtual QDateTime dateTimeAt(int row) const
Set the content of row 'row'.
Definition: String2DateTimeFilter.cpp:63
str
#define str(x)
Definition: PythonScripting.cpp:41
XmlStreamReader
XML stream parser that supports errors as well as warnings.
Definition: XmlStreamReader.h:42
String2DateTimeFilter::setFormat
void setFormat(const QString &format)
Set the format string to be used for conversion.
Definition: String2DateTimeFilter.cpp:130
String2DateTimeFilter::format
QString format() const
Return the format string.
Definition: String2DateTimeFilter.h:61
String2DateTimeFilterSetFormatCmd::d_other_format
QString d_other_format
Definition: String2DateTimeFilter.h:128
String2DateTimeFilter::load
virtual bool load(XmlStreamReader *reader)
Load from XML.
Definition: String2DateTimeFilter.cpp:117
String2DateTimeFilterSetFormatCmd::d_target
String2DateTimeFilter * d_target
Definition: String2DateTimeFilter.h:127
AbstractAspect::name
QString name() const
Definition: AbstractAspect.cpp:229
XmlStreamReader.h
AbstractAspect::parentAspect
AbstractAspect * parentAspect() const
Return my parent Aspect or 0 if I currently don't have one.
Definition: AbstractAspect.cpp:109
String2DateTimeFilter::time_formats
static const char * time_formats[]
Definition: String2DateTimeFilter.h:81
String2DateTimeFilter.h
String2DateTimeFilterSetFormatCmd::redo
virtual void redo()
Definition: String2DateTimeFilter.cpp:144
AbstractSimpleFilter::load
virtual bool load(XmlStreamReader *reader)
Load from XML.
Definition: AbstractSimpleFilter.cpp:135
String2DateTimeFilter::String2DateTimeFilterSetFormatCmd
friend class String2DateTimeFilterSetFormatCmd
Definition: String2DateTimeFilter.h:76
String2DateTimeFilter
Conversion filter QString -> QDateTime.
Definition: String2DateTimeFilter.h:48
AbstractFilter::d_inputs
QVector< const AbstractColumn * > d_inputs
The data sources connected to my input ports.
Definition: AbstractFilter.h:275
String2DateTimeFilter::formatChanged
void formatChanged()
String2DateTimeFilter::d_format
QString d_format
The format string.
Definition: String2DateTimeFilter.h:78
String2DateTimeFilterSetFormatCmd::String2DateTimeFilterSetFormatCmd
String2DateTimeFilterSetFormatCmd(String2DateTimeFilter *target, const QString &new_format)
Definition: String2DateTimeFilter.cpp:135