|
| 1 | +// SPDX-License-Identifier: GPL-2.0+ |
| 2 | + |
| 3 | +#include <QCheckBox> |
| 4 | +#include <QCloseEvent> |
| 5 | +#include <QComboBox> |
| 6 | +#include <QDockWidget> |
| 7 | +#include <QLabel> |
| 8 | + |
| 9 | +#include <cmath> |
| 10 | + |
| 11 | +#include "dockwindows.h" |
| 12 | +#include "HorizontalDock.h" |
| 13 | + |
| 14 | +#include "utils/printutils.h" |
| 15 | +#include "utils/dsoStrings.h" |
| 16 | +#include "settings.h" |
| 17 | +#include "sispinbox.h" |
| 18 | + |
| 19 | +//////////////////////////////////////////////////////////////////////////////// |
| 20 | +// class HorizontalDock |
| 21 | +/// \brief Initializes the horizontal axis docking window. |
| 22 | +/// \param settings The target settings object. |
| 23 | +/// \param parent The parent widget. |
| 24 | +/// \param flags Flags for the window manager. |
| 25 | +HorizontalDock::HorizontalDock(DsoSettings *settings, QWidget *parent, |
| 26 | + Qt::WindowFlags flags) |
| 27 | + : QDockWidget(tr("Horizontal"), parent, flags) { |
| 28 | + this->settings = settings; |
| 29 | + |
| 30 | + // Initialize elements |
| 31 | + this->samplerateLabel = new QLabel(tr("Samplerate")); |
| 32 | + this->samplerateSiSpinBox = new SiSpinBox(UNIT_SAMPLES); |
| 33 | + this->samplerateSiSpinBox->setMinimum(1); |
| 34 | + this->samplerateSiSpinBox->setMaximum(1e8); |
| 35 | + this->samplerateSiSpinBox->setUnitPostfix("/s"); |
| 36 | + |
| 37 | + timebaseSteps << 1.0 << 2.0 << 4.0 << 10.0; |
| 38 | + |
| 39 | + this->timebaseLabel = new QLabel(tr("Timebase")); |
| 40 | + this->timebaseSiSpinBox = new SiSpinBox(UNIT_SECONDS); |
| 41 | + this->timebaseSiSpinBox->setSteps(timebaseSteps); |
| 42 | + this->timebaseSiSpinBox->setMinimum(1e-9); |
| 43 | + this->timebaseSiSpinBox->setMaximum(3.6e3); |
| 44 | + |
| 45 | + this->frequencybaseLabel = new QLabel(tr("Frequencybase")); |
| 46 | + this->frequencybaseSiSpinBox = new SiSpinBox(UNIT_HERTZ); |
| 47 | + this->frequencybaseSiSpinBox->setMinimum(1.0); |
| 48 | + this->frequencybaseSiSpinBox->setMaximum(100e6); |
| 49 | + |
| 50 | + this->recordLengthLabel = new QLabel(tr("Record length")); |
| 51 | + this->recordLengthComboBox = new QComboBox(); |
| 52 | + |
| 53 | + this->formatLabel = new QLabel(tr("Format")); |
| 54 | + this->formatComboBox = new QComboBox(); |
| 55 | + for (int format = Dso::GRAPHFORMAT_TY; format < Dso::GRAPHFORMAT_COUNT; |
| 56 | + ++format) |
| 57 | + this->formatComboBox->addItem( |
| 58 | + Dso::graphFormatString((Dso::GraphFormat)format)); |
| 59 | + |
| 60 | + this->dockLayout = new QGridLayout(); |
| 61 | + this->dockLayout->setColumnMinimumWidth(0, 64); |
| 62 | + this->dockLayout->setColumnStretch(1, 1); |
| 63 | + this->dockLayout->addWidget(this->samplerateLabel, 0, 0); |
| 64 | + this->dockLayout->addWidget(this->samplerateSiSpinBox, 0, 1); |
| 65 | + this->dockLayout->addWidget(this->timebaseLabel, 1, 0); |
| 66 | + this->dockLayout->addWidget(this->timebaseSiSpinBox, 1, 1); |
| 67 | + this->dockLayout->addWidget(this->frequencybaseLabel, 2, 0); |
| 68 | + this->dockLayout->addWidget(this->frequencybaseSiSpinBox, 2, 1); |
| 69 | + this->dockLayout->addWidget(this->recordLengthLabel, 3, 0); |
| 70 | + this->dockLayout->addWidget(this->recordLengthComboBox, 3, 1); |
| 71 | + this->dockLayout->addWidget(this->formatLabel, 4, 0); |
| 72 | + this->dockLayout->addWidget(this->formatComboBox, 4, 1); |
| 73 | + |
| 74 | + this->dockWidget = new QWidget(); |
| 75 | + SetupDockWidget(this, dockWidget, dockLayout); |
| 76 | + |
| 77 | + // Connect signals and slots |
| 78 | + connect(this->samplerateSiSpinBox, SIGNAL(valueChanged(double)), this, |
| 79 | + SLOT(samplerateSelected(double))); |
| 80 | + connect(this->timebaseSiSpinBox, SIGNAL(valueChanged(double)), this, |
| 81 | + SLOT(timebaseSelected(double))); |
| 82 | + connect(this->frequencybaseSiSpinBox, SIGNAL(valueChanged(double)), this, |
| 83 | + SLOT(frequencybaseSelected(double))); |
| 84 | + connect(this->recordLengthComboBox, SIGNAL(currentIndexChanged(int)), this, |
| 85 | + SLOT(recordLengthSelected(int))); |
| 86 | + connect(this->formatComboBox, SIGNAL(currentIndexChanged(int)), this, |
| 87 | + SLOT(formatSelected(int))); |
| 88 | + |
| 89 | + // Set values |
| 90 | + this->setSamplerate(this->settings->scope.horizontal.samplerate); |
| 91 | + this->setTimebase(this->settings->scope.horizontal.timebase); |
| 92 | + this->setFrequencybase(this->settings->scope.horizontal.frequencybase); |
| 93 | + this->setRecordLength(this->settings->scope.horizontal.recordLength); |
| 94 | + this->setFormat(this->settings->scope.horizontal.format); |
| 95 | +} |
| 96 | + |
| 97 | +/// \brief Cleans up everything. |
| 98 | +HorizontalDock::~HorizontalDock() {} |
| 99 | + |
| 100 | +/// \brief Don't close the dock, just hide it. |
| 101 | +/// \param event The close event that should be handled. |
| 102 | +void HorizontalDock::closeEvent(QCloseEvent *event) { |
| 103 | + this->hide(); |
| 104 | + |
| 105 | + event->accept(); |
| 106 | +} |
| 107 | + |
| 108 | +/// \brief Changes the frequencybase. |
| 109 | +/// \param frequencybase The frequencybase in hertz. |
| 110 | +void HorizontalDock::setFrequencybase(double frequencybase) { |
| 111 | + this->suppressSignals = true; |
| 112 | + this->frequencybaseSiSpinBox->setValue(frequencybase); |
| 113 | + this->suppressSignals = false; |
| 114 | +} |
| 115 | + |
| 116 | +/// \brief Changes the samplerate. |
| 117 | +/// \param samplerate The samplerate in seconds. |
| 118 | +void HorizontalDock::setSamplerate(double samplerate) { |
| 119 | + this->suppressSignals = true; |
| 120 | + this->samplerateSiSpinBox->setValue(samplerate); |
| 121 | + this->suppressSignals = false; |
| 122 | +} |
| 123 | + |
| 124 | +/// \brief Changes the timebase. |
| 125 | +/// \param timebase The timebase in seconds. |
| 126 | +double HorizontalDock::setTimebase(double timebase) { |
| 127 | + // timebaseSteps are repeated in each decade |
| 128 | + double decade = pow(10, floor(log10(timebase))); |
| 129 | + double vNorm = timebase / decade; |
| 130 | + for (int i = 0; i < timebaseSteps.size() - 1; ++i) { |
| 131 | + if (timebaseSteps.at(i) <= vNorm && vNorm < timebaseSteps.at(i + 1)) { |
| 132 | + suppressSignals = true; |
| 133 | + timebaseSiSpinBox->setValue(decade * timebaseSteps.at(i)); |
| 134 | + suppressSignals = false; |
| 135 | + break; |
| 136 | + } |
| 137 | + } |
| 138 | + return timebaseSiSpinBox->value(); |
| 139 | +} |
| 140 | + |
| 141 | +/// \brief Changes the record length if the new value is supported. |
| 142 | +/// \param recordLength The record length in samples. |
| 143 | +void HorizontalDock::setRecordLength(unsigned int recordLength) { |
| 144 | + int index = this->recordLengthComboBox->findData(recordLength); |
| 145 | + |
| 146 | + if (index != -1) { |
| 147 | + this->suppressSignals = true; |
| 148 | + this->recordLengthComboBox->setCurrentIndex(index); |
| 149 | + this->suppressSignals = false; |
| 150 | + } |
| 151 | +} |
| 152 | + |
| 153 | +/// \brief Changes the format if the new value is supported. |
| 154 | +/// \param format The format for the horizontal axis. |
| 155 | +/// \return Index of format-value, -1 on error. |
| 156 | +int HorizontalDock::setFormat(Dso::GraphFormat format) { |
| 157 | + if (format >= Dso::GRAPHFORMAT_TY && format <= Dso::GRAPHFORMAT_XY) { |
| 158 | + this->suppressSignals = true; |
| 159 | + this->formatComboBox->setCurrentIndex(format); |
| 160 | + this->suppressSignals = false; |
| 161 | + return format; |
| 162 | + } |
| 163 | + |
| 164 | + return -1; |
| 165 | +} |
| 166 | + |
| 167 | +/// \brief Updates the available record lengths in the combo box. |
| 168 | +/// \param recordLengths The available record lengths for the combo box. |
| 169 | +void HorizontalDock::availableRecordLengthsChanged( |
| 170 | + const QList<unsigned int> &recordLengths) { |
| 171 | + /// \todo Empty lists should be interpreted as scope supporting continuous |
| 172 | + /// record length values. |
| 173 | + this->recordLengthComboBox->blockSignals( |
| 174 | + true); // Avoid messing up the settings |
| 175 | + this->recordLengthComboBox->setUpdatesEnabled(false); |
| 176 | + |
| 177 | + // Update existing elements to avoid unnecessary index updates |
| 178 | + int index = 0; |
| 179 | + for (; index < recordLengths.size(); ++index) { |
| 180 | + unsigned int recordLengthItem = recordLengths[index]; |
| 181 | + if (index < this->recordLengthComboBox->count()) { |
| 182 | + this->recordLengthComboBox->setItemData(index, recordLengthItem); |
| 183 | + this->recordLengthComboBox->setItemText( |
| 184 | + index, recordLengthItem == UINT_MAX |
| 185 | + ? tr("Roll") |
| 186 | + : valueToString(recordLengthItem, |
| 187 | + UNIT_SAMPLES, 3)); |
| 188 | + } else { |
| 189 | + this->recordLengthComboBox->addItem( |
| 190 | + recordLengthItem == UINT_MAX |
| 191 | + ? tr("Roll") |
| 192 | + : valueToString(recordLengthItem, UNIT_SAMPLES, |
| 193 | + 3), |
| 194 | + (uint)recordLengthItem); |
| 195 | + } |
| 196 | + } |
| 197 | + // Remove extra elements |
| 198 | + for (int extraIndex = this->recordLengthComboBox->count() - 1; |
| 199 | + extraIndex > index; --extraIndex) { |
| 200 | + this->recordLengthComboBox->removeItem(extraIndex); |
| 201 | + } |
| 202 | + |
| 203 | + this->setRecordLength(this->settings->scope.horizontal.recordLength); |
| 204 | + this->recordLengthComboBox->setUpdatesEnabled(true); |
| 205 | + this->recordLengthComboBox->blockSignals(false); |
| 206 | +} |
| 207 | + |
| 208 | +/// \brief Updates the minimum and maximum of the samplerate spin box. |
| 209 | +/// \param minimum The minimum value the spin box should accept. |
| 210 | +/// \param maximum The minimum value the spin box should accept. |
| 211 | +void HorizontalDock::samplerateLimitsChanged(double minimum, double maximum) { |
| 212 | + this->suppressSignals = true; |
| 213 | + this->samplerateSiSpinBox->setMinimum(minimum); |
| 214 | + this->samplerateSiSpinBox->setMaximum(maximum); |
| 215 | + this->suppressSignals = false; |
| 216 | +} |
| 217 | + |
| 218 | +/// \brief Updates the mode and steps of the samplerate spin box. |
| 219 | +/// \param mode The mode value the spin box should accept. |
| 220 | +/// \param steps The steps value the spin box should accept. |
| 221 | +void HorizontalDock::samplerateSet(int mode, QList<double> steps) { |
| 222 | + this->suppressSignals = true; |
| 223 | + this->samplerateSiSpinBox->setMode(mode); |
| 224 | + this->samplerateSiSpinBox->setSteps(steps); |
| 225 | + this->suppressSignals = false; |
| 226 | +} |
| 227 | + |
| 228 | +/// \brief Called when the frequencybase spinbox changes its value. |
| 229 | +/// \param frequencybase The frequencybase in hertz. |
| 230 | +void HorizontalDock::frequencybaseSelected(double frequencybase) { |
| 231 | + this->settings->scope.horizontal.frequencybase = frequencybase; |
| 232 | + if (!this->suppressSignals) |
| 233 | + emit frequencybaseChanged(frequencybase); |
| 234 | +} |
| 235 | + |
| 236 | +/// \brief Called when the samplerate spinbox changes its value. |
| 237 | +/// \param samplerate The samplerate in samples/second. |
| 238 | +void HorizontalDock::samplerateSelected(double samplerate) { |
| 239 | + this->settings->scope.horizontal.samplerate = samplerate; |
| 240 | + if (!this->suppressSignals) { |
| 241 | + this->settings->scope.horizontal.samplerateSet = true; |
| 242 | + emit samplerateChanged(samplerate); |
| 243 | + } |
| 244 | +} |
| 245 | + |
| 246 | +/// \brief Called when the timebase spinbox changes its value. |
| 247 | +/// \param timebase The timebase in seconds. |
| 248 | +void HorizontalDock::timebaseSelected(double timebase) { |
| 249 | + this->settings->scope.horizontal.timebase = timebase; |
| 250 | + if (!this->suppressSignals) { |
| 251 | + this->settings->scope.horizontal.samplerateSet = false; |
| 252 | + emit timebaseChanged(timebase); |
| 253 | + } |
| 254 | +} |
| 255 | + |
| 256 | +/// \brief Called when the record length combo box changes its value. |
| 257 | +/// \param index The index of the combo box item. |
| 258 | +void HorizontalDock::recordLengthSelected(int index) { |
| 259 | + this->settings->scope.horizontal.recordLength = |
| 260 | + this->recordLengthComboBox->itemData(index).toUInt(); |
| 261 | + if (!this->suppressSignals) |
| 262 | + emit recordLengthChanged(index); |
| 263 | +} |
| 264 | + |
| 265 | +/// \brief Called when the format combo box changes its value. |
| 266 | +/// \param index The index of the combo box item. |
| 267 | +void HorizontalDock::formatSelected(int index) { |
| 268 | + this->settings->scope.horizontal.format = (Dso::GraphFormat)index; |
| 269 | + if (!this->suppressSignals) |
| 270 | + emit formatChanged(this->settings->scope.horizontal.format); |
| 271 | +} |
0 commit comments