53 lines
1.5 KiB
C++
53 lines
1.5 KiB
C++
// SPDX-FileCopyrightText: 2025 Filip Leonarski, Paul Scherrer Institute <filip.leonarski@psi.ch>
|
|
// SPDX-License-Identifier: GPL-3.0-only
|
|
|
|
#include "NumericComboBox.h"
|
|
|
|
NumericComboBox::NumericComboBox(std::vector<int> options, int min, int max, QWidget *parent) {
|
|
setEditable(true);
|
|
|
|
for (const auto &i: options)
|
|
addItem(QString::number(i));
|
|
|
|
validator = new QIntValidator(this);
|
|
validator->setBottom(min);
|
|
validator->setTop(max);
|
|
setValidator(validator);
|
|
|
|
setCurrentText(QString::number(min));
|
|
|
|
setStyleSheet("background-color: rgb(255, 255, 255);");
|
|
|
|
connect(this, QOverload<int>::of(&QComboBox::currentIndexChanged),
|
|
this, &NumericComboBox::onCurrentIndexChanged);
|
|
connect(this, &QComboBox::editTextChanged,
|
|
this, &NumericComboBox::onEditTextChanged);
|
|
|
|
}
|
|
|
|
int NumericComboBox::getValue() const {
|
|
return currentText().toInt();
|
|
}
|
|
|
|
void NumericComboBox::setValue(int value) {
|
|
setCurrentText(QString::number(value));
|
|
}
|
|
|
|
void NumericComboBox::onCurrentIndexChanged(int index) {
|
|
if (index >= 0) {
|
|
setStyleSheet("background-color: rgb(255, 255, 255);");
|
|
emit valueChanged(getValue());
|
|
}
|
|
}
|
|
|
|
void NumericComboBox::onEditTextChanged(const QString &text) {
|
|
bool ok;
|
|
int value = text.toInt(&ok);
|
|
if (ok) {
|
|
setStyleSheet("background-color: rgb(255, 255, 255);");
|
|
emit valueChanged(value);
|
|
} else {
|
|
setStyleSheet("background-color: rgb(125, 0, 0);");
|
|
}
|
|
}
|