fix(cpn): radio specific name and label validators (#4670)

Only allows valid characters in names, drops invalid characters if present
This commit is contained in:
Neil Horne
2024-02-28 11:35:57 +10:00
committed by GitHub
parent 3807d7759b
commit 57c7aca2f6
31 changed files with 428 additions and 139 deletions
+8
View File
@@ -19,10 +19,18 @@ set(${PROJECT_NAME}_NAMES
curvedialog
genericpanel
verticalscrollarea
textvalidator
)
AddHeadersSources()
# these have no cpp
set(${PROJECT_NAME}_HDRS
${${PROJECT_NAME}_HDRS}
namevalidator.h
labelvalidator.h
)
# AUTOMOC does not detect so manually process
qt5_wrap_cpp(${PROJECT_NAME}_SRCS
${${PROJECT_NAME}_HDRS}
+7 -6
View File
@@ -39,19 +39,20 @@ AutoLineEdit::~AutoLineEdit()
void AutoLineEdit::setField(char * field, int len, GenericPanel * panel)
{
m_charField = field;
setPanel(panel);
setValidator(new QRegExpValidator(QRegExp("[ A-Za-z0-9_.-,]*"), this));
if (len)
setMaxLength(len);
updateValue();
setFieldInit(len, panel);
}
void AutoLineEdit::setField(QString & field, int len, GenericPanel * panel)
{
m_strField = &field;
setFieldInit(len, panel);
}
void AutoLineEdit::setFieldInit(int len, GenericPanel * panel)
{
setPanel(panel);
if (len)
setMaxLength(len);
setPanel(panel);
updateValue();
}
+2
View File
@@ -47,4 +47,6 @@ class AutoLineEdit: public QLineEdit, public AutoWidget
private:
char *m_charField;
QString *m_strField;
void setFieldInit(int len, GenericPanel * panel);
};
+2
View File
@@ -25,6 +25,7 @@
#include "helpers.h"
#include "filtereditemmodels.h"
#include "eeprominterface.h"
#include "namevalidator.h"
float curveLinear(float x, float coeff, float yMin, float yMid, float yMax)
{
@@ -133,6 +134,7 @@ CurveDialog::CurveDialog(QWidget * parent, ModelData & model, const int curveIdx
maxCurves = firmware->getCapability(NumCurves);
maxPoints = firmware->getCapability(NumCurvePoints);
ui->curveName->setValidator(new NameValidator(firmware->getBoard(), this));
ui->curveName->setField(curve.name, CURVEDATA_NAME_LEN);
connect(ui->curveName, &AutoLineEdit::currentDataChanged, this, &CurveDialog::on_curveNameChanged);
on_curveNameChanged();
+35
View File
@@ -0,0 +1,35 @@
/*
* Copyright (C) EdgeTX
*
* Based on code named
* opentx - https://github.com/opentx/opentx
* th9x - http://code.google.com/p/th9x
* er9x - http://code.google.com/p/er9x
* gruvin9x - http://code.google.com/p/gruvin9x
*
* License GPLv2: http://www.gnu.org/licenses/gpl-2.0.html
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*/
#pragma once
#include "textvalidator.h"
constexpr char LABEL_VALID_PATTERN[] {"[ A-Za-z0-9\\_\\,\\.\\+\\/\\*\\=\\%\\!\\?\\#\\<\\>\\@\\$\\(\\)\\{\\}\\[\\]\\;]*"};
class LabelValidator : public TextValidator
{
Q_OBJECT
public:
explicit LabelValidator(QObject * parent = nullptr) : TextValidator(parent, LABEL_VALID_PATTERN) {}
virtual ~LabelValidator() {}
};
+41
View File
@@ -0,0 +1,41 @@
/*
* Copyright (C) EdgeTX
*
* Based on code named
* opentx - https://github.com/opentx/opentx
* th9x - http://code.google.com/p/th9x
* er9x - http://code.google.com/p/er9x
* gruvin9x - http://code.google.com/p/gruvin9x
*
* License GPLv2: http://www.gnu.org/licenses/gpl-2.0.html
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*/
#pragma once
#include "boards.h"
#include "textvalidator.h"
// characters supportd by B&W radio firmware gui editor
constexpr char NAME_VALID_PATTERN_BW[] {"[ A-Za-z0-9\\-]*"};
// characters supported by color radio firmware keyboard widget
constexpr char NAME_VALID_PATTERN_COLOR[] {"[ A-Za-z0-9\\-\\_\\,\\.\"\\+\\/\\*\\=\\%\\!\\?\\#\\<\\>\\@\\$\\(\\)\\{\\}\\[\\]\\;\\:\\']*"};
class NameValidator : public TextValidator
{
Q_OBJECT
public:
explicit NameValidator(Board::Type board, QObject * parent = nullptr) :
TextValidator(parent, Boards::getCapability(board, Board::HasColorLcd) ? NAME_VALID_PATTERN_COLOR : NAME_VALID_PATTERN_BW) {}
virtual ~NameValidator() {}
};
+57
View File
@@ -0,0 +1,57 @@
/*
* Copyright (C) EdgeTX
*
* Based on code named
* opentx - https://github.com/opentx/opentx
* th9x - http://code.google.com/p/th9x
* er9x - http://code.google.com/p/er9x
* gruvin9x - http://code.google.com/p/gruvin9x
*
* License GPLv2: http://www.gnu.org/licenses/gpl-2.0.html
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*/
#include "textvalidator.h"
#include <QDebug>
TextValidator::TextValidator(QObject * parent, const QString &pattern, const QChar invalidRepl, QRegularExpression::PatternOptions options):
QRegularExpressionValidator(parent),
invalidRepl(invalidRepl)
{
regexp.setPattern(pattern);
regexp.setPatternOptions(options);
if (!regexp.isValid())
qDebug() << "Error - Pattern:" << regexp.pattern() << "Error msg:" << regexp.errorString() << "Error offset:" << regexp.patternErrorOffset();
if (!isValid(invalidRepl))
qDebug() << "Error - Replacement character: '" << invalidRepl << "' is not valid for pattern:" << regexp.pattern();
setRegularExpression(regexp);
}
void TextValidator::fixup(QString &input) const
{
for (int i = 0; i < input.size(); i++) {
QRegularExpressionMatch match = regexp.match(input.at(i));
if (match.captured() != input.at(i)) // cannot rely on hasMatch
input.replace(i, 1, invalidRepl); // replacement character MUST be valid for regexp to avoid possible loop condition
}
input = input.trimmed();
}
bool TextValidator::isValid(const QString &input) const
{
QRegularExpressionMatch match = regexp.match(input);
return input == match.captured();
}
+44
View File
@@ -0,0 +1,44 @@
/*
* Copyright (C) EdgeTX
*
* Based on code named
* opentx - https://github.com/opentx/opentx
* th9x - http://code.google.com/p/th9x
* er9x - http://code.google.com/p/er9x
* gruvin9x - http://code.google.com/p/gruvin9x
*
* License GPLv2: http://www.gnu.org/licenses/gpl-2.0.html
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*/
#pragma once
#include "boards.h"
#include <QRegularExpressionValidator>
class TextValidator : public QRegularExpressionValidator
{
Q_OBJECT
public:
explicit TextValidator(QObject * parent, const QString &pattern, const QChar invalidRepl = QChar(' '),
QRegularExpression::PatternOptions options = QRegularExpression::NoPatternOption);
virtual ~TextValidator() {}
void fixup(QString &input) const;
virtual bool isValid(const QString &input) const;
private:
QChar invalidRepl;
QRegularExpression regexp;
};