QGCBaseParamWidget.cc 4.08 KB
Newer Older
1 2 3 4
#include "QGCBaseParamWidget.h"

#include <QFile>
#include <QVariant>
5
#include <QTextStream>
6

7
#include "QGCUASParamManagerInterface.h"
8
#include "UASInterface.h"
Don Gagne's avatar
Don Gagne committed
9
#include "QGCApplication.h"
Don Gagne's avatar
Don Gagne committed
10
#include "QGCFileDialog.h"
11 12 13 14

QGCBaseParamWidget::QGCBaseParamWidget(QWidget *parent) :
    QWidget(parent),
    paramMgr(NULL),
Don Gagne's avatar
Don Gagne committed
15
    mav(NULL),
16 17 18 19 20 21 22 23 24 25 26 27
    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
    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
44
    }
45 46 47 48 49 50

}


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

57
    connect(paramMgr, SIGNAL(pendingParamUpdate(int , const QString&, QVariant , bool )),
58 59 60 61 62
            this, SLOT(handlePendingParamUpdate(int , const QString& ,  QVariant, bool )));

    // Listen for param list reload finished
    connect(paramMgr, SIGNAL(parameterListUpToDate()),
            this, SLOT(handleOnboardParameterListUpToDate()));
63 64 65
    if (paramMgr->parametersReady()) {
        handleOnboardParameterListUpToDate();
    }
66 67 68 69 70 71 72 73 74

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


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

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

    paramMgr = NULL;
89 90 91 92 93 94 95 96 97
}



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

98 99 100 101 102
void QGCBaseParamWidget::requestOnboardParamUpdate(QString parameterName)
{
    paramMgr->requestParameterUpdate(paramMgr->getDefaultComponentId(), parameterName);
}

103 104 105 106 107

void QGCBaseParamWidget::saveParametersToFile()
{
    if (!mav)
        return;
108
    QString fileName = QGCFileDialog::getSaveFileName(
109
        this, tr("Save Parameters"), qgcApp()->savedParameterFilesLocation(), tr("Parameter Files (*.params)"), "params", true);
110 111
    if (!fileName.isEmpty()) {
        QFile file(fileName);
112
        // TODO Display error message to the user if the file can't be created
113 114 115 116 117 118
        if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) {
            return;
        }
        QTextStream outstream(&file);
        paramMgr->writeOnboardParamsToStream(outstream,mav->getUASName());
        file.close();
119 120 121 122 123 124 125 126
    }
}


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