Fix incorrect sensor selection due to reorder sensors in SensorListWidget

This commit is contained in:
Carl Philipp Klemm 2025-10-05 23:29:23 +02:00
parent 271330d5fd
commit 0c5603ca44
7 changed files with 47 additions and 15 deletions

View file

@ -127,10 +127,15 @@ std::shared_ptr<Relay> Microcontroller::processRelayLine(const QString& buffer)
if(bufferList.size() >= 9 && buffer.startsWith("ITEM NUMBER:")) if(bufferList.size() >= 9 && buffer.startsWith("ITEM NUMBER:"))
{ {
QString name; QString name;
for(int i = 10; i < bufferList.size(); i++) name.append(bufferList[i] + ' '); for(int i = 10; i < bufferList.size(); i++)
if(name.size() > 1)name.remove(name.size()-1, 1); name.append(bufferList[i] + ' ');
else name = "Relay " + QString::number(bufferList[1].toInt(nullptr, 2)); if(name.size() > 1)
return std::shared_ptr<Relay>( new Relay(bufferList[2].toInt(), name, bufferList[4].toInt(nullptr, 2), name.remove(name.size()-1, 1);
else
name = "Relay " + QString::number(bufferList[1].toInt(nullptr, 2));
return std::shared_ptr<Relay>(new Relay(bufferList[2].toInt(),
name,
bufferList[6].toInt(nullptr, 2),
bufferList[8].toInt())); bufferList[8].toInt()));
} }
return nullptr; return nullptr;

View file

@ -46,5 +46,5 @@ void PolynomalActorWidget::setPow()
void PolynomalActorWidget::setSensor(const QModelIndex &index) void PolynomalActorWidget::setSensor(const QModelIndex &index)
{ {
actor_->setSensor(sensors_->getSensors()->at(index.row())); actor_->setSensor(ui->listView->getSensorForIndex(index));
} }

View file

@ -47,7 +47,7 @@ void RegulatorWdiget::setBand(double band)
void RegulatorWdiget::setSensor(const QModelIndex &index) void RegulatorWdiget::setSensor(const QModelIndex &index)
{ {
regulator_->setSensor(sensors_->getSensors()->at(index.row())); regulator_->setSensor(ui->listView->getSensorForIndex(index));
setPoint(sensors_->getSensors()->at(index.row()).field); setPoint(sensors_->getSensors()->at(index.row()).field);
ui->doubleSpinBox_setPoint->setValue(sensors_->getSensors()->at(index.row()).field); ui->doubleSpinBox_setPoint->setValue(sensors_->getSensors()->at(index.row()).field);
} }

View file

@ -17,9 +17,12 @@ SensorActorWidget::SensorActorWidget(std::shared_ptr<SensorActor> sensorActor, S
ui->label->hide(); ui->label->hide();
} }
if(sensorActor_->getSloap() == SensorActor::SLOPE_UP) ui->comboBox_slope->setCurrentIndex(0); if(sensorActor_->getSloap() == SensorActor::SLOPE_UP)
else if(sensorActor_->getSloap() == SensorActor::SLOPE_DOWN) ui->comboBox_slope->setCurrentIndex(1); ui->comboBox_slope->setCurrentIndex(0);
else if(sensorActor_->getSloap() == SensorActor::SLOPE_BOTH) ui->comboBox_slope->setCurrentIndex(2); else if(sensorActor_->getSloap() == SensorActor::SLOPE_DOWN)
ui->comboBox_slope->setCurrentIndex(1);
else if(sensorActor_->getSloap() == SensorActor::SLOPE_BOTH)
ui->comboBox_slope->setCurrentIndex(2);
ui->doubleSpinBox_threshold->setValue(sensorActor_->getThreshold()); ui->doubleSpinBox_threshold->setValue(sensorActor_->getThreshold());
@ -47,6 +50,5 @@ void SensorActorWidget::setSlope(int index)
void SensorActorWidget::setSensor(const QModelIndex &index) void SensorActorWidget::setSensor(const QModelIndex &index)
{ {
sensorActor_->setSensor(sensors_->getSensors()->at(index.row())); sensorActor_->setSensor(ui->listView->getSensorForIndex(index));
qDebug()<<"Selected "<<sensors_->getSensors()->at(index.row()).name;
} }

View file

@ -56,7 +56,7 @@ void SensorListWidget::sensorsChanged(std::vector<Sensor> sensors)
else itemString.append("\"Silent\""); else itemString.append("\"Silent\"");
} }
setItem(static_cast<int>(row), 0, new QTableWidgetItem(sensors[i].name + (sensors[i].hidden ? " (H)" : ""))); setItem(static_cast<int>(row), 0, new SensorListItem(sensors[i].name + (sensors[i].hidden ? " (H)" : ""), sensors[i]));
setItem(static_cast<int>(row), 1, new QTableWidgetItem(itemString)); setItem(static_cast<int>(row), 1, new QTableWidgetItem(itemString));
if(sensors[i].type <= 128) if(sensors[i].type <= 128)
setItem(static_cast<int>(row), 2, new QTableWidgetItem(sensors[i].lastSeen.time().toString("hh:mm"))); setItem(static_cast<int>(row), 2, new QTableWidgetItem(sensors[i].lastSeen.time().toString("hh:mm")));
@ -67,8 +67,23 @@ void SensorListWidget::sensorsChanged(std::vector<Sensor> sensors)
resizeColumnsToContents(); resizeColumnsToContents();
} }
const Sensor& SensorListWidget::getSensorForIndex(const QModelIndex &index)
{
return static_cast<SensorListItem*>(item(index.row(), 0))->getSensor();
}
void SensorListWidget::setShowHidden(const bool showHidden) void SensorListWidget::setShowHidden(const bool showHidden)
{ {
showHidden_=showHidden; showHidden_=showHidden;
} }
const Sensor& SensorListItem::getSensor()
{
return sensor;
}
SensorListItem::SensorListItem(const QString& text, const Sensor& sensor):
QTableWidgetItem(text, 1001), sensor(sensor)
{
}

View file

@ -3,6 +3,15 @@
#include <vector> #include <vector>
#include "../sensors/sensor.h" #include "../sensors/sensor.h"
class SensorListItem : public QTableWidgetItem
{
Sensor sensor;
public:
const Sensor& getSensor();
SensorListItem(const QString& text, const Sensor& sensor);
};
class SensorListWidget : public QTableWidget class SensorListWidget : public QTableWidget
{ {
Q_OBJECT Q_OBJECT
@ -15,6 +24,7 @@ public:
SensorListWidget(SensorStore& sensorStore, const bool showHidden = true, QWidget* parent = nullptr); SensorListWidget(SensorStore& sensorStore, const bool showHidden = true, QWidget* parent = nullptr);
virtual ~SensorListWidget() {} virtual ~SensorListWidget() {}
void setShowHidden(const bool showHidden); void setShowHidden(const bool showHidden);
const Sensor& getSensorForIndex(const QModelIndex &index);
public slots: public slots: