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

/// @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 40 41

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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