ParameterEditorController.cc 6.34 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
    : _currentCategory("Standard")  // FIXME: firmware specific
31
    , _parameters(new QmlObjectListModel(this))
Don Gagne's avatar
Don Gagne committed
32
{
33 34 35 36 37 38
    const QMap<QString, QMap<QString, QStringList> >& categoryMap = _vehicle->parameterManager()->getCategoryMap();
    _categories = categoryMap.keys();

    // Move default category to front
    _categories.removeOne(_currentCategory);
    _categories.prepend(_currentCategory);
39

40
    // Be careful about no parameters
41 42
    if (categoryMap.contains(_currentCategory) && categoryMap[_currentCategory].size() != 0) {
        _currentGroup = categoryMap[_currentCategory].keys()[0];
43
    }
44 45 46 47
    _updateParameters();

    connect(this, &ParameterEditorController::searchTextChanged, this, &ParameterEditorController::_updateParameters);
    connect(this, &ParameterEditorController::currentGroupChanged, this, &ParameterEditorController::_updateParameters);
Don Gagne's avatar
Don Gagne committed
48 49
}

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

55
QStringList ParameterEditorController::getGroupsForCategory(const QString& category)
Don Gagne's avatar
Don Gagne committed
56
{
57
    const QMap<QString, QMap<QString, QStringList> >& categoryMap = _vehicle->parameterManager()->getCategoryMap();
Don Gagne's avatar
Don Gagne committed
58

59
    return categoryMap[category].keys();
Don Gagne's avatar
Don Gagne committed
60 61
}

62
QStringList ParameterEditorController::getParametersForGroup(const QString& category, const QString& group)
Don Gagne's avatar
Don Gagne committed
63
{
64
    const QMap<QString, QMap<QString, QStringList> >& categoryMap = _vehicle->parameterManager()->getCategoryMap();
65

66
    return categoryMap[category][group];
Don Gagne's avatar
Don Gagne committed
67 68
}

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

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

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

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

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

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

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

164
    if (searchItems.isEmpty()) {
165 166 167
        const QMap<QString, QMap<QString, QStringList> >& categoryMap = _vehicle->parameterManager()->getCategoryMap();
        foreach (const QString& parameter, categoryMap[_currentCategory][_currentGroup]) {
            newParameterList.append(_vehicle->parameterManager()->getParameter(_vehicle->defaultComponentId(), parameter));
168 169
        }
    } else {
170 171
        foreach(const QString &parameter, _vehicle->parameterManager()->parameterNames(_vehicle->defaultComponentId())) {
            Fact* fact = _vehicle->parameterManager()->getParameter(_vehicle->defaultComponentId(), parameter);
172 173 174 175 176 177 178 179 180 181 182
            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) {
183 184 185 186 187 188 189
                newParameterList.append(fact);
            }
        }
    }

    _parameters->swapObjectList(newParameterList);
}