UASQuickView.cc 11 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
    //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");
    }
33 34 35 36 37

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

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

43 44 45
void UASQuickView::actionTriggered()
{
    if (quickViewSelectDialog)
46
    {
47 48
        quickViewSelectDialog->show();
        return;
49
    }
50 51 52 53 54 55 56 57 58 59 60
    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();
}
61

62
void UASQuickView::saveSettings()
63
{
64 65 66 67
    QSettings settings;
    settings.beginWriteArray("UAS_QUICK_VIEW_ITEMS");
    int count = 0;
    for (QMap<QString,UASQuickViewItem*>::const_iterator i = uasPropertyToLabelMap.constBegin();i!=uasPropertyToLabelMap.constEnd();i++)
68
    {
69 70 71
        settings.setArrayIndex(count++);
        settings.setValue("name",i.key());
        settings.setValue("type","text");
72
    }
73 74
    settings.endArray();
    settings.sync();
75
}
76

77 78 79 80 81
void UASQuickView::loadSettings()
{
    QSettings settings;
    int size = settings.beginReadArray("UAS_QUICK_VIEW_ITEMS");
    for (int i=0;i<size;i++)
82
    {
83 84 85 86 87 88 89
        settings.setArrayIndex(i);
        QString nameval = settings.value("name").toString();
        QString typeval = settings.value("type").toString();
        if (typeval == "text" && !uasPropertyToLabelMap.contains(nameval))
        {
            valueEnabled(nameval);
        }
90
    }
91
}
92

93 94
void UASQuickView::valueEnabled(QString value)
{
95
    UASQuickViewItem *item = new UASQuickViewTextItem(this);
96
    item->setTitle(value);
97
    m_ui->verticalLayout->addWidget(item);
98 99 100
    uasPropertyToLabelMap[value] = item;
    uasEnabledPropertyList.append(value);
    if (!uasPropertyValueMap.contains(value))
101
    {
102
        uasPropertyValueMap[value] = 0;
103
    }
104
    saveSettings();
105

106
}
107 108 109 110

void UASQuickView::valueDisabled(QString value)
{
    if (uasPropertyToLabelMap.contains(value))
111
    {
112 113 114
        UASQuickViewItem *item = uasPropertyToLabelMap[value];
        uasPropertyToLabelMap.remove(value);
        item->hide();
115
        m_ui->verticalLayout->removeWidget(item);
116 117
        item->deleteLater();
        uasEnabledPropertyList.removeOne(value);
118
        saveSettings();
119
    }
120
}
121

122 123 124
void UASQuickView::selectDialogClosed()
{
    quickViewSelectDialog = 0;
125
}
126

127 128
void UASQuickView::updateTimerTick()
{
129 130
    //uasPropertyValueMap
    for (QMap<QString,UASQuickViewItem*>::const_iterator i = uasPropertyToLabelMap.constBegin(); i != uasPropertyToLabelMap.constEnd();i++)
131
    {
132
        if (uasPropertyValueMap.contains(i.key()))
133
        {
134
            i.value()->setValue(uasPropertyValueMap[i.key()]);
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
        }
    }
}

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

void UASQuickView::setActiveUAS(UASInterface* uas)
{
152
    // Clean up from the old UAS
153
    if (this->uas)
154
    {
155
        uasPropertyValueMap.clear();
156 157 158 159
        foreach (UASQuickViewItem* i, uasPropertyToLabelMap.values())
        {
            i->deleteLater();
        }
160
        uasPropertyToLabelMap.clear();
161

162
        updateTimer->stop();
163 164 165 166
        foreach (QAction* i, this->actions())
        {
            i->deleteLater();
        }
167
    }
168

169
    // Update the UAS to point to the new one.
170
    this->uas = uas;
171 172 173 174 175 176

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

179 180 181 182 183 184 185 186 187 188 189 190
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)));
}
191

192 193
void UASQuickView::valueChanged(const int uasId, const QString& name, const QString& unit, const quint8 value, const quint64 msec)
{
194 195 196
    Q_UNUSED(uasId);
    Q_UNUSED(unit);
    Q_UNUSED(msec);
197 198 199 200 201 202 203 204
    if (!uasPropertyValueMap.contains(name))
    {
        if (quickViewSelectDialog)
        {
            quickViewSelectDialog->addItem(name);
        }
    }
    uasPropertyValueMap[name] = value;
205
}
206 207 208

void UASQuickView::valueChanged(const int uasId, const QString& name, const QString& unit, const qint8 value, const quint64 msec)
{
209 210 211
    Q_UNUSED(uasId);
    Q_UNUSED(unit);
    Q_UNUSED(msec);
212 213 214 215 216 217 218 219 220
    if (!uasPropertyValueMap.contains(name))
    {
        if (quickViewSelectDialog)
        {
            quickViewSelectDialog->addItem(name);
        }
    }
    uasPropertyValueMap[name] = value;
}
221

222 223
void UASQuickView::valueChanged(const int uasId, const QString& name, const QString& unit, const quint16 value, const quint64 msec)
{
224 225 226
    Q_UNUSED(uasId);
    Q_UNUSED(unit);
    Q_UNUSED(msec);
227 228 229 230 231 232 233 234 235
    if (!uasPropertyValueMap.contains(name))
    {
        if (quickViewSelectDialog)
        {
            quickViewSelectDialog->addItem(name);
        }
    }
    uasPropertyValueMap[name] = value;
}
236

237 238
void UASQuickView::valueChanged(const int uasId, const QString& name, const QString& unit, const qint16 value, const quint64 msec)
{
239 240 241
    Q_UNUSED(uasId);
    Q_UNUSED(unit);
    Q_UNUSED(msec);
242 243 244 245 246 247 248 249 250
    if (!uasPropertyValueMap.contains(name))
    {
        if (quickViewSelectDialog)
        {
            quickViewSelectDialog->addItem(name);
        }
    }
    uasPropertyValueMap[name] = value;
}
251

252 253
void UASQuickView::valueChanged(const int uasId, const QString& name, const QString& unit, const quint32 value, const quint64 msec)
{
254 255 256
    Q_UNUSED(uasId);
    Q_UNUSED(unit);
    Q_UNUSED(msec);
257 258 259 260 261 262
    if (!uasPropertyValueMap.contains(name))
    {
        if (quickViewSelectDialog)
        {
            quickViewSelectDialog->addItem(name);
        }
263 264 265

        // And periodically update the view.
        updateTimer->start(1000);
266 267 268
    }
    uasPropertyValueMap[name] = value;
}
269

270 271
void UASQuickView::valueChanged(const int uasId, const QString& name, const QString& unit, const qint32 value, const quint64 msec)
{
272 273 274
    Q_UNUSED(uasId);
    Q_UNUSED(unit);
    Q_UNUSED(msec);
275 276 277 278 279 280 281 282 283
    if (!uasPropertyValueMap.contains(name))
    {
        if (quickViewSelectDialog)
        {
            quickViewSelectDialog->addItem(name);
        }
    }
    uasPropertyValueMap[name] = value;
}
284

285 286
void UASQuickView::valueChanged(const int uasId, const QString& name, const QString& unit, const quint64 value, const quint64 msec)
{
287 288 289
    Q_UNUSED(uasId);
    Q_UNUSED(unit);
    Q_UNUSED(msec);
290 291 292 293 294 295 296 297 298
    if (!uasPropertyValueMap.contains(name))
    {
        if (quickViewSelectDialog)
        {
            quickViewSelectDialog->addItem(name);
        }
    }
    uasPropertyValueMap[name] = value;
}
299

300 301
void UASQuickView::valueChanged(const int uasId, const QString& name, const QString& unit, const qint64 value, const quint64 msec)
{
302 303 304
    Q_UNUSED(uasId);
    Q_UNUSED(unit);
    Q_UNUSED(msec);
305
    if (!uasPropertyValueMap.contains(name))
306
    {
307
        if (quickViewSelectDialog)
308
        {
309
            quickViewSelectDialog->addItem(name);
310
        }
311 312 313

        // And periodically update the view.
        updateTimer->start(1000);
314
    }
315
    uasPropertyValueMap[name] = value;
316
}
317

318 319
void UASQuickView::valueChanged(const int uasId, const QString& name, const QString& unit, const double value, const quint64 msec)
{
320 321 322
    Q_UNUSED(uasId);
    Q_UNUSED(unit);
    Q_UNUSED(msec);
323 324 325 326 327 328 329 330 331 332
    if (!uasPropertyValueMap.contains(name))
    {
        if (quickViewSelectDialog)
        {
            quickViewSelectDialog->addItem(name);
        }
    }
    uasPropertyValueMap[name] = value;
}

333 334 335 336 337
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);
338 339 340 341 342 343 344
    if (!uasPropertyValueMap.contains(name))
    {
        if (quickViewSelectDialog)
        {
            quickViewSelectDialog->addItem(name);
        }
    }
345 346 347
    uasPropertyValueMap[name] = value.toDouble();
}

348 349 350 351 352 353 354 355 356
void UASQuickView::actionTriggered(bool checked)
{
    QAction *senderlabel = qobject_cast<QAction*>(sender());
    if (!senderlabel)
    {
        return;
    }
    if (checked)
    {
357
        UASQuickViewItem *item = new UASQuickViewTextItem(this);
358
        item->setTitle(senderlabel->text());
359
        this->layout()->addWidget(item);
360 361 362 363
        uasPropertyToLabelMap[senderlabel->text()] = item;
    }
    else
    {
364
        this->layout()->removeWidget(uasPropertyToLabelMap[senderlabel->text()]);
365
        uasPropertyToLabelMap[senderlabel->text()]->deleteLater();
366 367
        uasPropertyToLabelMap.remove(senderlabel->text());

368 369 370 371 372
    }
}

void UASQuickView::valChanged(double val,QString type)
{
373 374
    Q_UNUSED(val);
    Q_UNUSED(type);
375
}