9#include <QStandardItemModel>
16void saveBinary(QDataStream &stream,
const QStandardItemModel *model,
const int inputPorts)
20 stream << static_cast<qint64>(inputPorts);
21 stream << static_cast<qint64>(model->columnCount());
23 for (
int col = 0; col < model->columnCount(); ++col) {
24 for (
int row = 0; row < inputPorts; ++row) {
25 stream << static_cast<qint64>(model->index(row, col).data().toInt());
32 qint64 rows; stream >> rows;
33 qint64 cols; stream >> cols;
38 if (rows > maxInputPorts) {
39 qCWarning(zero) <<
"DolphinSerializer: truncating waveform from" << rows <<
"rows to maximum" << maxInputPorts;
45 "Invalid number of columns: got %1, expected between 2 and %2.",
51 data.
columns =
static_cast<int>(cols);
55 for (
int col = 0; col < data.
columns; ++col) {
56 for (
int row = 0; row < data.
inputPorts; ++row) {
57 qint64 value; stream >> value;
58 data.
values[row * data.
columns + col] =
static_cast<int>(value);
65void saveCSV(QSaveFile &file,
const QStandardItemModel *model)
69 file.write(QString::number(model->rowCount()).toUtf8());
71 file.write(QString::number(model->columnCount()).toUtf8());
74 for (
int row = 0; row < model->rowCount(); ++row) {
75 for (
int col = 0; col < model->columnCount(); ++col) {
76 file.write(model->index(row, col).data().toString().toUtf8());
88 const QByteArray content = file.readAll();
89 const auto wordList = content.split(
',');
91 if (wordList.size() < 2) {
95 int rows = wordList.at(0).toInt();
96 const int cols = wordList.at(1).toInt();
101 if (rows > maxInputPorts) {
102 qCWarning(zero) <<
"DolphinSerializer: truncating waveform from" << rows <<
"rows to maximum" << maxInputPorts;
103 rows = maxInputPorts;
108 "Invalid number of columns: got %1, expected between 2 and %2.",
113 const qsizetype expectedSize = 2 +
static_cast<qsizetype
>(rows) * cols;
114 if (wordList.size() < expectedSize) {
118 "Invalid CSV format: expected %1 elements, got %2.",
119 QString::number(expectedSize), QString::number(wordList.size()));
125 data.
values.resize(
static_cast<qsizetype
>(rows) * cols);
128 bool hadInvalidValues =
false;
129 for (
int row = 0; row < rows; ++row) {
130 for (
int col = 0; col < cols; ++col) {
131 const int raw = wordList.at(2 + col + row * cols).toInt();
132 if (raw != 0 && raw != 1) {
133 hadInvalidValues =
true;
135 data.
values[row * cols + col] = (raw == 1) ? 1 : 0;
138 if (hadInvalidValues) {
139 qCWarning(zero) <<
"DolphinSerializer: CSV contained non-binary cell values — clamped to 0/1";
Common logging utilities, the Pandaception error type, and helper macros.
#define PANDACEPTION_WITH_CONTEXT(context, msg,...)
DolphinSerializer: encoding and decoding of .dolphin and .csv waveform files.
SignalModel for the beWavedDolphin waveform table.
static constexpr int kMaxColumns
Pure I/O functions for the beWavedDolphin waveform file formats.
void saveBinary(QDataStream &stream, const QStandardItemModel *model, const int inputPorts)
Serializes input rows to the binary .dolphin stream.
void saveCSV(QSaveFile &file, const QStandardItemModel *model)
Serializes all rows (inputs + outputs) to a .csv file.
WaveformData loadCSV(QFile &file, const int maxInputPorts)
Deserializes input rows from a .csv file.
WaveformData loadBinary(QDataStream &stream, const int maxInputPorts)
Deserializes input rows from a binary .dolphin stream.