Newer
Older
Michael Carpenter
committed
#include "UASQuickView.h"
#include <QMetaMethod>
#include <QDebug>
UASQuickView::UASQuickView(QWidget *parent) :
QWidget(parent),
m_ui(new Ui::UASQuickView)
Michael Carpenter
committed
{
m_ui->setupUi(this);
Michael Carpenter
committed
connect(UASManager::instance(),SIGNAL(activeUASSet(UASInterface*)),this,SLOT(setActiveUAS(UASInterface*)));
connect(UASManager::instance(),SIGNAL(UASCreated(UASInterface*)),this,SLOT(addUAS(UASInterface*)));
if (UASManager::instance()->getActiveUAS())
{
addUAS(UASManager::instance()->getActiveUAS());
}
this->setContextMenuPolicy(Qt::ActionsContextMenu);
addDefaultActions();
Michael Carpenter
committed
updateTimer = new QTimer(this);
connect(updateTimer,SIGNAL(timeout()),this,SLOT(updateTimerTick()));
}
void UASQuickView::addDefaultActions()
{
Michael Carpenter
committed
{
QAction *action = new QAction(tr("latitude"),this);
Michael Carpenter
committed
action->setCheckable(true);
action->setChecked(true);
connect(action,SIGNAL(toggled(bool)),this,SLOT(actionTriggered(bool)));
this->addAction(action);
UASQuickViewItem *item = new UASQuickViewItem(this);
item->setTitle(tr("latitude"));
this->layout()->addWidget(item);
Michael Carpenter
committed
uasPropertyToLabelMap["latitude"] = item;
}
{
QAction *action = new QAction(tr("longitude"),this);
Michael Carpenter
committed
action->setCheckable(true);
action->setChecked(true);
connect(action,SIGNAL(toggled(bool)),this,SLOT(actionTriggered(bool)));
this->addAction(action);
UASQuickViewItem *item = new UASQuickViewItem(this);
item->setTitle(tr("longitude"));
this->layout()->addWidget(item);
Michael Carpenter
committed
uasPropertyToLabelMap["longitude"] = item;
}
{
QAction *action = new QAction(tr("altitude"),this);
Michael Carpenter
committed
action->setCheckable(true);
action->setChecked(true);
connect(action,SIGNAL(toggled(bool)),this,SLOT(actionTriggered(bool)));
this->addAction(action);
UASQuickViewItem *item = new UASQuickViewItem(this);
item->setTitle(tr("altitude"));
this->layout()->addWidget(item);
Michael Carpenter
committed
uasPropertyToLabelMap["altitude"] = item;
}
Michael Carpenter
committed
{
QAction *action = new QAction(tr("satelliteCount"),this);
Michael Carpenter
committed
action->setCheckable(true);
action->setChecked(true);
connect(action,SIGNAL(toggled(bool)),this,SLOT(actionTriggered(bool)));
this->addAction(action);
UASQuickViewItem *item = new UASQuickViewItem(this);
item->setTitle(tr("satelliteCount"));
this->layout()->addWidget(item);
Michael Carpenter
committed
uasPropertyToLabelMap["satelliteCount"] = item;
}
Michael Carpenter
committed
{
QAction *action = new QAction(tr("distToWaypoint"),this);
Michael Carpenter
committed
action->setCheckable(true);
action->setChecked(true);
connect(action,SIGNAL(toggled(bool)),this,SLOT(actionTriggered(bool)));
this->addAction(action);
UASQuickViewItem *item = new UASQuickViewItem(this);
item->setTitle(tr("distToWaypoint"));
this->layout()->addWidget(item);
Michael Carpenter
committed
uasPropertyToLabelMap["distToWaypoint"] = item;
}
Michael Carpenter
committed
}
Michael Carpenter
committed
void UASQuickView::updateTimerTick()
{
for (int i=0;i<uasPropertyList.size();i++)
{
if (uasPropertyValueMap.contains(uasPropertyList[i]) && uasPropertyToLabelMap.contains(uasPropertyList[i]))
{
uasPropertyToLabelMap[uasPropertyList[i]]->setValue(uasPropertyValueMap.value(uasPropertyList[i],0));
}
}
}
void UASQuickView::addUAS(UASInterface* uas)
{
if (uas)
{
if (!this->uas)
{
setActiveUAS(uas);
}
}
}
void UASQuickView::setActiveUAS(UASInterface* uas)
{
Bryant
committed
// Clean up from the old UAS
if (this->uas)
Michael Carpenter
committed
{
Bryant
committed
uasPropertyList.clear();
uasPropertyValueMap.clear();
foreach (UASQuickViewItem* i, uasPropertyToLabelMap.values())
{
i->deleteLater();
}
Bryant
committed
uasPropertyToLabelMap.clear();
Bryant
committed
updateTimer->stop();
foreach (QAction* i, this->actions())
{
i->deleteLater();
}
Michael Carpenter
committed
}
Bryant
committed
// Update the UAS to point to the new one.
Michael Carpenter
committed
this->uas = uas;
Bryant
committed
// And re-add the default actions.
addDefaultActions();
Bryant
committed
// And connect the new one if it exists.
if (this->uas)
Michael Carpenter
committed
{
Bryant
committed
// Monitor new UAS for changes
connect(this->uas,SIGNAL(valueChanged(int,QString,QString,QVariant,quint64)),this,SLOT(valueChanged(int,QString,QString,QVariant,quint64)));
// Populate a right-click menu for selecting which properties to display
qDebug() << "UASInfoWidget property count:" << uas->metaObject()->propertyCount();
for (int i=0;i<this->uas->metaObject()->propertyCount();i++)
Michael Carpenter
committed
{
Bryant
committed
if (this->uas->metaObject()->property(i).hasNotifySignal())
Michael Carpenter
committed
{
Bryant
committed
qDebug() << "Property:" << i << this->uas->metaObject()->property(i).name();
uasPropertyList.append(this->uas->metaObject()->property(i).name());
if (!uasPropertyToLabelMap.contains(this->uas->metaObject()->property(i).name()))
Michael Carpenter
committed
{
Bryant
committed
QAction *action = new QAction(QString(this->uas->metaObject()->property(i).name()),this);
action->setCheckable(true);
connect(action,SIGNAL(toggled(bool)),this,SLOT(actionTriggered(bool)));
this->addAction(action);
}
qDebug() << "Signature:" << uas->metaObject()->property(i).notifySignal().signature();
int val = this->metaObject()->indexOfMethod("valChanged(double,QString)");
if (val != -1)
{
if (!connect(uas,uas->metaObject()->property(i).notifySignal(),this,this->metaObject()->method(val)))
{
qDebug() << "Error connecting signal";
}
Michael Carpenter
committed
}
}
}
Bryant
committed
// And periodically update the view.
updateTimer->start(1000);
Michael Carpenter
committed
}
}
void UASQuickView::actionTriggered(bool checked)
{
QAction *senderlabel = qobject_cast<QAction*>(sender());
if (!senderlabel)
{
return;
}
if (checked)
{
UASQuickViewItem *item = new UASQuickViewItem(this);
item->setTitle(senderlabel->text());
this->layout()->addWidget(item);
Michael Carpenter
committed
uasPropertyToLabelMap[senderlabel->text()] = item;
}
else
{
this->layout()->removeWidget(uasPropertyToLabelMap[senderlabel->text()]);
Michael Carpenter
committed
uasPropertyToLabelMap[senderlabel->text()]->deleteLater();
Michael Carpenter
committed
uasPropertyToLabelMap.remove(senderlabel->text());
Michael Carpenter
committed
}
}
Michael Carpenter
committed
void UASQuickView::valueChanged(const int uasid, const QString& name, const QString& unit, const QVariant value,const quint64 msecs)
{
Bryant
committed
Q_UNUSED(uasid);
Q_UNUSED(unit);
Q_UNUSED(msecs);
Michael Carpenter
committed
uasPropertyValueMap[name] = value.toDouble();
}
Michael Carpenter
committed
void UASQuickView::valChanged(double val,QString type)
{
Bryant
committed
Q_UNUSED(val);
Q_UNUSED(type);
Michael Carpenter
committed
//qDebug() << "Value changed:" << type << val;
Michael Carpenter
committed
// uasPropertyValueMap[type] = val;