UASQuickView.cc 6.86 KB
Newer Older
1 2 3
#include "UASQuickView.h"
#include <QMetaMethod>
#include <QDebug>
4 5 6
UASQuickView::UASQuickView(QWidget *parent) :
    QWidget(parent),
    m_ui(new Ui::UASQuickView)
7
{
8 9
    m_ui->setupUi(this);

10 11 12 13 14 15 16 17
    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);

18
    addDefaultActions();
19

20 21 22 23 24 25
    updateTimer = new QTimer(this);
    connect(updateTimer,SIGNAL(timeout()),this,SLOT(updateTimerTick()));
}

void UASQuickView::addDefaultActions()
{
26
    {
27
        QAction *action = new QAction(tr("latitude"),this);
28 29 30 31 32
        action->setCheckable(true);
        action->setChecked(true);
        connect(action,SIGNAL(toggled(bool)),this,SLOT(actionTriggered(bool)));
        this->addAction(action);
        UASQuickViewItem *item = new UASQuickViewItem(this);
33
        item->setTitle(tr("latitude"));
34
        this->layout()->addWidget(item);
35 36 37 38
        uasPropertyToLabelMap["latitude"] = item;
    }

    {
39
        QAction *action = new QAction(tr("longitude"),this);
40 41 42 43 44
        action->setCheckable(true);
        action->setChecked(true);
        connect(action,SIGNAL(toggled(bool)),this,SLOT(actionTriggered(bool)));
        this->addAction(action);
        UASQuickViewItem *item = new UASQuickViewItem(this);
45
        item->setTitle(tr("longitude"));
46
        this->layout()->addWidget(item);
47 48 49 50
        uasPropertyToLabelMap["longitude"] = item;
    }

    {
51
        QAction *action = new QAction(tr("altitude"),this);
52 53 54 55 56
        action->setCheckable(true);
        action->setChecked(true);
        connect(action,SIGNAL(toggled(bool)),this,SLOT(actionTriggered(bool)));
        this->addAction(action);
        UASQuickViewItem *item = new UASQuickViewItem(this);
57
        item->setTitle(tr("altitude"));
58
        this->layout()->addWidget(item);
59 60 61
        uasPropertyToLabelMap["altitude"] = item;
    }

62
    {
63
        QAction *action = new QAction(tr("satelliteCount"),this);
64 65 66 67 68
        action->setCheckable(true);
        action->setChecked(true);
        connect(action,SIGNAL(toggled(bool)),this,SLOT(actionTriggered(bool)));
        this->addAction(action);
        UASQuickViewItem *item = new UASQuickViewItem(this);
69
        item->setTitle(tr("satelliteCount"));
70
        this->layout()->addWidget(item);
71 72 73
        uasPropertyToLabelMap["satelliteCount"] = item;
    }

74
    {
75
        QAction *action = new QAction(tr("distToWaypoint"),this);
76 77 78 79 80
        action->setCheckable(true);
        action->setChecked(true);
        connect(action,SIGNAL(toggled(bool)),this,SLOT(actionTriggered(bool)));
        this->addAction(action);
        UASQuickViewItem *item = new UASQuickViewItem(this);
81
        item->setTitle(tr("distToWaypoint"));
82
        this->layout()->addWidget(item);
83 84
        uasPropertyToLabelMap["distToWaypoint"] = item;
    }
85
}
86

87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
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)
{
111 112
    // Clean up from the old UAS
    if (this->uas)
113
    {
114 115
        uasPropertyList.clear();
        uasPropertyValueMap.clear();
116 117 118 119
        foreach (UASQuickViewItem* i, uasPropertyToLabelMap.values())
        {
            i->deleteLater();
        }
120
        uasPropertyToLabelMap.clear();
121

122
        updateTimer->stop();
123 124 125 126
        foreach (QAction* i, this->actions())
        {
            i->deleteLater();
        }
127
    }
128

129
    // Update the UAS to point to the new one.
130
    this->uas = uas;
131

132 133 134
    // And re-add the default actions.
    addDefaultActions();

135 136
    // And connect the new one if it exists.
    if (this->uas)
137
    {
138 139 140 141 142 143
        // 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++)
144
        {
145
            if (this->uas->metaObject()->property(i).hasNotifySignal())
146
            {
147 148 149
                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()))
150
                {
151 152 153 154 155 156 157 158 159 160 161 162 163 164
                    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";
                    }
165 166 167
                }
            }
        }
168 169 170

        // And periodically update the view.
        updateTimer->start(1000);
171 172 173 174 175 176 177 178 179 180 181 182 183
    }
}
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());
184
        this->layout()->addWidget(item);
185 186 187 188
        uasPropertyToLabelMap[senderlabel->text()] = item;
    }
    else
    {
189
        this->layout()->removeWidget(uasPropertyToLabelMap[senderlabel->text()]);
190
        uasPropertyToLabelMap[senderlabel->text()]->deleteLater();
191 192
        uasPropertyToLabelMap.remove(senderlabel->text());

193 194
    }
}
195 196
void UASQuickView::valueChanged(const int uasid, const QString& name, const QString& unit, const QVariant value,const quint64 msecs)
{
197 198 199
    Q_UNUSED(uasid);
    Q_UNUSED(unit);
    Q_UNUSED(msecs);
200 201
    uasPropertyValueMap[name] = value.toDouble();
}
202 203 204

void UASQuickView::valChanged(double val,QString type)
{
205 206
    Q_UNUSED(val);
    Q_UNUSED(type);
207
    //qDebug() << "Value changed:" << type << val;
208
   // uasPropertyValueMap[type] = val;
209
}