summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorFriedemann Kleint <Friedemann.Kleint@qt.io>2022-10-13 15:39:05 +0200
committerFriedemann Kleint <Friedemann.Kleint@qt.io>2022-10-17 13:45:53 +0200
commitad4b1804ddc938c519efe7a393804145b084e5b5 (patch)
treeb5613f09b9895779e60b89abceba1812fcfc76f9
parent101295aef757747490fb5533710d9e10bf303d19 (diff)
Polish the can example, take 2
- Fix the settings organization - Use modern string literals - Avoid repeated instantiations of QRegularExpression - Streamline code Pick-to: 6.4 Change-Id: Id8412d9d21e0969ef4ef4606ce0c024a8803b314 Reviewed-by: Ivan Solovev <ivan.solovev@qt.io> Reviewed-by: André Hartmann <aha_1980@gmx.de>
-rw-r--r--examples/serialbus/can/connectdialog.cpp2
-rw-r--r--examples/serialbus/can/main.cpp4
-rw-r--r--examples/serialbus/can/mainwindow.cpp16
-rw-r--r--examples/serialbus/can/sendframebox.cpp110
4 files changed, 74 insertions, 58 deletions
diff --git a/examples/serialbus/can/connectdialog.cpp b/examples/serialbus/can/connectdialog.cpp
index 255728a..4db9646 100644
--- a/examples/serialbus/can/connectdialog.cpp
+++ b/examples/serialbus/can/connectdialog.cpp
@@ -10,7 +10,7 @@
ConnectDialog::ConnectDialog(QWidget *parent) :
QDialog(parent),
m_ui(new Ui::ConnectDialog),
- m_settings(new QSettings("Qt", "CAN example"))
+ m_settings(new QSettings("QtProject", "CAN example"))
{
m_ui->setupUi(this);
diff --git a/examples/serialbus/can/main.cpp b/examples/serialbus/can/main.cpp
index 3105379..f3e2a9d 100644
--- a/examples/serialbus/can/main.cpp
+++ b/examples/serialbus/can/main.cpp
@@ -6,9 +6,11 @@
#include <QApplication>
#include <QLoggingCategory>
+using namespace Qt::StringLiterals;
+
int main(int argc, char *argv[])
{
- QLoggingCategory::setFilterRules(QStringLiteral("qt.canbus* = true"));
+ QLoggingCategory::setFilterRules(u"qt.canbus* = true"_s);
QApplication a(argc, argv);
MainWindow w;
w.show();
diff --git a/examples/serialbus/can/mainwindow.cpp b/examples/serialbus/can/mainwindow.cpp
index 52395b3..01d2a6c 100644
--- a/examples/serialbus/can/mainwindow.cpp
+++ b/examples/serialbus/can/mainwindow.cpp
@@ -15,6 +15,8 @@
#include <QLabel>
#include <QTimer>
+using namespace Qt::StringLiterals;
+
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
m_ui(new Ui::MainWindow),
@@ -225,14 +227,14 @@ void MainWindow::closeEvent(QCloseEvent *event)
static QString frameFlags(const QCanBusFrame &frame)
{
- QString result = QLatin1String(" --- ");
+ QString result = u" --- "_s;
if (frame.hasBitrateSwitch())
- result[1] = QLatin1Char('B');
+ result[1] = u'B';
if (frame.hasErrorStateIndicator())
- result[2] = QLatin1Char('E');
+ result[2] = u'E';
if (frame.hasLocalEcho())
- result[3] = QLatin1Char('L');
+ result[3] = u'L';
return result;
}
@@ -250,11 +252,11 @@ void MainWindow::processReceivedFrames()
if (frame.frameType() == QCanBusFrame::ErrorFrame)
data = m_canDevice->interpretErrorFrame(frame);
else
- data = QLatin1String(frame.payload().toHex(' ').toUpper());
+ data = QString::fromLatin1(frame.payload().toHex(' ').toUpper());
const QString time = QString::fromLatin1("%1.%2 ")
- .arg(frame.timeStamp().seconds(), 10, 10, QLatin1Char(' '))
- .arg(frame.timeStamp().microSeconds() / 100, 4, 10, QLatin1Char('0'));
+ .arg(frame.timeStamp().seconds(), 10, 10, ' '_L1)
+ .arg(frame.timeStamp().microSeconds() / 100, 4, 10, '0'_L1);
const QString flags = frameFlags(frame);
diff --git a/examples/serialbus/can/sendframebox.cpp b/examples/serialbus/can/sendframebox.cpp
index 704ef3f..e26524e 100644
--- a/examples/serialbus/can/sendframebox.cpp
+++ b/examples/serialbus/can/sendframebox.cpp
@@ -4,7 +4,35 @@
#include "sendframebox.h"
#include "ui_sendframebox.h"
-constexpr char THREE_DIGITS[] = "[[:xdigit:]]{3}";
+using namespace Qt::StringLiterals;
+
+static const QRegularExpression &threeHexDigitsPattern()
+{
+ static const QRegularExpression result(u"[[:xdigit:]]{3}"_s);
+ Q_ASSERT(result.isValid());
+ return result;
+}
+
+static const QRegularExpression &oneDigitAndSpacePattern()
+{
+ static const QRegularExpression result(u"((\\s+)|^)([[:xdigit:]]{1})(\\s+)"_s);
+ Q_ASSERT(result.isValid());
+ return result;
+}
+
+const QRegularExpression &hexNumberPattern()
+{
+ static const QRegularExpression result(u"^[[:xdigit:]]*$"_s);
+ Q_ASSERT(result.isValid());
+ return result;
+}
+
+const QRegularExpression &twoSpacesPattern()
+{
+ static const QRegularExpression result(u"([\\s]{2})"_s);
+ Q_ASSERT(result.isValid());
+ return result;
+}
enum {
MaxStandardId = 0x7FF,
@@ -16,35 +44,27 @@ enum {
MaxPayloadFd = 64
};
-bool isEvenHex(QString input)
+static bool isEvenHex(QString input)
{
- const QChar space = QLatin1Char(' ');
- input.remove(space);
-
- if (input.size() % 2)
- return false;
-
- return true;
+ input.remove(u' ');
+ return (input.size() % 2) == 0;
}
// Formats a string of hex characters with a space between every byte
// Example: "012345" -> "01 23 45"
static QString formatHexData(const QString &input)
{
- const QChar space = QLatin1Char(' ');
QString out = input;
- const QRegularExpression threeDigits(THREE_DIGITS);
- const QRegularExpression oneDigitAndSpace(QStringLiteral("((\\s+)|^)([[:xdigit:]]{1})(\\s+)"));
-
- while (oneDigitAndSpace.match(out).hasMatch() || threeDigits.match(out).hasMatch()) {
- if (threeDigits.match(out).hasMatch()) {
- const QRegularExpressionMatch match = threeDigits.match(out);
- out.insert(match.capturedEnd() - 1, space);
- } else if (oneDigitAndSpace.match(out).hasMatch()) {
- const QRegularExpressionMatch match = oneDigitAndSpace.match(out);
- if (out.at(match.capturedEnd() - 1) == space)
- out.remove(match.capturedEnd() - 1, 1);
+ while (true) {
+ if (auto match = threeHexDigitsPattern().match(out); match.hasMatch()) {
+ out.insert(match.capturedEnd() - 1, u' ');
+ } else if (match = oneDigitAndSpacePattern().match(out); match.hasMatch()) {
+ const auto pos = match.capturedEnd() - 1;
+ if (out.at(pos) == u' ')
+ out.remove(pos, 1);
+ } else {
+ break;
}
}
@@ -59,16 +79,11 @@ HexIntegerValidator::HexIntegerValidator(QObject *parent) :
QValidator::State HexIntegerValidator::validate(QString &input, int &) const
{
- bool ok;
- uint value = input.toUInt(&ok, 16);
-
if (input.isEmpty())
return Intermediate;
-
- if (!ok || value > m_maximum)
- return Invalid;
-
- return Acceptable;
+ bool ok;
+ uint value = input.toUInt(&ok, 16);
+ return ok && value <= m_maximum ? Acceptable : Invalid;
}
void HexIntegerValidator::setMaximum(uint maximum)
@@ -85,10 +100,9 @@ HexStringValidator::HexStringValidator(QObject *parent) :
QValidator::State HexStringValidator::validate(QString &input, int &pos) const
{
const int maxSize = 2 * m_maxLength;
- const QChar space = QLatin1Char(' ');
QString data = input;
- data.remove(space);
+ data.remove(u' ');
if (data.isEmpty())
return Intermediate;
@@ -98,35 +112,33 @@ QValidator::State HexStringValidator::validate(QString &input, int &pos) const
return Invalid;
// check if all input is valid
- const QRegularExpression re(QStringLiteral("^[[:xdigit:]]*$"));
- if (!re.match(data).hasMatch())
+ if (!hexNumberPattern().match(data).hasMatch())
return Invalid;
// don't allow user to enter more than one space
- const QRegularExpression twoSpaces(QStringLiteral("([\\s]{2})"));
- if (twoSpaces.match(input).hasMatch()) {
- const QRegularExpressionMatch match = twoSpaces.match(input);
+ if (const auto match = twoSpacesPattern().match(input); match.hasMatch()) {
input.replace(match.capturedStart(), 2, ' ');
pos = match.capturedEnd() - 1;
}
// insert a space after every two hex nibbles
- const QRegularExpression threeDigits(THREE_DIGITS);
-
- while (threeDigits.match(input).hasMatch()) {
- const QRegularExpressionMatch match = threeDigits.match(input);
- if (pos == match.capturedStart() + 1) {
+ while (true) {
+ const QRegularExpressionMatch match = threeHexDigitsPattern().match(input);
+ if (!match.hasMatch())
+ break;
+ const auto start = match.capturedStart();
+ const auto end = match.capturedEnd();
+ if (pos == start + 1) {
// add one hex nibble before two - Abc
- input.insert(match.capturedStart() + 1, space);
- pos = match.capturedStart() + 1;
- } else if (pos == match.capturedStart() + 2) {
+ input.insert(pos, u' ');
+ } else if (pos == start + 2) {
// add hex nibble in the middle - aBc
- input.insert(match.capturedEnd() - 1, space);
- pos = match.capturedEnd();
+ input.insert(end - 1, u' ');
+ pos = end;
} else {
// add one hex nibble after two - abC
- input.insert(match.capturedEnd() - 1, space);
- pos = match.capturedEnd() + 1;
+ input.insert(end - 1, u' ');
+ pos = end + 1;
}
}
@@ -199,7 +211,7 @@ SendFrameBox::SendFrameBox(QWidget *parent) :
const uint frameId = m_ui->frameIdEdit->text().toUInt(nullptr, 16);
QString data = m_ui->payloadEdit->text();
m_ui->payloadEdit->setText(formatHexData(data));
- const QByteArray payload = QByteArray::fromHex(data.remove(QLatin1Char(' ')).toLatin1());
+ const QByteArray payload = QByteArray::fromHex(data.remove(u' ').toLatin1());
QCanBusFrame frame = QCanBusFrame(frameId, payload);
frame.setExtendedFrameFormat(m_ui->extendedFormatBox->isChecked());