1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
/****************************************************************************
*
* (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.
*
****************************************************************************/
/// @file
/// @author Don Gagne <don@thegagnes.com>
#include "ParameterEditorController.h"
#include "QGCApplication.h"
#include "ParameterManager.h"
#include "SettingsManager.h"
#include "AppSettings.h"
#ifndef __mobile__
#include "QGCQFileDialog.h"
#include "QGCMapRCToParamDialog.h"
#include "MainWindow.h"
#endif
#include <QStandardPaths>
/// @Brief Constructs a new ParameterEditorController Widget. This widget is used within the PX4VehicleConfig set of screens.
ParameterEditorController::ParameterEditorController(void)
: _currentComponentId(_vehicle->defaultComponentId())
, _parameters(new QmlObjectListModel(this))
{
const QMap<int, QMap<QString, QStringList> >& groupMap = _vehicle->parameterManager()->getGroupMap();
foreach (int componentId, groupMap.keys()) {
_componentIds += QString("%1").arg(componentId);
}
// Be careful about no parameters
if (groupMap.contains(_currentComponentId) && groupMap[_currentComponentId].size() != 0) {
_currentGroup = groupMap[_currentComponentId].keys()[0];
}
_updateParameters();
connect(this, &ParameterEditorController::searchTextChanged, this, &ParameterEditorController::_updateParameters);
connect(this, &ParameterEditorController::currentComponentIdChanged, this, &ParameterEditorController::_updateParameters);
connect(this, &ParameterEditorController::currentGroupChanged, this, &ParameterEditorController::_updateParameters);
}
ParameterEditorController::~ParameterEditorController()
{
}
QStringList ParameterEditorController::getGroupsForComponent(int componentId)
{
const QMap<int, QMap<QString, QStringList> >& groupMap = _vehicle->parameterManager()->getGroupMap();
return groupMap[componentId].keys();
}
QStringList ParameterEditorController::getParametersForGroup(int componentId, QString group)
{
const QMap<int, QMap<QString, QStringList> >& groupMap = _vehicle->parameterManager()->getGroupMap();
return groupMap[componentId][group];
}
QStringList ParameterEditorController::searchParametersForComponent(int componentId, const QString& searchText, bool searchInName, bool searchInDescriptions)
{
QStringList list;
foreach(const QString ¶mName, _vehicle->parameterManager()->parameterNames(componentId)) {
if (searchText.isEmpty()) {
list += paramName;
} else {
Fact* fact = _vehicle->parameterManager()->getParameter(componentId, paramName);
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;
}
void ParameterEditorController::clearRCToParam(void)
{
if (_uas) {
_uas->unsetRCToParameterMap();
}
}
void ParameterEditorController::saveToFile(const QString& filename)
{
if (!filename.isEmpty()) {
QFile file(filename);
if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) {
qgcApp()->showMessage(QString("Unable to create file: %1").arg(filename));
return;
}
QTextStream stream(&file);
_vehicle->parameterManager()->writeParametersToStream(stream);
file.close();
}
}
void ParameterEditorController::loadFromFile(const QString& filename)
{
QString errors;
if (!filename.isEmpty()) {
QFile file(filename);
if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
qgcApp()->showMessage(QString("Unable to open file: %1").arg(filename));
return;
}
QTextStream stream(&file);
errors = _vehicle->parameterManager()->readParametersFromStream(stream);
file.close();
if (!errors.isEmpty()) {
emit showErrorMessage(errors);
}
}
}
void ParameterEditorController::refresh(void)
{
_vehicle->parameterManager()->refreshAllParameters();
}
void ParameterEditorController::resetAllToDefaults(void)
{
_vehicle->parameterManager()->resetAllParametersToDefaults();
refresh();
}
void ParameterEditorController::setRCToParam(const QString& paramName)
{
#ifdef __mobile__
Q_UNUSED(paramName)
#else
if (_uas) {
QGCMapRCToParamDialog * d = new QGCMapRCToParamDialog(paramName, _uas, qgcApp()->toolbox()->multiVehicleManager(), MainWindow::instance());
d->exec();
}
#endif
}
void ParameterEditorController::_updateParameters(void)
{
QObjectList newParameterList;
QStringList searchItems = _searchText.split(' ', QString::SkipEmptyParts);
if (searchItems.isEmpty()) {
const QMap<int, QMap<QString, QStringList> >& groupMap = _vehicle->parameterManager()->getGroupMap();
foreach (const QString& parameter, groupMap[_currentComponentId][_currentGroup]) {
newParameterList.append(_vehicle->parameterManager()->getParameter(_currentComponentId, parameter));
}
} else {
foreach(const QString ¶meter, _vehicle->parameterManager()->parameterNames(_vehicle->defaultComponentId())) {
Fact* fact = _vehicle->parameterManager()->getParameter(_vehicle->defaultComponentId(), parameter);
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) {
newParameterList.append(fact);
}
}
}
_parameters->swapObjectList(newParameterList);
}