VisualizationGraphHelper.cpp
239 lines
| 8.0 KiB
| text/x-c
|
CppLexer
r243 | #include "Visualization/VisualizationGraphHelper.h" | |||
|
r181 | #include "Visualization/qcustomplot.h" | ||
|
r584 | #include <Common/ColorUtils.h> | ||
|
r182 | #include <Data/ScalarSeries.h> | ||
|
r583 | #include <Data/VectorSeries.h> | ||
|
r182 | |||
|
r181 | #include <Variable/Variable.h> | ||
r243 | Q_LOGGING_CATEGORY(LOG_VisualizationGraphHelper, "VisualizationGraphHelper") | |||
|
r181 | |||
|
r182 | namespace { | ||
r364 | class SqpDataContainer : public QCPGraphDataContainer { | |||
public: | ||||
|
r453 | void appendGraphData(const QCPGraphData &data) { mData.append(data); } | ||
r364 | }; | |||
|
r183 | /// Format for datetimes on a axis | ||
const auto DATETIME_TICKER_FORMAT = QStringLiteral("yyyy/MM/dd \nhh:mm:ss"); | ||||
/// Generates the appropriate ticker for an axis, depending on whether the axis displays time or | ||||
/// non-time data | ||||
QSharedPointer<QCPAxisTicker> axisTicker(bool isTimeAxis) | ||||
{ | ||||
if (isTimeAxis) { | ||||
auto dateTicker = QSharedPointer<QCPAxisTickerDateTime>::create(); | ||||
dateTicker->setDateTimeFormat(DATETIME_TICKER_FORMAT); | ||||
|
r490 | dateTicker->setDateTimeSpec(Qt::UTC); | ||
|
r183 | |||
return dateTicker; | ||||
} | ||||
else { | ||||
// default ticker | ||||
return QSharedPointer<QCPAxisTicker>::create(); | ||||
} | ||||
} | ||||
|
r182 | |||
|
r583 | /// Sets axes properties according to the properties of a data series | ||
template <int Dim> | ||||
void setAxesProperties(const DataSeries<Dim> &dataSeries, QCustomPlot &plot) noexcept | ||||
r235 | { | |||
|
r583 | /// @todo : for the moment, no control is performed on the axes: the units and the tickers | ||
/// are fixed for the default x-axis and y-axis of the plot, and according to the new graph | ||||
auto setAxisProperties = [](auto axis, const auto &unit) { | ||||
// label (unit name) | ||||
axis->setLabel(unit.m_Name); | ||||
// ticker (depending on the type of unit) | ||||
axis->setTicker(axisTicker(unit.m_TimeUnit)); | ||||
}; | ||||
setAxisProperties(plot.xAxis, dataSeries.xAxisUnit()); | ||||
setAxisProperties(plot.yAxis, dataSeries.valuesUnit()); | ||||
} | ||||
r315 | ||||
|
r583 | /** | ||
* Struct used to create plottables, depending on the type of the data series from which to create them | ||||
* @tparam T the data series' type | ||||
* @remarks Default implementation can't create plottables | ||||
*/ | ||||
template <typename T, typename Enabled = void> | ||||
struct PlottablesCreator { | ||||
static PlottablesMap createPlottables(T &, QCustomPlot &) | ||||
{ | ||||
qCCritical(LOG_DataSeries()) | ||||
<< QObject::tr("Can't create plottables: unmanaged data series type"); | ||||
return {}; | ||||
r235 | } | |||
|
r583 | }; | ||
r235 | ||||
|
r583 | /** | ||
* Specialization of PlottablesCreator for scalars and vectors | ||||
* @sa ScalarSeries | ||||
* @sa VectorSeries | ||||
*/ | ||||
template <typename T> | ||||
struct PlottablesCreator<T, | ||||
typename std::enable_if_t<std::is_base_of<ScalarSeries, T>::value | ||||
or std::is_base_of<VectorSeries, T>::value> > { | ||||
static PlottablesMap createPlottables(T &dataSeries, QCustomPlot &plot) | ||||
{ | ||||
PlottablesMap result{}; | ||||
// Gets the number of components of the data series | ||||
auto componentCount = dataSeries.valuesData()->componentCount(); | ||||
|
r584 | auto colors = ColorUtils::colors(Qt::blue, Qt::red, componentCount); | ||
|
r583 | // For each component of the data series, creates a QCPGraph to add to the plot | ||
for (auto i = 0; i < componentCount; ++i) { | ||||
auto graph = plot.addGraph(); | ||||
|
r584 | graph->setPen(QPen{colors.at(i)}); | ||
|
r583 | |||
result.insert({i, graph}); | ||||
} | ||||
r548 | ||||
// Axes properties | ||||
|
r583 | setAxesProperties(dataSeries, plot); | ||
plot.replot(); | ||||
return result; | ||||
r548 | } | |||
|
r583 | }; | ||
r548 | ||||
|
r583 | /** | ||
* Struct used to update plottables, depending on the type of the data series from which to update them | ||||
* @tparam T the data series' type | ||||
* @remarks Default implementation can't update plottables | ||||
*/ | ||||
template <typename T, typename Enabled = void> | ||||
struct PlottablesUpdater { | ||||
static void updatePlottables(T &, PlottablesMap &, const SqpRange &, bool) | ||||
{ | ||||
qCCritical(LOG_DataSeries()) | ||||
<< QObject::tr("Can't update plottables: unmanaged data series type"); | ||||
} | ||||
}; | ||||
|
r182 | |||
|
r583 | /** | ||
* Specialization of PlottablesUpdater for scalars and vectors | ||||
* @sa ScalarSeries | ||||
* @sa VectorSeries | ||||
*/ | ||||
template <typename T> | ||||
struct PlottablesUpdater<T, | ||||
typename std::enable_if_t<std::is_base_of<ScalarSeries, T>::value | ||||
or std::is_base_of<VectorSeries, T>::value> > { | ||||
static void updatePlottables(T &dataSeries, PlottablesMap &plottables, const SqpRange &range, | ||||
bool rescaleAxes) | ||||
{ | ||||
dataSeries.lockRead(); | ||||
// For each plottable to update, resets its data | ||||
std::map<int, QSharedPointer<SqpDataContainer> > dataContainers{}; | ||||
for (const auto &plottable : plottables) { | ||||
if (auto graph = dynamic_cast<QCPGraph *>(plottable.second)) { | ||||
auto dataContainer = QSharedPointer<SqpDataContainer>::create(); | ||||
graph->setData(dataContainer); | ||||
dataContainers.insert({plottable.first, dataContainer}); | ||||
} | ||||
} | ||||
|
r182 | |||
|
r583 | // - Gets the data of the series included in the current range | ||
// - Updates each plottable by adding, for each data item, a point that takes x-axis data and value data. The correct value is retrieved according to the index of the component | ||||
auto subDataIts = dataSeries.subData(range.m_TStart, range.m_TEnd); | ||||
for (auto it = subDataIts.first; it != subDataIts.second; ++it) { | ||||
for (const auto &dataContainer : dataContainers) { | ||||
auto componentIndex = dataContainer.first; | ||||
dataContainer.second->appendGraphData( | ||||
QCPGraphData(it->x(), it->value(componentIndex))); | ||||
} | ||||
} | ||||
r235 | ||||
|
r583 | dataSeries.unlock(); | ||
|
r183 | |||
|
r583 | if (!plottables.empty()) { | ||
auto plot = plottables.begin()->second->parentPlot(); | ||||
|
r183 | |||
|
r583 | if (rescaleAxes) { | ||
plot->rescaleAxes(); | ||||
} | ||||
|
r183 | |||
|
r583 | plot->replot(); | ||
} | ||||
|
r182 | } | ||
|
r583 | }; | ||
/** | ||||
* Helper used to create/update plottables | ||||
*/ | ||||
struct IPlottablesHelper { | ||||
virtual ~IPlottablesHelper() noexcept = default; | ||||
virtual PlottablesMap create(QCustomPlot &plot) const = 0; | ||||
virtual void update(PlottablesMap &plottables, const SqpRange &range, | ||||
bool rescaleAxes = false) const = 0; | ||||
}; | ||||
/** | ||||
* Default implementation of IPlottablesHelper, which takes data series to create/update plottables | ||||
* @tparam T the data series' type | ||||
*/ | ||||
template <typename T> | ||||
struct PlottablesHelper : public IPlottablesHelper { | ||||
explicit PlottablesHelper(T &dataSeries) : m_DataSeries{dataSeries} {} | ||||
PlottablesMap create(QCustomPlot &plot) const override | ||||
{ | ||||
return PlottablesCreator<T>::createPlottables(m_DataSeries, plot); | ||||
|
r182 | } | ||
|
r583 | void update(PlottablesMap &plottables, const SqpRange &range, bool rescaleAxes) const override | ||
{ | ||||
PlottablesUpdater<T>::updatePlottables(m_DataSeries, plottables, range, rescaleAxes); | ||||
} | ||||
|
r182 | |||
|
r583 | T &m_DataSeries; | ||
}; | ||||
|
r182 | |||
|
r583 | /// Creates IPlottablesHelper according to a data series | ||
std::unique_ptr<IPlottablesHelper> createHelper(IDataSeries *dataSeries) noexcept | ||||
r548 | { | |||
|
r583 | if (auto scalarSeries = dynamic_cast<ScalarSeries *>(dataSeries)) { | ||
return std::make_unique<PlottablesHelper<ScalarSeries> >(*scalarSeries); | ||||
} | ||||
else if (auto vectorSeries = dynamic_cast<VectorSeries *>(dataSeries)) { | ||||
return std::make_unique<PlottablesHelper<VectorSeries> >(*vectorSeries); | ||||
r548 | } | |||
else { | ||||
|
r583 | return std::make_unique<PlottablesHelper<IDataSeries> >(*dataSeries); | ||
r548 | } | |||
} | ||||
|
r583 | } // namespace | ||
|
r181 | |||
|
r583 | PlottablesMap VisualizationGraphHelper::create(std::shared_ptr<Variable> variable, | ||
QCustomPlot &plot) noexcept | ||||
{ | ||||
|
r181 | if (variable) { | ||
|
r583 | auto helper = createHelper(variable->dataSeries().get()); | ||
auto plottables = helper->create(plot); | ||||
return plottables; | ||||
|
r181 | } | ||
else { | ||||
r243 | qCDebug(LOG_VisualizationGraphHelper()) | |||
|
r181 | << QObject::tr("Can't create graph plottables : the variable is null"); | ||
|
r583 | return PlottablesMap{}; | ||
|
r181 | } | ||
} | ||||
r235 | ||||
|
r583 | void VisualizationGraphHelper::updateData(PlottablesMap &plottables, IDataSeries *dataSeries, | ||
r542 | const SqpRange &dateTime) | |||
r235 | { | |||
|
r583 | auto helper = createHelper(dataSeries); | ||
helper->update(plottables, dateTime); | ||||
r235 | } | |||