QGroundControl
Ground Control Station for MAVLink Drones
Loading...
Searching...
No Matches
ParameterEditorController.cc
Go to the documentation of this file.
2
3#include <limits>
4#include <type_traits>
5
6#include "AppMessages.h"
7#include "AppSettings.h"
8#include "ParameterManager.h"
10#include "QmlObjectListModel.h"
11#include "SettingsManager.h"
12#include "Vehicle.h"
13
14QGC_LOGGING_CATEGORY(ParameterEditorControllerLog, "QMLControls.ParameterEditorController")
15
17 : QAbstractTableModel(parent)
18{
19
20}
21
26
27int ParameterTableModel::rowCount(const QModelIndex& /*parent*/) const
28{
29 return _tableData.count();
30}
31
32int ParameterTableModel::columnCount(const QModelIndex & /*parent*/) const
33{
34 return _tableViewColCount;
35}
36
37QVariant ParameterTableModel::data(const QModelIndex &index, int role) const
38{
39 if (!index.isValid()) {
40 return QVariant();
41 }
42
43 if (index.row() < 0 || index.row() >= _tableData.count()) {
44 return QVariant();
45 }
46 if (index.column() < 0 || index.column() >= _tableViewColCount) {
47 return QVariant();
48 }
49
50 switch (role) {
51 case Qt::DisplayRole:
52 return QVariant::fromValue(_tableData[index.row()][index.column()]);
53 case FactRole:
54 return QVariant::fromValue(_tableData[index.row()][ValueColumn]);
55 default:
56 return QVariant();
57 }
58}
59
60QVariant ParameterTableModel::headerData(int section, Qt::Orientation orientation, int role) const
61{
62 if (orientation != Qt::Horizontal || role != Qt::DisplayRole) {
63 return QVariant();
64 }
65
66 switch (section) {
67 case FavColumn: return tr("Fav");
68 case NameColumn: return tr("Name");
69 case ValueColumn: return tr("Value");
70 case DescriptionColumn: return tr("Description");
71 default: return QVariant();
72 }
73}
74
75QHash<int, QByteArray> ParameterTableModel::roleNames() const
76{
77 return {
78 {Qt::DisplayRole, "display"},
79 {FactRole, "fact"}
80 };
81}
82
84{
85 beginReset();
86 _tableData.clear();
87 endReset();
88}
89
91{
92 insert(rowCount(), fact);
93}
94
96{
97 if (row < 0 || row > rowCount()) {
98 qWarning() << "Invalid row row:rowCount" << row << rowCount() << Q_FUNC_INFO;
99 row = qMax(qMin(row, rowCount()), 0);
100 }
101
102 ColumnData colData(_tableViewColCount, QString());
103 colData[FavColumn] = QString();
104 colData[NameColumn] = fact->name();
105 colData[ValueColumn] = QVariant::fromValue(fact);
106 colData[DescriptionColumn] = fact->shortDescription();
107
108 if (!_isResetting()) {
109 beginInsertRows(QModelIndex(), row, row);
110 }
111 _tableData.insert(row, colData);
112 if (!_isResetting()) {
113 endInsertRows();
115 }
116}
117
119{
120 _resetNestingCount++;
121
122 if (_resetNestingCount == 1) {
123 beginResetModel();
124 }
125}
126
128{
129 if (_resetNestingCount == 0) {
130 qWarning() << "ParameterTableModel::endReset called without prior beginReset";
131 return;
132 }
133 _resetNestingCount--;
134 if (_resetNestingCount == 0) {
135 endResetModel();
137 }
138}
139
141{
142 if (row < 0 || row >= _tableData.count()) {
143 qWarning() << "Invalid row row:rowCount" << row << _tableData.count() << Q_FUNC_INFO;
144 return nullptr;
145 }
146
147 return _tableData[row][ValueColumn].value<Fact*>();
148}
149
150
152 : QObject(parent)
153{
154
155}
156
158 : FactPanelController(parent)
159 , _parameterMgr(_vehicle->parameterManager())
160{
161 // qCDebug(ParameterEditorControllerLog) << Q_FUNC_INFO << this;
162
163 _buildLists();
164
165 _searchTimer.setSingleShot(true);
166 _searchTimer.setInterval(300);
167
168 connect(this, &ParameterEditorController::currentCategoryChanged, this, &ParameterEditorController::_currentCategoryChanged);
169 connect(this, &ParameterEditorController::currentGroupChanged, this, &ParameterEditorController::_currentGroupChanged);
170 connect(this, &ParameterEditorController::searchTextChanged, this, &ParameterEditorController::_searchTextChanged);
171 connect(this, &ParameterEditorController::showModifiedOnlyChanged, this, &ParameterEditorController::_searchTextChanged);
172 connect(this, &ParameterEditorController::showFavoritesOnlyChanged, this, &ParameterEditorController::_searchTextChanged);
173 connect(this, &ParameterEditorController::hideReadOnlyChanged, this, &ParameterEditorController::_hideReadOnlyChanged);
174 connect(&_searchTimer, &QTimer::timeout, this, &ParameterEditorController::_performSearch);
175 connect(_parameterMgr, &ParameterManager::factAdded, this, &ParameterEditorController::_factAdded);
176
177 _loadFavorites();
178
179 ParameterEditorCategory* category = _categories.count() ? _categories.value<ParameterEditorCategory*>(0) : nullptr;
180 setCurrentCategory(category);
181}
182
184{
185 // qCDebug(ParameterEditorControllerLog) << Q_FUNC_INFO << this;
186}
187
188void ParameterEditorController::_buildListsForComponent(int compId)
189{
190 for (const QString& factName: _parameterMgr->parameterNames(compId)) {
191 Fact* fact = _parameterMgr->getParameter(compId, factName);
192
193 if (_hideReadOnly && fact->readOnly()) {
194 continue;
195 }
196
197 ParameterEditorCategory* category = nullptr;
198 if (_mapCategoryName2Category.contains(fact->category())) {
199 category = _mapCategoryName2Category[fact->category()];
200 } else {
201 category = new ParameterEditorCategory(this);
202 category->name = fact->category();
203 _mapCategoryName2Category[fact->category()] = category;
204 _categories.append(category);
205 }
206
207 ParameterEditorGroup* group = nullptr;
208 if (category->mapGroupName2Group.contains(fact->group())) {
209 group = category->mapGroupName2Group[fact->group()];
210 } else {
211 group = new ParameterEditorGroup(this);
212 group->componentId = compId;
213 group->name = fact->group();
214 category->mapGroupName2Group[fact->group()] = group;
215 category->groups.append(group);
216 }
217
218 group->facts.append(fact);
219 }
220}
221
222void ParameterEditorController::_buildLists(void)
223{
224 _currentCategory = nullptr;
225 _currentGroup = nullptr;
226 _parameters = nullptr;
227 _mapCategoryName2Category.clear();
228 _categories.clearAndDeleteContents();
229 emit parametersChanged();
230
231 // Autopilot component should always be first list
232 _buildListsForComponent(MAV_COMP_ID_AUTOPILOT1);
233
234 // "Standard" category should always be first
235 for (int i=0; i<_categories.count(); i++) {
236 ParameterEditorCategory* category = _categories.value<ParameterEditorCategory*>(i);
237 if (category->name == "Standard" && i != 0) {
238 _categories.removeAt(i);
239 _categories.insert(0, category);
240 break;
241 }
242 }
243
244 // Default category should always be last
245 for (int i=0; i<_categories.count(); i++) {
246 ParameterEditorCategory* category = _categories.value<ParameterEditorCategory*>(i);
247 if (category->name == FactMetaData::kDefaultCategory) {
248 if (i != _categories.count() - 1) {
249 _categories.removeAt(i);
250 _categories.append(category);
251 }
252 break;
253 }
254 }
255
256 // Now add other random components
257 for (int compId: _parameterMgr->componentIds()) {
258 if (compId != MAV_COMP_ID_AUTOPILOT1) {
259 _buildListsForComponent(compId);
260 }
261 }
262
263 // Default group should always be last
264 for (int i=0; i<_categories.count(); i++) {
265 ParameterEditorCategory* category = _categories.value<ParameterEditorCategory*>(i);
266 for (int j=0; j<category->groups.count(); j++) {
267 ParameterEditorGroup* group = category->groups.value<ParameterEditorGroup*>(j);
268 if (group->name == FactMetaData::kDefaultGroup) {
269 if (j != _categories.count() - 1) {
270 category->groups.removeAt(j);
271 category->groups.append(group);
272 }
273 break;
274 }
275 }
276 }
277}
278
279void ParameterEditorController::_factAdded(int compId, Fact* fact)
280{
281 if (_hideReadOnly && fact->readOnly()) {
282 return;
283 }
284
285 bool inserted = false;
286 ParameterEditorCategory* category = nullptr;
287
288 if (_mapCategoryName2Category.contains(fact->category())) {
289 category = _mapCategoryName2Category[fact->category()];
290 } else {
291 category = new ParameterEditorCategory(this);
292 category->name = fact->category();
293 _mapCategoryName2Category[fact->category()] = category;
294
295 // Insert in sorted order
296 inserted = false;
297 for (int i=0; i<_categories.count(); i++) {
298 if (_categories.value<ParameterEditorCategory*>(i)->name > category->name) {
299 _categories.insert(i, category);
300 inserted = true;
301 break;
302 }
303 }
304 if (!inserted) {
305 _categories.append(category);
306 }
307 }
308
309 ParameterEditorGroup* group = nullptr;
310 if (category->mapGroupName2Group.contains(fact->group())) {
311 group = category->mapGroupName2Group[fact->group()];
312 } else {
313 group = new ParameterEditorGroup(this);
314 group->componentId = compId;
315 group->name = fact->group();
316 category->mapGroupName2Group[fact->group()] = group;
317
318 // Insert in sorted order
319 QmlObjectListModel& groups = category->groups;
320 inserted = false;
321 for (int i=0; i<groups.count(); i++) {
322 if (groups.value<ParameterEditorGroup*>(i)->name > group->name) {
323 groups.insert(i, group);
324 inserted = true;
325 break;
326 }
327 }
328 if (!inserted) {
329 groups.append(group);
330 }
331 }
332
333 // Insert in sorted order
334 auto& facts = group->facts;
335 for (int i=0; i<facts.rowCount(); i++) {
336 if (facts.factAt(i)->name() > fact->name()) {
337 facts.insert(i, fact);
338 return;
339 }
340 }
341 facts.append(fact);
342}
343
344void ParameterEditorController::saveToFile(const QString& filename)
345{
346 if (!filename.isEmpty()) {
347 QString parameterFilename = filename;
348 if (!QFileInfo(filename).fileName().contains(".")) {
349 parameterFilename += QString(".%1").arg(AppSettings::parameterFileExtension);
350 }
351
352 QFile file(parameterFilename);
353
354 if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) {
355 qCWarning(ParameterEditorControllerLog) << "saveToFile: unable to create file" << parameterFilename;
356 QGC::showAppMessage(tr("Unable to create file: %1").arg(parameterFilename));
357 return;
358 }
359
360 qCDebug(ParameterEditorControllerLog) << "saveToFile:" << parameterFilename;
361 QTextStream stream(&file);
362 _parameterMgr->writeParametersToStream(stream);
363 file.close();
364 }
365}
366
367template <typename T, typename SignalFn>
368void ParameterEditorController::_setDiffProperty(T& member, const T& value, SignalFn changedSignal)
369{
370 if (member == value) {
371 return;
372 }
373 member = value;
374 emit (this->*changedSignal)(member);
375}
376
378{
379 _diffList.clearAndDeleteContents();
380 _setDiffProperty(_diffOtherVehicle, false, &ParameterEditorController::diffOtherVehicleChanged);
381 _setDiffProperty(_diffMultipleComponents, false, &ParameterEditorController::diffMultipleComponentsChanged);
382 _setDiffProperty(_diffParsedCount, 0, &ParameterEditorController::diffParsedCountChanged);
383 _setDiffProperty(_diffUnchangedCount, 0, &ParameterEditorController::diffUnchangedCountChanged);
384 _setDiffProperty(_diffReadOnlyCount, 0, &ParameterEditorController::diffReadOnlyCountChanged);
385 _setDiffProperty(_diffNoVehicleCount, 0, &ParameterEditorController::diffNoVehicleCountChanged);
386 _setDiffProperty(_diffSendableCount, 0, &ParameterEditorController::diffSendableCountChanged);
387 _setDiffProperty(_diffSelectedCount, 0, &ParameterEditorController::diffSelectedCountChanged);
388 _setDiffProperty(_diffMissingParams, QStringList(), &ParameterEditorController::diffMissingParamsChanged);
389 _setDiffProperty(_diffInvalidParams, QStringList(), &ParameterEditorController::diffInvalidParamsChanged);
390}
391
392// Recomputed whenever a diff row's load checkbox changes. Drives the dialog's Ok button enabled state.
393void ParameterEditorController::_updateDiffSelectedCount()
394{
395 int selectedCount = 0;
396 for (int i=0; i<_diffList.count(); i++) {
397 const ParameterEditorDiff* paramDiff = _diffList.value<ParameterEditorDiff*>(i);
398 if (paramDiff->load && !paramDiff->cannotSend) {
399 selectedCount++;
400 }
401 }
402 _setDiffProperty(_diffSelectedCount, selectedCount, &ParameterEditorController::diffSelectedCountChanged);
403}
404
406{
407 int sentCount = 0;
408 int uncheckedCount = 0;
409
410 for (int i=0; i<_diffList.count(); i++) {
411 ParameterEditorDiff* paramDiff = _diffList.value<ParameterEditorDiff*>(i);
412
413 if (paramDiff->cannotSend) {
414 qCDebug(ParameterEditorControllerLog) << "sendDiff: skipped (cannot send) -" << paramDiff->name;
415 continue;
416 }
417
418 if (paramDiff->load) {
419 sentCount++;
420 if (paramDiff->noVehicleValue) {
421 qCDebug(ParameterEditorControllerLog) << "sendDiff: PARAM_SET new param -" << paramDiff->name
422 << "componentId:" << paramDiff->componentId << "value:" << paramDiff->fileValueVar;
423 _parameterMgr->_mavlinkParamSet(paramDiff->componentId, paramDiff->name, paramDiff->valueType, paramDiff->fileValueVar);
424 } else {
425 qCDebug(ParameterEditorControllerLog) << "sendDiff: fact write -" << paramDiff->name
426 << "componentId:" << paramDiff->componentId << "value:" << paramDiff->fileValueVar;
427 Fact* fact = _parameterMgr->getParameter(paramDiff->componentId, paramDiff->name);
428 fact->setRawValue(paramDiff->fileValueVar);
429 }
430 } else {
431 uncheckedCount++;
432 qCDebug(ParameterEditorControllerLog) << "sendDiff: skipped (unchecked) -" << paramDiff->name;
433 }
434 }
435
436 qCDebug(ParameterEditorControllerLog) << "sendDiff summary - sent:" << sentCount << "unchecked:" << uncheckedCount;
437}
438
439namespace {
440
441// Parses the text with an explicit range check for T, since QVariant's string to int conversion wraps out of
442// range values. Also accepts whole-number floats (e.g. "-1.000000"), as written for parameters that have since
443// changed from float to integer. These are matched as text rather than parsed as double, which would round
444// large values.
445template <typename T>
446bool convertParamFileInteger(const FactMetaData& metaData, const QString& fileValueStr, QVariant& typedValue,
447 QString& errorString)
448{
449 using Wide = std::conditional_t<std::is_signed_v<T>, qint64, quint64>;
450
451 static const QRegularExpression wholeNumberFloatRegExp(QStringLiteral("^([+-]?\\d+)\\.0*$"));
452
453 QString integerStr = fileValueStr;
454 const QRegularExpressionMatch match = wholeNumberFloatRegExp.match(fileValueStr);
455 if (match.hasMatch()) {
456 integerStr = match.captured(1);
457 }
458
459 bool ok = false;
460 Wide value = 0;
461 if constexpr (std::is_signed_v<T>) {
462 value = integerStr.toLongLong(&ok);
463 } else {
464 value = integerStr.toULongLong(&ok);
465 }
466 if (!ok) {
467 errorString = QStringLiteral("Not a whole number within the parameter's range");
468 return false;
469 }
470
471 if ((value < static_cast<Wide>(std::numeric_limits<T>::min())) ||
472 (value > static_cast<Wide>(std::numeric_limits<T>::max()))) {
473 errorString = QStringLiteral("Out of range for the parameter's type");
474 return false;
475 }
476
477 // In range now, so this only produces the variant type the rest of the Fact system expects
478 return metaData.convertAndValidateRaw(QVariant::fromValue(value), true /* convertOnly */, typedValue, errorString);
479}
480
481} // namespace
482
483bool ParameterEditorController::_convertFileValue(const FactMetaData& metaData, const QString& fileValueStr,
484 QVariant& typedValue, QString& errorString)
485{
486 switch (metaData.type()) {
488 return convertParamFileInteger<qint8>(metaData, fileValueStr, typedValue, errorString);
490 return convertParamFileInteger<qint16>(metaData, fileValueStr, typedValue, errorString);
492 return convertParamFileInteger<qint32>(metaData, fileValueStr, typedValue, errorString);
494 return convertParamFileInteger<qint64>(metaData, fileValueStr, typedValue, errorString);
496 return convertParamFileInteger<quint8>(metaData, fileValueStr, typedValue, errorString);
498 return convertParamFileInteger<quint16>(metaData, fileValueStr, typedValue, errorString);
500 return convertParamFileInteger<quint32>(metaData, fileValueStr, typedValue, errorString);
502 return convertParamFileInteger<quint64>(metaData, fileValueStr, typedValue, errorString);
503 default:
504 return metaData.convertAndValidateRaw(fileValueStr, true /* convertOnly */, typedValue, errorString);
505 }
506}
507
509{
510 QFile file(filename);
511
512 if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
513 qCWarning(ParameterEditorControllerLog) << "buildDiffFromFile: unable to open file" << filename;
514 QGC::showAppMessage(tr("Unable to open file: %1").arg(filename));
515 return false;
516 }
517
518 clearDiff();
519
520 qCDebug(ParameterEditorControllerLog) << "buildDiffFromFile:" << filename;
521
522 QTextStream stream(&file);
523
524 // Accumulate in locals; property setters at the end emit only for values that changed.
525 bool diffOtherVehicle = false;
526 bool diffMultipleComponents = false;
527 int diffParsedCount = 0;
528 int diffUnchangedCount = 0;
529 int diffReadOnlyCount = 0;
530 int diffNoVehicleCount = 0;
531 int diffSendableCount = 0;
532 QStringList diffMissingParams;
533 QStringList diffInvalidParams;
534
535 int firstComponentId = -1;
536 while (!stream.atEnd()) {
537 QString line = stream.readLine();
538 if (!line.startsWith("#") && !line.trimmed().isEmpty()) {
539 QStringList wpParams = line.trimmed().split(QRegularExpression("[\\t ,]+"));
540
541 int componentId = -1;
542 QString paramName;
543 QString fileValueStr;
544 int mavParamType = -1;
545 bool isMPFormat = false;
546
547 if (wpParams.size() == 5) {
548 // QGC tab-delimited: VehicleId ComponentId Name Value Type
549 quint32 vehicleId = wpParams.at(0).toUInt();
550 componentId = wpParams.at(1).toInt();
551 paramName = wpParams.at(2);
552 fileValueStr = wpParams.at(3);
553 mavParamType = wpParams.at(4).toInt();
554
555 if (_vehicle->id() != vehicleId) {
556 if (!diffOtherVehicle) {
557 qCDebug(ParameterEditorControllerLog) << "buildDiffFromFile: file is from other vehicle - file vehicleId:" << vehicleId << "connected vehicleId:" << _vehicle->id();
558 }
559 diffOtherVehicle = true;
560 }
561 if (firstComponentId == -1) {
562 firstComponentId = componentId;
563 } else if (firstComponentId != componentId) {
565 qCDebug(ParameterEditorControllerLog) << "buildDiffFromFile: file contains multiple componentIds:" << firstComponentId << componentId;
566 }
568 }
569 } else if (wpParams.size() == 2) {
570 // Mission Planner 2-column: Name Value
571 paramName = wpParams.at(0);
572 fileValueStr = wpParams.at(1);
574 isMPFormat = true;
575 } else {
576 qCDebug(ParameterEditorControllerLog) << "buildDiffFromFile: skipping unparseable line:" << line.trimmed().left(80);
577 continue;
578 }
579
581
582 QString vehicleValueStr;
583 QString units;
584 QVariant fileValueVar = fileValueStr;
585 bool noVehicleValue = false;
586 bool readOnly = false;
587
588 // Shown with the raw file text but never sent, so a bad value can't silently become 0 on the vehicle
589 auto appendInvalidDiff = [&](const QString& errorString) {
590 qCWarning(ParameterEditorControllerLog) << "invalid file value, will not send -" << paramName
591 << "value:" << fileValueStr << "error:" << errorString;
592 diffInvalidParams.append(paramName);
593
594 ParameterEditorDiff* paramDiff = new ParameterEditorDiff(this);
595
596 paramDiff->componentId = componentId;
597 paramDiff->name = paramName;
598 paramDiff->valueType = ParameterManager::mavTypeToFactType(static_cast<MAV_PARAM_TYPE>(mavParamType));
599 paramDiff->fileValue = fileValueStr;
600 paramDiff->fileValueVar = fileValueStr;
601 paramDiff->vehicleValue = vehicleValueStr;
602 paramDiff->noVehicleValue = noVehicleValue;
603 paramDiff->units = units;
604 paramDiff->cannotSend = true;
605 paramDiff->invalidFileValue = true;
606 paramDiff->load = false;
607
608 _diffList.append(paramDiff);
609 };
610
611 if (_parameterMgr->parameterExists(componentId, paramName)) {
612 Fact* vehicleFact = _parameterMgr->getParameter(componentId, paramName);
613 FactMetaData* vehicleFactMetaData = vehicleFact->metaData();
614 Fact fileFact(vehicleFact->componentId(), vehicleFact->name(), vehicleFact->type());
615
616 if (mavParamType == -1) {
617 mavParamType = ParameterManager::factTypeToMavType(vehicleFact->type());
618 }
619
620 readOnly = vehicleFact->readOnly();
621
622 QVariant typedValue;
623 QString errorString;
624 if (!_convertFileValue(*vehicleFactMetaData, fileValueStr, typedValue, errorString)) {
625 if (readOnly) {
626 qCDebug(ParameterEditorControllerLog) << "buildDiffFromFile: skipping read-only param -" << paramName;
628 continue;
629 }
630 vehicleValueStr = vehicleFact->enumOrValueString();
631 units = vehicleFact->cookedUnits();
632 appendInvalidDiff(errorString);
633 continue;
634 }
635
636 // Turn off reboot messaging before setting value in fileFact
637 bool vehicleRebootRequired = vehicleFactMetaData->vehicleRebootRequired();
638 vehicleFactMetaData->setVehicleRebootRequired(false);
639 fileFact.setMetaData(vehicleFact->metaData());
640 fileFact.setRawValue(typedValue);
641 vehicleFactMetaData->setVehicleRebootRequired(vehicleRebootRequired);
642
643 if (vehicleFact->rawValue() == fileFact.rawValue()) {
645 continue;
646 }
647 qCDebug(ParameterEditorControllerLog) << "buildDiffFromFile: changed -" << paramName
648 << "vehicle:" << vehicleFact->rawValue() << "file:" << fileFact.rawValue();
649 fileValueStr = fileFact.enumOrValueString();
650 fileValueVar = fileFact.rawValue();
651 vehicleValueStr = vehicleFact->enumOrValueString();
652 units = vehicleFact->cookedUnits();
653 } else if (isMPFormat) {
654 // MP format: param not on vehicle and file carries no type info, so it can never
655 // be sent. Show it in the diff list as a disabled (cannot-send) row.
656 qCDebug(ParameterEditorControllerLog) << "buildDiffFromFile: not on vehicle, cannot send (MP format) -" << paramName;
657 diffMissingParams.append(paramName);
658
659 ParameterEditorDiff* paramDiff = new ParameterEditorDiff(this);
660
661 paramDiff->componentId = componentId;
662 paramDiff->name = paramName;
663 paramDiff->valueType = FactMetaData::valueTypeFloat; // Unknown - never sent
664 paramDiff->fileValue = fileValueStr;
665 paramDiff->fileValueVar = fileValueVar;
666 paramDiff->cannotSend = true;
667 paramDiff->load = false;
668
669 _diffList.append(paramDiff);
670 continue;
671 } else {
672 noVehicleValue = true;
673
674 // fileValueVar is still a QString variant. Convert it to the typed variant matching the
675 // file's param type, otherwise the PARAM_VALUE ack type check in _mavlinkParamSet fails
676 // and the send would retry/time out.
677 const FactMetaData metaData(ParameterManager::mavTypeToFactType(static_cast<MAV_PARAM_TYPE>(mavParamType)));
678 QVariant typedValue;
679 QString errorString;
680 if (_convertFileValue(metaData, fileValueStr, typedValue, errorString)) {
681 qCDebug(ParameterEditorControllerLog) << "buildDiffFromFile: not on vehicle, will send as new (QGC format) -" << paramName << "value:" << fileValueStr;
682 fileValueVar = typedValue;
683 } else {
684 appendInvalidDiff(errorString);
685 continue;
686 }
687 }
688
689 if (!readOnly) {
690 ParameterEditorDiff* paramDiff = new ParameterEditorDiff(this);
691
692 paramDiff->componentId = componentId;
693 paramDiff->name = paramName;
694 paramDiff->valueType = ParameterManager::mavTypeToFactType(static_cast<MAV_PARAM_TYPE>(mavParamType));
695 paramDiff->fileValue = fileValueStr;
696 paramDiff->fileValueVar = fileValueVar;
697 paramDiff->vehicleValue = vehicleValueStr;
698 paramDiff->noVehicleValue = noVehicleValue;
699 paramDiff->units = units;
700
701 (void) connect(paramDiff, &ParameterEditorDiff::loadChanged, this, &ParameterEditorController::_updateDiffSelectedCount);
702
703 _diffList.append(paramDiff);
705
706 if (noVehicleValue) {
708 }
709 } else {
710 qCDebug(ParameterEditorControllerLog) << "buildDiffFromFile: skipping read-only param -" << paramName;
712 }
713 }
714 }
715
716 file.close();
717
718 if (diffParsedCount == 0) {
719 QGC::showAppMessage(tr("No valid parameters found in file. Check that the file is in QGC or Mission Planner format."));
720 return false;
721 }
722
723 qCDebug(ParameterEditorControllerLog) << "buildDiffFromFile summary -"
724 << "parsed:" << diffParsedCount
725 << "changed:" << diffSendableCount
726 << "unchanged:" << diffUnchangedCount
727 << "readOnly:" << diffReadOnlyCount
728 << "noVehicleValue:" << diffNoVehicleCount
729 << "missing:" << diffMissingParams.count()
730 << (diffMissingParams.isEmpty() ? QString() : diffMissingParams.join(QStringLiteral(", ")))
731 << "invalid:" << diffInvalidParams.count()
732 << (diffInvalidParams.isEmpty() ? QString() : diffInvalidParams.join(QStringLiteral(", ")));
733
734 _setDiffProperty(_diffOtherVehicle, diffOtherVehicle, &ParameterEditorController::diffOtherVehicleChanged);
735 _setDiffProperty(_diffMultipleComponents, diffMultipleComponents, &ParameterEditorController::diffMultipleComponentsChanged);
736 _setDiffProperty(_diffParsedCount, diffParsedCount, &ParameterEditorController::diffParsedCountChanged);
737 _setDiffProperty(_diffUnchangedCount, diffUnchangedCount, &ParameterEditorController::diffUnchangedCountChanged);
738 _setDiffProperty(_diffReadOnlyCount, diffReadOnlyCount, &ParameterEditorController::diffReadOnlyCountChanged);
739 _setDiffProperty(_diffNoVehicleCount, diffNoVehicleCount, &ParameterEditorController::diffNoVehicleCountChanged);
740 _setDiffProperty(_diffSendableCount, diffSendableCount, &ParameterEditorController::diffSendableCountChanged);
741 _setDiffProperty(_diffSelectedCount, diffSendableCount, &ParameterEditorController::diffSelectedCountChanged); // All sendable rows start checked
742 _setDiffProperty(_diffMissingParams, diffMissingParams, &ParameterEditorController::diffMissingParamsChanged);
743 _setDiffProperty(_diffInvalidParams, diffInvalidParams, &ParameterEditorController::diffInvalidParamsChanged);
744
745 return true;
746}
747
749{
750 _parameterMgr->refreshAllParameters();
751}
752
754{
755 _parameterMgr->resetAllParametersToDefaults();
756 refresh();
757}
758
760{
761 _parameterMgr->resetAllToVehicleConfiguration();
762 refresh();
763}
764
765bool ParameterEditorController::_shouldShow(Fact* fact) const
766{
767 if (_hideReadOnly && fact->readOnly()) {
768 return false;
769 }
770 if (_showModifiedOnly) {
771 if (!fact->defaultValueAvailable() || fact->valueEqualsDefault()) {
772 return false;
773 }
774 }
775 if (_showFavoritesOnly) {
776 if (!_favoriteNames.contains(fact->name())) {
777 return false;
778 }
779 }
780 return true;
781}
782
783void ParameterEditorController::_searchTextChanged(void)
784{
785 _searchTimer.start();
786}
787
788void ParameterEditorController::_hideReadOnlyChanged(void)
789{
790 _buildLists();
791
792 ParameterEditorCategory* category = _categories.count() ? _categories.value<ParameterEditorCategory*>(0) : nullptr;
793 setCurrentCategory(category);
794
795 // Re-trigger search if active
796 if (!_searchText.isEmpty() || _showModifiedOnly) {
797 _performSearch();
798 }
799}
800
801void ParameterEditorController::_performSearch(void)
802{
803 QObjectList newParameterList;
804
805 QStringList rgSearchStrings = _searchText.split(' ', Qt::SkipEmptyParts);
806
807 if (rgSearchStrings.isEmpty() && !_showModifiedOnly && !_showFavoritesOnly) {
808 ParameterEditorCategory* category = _categories.count() ? _categories.value<ParameterEditorCategory*>(0) : nullptr;
809 setCurrentCategory(category);
810 _searchParameters.clear();
811 } else {
812 QVector<QRegularExpression> regexList;
813 regexList.reserve(rgSearchStrings.size());
814 for (const QString &searchItem : rgSearchStrings) {
815 QRegularExpression re(searchItem, QRegularExpression::CaseInsensitiveOption);
816 regexList.append(re.isValid() ? re : QRegularExpression());
817 }
818
819 _searchParameters.beginReset();
820 _searchParameters.clear();
821
822 for (int compId : _parameterMgr->componentIds()) {
823 for (const QString &paraName: _parameterMgr->parameterNames(compId)) {
824 Fact* fact = _parameterMgr->getParameter(compId, paraName);
825 bool matched = _shouldShow(fact);
826 // All of the search items must match in order for the parameter to be added to the list
827 if (matched) {
828 for (int i = 0; i < rgSearchStrings.size(); ++i) {
829 const QRegularExpression &re = regexList.at(i);
830 if (re.isValid()) {
831 if (!fact->name().contains(re) &&
832 !fact->shortDescription().contains(re) &&
833 !fact->longDescription().contains(re)) {
834 matched = false;
835 }
836 } else {
837 const QString &searchItem = rgSearchStrings.at(i);
838 if (!fact->name().contains(searchItem, Qt::CaseInsensitive) &&
839 !fact->shortDescription().contains(searchItem, Qt::CaseInsensitive) &&
840 !fact->longDescription().contains(searchItem, Qt::CaseInsensitive)) {
841 matched = false;
842 }
843 }
844 }
845 }
846 if (matched) {
847 _searchParameters.append(fact);
848 }
849 }
850 }
851
852 _searchParameters.endReset();
853
854 if (_parameters != &_searchParameters) {
855 _parameters = &_searchParameters;
856 emit parametersChanged();
857
858 _currentCategory = nullptr;
859 _currentGroup = nullptr;
860 }
861 }
862}
863
864void ParameterEditorController::_currentCategoryChanged(void)
865{
866 ParameterEditorGroup* group = nullptr;
867 if (_currentCategory) {
868 // Select first group when category changes
869 group = _currentCategory->groups.value<ParameterEditorGroup*>(0);
870 } else {
871 group = nullptr;
872 }
873 setCurrentGroup(group);
874}
875
876void ParameterEditorController::_currentGroupChanged(void)
877{
878 _parameters = _currentGroup ? &_currentGroup->facts : nullptr;
879 emit parametersChanged();
880}
881
883{
884 ParameterEditorCategory* category = qobject_cast<ParameterEditorCategory*>(currentCategory);
885 if (category != _currentCategory) {
886 _currentCategory = category;
888 }
889}
890
892{
893 ParameterEditorGroup* group = qobject_cast<ParameterEditorGroup*>(currentGroup);
894 if (group != _currentGroup) {
895 _currentGroup = group;
896 emit currentGroupChanged();
897 }
898}
899
901{
902 QStringList list(_favoriteNames.begin(), _favoriteNames.end());
903 list.sort();
904 return list;
905}
906
907void ParameterEditorController::toggleFavorite(const QString& paramName)
908{
909 if (_favoriteNames.contains(paramName)) {
910 _favoriteNames.remove(paramName);
911 } else {
912 _favoriteNames.insert(paramName);
913 }
914 _saveFavorites();
915 emit favoritesChanged();
916
917 if (_showFavoritesOnly) {
918 _performSearch();
919 }
920}
921
922bool ParameterEditorController::isFavorite(const QString& paramName) const
923{
924 return _favoriteNames.contains(paramName);
925}
926
928{
929 _favoriteNames.clear();
930 _saveFavorites();
931 emit favoritesChanged();
932
933 if (_showFavoritesOnly) {
934 _performSearch();
935 }
936}
937
938void ParameterEditorController::_loadFavorites()
939{
940 Fact* fact = SettingsManager::instance()->appSettings()->favoriteParameters();
941 const QStringList list = fact->rawValue().toString().split(",", Qt::SkipEmptyParts);
942 _favoriteNames = QSet<QString>(list.begin(), list.end());
943}
944
945void ParameterEditorController::_saveFavorites()
946{
947 QStringList list(_favoriteNames.begin(), _favoriteNames.end());
948 list.sort();
949 Fact* fact = SettingsManager::instance()->appSettings()->favoriteParameters();
950 fact->setRawValue(list.join(","));
951}
#define QGC_LOGGING_CATEGORY(name, categoryStr)
static constexpr const char * parameterFileExtension
Holds the meta data associated with a Fact.
static constexpr const char * kDefaultGroup
static constexpr const char * kDefaultCategory
void setVehicleRebootRequired(bool rebootRequired)
bool convertAndValidateRaw(const QVariant &rawValue, bool convertOnly, QVariant &typedValue, QString &errorString) const
ValueType_t type() const
bool vehicleRebootRequired() const
FactPanelController(QObject *parent=nullptr)
A Fact is used to hold a single value within the system.
Definition Fact.h:15
QString enumOrValueString()
Definition Fact.cc:865
void setMetaData(FactMetaData *metaData, bool setDefaultFromMetaData=false)
Definition Fact.cc:742
QString cookedUnits() const
Definition Fact.cc:572
QString longDescription() const
Definition Fact.cc:552
QString shortDescription() const
Definition Fact.cc:525
FactMetaData * metaData()
Definition Fact.h:172
bool valueEqualsDefault() const
Definition Fact.cc:764
bool readOnly() const
Definition Fact.cc:909
int componentId() const
Definition Fact.h:92
QString group() const
Definition Fact.cc:732
FactMetaData::ValueType_t type() const
Definition Fact.h:127
void setRawValue(const QVariant &value)
Definition Fact.cc:135
bool defaultValueAvailable() const
Definition Fact.cc:778
QString name() const
Definition Fact.h:124
QString category() const
Definition Fact.cc:722
QVariant rawValue() const
Definition Fact.h:87
QMap< QString, ParameterEditorGroup * > mapGroupName2Group
QStringList diffInvalidParams(void) const
void diffMissingParamsChanged(const QStringList &diffMissingParams)
QStringList diffMissingParams(void) const
Q_INVOKABLE void resetAllToVehicleConfiguration(void)
void showFavoritesOnlyChanged(void)
Q_INVOKABLE bool buildDiffFromFile(const QString &filename)
void diffSelectedCountChanged(int diffSelectedCount)
void diffMultipleComponentsChanged(bool diffMultipleComponents)
Q_INVOKABLE void toggleFavorite(const QString &paramName)
void diffParsedCountChanged(int diffParsedCount)
void diffReadOnlyCountChanged(int diffReadOnlyCount)
Q_INVOKABLE void clearAllFavorites(void)
void diffNoVehicleCountChanged(int diffNoVehicleCount)
QStringList favoriteParameterNames(void) const
void setCurrentGroup(QObject *currentGroup)
Q_INVOKABLE void saveToFile(const QString &filename)
void diffUnchangedCountChanged(int diffUnchangedCount)
ParameterEditorController(QObject *parent=nullptr)
void searchTextChanged(QString searchText)
void currentCategoryChanged(void)
void diffOtherVehicleChanged(bool diffOtherVehicle)
void setCurrentCategory(QObject *currentCategory)
void diffSendableCountChanged(int diffSendableCount)
Q_INVOKABLE bool isFavorite(const QString &paramName) const
void diffInvalidParamsChanged(const QStringList &diffInvalidParams)
void showModifiedOnlyChanged(void)
Q_INVOKABLE void resetAllToDefaults(void)
bool invalidFileValue
File value cannot be converted to the parameter's type.
bool cannotSend
Shown but never sent: MP param not on vehicle, or invalid file value.
void loadChanged(bool load)
FactMetaData::ValueType_t valueType
Fact * getParameter(int componentId, const QString &paramName)
void factAdded(int componentId, Fact *fact)
static FactMetaData::ValueType_t mavTypeToFactType(MAV_PARAM_TYPE mavType)
static constexpr int defaultComponentId
QStringList parameterNames(int componentId) const
Returns all parameter names.
static MAV_PARAM_TYPE factTypeToMavType(FactMetaData::ValueType_t factType)
QVector< QVariant > ColumnData
void insert(int row, Fact *fact)
int rowCount(const QModelIndex &parent=QModelIndex()) const override
int columnCount(const QModelIndex &parent=QModelIndex()) const override
QVariant headerData(int section, Qt::Orientation orientation, int role=Qt::DisplayRole) const override
QVariant data(const QModelIndex &index, int role=Qt::DisplayRole) const override
QHash< int, QByteArray > roleNames(void) const override
void rowCountChanged(int count)
void endReset()
Supports nesting - only outermost call has effect.
void beginReset()
Supports nesting - only outermost call has effect.
ParameterTableModel(QObject *parent=nullptr)
void append(QObject *object)
Caller maintains responsibility for object ownership and deletion.
T value(int index) const
QObject * removeAt(int index)
int count() const override final
void insert(int index, QObject *object)
static SettingsManager * instance()
AppSettings * appSettings() const
void showAppMessage(const QString &message, const QString &title)
Modal application message. Queued if the UI isn't ready yet.
Definition AppMessages.cc:9