ParameterEditorController.cc 7.15 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

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

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

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

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

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

36 37 38 39
    // Be careful about no parameters
    if (groupMap.contains(_currentComponentId) && groupMap[_currentComponentId].size() != 0) {
        _currentGroup = groupMap[_currentComponentId].keys()[0];
    }
40 41 42 43 44
    _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
45 46
}

47 48 49 50 51
ParameterEditorController::~ParameterEditorController()
{
    
}

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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