ParameterEditorController.cc 6.33 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"
17 18
#include "SettingsManager.h"
#include "AppSettings.h"
Don Gagne's avatar
Don Gagne committed
19

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

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

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

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

49 50 51 52 53
ParameterEditorController::~ParameterEditorController()
{
    
}

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

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

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

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

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

Don Gagne's avatar
Don Gagne committed
97
void ParameterEditorController::saveToFile(const QString& filename)
Don Gagne's avatar
Don Gagne committed
98
{
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 114
void ParameterEditorController::loadFromFile(const QString& filename)
{
115 116
    QString errors;
    
Don Gagne's avatar
Don Gagne committed
117 118
    if (!filename.isEmpty()) {
        QFile file(filename);
Don Gagne's avatar
Don Gagne committed
119 120
        
        if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
Don Gagne's avatar
Don Gagne committed
121
            qgcApp()->showMessage(QString("Unable to open file: %1").arg(filename));
Don Gagne's avatar
Don Gagne committed
122 123 124 125
            return;
        }
        
        QTextStream stream(&file);
126
        errors = _vehicle->parameterManager()->readParametersFromStream(stream);
Don Gagne's avatar
Don Gagne committed
127
        file.close();
128 129 130 131
        
        if (!errors.isEmpty()) {
            emit showErrorMessage(errors);
        }
Don Gagne's avatar
Don Gagne committed
132
    }
Don Gagne's avatar
Don Gagne committed
133 134
}

Don Gagne's avatar
Don Gagne committed
135 136
void ParameterEditorController::refresh(void)
{
137
    _vehicle->parameterManager()->refreshAllParameters();
Don Gagne's avatar
Don Gagne committed
138 139
}

140 141
void ParameterEditorController::resetAllToDefaults(void)
{
142
    _vehicle->parameterManager()->resetAllParametersToDefaults();
143 144 145
    refresh();
}

Don Gagne's avatar
Don Gagne committed
146 147
void ParameterEditorController::setRCToParam(const QString& paramName)
{
148 149 150
#ifdef __mobile__
    Q_UNUSED(paramName)
#else
DonLakeFlyer's avatar
DonLakeFlyer committed
151 152 153 154
    if (_uas) {
        QGCMapRCToParamDialog * d = new QGCMapRCToParamDialog(paramName, _uas, qgcApp()->toolbox()->multiVehicleManager(), MainWindow::instance());
        d->exec();
    }
155
#endif
Don Gagne's avatar
Don Gagne committed
156
}
157 158 159 160

void ParameterEditorController::_updateParameters(void)
{
    QObjectList newParameterList;
161
    QStringList searchItems = _searchText.split(' ', QString::SkipEmptyParts);
162

163
    if (searchItems.isEmpty()) {
164
        const QMap<int, QMap<QString, QStringList> >& groupMap = _vehicle->parameterManager()->getGroupMap();
165
        foreach (const QString& parameter, groupMap[_currentComponentId][_currentGroup]) {
166
            newParameterList.append(_vehicle->parameterManager()->getParameter(_currentComponentId, parameter));
167 168
        }
    } else {
169 170
        foreach(const QString &parameter, _vehicle->parameterManager()->parameterNames(_vehicle->defaultComponentId())) {
            Fact* fact = _vehicle->parameterManager()->getParameter(_vehicle->defaultComponentId(), parameter);
171 172 173 174 175 176 177 178 179 180 181
            bool matched = true;

            // all of the search items must match in order for the parameter to be added to the list
            for (const auto& searchItem : searchItems) {
                if (!fact->name().contains(searchItem, Qt::CaseInsensitive) &&
                        !fact->shortDescription().contains(searchItem, Qt::CaseInsensitive) &&
                        !fact->longDescription().contains(searchItem, Qt::CaseInsensitive)) {
                    matched = false;
                }
            }
            if (matched) {
182 183 184 185 186 187 188
                newParameterList.append(fact);
            }
        }
    }

    _parameters->swapObjectList(newParameterList);
}