DataToReplay PrepareReplay(const QString& file, SignalData& signalData) { SignalData data; PlotInfo info; WorkspaceSerializer::Read(file, data, info); DataMap toSet; vector < pair<QString, QVector<qreal>>> toSend; data.onSignals([&](const Signal& signal){ toSet.emplace(signal.name, Signal{ signal.name, {}, signal.graphic }); toSend.emplace_back(make_pair(signal.name, std::move(signal.y))); }); for (auto& signal : toSet) { signalData.addIfNotExists(std::move(signal.second)); } signalData.setAsDomain(data.getDomain()->name); return toSend; }
void Read(const QString& fileName, SignalData& data, PlotInfo& plotInfo, std::function<void(Signal&&)> onSignal, std::function<void()> onAfterSignalRead) { QFile in(fileName); if (!in.open(QIODevice::ReadOnly | QFile::Text)) { auto mex = QString("Cannot open specified file -> %1").arg(fileName); QMessageBox::critical(nullptr, "Anvedi", mex); throw std::exception(mex.toUtf8().data()); } const auto json = QJsonDocument::fromJson(in.readAll()).object(); auto backgroundIt = json.find("background"); if (backgroundIt != json.end()) { plotInfo.setBackgroundColor(backgroundIt->toString()); } auto graphIt = json.find("signals"); if ((graphIt != json.end()) && graphIt->isArray()) { for (const auto& elem : graphIt->toArray()) { if (elem.isObject()) { onSignal(ReadGraph(elem.toObject())); } } } onAfterSignalRead(); auto domainIt = json.find("domain"); if (domainIt != json.end()) { const auto domainName = domainIt->toString(); try { data.setAsDomain(domainName); } catch (const std::exception&){} } }
void WorkspaceSerializerTests::WriteTest() { DataMap dataMap = { { "cubic", { "cubic", { 1, 8, 27, 64, 125, 216, 343, 512, 729, 1000 }, { "#008000", true, 1, 1000 } } }, { "line", { "line", { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }, { "#ffff00", false, 1, 10 } } } }; PlotInfo plot; plot.setBackgroundColor("red"); SignalData data; data.add(dataMap); data.setAsDomain("line"); QString tmpFileName = std::tmpnam(nullptr); struct guard { guard(const QString& fileName) : fileName(fileName) { } ~guard() { QFile f(fileName); f.remove(); } const QString& fileName; } fileDeleter(tmpFileName); WorkspaceSerializer::Write(tmpFileName, data, plot); VerifyActualDataAndPersistedFile(tmpFileName, dataMap, "line", "red"); }