ParameterEditorController.cc 5.95 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)
{
92 93
    Q_ASSERT(_uas);
    _uas->unsetRCToParameterMap();
Don Gagne's avatar
Don Gagne committed
94 95
}

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

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

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

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

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

    if (_searchText.isEmpty()) {
161
        const QMap<int, QMap<QString, QStringList> >& groupMap = _vehicle->parameterManager()->getGroupMap();
162
        foreach (const QString& parameter, groupMap[_currentComponentId][_currentGroup]) {
163
            newParameterList.append(_vehicle->parameterManager()->getParameter(_currentComponentId, parameter));
164 165
        }
    } else {
166 167
        foreach(const QString &parameter, _vehicle->parameterManager()->parameterNames(_vehicle->defaultComponentId())) {
            Fact* fact = _vehicle->parameterManager()->getParameter(_vehicle->defaultComponentId(), parameter);
168 169 170 171 172 173 174 175 176 177
            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);
}