Skip to content
ParameterEditorController.cc 5.95 KiB
Newer Older
/****************************************************************************
 *
 *   (c) 2009-2016 QGROUNDCONTROL PROJECT <http://www.qgroundcontrol.org>
 *
 * QGroundControl is licensed according to the terms in the file
 * COPYING.md in the root of the source code directory.
 *
 ****************************************************************************/

Don Gagne's avatar
Don Gagne committed

/// @file
///     @author Don Gagne <don@thegagnes.com>

#include "ParameterEditorController.h"
#include "QGCApplication.h"
#include "ParameterManager.h"
#include "SettingsManager.h"
#include "AppSettings.h"
Don Gagne's avatar
Don Gagne committed

#ifndef __mobile__
#include "QGCMapRCToParamDialog.h"
Don Gagne's avatar
Don Gagne committed
#include "MainWindow.h"
Don Gagne's avatar
Don Gagne committed
#include <QStandardPaths>

Don Gagne's avatar
Don Gagne committed
/// @Brief Constructs a new ParameterEditorController Widget. This widget is used within the PX4VehicleConfig set of screens.
Don Gagne's avatar
Don Gagne committed
ParameterEditorController::ParameterEditorController(void)
    : _currentComponentId(_vehicle->defaultComponentId())
    , _parameters(new QmlObjectListModel(this))
Don Gagne's avatar
Don Gagne committed
{
    const QMap<int, QMap<QString, QStringList> >& groupMap = _vehicle->parameterManager()->getGroupMap();
    foreach (int componentId, groupMap.keys()) {
        _componentIds += QString("%1").arg(componentId);
    // Be careful about no parameters
    if (groupMap.contains(_currentComponentId) && groupMap[_currentComponentId].size() != 0) {
        _currentGroup = groupMap[_currentComponentId].keys()[0];
    }
    _updateParameters();

    connect(this, &ParameterEditorController::searchTextChanged, this, &ParameterEditorController::_updateParameters);
    connect(this, &ParameterEditorController::currentComponentIdChanged, this, &ParameterEditorController::_updateParameters);
    connect(this, &ParameterEditorController::currentGroupChanged, this, &ParameterEditorController::_updateParameters);
Don Gagne's avatar
Don Gagne committed
}

ParameterEditorController::~ParameterEditorController()
{
    
}

Don Gagne's avatar
Don Gagne committed
QStringList ParameterEditorController::getGroupsForComponent(int componentId)
{
    const QMap<int, QMap<QString, QStringList> >& groupMap = _vehicle->parameterManager()->getGroupMap();
Don Gagne's avatar
Don Gagne committed

    return groupMap[componentId].keys();
Don Gagne's avatar
Don Gagne committed
}

Don Gagne's avatar
Don Gagne committed
QStringList ParameterEditorController::getParametersForGroup(int componentId, QString group)
Don Gagne's avatar
Don Gagne committed
{
    const QMap<int, QMap<QString, QStringList> >& groupMap = _vehicle->parameterManager()->getGroupMap();

    return groupMap[componentId][group];
Don Gagne's avatar
Don Gagne committed
}

Don Gagne's avatar
Don Gagne committed
QStringList ParameterEditorController::searchParametersForComponent(int componentId, const QString& searchText, bool searchInName, bool searchInDescriptions)
{
    QStringList list;
    
    foreach(const QString &paramName, _vehicle->parameterManager()->parameterNames(componentId)) {
Don Gagne's avatar
Don Gagne committed
        if (searchText.isEmpty()) {
            list += paramName;
        } else {
            Fact* fact = _vehicle->parameterManager()->getParameter(componentId, paramName);
Don Gagne's avatar
Don Gagne committed
            
            if (searchInName && fact->name().contains(searchText, Qt::CaseInsensitive)) {
                list += paramName;
            } else if (searchInDescriptions && (fact->shortDescription().contains(searchText, Qt::CaseInsensitive) || fact->longDescription().contains(searchText, Qt::CaseInsensitive))) {
                list += paramName;
            }
        }
    }
    list.sort();
    
    return list;
}

Don Gagne's avatar
Don Gagne committed
void ParameterEditorController::clearRCToParam(void)
{
    Q_ASSERT(_uas);
    _uas->unsetRCToParameterMap();
Don Gagne's avatar
Don Gagne committed
}

Don Gagne's avatar
Don Gagne committed
void ParameterEditorController::saveToFile(const QString& filename)
Don Gagne's avatar
Don Gagne committed
{
Don Gagne's avatar
Don Gagne committed
    if (!filename.isEmpty()) {
        QFile file(filename);
Don Gagne's avatar
Don Gagne committed
        
        if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) {
Don Gagne's avatar
Don Gagne committed
            qgcApp()->showMessage(QString("Unable to create file: %1").arg(filename));
Don Gagne's avatar
Don Gagne committed
        
        QTextStream stream(&file);
        _vehicle->parameterManager()->writeParametersToStream(stream);
        file.close();
    }
Don Gagne's avatar
Don Gagne committed
}

Don Gagne's avatar
Don Gagne committed
void ParameterEditorController::loadFromFile(const QString& filename)
{
    QString errors;
    
Don Gagne's avatar
Don Gagne committed
    if (!filename.isEmpty()) {
        QFile file(filename);
Don Gagne's avatar
Don Gagne committed
        
        if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
Don Gagne's avatar
Don Gagne committed
            qgcApp()->showMessage(QString("Unable to open file: %1").arg(filename));
Don Gagne's avatar
Don Gagne committed
            return;
        }
        
        QTextStream stream(&file);
        errors = _vehicle->parameterManager()->readParametersFromStream(stream);
Don Gagne's avatar
Don Gagne committed
        file.close();
        
        if (!errors.isEmpty()) {
            emit showErrorMessage(errors);
        }
Don Gagne's avatar
Don Gagne committed
    }
Don Gagne's avatar
Don Gagne committed
void ParameterEditorController::refresh(void)
{
    _vehicle->parameterManager()->refreshAllParameters();
Don Gagne's avatar
Don Gagne committed
}

void ParameterEditorController::resetAllToDefaults(void)
{
    _vehicle->parameterManager()->resetAllParametersToDefaults();
Don Gagne's avatar
Don Gagne committed
void ParameterEditorController::setRCToParam(const QString& paramName)
{
#ifdef __mobile__
    Q_UNUSED(paramName)
#else
    Q_ASSERT(_uas);
    QGCMapRCToParamDialog * d = new QGCMapRCToParamDialog(paramName, _uas, qgcApp()->toolbox()->multiVehicleManager(), MainWindow::instance());
    d->exec();
Don Gagne's avatar
Don Gagne committed
}

void ParameterEditorController::_updateParameters(void)
{
    QObjectList newParameterList;

    if (_searchText.isEmpty()) {
        const QMap<int, QMap<QString, QStringList> >& groupMap = _vehicle->parameterManager()->getGroupMap();
        foreach (const QString& parameter, groupMap[_currentComponentId][_currentGroup]) {
            newParameterList.append(_vehicle->parameterManager()->getParameter(_currentComponentId, parameter));
        foreach(const QString &parameter, _vehicle->parameterManager()->parameterNames(_vehicle->defaultComponentId())) {
            Fact* fact = _vehicle->parameterManager()->getParameter(_vehicle->defaultComponentId(), parameter);
            if (fact->name().contains(_searchText, Qt::CaseInsensitive) ||
                    fact->shortDescription().contains(_searchText, Qt::CaseInsensitive) ||
                    fact->longDescription().contains(_searchText, Qt::CaseInsensitive)) {
                newParameterList.append(fact);
            }
        }
    }

    _parameters->swapObjectList(newParameterList);
}