Newer
Older
Michael Carpenter
committed
#include "UASQuickView.h"
#include <QMetaMethod>
#include <QDebug>
#include "UASQuickViewItemSelect.h"
Michael Carpenter
committed
#include "UASQuickViewTextItem.h"
Michael Carpenter
committed
#include <QSettings>
#include <QInputDialog>
Michael Carpenter
committed
UASQuickView::UASQuickView(QWidget *parent) : QWidget(parent)
{
quickViewSelectDialog=0;
m_columnCount=2;
m_currentColumn=0;
Michael Carpenter
committed
ui.setupUi(this);
m_verticalLayoutList.append(new QVBoxLayout());
ui.horizontalLayout->addItem(m_verticalLayoutList[0]);
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);
loadSettings();
Michael Carpenter
committed
Michael Carpenter
committed
//If we don't have any predefined settings, set some defaults.
if (uasPropertyValueMap.size() == 0)
{
valueEnabled("altitude");
valueEnabled("groundSpeed");
valueEnabled("distToWP");
Michael Carpenter
committed
valueEnabled("yaw");
valueEnabled("roll");
}
QAction *action = new QAction("Add Item",this);
action->setCheckable(false);
connect(action,SIGNAL(triggered()),this,SLOT(actionTriggered()));
this->addAction(action);
Michael Carpenter
committed
QAction *columnaction = new QAction("Set Column Count",this);
columnaction->setCheckable(false);
connect(columnaction,SIGNAL(triggered()),this,SLOT(columnActionTriggered()));
this->addAction(columnaction);
Michael Carpenter
committed
updateTimer = new QTimer(this);
connect(updateTimer,SIGNAL(timeout()),this,SLOT(updateTimerTick()));
updateTimer->start(1000);
Michael Carpenter
committed
}
Michael Carpenter
committed
UASQuickView::~UASQuickView()
{
if (quickViewSelectDialog)
{
delete quickViewSelectDialog;
}
}
void UASQuickView::columnActionTriggered()
{
bool ok = false;
int newcolumns = QInputDialog::getInt(this,"Columns","Enter number of columns",1,0,100,1,&ok);
if (!ok)
{
return;
}
m_columnCount = newcolumns;
sortItems(newcolumns);
saveSettings();
}
Michael Carpenter
committed
void UASQuickView::actionTriggered()
{
if (quickViewSelectDialog)
{
quickViewSelectDialog->show();
return;
}
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();
}
void UASQuickView::saveSettings()
{
Michael Carpenter
committed
QSettings settings;
settings.beginWriteArray("UAS_QUICK_VIEW_ITEMS");
int count = 0;
for (QMap<QString,UASQuickViewItem*>::const_iterator i = uasPropertyToLabelMap.constBegin();i!=uasPropertyToLabelMap.constEnd();i++)
{
settings.setArrayIndex(count++);
settings.setValue("name",i.key());
settings.setValue("type","text");
}
settings.endArray();
settings.setValue("UAS_QUICK_VIEW_COLUMNS",m_columnCount);
Michael Carpenter
committed
settings.sync();
}
void UASQuickView::loadSettings()
{
QSettings settings;
int size = settings.beginReadArray("UAS_QUICK_VIEW_ITEMS");
for (int i=0;i<size;i++)
{
settings.setArrayIndex(i);
QString nameval = settings.value("name").toString();
QString typeval = settings.value("type").toString();
if (typeval == "text" && !uasPropertyToLabelMap.contains(nameval))
{
valueEnabled(nameval);
}
}
settings.endArray();
m_columnCount = settings.value("UAS_QUICK_VIEW_COLUMNS",1).toInt();
sortItems(m_columnCount);
}
void UASQuickView::valueEnabled(QString value)
{
Michael Carpenter
committed
UASQuickViewItem *item = new UASQuickViewTextItem(this);
item->setTitle(value);
//ui.verticalLayout->addWidget(item);
//m_currentColumn
m_verticalLayoutList[m_currentColumn]->addWidget(item);
m_PropertyToLayoutIndexMap[value] = m_currentColumn;
m_currentColumn++;
if (m_currentColumn >= m_columnCount-1)
{
m_currentColumn = 0;
}
uasPropertyToLabelMap[value] = item;
uasEnabledPropertyList.append(value);
if (!uasPropertyValueMap.contains(value))
{
uasPropertyValueMap[value] = 0;
}
saveSettings();
}
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
void UASQuickView::sortItems(int columncount)
{
QList<QWidget*> itemlist;
for (QMap<QString,UASQuickViewItem*>::const_iterator i = uasPropertyToLabelMap.constBegin();i!=uasPropertyToLabelMap.constEnd();i++)
{
m_verticalLayoutList[m_PropertyToLayoutIndexMap[i.key()]]->removeWidget(i.value());
m_PropertyToLayoutIndexMap.remove(i.key());
itemlist.append(i.value());
}
//Item list has all the widgets availble, now re-add them to the layouts.
for (int i=0;i<m_verticalLayoutList.size();i++)
{
ui.horizontalLayout->removeItem(m_verticalLayoutList[i]);
m_verticalLayoutList[i]->deleteLater(); //removeItem de-parents the item.
}
m_verticalLayoutList.clear();
//Create a vertical layout for every intended column
for (int i=0;i<columncount;i++)
{
QVBoxLayout *layout = new QVBoxLayout(this);
ui.horizontalLayout->addItem(layout);
m_verticalLayoutList.append(layout);
}
//Cycle through all items and add them to the layout
int currcol = 0;
for (int i=0;i<itemlist.size();i++)
{
m_verticalLayoutList[currcol]->addWidget(itemlist[i]);
currcol++;
if (currcol >= columncount)
{
currcol = 0;
}
}
m_currentColumn = currcol;
}
void UASQuickView::valueDisabled(QString value)
{
if (uasPropertyToLabelMap.contains(value))
{
UASQuickViewItem *item = uasPropertyToLabelMap[value];
uasPropertyToLabelMap.remove(value);
item->hide();
//ui.verticalLayout->removeWidget(item);
//layout->removeWidget(item);
m_verticalLayoutList[m_PropertyToLayoutIndexMap[value]]->removeWidget(item);
sortItems(m_columnCount);
item->deleteLater();
uasEnabledPropertyList.removeOne(value);
saveSettings();
}
}
void UASQuickView::selectDialogClosed()
{
quickViewSelectDialog = 0;
}
Michael Carpenter
committed
void UASQuickView::updateTimerTick()
{
//uasPropertyValueMap
for (QMap<QString,UASQuickViewItem*>::const_iterator i = uasPropertyToLabelMap.constBegin(); i != uasPropertyToLabelMap.constEnd();i++)
Michael Carpenter
committed
{
if (uasPropertyValueMap.contains(i.key()))
Michael Carpenter
committed
{
i.value()->setValue(uasPropertyValueMap[i.key()]);
Michael Carpenter
committed
}
}
}
void UASQuickView::addUAS(UASInterface* uas)
{
if (uas)
{
if (!this->uas)
{
setActiveUAS(uas);
}
}
}
void UASQuickView::setActiveUAS(UASInterface* uas)
{
if (!uas)
{
return;
}
this->uas = uas;
Michael Carpenter
committed
connect(uas,SIGNAL(valueChanged(int,QString,QString,QVariant,quint64)),this,SLOT(valueChanged(int,QString,QString,QVariant,quint64)));
//connect(uas,SIGNAL())
}
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)));
}
void UASQuickView::valueChanged(const int uasId, const QString& name, const QString& unit, const quint8 value, const quint64 msec)
{
if (!uasPropertyValueMap.contains(name))
{
if (quickViewSelectDialog)
{
quickViewSelectDialog->addItem(name);
}
}
uasPropertyValueMap[name] = value;
Michael Carpenter
committed
}
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
void UASQuickView::valueChanged(const int uasId, const QString& name, const QString& unit, const qint8 value, const quint64 msec)
{
if (!uasPropertyValueMap.contains(name))
{
if (quickViewSelectDialog)
{
quickViewSelectDialog->addItem(name);
}
}
uasPropertyValueMap[name] = value;
}
void UASQuickView::valueChanged(const int uasId, const QString& name, const QString& unit, const quint16 value, const quint64 msec)
{
if (!uasPropertyValueMap.contains(name))
{
if (quickViewSelectDialog)
{
quickViewSelectDialog->addItem(name);
}
}
uasPropertyValueMap[name] = value;
}
void UASQuickView::valueChanged(const int uasId, const QString& name, const QString& unit, const qint16 value, const quint64 msec)
{
if (!uasPropertyValueMap.contains(name))
{
if (quickViewSelectDialog)
{
quickViewSelectDialog->addItem(name);
}
}
uasPropertyValueMap[name] = value;
}
void UASQuickView::valueChanged(const int uasId, const QString& name, const QString& unit, const quint32 value, const quint64 msec)
{
if (!uasPropertyValueMap.contains(name))
{
if (quickViewSelectDialog)
{
quickViewSelectDialog->addItem(name);
}
}
uasPropertyValueMap[name] = value;
}
void UASQuickView::valueChanged(const int uasId, const QString& name, const QString& unit, const qint32 value, const quint64 msec)
{
if (!uasPropertyValueMap.contains(name))
{
if (quickViewSelectDialog)
{
quickViewSelectDialog->addItem(name);
}
}
uasPropertyValueMap[name] = value;
}
void UASQuickView::valueChanged(const int uasId, const QString& name, const QString& unit, const quint64 value, const quint64 msec)
{
if (!uasPropertyValueMap.contains(name))
{
if (quickViewSelectDialog)
{
quickViewSelectDialog->addItem(name);
}
}
uasPropertyValueMap[name] = value;
}
void UASQuickView::valueChanged(const int uasId, const QString& name, const QString& unit, const qint64 value, const quint64 msec)
{
if (!uasPropertyValueMap.contains(name))
{
if (quickViewSelectDialog)
{
quickViewSelectDialog->addItem(name);
}
}
uasPropertyValueMap[name] = value;
}
void UASQuickView::valueChanged(const int uasId, const QString& name, const QString& unit, const double value, const quint64 msec)
{
if (!uasPropertyValueMap.contains(name))
{
if (quickViewSelectDialog)
{
quickViewSelectDialog->addItem(name);
}
}
uasPropertyValueMap[name] = value;
}
Michael Carpenter
committed
void UASQuickView::actionTriggered(bool checked)
{
QAction *senderlabel = qobject_cast<QAction*>(sender());
if (!senderlabel)
{
return;
}
if (checked)
{
valueEnabled(senderlabel->text());
/*UASQuickViewItem *item = new UASQuickViewTextItem(this);
Michael Carpenter
committed
item->setTitle(senderlabel->text());
layout->addWidget(item);
//ui.verticalLayout->addWidget(item);
m_currentColumn++;
if (m_currentColumn >= m_verticalLayoutList.size())
{
m_currentColumn = 0;
}
uasPropertyToLabelMap[senderlabel->text()] = item;*/
Michael Carpenter
committed
}
else
{
valueDisabled(senderlabel->text());
/*layout->removeWidget(uasPropertyToLabelMap[senderlabel->text()]);
Michael Carpenter
committed
uasPropertyToLabelMap[senderlabel->text()]->deleteLater();
uasPropertyToLabelMap.remove(senderlabel->text());*/
Michael Carpenter
committed
Michael Carpenter
committed
}
}
Michael Carpenter
committed
void UASQuickView::valueChanged(const int uasid, const QString& name, const QString& unit, const QVariant value,const quint64 msecs)
{
uasPropertyValueMap[name] = value.toDouble();
}
Michael Carpenter
committed
void UASQuickView::valChanged(double val,QString type)
{
//qDebug() << "Value changed:" << type << val;
Michael Carpenter
committed
// uasPropertyValueMap[type] = val;