UASQuickView.cc 9.78 KB
Newer Older
1
#include "UASQuickView.h"
2
#include "UASQuickViewItemSelect.h"
3
#include "UASQuickViewTextItem.h"
4 5
#include "MultiVehicleManager.h"
#include "UAS.h"
6
#include "QGCApplication.h"
7 8 9

#include <QMetaMethod>
#include <QDebug>
10
#include <QSettings>
11
#include <QInputDialog>
12

13 14
UASQuickView::UASQuickView(QWidget *parent) : QWidget(parent),
    uas(NULL)
15
{
16
    quickViewSelectDialog=0;
17 18
    m_columnCount=2;
    m_currentColumn=0;
19
    ui.setupUi(this);
20

21
    ui.horizontalLayout->setMargin(0);
22 23 24
    m_verticalLayoutList.append(new QVBoxLayout());
    ui.horizontalLayout->addItem(m_verticalLayoutList[0]);

25 26
    connect(qgcApp()->toolbox()->multiVehicleManager(), &MultiVehicleManager::activeVehicleChanged, this, &UASQuickView::_activeVehicleChanged);
    _activeVehicleChanged(qgcApp()->toolbox()->multiVehicleManager()->activeVehicle());
27 28
    this->setContextMenuPolicy(Qt::ActionsContextMenu);

29
    loadSettings();
30

31 32 33
    //If we don't have any predefined settings, set some defaults.
    if (uasPropertyValueMap.size() == 0)
    {
34
        valueEnabled("altitudeAMSL");
35
        valueEnabled("altitudeAMSLFT");
36
        valueEnabled("altitudeRelative");
37
        valueEnabled("groundSpeed");
38
        valueEnabled("distToWaypoint");
39
    }
40

41
    QAction *action = new QAction("Add/Remove Items",this);
42
    action->setCheckable(false);
Don Gagne's avatar
Don Gagne committed
43
    connect(action,&QAction::triggered,this, &UASQuickView::addActionTriggered);
44
    this->addAction(action);
45

46 47
    QAction *columnaction = new QAction("Set Column Count",this);
    columnaction->setCheckable(false);
48
    connect(columnaction,&QAction::triggered,this,&UASQuickView::columnActionTriggered);
49 50
    this->addAction(columnaction);

51
    updateTimer = new QTimer(this);
52
    connect(updateTimer,&QTimer::timeout,this,&UASQuickView::updateTimerTick);
53
    updateTimer->start(1000);
54

55
}
56 57 58 59 60 61 62
UASQuickView::~UASQuickView()
{
    if (quickViewSelectDialog)
    {
        delete quickViewSelectDialog;
    }
}
63 64 65
void UASQuickView::columnActionTriggered()
{
    bool ok = false;
66 67
    int newcolumns = QInputDialog::getInt(
        this,"Columns","Enter number of columns", m_columnCount, 1, 10, 1, &ok);
68 69 70 71 72 73 74 75
    if (!ok)
    {
        return;
    }
    m_columnCount = newcolumns;
    sortItems(newcolumns);
    saveSettings();
}
76

Don Gagne's avatar
Don Gagne committed
77
void UASQuickView::addActionTriggered()
78 79 80 81 82 83 84
{
    if (quickViewSelectDialog)
    {
        quickViewSelectDialog->show();
        return;
    }
    quickViewSelectDialog = new UASQuickViewItemSelect();
85 86 87 88
    connect(quickViewSelectDialog,&UASQuickViewItemSelect::destroyed,this,&UASQuickView::selectDialogClosed);
    connect(quickViewSelectDialog,&UASQuickViewItemSelect::valueDisabled,this,&UASQuickView::valueDisabled);
    connect(quickViewSelectDialog,&UASQuickViewItemSelect::valueEnabled,this,&UASQuickView::valueEnabled);

89 90 91 92 93 94 95
    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();
}
96
void UASQuickView::saveSettings()
97
{
98 99 100 101 102 103 104 105 106 107
    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();
108
    settings.setValue("UAS_QUICK_VIEW_COLUMNS",m_columnCount);
109
}
110 111 112
void UASQuickView::loadSettings()
{
    QSettings settings;
113
    m_columnCount = settings.value("UAS_QUICK_VIEW_COLUMNS",1).toInt();
114
    int size = settings.beginReadArray("UAS_QUICK_VIEW_ITEMS");
115
    for (int i = 0; i < size; i++)
116 117 118 119 120 121 122 123 124
    {
        settings.setArrayIndex(i);
        QString nameval = settings.value("name").toString();
        QString typeval = settings.value("type").toString();
        if (typeval == "text" && !uasPropertyToLabelMap.contains(nameval))
        {
            valueEnabled(nameval);
        }
    }
125 126
    settings.endArray();
    sortItems(m_columnCount);
127 128 129 130
}

void UASQuickView::valueEnabled(QString value)
{
131
    UASQuickViewItem *item = new UASQuickViewTextItem(this);
132
    item->setTitle(value);
133 134 135 136 137 138 139 140 141
    //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;
    }
142 143
    uasPropertyToLabelMap[value] = item;
    uasEnabledPropertyList.append(value);
144

145 146 147 148 149
    if (!uasPropertyValueMap.contains(value))
    {
        uasPropertyValueMap[value] = 0;
    }
    saveSettings();
150 151
    item->show();
    sortItems(m_columnCount);
152 153

}
154 155 156 157 158 159 160 161 162
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());
    }
nopeppermint's avatar
nopeppermint committed
163
    // Item list has all the widgets available, now re-add them to the layouts.
164
    for (int i = 0; i < m_verticalLayoutList.size(); i++)
165 166 167 168 169 170
    {
        ui.horizontalLayout->removeItem(m_verticalLayoutList[i]);
        m_verticalLayoutList[i]->deleteLater(); //removeItem de-parents the item.
    }
    m_verticalLayoutList.clear();

171 172
    // Create a vertical layout for every intended column
    for (int i = 0; i < columncount; i++)
173
    {
174
        QVBoxLayout *layout = new QVBoxLayout();
175 176
        ui.horizontalLayout->addItem(layout);
        m_verticalLayoutList.append(layout);
177
        layout->setMargin(0);
178 179 180 181
    }

    //Cycle through all items and add them to the layout
    int currcol = 0;
182
    for (int i = 0; i < itemlist.size(); i++)
183 184 185 186 187 188 189 190 191
    {
        m_verticalLayoutList[currcol]->addWidget(itemlist[i]);
        currcol++;
        if (currcol >= columncount)
        {
            currcol = 0;
        }
    }
    m_currentColumn = currcol;
192 193 194 195 196
    QApplication::processEvents();
    recalculateItemTextSizing();
}
void UASQuickView::resizeEvent(QResizeEvent *evt)
{
197
    Q_UNUSED(evt);
198 199 200 201 202 203 204 205 206 207 208 209 210
    recalculateItemTextSizing();
}
void UASQuickView::recalculateItemTextSizing()
{
    int minpixelsize = 65535;
    for (QMap<QString,UASQuickViewItem*>::const_iterator i = uasPropertyToLabelMap.constBegin();i!=uasPropertyToLabelMap.constEnd();i++)
    {
        int tempmin = i.value()->minValuePixelSize();
        if (tempmin < minpixelsize)
        {
            minpixelsize = tempmin;
        }
    }
211 212
    if(minpixelsize < 6)
        minpixelsize = 6;
213 214 215 216
    for (QMap<QString,UASQuickViewItem*>::const_iterator i = uasPropertyToLabelMap.constBegin();i!=uasPropertyToLabelMap.constEnd();i++)
    {
        i.value()->setValuePixelSize(minpixelsize);
    }
217
}
218 219 220 221 222 223 224 225

void UASQuickView::valueDisabled(QString value)
{
    if (uasPropertyToLabelMap.contains(value))
    {
        UASQuickViewItem *item = uasPropertyToLabelMap[value];
        uasPropertyToLabelMap.remove(value);
        item->hide();
226 227 228 229
        //ui.verticalLayout->removeWidget(item);
        //layout->removeWidget(item);
        m_verticalLayoutList[m_PropertyToLayoutIndexMap[value]]->removeWidget(item);
        sortItems(m_columnCount);
230 231
        item->deleteLater();
        uasEnabledPropertyList.removeOne(value);
232
        saveSettings();
233 234 235 236 237 238 239 240
    }
}

void UASQuickView::selectDialogClosed()
{
    quickViewSelectDialog = 0;
}

241 242
void UASQuickView::updateTimerTick()
{
243 244
    //uasPropertyValueMap
    for (QMap<QString,UASQuickViewItem*>::const_iterator i = uasPropertyToLabelMap.constBegin(); i != uasPropertyToLabelMap.constEnd();i++)
245
    {
246
        if (uasPropertyValueMap.contains(i.key()))
247
        {
248
            i.value()->setValue(uasPropertyValueMap[i.key()]);
249 250 251 252
        }
    }
}

253
void UASQuickView::_activeVehicleChanged(Vehicle* vehicle)
254
{
Don Gagne's avatar
Don Gagne committed
255
    if (uas || !vehicle) {
256 257
        return;
    }
258
    this->uas = vehicle->uas();
Don Gagne's avatar
Don Gagne committed
259
    connect(uas, SIGNAL(valueChanged(int,QString,QString,QVariant,quint64)),this,SLOT(valueChanged(int,QString,QString,QVariant,quint64)));
260 261 262
}
void UASQuickView::addSource(MAVLinkDecoder *decoder)
{
263
    connect(decoder,SIGNAL(valueChanged(int,QString,QString,QVariant,quint64)),this,SLOT(valueChanged(int,QString,QString,QVariant,quint64)));
264
}
265

266
void UASQuickView::valueChanged(const int uasId, const QString& name, const QString& unit, const QVariant &variant, const quint64 msec)
267
{
268 269
    Q_UNUSED(uasId);
    Q_UNUSED(unit);
270 271
    Q_UNUSED(msec);
    
272 273
    bool ok;
    double value = variant.toDouble(&ok);
Don Gagne's avatar
Don Gagne committed
274
    QMetaType::Type metaType = static_cast<QMetaType::Type>(variant.type());
Don Gagne's avatar
Don Gagne committed
275
    if(!ok || metaType == QMetaType::QString || metaType == QMetaType::QByteArray)
276 277
        return;

278 279 280 281 282 283 284 285 286 287
    if (!uasPropertyValueMap.contains(name))
    {
        if (quickViewSelectDialog)
        {
            quickViewSelectDialog->addItem(name);
        }
    }
    uasPropertyValueMap[name] = value;
}

288 289 290 291 292 293 294 295 296
void UASQuickView::actionTriggered(bool checked)
{
    QAction *senderlabel = qobject_cast<QAction*>(sender());
    if (!senderlabel)
    {
        return;
    }
    if (checked)
    {
297 298
        valueEnabled(senderlabel->text());
        /*UASQuickViewItem *item = new UASQuickViewTextItem(this);
299
        item->setTitle(senderlabel->text());
300 301 302 303 304 305 306 307 308 309
        layout->addWidget(item);
        //ui.verticalLayout->addWidget(item);
        m_currentColumn++;
        if (m_currentColumn >= m_verticalLayoutList.size())
        {
            m_currentColumn = 0;
        }
        uasPropertyToLabelMap[senderlabel->text()] = item;*/


310 311 312
    }
    else
    {
313 314
        valueDisabled(senderlabel->text());
        /*layout->removeWidget(uasPropertyToLabelMap[senderlabel->text()]);
315
        uasPropertyToLabelMap[senderlabel->text()]->deleteLater();
316
        uasPropertyToLabelMap.remove(senderlabel->text());*/
317

318 319
    }
}