QGCBaseParamWidget.cc 3.49 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
#include "QGCBaseParamWidget.h"

#include <QFileDialog>
#include <QFile>
#include <QVariant>
#include <QTextStream>>

#include "QGCUASParamManager.h"
#include "UASInterface.h"


QGCBaseParamWidget::QGCBaseParamWidget(QWidget *parent) :
    QWidget(parent),
    mav(NULL),
    paramMgr(NULL),
    updatingParamNameLock("")
{
}

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

void QGCBaseParamWidget::setUAS(UASInterface* uas)
{
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
    if (uas != mav) {
        if (mav) {
            //TODO disconnect any connections as needed
            disconnectViewSignalsAndSlots();
            disconnectFromParamManager();
            clearOnboardParamDisplay();
            clearPendingParamDisplay();
        }

        mav = uas;

        if (mav) {
            connectToParamManager();
            connectViewSignalsAndSlots();
            layoutWidget();

            paramMgr->requestParameterListIfEmpty();
        }
tstellanova's avatar
tstellanova committed
46
    }
47 48 49 50 51 52

}


void QGCBaseParamWidget::connectToParamManager()
{
53
    paramMgr = mav->getParamManager();
54 55
    //TODO route via paramManager instead?
    // Listen to updated param signals from the data model
56
    connect(paramMgr, SIGNAL(parameterUpdated(int, QString , QVariant )),
57 58
            this, SLOT(handleOnboardParamUpdate(int,QString,QVariant)));

59
    connect(paramMgr, SIGNAL(pendingParamUpdate(int , const QString&, QVariant , bool )),
60 61 62 63 64 65 66 67 68 69 70 71 72 73
            this, SLOT(handlePendingParamUpdate(int , const QString& ,  QVariant, bool )));

    // Listen for param list reload finished
    connect(paramMgr, SIGNAL(parameterListUpToDate()),
            this, SLOT(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()
{
74
    disconnect(paramMgr, SIGNAL(parameterUpdated(int, QString , QVariant )),
75 76
            this, SLOT(handleOnboardParamUpdate(int,QString,QVariant)));

77
    disconnect(paramMgr, SIGNAL(pendingParamUpdate(int , const QString&, QVariant , bool )),
78 79 80 81 82 83 84 85
            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 )));
86 87

    paramMgr = NULL;
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
}



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


void QGCBaseParamWidget::saveParametersToFile()
{
    if (!mav)
        return;
    QString fileName = QFileDialog::getSaveFileName(this, tr("Save File"), "./parameters.txt", tr("Parameter File (*.txt)"));
    QFile file(fileName);
    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 = QFileDialog::getOpenFileName(this, tr("Load File"), ".", tr("Parameter file (*.txt)"));
    QFile file(fileName);
    if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
        return;

    QTextStream in(&file);
    paramMgr->readPendingParamsFromStream(in);
    file.close();
}