Linecharts.cc 2.69 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 9 10 11 12
Linecharts::Linecharts(QWidget *parent) :
        QStackedWidget(parent),
        plots(),
        active(true)
{
13
  this->setVisible(false);
lm's avatar
lm committed
14 15 16 17 18 19 20 21 22 23 24 25
  // Get current MAV list
  QList<UASInterface*> systems = UASManager::instance()->getUASList();

  // Add each of them
  foreach (UASInterface* sys, systems)
  {
      addSystem(sys);
  }
  connect(UASManager::instance(), SIGNAL(UASCreated(UASInterface*)),
          this, SLOT(addSystem(UASInterface*)));
  connect(UASManager::instance(), SIGNAL(activeUASSet(int)),
          this, SLOT(selectSystem(int)));
26 27
  connect(this, SIGNAL(logfileWritten(QString)),
          MainWindow::instance(), SLOT(loadDataView(QString)));
28 29
}

30
void Linecharts::showEvent(QShowEvent* event)
31
{
32 33 34
    // React only to internal (pre-display)
    // events
    if (!event->spontaneous())
35
    {
36 37
        QWidget* prevWidget = currentWidget();
        if (prevWidget)
38
        {
39 40 41 42 43 44 45 46 47 48 49 50 51 52
            LinechartWidget* chart = dynamic_cast<LinechartWidget*>(prevWidget);
            if (chart)
            {
                if (event->type() == QEvent::Hide)
                {
                    this->active = false;
                    chart->setActive(false);
                }
                else if (event->type() == QEvent::Show)
                {
                    this->active = true;
                    chart->setActive(true);
                }
            }
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
        }
    }
}

void Linecharts::selectSystem(int systemid)
{
    QWidget* prevWidget = currentWidget();
    if (prevWidget)
    {
        LinechartWidget* chart = dynamic_cast<LinechartWidget*>(prevWidget);
        if (chart)
        {
            chart->setActive(false);
        }
    }
    QWidget* widget = plots.value(systemid, NULL);
    if (widget)
    {
        setCurrentWidget(widget);
        LinechartWidget* chart = dynamic_cast<LinechartWidget*>(widget);
        if (chart)
        {
            chart->setActive(true);
        }
    }
}

void Linecharts::addSystem(UASInterface* uas)
{
    if (!plots.contains(uas->getUASID()))
    {
        LinechartWidget* widget = new LinechartWidget(uas->getUASID(), this);
        addWidget(widget);
        plots.insert(uas->getUASID(), widget);
87
#ifndef MAVLINK_ENABLED_SLUGS
88
        connect(uas, SIGNAL(valueChanged(int,QString,double,quint64)), widget, SLOT(appendData(int,QString,double,quint64)));
89
#endif
90
        connect(widget, SIGNAL(logfileWritten(QString)), this, SIGNAL(logfileWritten(QString)));
91 92 93 94 95 96 97 98 99 100
        // Set system active if this is the only system
        if (active)
        {
            if (plots.size() == 1)
            {
                selectSystem(uas->getUASID());
            }
        }
    }
}