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

@ -4,7 +4,7 @@
#
#-------------------------------------------------
QT += core gui widgets network multimedia
QT += core gui widgets network multimedia
QT += serialport

View file

@ -127,11 +127,16 @@ std::shared_ptr<Relay> Microcontroller::processRelayLine(const QString& buffer)
if(bufferList.size() >= 9 && buffer.startsWith("ITEM NUMBER:"))
{
QString name;
for(int i = 10; i < bufferList.size(); i++) name.append(bufferList[i] + ' ');
if(name.size() > 1)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[4].toInt(nullptr, 2),
bufferList[8].toInt()));
for(int i = 10; i < bufferList.size(); i++)
name.append(bufferList[i] + ' ');
if(name.size() > 1)
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()));
}
return nullptr;
}

View file

@ -46,5 +46,5 @@ void PolynomalActorWidget::setPow()
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)
{
regulator_->setSensor(sensors_->getSensors()->at(index.row()));
regulator_->setSensor(ui->listView->getSensorForIndex(index));
setPoint(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();
}
if(sensorActor_->getSloap() == SensorActor::SLOPE_UP) ui->comboBox_slope->setCurrentIndex(0);
else if(sensorActor_->getSloap() == SensorActor::SLOPE_DOWN) ui->comboBox_slope->setCurrentIndex(1);
else if(sensorActor_->getSloap() == SensorActor::SLOPE_BOTH) ui->comboBox_slope->setCurrentIndex(2);
if(sensorActor_->getSloap() == SensorActor::SLOPE_UP)
ui->comboBox_slope->setCurrentIndex(0);
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());
@ -47,6 +50,5 @@ void SensorActorWidget::setSlope(int index)
void SensorActorWidget::setSensor(const QModelIndex &index)
{
sensorActor_->setSensor(sensors_->getSensors()->at(index.row()));
qDebug()<<"Selected "<<sensors_->getSensors()->at(index.row()).name;
sensorActor_->setSensor(ui->listView->getSensorForIndex(index));
}

View file

@ -56,7 +56,7 @@ void SensorListWidget::sensorsChanged(std::vector<Sensor> sensors)
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));
if(sensors[i].type <= 128)
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();
}
const Sensor& SensorListWidget::getSensorForIndex(const QModelIndex &index)
{
return static_cast<SensorListItem*>(item(index.row(), 0))->getSensor();
}
void SensorListWidget::setShowHidden(const bool 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 "../sensors/sensor.h"
class SensorListItem : public QTableWidgetItem
{
Sensor sensor;
public:
const Sensor& getSensor();
SensorListItem(const QString& text, const Sensor& sensor);
};
class SensorListWidget : public QTableWidget
{
Q_OBJECT
@ -11,10 +20,11 @@ class SensorListWidget : public QTableWidget
public:
SensorListWidget(const bool showHidden = true, QWidget *parent = nullptr);
SensorListWidget(const bool showHidden = true, QWidget* parent = nullptr);
SensorListWidget(SensorStore& sensorStore, const bool showHidden = true, QWidget* parent = nullptr);
virtual ~SensorListWidget() {}
void setShowHidden(const bool showHidden);
const Sensor& getSensorForIndex(const QModelIndex &index);
public slots: