Skip to content

Commit

Permalink
Enhance settings load/save
Browse files Browse the repository at this point in the history
  • Loading branch information
dendvz authored and David Gräff committed Dec 24, 2017
1 parent cfb6bad commit 3ccfb5b
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 79 deletions.
20 changes: 10 additions & 10 deletions openhantek/src/scopesettings.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,27 +38,27 @@ struct DsoSettingsScopeTrigger {
/// \brief Holds the settings for the spectrum analysis.
struct DsoSettingsScopeSpectrum {
unsigned channel;
double magnitude; ///< The vertical resolution in dB/div
QString name; ///< Name of this channel
double offset; ///< Vertical offset in divs
bool used; ///< true if the spectrum is turned on
double magnitude = 20.0; ///< The vertical resolution in dB/div
QString name; ///< Name of this channel
double offset = 0.0; ///< Vertical offset in divs
bool used = false; ///< true if the spectrum is turned on
};

////////////////////////////////////////////////////////////////////////////////
/// \struct DsoSettingsScopeVoltage settings.h
/// \brief Holds the settings for the normal voltage graphs.
struct DsoSettingsScopeVoltage {
double gain; ///< The vertical resolution in V/div
bool inverted; ///< true if the channel is inverted (mirrored on cross-axis)
double gain = 1.0; ///< The vertical resolution in V/div
bool inverted = false; ///< true if the channel is inverted (mirrored on cross-axis)
union { ///< Different enums, coupling for real- and mode for math-channels
Dso::MathMode math;
Dso::Coupling coupling;
int rawValue;
};
QString name; ///< Name of this channel
double offset; ///< Vertical offset in divs
double trigger; ///< Trigger level in V
bool used; ///< true if this channel is enabled
QString name; ///< Name of this channel
double offset = 0.0; ///< Vertical offset in divs
double trigger = 0.0; ///< Trigger level in V
bool used = false; ///< true if this channel is enabled
};

////////////////////////////////////////////////////////////////////////////////
Expand Down
96 changes: 31 additions & 65 deletions openhantek/src/settings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,79 +34,33 @@
/// \brief Set the number of channels.
/// \param channels The new channel count, that will be applied to lists.
DsoSettings::DsoSettings(unsigned int channels) {
setChannelCount(channels);
load();
}

bool DsoSettings::setFilename(const QString &filename) {
std::unique_ptr<QSettings> local = std::unique_ptr<QSettings>(new QSettings(filename, QSettings::IniFormat));
if (local->status() != QSettings::NoError) {
qWarning() << "Could not change the settings file to " << filename;
return false;
}
store.swap(local);
return true;
}

template<typename S>
inline void removeIfPossible(std::vector<S>& v, unsigned start, unsigned keepAtEnd) {
if (start<v.size())
v.erase(v.begin()+start, v.end()-((v.size()-start>=keepAtEnd) ? keepAtEnd : 0));
}

void DsoSettings::setChannelCount(unsigned channels) {
scope.physicalChannels = channels;

// Remove list items for removed channels, keep last channel (math channel)
removeIfPossible(scope.spectrum, channels, 1);
removeIfPossible(scope.voltage, channels, 1);
removeIfPossible(view.screen.voltage, channels, 1);
removeIfPossible(view.print.voltage, channels, 1);
DsoSettingsScopeSpectrum newSpectrum;
newSpectrum.name = QApplication::tr("SPM");
scope.spectrum.push_back(newSpectrum);

// Add math channel if missing
// Check if the math channel is missing
if (!scope.spectrum.size()) {
DsoSettingsScopeSpectrum newSpectrum;
newSpectrum.magnitude = 20.0;
newSpectrum.name = QApplication::tr("SPM");
newSpectrum.offset = 0.0;
newSpectrum.used = false;
scope.spectrum.push_back(newSpectrum);

DsoSettingsScopeVoltage newVoltage;
newVoltage.gain = 1.0;
newVoltage.math = Dso::MathMode::ADD_CH1_CH2;
newVoltage.name = QApplication::tr("MATH");
newVoltage.offset = 0.0;
newVoltage.trigger = 0.0;
newVoltage.used = false;
scope.voltage.push_back(newVoltage);
DsoSettingsScopeVoltage newVoltage;
newVoltage.math = Dso::MathMode::ADD_CH1_CH2;
newVoltage.name = QApplication::tr("MATH");
scope.voltage.push_back(newVoltage);

view.screen.voltage.push_back(QColor(0x7f, 0x7f, 0x7f, 0xff));
view.screen.spectrum.push_back(view.screen.voltage.back().lighter());
view.print.voltage.push_back(view.screen.voltage.back());
view.print.spectrum.push_back(view.print.voltage.back().darker());
}
view.screen.voltage.push_back(QColor(0x7f, 0x7f, 0x7f, 0xff));
view.screen.spectrum.push_back(view.screen.voltage.back().lighter());
view.print.voltage.push_back(view.screen.voltage.back());
view.print.spectrum.push_back(view.print.voltage.back().darker());

// Add new channels to the list
while (scope.spectrum.size() <= channels) {
// Oscilloscope settings
// Spectrum
DsoSettingsScopeSpectrum newSpectrum;
newSpectrum.magnitude = 20.0;
newSpectrum.name = QApplication::tr("SP%1").arg(scope.spectrum.size());
newSpectrum.offset = 0.0;
newSpectrum.used = false;
scope.spectrum.insert(scope.spectrum.end()-1, newSpectrum);

// Voltage
DsoSettingsScopeVoltage newVoltage;
newVoltage.gain = 1.0;
newVoltage.coupling = Dso::COUPLING_DC;
newVoltage.name = QApplication::tr("CH%1").arg(scope.voltage.size());
newVoltage.offset = 0.0;
newVoltage.trigger = 0.0;
newVoltage.used = false;
scope.voltage.insert(scope.voltage.end()-1, newVoltage);

view.screen.voltage.insert(view.screen.voltage.end()-1, QColor::fromHsv((int)(view.screen.voltage.size()-1) * 60, 0xff, 0xff));
Expand All @@ -115,7 +69,17 @@ void DsoSettings::setChannelCount(unsigned channels) {
view.print.spectrum.insert(view.print.spectrum.end()-1, view.screen.voltage.back().darker());
}

load();
}

bool DsoSettings::setFilename(const QString &filename) {
std::unique_ptr<QSettings> local = std::unique_ptr<QSettings>(new QSettings(filename, QSettings::IniFormat));
if (local->status() != QSettings::NoError) {
qWarning() << "Could not change the settings file to " << filename;
return false;
}
store.swap(local);
return true;
}

void DsoSettings::load() {
Expand Down Expand Up @@ -166,6 +130,7 @@ void DsoSettings::load() {
for (unsigned channel = 0; channel < scope.voltage.size(); ++channel) {
store->beginGroup(QString("vertical%1").arg(channel));
if (store->contains("gain")) scope.voltage[channel].gain = store->value("gain").toDouble();
if (store->contains("inverted")) scope.voltage[channel].inverted = store->value("inverted").toBool();
if (store->contains("misc")) scope.voltage[channel].rawValue = store->value("misc").toInt();
if (store->contains("offset")) scope.voltage[channel].offset = store->value("offset").toDouble();
if (store->contains("trigger")) scope.voltage[channel].trigger = store->value("trigger").toDouble();
Expand Down Expand Up @@ -265,6 +230,7 @@ void DsoSettings::save() {
for (unsigned channel = 0; channel < scope.voltage.size(); ++channel) {
store->beginGroup(QString("vertical%1").arg(channel));
store->setValue("gain", scope.voltage[channel].gain);
store->setValue("inverted", scope.voltage[channel].inverted);
store->setValue("misc", scope.voltage[channel].rawValue);
store->setValue("offset", scope.voltage[channel].offset);
store->setValue("trigger", scope.voltage[channel].trigger);
Expand All @@ -291,16 +257,16 @@ void DsoSettings::save() {
store->beginGroup("print");
}

store->setValue("axes", colors->axes);
store->setValue("background", colors->background);
store->setValue("border", colors->border);
store->setValue("grid", colors->grid);
store->setValue("markers", colors->markers);
store->setValue("axes", colors->axes.name(QColor::HexArgb));
store->setValue("background", colors->background.name(QColor::HexArgb));
store->setValue("border", colors->border.name(QColor::HexArgb));
store->setValue("grid", colors->grid.name(QColor::HexArgb));
store->setValue("markers", colors->markers.name(QColor::HexArgb));
for (unsigned channel = 0; channel < scope.spectrum.size(); ++channel)
store->setValue(QString("spectrum%1").arg(channel), colors->spectrum[channel]);
store->setValue("text", colors->text);
store->setValue(QString("spectrum%1").arg(channel), colors->spectrum[channel].name(QColor::HexArgb));
store->setValue("text", colors->text.name(QColor::HexArgb));
for (unsigned channel = 0; channel < scope.voltage.size(); ++channel)
store->setValue(QString("voltage%1").arg(channel), colors->voltage[channel]);
store->setValue(QString("voltage%1").arg(channel), colors->voltage[channel].name(QColor::HexArgb));
store->endGroup();
}
store->endGroup();
Expand Down
2 changes: 0 additions & 2 deletions openhantek/src/settings.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@ class DsoSettings {
explicit DsoSettings(unsigned int channels);
bool setFilename(const QString &filename);

void setChannelCount(unsigned int channels);

DsoSettingsOptions options; ///< General options of the program
DsoSettingsScope scope; ///< All oscilloscope related settings
DsoSettingsView view; ///< All view related settings
Expand Down
4 changes: 2 additions & 2 deletions openhantek/src/widgets/levelslider.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@ LevelSlider::LevelSlider(Qt::ArrowType direction, QWidget *parent) : QWidget(par
this->setFont(font);

this->pressedSlider = -1;
this->sliderWidth = 12;

this->setDirection(direction);
calculateWidth();
setDirection(direction);
}

/// \brief Cleans up the widget.
Expand Down

0 comments on commit 3ccfb5b

Please sign in to comment.