94 lines
2.7 KiB
C++
94 lines
2.7 KiB
C++
#include "sensorlistwidget.h"
|
|
|
|
#include <QDebug>
|
|
#include <QHeaderView>
|
|
#include <QScroller>
|
|
|
|
SensorListWidget::SensorListWidget(const bool showHidden, QWidget *parent): QTableWidget(parent),
|
|
showHidden_(showHidden)
|
|
{
|
|
setColumnCount(3);
|
|
setSelectionBehavior(QAbstractItemView::SelectRows);
|
|
horizontalHeader()->setSectionResizeMode(0, QHeaderView::Stretch);
|
|
QScroller::grabGesture(this, QScroller::LeftMouseButtonGesture);
|
|
setAutoScroll(true);
|
|
setVerticalScrollMode(QAbstractItemView::ScrollPerPixel);
|
|
sensorsChanged(std::vector<Sensor>());
|
|
verticalHeader()->hide();
|
|
}
|
|
|
|
SensorListWidget::SensorListWidget(SensorStore& sensorStore, const bool showHidden,
|
|
QWidget* parent): QTableWidget (parent), showHidden_(showHidden)
|
|
{
|
|
sensorsChanged(*(sensorStore.getSensors()));
|
|
}
|
|
|
|
void SensorListWidget::sensorsChanged(std::vector<Sensor> sensors)
|
|
{
|
|
clear();
|
|
setHorizontalHeaderItem(0, new QTableWidgetItem("Sensor"));
|
|
setHorizontalHeaderItem(1, new QTableWidgetItem("Value"));
|
|
setHorizontalHeaderItem(2, new QTableWidgetItem("Time"));
|
|
size_t listLen = 0;
|
|
for(size_t i = 0; i < sensors.size(); ++i)
|
|
if(showHidden_ || !sensors[i].hidden)
|
|
++listLen;
|
|
setRowCount(static_cast<int>(listLen));
|
|
size_t row = 0;
|
|
for(size_t i = 0; i < sensors.size(); ++i)
|
|
{
|
|
if(showHidden_ || !sensors[i].hidden)
|
|
{
|
|
QString itemString;
|
|
itemString.append(QString::number(sensors[i].field));
|
|
itemString.append(' ');
|
|
|
|
if(sensors[i].type == Sensor::TYPE_DOOR)
|
|
{
|
|
if(static_cast<bool>(sensors[i].field))
|
|
itemString.append("\"Open\"");
|
|
else itemString.append("\"Closed\"");
|
|
}
|
|
else if(sensors[i].type == Sensor::TYPE_AUDIO_OUTPUT)
|
|
{
|
|
if(static_cast<bool>(sensors[i].field))
|
|
itemString.append("\"Playing\"");
|
|
else itemString.append("\"Silent\"");
|
|
}
|
|
else if(!sensors[i].getUnit().isEmpty())
|
|
{
|
|
itemString.append(" ");
|
|
itemString.append(sensors[i].getUnit());
|
|
}
|
|
|
|
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")));
|
|
++row;
|
|
}
|
|
}
|
|
sortItems(0, Qt::AscendingOrder);
|
|
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)
|
|
{
|
|
}
|
|
|