Linecharts.cc 3.1 KB
Newer Older
1 2
#include <QShowEvent>

3
#include "Linecharts.h"
lm's avatar
lm committed
4
#include "UASManager.h"
5

6 7
#include "MainWindow.h"

8
Linecharts::Linecharts(QWidget *parent) :
9 10 11
    QStackedWidget(parent),
    plots(),
    active(true)
12
{
13 14 15
    this->setVisible(false);
    // Get current MAV list
    QList<UASInterface*> systems = UASManager::instance()->getUASList();
lm's avatar
lm committed
16

17
    // Add each of them
18
    foreach (UASInterface* sys, systems) {
19 20 21 22 23 24 25 26
        addSystem(sys);
    }
    connect(UASManager::instance(), SIGNAL(UASCreated(UASInterface*)),
            this, SLOT(addSystem(UASInterface*)));
    connect(UASManager::instance(), SIGNAL(activeUASSet(int)),
            this, SLOT(selectSystem(int)));
    connect(this, SIGNAL(logfileWritten(QString)),
            MainWindow::instance(), SLOT(loadDataView(QString)));
27 28
}

29
void Linecharts::showEvent(QShowEvent* event)
30
{
31 32
    // React only to internal (pre-display)
    // events
33
    Q_UNUSED(event) {
34
        QWidget* prevWidget = currentWidget();
35
        if (prevWidget) {
36
            LinechartWidget* chart = dynamic_cast<LinechartWidget*>(prevWidget);
37
            if (chart) {
38 39 40 41 42 43 44 45 46 47 48
                this->active = true;
                chart->setActive(true);
            }
        }
    }
}

void Linecharts::hideEvent(QHideEvent* event)
{
    // React only to internal (pre-display)
    // events
49
    Q_UNUSED(event) {
50
        QWidget* prevWidget = currentWidget();
51
        if (prevWidget) {
52
            LinechartWidget* chart = dynamic_cast<LinechartWidget*>(prevWidget);
53
            if (chart) {
54 55
                this->active = false;
                chart->setActive(false);
56
            }
57 58 59 60 61 62 63
        }
    }
}

void Linecharts::selectSystem(int systemid)
{
    QWidget* prevWidget = currentWidget();
64
    if (prevWidget) {
65
        LinechartWidget* chart = dynamic_cast<LinechartWidget*>(prevWidget);
66
        if (chart) {
67 68 69 70
            chart->setActive(false);
        }
    }
    QWidget* widget = plots.value(systemid, NULL);
71
    if (widget) {
72 73
        setCurrentWidget(widget);
        LinechartWidget* chart = dynamic_cast<LinechartWidget*>(widget);
74
        if (chart) {
75 76 77 78 79 80 81
            chart->setActive(true);
        }
    }
}

void Linecharts::addSystem(UASInterface* uas)
{
82
    if (!plots.contains(uas->getUASID())) {
83 84 85
        LinechartWidget* widget = new LinechartWidget(uas->getUASID(), this);
        addWidget(widget);
        plots.insert(uas->getUASID(), widget);
86 87 88 89 90
        // Values without unit
        //connect(uas, SIGNAL(valueChanged(int,QString,double,quint64)), widget, SLOT(appendData(int,QString,double,quint64)));
        // Values with unit as double
        connect(uas, SIGNAL(valueChanged(int,QString,QString,double,quint64)), widget, SLOT(appendData(int,QString,QString,double,quint64)));
        // Values with unit as integer
91
        connect(uas, SIGNAL(valueChanged(int,QString,QString,int,quint64)), widget, SLOT(appendData(int,QString,QString,int,quint64)));
92

93
        connect(widget, SIGNAL(logfileWritten(QString)), this, SIGNAL(logfileWritten(QString)));
94
        // Set system active if this is the only system
95 96
        if (active) {
            if (plots.size() == 1) {
97 98 99 100 101
                selectSystem(uas->getUASID());
            }
        }
    }
}