explorer.cpp Example File
sensor_explorer/import/explorer.cpp
#include "explorer.h"
#include <QtCore/QDebug>
#include <qsensor.h>
#include <QMetaObject>
#include <QMetaProperty>
QSensorExplorer::QSensorExplorer(QObject* parent)
: QObject(parent)
, _selectedSensorItem(0)
{
loadSensors();
}
QSensorExplorer::~QSensorExplorer()
{
}
void QSensorExplorer::loadSensors()
{
_availableSensors.clear();
foreach (const QByteArray &type, QSensor::sensorTypes()) {
qDebug() << "Found type" << type;
foreach (const QByteArray &identifier, QSensor::sensorsForType(type)) {
qDebug() << "Found identifier" << identifier;
QSensor* sensor = new QSensor(type, this);
sensor->setIdentifier(identifier);
if (!sensor->connectToBackend()) {
qDebug() << "Couldn't connect to" << identifier;
continue;
}
qDebug() << "Adding identifier" << identifier;
_availableSensors.append(new QSensorItem(sensor, this));
}
}
emit availableSensorsChanged();
}
QQmlListProperty<QSensorItem> QSensorExplorer::availableSensors()
{
return QQmlListProperty<QSensorItem>(this,_availableSensors);
}
QSensorItem* QSensorExplorer::selectedSensorItem()
{
return _selectedSensorItem;
}
void QSensorExplorer::setSelectedSensorItem(QSensorItem* selitem)
{
if (selitem && _selectedSensorItem != selitem) {
if (_selectedSensorItem)
_selectedSensorItem->unSelect();
_selectedSensorItem = selitem;
_selectedSensorItem->select();
emit selectedSensorItemChanged();
}
}