ParameterEditorController.cc 7.01 KB
Newer Older
1 2 3 4 5 6 7 8 9
/****************************************************************************
 *
 *   (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
10 11 12 13 14 15

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

#include "ParameterEditorController.h"
#include "AutoPilotPluginManager.h"
16
#include "QGCApplication.h"
17
#include "ParameterManager.h"
Don Gagne's avatar
Don Gagne committed
18

19 20 21
#ifndef __mobile__
#include "QGCFileDialog.h"
#include "QGCMapRCToParamDialog.h"
Don Gagne's avatar
Don Gagne committed
22
#include "MainWindow.h"
23 24
#endif

Don Gagne's avatar
Don Gagne committed
25 26
#include <QStandardPaths>

Don Gagne's avatar
Don Gagne committed
27
/// @Brief Constructs a new ParameterEditorController Widget. This widget is used within the PX4VehicleConfig set of screens.
Don Gagne's avatar
Don Gagne committed
28
ParameterEditorController::ParameterEditorController(void)
29 30
    : _currentComponentId(_vehicle->defaultComponentId())
    , _parameters(new QmlObjectListModel(this))
Don Gagne's avatar
Don Gagne committed
31
{
32
    const QMap<int, QMap<QString, QStringList> >& groupMap = _vehicle->parameterManager()->getGroupMap();
33 34
    foreach (int componentId, groupMap.keys()) {
        _componentIds += QString("%1").arg(componentId);
35
    }
36 37 38 39 40 41 42

    _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
43 44
}

45 46 47 48 49
ParameterEditorController::~ParameterEditorController()
{
    
}

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

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

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

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

Don Gagne's avatar
Don Gagne committed
64 65 66 67
QStringList ParameterEditorController::searchParametersForComponent(int componentId, const QString& searchText, bool searchInName, bool searchInDescriptions)
{
    QStringList list;
    
68
    foreach(const QString &paramName, _vehicle->parameterManager()->parameterNames(componentId)) {
Don Gagne's avatar
Don Gagne committed
69 70 71
        if (searchText.isEmpty()) {
            list += paramName;
        } else {
72
            Fact* fact = _vehicle->parameterManager()->getParameter(componentId, paramName);
Don Gagne's avatar
Don Gagne committed
73 74 75 76 77 78 79 80 81 82 83 84 85
            
            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
86 87
void ParameterEditorController::clearRCToParam(void)
{
88 89
    Q_ASSERT(_uas);
    _uas->unsetRCToParameterMap();
Don Gagne's avatar
Don Gagne committed
90 91
}

Don Gagne's avatar
Don Gagne committed
92
void ParameterEditorController::saveToFile(const QString& filename)
Don Gagne's avatar
Don Gagne committed
93
{
94 95 96 97 98
    if (!_autopilot) {
        qWarning() << "Internal error _autopilot==NULL";
        return;
    }

Don Gagne's avatar
Don Gagne committed
99 100
    if (!filename.isEmpty()) {
        QFile file(filename);
Don Gagne's avatar
Don Gagne committed
101
        
102
        if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) {
Don Gagne's avatar
Don Gagne committed
103
            qgcApp()->showMessage(QString("Unable to create file: %1").arg(filename));
104 105
            return;
        }
Don Gagne's avatar
Don Gagne committed
106
        
107
        QTextStream stream(&file);
108
        _vehicle->parameterManager()->writeParametersToStream(stream);
109 110
        file.close();
    }
Don Gagne's avatar
Don Gagne committed
111 112
}

Don Gagne's avatar
Don Gagne committed
113
void ParameterEditorController::saveToFilePicker(void)
Don Gagne's avatar
Don Gagne committed
114
{
115
#ifndef __mobile__
Don Gagne's avatar
Don Gagne committed
116 117 118 119 120 121 122 123 124 125 126 127
    QString fileName = QGCFileDialog::getSaveFileName(NULL,
                                                      "Save Parameters",
                                                      QStandardPaths::writableLocation(QStandardPaths::DocumentsLocation),
                                                      "Parameter Files (*.params)",
                                                      "params",
                                                      true);
    saveToFile(fileName);
#endif
}

void ParameterEditorController::loadFromFile(const QString& filename)
{
128 129
    QString errors;
    
130 131 132 133 134
    if (!_autopilot) {
        qWarning() << "Internal error _autopilot==NULL";
        return;
    }

Don Gagne's avatar
Don Gagne committed
135 136
    if (!filename.isEmpty()) {
        QFile file(filename);
Don Gagne's avatar
Don Gagne committed
137 138
        
        if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
Don Gagne's avatar
Don Gagne committed
139
            qgcApp()->showMessage(QString("Unable to open file: %1").arg(filename));
Don Gagne's avatar
Don Gagne committed
140 141 142 143
            return;
        }
        
        QTextStream stream(&file);
144
        errors = _vehicle->parameterManager()->readParametersFromStream(stream);
Don Gagne's avatar
Don Gagne committed
145
        file.close();
146 147 148 149
        
        if (!errors.isEmpty()) {
            emit showErrorMessage(errors);
        }
Don Gagne's avatar
Don Gagne committed
150
    }
Don Gagne's avatar
Don Gagne committed
151 152 153 154 155 156 157 158 159 160
}

void ParameterEditorController::loadFromFilePicker(void)
{
#ifndef __mobile__
    QString fileName = QGCFileDialog::getOpenFileName(NULL,
                                                      "Load Parameters",
                                                      QStandardPaths::writableLocation(QStandardPaths::DocumentsLocation),
                                                      "Parameter Files (*.params);;All Files (*)");
    loadFromFile(fileName);
161
#endif
Don Gagne's avatar
Don Gagne committed
162 163 164 165
}

void ParameterEditorController::refresh(void)
{
166
    _vehicle->parameterManager()->refreshAllParameters();
Don Gagne's avatar
Don Gagne committed
167 168
}

169 170
void ParameterEditorController::resetAllToDefaults(void)
{
171
    _vehicle->parameterManager()->resetAllParametersToDefaults();
172 173 174
    refresh();
}

Don Gagne's avatar
Don Gagne committed
175 176
void ParameterEditorController::setRCToParam(const QString& paramName)
{
177 178 179
#ifdef __mobile__
    Q_UNUSED(paramName)
#else
180
    Q_ASSERT(_uas);
181
    QGCMapRCToParamDialog * d = new QGCMapRCToParamDialog(paramName, _uas, qgcApp()->toolbox()->multiVehicleManager(), MainWindow::instance());
182
    d->exec();
183
#endif
Don Gagne's avatar
Don Gagne committed
184
}
185 186 187 188 189 190

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

    if (_searchText.isEmpty()) {
191
        const QMap<int, QMap<QString, QStringList> >& groupMap = _vehicle->parameterManager()->getGroupMap();
192
        foreach (const QString& parameter, groupMap[_currentComponentId][_currentGroup]) {
193
            newParameterList.append(_vehicle->parameterManager()->getParameter(_currentComponentId, parameter));
194 195
        }
    } else {
196 197
        foreach(const QString &parameter, _vehicle->parameterManager()->parameterNames(_vehicle->defaultComponentId())) {
            Fact* fact = _vehicle->parameterManager()->getParameter(_vehicle->defaultComponentId(), parameter);
198 199 200 201 202 203 204 205 206 207
            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);
}