Linecharts.cc 5.3 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();
lm's avatar
lm committed
64 65
    if (prevWidget)
    {
66
        LinechartWidget* chart = dynamic_cast<LinechartWidget*>(prevWidget);
lm's avatar
lm committed
67 68
        if (chart)
        {
69
            chart->setActive(false);
lm's avatar
lm committed
70
            chart->setActiveSystem(systemid);
71 72 73
        }
    }
    QWidget* widget = plots.value(systemid, NULL);
lm's avatar
lm committed
74 75
    if (widget)
    {
76 77
        setCurrentWidget(widget);
        LinechartWidget* chart = dynamic_cast<LinechartWidget*>(widget);
lm's avatar
lm committed
78 79
        if (chart)
        {
80
            chart->setActive(true);
lm's avatar
lm committed
81
            chart->setActiveSystem(systemid);
82 83 84 85 86 87
        }
    }
}

void Linecharts::addSystem(UASInterface* uas)
{
lm's avatar
lm committed
88 89
    if (!plots.contains(uas->getUASID()))
    {
90 91 92
        LinechartWidget* widget = new LinechartWidget(uas->getUASID(), this);
        addWidget(widget);
        plots.insert(uas->getUASID(), widget);
93 94 95 96 97
        // 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
98
        connect(uas, SIGNAL(valueChanged(int,QString,QString,int,quint64)), widget, SLOT(appendData(int,QString,QString,int,quint64)));
99

100
        connect(widget, SIGNAL(logfileWritten(QString)), this, SIGNAL(logfileWritten(QString)));
101
        // Set system active if this is the only system
lm's avatar
lm committed
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
        if (active)
        {
            if (plots.size() == 1)
            {
                // FIXME XXX HACK
                // Connect generic sources
                for (int i = 0; i < genericSources.count(); ++i)
                {
                    connect(genericSources[i], SIGNAL(valueChanged(int,QString,QString,int,quint64)), plots.values().first(), SLOT(appendData(int,QString,QString,int,quint64)));
                    connect(genericSources[i], SIGNAL(valueChanged(int,QString,QString,unsigned int,quint64)), plots.values().first(), SLOT(appendData(int,QString,QString,unsigned int,quint64)));
                    connect(genericSources[i], SIGNAL(valueChanged(int,QString,QString,quint64,quint64)), plots.values().first(), SLOT(appendData(int,QString,QString,quint64,quint64)));
                    connect(genericSources[i], SIGNAL(valueChanged(int,QString,QString,qint64,quint64)), plots.values().first(), SLOT(appendData(int,QString,QString,qint64,quint64)));
                    connect(genericSources[i], SIGNAL(valueChanged(int,QString,QString,double,quint64)), plots.values().first(), SLOT(appendData(int,QString,QString,double,quint64)));
                }
                // Select system
117 118 119 120 121
                selectSystem(uas->getUASID());
            }
        }
    }
}
lm's avatar
lm committed
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136

void Linecharts::addSource(QObject* obj)
{
    genericSources.append(obj);
    // FIXME XXX HACK
    if (plots.size() > 0)
    {
        // Connect generic source
        connect(obj, SIGNAL(valueChanged(int,QString,QString,int,quint64)), plots.values().first(), SLOT(appendData(int,QString,QString,int,quint64)));
        connect(obj, SIGNAL(valueChanged(int,QString,QString,unsigned int,quint64)), plots.values().first(), SLOT(appendData(int,QString,QString,unsigned int,quint64)));
        connect(obj, SIGNAL(valueChanged(int,QString,QString,quint64,quint64)), plots.values().first(), SLOT(appendData(int,QString,QString,quint64,quint64)));
        connect(obj, SIGNAL(valueChanged(int,QString,QString,qint64,quint64)), plots.values().first(), SLOT(appendData(int,QString,QString,qint64,quint64)));
        connect(obj, SIGNAL(valueChanged(int,QString,QString,double,quint64)), plots.values().first(), SLOT(appendData(int,QString,QString,double,quint64)));
    }
}