Skip to content
Linecharts.cc 2.69 KiB
Newer Older
lm's avatar
lm committed
#include "UASManager.h"
Linecharts::Linecharts(QWidget *parent) :
        QStackedWidget(parent),
        plots(),
        active(true)
{
lm's avatar
lm committed
  // 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)));
  connect(this, SIGNAL(logfileWritten(QString)),
          MainWindow::instance(), SLOT(loadDataView(QString)));
void Linecharts::showEvent(QShowEvent* event)
    // React only to internal (pre-display)
    // events
    if (!event->spontaneous())
        QWidget* prevWidget = currentWidget();
        if (prevWidget)
            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);
                }
            }
        }
    }
}

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);
#ifndef MAVLINK_ENABLED_SLUGS
        connect(uas, SIGNAL(valueChanged(int,QString,double,quint64)), widget, SLOT(appendData(int,QString,double,quint64)));
        connect(widget, SIGNAL(logfileWritten(QString)), this, SIGNAL(logfileWritten(QString)));
        // Set system active if this is the only system
        if (active)
        {
            if (plots.size() == 1)
            {
                selectSystem(uas->getUASID());
            }
        }
    }
}