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
{
if (group < groupCount())
return m_groups.at(group)->m_datasets.count();
return 0;
return getGroup(group).m_datasets.count();
}
/**
* 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 Q_NULLPTR;
return m_groups.at(index);
}
/**
* Returns a pointer to the dataset object contained by the @a group at
* 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)->m_datasets.at(index);
return Q_NULLPTR;
return getGroup(group).getDataset(index);
}
/**
@ -401,11 +392,7 @@ JSON::Dataset *JSON::Editor::getDataset(const int group, const int index) const
*/
QString JSON::Editor::groupTitle(const int group) const
{
const auto grp = getGroup(group);
if (grp)
return grp->m_title;
return "";
return getGroup(group).title();
}
/**
@ -413,11 +400,7 @@ QString JSON::Editor::groupTitle(const int group) const
*/
QString JSON::Editor::groupWidget(const int group) const
{
const auto grp = getGroup(group);
if (grp)
return grp->m_widget;
return "";
return getGroup(group).widget();
}
/**
@ -428,23 +411,19 @@ QString JSON::Editor::groupWidget(const int group) const
*/
int JSON::Editor::groupWidgetIndex(const int group) const
{
const auto grp = getGroup(group);
if (grp)
{
const auto widget = grp->m_widget;
auto widget = groupWidget(group);
if (widget == "accelerometer")
return 1;
if (widget == "accelerometer")
return 1;
if (widget == "gyro")
return 2;
if (widget == "gyro")
return 2;
if (widget == "map")
return 3;
if (widget == "map")
return 3;
if (widget == "multiplot")
return 4;
}
if (widget == "multiplot")
return 4;
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
{
const auto set = getDataset(group, dataset);
if (set)
return set->m_index;
return 0;
return getDataset(group, dataset).m_index;
}
/**
@ -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
{
const auto set = getDataset(group, dataset);
if (set)
return set->led();
return false;
return getDataset(group, dataset).led();
}
/**
@ -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
{
const auto set = getDataset(group, dataset);
if (set)
return set->graph();
return false;
return getDataset(group, dataset).graph();
}
/**
@ -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
{
const auto set = getDataset(group, dataset);
if (set)
return set->fft();
return false;
return getDataset(group, dataset).fft();
}
/**
@ -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
{
const auto set = getDataset(group, dataset);
if (set)
return set->log();
return false;
return getDataset(group, dataset).log();
}
/**
@ -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
{
const auto set = getDataset(group, dataset);
if (set)
return set->title();
return "";
return getDataset(group, dataset).title();
}
/**
@ -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
{
const auto set = getDataset(group, dataset);
if (set)
return set->units();
return "";
return getDataset(group, dataset).units();
}
/**
@ -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
{
const auto set = getDataset(group, dataset);
if (set)
return set->widget();
return "";
return getDataset(group, dataset).widget();
}
/**
@ -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
{
const auto widget = datasetWidget(group, dataset);
auto widget = datasetWidget(group, dataset);
if (widget == "gauge")
return 1;
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
{
const auto set = getDataset(group, dataset);
if (set)
return QString::number(set->m_min);
return "0";
return QString::number(getDataset(group, dataset).min());
}
/**
@ -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
{
const auto set = getDataset(group, dataset);
if (set)
return QString::number(set->m_max);
return "0";
return QString::number(getDataset(group, dataset).max());
}
/**
@ -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
{
const auto set = getDataset(group, dataset);
if (set)
return QString::number(qMax(1, set->m_fftSamples));
return "0";
return QString::number(getDataset(group, dataset).fftSamples());
}
/**
@ -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
{
const 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);
}
auto set = getDataset(group, dataset);
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()
{
// Clear groups list
qDeleteAll(m_groups);
m_groups.clear();
// Reset project properties
@ -862,7 +793,7 @@ void JSON::Editor::setFrameStartSequence(const QString &sequence)
*/
void JSON::Editor::addGroup()
{
m_groups.append(new Group);
m_groups.append(Group());
setGroupTitle(m_groups.count() - 1, tr("New Group"));
Q_EMIT groupCountChanged();
@ -874,18 +805,15 @@ void JSON::Editor::addGroup()
*/
void JSON::Editor::deleteGroup(const int group)
{
const auto grp = getGroup(group);
if (grp)
auto ret = Misc::Utilities::showMessageBox(
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(
tr("Delete group \"%1\"").arg(grp->title()),
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();
}
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)
{
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 (!(grp->m_datasets.isEmpty()) && widgetId != 4)
{
if (widgetId == 0 && grp->widget() == "multiplot")
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
{
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();
}
}
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)
{
// Get group
auto grp = getGroup(group);
if (grp)
{
grp->m_title = title;
Q_EMIT groupChanged(group);
}
// Change group values & update groups vector
grp.m_title = title;
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)
{
// Get group
auto grp = getGroup(group);
if (grp)
{
grp->m_widget = widget;
Q_EMIT groupChanged(group);
}
// Change group values & update groups vector
grp.m_widget = widget;
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)
{
// Get group
auto grp = getGroup(group);
if (grp)
{
grp->m_datasets.append(new Dataset);
setDatasetIndex(group, grp->m_datasets.count() - 1, nextDatasetIndex());
setDatasetTitle(group, grp->m_datasets.count() - 1, tr("New dataset"));
Q_EMIT groupChanged(group);
}
// Change group values & update groups vector
grp.m_datasets.append(Dataset());
m_groups.replace(group, grp);
// 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)
{
const auto set = getDataset(group, dataset);
if (set)
auto ret = Misc::Utilities::showMessageBox(
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(
tr("Delete dataset \"%1\"").arg(set->title()),
tr("Are you sure you want to delete this dataset?"), APP_NAME,
QMessageBox::Yes | QMessageBox::No);
if (ret == QMessageBox::Yes)
{
getGroup(group)->m_datasets.removeAt(dataset);
Q_EMIT groupChanged(group);
}
auto grp = getGroup(group);
grp.m_datasets.removeAt(dataset);
m_groups.replace(group, grp);
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,
const QString &title)
{
// Get dataset & group
auto grp = getGroup(group);
auto set = getDataset(group, dataset);
if (set)
{
set->m_title = title;
Q_EMIT datasetChanged(group, dataset);
}
// Update dataset & group
set.m_title = title;
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,
const QString &units)
{
// Get dataset & group
auto grp = getGroup(group);
auto set = getDataset(group, dataset);
if (set)
{
set->m_units = units;
Q_EMIT datasetChanged(group, dataset);
}
// Update dataset & group
set.m_units = units;
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,
const int frameIndex)
{
// Get dataset & group
auto grp = getGroup(group);
auto set = getDataset(group, dataset);
if (set)
{
set->m_index = frameIndex;
Q_EMIT datasetChanged(group, dataset);
}
// Update dataset & group
set.m_index = frameIndex;
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,
const bool generateLED)
{
// Get dataset & group
auto grp = getGroup(group);
auto set = getDataset(group, dataset);
if (set)
{
set->m_led = generateLED;
Q_EMIT datasetChanged(group, dataset);
}
// Update dataset & group
set.m_led = generateLED;
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,
const bool generateGraph)
{
// Get dataset & group
auto grp = getGroup(group);
auto set = getDataset(group, dataset);
if (set)
{
set->m_graph = generateGraph;
Q_EMIT datasetChanged(group, dataset);
}
// Update dataset & group
set.m_graph = generateGraph;
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,
const bool generateFft)
{
// Get dataset & group
auto grp = getGroup(group);
auto set = getDataset(group, dataset);
if (set)
{
set->m_fft = generateFft;
Q_EMIT datasetChanged(group, dataset);
}
// Update dataset & group
set.m_fft = generateFft;
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,
const bool generateLog)
{
// Get dataset & group
auto grp = getGroup(group);
auto set = getDataset(group, dataset);
if (set)
{
set->m_log = generateLog;
Q_EMIT datasetChanged(group, dataset);
}
// Update dataset & group
set.m_log = generateLog;
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,
const int widgetId)
{
// Get dataset & group
auto grp = getGroup(group);
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;
Q_EMIT datasetChanged(group, dataset);
// Get widget string
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,
const QString &minimum)
{
// Get dataset & group
auto grp = getGroup(group);
auto set = getDataset(group, dataset);
if (set)
{
set->m_min = minimum.toDouble();
Q_EMIT datasetChanged(group, dataset);
}
// Update dataset & group
set.m_min = minimum.toDouble();
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,
const QString &maximum)
{
// Get dataset & group
auto grp = getGroup(group);
auto set = getDataset(group, dataset);
if (set)
{
set->m_max = maximum.toDouble();
Q_EMIT datasetChanged(group, dataset);
}
// Update dataset & group
set.m_max = maximum.toDouble();
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,
const QString &widget)
{
// Get dataset & group
auto grp = getGroup(group);
auto set = getDataset(group, dataset);
if (set)
{
set->m_widget = widget;
Q_EMIT datasetChanged(group, dataset);
}
// 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);
}
/**
@ -1346,12 +1330,17 @@ void JSON::Editor::setDatasetWidgetData(const int group, const int dataset,
void JSON::Editor::setDatasetWidgetAlarm(const int group, const int dataset,
const QString &alarm)
{
// Get dataset & group
auto grp = getGroup(group);
auto set = getDataset(group, dataset);
if (set)
{
set->m_alarm = alarm.toDouble();
Q_EMIT datasetChanged(group, dataset);
}
// Update dataset & group
set.m_alarm = alarm.toDouble();
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,
const QString &samples)
{
// Get dataset & group
auto grp = getGroup(group);
auto set = getDataset(group, dataset);
if (set)
{
auto sample = samples.toInt();
if (sample < 1)
sample = 1;
set->m_fftSamples = sample;
Q_EMIT datasetChanged(group, dataset);
}
// Validate FFT samples
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)
{
const auto dataset = getDataset(i, j);
if (dataset)
{
if (dataset->m_index >= maxIndex)
maxIndex = dataset->m_index + 1;
}
auto dataset = getDataset(i, j);
if (dataset.m_index >= maxIndex)
maxIndex = dataset.m_index + 1;
}
}

View File

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

View File

@ -29,7 +29,6 @@
*/
JSON::Frame::~Frame()
{
qDeleteAll(m_groups);
m_groups.clear();
}
@ -40,7 +39,6 @@ JSON::Frame::~Frame()
void JSON::Frame::clear()
{
m_title = "";
qDeleteAll(m_groups);
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.
*/
QVector<JSON::Group *> JSON::Frame::groups() const
QVector<JSON::Group> JSON::Frame::groups() const
{
return m_groups;
}
@ -92,11 +90,9 @@ bool JSON::Frame::read(const QJsonObject &object)
// Generate groups & datasets from data frame
for (auto i = 0; i < groups.count(); ++i)
{
Group *group = new Group();
if (group->read(groups.at(i).toObject()))
Group group;
if (group.read(groups.at(i).toObject()))
m_groups.append(group);
else
delete group;
}
// 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 Q_NULLPTR;
return m_groups.at(index);
}

View File

@ -60,14 +60,14 @@ public:
void clear();
QString title() const;
int groupCount() const;
QVector<Group *> groups() const;
QVector<Group> groups() const;
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; }
private:
QString m_title;
QVector<Group *> m_groups;
QVector<Group> m_groups;
};
}

View File

@ -29,7 +29,6 @@
*/
JSON::Group::~Group()
{
qDeleteAll(m_datasets);
m_datasets.clear();
}
@ -60,7 +59,7 @@ int JSON::Group::datasetCount() const
/**
* @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;
}
@ -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
*/
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 Q_NULLPTR;
return m_datasets.at(index);
}
/**
@ -101,11 +97,9 @@ bool JSON::Group::read(const QJsonObject &object)
const auto object = array.at(i).toObject();
if (!object.isEmpty())
{
Dataset *dataset = new Dataset();
if (dataset->read(object))
Dataset dataset;
if (dataset.read(object))
m_datasets.append(dataset);
else
delete dataset;
}
}

View File

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

View File

@ -72,17 +72,17 @@ QFont UI::Dashboard::monoFont() const
}
// clang-format off
JSON::Group *UI::Dashboard::getLED(const int index) { return getGroupWidget(m_ledWidgets, index); }
JSON::Group *UI::Dashboard::getGPS(const int index) { return getGroupWidget(m_gpsWidgets, index); }
JSON::Dataset *UI::Dashboard::getBar(const int index) { return getDatasetWidget(m_barWidgets, index); }
JSON::Dataset *UI::Dashboard::getFFT(const int index) { return getDatasetWidget(m_fftWidgets, index); }
JSON::Dataset *UI::Dashboard::getPlot(const int index) { return getDatasetWidget(m_plotWidgets, index); }
JSON::Group *UI::Dashboard::getGroups(const int index) { return getGroupWidget(m_groupWidgets, index); }
JSON::Dataset *UI::Dashboard::getGauge(const int index) { return getDatasetWidget(m_gaugeWidgets, index); }
JSON::Group *UI::Dashboard::getGyroscope(const int index) { return getGroupWidget(m_gyroscopeWidgets, index); }
JSON::Dataset *UI::Dashboard::getCompass(const int index) { return getDatasetWidget(m_compassWidgets, index); }
JSON::Group *UI::Dashboard::getMultiplot(const int index) { return getGroupWidget(m_multiPlotWidgets, index); }
JSON::Group *UI::Dashboard::getAccelerometer(const int index) { return getGroupWidget(m_accelerometerWidgets, index); }
const JSON::Group &UI::Dashboard::getLED(const int index) { return m_ledWidgets.at(index); }
const JSON::Group &UI::Dashboard::getGPS(const int index) { return m_gpsWidgets.at(index); }
const JSON::Dataset &UI::Dashboard::getBar(const int index) { return m_barWidgets.at(index); }
const JSON::Dataset &UI::Dashboard::getFFT(const int index) { return m_fftWidgets.at(index); }
const JSON::Dataset &UI::Dashboard::getPlot(const int index) { return m_plotWidgets.at(index); }
const JSON::Group &UI::Dashboard::getGroups(const int index) { return m_groupWidgets.at(index); }
const JSON::Dataset &UI::Dashboard::getGauge(const int index) { return m_gaugeWidgets.at(index); }
const JSON::Group &UI::Dashboard::getGyroscope(const int index) { return m_gyroscopeWidgets.at(index); }
const JSON::Dataset &UI::Dashboard::getCompass(const int index) { return m_compassWidgets.at(index); }
const JSON::Group &UI::Dashboard::getMultiplot(const int index) { return m_multiPlotWidgets.at(index); }
const JSON::Group &UI::Dashboard::getAccelerometer(const int index) { return m_accelerometerWidgets.at(index); }
// clang-format on
//----------------------------------------------------------------------------------------
@ -669,13 +669,13 @@ void UI::Dashboard::updatePlots()
for (int i = 0; i < m_latestFrame.groupCount(); ++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);
if (dataset->fft())
fftDatasets.append(dataset);
if (dataset->graph())
linearDatasets.append(dataset);
auto dataset = group.getDataset(j);
if (dataset.fft())
fftDatasets.append(&dataset);
if (dataset.graph())
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
* 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 dataset, group->datasets())
Q_FOREACH (auto dataset, group.datasets())
{
if (dataset->led())
if (dataset.led())
widgets.append(dataset);
}
}
QVector<JSON::Group *> groups;
QVector<JSON::Group> groups;
if (widgets.count() > 0)
{
JSON::Group *group = new JSON::Group();
group->m_title = tr("Status Panel");
group->m_datasets = widgets;
JSON::Group group;
group.m_title = tr("Status Panel");
group.m_datasets = widgets;
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.
*/
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 dataset, group->datasets())
Q_FOREACH (auto dataset, group.datasets())
{
if (dataset->fft())
if (dataset.fft())
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.
*/
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 dataset, group->datasets())
Q_FOREACH (auto dataset, group.datasets())
{
if (dataset->graph())
if (dataset.graph())
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
* @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())
{
if (group->widget() == handle)
if (group.widget() == handle)
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
* @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 dataset, group->datasets())
Q_FOREACH (auto dataset, group.datasets())
{
if (dataset->widget() == handle)
if (dataset.widget() == handle)
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.
*/
StringList UI::Dashboard::datasetTitles(const QVector<JSON::Dataset *> &vector) const
StringList UI::Dashboard::datasetTitles(const QVector<JSON::Dataset> &vector) const
{
StringList list;
Q_FOREACH (auto set, vector)
list.append(set->title());
list.append(set.title());
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.
*/
StringList UI::Dashboard::groupTitles(const QVector<JSON::Group *> &vector) const
StringList UI::Dashboard::groupTitles(const QVector<JSON::Group> &vector) const
{
StringList list;
Q_FOREACH (auto group, vector)
list.append(group->title());
list.append(group.title());
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
# include "moc_Dashboard.cpp"
#endif

View File

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

View File

@ -80,46 +80,39 @@ Widgets::Accelerometer::Accelerometer(const int index)
*/
void Widgets::Accelerometer::updateData()
{
// Widget not enabled, do nothing
if (!isEnabled())
return;
// Update accelerometer values
const auto accelerometer = UI::Dashboard::instance().getAccelerometer(m_index);
if (accelerometer)
auto accelerometer = UI::Dashboard::instance().getAccelerometer(m_index);
if (accelerometer.datasetCount() != 3)
return;
double x = 0;
double y = 0;
double z = 0;
for (int i = 0; i < 3; ++i)
{
if (accelerometer->datasetCount() != 3)
return;
double x = 0;
double y = 0;
double z = 0;
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();
auto 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())));
requestRepaint();
}
#ifdef SERIAL_STUDIO_INCLUDE_MOC

View File

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

View File

@ -79,29 +79,23 @@ Widgets::Compass::Compass(const int index)
*/
void Widgets::Compass::update()
{
// Widget not enabled, do nothing
if (!isEnabled())
return;
// Update compass heading
const auto dataset = UI::Dashboard::instance().getCompass(m_index);
if (dataset)
{
auto value = dataset->value().toDouble();
auto text = QString("%1°").arg(
QString::number(value, 'f', UI::Dashboard::instance().precision()));
m_compass.setValue(value);
auto dataset = UI::Dashboard::instance().getCompass(m_index);
auto value = dataset.value().toDouble();
auto text = QString("%1°").arg(
QString::number(value, 'f', UI::Dashboard::instance().precision()));
if (text.length() == 2)
text.prepend("00");
else if (text.length() == 3)
text.prepend("0");
if (text.length() == 2)
text.prepend("00");
else if (text.length() == 3)
text.prepend("0");
setValue(text);
setValue(text);
m_compass.setValue(value);
// Repaint widget
requestRepaint();
}
requestRepaint();
}
#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())
return;
// Get group pointer
const auto group = dash->getGroups(m_index);
if (!group)
return;
// Get group reference
auto group = dash->getGroups(m_index);
// Generate widget stylesheets
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);
// Configure grid layout
m_units.reserve(group->datasetCount());
m_icons.reserve(group->datasetCount());
m_titles.reserve(group->datasetCount());
m_values.reserve(group->datasetCount());
m_units.reserve(group.datasetCount());
m_icons.reserve(group.datasetCount());
m_titles.reserve(group.datasetCount());
m_values.reserve(group.datasetCount());
m_gridLayout = new QGridLayout(m_dataContainer);
for (int dataset = 0; dataset < group->datasetCount(); ++dataset)
for (int dataset = 0; dataset < group.datasetCount(); ++dataset)
{
// Create labels
m_units.append(new QLabel(m_dataContainer));
@ -117,13 +115,10 @@ Widgets::DataGroup::DataGroup(const int index)
dicon->setStyleSheet(iconsQSS);
// Set label initial data
auto set = group->getDataset(dataset);
if (set)
{
title->setText(set->title());
if (!set->units().isEmpty())
units->setText(QString("[%1]").arg(set->units()));
}
auto set = group.getDataset(dataset);
title->setText(set.title());
if (!set.units().isEmpty())
units->setText(QString("[%1]").arg(set.units()));
// Set icon text
dicon->setText("");
@ -196,32 +191,25 @@ void Widgets::DataGroup::updateData()
return;
// Get group pointer
const auto dash = &UI::Dashboard::instance();
const auto group = dash->getGroups(m_index);
if (!group)
return;
auto dash = &UI::Dashboard::instance();
auto group = dash->getGroups(m_index);
// Regular expresion handler
const QRegularExpression regex("^[+-]?(\\d*\\.)?\\d+$");
// 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);
if (dataset)
{
// Get dataset value
auto value = dataset->value();
// Get dataset value
auto value = group.getDataset(i).value();
// Check if value is a number, if so make sure that
// we always show a fixed number of decimal places
if (regex.match(value).hasMatch())
value = QString::number(value.toDouble(), 'f', dash->precision());
// Check if value is a number, if so make sure that
// we always show a fixed number of decimal places
if (regex.match(value).hasMatch())
value = QString::number(value.toDouble(), 'f', dash->precision());
// Update label
m_values.at(i)->setText(value + " ");
}
// Update label
m_values.at(i)->setText(value + " ");
}
// Repaint widget

View File

@ -87,46 +87,42 @@ Widgets::FFTPlot::FFTPlot(const int index)
m_curve.setPen(QColor(color), 2, Qt::SolidLine);
// Get dataset max freq. & calculate fft size
const auto dataset = UI::Dashboard::instance().getFFT(m_index);
if (dataset)
auto dataset = UI::Dashboard::instance().getFFT(m_index);
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
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)
{
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();
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();
// React to dashboard events
connect(dash, SIGNAL(updated()), this, SLOT(updateData()), Qt::QueuedConnection);
}
@ -157,11 +153,11 @@ void Widgets::FFTPlot::updateData()
return;
// Replot
const auto plotData = UI::Dashboard::instance().fftPlotValues();
if (plotData->count() > m_index)
auto plotData = UI::Dashboard::instance().fftPlotValues();
if (plotData.count() > m_index)
{
// 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)
m_samples[i] = static_cast<float>(data[i]);

View File

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

View File

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

View File

@ -70,43 +70,37 @@ Widgets::Gyroscope::Gyroscope(const int index)
*/
void Widgets::Gyroscope::updateData()
{
// Widget not enabled, do nothing
if (!isEnabled())
return;
// Update gyroscope values
const auto dash = &UI::Dashboard::instance();
const auto gyro = dash->getGyroscope(m_index);
if (gyro)
auto gyro = dash->getGyroscope(m_index);
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)
return;
double pitch = 0;
double roll = 0;
double yaw = 0;
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();
auto 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();
}
void Widgets::Gyroscope::updateLabel()

View File

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

View File

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

View File

@ -86,33 +86,29 @@ Widgets::Plot::Plot(const int index)
// Set curve color & plot style
m_curve.setPen(QColor(color), 2, Qt::SolidLine);
// Get dataset units
const auto dataset = UI::Dashboard::instance().getPlot(m_index);
if (dataset)
// Update graph scale
auto dataset = UI::Dashboard::instance().getPlot(m_index);
const auto max = dataset.max();
const auto min = dataset.min();
if (max > min)
{
// Update graph scale
const auto max = dataset->max();
const auto min = dataset->min();
if (max > min)
{
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());
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());
// React to dashboard events
// clang-format off
connect(dash, SIGNAL(updated()),
@ -139,17 +135,17 @@ void Widgets::Plot::updateData()
return;
// Get new data
const auto plotData = UI::Dashboard::instance().linearPlotValues();
if (plotData->count() > m_index)
auto plotData = UI::Dashboard::instance().linearPlotValues();
if (plotData.count() > m_index)
{
// Check if we need to update graph scale
if (m_autoscale)
{
// Scan new values to see if chart should be updated
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)
{
m_max = v + 1;
@ -200,7 +196,7 @@ void Widgets::Plot::updateData()
}
// Replot graph
m_curve.setSamples(plotData->at(m_index));
m_curve.setSamples(plotData.at(m_index));
m_plot.replot();
// Repaint widget
@ -223,7 +219,7 @@ void Widgets::Plot::updateRange()
tempYData.append(0);
// Redraw graph
m_curve.setSamples(*dash->xPlotValues(), tempYData);
m_curve.setSamples(dash->xPlotValues(), tempYData);
m_plot.replot();
// Repaint widget