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