##// END OF EJS Templates
Passes TimeWidget in UTC...
Alexandre Leroux -
r489:e186227e6169
parent child
Show More
@@ -1,47 +1,50
1 1 #include "TimeWidget/TimeWidget.h"
2 2 #include "ui_TimeWidget.h"
3 3
4 #include <Common/DateUtils.h>
4 5 #include <SqpApplication.h>
5 6 #include <Time/TimeController.h>
6 7
7 8 TimeWidget::TimeWidget(QWidget *parent) : QWidget{parent}, ui{new Ui::TimeWidget}
8 9 {
9 10 ui->setupUi(this);
10 11
11 12 ui->applyToolButton->setIcon(sqpApp->style()->standardIcon(QStyle::SP_DialogApplyButton));
12 13
13 14 // Connection
14 15 connect(ui->startDateTimeEdit, &QDateTimeEdit::dateTimeChanged, this,
15 16 &TimeWidget::onTimeUpdateRequested);
16 17
17 18 connect(ui->endDateTimeEdit, &QDateTimeEdit::dateTimeChanged, this,
18 19 &TimeWidget::onTimeUpdateRequested);
19 20
20 21
21 22 connect(ui->applyToolButton, &QToolButton::clicked, &sqpApp->timeController(),
22 23 &TimeController::onTimeNotify);
23 24
24 25 // Initialisation
25 ui->startDateTimeEdit->setDateTime(
26 QDateTime::currentDateTime().addSecs(-3600)); // one hour berefore
27 ui->endDateTimeEdit->setDateTime(QDateTime::currentDateTime());
26 auto endDateTime = QDateTime::currentDateTimeUtc();
27 auto startDateTime = endDateTime.addSecs(-3600); // one hour before
28
29 ui->startDateTimeEdit->setDateTime(startDateTime);
30 ui->endDateTimeEdit->setDateTime(endDateTime);
31
32 auto dateTime = SqpDateTime{DateUtils::secondsSinceEpoch(startDateTime),
33 DateUtils::secondsSinceEpoch(endDateTime)};
28 34
29 auto dateTime
30 = SqpDateTime{QDateTime::currentDateTime().addSecs(-3600).toMSecsSinceEpoch() / 1000.0,
31 QDateTime::currentDateTime().toMSecsSinceEpoch() / 1000.0};
32 35 sqpApp->timeController().onTimeToUpdate(dateTime);
33 36 }
34 37
35 38
36 39 TimeWidget::~TimeWidget()
37 40 {
38 41 delete ui;
39 42 }
40 43
41 44 void TimeWidget::onTimeUpdateRequested()
42 45 {
43 46 auto dateTime = SqpDateTime{DateUtils::secondsSinceEpoch(ui->startDateTimeEdit->dateTime()),
44 47 DateUtils::secondsSinceEpoch(ui->endDateTimeEdit->dateTime())};
45 48
46 49 emit timeUpdated(std::move(dateTime));
47 50 }
@@ -1,92 +1,98
1 1 <?xml version="1.0" encoding="UTF-8"?>
2 2 <ui version="4.0">
3 3 <class>TimeWidget</class>
4 4 <widget class="QWidget" name="TimeWidget">
5 5 <property name="geometry">
6 6 <rect>
7 7 <x>0</x>
8 8 <y>0</y>
9 9 <width>716</width>
10 10 <height>48</height>
11 11 </rect>
12 12 </property>
13 13 <property name="sizePolicy">
14 14 <sizepolicy hsizetype="Fixed" vsizetype="Preferred">
15 15 <horstretch>0</horstretch>
16 16 <verstretch>0</verstretch>
17 17 </sizepolicy>
18 18 </property>
19 19 <property name="windowTitle">
20 20 <string>Form</string>
21 21 </property>
22 22 <layout class="QHBoxLayout" name="horizontalLayout_2">
23 23 <item>
24 24 <widget class="QLabel" name="label">
25 25 <property name="sizePolicy">
26 26 <sizepolicy hsizetype="Minimum" vsizetype="Preferred">
27 27 <horstretch>0</horstretch>
28 28 <verstretch>0</verstretch>
29 29 </sizepolicy>
30 30 </property>
31 31 <property name="text">
32 32 <string>TStart :</string>
33 33 </property>
34 34 </widget>
35 35 </item>
36 36 <item>
37 37 <widget class="QDateTimeEdit" name="startDateTimeEdit">
38 38 <property name="sizePolicy">
39 39 <sizepolicy hsizetype="Preferred" vsizetype="Fixed">
40 40 <horstretch>0</horstretch>
41 41 <verstretch>0</verstretch>
42 42 </sizepolicy>
43 43 </property>
44 44 <property name="displayFormat">
45 45 <string>dd/MM/yyyy HH:mm:ss:zzz</string>
46 46 </property>
47 47 <property name="calendarPopup">
48 48 <bool>true</bool>
49 49 </property>
50 <property name="timeSpec">
51 <enum>Qt::UTC</enum>
52 </property>
50 53 </widget>
51 54 </item>
52 55 <item>
53 56 <widget class="QLabel" name="label_2">
54 57 <property name="sizePolicy">
55 58 <sizepolicy hsizetype="Minimum" vsizetype="Preferred">
56 59 <horstretch>0</horstretch>
57 60 <verstretch>0</verstretch>
58 61 </sizepolicy>
59 62 </property>
60 63 <property name="text">
61 64 <string>TEnd :</string>
62 65 </property>
63 66 </widget>
64 67 </item>
65 68 <item>
66 69 <widget class="QDateTimeEdit" name="endDateTimeEdit">
67 70 <property name="sizePolicy">
68 71 <sizepolicy hsizetype="Preferred" vsizetype="Fixed">
69 72 <horstretch>0</horstretch>
70 73 <verstretch>0</verstretch>
71 74 </sizepolicy>
72 75 </property>
73 76 <property name="displayFormat">
74 77 <string>dd/MM/yyyy HH:mm:ss:zzz</string>
75 78 </property>
76 79 <property name="calendarPopup">
77 80 <bool>true</bool>
78 81 </property>
82 <property name="timeSpec">
83 <enum>Qt::UTC</enum>
84 </property>
79 85 </widget>
80 86 </item>
81 87 <item>
82 88 <widget class="QToolButton" name="applyToolButton">
83 89 <property name="text">
84 90 <string>...</string>
85 91 </property>
86 92 </widget>
87 93 </item>
88 94 </layout>
89 95 </widget>
90 96 <resources/>
91 97 <connections/>
92 98 </ui>
@@ -1,145 +1,147
1 1 #include "AmdaResultParser.h"
2 2
3 #include <Common/DateUtils.h>
3 4 #include <Data/ScalarSeries.h>
4 5
5 6 #include <QDateTime>
6 7 #include <QFile>
7 8 #include <QRegularExpression>
8 9
9 10 #include <cmath>
10 11
11 12 Q_LOGGING_CATEGORY(LOG_AmdaResultParser, "AmdaResultParser")
12 13
13 14 namespace {
14 15
15 16 /// Message in result file when the file was not found on server
16 17 const auto FILE_NOT_FOUND_MESSAGE = QStringLiteral("Not Found");
17 18
18 19 /// Format for dates in result files
19 20 const auto DATE_FORMAT = QStringLiteral("yyyy-MM-ddThh:mm:ss.zzz");
20 21
21 22 /// Separator between values in a result line
22 23 const auto RESULT_LINE_SEPARATOR = QRegularExpression{QStringLiteral("\\s+")};
23 24
24 25 /// Regex to find unit in a line. Examples of valid lines:
25 26 /// ... - Units : nT - ...
26 27 /// ... -Units:nT- ...
27 28 /// ... -Units: mΒ²- ...
28 29 /// ... - Units : m/s - ...
29 30 const auto UNIT_REGEX = QRegularExpression{QStringLiteral("-\\s*Units\\s*:\\s*(.+?)\\s*-")};
30 31
31 32 /// Converts a string date to a double date
32 33 /// @return a double that represents the date in seconds, NaN if the string date can't be converted
33 34 double doubleDate(const QString &stringDate) noexcept
34 35 {
35 36 auto dateTime = QDateTime::fromString(stringDate, DATE_FORMAT);
36 return dateTime.isValid() ? (dateTime.toMSecsSinceEpoch() / 1000.)
37 dateTime.setTimeSpec(Qt::UTC);
38 return dateTime.isValid() ? DateUtils::secondsSinceEpoch(dateTime)
37 39 : std::numeric_limits<double>::quiet_NaN();
38 40 }
39 41
40 42 /**
41 43 * Reads stream to retrieve x-axis unit
42 44 * @param stream the stream to read
43 45 * @return the unit that has been read in the stream, a default unit (time unit with no label) if an
44 46 * error occured during reading
45 47 */
46 48 Unit readXAxisUnit(QTextStream &stream)
47 49 {
48 50 QString line{};
49 51
50 52 if (stream.readLineInto(&line)) {
51 53 auto match = UNIT_REGEX.match(line);
52 54 if (match.hasMatch()) {
53 55 return Unit{match.captured(1), true};
54 56 }
55 57 else {
56 58 qCWarning(LOG_AmdaResultParser())
57 59 << QObject::tr("Can't read unit: invalid line %1").arg(line);
58 60 }
59 61 }
60 62 else {
61 63 qCWarning(LOG_AmdaResultParser()) << QObject::tr("Can't read unit: end of file");
62 64 }
63 65
64 66 // Error cases
65 67 return Unit{{}, true};
66 68 }
67 69
68 70 /**
69 71 * Reads stream to retrieve results
70 72 * @param stream the stream to read
71 73 * @return the pair of vectors x-axis data/values data that has been read in the stream
72 74 */
73 75 QPair<QVector<double>, QVector<double> > readResults(QTextStream &stream)
74 76 {
75 77 auto xData = QVector<double>{};
76 78 auto valuesData = QVector<double>{};
77 79
78 80 QString line{};
79 81 while (stream.readLineInto(&line)) {
80 82 auto lineData = line.split(RESULT_LINE_SEPARATOR, QString::SkipEmptyParts);
81 83 if (lineData.size() == 2) {
82 84 // X : the data is converted from date to double (in secs)
83 85 auto x = doubleDate(lineData.at(0));
84 86
85 87 // Value
86 88 bool valueOk;
87 89 auto value = lineData.at(1).toDouble(&valueOk);
88 90
89 91 // Adds result only if x and value are valid
90 92 if (!std::isnan(x) && !std::isnan(value) && valueOk) {
91 93 xData.push_back(x);
92 94 valuesData.push_back(value);
93 95 }
94 96 else {
95 97 qCWarning(LOG_AmdaResultParser())
96 98 << QObject::tr(
97 99 "Can't retrieve results from line %1: x and/or value are invalid")
98 100 .arg(line);
99 101 }
100 102 }
101 103 else {
102 104 qCWarning(LOG_AmdaResultParser())
103 105 << QObject::tr("Can't retrieve results from line %1: invalid line").arg(line);
104 106 }
105 107 }
106 108
107 109 return qMakePair(std::move(xData), std::move(valuesData));
108 110 }
109 111
110 112 } // namespace
111 113
112 114 std::shared_ptr<IDataSeries> AmdaResultParser::readTxt(const QString &filePath) noexcept
113 115 {
114 116 QFile file{filePath};
115 117
116 118 if (!file.open(QFile::ReadOnly | QIODevice::Text)) {
117 119 qCCritical(LOG_AmdaResultParser())
118 120 << QObject::tr("Can't retrieve AMDA data from file %1: %2")
119 121 .arg(filePath, file.errorString());
120 122 return nullptr;
121 123 }
122 124
123 125 QTextStream stream{&file};
124 126
125 127 // Checks if the file was found on the server
126 128 auto firstLine = stream.readLine();
127 129 if (firstLine.compare(FILE_NOT_FOUND_MESSAGE) == 0) {
128 130 qCCritical(LOG_AmdaResultParser())
129 131 << QObject::tr("Can't retrieve AMDA data from file %1: file was not found on server")
130 132 .arg(filePath);
131 133 return nullptr;
132 134 }
133 135
134 136 // Ignore comments lines
135 137 stream.readLine();
136 138
137 139 // Reads x-axis unit
138 140 auto xAxisUnit = readXAxisUnit(stream);
139 141
140 142 // Reads results
141 143 auto results = readResults(stream);
142 144
143 145 return std::make_shared<ScalarSeries>(std::move(results.first), std::move(results.second),
144 146 xAxisUnit, Unit{});
145 147 }
General Comments 0
You need to be logged in to leave comments. Login now