ParameterEditorController.cc 7.1 KB
Newer Older
1 2
/****************************************************************************
 *
Gus Grubba's avatar
Gus Grubba committed
3
 * (c) 2009-2020 QGROUNDCONTROL PROJECT <http://www.qgroundcontrol.org>
4 5 6 7 8 9
 *
 * 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
#include "ParameterEditorController.h"
11
#include "QGCApplication.h"
12
#include "ParameterManager.h"
13 14
#include "SettingsManager.h"
#include "AppSettings.h"
Don Gagne's avatar
Don Gagne committed
15

16 17 18 19
#ifndef __mobile__
#include "QGCMapRCToParamDialog.h"
#endif

Don Gagne's avatar
Don Gagne committed
20 21
#include <QStandardPaths>

Don Gagne's avatar
Don Gagne committed
22
ParameterEditorController::ParameterEditorController(void)
23 24 25
    : _currentCategory          ("Standard")  // FIXME: firmware specific
    , _parameters               (new QmlObjectListModel(this))
    , _parameterMgr             (_vehicle->parameterManager())
26
    , _showModifiedOnly          (false)
Don Gagne's avatar
Don Gagne committed
27
{
28
    const QMap<QString, QMap<QString, QStringList> >& categoryMap = _parameterMgr->getComponentCategoryMap(_vehicle->defaultComponentId());
29 30 31 32 33
    _categories = categoryMap.keys();

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

35 36 37
    // There is a category for each non default component
    for (int compId: _parameterMgr->componentIds()) {
        if (compId != _vehicle->defaultComponentId()) {
38
            _categories.append(_parameterMgr->getComponentCategory(compId));
39 40 41
        }
    }

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

47 48
    _updateParameters();

49 50 51
    connect(this, &ParameterEditorController::searchTextChanged,        this, &ParameterEditorController::_updateParameters);
    connect(this, &ParameterEditorController::currentCategoryChanged,   this, &ParameterEditorController::_updateParameters);
    connect(this, &ParameterEditorController::currentGroupChanged,      this, &ParameterEditorController::_updateParameters);
52
    connect(this, &ParameterEditorController::showModifiedOnlyChanged,  this, &ParameterEditorController::_updateParameters);
Don Gagne's avatar
Don Gagne committed
53 54
}

55 56
ParameterEditorController::~ParameterEditorController()
{
57

58 59
}

60
QStringList ParameterEditorController::getGroupsForCategory(const QString& category)
Don Gagne's avatar
Don Gagne committed
61
{
62 63 64
    int compId = _parameterMgr->getComponentId(category);
    const QMap<QString, QMap<QString, QStringList> >& categoryMap = _parameterMgr->getComponentCategoryMap(compId);
    return categoryMap[category].keys();
Don Gagne's avatar
Don Gagne committed
65 66
}

67
QStringList ParameterEditorController::searchParameters(const QString& searchText, bool searchInName, bool searchInDescriptions)
Don Gagne's avatar
Don Gagne committed
68 69
{
    QStringList list;
70

71
    for(const QString &paramName: _parameterMgr->parameterNames(_vehicle->defaultComponentId())) {
Don Gagne's avatar
Don Gagne committed
72 73 74
        if (searchText.isEmpty()) {
            list += paramName;
        } else {
75
            Fact* fact = _parameterMgr->getParameter(_vehicle->defaultComponentId(), paramName);
76

Don Gagne's avatar
Don Gagne committed
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();
85

Don Gagne's avatar
Don Gagne committed
86 87 88
    return list;
}

Don Gagne's avatar
Don Gagne committed
89
void ParameterEditorController::saveToFile(const QString& filename)
Don Gagne's avatar
Don Gagne committed
90
{
Don Gagne's avatar
Don Gagne committed
91
    if (!filename.isEmpty()) {
Don Gagne's avatar
Don Gagne committed
92 93 94 95 96 97
        QString parameterFilename = filename;
        if (!QFileInfo(filename).fileName().contains(".")) {
            parameterFilename += QString(".%1").arg(AppSettings::parameterFileExtension);
        }

        QFile file(parameterFilename);
98

99
        if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) {
100
            qgcApp()->showAppMessage(tr("Unable to create file: %1").arg(parameterFilename));
101 102
            return;
        }
103

104
        QTextStream stream(&file);
105
        _parameterMgr->writeParametersToStream(stream);
106 107
        file.close();
    }
Don Gagne's avatar
Don Gagne committed
108 109
}

Don Gagne's avatar
Don Gagne committed
110 111
void ParameterEditorController::loadFromFile(const QString& filename)
{
112
    QString errors;
113

Don Gagne's avatar
Don Gagne committed
114 115
    if (!filename.isEmpty()) {
        QFile file(filename);
116

Don Gagne's avatar
Don Gagne committed
117
        if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
118
            qgcApp()->showAppMessage(tr("Unable to open file: %1").arg(filename));
Don Gagne's avatar
Don Gagne committed
119 120
            return;
        }
121

Don Gagne's avatar
Don Gagne committed
122
        QTextStream stream(&file);
123
        errors = _parameterMgr->readParametersFromStream(stream);
Don Gagne's avatar
Don Gagne committed
124
        file.close();
125

126 127 128
        if (!errors.isEmpty()) {
            emit showErrorMessage(errors);
        }
Don Gagne's avatar
Don Gagne committed
129
    }
Don Gagne's avatar
Don Gagne committed
130 131
}

Don Gagne's avatar
Don Gagne committed
132 133
void ParameterEditorController::refresh(void)
{
134
    _parameterMgr->refreshAllParameters();
Don Gagne's avatar
Don Gagne committed
135 136
}

137 138
void ParameterEditorController::resetAllToDefaults(void)
{
139
    _parameterMgr->resetAllParametersToDefaults();
140 141 142
    refresh();
}

143 144 145 146 147 148
void ParameterEditorController::resetAllToVehicleConfiguration(void)
{
    _parameterMgr->resetAllToVehicleConfiguration();
    refresh();
}

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

162 163 164 165 166 167
bool ParameterEditorController::_shouldShow(Fact* fact)
{
    bool show = _showModifiedOnly ? (fact->defaultValueAvailable() ? (fact->valueEqualsDefault() ? false : true) : false) : true;
    return show;
}

168 169 170
void ParameterEditorController::_updateParameters(void)
{
    QObjectList newParameterList;
171 172

#if QT_VERSION < QT_VERSION_CHECK(5, 15, 0)
173
    QStringList searchItems = _searchText.split(' ', QString::SkipEmptyParts);
174 175 176
#else
    QStringList searchItems = _searchText.split(' ', Qt::SkipEmptyParts);
#endif
177

178
    if (searchItems.isEmpty() && !_showModifiedOnly) {
179 180 181 182
        int compId = _parameterMgr->getComponentId(_currentCategory);
        const QMap<QString, QMap<QString, QStringList> >& categoryMap = _parameterMgr->getComponentCategoryMap(compId);
        for (const QString& paramName: categoryMap[_currentCategory][_currentGroup]) {
            newParameterList.append(_parameterMgr->getParameter(compId, paramName));
183 184
        }
    } else {
185 186
        for(const QString &paraName: _parameterMgr->parameterNames(_vehicle->defaultComponentId())) {
            Fact* fact = _parameterMgr->getParameter(_vehicle->defaultComponentId(), paraName);
187 188 189 190 191
            bool matched = _shouldShow(fact);
            // All of the search items must match in order for the parameter to be added to the list
            if(matched) {
                for (const auto& searchItem : searchItems) {
                    if (!fact->name().contains(searchItem, Qt::CaseInsensitive) &&
192 193
                        !fact->shortDescription().contains(searchItem, Qt::CaseInsensitive) &&
                        !fact->longDescription().contains(searchItem, Qt::CaseInsensitive)) {
194 195
                        matched = false;
                    }
196 197 198
                }
            }
            if (matched) {
199 200 201 202 203 204 205
                newParameterList.append(fact);
            }
        }
    }

    _parameters->swapObjectList(newParameterList);
}