17 : QAbstractTableModel(parent)
29 return _tableData.count();
34 return _tableViewColCount;
39 if (!index.isValid()) {
43 if (index.row() < 0 || index.row() >= _tableData.count()) {
46 if (index.column() < 0 || index.column() >= _tableViewColCount) {
52 return QVariant::fromValue(_tableData[index.row()][index.column()]);
54 return QVariant::fromValue(_tableData[index.row()][
ValueColumn]);
62 if (orientation != Qt::Horizontal || role != Qt::DisplayRole) {
71 default:
return QVariant();
78 {Qt::DisplayRole,
"display"},
98 qWarning() <<
"Invalid row row:rowCount" << row <<
rowCount() << Q_FUNC_INFO;
99 row = qMax(qMin(row,
rowCount()), 0);
102 ColumnData colData(_tableViewColCount, QString());
108 if (!_isResetting()) {
109 beginInsertRows(QModelIndex(), row, row);
111 _tableData.insert(row, colData);
112 if (!_isResetting()) {
120 _resetNestingCount++;
122 if (_resetNestingCount == 1) {
129 if (_resetNestingCount == 0) {
130 qWarning() <<
"ParameterTableModel::endReset called without prior beginReset";
133 _resetNestingCount--;
134 if (_resetNestingCount == 0) {
142 if (row < 0 || row >= _tableData.count()) {
143 qWarning() <<
"Invalid row row:rowCount" << row << _tableData.count() << Q_FUNC_INFO;
159 , _parameterMgr(
_vehicle->parameterManager())
165 _searchTimer.setSingleShot(
true);
166 _searchTimer.setInterval(300);
174 connect(&_searchTimer, &QTimer::timeout,
this, &ParameterEditorController::_performSearch);
188void ParameterEditorController::_buildListsForComponent(
int compId)
190 for (
const QString& factName: _parameterMgr->
parameterNames(compId)) {
193 if (_hideReadOnly && fact->
readOnly()) {
197 ParameterEditorCategory* category =
nullptr;
198 if (_mapCategoryName2Category.contains(fact->
category())) {
199 category = _mapCategoryName2Category[fact->
category()];
201 category =
new ParameterEditorCategory(
this);
203 _mapCategoryName2Category[fact->
category()] = category;
204 _categories.append(category);
207 ParameterEditorGroup* group =
nullptr;
211 group =
new ParameterEditorGroup(
this);
222void ParameterEditorController::_buildLists(
void)
224 _currentCategory =
nullptr;
225 _currentGroup =
nullptr;
226 _parameters =
nullptr;
227 _mapCategoryName2Category.clear();
228 _categories.clearAndDeleteContents();
232 _buildListsForComponent(MAV_COMP_ID_AUTOPILOT1);
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);
245 for (
int i=0; i<_categories.count(); i++) {
246 ParameterEditorCategory* category = _categories.value<ParameterEditorCategory*>(i);
248 if (i != _categories.count() - 1) {
249 _categories.removeAt(i);
250 _categories.append(category);
257 for (
int compId: _parameterMgr->componentIds()) {
258 if (compId != MAV_COMP_ID_AUTOPILOT1) {
259 _buildListsForComponent(compId);
264 for (
int i=0; i<_categories.count(); i++) {
265 ParameterEditorCategory* category = _categories.value<ParameterEditorCategory*>(i);
267 ParameterEditorGroup* group = category->
groups.
value<ParameterEditorGroup*>(j);
269 if (j != _categories.count() - 1) {
279void ParameterEditorController::_factAdded(
int compId,
Fact* fact)
281 if (_hideReadOnly && fact->
readOnly()) {
285 bool inserted =
false;
286 ParameterEditorCategory* category =
nullptr;
288 if (_mapCategoryName2Category.contains(fact->
category())) {
289 category = _mapCategoryName2Category[fact->
category()];
291 category =
new ParameterEditorCategory(
this);
293 _mapCategoryName2Category[fact->
category()] = category;
297 for (
int i=0; i<_categories.count(); i++) {
298 if (_categories.value<ParameterEditorCategory*>(i)->name > category->
name) {
299 _categories.insert(i, category);
305 _categories.append(category);
309 ParameterEditorGroup* group =
nullptr;
313 group =
new ParameterEditorGroup(
this);
319 QmlObjectListModel& groups = category->
groups;
321 for (
int i=0; i<groups.
count(); i++) {
322 if (groups.
value<ParameterEditorGroup*>(i)->name > group->
name) {
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);
346 if (!filename.isEmpty()) {
347 QString parameterFilename = filename;
348 if (!QFileInfo(filename).fileName().contains(
".")) {
352 QFile file(parameterFilename);
354 if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) {
355 qCWarning(ParameterEditorControllerLog) <<
"saveToFile: unable to create file" << parameterFilename;
360 qCDebug(ParameterEditorControllerLog) <<
"saveToFile:" << parameterFilename;
361 QTextStream stream(&file);
362 _parameterMgr->writeParametersToStream(stream);
367template <
typename T,
typename SignalFn>
368void ParameterEditorController::_setDiffProperty(T& member,
const T& value, SignalFn changedSignal)
370 if (member == value) {
374 emit (this->*changedSignal)(member);
379 _diffList.clearAndDeleteContents();
393void ParameterEditorController::_updateDiffSelectedCount()
395 int selectedCount = 0;
396 for (
int i=0; i<_diffList.
count(); i++) {
408 int uncheckedCount = 0;
410 for (
int i=0; i<_diffList.count(); i++) {
414 qCDebug(ParameterEditorControllerLog) <<
"sendDiff: skipped (cannot send) -" << paramDiff->
name;
418 if (paramDiff->
load) {
421 qCDebug(ParameterEditorControllerLog) <<
"sendDiff: PARAM_SET new param -" << paramDiff->
name
425 qCDebug(ParameterEditorControllerLog) <<
"sendDiff: fact write -" << paramDiff->
name
432 qCDebug(ParameterEditorControllerLog) <<
"sendDiff: skipped (unchecked) -" << paramDiff->
name;
436 qCDebug(ParameterEditorControllerLog) <<
"sendDiff summary - sent:" << sentCount <<
"unchecked:" << uncheckedCount;
446bool convertParamFileInteger(
const FactMetaData& metaData,
const QString& fileValueStr, QVariant& typedValue,
447 QString& errorString)
449 using Wide = std::conditional_t<std::is_signed_v<T>, qint64, quint64>;
451 static const QRegularExpression wholeNumberFloatRegExp(QStringLiteral(
"^([+-]?\\d+)\\.0*$"));
453 QString integerStr = fileValueStr;
454 const QRegularExpressionMatch match = wholeNumberFloatRegExp.match(fileValueStr);
455 if (match.hasMatch()) {
456 integerStr = match.captured(1);
461 if constexpr (std::is_signed_v<T>) {
462 value = integerStr.toLongLong(&ok);
464 value = integerStr.toULongLong(&ok);
467 errorString = QStringLiteral(
"Not a whole number within the parameter's range");
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");
483bool ParameterEditorController::_convertFileValue(
const FactMetaData& metaData,
const QString& fileValueStr,
484 QVariant& typedValue, QString& errorString)
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);
510 QFile file(filename);
512 if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
513 qCWarning(ParameterEditorControllerLog) <<
"buildDiffFromFile: unable to open file" << filename;
520 qCDebug(ParameterEditorControllerLog) <<
"buildDiffFromFile:" << filename;
522 QTextStream stream(&file);
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 ,]+"));
541 int componentId = -1;
543 QString fileValueStr;
544 int mavParamType = -1;
545 bool isMPFormat =
false;
547 if (wpParams.size() == 5) {
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();
557 qCDebug(ParameterEditorControllerLog) <<
"buildDiffFromFile: file is from other vehicle - file vehicleId:" << vehicleId <<
"connected vehicleId:" <<
_vehicle->id();
561 if (firstComponentId == -1) {
562 firstComponentId = componentId;
563 }
else if (firstComponentId != componentId) {
565 qCDebug(ParameterEditorControllerLog) <<
"buildDiffFromFile: file contains multiple componentIds:" << firstComponentId << componentId;
569 }
else if (wpParams.size() == 2) {
571 paramName = wpParams.at(0);
572 fileValueStr = wpParams.at(1);
576 qCDebug(ParameterEditorControllerLog) <<
"buildDiffFromFile: skipping unparseable line:" << line.trimmed().left(80);
582 QString vehicleValueStr;
584 QVariant fileValueVar = fileValueStr;
585 bool noVehicleValue =
false;
586 bool readOnly =
false;
589 auto appendInvalidDiff = [&](
const QString& errorString) {
590 qCWarning(ParameterEditorControllerLog) <<
"invalid file value, will not send -" << paramName
591 <<
"value:" << fileValueStr <<
"error:" << errorString;
597 paramDiff->
name = paramName;
603 paramDiff->
units = units;
606 paramDiff->
load =
false;
608 _diffList.append(paramDiff);
611 if (_parameterMgr->parameterExists(componentId, paramName)) {
612 Fact* vehicleFact = _parameterMgr->getParameter(componentId, paramName);
616 if (mavParamType == -1) {
624 if (!_convertFileValue(*vehicleFactMetaData, fileValueStr, typedValue, errorString)) {
626 qCDebug(ParameterEditorControllerLog) <<
"buildDiffFromFile: skipping read-only param -" << paramName;
632 appendInvalidDiff(errorString);
647 qCDebug(ParameterEditorControllerLog) <<
"buildDiffFromFile: changed -" << paramName
653 }
else if (isMPFormat) {
656 qCDebug(ParameterEditorControllerLog) <<
"buildDiffFromFile: not on vehicle, cannot send (MP format) -" << paramName;
662 paramDiff->
name = paramName;
667 paramDiff->
load =
false;
669 _diffList.append(paramDiff);
672 noVehicleValue =
true;
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;
684 appendInvalidDiff(errorString);
693 paramDiff->
name = paramName;
699 paramDiff->
units = units;
703 _diffList.append(paramDiff);
706 if (noVehicleValue) {
710 qCDebug(ParameterEditorControllerLog) <<
"buildDiffFromFile: skipping read-only param -" << paramName;
719 QGC::showAppMessage(tr(
"No valid parameters found in file. Check that the file is in QGC or Mission Planner format."));
723 qCDebug(ParameterEditorControllerLog) <<
"buildDiffFromFile summary -"
750 _parameterMgr->refreshAllParameters();
755 _parameterMgr->resetAllParametersToDefaults();
761 _parameterMgr->resetAllToVehicleConfiguration();
765bool ParameterEditorController::_shouldShow(
Fact* fact)
const
767 if (_hideReadOnly && fact->
readOnly()) {
770 if (_showModifiedOnly) {
775 if (_showFavoritesOnly) {
776 if (!_favoriteNames.contains(fact->
name())) {
783void ParameterEditorController::_searchTextChanged(
void)
785 _searchTimer.start();
788void ParameterEditorController::_hideReadOnlyChanged(
void)
792 ParameterEditorCategory* category = _categories.count() ? _categories.value<ParameterEditorCategory*>(0) : nullptr;
796 if (!_searchText.isEmpty() || _showModifiedOnly) {
801void ParameterEditorController::_performSearch(
void)
803 QObjectList newParameterList;
805 QStringList rgSearchStrings = _searchText.split(
' ', Qt::SkipEmptyParts);
807 if (rgSearchStrings.isEmpty() && !_showModifiedOnly && !_showFavoritesOnly) {
808 ParameterEditorCategory* category = _categories.count() ? _categories.value<ParameterEditorCategory*>(0) : nullptr;
810 _searchParameters.clear();
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());
819 _searchParameters.beginReset();
820 _searchParameters.clear();
822 for (
int compId : _parameterMgr->componentIds()) {
823 for (
const QString ¶Name: _parameterMgr->parameterNames(compId)) {
824 Fact* fact = _parameterMgr->getParameter(compId, paraName);
825 bool matched = _shouldShow(fact);
828 for (
int i = 0; i < rgSearchStrings.size(); ++i) {
829 const QRegularExpression &re = regexList.at(i);
831 if (!fact->
name().contains(re) &&
837 const QString &searchItem = rgSearchStrings.at(i);
838 if (!fact->
name().contains(searchItem, Qt::CaseInsensitive) &&
847 _searchParameters.append(fact);
852 _searchParameters.endReset();
854 if (_parameters != &_searchParameters) {
855 _parameters = &_searchParameters;
858 _currentCategory =
nullptr;
859 _currentGroup =
nullptr;
864void ParameterEditorController::_currentCategoryChanged(
void)
866 ParameterEditorGroup* group =
nullptr;
867 if (_currentCategory) {
869 group = _currentCategory->groups.value<ParameterEditorGroup*>(0);
876void ParameterEditorController::_currentGroupChanged(
void)
878 _parameters = _currentGroup ? &_currentGroup->facts :
nullptr;
885 if (category != _currentCategory) {
886 _currentCategory = category;
894 if (group != _currentGroup) {
895 _currentGroup = group;
902 QStringList list(_favoriteNames.begin(), _favoriteNames.end());
909 if (_favoriteNames.contains(paramName)) {
910 _favoriteNames.remove(paramName);
912 _favoriteNames.insert(paramName);
917 if (_showFavoritesOnly) {
924 return _favoriteNames.contains(paramName);
929 _favoriteNames.clear();
933 if (_showFavoritesOnly) {
938void ParameterEditorController::_loadFavorites()
941 const QStringList list = fact->
rawValue().toString().split(
",", Qt::SkipEmptyParts);
942 _favoriteNames = QSet<QString>(list.begin(), list.end());
945void ParameterEditorController::_saveFavorites()
947 QStringList list(_favoriteNames.begin(), _favoriteNames.end());
#define QGC_LOGGING_CATEGORY(name, categoryStr)
static constexpr const char * parameterFileExtension
FactPanelController(QObject *parent=nullptr)
A Fact is used to hold a single value within the system.
QString enumOrValueString()
void setMetaData(FactMetaData *metaData, bool setDefaultFromMetaData=false)
QString cookedUnits() const
QString longDescription() const
QString shortDescription() const
FactMetaData * metaData()
bool valueEqualsDefault() const
FactMetaData::ValueType_t type() const
void setRawValue(const QVariant &value)
bool defaultValueAvailable() const
QVariant rawValue() const
QMap< QString, ParameterEditorGroup * > mapGroupName2Group
QmlObjectListModel groups
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 parametersChanged(void)
int diffParsedCount(void) const
Q_INVOKABLE void refresh(void)
void currentGroupChanged(void)
int diffSendableCount(void) const
void diffSelectedCountChanged(int diffSelectedCount)
void diffMultipleComponentsChanged(bool diffMultipleComponents)
Q_INVOKABLE void toggleFavorite(const QString ¶mName)
QObject * currentCategory(void)
void diffParsedCountChanged(int diffParsedCount)
void diffReadOnlyCountChanged(int diffReadOnlyCount)
int diffReadOnlyCount(void) const
Q_INVOKABLE void clearAllFavorites(void)
~ParameterEditorController()
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)
Q_INVOKABLE void sendDiff(void)
void currentCategoryChanged(void)
void diffOtherVehicleChanged(bool diffOtherVehicle)
void setCurrentCategory(QObject *currentCategory)
void diffSendableCountChanged(int diffSendableCount)
void favoritesChanged(void)
int diffNoVehicleCount(void) const
bool diffMultipleComponents(void) const
Q_INVOKABLE bool isFavorite(const QString ¶mName) const
void diffInvalidParamsChanged(const QStringList &diffInvalidParams)
bool diffOtherVehicle(void) const
Q_INVOKABLE void clearDiff(void)
void showModifiedOnlyChanged(void)
int diffUnchangedCount(void) const
QObject * currentGroup(void)
Q_INVOKABLE void resetAllToDefaults(void)
void hideReadOnlyChanged(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
ParameterEditorGroup(QObject *parent)
ParameterTableModel facts
Fact * getParameter(int componentId, const QString ¶mName)
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
~ParameterTableModel() 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)
Fact * factAt(int row) const
void append(QObject *object)
Caller maintains responsibility for object ownership and deletion.
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.