QGCBaseParamWidget.cc 3.79 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;
Don Gagne's avatar
Don Gagne committed
108
    QString fileName = QGCFileDialog::getSaveFileName(this, tr("Save File"), qgcApp()->savedParameterFilesLocation(), tr("Parameter File (*.txt)"));
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
    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;

Don Gagne's avatar
Don Gagne committed
125
    QString fileName = QGCFileDialog::getOpenFileName(this, tr("Load File"), qgcApp()->savedParameterFilesLocation(), tr("Parameter file (*.txt)"));
126 127 128 129 130 131 132 133 134 135
    QFile file(fileName);
    if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
        return;

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