UASQuickView.cc 11.2 KB
Newer Older
1 2 3
#include "UASQuickView.h"
#include <QMetaMethod>
#include <QDebug>
4
#include "UASQuickViewItemSelect.h"
5
#include "UASQuickViewTextItem.h"
6
#include <QSettings>
7

8 9 10
UASQuickView::UASQuickView(QWidget *parent) :
    QWidget(parent),
    m_ui(new Ui::UASQuickView)
11
{
12
    m_ui->setupUi(this);
13
    quickViewSelectDialog=0;
14

15 16 17 18 19 20 21 22
    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);

23
    loadSettings();
24

25 26 27 28 29 30 31 32 33
    //If we don't have any predefined settings, set some defaults.
    if (uasPropertyValueMap.size() == 0)
    {
        valueEnabled("altitude");
        valueEnabled("groundSpeed");
        valueEnabled("distToWaypoint");
        valueEnabled("yaw");
        valueEnabled("roll");
    }
34 35 36 37 38

    QAction *action = new QAction("Add Item",this);
    action->setCheckable(false);
    connect(action,SIGNAL(triggered()),this,SLOT(actionTriggered()));
    this->addAction(action);
39

40 41 42 43
    updateTimer = new QTimer(this);
    connect(updateTimer,SIGNAL(timeout()),this,SLOT(updateTimerTick()));
}

44 45 46 47 48 49 50 51
UASQuickView::~UASQuickView()
{
    if (quickViewSelectDialog)
    {
        delete quickViewSelectDialog;
    }
}

52 53 54
void UASQuickView::actionTriggered()
{
    if (quickViewSelectDialog)
55
    {
56 57
        quickViewSelectDialog->show();
        return;
58
    }
59 60 61 62 63 64 65 66 67 68 69
    quickViewSelectDialog = new UASQuickViewItemSelect();
    connect(quickViewSelectDialog,SIGNAL(destroyed()),this,SLOT(selectDialogClosed()));
    connect(quickViewSelectDialog,SIGNAL(valueDisabled(QString)),this,SLOT(valueDisabled(QString)));
    connect(quickViewSelectDialog,SIGNAL(valueEnabled(QString)),this,SLOT(valueEnabled(QString)));
    quickViewSelectDialog->setAttribute(Qt::WA_DeleteOnClose,true);
    for (QMap<QString,double>::const_iterator i = uasPropertyValueMap.constBegin();i!=uasPropertyValueMap.constEnd();i++)
    {
        quickViewSelectDialog->addItem(i.key(),uasEnabledPropertyList.contains(i.key()));
    }
    quickViewSelectDialog->show();
}
70

71
void UASQuickView::saveSettings()
72
{
73 74 75 76
    QSettings settings;
    settings.beginWriteArray("UAS_QUICK_VIEW_ITEMS");
    int count = 0;
    for (QMap<QString,UASQuickViewItem*>::const_iterator i = uasPropertyToLabelMap.constBegin();i!=uasPropertyToLabelMap.constEnd();i++)
77
    {
78 79 80
        settings.setArrayIndex(count++);
        settings.setValue("name",i.key());
        settings.setValue("type","text");
81
    }
82 83
    settings.endArray();
    settings.sync();
84
}
85

86 87 88 89 90
void UASQuickView::loadSettings()
{
    QSettings settings;
    int size = settings.beginReadArray("UAS_QUICK_VIEW_ITEMS");
    for (int i=0;i<size;i++)
91
    {
92 93 94 95 96 97 98
        settings.setArrayIndex(i);
        QString nameval = settings.value("name").toString();
        QString typeval = settings.value("type").toString();
        if (typeval == "text" && !uasPropertyToLabelMap.contains(nameval))
        {
            valueEnabled(nameval);
        }
99
    }
100
}
101

102 103
void UASQuickView::valueEnabled(QString value)
{
104
    UASQuickViewItem *item = new UASQuickViewTextItem(this);
105
    item->setTitle(value);
106
    m_ui->verticalLayout->addWidget(item);
107 108 109
    uasPropertyToLabelMap[value] = item;
    uasEnabledPropertyList.append(value);
    if (!uasPropertyValueMap.contains(value))
110
    {
111
        uasPropertyValueMap[value] = 0;
112
    }
113
    saveSettings();
114

115
}
116 117 118 119

void UASQuickView::valueDisabled(QString value)
{
    if (uasPropertyToLabelMap.contains(value))
120
    {
121 122 123
        UASQuickViewItem *item = uasPropertyToLabelMap[value];
        uasPropertyToLabelMap.remove(value);
        item->hide();
124
        m_ui->verticalLayout->removeWidget(item);
125 126
        item->deleteLater();
        uasEnabledPropertyList.removeOne(value);
127
        saveSettings();
128
    }
129
}
130

131 132 133
void UASQuickView::selectDialogClosed()
{
    quickViewSelectDialog = 0;
134
}
135

136 137
void UASQuickView::updateTimerTick()
{
138 139
    //uasPropertyValueMap
    for (QMap<QString,UASQuickViewItem*>::const_iterator i = uasPropertyToLabelMap.constBegin(); i != uasPropertyToLabelMap.constEnd();i++)
140
    {
141
        if (uasPropertyValueMap.contains(i.key()))
142
        {
143
            i.value()->setValue(uasPropertyValueMap[i.key()]);
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
        }
    }
}

void UASQuickView::addUAS(UASInterface* uas)
{
    if (uas)
    {
        if (!this->uas)
        {
            setActiveUAS(uas);
        }
    }
}

void UASQuickView::setActiveUAS(UASInterface* uas)
{
161
    // Clean up from the old UAS
162
    if (this->uas)
163
    {
164
        uasPropertyValueMap.clear();
165 166 167 168
        foreach (UASQuickViewItem* i, uasPropertyToLabelMap.values())
        {
            i->deleteLater();
        }
169
        uasPropertyToLabelMap.clear();
170

171
        updateTimer->stop();
172 173 174 175
        foreach (QAction* i, this->actions())
        {
            i->deleteLater();
        }
176
    }
177

178
    // Update the UAS to point to the new one.
179
    this->uas = uas;
180 181 182 183 184 185

    if (this->uas)
    {
        connect(uas,SIGNAL(valueChanged(int,QString,QString,QVariant,quint64)),this,SLOT(valueChanged(int,QString,QString,QVariant,quint64)));
        updateTimer->start(1000);
    }
186
}
187

188 189 190 191 192 193 194 195 196 197 198 199
void UASQuickView::addSource(MAVLinkDecoder *decoder)
{
    connect(decoder,SIGNAL(valueChanged(int,QString,QString,double,quint64)),this,SLOT(valueChanged(int,QString,QString,double,quint64)));
    connect(decoder,SIGNAL(valueChanged(int,QString,QString,qint8,quint64)),this,SLOT(valueChanged(int,QString,QString,qint8,quint64)));
    connect(decoder,SIGNAL(valueChanged(int,QString,QString,qint16,quint64)),this,SLOT(valueChanged(int,QString,QString,qint16,quint64)));
    connect(decoder,SIGNAL(valueChanged(int,QString,QString,qint32,quint64)),this,SLOT(valueChanged(int,QString,QString,qint32,quint64)));
    connect(decoder,SIGNAL(valueChanged(int,QString,QString,qint64,quint64)),this,SLOT(valueChanged(int,QString,QString,qint64,quint64)));
    connect(decoder,SIGNAL(valueChanged(int,QString,QString,quint8,quint64)),this,SLOT(valueChanged(int,QString,QString,quint8,quint64)));
    connect(decoder,SIGNAL(valueChanged(int,QString,QString,quint16,quint64)),this,SLOT(valueChanged(int,QString,QString,quint16,quint64)));
    connect(decoder,SIGNAL(valueChanged(int,QString,QString,quint32,quint64)),this,SLOT(valueChanged(int,QString,QString,quint32,quint64)));
    connect(decoder,SIGNAL(valueChanged(int,QString,QString,quint64,quint64)),this,SLOT(valueChanged(int,QString,QString,quint64,quint64)));
}
200

201 202
void UASQuickView::valueChanged(const int uasId, const QString& name, const QString& unit, const quint8 value, const quint64 msec)
{
203 204 205
    Q_UNUSED(uasId);
    Q_UNUSED(unit);
    Q_UNUSED(msec);
206 207 208 209 210 211 212 213
    if (!uasPropertyValueMap.contains(name))
    {
        if (quickViewSelectDialog)
        {
            quickViewSelectDialog->addItem(name);
        }
    }
    uasPropertyValueMap[name] = value;
214
}
215 216 217

void UASQuickView::valueChanged(const int uasId, const QString& name, const QString& unit, const qint8 value, const quint64 msec)
{
218 219 220
    Q_UNUSED(uasId);
    Q_UNUSED(unit);
    Q_UNUSED(msec);
221 222 223 224 225 226 227 228 229
    if (!uasPropertyValueMap.contains(name))
    {
        if (quickViewSelectDialog)
        {
            quickViewSelectDialog->addItem(name);
        }
    }
    uasPropertyValueMap[name] = value;
}
230

231 232
void UASQuickView::valueChanged(const int uasId, const QString& name, const QString& unit, const quint16 value, const quint64 msec)
{
233 234 235
    Q_UNUSED(uasId);
    Q_UNUSED(unit);
    Q_UNUSED(msec);
236 237 238 239 240 241 242 243 244
    if (!uasPropertyValueMap.contains(name))
    {
        if (quickViewSelectDialog)
        {
            quickViewSelectDialog->addItem(name);
        }
    }
    uasPropertyValueMap[name] = value;
}
245

246 247
void UASQuickView::valueChanged(const int uasId, const QString& name, const QString& unit, const qint16 value, const quint64 msec)
{
248 249 250
    Q_UNUSED(uasId);
    Q_UNUSED(unit);
    Q_UNUSED(msec);
251 252 253 254 255 256 257 258 259
    if (!uasPropertyValueMap.contains(name))
    {
        if (quickViewSelectDialog)
        {
            quickViewSelectDialog->addItem(name);
        }
    }
    uasPropertyValueMap[name] = value;
}
260

261 262
void UASQuickView::valueChanged(const int uasId, const QString& name, const QString& unit, const quint32 value, const quint64 msec)
{
263 264 265
    Q_UNUSED(uasId);
    Q_UNUSED(unit);
    Q_UNUSED(msec);
266 267 268 269 270 271
    if (!uasPropertyValueMap.contains(name))
    {
        if (quickViewSelectDialog)
        {
            quickViewSelectDialog->addItem(name);
        }
272 273 274

        // And periodically update the view.
        updateTimer->start(1000);
275 276 277
    }
    uasPropertyValueMap[name] = value;
}
278

279 280
void UASQuickView::valueChanged(const int uasId, const QString& name, const QString& unit, const qint32 value, const quint64 msec)
{
281 282 283
    Q_UNUSED(uasId);
    Q_UNUSED(unit);
    Q_UNUSED(msec);
284 285 286 287 288 289 290 291 292
    if (!uasPropertyValueMap.contains(name))
    {
        if (quickViewSelectDialog)
        {
            quickViewSelectDialog->addItem(name);
        }
    }
    uasPropertyValueMap[name] = value;
}
293

294 295
void UASQuickView::valueChanged(const int uasId, const QString& name, const QString& unit, const quint64 value, const quint64 msec)
{
296 297 298
    Q_UNUSED(uasId);
    Q_UNUSED(unit);
    Q_UNUSED(msec);
299 300 301 302 303 304 305 306 307
    if (!uasPropertyValueMap.contains(name))
    {
        if (quickViewSelectDialog)
        {
            quickViewSelectDialog->addItem(name);
        }
    }
    uasPropertyValueMap[name] = value;
}
308

309 310
void UASQuickView::valueChanged(const int uasId, const QString& name, const QString& unit, const qint64 value, const quint64 msec)
{
311 312 313
    Q_UNUSED(uasId);
    Q_UNUSED(unit);
    Q_UNUSED(msec);
314
    if (!uasPropertyValueMap.contains(name))
315
    {
316
        if (quickViewSelectDialog)
317
        {
318
            quickViewSelectDialog->addItem(name);
319
        }
320 321 322

        // And periodically update the view.
        updateTimer->start(1000);
323
    }
324
    uasPropertyValueMap[name] = value;
325
}
326

327 328
void UASQuickView::valueChanged(const int uasId, const QString& name, const QString& unit, const double value, const quint64 msec)
{
329 330 331
    Q_UNUSED(uasId);
    Q_UNUSED(unit);
    Q_UNUSED(msec);
332 333 334 335 336 337 338 339 340 341
    if (!uasPropertyValueMap.contains(name))
    {
        if (quickViewSelectDialog)
        {
            quickViewSelectDialog->addItem(name);
        }
    }
    uasPropertyValueMap[name] = value;
}

342 343 344 345 346
void UASQuickView::valueChanged(const int uasId, const QString& name, const QString& unit, const QVariant value,const quint64 msec)
{
    Q_UNUSED(uasId);
    Q_UNUSED(unit);
    Q_UNUSED(msec);
347 348 349 350 351 352 353
    if (!uasPropertyValueMap.contains(name))
    {
        if (quickViewSelectDialog)
        {
            quickViewSelectDialog->addItem(name);
        }
    }
354 355 356
    uasPropertyValueMap[name] = value.toDouble();
}

357 358 359 360 361 362 363 364 365
void UASQuickView::actionTriggered(bool checked)
{
    QAction *senderlabel = qobject_cast<QAction*>(sender());
    if (!senderlabel)
    {
        return;
    }
    if (checked)
    {
366
        UASQuickViewItem *item = new UASQuickViewTextItem(this);
367
        item->setTitle(senderlabel->text());
368
        this->layout()->addWidget(item);
369 370 371 372
        uasPropertyToLabelMap[senderlabel->text()] = item;
    }
    else
    {
373
        this->layout()->removeWidget(uasPropertyToLabelMap[senderlabel->text()]);
374
        uasPropertyToLabelMap[senderlabel->text()]->deleteLater();
375 376
        uasPropertyToLabelMap.remove(senderlabel->text());

377 378 379 380 381
    }
}

void UASQuickView::valChanged(double val,QString type)
{
382 383
    Q_UNUSED(val);
    Q_UNUSED(type);
384
}