ParameterEditorController.cc 6.56 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
    if (!filename.isEmpty()) {
Don Gagne's avatar
Don Gagne committed
101 102 103 104 105 106
        QString parameterFilename = filename;
        if (!QFileInfo(filename).fileName().contains(".")) {
            parameterFilename += QString(".%1").arg(AppSettings::parameterFileExtension);
        }

        QFile file(parameterFilename);
Don Gagne's avatar
Don Gagne committed
107
        
108
        if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) {
Don Gagne's avatar
Don Gagne committed
109
            qgcApp()->showMessage(tr("Unable to create file: %1").arg(parameterFilename));
110 111
            return;
        }
Don Gagne's avatar
Don Gagne committed
112
        
113
        QTextStream stream(&file);
114
        _vehicle->parameterManager()->writeParametersToStream(stream);
115 116
        file.close();
    }
Don Gagne's avatar
Don Gagne committed
117 118
}

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

Don Gagne's avatar
Don Gagne committed
141 142
void ParameterEditorController::refresh(void)
{
143
    _vehicle->parameterManager()->refreshAllParameters();
Don Gagne's avatar
Don Gagne committed
144 145
}

146 147
void ParameterEditorController::resetAllToDefaults(void)
{
148
    _vehicle->parameterManager()->resetAllParametersToDefaults();
149 150 151
    refresh();
}

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

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

169
    if (searchItems.isEmpty()) {
170 171 172
        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));
173 174
        }
    } else {
175 176
        foreach(const QString &parameter, _vehicle->parameterManager()->parameterNames(_vehicle->defaultComponentId())) {
            Fact* fact = _vehicle->parameterManager()->getParameter(_vehicle->defaultComponentId(), parameter);
177 178 179 180 181 182 183 184 185 186 187
            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) {
188 189 190 191 192 193 194
                newParameterList.append(fact);
            }
        }
    }

    _parameters->swapObjectList(newParameterList);
}