Skip to content
QGCBaseParamWidget.cc 4.08 KiB
Newer Older
#include "QGCBaseParamWidget.h"

#include <QFile>
#include <QVariant>
#include "QGCUASParamManagerInterface.h"
Don Gagne's avatar
Don Gagne committed
#include "QGCApplication.h"
Don Gagne's avatar
Don Gagne committed
#include "QGCFileDialog.h"

QGCBaseParamWidget::QGCBaseParamWidget(QWidget *parent) :
    QWidget(parent),
    paramMgr(NULL),
Don Gagne's avatar
Don Gagne committed
    mav(NULL),
    updatingParamNameLock("")
{
}

QGCBaseParamWidget* QGCBaseParamWidget::initWithUAS(UASInterface *uas)
{
    setUAS(uas);
    return this;
}

void QGCBaseParamWidget::setUAS(UASInterface* uas)
{
    if (uas != mav) {
        if (mav) {
            //TODO disconnect any connections as needed
            disconnectViewSignalsAndSlots();
            disconnectFromParamManager();
            clearOnboardParamDisplay();
            clearPendingParamDisplay();
        }

        mav = uas;

        if (mav) {
            connectToParamManager();
            connectViewSignalsAndSlots();
            layoutWidget();
        }
tstellanova's avatar
tstellanova committed
    }

}


void QGCBaseParamWidget::connectToParamManager()
{
tstellanova's avatar
tstellanova committed
    paramMgr = mav->getParamManager();
    //TODO route via paramManager instead?
    // Listen to updated param signals from the data model
    connect(paramMgr, SIGNAL(parameterUpdated(int, QString , QVariant )),
            this, SLOT(handleOnboardParamUpdate(int,QString,QVariant)));

    connect(paramMgr, SIGNAL(pendingParamUpdate(int , const QString&, QVariant , bool )),
            this, SLOT(handlePendingParamUpdate(int , const QString& ,  QVariant, bool )));

    // Listen for param list reload finished
    connect(paramMgr, SIGNAL(parameterListUpToDate()),
            this, SLOT(handleOnboardParameterListUpToDate()));
    if (paramMgr->parametersReady()) {
        handleOnboardParameterListUpToDate();
    }

    // Listen to communications status messages so we can display them
    connect(paramMgr, SIGNAL(parameterStatusMsgUpdated(QString,int)),
            this, SLOT(handleParamStatusMsgUpdate(QString , int )));
}


void QGCBaseParamWidget::disconnectFromParamManager()
{
    disconnect(paramMgr, SIGNAL(parameterUpdated(int, QString , QVariant )),
            this, SLOT(handleOnboardParamUpdate(int,QString,QVariant)));

    disconnect(paramMgr, SIGNAL(pendingParamUpdate(int , const QString&, QVariant , bool )),
            this, SLOT(handlePendingParamUpdate(int , const QString& ,  QVariant, bool )));

    disconnect(paramMgr, SIGNAL(parameterListUpToDate()),
            this, SLOT(handleOnboardParameterListUpToDate()));

    // Listen to communications status messages so we can display them
    disconnect(paramMgr, SIGNAL(parameterStatusMsgUpdated(QString,int)),
            this, SLOT(handleParamStatusMsgUpdate(QString , int )));
tstellanova's avatar
tstellanova committed

    paramMgr = NULL;
}



void QGCBaseParamWidget::requestOnboardParamsUpdate()
{
    paramMgr->requestParameterList();
}

void QGCBaseParamWidget::requestOnboardParamUpdate(QString parameterName)
{
    paramMgr->requestParameterUpdate(paramMgr->getDefaultComponentId(), parameterName);
}


void QGCBaseParamWidget::saveParametersToFile()
{
    if (!mav)
        return;
    QString fileName = QGCFileDialog::getSaveFileName(
        this, tr("Save Parameters"), qgcApp()->savedParameterFilesLocation(), tr("Parameter Files (*.params)"), "params", true);
    if (!fileName.isEmpty()) {
        QFile file(fileName);
        // TODO Display error message to the user if the file can't be created
        if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) {
            return;
        }
        QTextStream outstream(&file);
        paramMgr->writeOnboardParamsToStream(outstream,mav->getUASName());
        file.close();
    }
}


void QGCBaseParamWidget::loadParametersFromFile()
{
    if (!mav)
        return;
    QString fileName = QGCFileDialog::getOpenFileName(
        this, tr("Load Parameters"), qgcApp()->savedParameterFilesLocation(),
        tr("Parameter Files (*.params);;All Files (*)"));
    // TODO Display error message to the user if the file can't be opened
    if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
    QTextStream in(&file);
    paramMgr->readPendingParamsFromStream(in);
    file.close();
}