SensorExplorer.cpp Example File
sensorexplorer/src/SensorExplorer.cpp
#include "SensorExplorer.hpp"
using namespace bb::cascades;
static QString convertValue(const QString& type, const QVariant& value)
{
if (type == "LightLevel") {
switch (value.toInt()) {
case 1: return "Dark";
case 2: return "Twilight";
case 3: return "Light";
case 4: return "Bright";
case 5: return "Sunny";
default: return "Undefined";
}
} else if (type == "Orientation") {
switch (value.toInt()) {
case 1: return "TopUp";
case 2: return "TopDown";
case 3: return "LeftUp";
case 4: return "RightUp";
case 5: return "FaceUp";
case 6: return "FaceDown";
default: return "Undefined";
}
} else if (type == "qrangelist") {
const qrangelist rangeList = value.value<qrangelist>();
QStringList ranges;
foreach (const qrange &range, rangeList) {
if (range.first == range.second)
ranges << QString("%1 Hz").arg(range.first);
else
ranges << QString("%1-%2 Hz").arg(range.first).arg(range.second);
}
if (ranges.count() > 0)
return ranges.join(", ");
return "-";
} else if (type == "qoutputrangelist") {
const qoutputrangelist rangeList = value.value<qoutputrangelist>();
QStringList ranges;
foreach (const qoutputrange &range, rangeList) {
ranges << QString("(%1, %2) += %3").arg(range.minimum).arg(range.maximum).arg(range.accuracy);
}
if (ranges.count() > 0)
return ranges.join(", ");
return "-";
} else if (type == "qtimestamp") {
return QString("%1").arg(value.value<qtimestamp>());
}
return value.toString();
}
static QString typeName(const QMetaObject *metaObject, int propertyIndex)
{
const QString typeName = QLatin1String(metaObject->property(propertyIndex).typeName());
const int crap = typeName.lastIndexOf("::");
if (crap != -1)
return typeName.mid(crap + 2);
return typeName;
}
static bool ignoreProperty(const QString &propertyName)
{
if (propertyName == "sensorid" ||
propertyName == "reading" ||
propertyName == "connected" ||
propertyName == "running" ||
propertyName == "supportsPolling")
return true;
return false;
}
SensorExplorer::SensorExplorer(QObject *parent)
: QObject(parent)
, m_sensorsModel(new GroupDataModel(this))
, m_sensorPropertiesModel(new GroupDataModel(this))
, m_readingPropertiesModel(new GroupDataModel(QStringList() << QLatin1String("position"), this))
, m_sensor(0)
, m_lastReading(0)
{
m_sensorsModel->setGrouping(ItemGrouping::None);
m_sensorPropertiesModel->setGrouping(ItemGrouping::None);
m_readingPropertiesModel->setGrouping(ItemGrouping::None);
loadSensors();
}
bool SensorExplorer::filter(QSensorReading *reading)
{
if ((reading->timestamp() - m_lastReading) < 1000000)
return false;
m_lastReading = reading->timestamp();
const QMetaObject *metaObject = reading->metaObject();
const int firstProperty = QSensorReading::staticMetaObject.propertyOffset();
for (int i = firstProperty; i < metaObject->propertyCount(); ++i) {
const int position = i - firstProperty;
const QString type = typeName(metaObject, i);
const QString name = QString::fromLatin1(metaObject->property(i).name());
const QVariant value = metaObject->property(i).read(reading);
QVariantMap readingEntry;
readingEntry["position"] = position;
readingEntry["type"] = type;
readingEntry["name"] = name;
readingEntry["value"] = convertValue(type, value);
const QVariantList indexPath = m_readingPropertiesModel->find(QVariantList() << position);
m_readingPropertiesModel->updateItem(indexPath, readingEntry);
}
return false;
}
DataModel* SensorExplorer::sensorsModel() const
{
return const_cast<GroupDataModel*>(m_sensorsModel);
}
DataModel* SensorExplorer::sensorPropertiesModel() const
{
return const_cast<GroupDataModel*>(m_sensorPropertiesModel);
}
DataModel* SensorExplorer::readingPropertiesModel() const
{
return const_cast<GroupDataModel*>(m_readingPropertiesModel);
}
bool SensorExplorer::sensorActive() const
{
if (!m_sensor)
return false;
return m_sensor->isActive();
}
void SensorExplorer::setSensorActive(bool active)
{
if (!m_sensor)
return;
if (m_sensor->isActive() != active) {
m_sensor->setActive(active);
emit sensorActiveChanged();
}
}
void SensorExplorer::setCurrentSensor(const QVariantList &indexPath)
{
if (indexPath.isEmpty())
return;
if (m_sensor) {
delete m_sensor;
m_sensor = 0;
}
m_sensorPropertiesModel->clear();
m_readingPropertiesModel->clear();
const QVariantMap sensorEntry = m_sensorsModel->data(indexPath).toMap();
const QByteArray type = sensorEntry["type"].toString().toLatin1();
const QByteArray identifier = sensorEntry["identifier"].toString().toLatin1();
m_sensor = new QSensor(type, this);
connect(m_sensor, SIGNAL(readingChanged()), this, SLOT(sensorChanged()));
connect(m_sensor, SIGNAL(activeChanged()), this, SIGNAL(sensorActiveChanged()));
m_sensor->setIdentifier(identifier);
if (!m_sensor->connectToBackend()) {
delete m_sensor;
m_sensor = 0;
qWarning() << "Can't connect to the sensor!";
return;
}
loadSensorProperties();
loadReadingProperties();
}
void SensorExplorer::loadSensors()
{
m_sensorsModel->clear();
foreach (const QByteArray &type, QSensor::sensorTypes()) {
foreach (const QByteArray &identifier, QSensor::sensorsForType(type)) {
if (type == "QHolsterSensor")
continue;
QSensor sensor(type);
sensor.setIdentifier(identifier);
if (!sensor.connectToBackend()) {
qDebug() << "Couldn't connect to" << identifier;
continue;
}
QVariantMap sensorEntry;
sensorEntry["identifier"] = identifier;
sensorEntry["type"] = QString::fromLatin1(type);
m_sensorsModel->insert(sensorEntry);
}
}
}
void SensorExplorer::sensorChanged()
{
filter(m_sensor->reading());
}
void SensorExplorer::loadSensorProperties()
{
if (!m_sensor)
return;
const QMetaObject *metaObject = m_sensor->metaObject();
const int firstProperty = QSensor::staticMetaObject.propertyOffset();
for (int i = firstProperty; i < metaObject->propertyCount(); ++i) {
const QString name = QString::fromLatin1(metaObject->property(i).name());
if (ignoreProperty(name))
continue;
const QString type = typeName(metaObject, i);
const QVariant value = metaObject->property(i).read(m_sensor);
QVariantMap sensorPropertiesEntry;
sensorPropertiesEntry["type"] = type;
sensorPropertiesEntry["name"] = name;
sensorPropertiesEntry["value"] = convertValue(type, value);
m_sensorPropertiesModel->insert(sensorPropertiesEntry);
}
}
void SensorExplorer::loadReadingProperties()
{
const QSensorReading *reading = m_sensor->reading();
const QMetaObject *metaObject = reading->metaObject();
const int firstProperty = QSensorReading::staticMetaObject.propertyOffset();
for (int i = firstProperty; i < metaObject->propertyCount(); ++i) {
const int position = i - firstProperty;
const QString type = typeName(metaObject, i);
const QString name = QString::fromLatin1(metaObject->property(i).name());
const QVariant value = metaObject->property(i).read(reading);
QVariantMap readingEntry;
readingEntry["position"] = position;
readingEntry["type"] = type;
readingEntry["name"] = name;
readingEntry["value"] = convertValue(type, value);
m_readingPropertiesModel->insert(readingEntry);
}
}