Work with references & not pointers

This commit is contained in:
Alex Spataru 2022-01-02 00:05:15 -05:00
parent d9a20ad574
commit 1769fcfcff
19 changed files with 663 additions and 786 deletions

View File

@ -367,33 +367,24 @@ bool JSON::Editor::saveJsonFile()
*/ */
int JSON::Editor::datasetCount(const int group) const int JSON::Editor::datasetCount(const int group) const
{ {
if (group < groupCount()) return getGroup(group).m_datasets.count();
return m_groups.at(group)->m_datasets.count();
return 0;
} }
/** /**
* Returns a pointer to the group object positioned at the given @a index * Returns a pointer to the group object positioned at the given @a index
*/ */
JSON::Group *JSON::Editor::getGroup(const int index) const const JSON::Group &JSON::Editor::getGroup(const int index) const
{ {
if (index < groupCount()) return m_groups.at(index);
return m_groups.at(index);
return Q_NULLPTR;
} }
/** /**
* Returns a pointer to the dataset object contained by the @a group at * Returns a pointer to the dataset object contained by the @a group at
* the given @a index * the given @a index
*/ */
JSON::Dataset *JSON::Editor::getDataset(const int group, const int index) const const JSON::Dataset &JSON::Editor::getDataset(const int group, const int index) const
{ {
if (index < datasetCount(group)) return getGroup(group).getDataset(index);
return getGroup(group)->m_datasets.at(index);
return Q_NULLPTR;
} }
/** /**
@ -401,11 +392,7 @@ JSON::Dataset *JSON::Editor::getDataset(const int group, const int index) const
*/ */
QString JSON::Editor::groupTitle(const int group) const QString JSON::Editor::groupTitle(const int group) const
{ {
const auto grp = getGroup(group); return getGroup(group).title();
if (grp)
return grp->m_title;
return "";
} }
/** /**
@ -413,11 +400,7 @@ QString JSON::Editor::groupTitle(const int group) const
*/ */
QString JSON::Editor::groupWidget(const int group) const QString JSON::Editor::groupWidget(const int group) const
{ {
const auto grp = getGroup(group); return getGroup(group).widget();
if (grp)
return grp->m_widget;
return "";
} }
/** /**
@ -428,23 +411,19 @@ QString JSON::Editor::groupWidget(const int group) const
*/ */
int JSON::Editor::groupWidgetIndex(const int group) const int JSON::Editor::groupWidgetIndex(const int group) const
{ {
const auto grp = getGroup(group); auto widget = groupWidget(group);
if (grp)
{
const auto widget = grp->m_widget;
if (widget == "accelerometer") if (widget == "accelerometer")
return 1; return 1;
if (widget == "gyro") if (widget == "gyro")
return 2; return 2;
if (widget == "map") if (widget == "map")
return 3; return 3;
if (widget == "multiplot") if (widget == "multiplot")
return 4; return 4;
}
return 0; return 0;
} }
@ -458,11 +437,7 @@ int JSON::Editor::groupWidgetIndex(const int group) const
*/ */
int JSON::Editor::datasetIndex(const int group, const int dataset) const int JSON::Editor::datasetIndex(const int group, const int dataset) const
{ {
const auto set = getDataset(group, dataset); return getDataset(group, dataset).m_index;
if (set)
return set->m_index;
return 0;
} }
/** /**
@ -474,11 +449,7 @@ int JSON::Editor::datasetIndex(const int group, const int dataset) const
*/ */
bool JSON::Editor::datasetLED(const int group, const int dataset) const bool JSON::Editor::datasetLED(const int group, const int dataset) const
{ {
const auto set = getDataset(group, dataset); return getDataset(group, dataset).led();
if (set)
return set->led();
return false;
} }
/** /**
@ -490,11 +461,7 @@ bool JSON::Editor::datasetLED(const int group, const int dataset) const
*/ */
bool JSON::Editor::datasetGraph(const int group, const int dataset) const bool JSON::Editor::datasetGraph(const int group, const int dataset) const
{ {
const auto set = getDataset(group, dataset); return getDataset(group, dataset).graph();
if (set)
return set->graph();
return false;
} }
/** /**
@ -506,11 +473,7 @@ bool JSON::Editor::datasetGraph(const int group, const int dataset) const
*/ */
bool JSON::Editor::datasetFftPlot(const int group, const int dataset) const bool JSON::Editor::datasetFftPlot(const int group, const int dataset) const
{ {
const auto set = getDataset(group, dataset); return getDataset(group, dataset).fft();
if (set)
return set->fft();
return false;
} }
/** /**
@ -523,11 +486,7 @@ bool JSON::Editor::datasetFftPlot(const int group, const int dataset) const
*/ */
bool JSON::Editor::datasetLogPlot(const int group, const int dataset) const bool JSON::Editor::datasetLogPlot(const int group, const int dataset) const
{ {
const auto set = getDataset(group, dataset); return getDataset(group, dataset).log();
if (set)
return set->log();
return false;
} }
/** /**
@ -538,11 +497,7 @@ bool JSON::Editor::datasetLogPlot(const int group, const int dataset) const
*/ */
QString JSON::Editor::datasetTitle(const int group, const int dataset) const QString JSON::Editor::datasetTitle(const int group, const int dataset) const
{ {
const auto set = getDataset(group, dataset); return getDataset(group, dataset).title();
if (set)
return set->title();
return "";
} }
/** /**
@ -553,11 +508,7 @@ QString JSON::Editor::datasetTitle(const int group, const int dataset) const
*/ */
QString JSON::Editor::datasetUnits(const int group, const int dataset) const QString JSON::Editor::datasetUnits(const int group, const int dataset) const
{ {
const auto set = getDataset(group, dataset); return getDataset(group, dataset).units();
if (set)
return set->units();
return "";
} }
/** /**
@ -568,11 +519,7 @@ QString JSON::Editor::datasetUnits(const int group, const int dataset) const
*/ */
QString JSON::Editor::datasetWidget(const int group, const int dataset) const QString JSON::Editor::datasetWidget(const int group, const int dataset) const
{ {
const auto set = getDataset(group, dataset); return getDataset(group, dataset).widget();
if (set)
return set->widget();
return "";
} }
/** /**
@ -585,7 +532,8 @@ QString JSON::Editor::datasetWidget(const int group, const int dataset) const
*/ */
int JSON::Editor::datasetWidgetIndex(const int group, const int dataset) const int JSON::Editor::datasetWidgetIndex(const int group, const int dataset) const
{ {
const auto widget = datasetWidget(group, dataset); auto widget = datasetWidget(group, dataset);
if (widget == "gauge") if (widget == "gauge")
return 1; return 1;
if (widget == "bar") if (widget == "bar")
@ -605,11 +553,7 @@ int JSON::Editor::datasetWidgetIndex(const int group, const int dataset) const
*/ */
QString JSON::Editor::datasetWidgetMin(const int group, const int dataset) const QString JSON::Editor::datasetWidgetMin(const int group, const int dataset) const
{ {
const auto set = getDataset(group, dataset); return QString::number(getDataset(group, dataset).min());
if (set)
return QString::number(set->m_min);
return "0";
} }
/** /**
@ -621,11 +565,7 @@ QString JSON::Editor::datasetWidgetMin(const int group, const int dataset) const
*/ */
QString JSON::Editor::datasetWidgetMax(const int group, const int dataset) const QString JSON::Editor::datasetWidgetMax(const int group, const int dataset) const
{ {
const auto set = getDataset(group, dataset); return QString::number(getDataset(group, dataset).max());
if (set)
return QString::number(set->m_max);
return "0";
} }
/** /**
@ -636,11 +576,7 @@ QString JSON::Editor::datasetWidgetMax(const int group, const int dataset) const
*/ */
QString JSON::Editor::datasetFFTSamples(const int group, const int dataset) const QString JSON::Editor::datasetFFTSamples(const int group, const int dataset) const
{ {
const auto set = getDataset(group, dataset); return QString::number(getDataset(group, dataset).fftSamples());
if (set)
return QString::number(qMax(1, set->m_fftSamples));
return "0";
} }
/** /**
@ -652,16 +588,12 @@ QString JSON::Editor::datasetFFTSamples(const int group, const int dataset) cons
*/ */
QString JSON::Editor::datasetWidgetAlarm(const int group, const int dataset) const QString JSON::Editor::datasetWidgetAlarm(const int group, const int dataset) const
{ {
const auto set = getDataset(group, dataset); auto set = getDataset(group, dataset);
if (set)
{
if (set->m_alarm <= set->min())
return QString::number(set->m_max);
else
return QString::number(set->m_alarm);
}
return "0"; if (set.alarm() <= set.min())
return QString::number(set.max());
return QString::number(set.alarm());
} }
//---------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------
@ -674,7 +606,6 @@ QString JSON::Editor::datasetWidgetAlarm(const int group, const int dataset) con
void JSON::Editor::newJsonFile() void JSON::Editor::newJsonFile()
{ {
// Clear groups list // Clear groups list
qDeleteAll(m_groups);
m_groups.clear(); m_groups.clear();
// Reset project properties // Reset project properties
@ -862,7 +793,7 @@ void JSON::Editor::setFrameStartSequence(const QString &sequence)
*/ */
void JSON::Editor::addGroup() void JSON::Editor::addGroup()
{ {
m_groups.append(new Group); m_groups.append(Group());
setGroupTitle(m_groups.count() - 1, tr("New Group")); setGroupTitle(m_groups.count() - 1, tr("New Group"));
Q_EMIT groupCountChanged(); Q_EMIT groupCountChanged();
@ -874,18 +805,15 @@ void JSON::Editor::addGroup()
*/ */
void JSON::Editor::deleteGroup(const int group) void JSON::Editor::deleteGroup(const int group)
{ {
const auto grp = getGroup(group); auto ret = Misc::Utilities::showMessageBox(
if (grp) tr("Delete group \"%1\"").arg(groupTitle(group)),
tr("Are you sure you want to delete this group?"), APP_NAME,
QMessageBox::Yes | QMessageBox::No);
if (ret == QMessageBox::Yes)
{ {
auto ret = Misc::Utilities::showMessageBox( m_groups.removeAt(group);
tr("Delete group \"%1\"").arg(grp->title()), Q_EMIT groupCountChanged();
tr("Are you sure you want to delete this group?"), APP_NAME,
QMessageBox::Yes | QMessageBox::No);
if (ret == QMessageBox::Yes)
{
m_groups.removeAt(group);
Q_EMIT groupCountChanged();
}
} }
} }
@ -921,148 +849,138 @@ void JSON::Editor::moveGroupDown(const int group)
bool JSON::Editor::setGroupWidget(const int group, const int widgetId) bool JSON::Editor::setGroupWidget(const int group, const int widgetId)
{ {
auto grp = getGroup(group); auto grp = getGroup(group);
if (grp)
// Warn user if group contains existing datasets
if (!(grp.m_datasets.isEmpty()) && widgetId != 4)
{ {
// Warn user if group contains existing datasets if (widgetId == 0 && grp.widget() == "multiplot")
if (!(grp->m_datasets.isEmpty()) && widgetId != 4) grp.m_widget = "";
{
if (widgetId == 0 && grp->widget() == "multiplot")
grp->m_widget = "";
else
{
auto ret = Misc::Utilities::showMessageBox(
tr("Are you sure you want to change the group-level widget?"),
tr("Existing datasets for this group will be deleted"), APP_NAME,
QMessageBox::Yes | QMessageBox::No);
if (ret == QMessageBox::No)
return false;
else else
{ grp.m_datasets.clear();
auto ret = Misc::Utilities::showMessageBox(
tr("Are you sure you want to change the group-level widget?"),
tr("Existing datasets for this group will be deleted"), APP_NAME,
QMessageBox::Yes | QMessageBox::No);
if (ret == QMessageBox::No)
return false;
else
{
qDeleteAll(grp->m_datasets);
grp->m_datasets.clear();
}
}
} }
// Accelerometer widget
if (widgetId == 1)
{
// Set widget title
grp->m_widget = "accelerometer";
grp->m_title = tr("Accelerometer");
// Create datasets
auto x = new Dataset;
auto y = new Dataset;
auto z = new Dataset;
// Set dataset indexes
x->m_index = nextDatasetIndex();
y->m_index = nextDatasetIndex() + 1;
z->m_index = nextDatasetIndex() + 2;
// Set measurement units
x->m_units = "m/s²";
y->m_units = "m/s²";
z->m_units = "m/s²";
// Set dataset properties
x->m_widget = "x";
y->m_widget = "y";
z->m_widget = "z";
x->m_title = tr("Accelerometer %1").arg("X");
y->m_title = tr("Accelerometer %1").arg("Y");
z->m_title = tr("Accelerometer %1").arg("Z");
// Add datasets to group
grp->m_datasets.append(x);
grp->m_datasets.append(y);
grp->m_datasets.append(z);
}
// Gyroscope widget
else if (widgetId == 2)
{
// Set widget title
grp->m_widget = "gyro";
grp->m_title = tr("Gyroscope");
// Create datasets
auto x = new Dataset;
auto y = new Dataset;
auto z = new Dataset;
// Set dataset indexes
x->m_index = nextDatasetIndex();
y->m_index = nextDatasetIndex() + 1;
z->m_index = nextDatasetIndex() + 2;
// Set measurement units
x->m_units = "°";
y->m_units = "°";
z->m_units = "°";
// Set dataset properties
x->m_widget = "roll";
y->m_widget = "pitch";
z->m_widget = "yaw";
x->m_title = tr("Gyro %1").arg("Roll");
y->m_title = tr("Gyro %1").arg("Pitch");
z->m_title = tr("Gyro %1").arg("Yaw");
// Add datasets to group
grp->m_datasets.append(x);
grp->m_datasets.append(y);
grp->m_datasets.append(z);
}
// Map widget
else if (widgetId == 3)
{
// Set widget title
grp->m_widget = "map";
grp->m_title = tr("GPS");
// Create datasets
auto lat = new Dataset;
auto lon = new Dataset;
auto alt = new Dataset;
// Set dataset indexes
lat->m_index = nextDatasetIndex();
lon->m_index = nextDatasetIndex() + 1;
alt->m_index = nextDatasetIndex() + 2;
// Set measurement units
lat->m_units = "°";
lon->m_units = "°";
alt->m_units = "m";
// Set dataset properties
lat->m_widget = "lat";
lon->m_widget = "lon";
alt->m_widget = "alt";
lat->m_title = tr("Latitude");
lon->m_title = tr("Longitude");
alt->m_title = tr("Altitude");
// Add datasets to group
grp->m_datasets.append(lat);
grp->m_datasets.append(lon);
grp->m_datasets.append(alt);
}
// Multi plot widget
else if (widgetId == 4)
grp->m_widget = "multiplot";
// Update UI
Q_EMIT groupChanged(group);
return true;
} }
return false; // Accelerometer widget
if (widgetId == 1)
{
// Set widget title
grp.m_widget = "accelerometer";
grp.m_title = tr("Accelerometer");
// Create datasets
Dataset x, y, z;
// Set dataset indexes
x.m_index = nextDatasetIndex();
y.m_index = nextDatasetIndex() + 1;
z.m_index = nextDatasetIndex() + 2;
// Set measurement units
x.m_units = "m/s²";
y.m_units = "m/s²";
z.m_units = "m/s²";
// Set dataset properties
x.m_widget = "x";
y.m_widget = "y";
z.m_widget = "z";
x.m_title = tr("Accelerometer %1").arg("X");
y.m_title = tr("Accelerometer %1").arg("Y");
z.m_title = tr("Accelerometer %1").arg("Z");
// Add datasets to group
grp.m_datasets.append(x);
grp.m_datasets.append(y);
grp.m_datasets.append(z);
}
// Gyroscope widget
else if (widgetId == 2)
{
// Set widget title
grp.m_widget = "gyro";
grp.m_title = tr("Gyroscope");
// Create datasets
Dataset x, y, z;
// Set dataset indexes
x.m_index = nextDatasetIndex();
y.m_index = nextDatasetIndex() + 1;
z.m_index = nextDatasetIndex() + 2;
// Set measurement units
x.m_units = "°";
y.m_units = "°";
z.m_units = "°";
// Set dataset properties
x.m_widget = "roll";
y.m_widget = "pitch";
z.m_widget = "yaw";
x.m_title = tr("Gyro %1").arg("Roll");
y.m_title = tr("Gyro %1").arg("Pitch");
z.m_title = tr("Gyro %1").arg("Yaw");
// Add datasets to group
grp.m_datasets.append(x);
grp.m_datasets.append(y);
grp.m_datasets.append(z);
}
// Map widget
else if (widgetId == 3)
{
// Set widget title
grp.m_widget = "map";
grp.m_title = tr("GPS");
// Create datasets
Dataset lat, lon, alt;
// Set dataset indexes
lat.m_index = nextDatasetIndex();
lon.m_index = nextDatasetIndex() + 1;
alt.m_index = nextDatasetIndex() + 2;
// Set measurement units
lat.m_units = "°";
lon.m_units = "°";
alt.m_units = "m";
// Set dataset properties
lat.m_widget = "lat";
lon.m_widget = "lon";
alt.m_widget = "alt";
lat.m_title = tr("Latitude");
lon.m_title = tr("Longitude");
alt.m_title = tr("Altitude");
// Add datasets to group
grp.m_datasets.append(lat);
grp.m_datasets.append(lon);
grp.m_datasets.append(alt);
}
// Multi plot widget
else if (widgetId == 4)
grp.m_widget = "multiplot";
// Replace previous group with new group
m_groups.replace(group, grp);
// Update UI
Q_EMIT groupChanged(group);
return true;
} }
/** /**
@ -1070,12 +988,15 @@ bool JSON::Editor::setGroupWidget(const int group, const int widgetId)
*/ */
void JSON::Editor::setGroupTitle(const int group, const QString &title) void JSON::Editor::setGroupTitle(const int group, const QString &title)
{ {
// Get group
auto grp = getGroup(group); auto grp = getGroup(group);
if (grp)
{ // Change group values & update groups vector
grp->m_title = title; grp.m_title = title;
Q_EMIT groupChanged(group); m_groups.replace(group, grp);
}
// Update UI
Q_EMIT groupChanged(group);
} }
/** /**
@ -1083,12 +1004,15 @@ void JSON::Editor::setGroupTitle(const int group, const QString &title)
*/ */
void JSON::Editor::setGroupWidgetData(const int group, const QString &widget) void JSON::Editor::setGroupWidgetData(const int group, const QString &widget)
{ {
// Get group
auto grp = getGroup(group); auto grp = getGroup(group);
if (grp)
{ // Change group values & update groups vector
grp->m_widget = widget; grp.m_widget = widget;
Q_EMIT groupChanged(group); m_groups.replace(group, grp);
}
// Update UI
Q_EMIT groupChanged(group);
} }
/** /**
@ -1098,14 +1022,19 @@ void JSON::Editor::setGroupWidgetData(const int group, const QString &widget)
*/ */
void JSON::Editor::addDataset(const int group) void JSON::Editor::addDataset(const int group)
{ {
// Get group
auto grp = getGroup(group); auto grp = getGroup(group);
if (grp)
{ // Change group values & update groups vector
grp->m_datasets.append(new Dataset); grp.m_datasets.append(Dataset());
setDatasetIndex(group, grp->m_datasets.count() - 1, nextDatasetIndex()); m_groups.replace(group, grp);
setDatasetTitle(group, grp->m_datasets.count() - 1, tr("New dataset"));
Q_EMIT groupChanged(group); // Set dataset title & index
} setDatasetIndex(group, grp.m_datasets.count() - 1, nextDatasetIndex());
setDatasetTitle(group, grp.m_datasets.count() - 1, tr("New dataset"));
// Update UI
Q_EMIT groupChanged(group);
} }
/** /**
@ -1116,18 +1045,17 @@ void JSON::Editor::addDataset(const int group)
*/ */
void JSON::Editor::deleteDataset(const int group, const int dataset) void JSON::Editor::deleteDataset(const int group, const int dataset)
{ {
const auto set = getDataset(group, dataset); auto ret = Misc::Utilities::showMessageBox(
if (set) tr("Delete dataset \"%1\"").arg(getDataset(group, dataset).title()),
tr("Are you sure you want to delete this dataset?"), APP_NAME,
QMessageBox::Yes | QMessageBox::No);
if (ret == QMessageBox::Yes)
{ {
auto ret = Misc::Utilities::showMessageBox( auto grp = getGroup(group);
tr("Delete dataset \"%1\"").arg(set->title()), grp.m_datasets.removeAt(dataset);
tr("Are you sure you want to delete this dataset?"), APP_NAME, m_groups.replace(group, grp);
QMessageBox::Yes | QMessageBox::No); Q_EMIT groupChanged(group);
if (ret == QMessageBox::Yes)
{
getGroup(group)->m_datasets.removeAt(dataset);
Q_EMIT groupChanged(group);
}
} }
} }
@ -1140,12 +1068,17 @@ void JSON::Editor::deleteDataset(const int group, const int dataset)
void JSON::Editor::setDatasetTitle(const int group, const int dataset, void JSON::Editor::setDatasetTitle(const int group, const int dataset,
const QString &title) const QString &title)
{ {
// Get dataset & group
auto grp = getGroup(group);
auto set = getDataset(group, dataset); auto set = getDataset(group, dataset);
if (set)
{ // Update dataset & group
set->m_title = title; set.m_title = title;
Q_EMIT datasetChanged(group, dataset); grp.m_datasets.replace(dataset, set);
} m_groups.replace(group, grp);
// Update UI
Q_EMIT datasetChanged(group, dataset);
} }
/** /**
@ -1157,12 +1090,17 @@ void JSON::Editor::setDatasetTitle(const int group, const int dataset,
void JSON::Editor::setDatasetUnits(const int group, const int dataset, void JSON::Editor::setDatasetUnits(const int group, const int dataset,
const QString &units) const QString &units)
{ {
// Get dataset & group
auto grp = getGroup(group);
auto set = getDataset(group, dataset); auto set = getDataset(group, dataset);
if (set)
{ // Update dataset & group
set->m_units = units; set.m_units = units;
Q_EMIT datasetChanged(group, dataset); grp.m_datasets.replace(dataset, set);
} m_groups.replace(group, grp);
// Update UI
Q_EMIT datasetChanged(group, dataset);
} }
/** /**
@ -1174,12 +1112,17 @@ void JSON::Editor::setDatasetUnits(const int group, const int dataset,
void JSON::Editor::setDatasetIndex(const int group, const int dataset, void JSON::Editor::setDatasetIndex(const int group, const int dataset,
const int frameIndex) const int frameIndex)
{ {
// Get dataset & group
auto grp = getGroup(group);
auto set = getDataset(group, dataset); auto set = getDataset(group, dataset);
if (set)
{ // Update dataset & group
set->m_index = frameIndex; set.m_index = frameIndex;
Q_EMIT datasetChanged(group, dataset); grp.m_datasets.replace(dataset, set);
} m_groups.replace(group, grp);
// Update UI
Q_EMIT datasetChanged(group, dataset);
} }
/** /**
@ -1191,12 +1134,17 @@ void JSON::Editor::setDatasetIndex(const int group, const int dataset,
void JSON::Editor::setDatasetLED(const int group, const int dataset, void JSON::Editor::setDatasetLED(const int group, const int dataset,
const bool generateLED) const bool generateLED)
{ {
// Get dataset & group
auto grp = getGroup(group);
auto set = getDataset(group, dataset); auto set = getDataset(group, dataset);
if (set)
{ // Update dataset & group
set->m_led = generateLED; set.m_led = generateLED;
Q_EMIT datasetChanged(group, dataset); grp.m_datasets.replace(dataset, set);
} m_groups.replace(group, grp);
// Update UI
Q_EMIT datasetChanged(group, dataset);
} }
/** /**
@ -1208,12 +1156,17 @@ void JSON::Editor::setDatasetLED(const int group, const int dataset,
void JSON::Editor::setDatasetGraph(const int group, const int dataset, void JSON::Editor::setDatasetGraph(const int group, const int dataset,
const bool generateGraph) const bool generateGraph)
{ {
// Get dataset & group
auto grp = getGroup(group);
auto set = getDataset(group, dataset); auto set = getDataset(group, dataset);
if (set)
{ // Update dataset & group
set->m_graph = generateGraph; set.m_graph = generateGraph;
Q_EMIT datasetChanged(group, dataset); grp.m_datasets.replace(dataset, set);
} m_groups.replace(group, grp);
// Update UI
Q_EMIT datasetChanged(group, dataset);
} }
/** /**
@ -1225,12 +1178,17 @@ void JSON::Editor::setDatasetGraph(const int group, const int dataset,
void JSON::Editor::setDatasetFftPlot(const int group, const int dataset, void JSON::Editor::setDatasetFftPlot(const int group, const int dataset,
const bool generateFft) const bool generateFft)
{ {
// Get dataset & group
auto grp = getGroup(group);
auto set = getDataset(group, dataset); auto set = getDataset(group, dataset);
if (set)
{ // Update dataset & group
set->m_fft = generateFft; set.m_fft = generateFft;
Q_EMIT datasetChanged(group, dataset); grp.m_datasets.replace(dataset, set);
} m_groups.replace(group, grp);
// Update UI
Q_EMIT datasetChanged(group, dataset);
} }
/** /**
@ -1242,12 +1200,17 @@ void JSON::Editor::setDatasetFftPlot(const int group, const int dataset,
void JSON::Editor::setDatasetLogPlot(const int group, const int dataset, void JSON::Editor::setDatasetLogPlot(const int group, const int dataset,
const bool generateLog) const bool generateLog)
{ {
// Get dataset & group
auto grp = getGroup(group);
auto set = getDataset(group, dataset); auto set = getDataset(group, dataset);
if (set)
{ // Update dataset & group
set->m_log = generateLog; set.m_log = generateLog;
Q_EMIT datasetChanged(group, dataset); grp.m_datasets.replace(dataset, set);
} m_groups.replace(group, grp);
// Update UI
Q_EMIT datasetChanged(group, dataset);
} }
/** /**
@ -1260,24 +1223,30 @@ void JSON::Editor::setDatasetLogPlot(const int group, const int dataset,
void JSON::Editor::setDatasetWidget(const int group, const int dataset, void JSON::Editor::setDatasetWidget(const int group, const int dataset,
const int widgetId) const int widgetId)
{ {
// Get dataset & group
auto grp = getGroup(group);
auto set = getDataset(group, dataset); auto set = getDataset(group, dataset);
if (set)
{
QString widget;
if (widgetId == 1)
widget = "gauge";
else if (widgetId == 2)
widget = "bar";
else if (widgetId == 3)
{
widget = "compass";
set->m_min = 0;
set->m_max = 360;
}
set->m_widget = widget; // Get widget string
Q_EMIT datasetChanged(group, dataset); QString widget;
if (widgetId == 1)
widget = "gauge";
else if (widgetId == 2)
widget = "bar";
else if (widgetId == 3)
{
widget = "compass";
set.m_min = 0;
set.m_max = 360;
} }
// Update dataset & group
set.m_widget = widget;
grp.m_datasets.replace(dataset, set);
m_groups.replace(group, grp);
// Update UI
Q_EMIT datasetChanged(group, dataset);
} }
/** /**
@ -1291,12 +1260,17 @@ void JSON::Editor::setDatasetWidget(const int group, const int dataset,
void JSON::Editor::setDatasetWidgetMin(const int group, const int dataset, void JSON::Editor::setDatasetWidgetMin(const int group, const int dataset,
const QString &minimum) const QString &minimum)
{ {
// Get dataset & group
auto grp = getGroup(group);
auto set = getDataset(group, dataset); auto set = getDataset(group, dataset);
if (set)
{ // Update dataset & group
set->m_min = minimum.toDouble(); set.m_min = minimum.toDouble();
Q_EMIT datasetChanged(group, dataset); grp.m_datasets.replace(dataset, set);
} m_groups.replace(group, grp);
// Update UI
Q_EMIT datasetChanged(group, dataset);
} }
/** /**
@ -1310,12 +1284,17 @@ void JSON::Editor::setDatasetWidgetMin(const int group, const int dataset,
void JSON::Editor::setDatasetWidgetMax(const int group, const int dataset, void JSON::Editor::setDatasetWidgetMax(const int group, const int dataset,
const QString &maximum) const QString &maximum)
{ {
// Get dataset & group
auto grp = getGroup(group);
auto set = getDataset(group, dataset); auto set = getDataset(group, dataset);
if (set)
{ // Update dataset & group
set->m_max = maximum.toDouble(); set.m_max = maximum.toDouble();
Q_EMIT datasetChanged(group, dataset); grp.m_datasets.replace(dataset, set);
} m_groups.replace(group, grp);
// Update UI
Q_EMIT datasetChanged(group, dataset);
} }
/** /**
@ -1327,12 +1306,17 @@ void JSON::Editor::setDatasetWidgetMax(const int group, const int dataset,
void JSON::Editor::setDatasetWidgetData(const int group, const int dataset, void JSON::Editor::setDatasetWidgetData(const int group, const int dataset,
const QString &widget) const QString &widget)
{ {
// Get dataset & group
auto grp = getGroup(group);
auto set = getDataset(group, dataset); auto set = getDataset(group, dataset);
if (set)
{ // Update dataset & group
set->m_widget = widget; set.m_widget = widget;
Q_EMIT datasetChanged(group, dataset); grp.m_datasets.replace(dataset, set);
} m_groups.replace(group, grp);
// Update UI
Q_EMIT datasetChanged(group, dataset);
} }
/** /**
@ -1346,12 +1330,17 @@ void JSON::Editor::setDatasetWidgetData(const int group, const int dataset,
void JSON::Editor::setDatasetWidgetAlarm(const int group, const int dataset, void JSON::Editor::setDatasetWidgetAlarm(const int group, const int dataset,
const QString &alarm) const QString &alarm)
{ {
// Get dataset & group
auto grp = getGroup(group);
auto set = getDataset(group, dataset); auto set = getDataset(group, dataset);
if (set)
{ // Update dataset & group
set->m_alarm = alarm.toDouble(); set.m_alarm = alarm.toDouble();
Q_EMIT datasetChanged(group, dataset); grp.m_datasets.replace(dataset, set);
} m_groups.replace(group, grp);
// Update UI
Q_EMIT datasetChanged(group, dataset);
} }
/** /**
@ -1363,16 +1352,22 @@ void JSON::Editor::setDatasetWidgetAlarm(const int group, const int dataset,
void JSON::Editor::setDatasetFFTSamples(const int group, const int dataset, void JSON::Editor::setDatasetFFTSamples(const int group, const int dataset,
const QString &samples) const QString &samples)
{ {
// Get dataset & group
auto grp = getGroup(group);
auto set = getDataset(group, dataset); auto set = getDataset(group, dataset);
if (set)
{
auto sample = samples.toInt();
if (sample < 1)
sample = 1;
set->m_fftSamples = sample; // Validate FFT samples
Q_EMIT datasetChanged(group, dataset); auto sample = samples.toInt();
} if (sample < 128)
sample = 128;
// Update dataset & group
set.m_fftSamples = sample;
grp.m_datasets.replace(dataset, set);
m_groups.replace(group, grp);
// Update UI
Q_EMIT datasetChanged(group, dataset);
} }
/** /**
@ -1443,12 +1438,9 @@ int JSON::Editor::nextDatasetIndex()
{ {
for (int j = 0; j < datasetCount(i); ++j) for (int j = 0; j < datasetCount(i); ++j)
{ {
const auto dataset = getDataset(i, j); auto dataset = getDataset(i, j);
if (dataset) if (dataset.m_index >= maxIndex)
{ maxIndex = dataset.m_index + 1;
if (dataset->m_index >= maxIndex)
maxIndex = dataset->m_index + 1;
}
} }
} }

View File

@ -114,8 +114,8 @@ public:
Q_INVOKABLE bool saveJsonFile(); Q_INVOKABLE bool saveJsonFile();
Q_INVOKABLE int datasetCount(const int group) const; Q_INVOKABLE int datasetCount(const int group) const;
Q_INVOKABLE JSON::Group *getGroup(const int index) const; Q_INVOKABLE const Group &getGroup(const int index) const;
Q_INVOKABLE JSON::Dataset *getDataset(const int group, const int index) const; Q_INVOKABLE const JSON::Dataset &getDataset(const int group, const int index) const;
Q_INVOKABLE QString groupTitle(const int group) const; Q_INVOKABLE QString groupTitle(const int group) const;
Q_INVOKABLE QString groupWidget(const int group) const; Q_INVOKABLE QString groupWidget(const int group) const;
@ -188,6 +188,6 @@ private:
bool m_modified; bool m_modified;
QString m_filePath; QString m_filePath;
QVector<Group *> m_groups; QVector<Group> m_groups;
}; };
} }

View File

@ -29,7 +29,6 @@
*/ */
JSON::Frame::~Frame() JSON::Frame::~Frame()
{ {
qDeleteAll(m_groups);
m_groups.clear(); m_groups.clear();
} }
@ -40,7 +39,6 @@ JSON::Frame::~Frame()
void JSON::Frame::clear() void JSON::Frame::clear()
{ {
m_title = ""; m_title = "";
qDeleteAll(m_groups);
m_groups.clear(); m_groups.clear();
} }
@ -63,7 +61,7 @@ int JSON::Frame::groupCount() const
/** /**
* Returns a vector of pointers to the @c Group objects associated to this frame. * Returns a vector of pointers to the @c Group objects associated to this frame.
*/ */
QVector<JSON::Group *> JSON::Frame::groups() const QVector<JSON::Group> JSON::Frame::groups() const
{ {
return m_groups; return m_groups;
} }
@ -92,11 +90,9 @@ bool JSON::Frame::read(const QJsonObject &object)
// Generate groups & datasets from data frame // Generate groups & datasets from data frame
for (auto i = 0; i < groups.count(); ++i) for (auto i = 0; i < groups.count(); ++i)
{ {
Group *group = new Group(); Group group;
if (group->read(groups.at(i).toObject())) if (group.read(groups.at(i).toObject()))
m_groups.append(group); m_groups.append(group);
else
delete group;
} }
// Return status // Return status
@ -109,12 +105,9 @@ bool JSON::Frame::read(const QJsonObject &object)
} }
/** /**
* @return The group at the given @a index,vreturns @c Q_NULLPTR on invalid index * @return The group at the given @a index
*/ */
JSON::Group *JSON::Frame::getGroup(const int index) const JSON::Group &JSON::Frame::getGroup(const int index) const
{ {
if (index < groupCount() && index >= 0) return m_groups.at(index);
return m_groups.at(index);
return Q_NULLPTR;
} }

View File

@ -60,14 +60,14 @@ public:
void clear(); void clear();
QString title() const; QString title() const;
int groupCount() const; int groupCount() const;
QVector<Group *> groups() const; QVector<Group> groups() const;
bool read(const QJsonObject &object); bool read(const QJsonObject &object);
Q_INVOKABLE JSON::Group *getGroup(const int index); Q_INVOKABLE const JSON::Group &getGroup(const int index) const;
inline bool isValid() const { return !title().isEmpty() && groupCount() > 0; } inline bool isValid() const { return !title().isEmpty() && groupCount() > 0; }
private: private:
QString m_title; QString m_title;
QVector<Group *> m_groups; QVector<Group> m_groups;
}; };
} }

View File

@ -29,7 +29,6 @@
*/ */
JSON::Group::~Group() JSON::Group::~Group()
{ {
qDeleteAll(m_datasets);
m_datasets.clear(); m_datasets.clear();
} }
@ -60,7 +59,7 @@ int JSON::Group::datasetCount() const
/** /**
* @return A list with all the dataset objects contained in this group * @return A list with all the dataset objects contained in this group
*/ */
QVector<JSON::Dataset *> &JSON::Group::datasets() QVector<JSON::Dataset> &JSON::Group::datasets()
{ {
return m_datasets; return m_datasets;
} }
@ -68,12 +67,9 @@ QVector<JSON::Dataset *> &JSON::Group::datasets()
/** /**
* @return The dataset at the given @a index,vreturns @c Q_NULLPTR on invalid index * @return The dataset at the given @a index,vreturns @c Q_NULLPTR on invalid index
*/ */
JSON::Dataset *JSON::Group::getDataset(const int index) const JSON::Dataset &JSON::Group::getDataset(const int index) const
{ {
if (index < datasetCount() && index >= 0) return m_datasets.at(index);
return m_datasets.at(index);
return Q_NULLPTR;
} }
/** /**
@ -101,11 +97,9 @@ bool JSON::Group::read(const QJsonObject &object)
const auto object = array.at(i).toObject(); const auto object = array.at(i).toObject();
if (!object.isEmpty()) if (!object.isEmpty())
{ {
Dataset *dataset = new Dataset(); Dataset dataset;
if (dataset->read(object)) if (dataset.read(object))
m_datasets.append(dataset); m_datasets.append(dataset);
else
delete dataset;
} }
} }

View File

@ -65,15 +65,15 @@ public:
QString title() const; QString title() const;
QString widget() const; QString widget() const;
int datasetCount() const; int datasetCount() const;
QVector<Dataset *> &datasets(); QVector<Dataset> &datasets();
bool read(const QJsonObject &object); bool read(const QJsonObject &object);
Q_INVOKABLE JSON::Dataset *getDataset(const int index); Q_INVOKABLE const JSON::Dataset &getDataset(const int index) const;
private: private:
QString m_title; QString m_title;
QString m_widget; QString m_widget;
QVector<Dataset *> m_datasets; QVector<Dataset> m_datasets;
friend class Editor; friend class Editor;
friend class UI::Dashboard; friend class UI::Dashboard;

View File

@ -72,17 +72,17 @@ QFont UI::Dashboard::monoFont() const
} }
// clang-format off // clang-format off
JSON::Group *UI::Dashboard::getLED(const int index) { return getGroupWidget(m_ledWidgets, index); } const JSON::Group &UI::Dashboard::getLED(const int index) { return m_ledWidgets.at(index); }
JSON::Group *UI::Dashboard::getGPS(const int index) { return getGroupWidget(m_gpsWidgets, index); } const JSON::Group &UI::Dashboard::getGPS(const int index) { return m_gpsWidgets.at(index); }
JSON::Dataset *UI::Dashboard::getBar(const int index) { return getDatasetWidget(m_barWidgets, index); } const JSON::Dataset &UI::Dashboard::getBar(const int index) { return m_barWidgets.at(index); }
JSON::Dataset *UI::Dashboard::getFFT(const int index) { return getDatasetWidget(m_fftWidgets, index); } const JSON::Dataset &UI::Dashboard::getFFT(const int index) { return m_fftWidgets.at(index); }
JSON::Dataset *UI::Dashboard::getPlot(const int index) { return getDatasetWidget(m_plotWidgets, index); } const JSON::Dataset &UI::Dashboard::getPlot(const int index) { return m_plotWidgets.at(index); }
JSON::Group *UI::Dashboard::getGroups(const int index) { return getGroupWidget(m_groupWidgets, index); } const JSON::Group &UI::Dashboard::getGroups(const int index) { return m_groupWidgets.at(index); }
JSON::Dataset *UI::Dashboard::getGauge(const int index) { return getDatasetWidget(m_gaugeWidgets, index); } const JSON::Dataset &UI::Dashboard::getGauge(const int index) { return m_gaugeWidgets.at(index); }
JSON::Group *UI::Dashboard::getGyroscope(const int index) { return getGroupWidget(m_gyroscopeWidgets, index); } const JSON::Group &UI::Dashboard::getGyroscope(const int index) { return m_gyroscopeWidgets.at(index); }
JSON::Dataset *UI::Dashboard::getCompass(const int index) { return getDatasetWidget(m_compassWidgets, index); } const JSON::Dataset &UI::Dashboard::getCompass(const int index) { return m_compassWidgets.at(index); }
JSON::Group *UI::Dashboard::getMultiplot(const int index) { return getGroupWidget(m_multiPlotWidgets, index); } const JSON::Group &UI::Dashboard::getMultiplot(const int index) { return m_multiPlotWidgets.at(index); }
JSON::Group *UI::Dashboard::getAccelerometer(const int index) { return getGroupWidget(m_accelerometerWidgets, index); } const JSON::Group &UI::Dashboard::getAccelerometer(const int index) { return m_accelerometerWidgets.at(index); }
// clang-format on // clang-format on
//---------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------
@ -669,13 +669,13 @@ void UI::Dashboard::updatePlots()
for (int i = 0; i < m_latestFrame.groupCount(); ++i) for (int i = 0; i < m_latestFrame.groupCount(); ++i)
{ {
const auto group = m_latestFrame.groups().at(i); const auto group = m_latestFrame.groups().at(i);
for (int j = 0; j < group->datasetCount(); ++j) for (int j = 0; j < group.datasetCount(); ++j)
{ {
auto dataset = group->datasets().at(j); auto dataset = group.getDataset(j);
if (dataset->fft()) if (dataset.fft())
fftDatasets.append(dataset); fftDatasets.append(&dataset);
if (dataset->graph()) if (dataset.graph())
linearDatasets.append(dataset); linearDatasets.append(&dataset);
} }
} }
@ -845,24 +845,24 @@ void UI::Dashboard::processLatestJSON(const JFI_Object &frameInfo)
* @note We return a vector with a single group item because we want to display a title on * @note We return a vector with a single group item because we want to display a title on
* the window without breaking the current software architecture. * the window without breaking the current software architecture.
*/ */
QVector<JSON::Group *> UI::Dashboard::getLEDWidgets() const QVector<JSON::Group> UI::Dashboard::getLEDWidgets() const
{ {
QVector<JSON::Dataset *> widgets; QVector<JSON::Dataset> widgets;
Q_FOREACH (auto group, m_latestFrame.groups()) Q_FOREACH (auto group, m_latestFrame.groups())
{ {
Q_FOREACH (auto dataset, group->datasets()) Q_FOREACH (auto dataset, group.datasets())
{ {
if (dataset->led()) if (dataset.led())
widgets.append(dataset); widgets.append(dataset);
} }
} }
QVector<JSON::Group *> groups; QVector<JSON::Group> groups;
if (widgets.count() > 0) if (widgets.count() > 0)
{ {
JSON::Group *group = new JSON::Group(); JSON::Group group;
group->m_title = tr("Status Panel"); group.m_title = tr("Status Panel");
group->m_datasets = widgets; group.m_datasets = widgets;
groups.append(group); groups.append(group);
} }
@ -872,14 +872,14 @@ QVector<JSON::Group *> UI::Dashboard::getLEDWidgets() const
/** /**
* Returns a vector with all the datasets that need to be shown in the FFT widgets. * Returns a vector with all the datasets that need to be shown in the FFT widgets.
*/ */
QVector<JSON::Dataset *> UI::Dashboard::getFFTWidgets() const QVector<JSON::Dataset> UI::Dashboard::getFFTWidgets() const
{ {
QVector<JSON::Dataset *> widgets; QVector<JSON::Dataset> widgets;
Q_FOREACH (auto group, m_latestFrame.groups()) Q_FOREACH (auto group, m_latestFrame.groups())
{ {
Q_FOREACH (auto dataset, group->datasets()) Q_FOREACH (auto dataset, group.datasets())
{ {
if (dataset->fft()) if (dataset.fft())
widgets.append(dataset); widgets.append(dataset);
} }
} }
@ -890,14 +890,14 @@ QVector<JSON::Dataset *> UI::Dashboard::getFFTWidgets() const
/** /**
* Returns a vector with all the datasets that need to be plotted. * Returns a vector with all the datasets that need to be plotted.
*/ */
QVector<JSON::Dataset *> UI::Dashboard::getPlotWidgets() const QVector<JSON::Dataset> UI::Dashboard::getPlotWidgets() const
{ {
QVector<JSON::Dataset *> widgets; QVector<JSON::Dataset> widgets;
Q_FOREACH (auto group, m_latestFrame.groups()) Q_FOREACH (auto group, m_latestFrame.groups())
{ {
Q_FOREACH (auto dataset, group->datasets()) Q_FOREACH (auto dataset, group.datasets())
{ {
if (dataset->graph()) if (dataset.graph())
widgets.append(dataset); widgets.append(dataset);
} }
} }
@ -909,12 +909,12 @@ QVector<JSON::Dataset *> UI::Dashboard::getPlotWidgets() const
* Returns a vector with all the groups that implement the widget with the specied * Returns a vector with all the groups that implement the widget with the specied
* @a handle. * @a handle.
*/ */
QVector<JSON::Group *> UI::Dashboard::getWidgetGroups(const QString &handle) const QVector<JSON::Group> UI::Dashboard::getWidgetGroups(const QString &handle) const
{ {
QVector<JSON::Group *> widgets; QVector<JSON::Group> widgets;
Q_FOREACH (auto group, m_latestFrame.groups()) Q_FOREACH (auto group, m_latestFrame.groups())
{ {
if (group->widget() == handle) if (group.widget() == handle)
widgets.append(group); widgets.append(group);
} }
@ -925,14 +925,14 @@ QVector<JSON::Group *> UI::Dashboard::getWidgetGroups(const QString &handle) con
* Returns a vector with all the datasets that implement a widget with the specified * Returns a vector with all the datasets that implement a widget with the specified
* @a handle. * @a handle.
*/ */
QVector<JSON::Dataset *> UI::Dashboard::getWidgetDatasets(const QString &handle) const QVector<JSON::Dataset> UI::Dashboard::getWidgetDatasets(const QString &handle) const
{ {
QVector<JSON::Dataset *> widgets; QVector<JSON::Dataset> widgets;
Q_FOREACH (auto group, m_latestFrame.groups()) Q_FOREACH (auto group, m_latestFrame.groups())
{ {
Q_FOREACH (auto dataset, group->datasets()) Q_FOREACH (auto dataset, group.datasets())
{ {
if (dataset->widget() == handle) if (dataset.widget() == handle)
widgets.append(dataset); widgets.append(dataset);
} }
} }
@ -943,11 +943,11 @@ QVector<JSON::Dataset *> UI::Dashboard::getWidgetDatasets(const QString &handle)
/** /**
* Returns the titles of the datasets contained in the specified @a vector. * Returns the titles of the datasets contained in the specified @a vector.
*/ */
StringList UI::Dashboard::datasetTitles(const QVector<JSON::Dataset *> &vector) const StringList UI::Dashboard::datasetTitles(const QVector<JSON::Dataset> &vector) const
{ {
StringList list; StringList list;
Q_FOREACH (auto set, vector) Q_FOREACH (auto set, vector)
list.append(set->title()); list.append(set.title());
return list; return list;
} }
@ -955,11 +955,11 @@ StringList UI::Dashboard::datasetTitles(const QVector<JSON::Dataset *> &vector)
/** /**
* Returns the titles of the groups contained in the specified @a vector. * Returns the titles of the groups contained in the specified @a vector.
*/ */
StringList UI::Dashboard::groupTitles(const QVector<JSON::Group *> &vector) const StringList UI::Dashboard::groupTitles(const QVector<JSON::Group> &vector) const
{ {
StringList list; StringList list;
Q_FOREACH (auto group, vector) Q_FOREACH (auto group, vector)
list.append(group->title()); list.append(group.title());
return list; return list;
} }
@ -991,32 +991,6 @@ void UI::Dashboard::setVisibility(QVector<bool> &vector, const int index,
} }
} }
/**
* Returns a pointer to the group at the specified @a index of the given @a vector.
* If the @a index is invalid, then this function shall return a NULL pointer.
*/
JSON::Group *UI::Dashboard::getGroupWidget(const QVector<JSON::Group *> &vector,
const int index)
{
if (index < vector.count())
return vector.at(index);
return Q_NULLPTR;
}
/**
* Returns a pointer to the dataset at the specified @a index of the given @a vector.
* If the @a index is invalid, then this function shall return a NULL pointer.
*/
JSON::Dataset *UI::Dashboard::getDatasetWidget(const QVector<JSON::Dataset *> &vector,
const int index)
{
if (index < vector.count())
return vector.at(index);
return Q_NULLPTR;
}
#ifdef SERIAL_STUDIO_INCLUDE_MOC #ifdef SERIAL_STUDIO_INCLUDE_MOC
# include "moc_Dashboard.cpp" # include "moc_Dashboard.cpp"
#endif #endif

View File

@ -182,17 +182,17 @@ public:
static Dashboard &instance(); static Dashboard &instance();
QFont monoFont() const; QFont monoFont() const;
JSON::Group *getLED(const int index); const JSON::Group &getLED(const int index);
JSON::Group *getGPS(const int index); const JSON::Group &getGPS(const int index);
JSON::Dataset *getFFT(const int index); const JSON::Dataset &getFFT(const int index);
JSON::Dataset *getBar(const int index); const JSON::Dataset &getBar(const int index);
JSON::Group *getGroups(const int index); const JSON::Group &getGroups(const int index);
JSON::Dataset *getPlot(const int index); const JSON::Dataset &getPlot(const int index);
JSON::Dataset *getGauge(const int index); const JSON::Dataset &getGauge(const int index);
JSON::Group *getGyroscope(const int index); const JSON::Group &getGyroscope(const int index);
JSON::Dataset *getCompass(const int index); const JSON::Dataset &getCompass(const int index);
JSON::Group *getMultiplot(const int index); const JSON::Group &getMultiplot(const int index);
JSON::Group *getAccelerometer(const int index); const JSON::Group &getAccelerometer(const int index);
QString title(); QString title();
bool available(); bool available();
@ -243,9 +243,9 @@ public:
StringList multiPlotTitles() const; StringList multiPlotTitles() const;
StringList accelerometerTitles() const; StringList accelerometerTitles() const;
PlotData *xPlotValues() { return &m_xData; } const PlotData &xPlotValues() { return m_xData; }
QVector<PlotData> *fftPlotValues() { return &m_fftPlotValues; } const QVector<PlotData> &fftPlotValues() { return m_fftPlotValues; }
QVector<PlotData> *linearPlotValues() { return &m_linearPlotValues; } const QVector<PlotData> &linearPlotValues() { return m_linearPlotValues; }
public Q_SLOTS: public Q_SLOTS:
void setPoints(const int points); void setPoints(const int points);
@ -268,22 +268,18 @@ private Q_SLOTS:
void processLatestJSON(const JFI_Object &frameInfo); void processLatestJSON(const JFI_Object &frameInfo);
private: private:
QVector<JSON::Group *> getLEDWidgets() const; QVector<JSON::Group> getLEDWidgets() const;
QVector<JSON::Dataset *> getFFTWidgets() const; QVector<JSON::Dataset> getFFTWidgets() const;
QVector<JSON::Dataset *> getPlotWidgets() const; QVector<JSON::Dataset> getPlotWidgets() const;
QVector<JSON::Group *> getWidgetGroups(const QString &handle) const; QVector<JSON::Group> getWidgetGroups(const QString &handle) const;
QVector<JSON::Dataset *> getWidgetDatasets(const QString &handle) const; QVector<JSON::Dataset> getWidgetDatasets(const QString &handle) const;
StringList groupTitles(const QVector<JSON::Group *> &vector) const; StringList groupTitles(const QVector<JSON::Group> &vector) const;
StringList datasetTitles(const QVector<JSON::Dataset *> &vector) const; StringList datasetTitles(const QVector<JSON::Dataset> &vector) const;
bool getVisibility(const QVector<bool> &vector, const int index) const; bool getVisibility(const QVector<bool> &vector, const int index) const;
void setVisibility(QVector<bool> &vector, const int index, const bool visible); void setVisibility(QVector<bool> &vector, const int index, const bool visible);
JSON::Group *getGroupWidget(const QVector<JSON::Group *> &vector, const int index);
JSON::Dataset *getDatasetWidget(const QVector<JSON::Dataset *> &vector,
const int index);
private: private:
int m_points; int m_points;
int m_precision; int m_precision;
@ -304,18 +300,18 @@ private:
QVector<bool> m_multiPlotVisibility; QVector<bool> m_multiPlotVisibility;
QVector<bool> m_accelerometerVisibility; QVector<bool> m_accelerometerVisibility;
QVector<JSON::Dataset *> m_barWidgets; QVector<JSON::Dataset> m_barWidgets;
QVector<JSON::Dataset *> m_fftWidgets; QVector<JSON::Dataset> m_fftWidgets;
QVector<JSON::Dataset *> m_plotWidgets; QVector<JSON::Dataset> m_plotWidgets;
QVector<JSON::Dataset *> m_gaugeWidgets; QVector<JSON::Dataset> m_gaugeWidgets;
QVector<JSON::Dataset *> m_compassWidgets; QVector<JSON::Dataset> m_compassWidgets;
QVector<JSON::Group *> m_ledWidgets; QVector<JSON::Group> m_ledWidgets;
QVector<JSON::Group *> m_gpsWidgets; QVector<JSON::Group> m_gpsWidgets;
QVector<JSON::Group *> m_groupWidgets; QVector<JSON::Group> m_groupWidgets;
QVector<JSON::Group *> m_multiPlotWidgets; QVector<JSON::Group> m_multiPlotWidgets;
QVector<JSON::Group *> m_gyroscopeWidgets; QVector<JSON::Group> m_gyroscopeWidgets;
QVector<JSON::Group *> m_accelerometerWidgets; QVector<JSON::Group> m_accelerometerWidgets;
JSON::Frame m_latestFrame; JSON::Frame m_latestFrame;
}; };

View File

@ -80,46 +80,39 @@ Widgets::Accelerometer::Accelerometer(const int index)
*/ */
void Widgets::Accelerometer::updateData() void Widgets::Accelerometer::updateData()
{ {
// Widget not enabled, do nothing
if (!isEnabled()) if (!isEnabled())
return; return;
// Update accelerometer values auto accelerometer = UI::Dashboard::instance().getAccelerometer(m_index);
const auto accelerometer = UI::Dashboard::instance().getAccelerometer(m_index); if (accelerometer.datasetCount() != 3)
if (accelerometer) return;
double x = 0;
double y = 0;
double z = 0;
for (int i = 0; i < 3; ++i)
{ {
if (accelerometer->datasetCount() != 3) auto dataset = accelerometer.getDataset(i);
return; if (dataset.widget() == "x")
x = dataset.value().toDouble();
double x = 0; if (dataset.widget() == "y")
double y = 0; y = dataset.value().toDouble();
double z = 0; if (dataset.widget() == "z")
z = dataset.value().toDouble();
JSON::Dataset *dataset;
for (int i = 0; i < 3; ++i)
{
dataset = accelerometer->getDataset(i);
if (dataset->widget() == "x")
x = dataset->value().toDouble();
if (dataset->widget() == "y")
y = dataset->value().toDouble();
if (dataset->widget() == "z")
z = dataset->value().toDouble();
}
x /= 9.18;
y /= 9.18;
z /= 9.18;
const double G = qSqrt(qPow(x, 2) + qPow(y, 2) + qPow(z, 2));
m_gauge.setValue(G);
setValue(QString("%1 G").arg(
QString::number(G, 'f', UI::Dashboard::instance().precision())));
// Repaint widget
requestRepaint();
} }
x /= 9.18;
y /= 9.18;
z /= 9.18;
const double G = qSqrt(qPow(x, 2) + qPow(y, 2) + qPow(z, 2));
m_gauge.setValue(G);
setValue(QString("%1 G").arg(
QString::number(G, 'f', UI::Dashboard::instance().precision())));
requestRepaint();
} }
#ifdef SERIAL_STUDIO_INCLUDE_MOC #ifdef SERIAL_STUDIO_INCLUDE_MOC

View File

@ -65,13 +65,10 @@ Widgets::Bar::Bar(const int index)
m_thermo.setFillBrush(QBrush(QColor(color))); m_thermo.setFillBrush(QBrush(QColor(color)));
// Get initial properties from dataset // Get initial properties from dataset
const auto dataset = UI::Dashboard::instance().getBar(m_index); auto dataset = UI::Dashboard::instance().getBar(m_index);
if (dataset) m_thermo.setAlarmLevel(dataset.alarm());
{ m_thermo.setAlarmEnabled(m_thermo.alarmLevel() > 0);
m_thermo.setAlarmLevel(dataset->alarm()); m_thermo.setScale(dataset.min(), dataset.max());
m_thermo.setAlarmEnabled(m_thermo.alarmLevel() > 0);
m_thermo.setScale(dataset->min(), dataset->max());
}
// Set widget pointer & disable auto resize // Set widget pointer & disable auto resize
setWidget(&m_thermo, Qt::AlignHCenter, false); setWidget(&m_thermo, Qt::AlignHCenter, false);
@ -95,18 +92,15 @@ void Widgets::Bar::updateData()
return; return;
// Update bar level // Update bar level
const auto dataset = UI::Dashboard::instance().getBar(m_index); auto dataset = UI::Dashboard::instance().getBar(m_index);
if (dataset) const auto value = dataset.value().toDouble();
{ m_thermo.setValue(value);
const auto value = dataset->value().toDouble(); setValue(QString("%1 %2").arg(
m_thermo.setValue(value); QString::number(value, 'f', UI::Dashboard::instance().precision()),
setValue(QString("%1 %2").arg( dataset.units()));
QString::number(value, 'f', UI::Dashboard::instance().precision()),
dataset->units()));
// Repaint widget // Repaint widget
requestRepaint(); requestRepaint();
}
} }
/** /**

View File

@ -79,29 +79,23 @@ Widgets::Compass::Compass(const int index)
*/ */
void Widgets::Compass::update() void Widgets::Compass::update()
{ {
// Widget not enabled, do nothing
if (!isEnabled()) if (!isEnabled())
return; return;
// Update compass heading auto dataset = UI::Dashboard::instance().getCompass(m_index);
const auto dataset = UI::Dashboard::instance().getCompass(m_index); auto value = dataset.value().toDouble();
if (dataset) auto text = QString("%1°").arg(
{ QString::number(value, 'f', UI::Dashboard::instance().precision()));
auto value = dataset->value().toDouble();
auto text = QString("%1°").arg(
QString::number(value, 'f', UI::Dashboard::instance().precision()));
m_compass.setValue(value);
if (text.length() == 2) if (text.length() == 2)
text.prepend("00"); text.prepend("00");
else if (text.length() == 3) else if (text.length() == 3)
text.prepend("0"); text.prepend("0");
setValue(text); setValue(text);
m_compass.setValue(value);
// Repaint widget requestRepaint();
requestRepaint();
}
} }
#ifdef SERIAL_STUDIO_INCLUDE_MOC #ifdef SERIAL_STUDIO_INCLUDE_MOC

View File

@ -53,10 +53,8 @@ Widgets::DataGroup::DataGroup(const int index)
if (m_index < 0 || m_index >= dash->groupCount()) if (m_index < 0 || m_index >= dash->groupCount())
return; return;
// Get group pointer // Get group reference
const auto group = dash->getGroups(m_index); auto group = dash->getGroups(m_index);
if (!group)
return;
// Generate widget stylesheets // Generate widget stylesheets
const auto titleQSS = QSS("color:%1", theme->widgetTextPrimary()); const auto titleQSS = QSS("color:%1", theme->widgetTextPrimary());
@ -78,12 +76,12 @@ Widgets::DataGroup::DataGroup(const int index)
valueFont.setPixelSize(dash->monoFont().pixelSize() * 1.3); valueFont.setPixelSize(dash->monoFont().pixelSize() * 1.3);
// Configure grid layout // Configure grid layout
m_units.reserve(group->datasetCount()); m_units.reserve(group.datasetCount());
m_icons.reserve(group->datasetCount()); m_icons.reserve(group.datasetCount());
m_titles.reserve(group->datasetCount()); m_titles.reserve(group.datasetCount());
m_values.reserve(group->datasetCount()); m_values.reserve(group.datasetCount());
m_gridLayout = new QGridLayout(m_dataContainer); m_gridLayout = new QGridLayout(m_dataContainer);
for (int dataset = 0; dataset < group->datasetCount(); ++dataset) for (int dataset = 0; dataset < group.datasetCount(); ++dataset)
{ {
// Create labels // Create labels
m_units.append(new QLabel(m_dataContainer)); m_units.append(new QLabel(m_dataContainer));
@ -117,13 +115,10 @@ Widgets::DataGroup::DataGroup(const int index)
dicon->setStyleSheet(iconsQSS); dicon->setStyleSheet(iconsQSS);
// Set label initial data // Set label initial data
auto set = group->getDataset(dataset); auto set = group.getDataset(dataset);
if (set) title->setText(set.title());
{ if (!set.units().isEmpty())
title->setText(set->title()); units->setText(QString("[%1]").arg(set.units()));
if (!set->units().isEmpty())
units->setText(QString("[%1]").arg(set->units()));
}
// Set icon text // Set icon text
dicon->setText(""); dicon->setText("");
@ -196,32 +191,25 @@ void Widgets::DataGroup::updateData()
return; return;
// Get group pointer // Get group pointer
const auto dash = &UI::Dashboard::instance(); auto dash = &UI::Dashboard::instance();
const auto group = dash->getGroups(m_index); auto group = dash->getGroups(m_index);
if (!group)
return;
// Regular expresion handler // Regular expresion handler
const QRegularExpression regex("^[+-]?(\\d*\\.)?\\d+$"); const QRegularExpression regex("^[+-]?(\\d*\\.)?\\d+$");
// Update labels // Update labels
JSON::Dataset *dataset; for (int i = 0; i < group.datasetCount(); ++i)
for (int i = 0; i < group->datasetCount(); ++i)
{ {
dataset = group->getDataset(i); // Get dataset value
if (dataset) auto value = group.getDataset(i).value();
{
// Get dataset value
auto value = dataset->value();
// Check if value is a number, if so make sure that // Check if value is a number, if so make sure that
// we always show a fixed number of decimal places // we always show a fixed number of decimal places
if (regex.match(value).hasMatch()) if (regex.match(value).hasMatch())
value = QString::number(value.toDouble(), 'f', dash->precision()); value = QString::number(value.toDouble(), 'f', dash->precision());
// Update label // Update label
m_values.at(i)->setText(value + " "); m_values.at(i)->setText(value + " ");
}
} }
// Repaint widget // Repaint widget

View File

@ -87,46 +87,42 @@ Widgets::FFTPlot::FFTPlot(const int index)
m_curve.setPen(QColor(color), 2, Qt::SolidLine); m_curve.setPen(QColor(color), 2, Qt::SolidLine);
// Get dataset max freq. & calculate fft size // Get dataset max freq. & calculate fft size
const auto dataset = UI::Dashboard::instance().getFFT(m_index); auto dataset = UI::Dashboard::instance().getFFT(m_index);
if (dataset) int size = qMax(8, dataset.fftSamples());
// Ensure that FFT size is valid
while (m_transformer.setSize(size) != QFourierTransformer::FixedSize)
--size;
// Set FFT size
m_size = size;
// Initialize samples & FFT arrays
m_fft = (float *)calloc(m_size, sizeof(float));
m_samples = (float *)calloc(m_size, sizeof(float));
// Clear Y-axis data
PlotData xData;
PlotData yData;
xData.reserve(m_size);
yData.reserve(m_size);
for (int i = 0; i < m_size; ++i)
{ {
// Calculate FFT size yData.append(0);
int size = qMax(8, dataset->fftSamples()); xData.append(i);
// Ensure that FFT size is valid
while (m_transformer.setSize(size) != QFourierTransformer::FixedSize)
--size;
// Set FFT size
m_size = size;
// Initialize samples & FFT arrays
m_fft = (float *)calloc(m_size, sizeof(float));
m_samples = (float *)calloc(m_size, sizeof(float));
// Clear Y-axis data
PlotData xData;
PlotData yData;
xData.reserve(m_size);
yData.reserve(m_size);
for (int i = 0; i < m_size; ++i)
{
yData.append(0);
xData.append(i);
}
// Set y-scale from -1 to 1
m_plot.setAxisScale(QwtPlot::yLeft, -1, 1);
// Set axis titles
m_plot.setAxisTitle(QwtPlot::xBottom, tr("Samples"));
m_plot.setAxisTitle(QwtPlot::yLeft, tr("FFT of %1").arg(dataset->title()));
// Set curve data & replot
m_curve.setSamples(xData, yData);
m_plot.replot();
} }
// Set y-scale from -1 to 1
m_plot.setAxisScale(QwtPlot::yLeft, -1, 1);
// Set axis titles
m_plot.setAxisTitle(QwtPlot::xBottom, tr("Samples"));
m_plot.setAxisTitle(QwtPlot::yLeft, tr("FFT of %1").arg(dataset.title()));
// Set curve data & replot
m_curve.setSamples(xData, yData);
m_plot.replot();
// React to dashboard events // React to dashboard events
connect(dash, SIGNAL(updated()), this, SLOT(updateData()), Qt::QueuedConnection); connect(dash, SIGNAL(updated()), this, SLOT(updateData()), Qt::QueuedConnection);
} }
@ -157,11 +153,11 @@ void Widgets::FFTPlot::updateData()
return; return;
// Replot // Replot
const auto plotData = UI::Dashboard::instance().fftPlotValues(); auto plotData = UI::Dashboard::instance().fftPlotValues();
if (plotData->count() > m_index) if (plotData.count() > m_index)
{ {
// Copy data to samples array // Copy data to samples array
auto data = plotData->at(m_index); auto data = plotData.at(m_index);
for (int i = 0; i < m_size; ++i) for (int i = 0; i < m_size; ++i)
m_samples[i] = static_cast<float>(data[i]); m_samples[i] = static_cast<float>(data[i]);

View File

@ -115,28 +115,23 @@ void Widgets::GPS::updateData()
if (!isEnabled()) if (!isEnabled())
return; return;
// Get group pointer // Get group reference
const auto dash = &UI::Dashboard::instance(); const auto dash = &UI::Dashboard::instance();
const auto group = dash->getGPS(m_index); auto group = dash->getGPS(m_index);
if (!group)
return;
// Get latitiude/longitude from datasets // Get latitiude/longitude from datasets
qreal lat = -1; qreal lat = -1;
qreal lon = -1; qreal lon = -1;
qreal alt = -1; qreal alt = -1;
for (int i = 0; i < group->datasetCount(); ++i) for (int i = 0; i < group.datasetCount(); ++i)
{ {
const auto dataset = group->getDataset(i); auto dataset = group.getDataset(i);
if (dataset) if (dataset.widget() == "lat")
{ lat = dataset.value().toDouble();
if (dataset->widget() == "lat") else if (dataset.widget() == "lon")
lat = dataset->value().toDouble(); lon = dataset.value().toDouble();
else if (dataset->widget() == "lon") else if (dataset.widget() == "alt")
lon = dataset->value().toDouble(); alt = dataset.value().toDouble();
else if (dataset->widget() == "alt")
alt = dataset->value().toDouble();
}
} }
// Update map position // Update map position

View File

@ -55,8 +55,7 @@ Widgets::Gauge::Gauge(const int index)
// Set gauge scale // Set gauge scale
auto dataset = dash->getGauge(m_index); auto dataset = dash->getGauge(m_index);
if (dataset) m_gauge.setScale(dataset.min(), dataset.max());
m_gauge.setScale(dataset->min(), dataset->max());
// Set gauge palette // Set gauge palette
QPalette palette; QPalette palette;
@ -85,17 +84,12 @@ void Widgets::Gauge::updateData()
return; return;
// Update gauge value // Update gauge value
const auto dataset = UI::Dashboard::instance().getGauge(m_index); auto dataset = UI::Dashboard::instance().getGauge(m_index);
if (dataset) m_gauge.setValue(dataset.value().toDouble());
{ setValue(QString("%1 %2").arg(QString::number(dataset.value().toDouble(), 'f',
const auto value = dataset->value().toDouble(); UI::Dashboard::instance().precision()),
m_gauge.setValue(dataset->value().toDouble()); dataset.units()));
setValue(QString("%1 %2").arg( requestRepaint();
QString::number(value, 'f', UI::Dashboard::instance().precision()),
dataset->units()));
requestRepaint();
}
} }
#ifdef SERIAL_STUDIO_INCLUDE_MOC #ifdef SERIAL_STUDIO_INCLUDE_MOC

View File

@ -70,43 +70,37 @@ Widgets::Gyroscope::Gyroscope(const int index)
*/ */
void Widgets::Gyroscope::updateData() void Widgets::Gyroscope::updateData()
{ {
// Widget not enabled, do nothing
if (!isEnabled()) if (!isEnabled())
return; return;
// Update gyroscope values
const auto dash = &UI::Dashboard::instance(); const auto dash = &UI::Dashboard::instance();
const auto gyro = dash->getGyroscope(m_index); auto gyro = dash->getGyroscope(m_index);
if (gyro) if (gyro.datasetCount() != 3)
return;
double pitch = 0;
double roll = 0;
double yaw = 0;
for (int i = 0; i < 3; ++i)
{ {
if (gyro->datasetCount() != 3) auto dataset = gyro.getDataset(i);
return; if (dataset.widget() == "pitch")
pitch = dataset.value().toDouble();
double pitch = 0; if (dataset.widget() == "roll")
double roll = 0; roll = dataset.value().toDouble();
double yaw = 0; if (dataset.widget() == "yaw")
yaw = dataset.value().toDouble();
JSON::Dataset *dataset;
for (int i = 0; i < 3; ++i)
{
dataset = gyro->getDataset(i);
if (dataset->widget() == "pitch")
pitch = dataset->value().toDouble();
if (dataset->widget() == "roll")
roll = dataset->value().toDouble();
if (dataset->widget() == "yaw")
yaw = dataset->value().toDouble();
}
m_yaw = QString::number(qAbs(yaw), 'f', dash->precision());
m_roll = QString::number(qAbs(roll), 'f', dash->precision());
m_pitch = QString::number(qAbs(pitch), 'f', dash->precision());
m_gauge.setValue(pitch);
m_gauge.setGradient(roll / 360.0);
requestRepaint();
} }
m_yaw = QString::number(qAbs(yaw), 'f', dash->precision());
m_roll = QString::number(qAbs(roll), 'f', dash->precision());
m_pitch = QString::number(qAbs(pitch), 'f', dash->precision());
m_gauge.setValue(pitch);
m_gauge.setGradient(roll / 360.0);
requestRepaint();
} }
void Widgets::Gyroscope::updateLabel() void Widgets::Gyroscope::updateLabel()

View File

@ -40,10 +40,8 @@ Widgets::LEDPanel::LEDPanel(const int index)
if (m_index < 0 || m_index >= dash->ledCount()) if (m_index < 0 || m_index >= dash->ledCount())
return; return;
// Get group pointer // Get group reference
const auto group = dash->getLED(m_index); auto group = dash->getLED(m_index);
if (!group)
return;
// Generate widget stylesheets // Generate widget stylesheets
const auto titleQSS = QSS("color:%1", theme->widgetTextPrimary()); const auto titleQSS = QSS("color:%1", theme->widgetTextPrimary());
@ -62,11 +60,11 @@ Widgets::LEDPanel::LEDPanel(const int index)
valueFont.setPixelSize(dash->monoFont().pixelSize() * 1.3); valueFont.setPixelSize(dash->monoFont().pixelSize() * 1.3);
// Configure grid layout // Configure grid layout
m_leds.reserve(group->datasetCount()); m_leds.reserve(group.datasetCount());
m_titles.reserve(group->datasetCount()); m_titles.reserve(group.datasetCount());
m_gridLayout = new QGridLayout(m_dataContainer); m_gridLayout = new QGridLayout(m_dataContainer);
m_gridLayout->setSpacing(16); m_gridLayout->setSpacing(16);
for (int dataset = 0; dataset < group->datasetCount(); ++dataset) for (int dataset = 0; dataset < group.datasetCount(); ++dataset)
{ {
// Create labels // Create labels
m_leds.append(new KLed(m_dataContainer)); m_leds.append(new KLed(m_dataContainer));
@ -79,7 +77,7 @@ Widgets::LEDPanel::LEDPanel(const int index)
// Set label styles & fonts // Set label styles & fonts
title->setStyleSheet(titleQSS); title->setStyleSheet(titleQSS);
title->setFont(dash->monoFont()); title->setFont(dash->monoFont());
title->setText(group->getDataset(dataset)->title()); title->setText(group.getDataset(dataset).title());
// Set LED color & style // Set LED color & style
led->setLook(KLed::Sunken); led->setLook(KLed::Sunken);
@ -156,28 +154,21 @@ void Widgets::LEDPanel::updateData()
// Get group pointer // Get group pointer
const auto dash = &UI::Dashboard::instance(); const auto dash = &UI::Dashboard::instance();
const auto group = dash->getLED(m_index); auto group = dash->getLED(m_index);
if (!group)
return;
// Update labels // Update labels
JSON::Dataset *dataset; for (int i = 0; i < group.datasetCount(); ++i)
for (int i = 0; i < group->datasetCount(); ++i)
{ {
dataset = group->getDataset(i); // Get dataset value (we compare with 0.1 for low voltages)
if (dataset) const auto value = group.getDataset(i).value().toDouble();
{ if (qAbs(value) < 0.10)
// Get dataset value (we compare with 0.1 for low voltages) m_leds.at(i)->off();
const auto value = dataset->value().toDouble(); else
if (qAbs(value) < 0.10) m_leds.at(i)->on();
m_leds.at(i)->off();
else
m_leds.at(i)->on();
// Repaint widget
requestRepaint();
}
} }
// Repaint widget
requestRepaint();
} }
/** /**

View File

@ -68,19 +68,16 @@ Widgets::MultiPlot::MultiPlot(const int index)
// Create curves from datasets // Create curves from datasets
bool normalize = true; bool normalize = true;
const auto group = dash->getMultiplot(m_index); auto group = dash->getMultiplot(m_index);
StringList colors = theme->widgetColors(); StringList colors = theme->widgetColors();
m_curves.reserve(group->datasetCount()); m_curves.reserve(group.datasetCount());
for (int i = 0; i < group->datasetCount(); ++i) for (int i = 0; i < group.datasetCount(); ++i)
{ {
// Get dataset title & min/max values // Get dataset title & min/max values
QString title = tr("Unknown"); QString title = tr("Unknown");
const auto dataset = group->getDataset(i); auto dataset = group.getDataset(i);
if (dataset) title = dataset.title();
{ normalize &= dataset.max() > dataset.min();
title = group->getDataset(i)->title();
normalize &= dataset->max() > dataset->min();
}
// Create curve // Create curve
auto curve = new QwtPlotCurve(title); auto curve = new QwtPlotCurve(title);
@ -102,7 +99,7 @@ Widgets::MultiPlot::MultiPlot(const int index)
// Add plot legend to display curve names // Add plot legend to display curve names
m_legend.setFrameStyle(QFrame::Plain); m_legend.setFrameStyle(QFrame::Plain);
m_plot.setAxisTitle(QwtPlot::yLeft, group->title()); m_plot.setAxisTitle(QwtPlot::yLeft, group.title());
m_plot.setAxisTitle(QwtPlot::xBottom, tr("Samples")); m_plot.setAxisTitle(QwtPlot::xBottom, tr("Samples"));
m_plot.insertLegend(&m_legend, QwtPlot::BottomLegend); m_plot.insertLegend(&m_legend, QwtPlot::BottomLegend);
@ -137,34 +134,30 @@ Widgets::MultiPlot::MultiPlot(const int index)
void Widgets::MultiPlot::updateData() void Widgets::MultiPlot::updateData()
{ {
// Get group // Get group
const auto group = UI::Dashboard::instance().getMultiplot(m_index); auto group = UI::Dashboard::instance().getMultiplot(m_index);
if (!group)
return;
// Plot each dataset // Plot each dataset
for (int i = 0; i < group->datasetCount(); ++i) for (int i = 0; i < group.datasetCount(); ++i)
{ {
// Get dataset // Get dataset
const auto dataset = group->getDataset(i); auto dataset = group.getDataset(i);
if (!dataset)
continue;
// Add point to plot data // Add point to plot data
const auto count = m_yData[i].count(); const auto count = m_yData[i].count();
memmove(m_yData[i].data(), m_yData[i].data() + 1, count * sizeof(double)); memmove(m_yData[i].data(), m_yData[i].data() + 1, count * sizeof(double));
// Normalize dataset value // Normalize dataset value
if (dataset->max() > dataset->min()) if (dataset.max() > dataset.min())
{ {
const auto vmin = dataset->min(); const auto vmin = dataset.min();
const auto vmax = dataset->max(); const auto vmax = dataset.max();
const auto v = dataset->value().toDouble(); const auto v = dataset.value().toDouble();
m_yData[i][count - 1] = (v - vmin) / (vmax - vmin); m_yData[i][count - 1] = (v - vmin) / (vmax - vmin);
} }
// Plot dataset value directly // Plot dataset value directly
else else
m_yData[i][count - 1] = dataset->value().toDouble(); m_yData[i][count - 1] = dataset.value().toDouble();
// Widget not enabled, do not redraw // Widget not enabled, do not redraw
if (!isEnabled()) if (!isEnabled())
@ -192,7 +185,7 @@ void Widgets::MultiPlot::updateRange()
// Set number of points // Set number of points
m_yData.clear(); m_yData.clear();
const auto group = UI::Dashboard::instance().getMultiplot(m_index); auto group = UI::Dashboard::instance().getMultiplot(m_index);
for (int i = 0; i < dash->points(); ++i) for (int i = 0; i < dash->points(); ++i)
{ {
m_yData.append(PlotData()); m_yData.append(PlotData());
@ -202,9 +195,9 @@ void Widgets::MultiPlot::updateRange()
} }
// Create curve from data // Create curve from data
for (int i = 0; i < group->datasetCount(); ++i) for (int i = 0; i < group.datasetCount(); ++i)
if (m_curves.count() > i) if (m_curves.count() > i)
m_curves.at(i)->setSamples(*dash->xPlotValues(), m_yData[i]); m_curves.at(i)->setSamples(dash->xPlotValues(), m_yData[i]);
// Repaint widget // Repaint widget
requestRepaint(); requestRepaint();

View File

@ -86,33 +86,29 @@ Widgets::Plot::Plot(const int index)
// Set curve color & plot style // Set curve color & plot style
m_curve.setPen(QColor(color), 2, Qt::SolidLine); m_curve.setPen(QColor(color), 2, Qt::SolidLine);
// Get dataset units // Update graph scale
const auto dataset = UI::Dashboard::instance().getPlot(m_index); auto dataset = UI::Dashboard::instance().getPlot(m_index);
if (dataset) const auto max = dataset.max();
const auto min = dataset.min();
if (max > min)
{ {
// Update graph scale m_max = max;
const auto max = dataset->max(); m_min = min;
const auto min = dataset->min(); m_autoscale = false;
if (max > min) m_plot.setAxisScale(QwtPlot::yLeft, m_min, m_max);
{
m_max = max;
m_min = min;
m_autoscale = false;
m_plot.setAxisScale(QwtPlot::yLeft, m_min, m_max);
}
// Enable logarithmic scale
// clang-format off
if (dataset->log())
m_plot.setAxisScaleEngine(QwtPlot::yLeft,
new QwtLogScaleEngine(10));
// clang-format on
// Set axis titles
m_plot.setAxisTitle(QwtPlot::xBottom, tr("Samples"));
m_plot.setAxisTitle(QwtPlot::yLeft, dataset->title());
} }
// Enable logarithmic scale
// clang-format off
if (dataset.log())
m_plot.setAxisScaleEngine(QwtPlot::yLeft,
new QwtLogScaleEngine(10));
// clang-format on
// Set axis titles
m_plot.setAxisTitle(QwtPlot::xBottom, tr("Samples"));
m_plot.setAxisTitle(QwtPlot::yLeft, dataset.title());
// React to dashboard events // React to dashboard events
// clang-format off // clang-format off
connect(dash, SIGNAL(updated()), connect(dash, SIGNAL(updated()),
@ -139,17 +135,17 @@ void Widgets::Plot::updateData()
return; return;
// Get new data // Get new data
const auto plotData = UI::Dashboard::instance().linearPlotValues(); auto plotData = UI::Dashboard::instance().linearPlotValues();
if (plotData->count() > m_index) if (plotData.count() > m_index)
{ {
// Check if we need to update graph scale // Check if we need to update graph scale
if (m_autoscale) if (m_autoscale)
{ {
// Scan new values to see if chart should be updated // Scan new values to see if chart should be updated
bool changed = false; bool changed = false;
for (int i = 0; i < plotData->at(m_index).count(); ++i) for (int i = 0; i < plotData.at(m_index).count(); ++i)
{ {
const auto v = plotData->at(m_index).at(i); const auto v = plotData.at(m_index).at(i);
if (v > m_max) if (v > m_max)
{ {
m_max = v + 1; m_max = v + 1;
@ -200,7 +196,7 @@ void Widgets::Plot::updateData()
} }
// Replot graph // Replot graph
m_curve.setSamples(plotData->at(m_index)); m_curve.setSamples(plotData.at(m_index));
m_plot.replot(); m_plot.replot();
// Repaint widget // Repaint widget
@ -223,7 +219,7 @@ void Widgets::Plot::updateRange()
tempYData.append(0); tempYData.append(0);
// Redraw graph // Redraw graph
m_curve.setSamples(*dash->xPlotValues(), tempYData); m_curve.setSamples(dash->xPlotValues(), tempYData);
m_plot.replot(); m_plot.replot();
// Repaint widget // Repaint widget