Commit 7713d0612f8e757513041f4740afdb321ccf4244
Committed by
David Gräff
1 parent
ef0db033
Use QSignalBlocker instead of boolean flags in HorizontalDock
Showing
2 changed files
with
33 additions
and
61 deletions
openhantek/src/docks/HorizontalDock.cpp
| ... | ... | @@ -5,6 +5,7 @@ |
| 5 | 5 | #include <QComboBox> |
| 6 | 6 | #include <QDockWidget> |
| 7 | 7 | #include <QLabel> |
| 8 | +#include <QSignalBlocker> | |
| 8 | 9 | |
| 9 | 10 | #include <cmath> |
| 10 | 11 | |
| ... | ... | @@ -81,7 +82,7 @@ HorizontalDock::HorizontalDock(DsoSettings *settings, QWidget *parent, Qt::Windo |
| 81 | 82 | this->setSamplerate(settings->scope.horizontal.samplerate); |
| 82 | 83 | this->setTimebase(settings->scope.horizontal.timebase); |
| 83 | 84 | this->setFrequencybase(settings->scope.horizontal.frequencybase); |
| 84 | - this->setRecordLength(settings->scope.horizontal.recordLength); | |
| 85 | + // this->setRecordLength(settings->scope.horizontal.recordLength); | |
| 85 | 86 | this->setFormat(settings->scope.horizontal.format); |
| 86 | 87 | } |
| 87 | 88 | |
| ... | ... | @@ -99,30 +100,27 @@ void HorizontalDock::closeEvent(QCloseEvent *event) { |
| 99 | 100 | /// \brief Changes the frequencybase. |
| 100 | 101 | /// \param frequencybase The frequencybase in hertz. |
| 101 | 102 | void HorizontalDock::setFrequencybase(double frequencybase) { |
| 102 | - this->suppressSignals = true; | |
| 103 | - this->frequencybaseSiSpinBox->setValue(frequencybase); | |
| 104 | - this->suppressSignals = false; | |
| 103 | + QSignalBlocker blocker(frequencybaseSiSpinBox); | |
| 104 | + frequencybaseSiSpinBox->setValue(frequencybase); | |
| 105 | 105 | } |
| 106 | 106 | |
| 107 | 107 | /// \brief Changes the samplerate. |
| 108 | 108 | /// \param samplerate The samplerate in seconds. |
| 109 | 109 | void HorizontalDock::setSamplerate(double samplerate) { |
| 110 | - this->suppressSignals = true; | |
| 111 | - this->samplerateSiSpinBox->setValue(samplerate); | |
| 112 | - this->suppressSignals = false; | |
| 110 | + QSignalBlocker blocker(samplerateSiSpinBox); | |
| 111 | + samplerateSiSpinBox->setValue(samplerate); | |
| 113 | 112 | } |
| 114 | 113 | |
| 115 | 114 | /// \brief Changes the timebase. |
| 116 | 115 | /// \param timebase The timebase in seconds. |
| 117 | 116 | double HorizontalDock::setTimebase(double timebase) { |
| 117 | + QSignalBlocker blocker(timebaseSiSpinBox); | |
| 118 | 118 | // timebaseSteps are repeated in each decade |
| 119 | 119 | double decade = pow(10, floor(log10(timebase))); |
| 120 | 120 | double vNorm = timebase / decade; |
| 121 | 121 | for (int i = 0; i < timebaseSteps.size() - 1; ++i) { |
| 122 | 122 | if (timebaseSteps.at(i) <= vNorm && vNorm < timebaseSteps.at(i + 1)) { |
| 123 | - suppressSignals = true; | |
| 124 | 123 | timebaseSiSpinBox->setValue(decade * timebaseSteps.at(i)); |
| 125 | - suppressSignals = false; | |
| 126 | 124 | break; |
| 127 | 125 | } |
| 128 | 126 | } |
| ... | ... | @@ -132,23 +130,23 @@ double HorizontalDock::setTimebase(double timebase) { |
| 132 | 130 | /// \brief Changes the record length if the new value is supported. |
| 133 | 131 | /// \param recordLength The record length in samples. |
| 134 | 132 | void HorizontalDock::setRecordLength(unsigned int recordLength) { |
| 135 | - int index = this->recordLengthComboBox->findData(recordLength); | |
| 133 | + QSignalBlocker blocker(recordLengthComboBox); | |
| 134 | + int index = recordLengthComboBox->findData(recordLength); | |
| 136 | 135 | |
| 137 | 136 | if (index != -1) { |
| 138 | - this->suppressSignals = true; | |
| 139 | - this->recordLengthComboBox->setCurrentIndex(index); | |
| 140 | - this->suppressSignals = false; | |
| 141 | - } | |
| 137 | + recordLengthComboBox->setCurrentIndex(index); | |
| 138 | + settings->scope.horizontal.recordLength = this->recordLengthComboBox->itemData(index).toUInt(); | |
| 139 | + } else | |
| 140 | + throw new std::runtime_error("Record length cannot be found"); | |
| 142 | 141 | } |
| 143 | 142 | |
| 144 | 143 | /// \brief Changes the format if the new value is supported. |
| 145 | 144 | /// \param format The format for the horizontal axis. |
| 146 | 145 | /// \return Index of format-value, -1 on error. |
| 147 | 146 | int HorizontalDock::setFormat(Dso::GraphFormat format) { |
| 147 | + QSignalBlocker blocker(formatComboBox); | |
| 148 | 148 | if (format >= Dso::GRAPHFORMAT_TY && format <= Dso::GRAPHFORMAT_XY) { |
| 149 | - this->suppressSignals = true; | |
| 150 | - this->formatComboBox->setCurrentIndex(format); | |
| 151 | - this->suppressSignals = false; | |
| 149 | + formatComboBox->setCurrentIndex(format); | |
| 152 | 150 | return format; |
| 153 | 151 | } |
| 154 | 152 | |
| ... | ... | @@ -157,93 +155,69 @@ int HorizontalDock::setFormat(Dso::GraphFormat format) { |
| 157 | 155 | |
| 158 | 156 | /// \brief Updates the available record lengths in the combo box. |
| 159 | 157 | /// \param recordLengths The available record lengths for the combo box. |
| 160 | -void HorizontalDock::availableRecordLengthsChanged(const QList<unsigned int> &recordLengths) { | |
| 161 | - /// \todo Empty lists should be interpreted as scope supporting continuous | |
| 162 | - /// record length values. | |
| 163 | - this->recordLengthComboBox->blockSignals(true); // Avoid messing up the settings | |
| 164 | - this->recordLengthComboBox->setUpdatesEnabled(false); | |
| 165 | - | |
| 166 | - // Update existing elements to avoid unnecessary index updates | |
| 167 | - int index = 0; | |
| 168 | - for (; index < recordLengths.size(); ++index) { | |
| 169 | - unsigned int recordLengthItem = recordLengths[index]; | |
| 170 | - if (index < this->recordLengthComboBox->count()) { | |
| 171 | - this->recordLengthComboBox->setItemData(index, recordLengthItem); | |
| 172 | - this->recordLengthComboBox->setItemText( | |
| 173 | - index, recordLengthItem == UINT_MAX ? tr("Roll") : valueToString(recordLengthItem, UNIT_SAMPLES, 3)); | |
| 174 | - } else { | |
| 175 | - this->recordLengthComboBox->addItem( | |
| 176 | - recordLengthItem == UINT_MAX ? tr("Roll") : valueToString(recordLengthItem, UNIT_SAMPLES, 3), | |
| 177 | - (uint)recordLengthItem); | |
| 178 | - } | |
| 179 | - } | |
| 180 | - // Remove extra elements | |
| 181 | - for (int extraIndex = this->recordLengthComboBox->count() - 1; extraIndex > index; --extraIndex) { | |
| 182 | - this->recordLengthComboBox->removeItem(extraIndex); | |
| 158 | +void HorizontalDock::availableRecordLengthsChanged(const std::vector<unsigned> &recordLengths) { | |
| 159 | + QSignalBlocker blocker(recordLengthComboBox); | |
| 160 | + | |
| 161 | + recordLengthComboBox->clear(); | |
| 162 | + for (auto recordLength : recordLengths) { | |
| 163 | + recordLengthComboBox->addItem( | |
| 164 | + recordLength == UINT_MAX ? tr("Roll") : valueToString(recordLength, UNIT_SAMPLES, 3), recordLength); | |
| 183 | 165 | } |
| 184 | 166 | |
| 185 | - this->setRecordLength(settings->scope.horizontal.recordLength); | |
| 186 | - this->recordLengthComboBox->setUpdatesEnabled(true); | |
| 187 | - this->recordLengthComboBox->blockSignals(false); | |
| 167 | + setRecordLength(settings->scope.horizontal.recordLength); | |
| 188 | 168 | } |
| 189 | 169 | |
| 190 | 170 | /// \brief Updates the minimum and maximum of the samplerate spin box. |
| 191 | 171 | /// \param minimum The minimum value the spin box should accept. |
| 192 | 172 | /// \param maximum The minimum value the spin box should accept. |
| 193 | 173 | void HorizontalDock::samplerateLimitsChanged(double minimum, double maximum) { |
| 194 | - this->suppressSignals = true; | |
| 174 | + QSignalBlocker blocker(samplerateSiSpinBox); | |
| 195 | 175 | this->samplerateSiSpinBox->setMinimum(minimum); |
| 196 | 176 | this->samplerateSiSpinBox->setMaximum(maximum); |
| 197 | - this->suppressSignals = false; | |
| 198 | 177 | } |
| 199 | 178 | |
| 200 | 179 | /// \brief Updates the mode and steps of the samplerate spin box. |
| 201 | 180 | /// \param mode The mode value the spin box should accept. |
| 202 | 181 | /// \param steps The steps value the spin box should accept. |
| 203 | 182 | void HorizontalDock::samplerateSet(int mode, QList<double> steps) { |
| 204 | - this->suppressSignals = true; | |
| 183 | + QSignalBlocker blocker(samplerateSiSpinBox); | |
| 205 | 184 | this->samplerateSiSpinBox->setMode(mode); |
| 206 | 185 | this->samplerateSiSpinBox->setSteps(steps); |
| 207 | - this->suppressSignals = false; | |
| 208 | 186 | } |
| 209 | 187 | |
| 210 | 188 | /// \brief Called when the frequencybase spinbox changes its value. |
| 211 | 189 | /// \param frequencybase The frequencybase in hertz. |
| 212 | 190 | void HorizontalDock::frequencybaseSelected(double frequencybase) { |
| 213 | 191 | settings->scope.horizontal.frequencybase = frequencybase; |
| 214 | - if (!this->suppressSignals) emit frequencybaseChanged(frequencybase); | |
| 192 | + emit frequencybaseChanged(frequencybase); | |
| 215 | 193 | } |
| 216 | 194 | |
| 217 | 195 | /// \brief Called when the samplerate spinbox changes its value. |
| 218 | 196 | /// \param samplerate The samplerate in samples/second. |
| 219 | 197 | void HorizontalDock::samplerateSelected(double samplerate) { |
| 220 | 198 | settings->scope.horizontal.samplerate = samplerate; |
| 221 | - if (!this->suppressSignals) { | |
| 222 | - settings->scope.horizontal.samplerateSet = true; | |
| 223 | - emit samplerateChanged(samplerate); | |
| 224 | - } | |
| 199 | + settings->scope.horizontal.samplerateSet = true; | |
| 200 | + emit samplerateChanged(samplerate); | |
| 225 | 201 | } |
| 226 | 202 | |
| 227 | 203 | /// \brief Called when the timebase spinbox changes its value. |
| 228 | 204 | /// \param timebase The timebase in seconds. |
| 229 | 205 | void HorizontalDock::timebaseSelected(double timebase) { |
| 230 | 206 | settings->scope.horizontal.timebase = timebase; |
| 231 | - if (!this->suppressSignals) { | |
| 232 | - settings->scope.horizontal.samplerateSet = false; | |
| 233 | - emit timebaseChanged(timebase); | |
| 234 | - } | |
| 207 | + settings->scope.horizontal.samplerateSet = false; | |
| 208 | + emit timebaseChanged(timebase); | |
| 235 | 209 | } |
| 236 | 210 | |
| 237 | 211 | /// \brief Called when the record length combo box changes its value. |
| 238 | 212 | /// \param index The index of the combo box item. |
| 239 | 213 | void HorizontalDock::recordLengthSelected(int index) { |
| 240 | 214 | settings->scope.horizontal.recordLength = this->recordLengthComboBox->itemData(index).toUInt(); |
| 241 | - if (!this->suppressSignals) emit recordLengthChanged(index); | |
| 215 | + emit recordLengthChanged(index); | |
| 242 | 216 | } |
| 243 | 217 | |
| 244 | 218 | /// \brief Called when the format combo box changes its value. |
| 245 | 219 | /// \param index The index of the combo box item. |
| 246 | 220 | void HorizontalDock::formatSelected(int index) { |
| 247 | 221 | settings->scope.horizontal.format = (Dso::GraphFormat)index; |
| 248 | - if (!this->suppressSignals) emit formatChanged(settings->scope.horizontal.format); | |
| 222 | + emit formatChanged(settings->scope.horizontal.format); | |
| 249 | 223 | } | ... | ... |
openhantek/src/docks/HorizontalDock.h
| ... | ... | @@ -53,10 +53,8 @@ class HorizontalDock : public QDockWidget { |
| 53 | 53 | |
| 54 | 54 | QStringList formatStrings; ///< Strings for the formats |
| 55 | 55 | |
| 56 | - bool suppressSignals; ///< Disable changed-signals temporarily | |
| 57 | - | |
| 58 | 56 | public slots: |
| 59 | - void availableRecordLengthsChanged(const QList<unsigned int> &recordLengths); | |
| 57 | + void availableRecordLengthsChanged(const std::vector<unsigned> &recordLengths); | |
| 60 | 58 | void samplerateLimitsChanged(double minimum, double maximum); |
| 61 | 59 | void samplerateSet(int mode, QList<double> sampleSteps); |
| 62 | 60 | ... | ... |