ParameterEditorController.cc 7.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
#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 26
    : _currentCategory          ("Standard")  // FIXME: firmware specific
    , _parameters               (new QmlObjectListModel(this))
    , _parameterMgr             (_vehicle->parameterManager())
    , _componentCategoryPrefix  (tr("Component "))
27
    , _showModifiedOnly          (false)
Don Gagne's avatar
Don Gagne committed
28
{
29
    const QMap<QString, QMap<QString, QStringList> >& categoryMap = _parameterMgr->getDefaultComponentCategoryMap();
30 31 32 33 34
    _categories = categoryMap.keys();

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

36 37 38 39 40 41 42
    // There is a category for each non default component
    for (int compId: _parameterMgr->componentIds()) {
        if (compId != _vehicle->defaultComponentId()) {
            _categories.append(QString("%1%2").arg(_componentCategoryPrefix).arg(compId));
        }
    }

43
    // Be careful about no parameters
44 45
    if (categoryMap.contains(_currentCategory) && categoryMap[_currentCategory].size() != 0) {
        _currentGroup = categoryMap[_currentCategory].keys()[0];
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 65
    if (category.startsWith(_componentCategoryPrefix)) {
        return QStringList(tr("All"));
    } else {
        const QMap<QString, QMap<QString, QStringList> >& categoryMap = _parameterMgr->getDefaultComponentCategoryMap();
66

67 68
        return categoryMap[category].keys();
    }
Don Gagne's avatar
Don Gagne committed
69 70
}

71
QStringList ParameterEditorController::searchParameters(const QString& searchText, bool searchInName, bool searchInDescriptions)
Don Gagne's avatar
Don Gagne committed
72 73
{
    QStringList list;
74

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

Don Gagne's avatar
Don Gagne committed
81 82 83 84 85 86 87 88
            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();
89

Don Gagne's avatar
Don Gagne committed
90 91 92
    return list;
}

Don Gagne's avatar
Don Gagne committed
93 94
void ParameterEditorController::clearRCToParam(void)
{
DonLakeFlyer's avatar
DonLakeFlyer committed
95 96 97
    if (_uas) {
        _uas->unsetRCToParameterMap();
    }
Don Gagne's avatar
Don Gagne committed
98 99
}

Don Gagne's avatar
Don Gagne committed
100
void ParameterEditorController::saveToFile(const QString& filename)
Don Gagne's avatar
Don Gagne committed
101
{
Don Gagne's avatar
Don Gagne committed
102
    if (!filename.isEmpty()) {
Don Gagne's avatar
Don Gagne committed
103 104 105 106 107 108
        QString parameterFilename = filename;
        if (!QFileInfo(filename).fileName().contains(".")) {
            parameterFilename += QString(".%1").arg(AppSettings::parameterFileExtension);
        }

        QFile file(parameterFilename);
109

110
        if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) {
Don Gagne's avatar
Don Gagne committed
111
            qgcApp()->showMessage(tr("Unable to create file: %1").arg(parameterFilename));
112 113
            return;
        }
114

115
        QTextStream stream(&file);
116
        _parameterMgr->writeParametersToStream(stream);
117 118
        file.close();
    }
Don Gagne's avatar
Don Gagne committed
119 120
}

Don Gagne's avatar
Don Gagne committed
121 122
void ParameterEditorController::loadFromFile(const QString& filename)
{
123
    QString errors;
124

Don Gagne's avatar
Don Gagne committed
125 126
    if (!filename.isEmpty()) {
        QFile file(filename);
127

Don Gagne's avatar
Don Gagne committed
128
        if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
129
            qgcApp()->showMessage(tr("Unable to open file: %1").arg(filename));
Don Gagne's avatar
Don Gagne committed
130 131
            return;
        }
132

Don Gagne's avatar
Don Gagne committed
133
        QTextStream stream(&file);
134
        errors = _parameterMgr->readParametersFromStream(stream);
Don Gagne's avatar
Don Gagne committed
135
        file.close();
136

137 138 139
        if (!errors.isEmpty()) {
            emit showErrorMessage(errors);
        }
Don Gagne's avatar
Don Gagne committed
140
    }
Don Gagne's avatar
Don Gagne committed
141 142
}

Don Gagne's avatar
Don Gagne committed
143 144
void ParameterEditorController::refresh(void)
{
145
    _parameterMgr->refreshAllParameters();
Don Gagne's avatar
Don Gagne committed
146 147
}

148 149
void ParameterEditorController::resetAllToDefaults(void)
{
150
    _parameterMgr->resetAllParametersToDefaults();
151 152 153
    refresh();
}

154 155 156 157 158 159
void ParameterEditorController::resetAllToVehicleConfiguration(void)
{
    _parameterMgr->resetAllToVehicleConfiguration();
    refresh();
}

Don Gagne's avatar
Don Gagne committed
160 161
void ParameterEditorController::setRCToParam(const QString& paramName)
{
162 163 164
#ifdef __mobile__
    Q_UNUSED(paramName)
#else
DonLakeFlyer's avatar
DonLakeFlyer committed
165
    if (_uas) {
Gus Grubba's avatar
Gus Grubba committed
166 167 168
        Q_UNUSED(paramName)
        //-- TODO QGCMapRCToParamDialog * d = new QGCMapRCToParamDialog(paramName, _uas, qgcApp()->toolbox()->multiVehicleManager(), MainWindow::instance());
        //d->exec();
DonLakeFlyer's avatar
DonLakeFlyer committed
169
    }
170
#endif
Don Gagne's avatar
Don Gagne committed
171
}
172

173 174 175 176 177 178
bool ParameterEditorController::_shouldShow(Fact* fact)
{
    bool show = _showModifiedOnly ? (fact->defaultValueAvailable() ? (fact->valueEqualsDefault() ? false : true) : false) : true;
    return show;
}

179 180 181
void ParameterEditorController::_updateParameters(void)
{
    QObjectList newParameterList;
182
    QStringList searchItems = _searchText.split(' ', QString::SkipEmptyParts);
183

184
    if (searchItems.isEmpty() && !_showModifiedOnly) {
185 186 187 188 189 190 191 192 193 194 195
        if (_currentCategory.startsWith(_componentCategoryPrefix)) {
            int compId = _currentCategory.right(_currentCategory.length() - _componentCategoryPrefix.length()).toInt();
            for (const QString& paramName: _parameterMgr->parameterNames(compId)) {
                newParameterList.append(_parameterMgr->getParameter(compId, paramName));
            }

        } else {
            const QMap<QString, QMap<QString, QStringList> >& categoryMap = _parameterMgr->getDefaultComponentCategoryMap();
            for (const QString& parameter: categoryMap[_currentCategory][_currentGroup]) {
                newParameterList.append(_parameterMgr->getParameter(_vehicle->defaultComponentId(), parameter));
            }
196 197
        }
    } else {
198 199
        for(const QString &parameter: _parameterMgr->parameterNames(_vehicle->defaultComponentId())) {
            Fact* fact = _parameterMgr->getParameter(_vehicle->defaultComponentId(), parameter);
200 201 202 203 204
            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) &&
205 206
                        !fact->shortDescription().contains(searchItem, Qt::CaseInsensitive) &&
                        !fact->longDescription().contains(searchItem, Qt::CaseInsensitive)) {
207 208
                        matched = false;
                    }
209 210 211
                }
            }
            if (matched) {
212 213 214 215 216 217 218
                newParameterList.append(fact);
            }
        }
    }

    _parameters->swapObjectList(newParameterList);
}