Skip to content

Commit 5bd8e85

Browse files
committed
Review remarks
1 parent 5f8a825 commit 5bd8e85

9 files changed

Lines changed: 90 additions & 25 deletions

File tree

src/communication/modbuspoll.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,9 @@ void ModbusPoll::onDescribeResult(const QJsonObject& description)
125125
_pAdapterClient->requestRegisterSchema();
126126
}
127127

128+
/*! \brief Receive the adapter register schema and forward it to the settings model.
129+
* \param schema The register schema JSON object returned by adapter.registerSchema.
130+
*/
128131
void ModbusPoll::onRegisterSchemaResult(const QJsonObject& schema)
129132
{
130133
_pSettingsModel->setAdapterRegisterSchema("modbus", schema);

src/dialogs/addregisterwidget.cpp

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,12 @@
1010
#include <QJsonArray>
1111
#include <QVBoxLayout>
1212

13+
/*!
14+
* \brief Constructs the widget and populates it from the adapter's register schema.
15+
* \param pSettingsModel Pointer to the application settings model.
16+
* \param adapterId Identifier of the adapter whose register schema to use.
17+
* \param parent Optional parent widget.
18+
*/
1319
AddRegisterWidget::AddRegisterWidget(SettingsModel* pSettingsModel, const QString& adapterId, QWidget* parent)
1420
: QWidget(parent),
1521
_pUi(new Ui::AddRegisterWidget),
@@ -59,6 +65,12 @@ AddRegisterWidget::AddRegisterWidget(SettingsModel* pSettingsModel, const QStrin
5965
_pUi->cmbDevice->addItem(QString(tr("Device %1").arg(devId)), devId);
6066
}
6167

68+
if (deviceList.isEmpty())
69+
{
70+
_pUi->btnAdd->setEnabled(false);
71+
_pUi->cmbDevice->setEnabled(false);
72+
}
73+
6274
connect(_pUi->btnAdd, &QPushButton::clicked, this, &AddRegisterWidget::handleResultAccept);
6375

6476
_axisGroup.setExclusive(true);
@@ -75,7 +87,12 @@ AddRegisterWidget::~AddRegisterWidget()
7587

7688
void AddRegisterWidget::handleResultAccept()
7789
{
78-
QString expression = generateExpression();
90+
const QString expression = generateExpression();
91+
if (expression.isEmpty())
92+
{
93+
return;
94+
}
95+
7996
GraphData graphData;
8097

8198
graphData.setLabel(_pUi->lineName->text());
@@ -107,10 +124,19 @@ void AddRegisterWidget::resetFields()
107124

108125
QString AddRegisterWidget::generateExpression()
109126
{
127+
if (_pUi->cmbDevice->count() == 0)
128+
{
129+
return QString();
130+
}
131+
110132
const QJsonObject addressValues = _pAddressForm->values();
133+
if (!addressValues.contains("objectType") || !addressValues.contains("address"))
134+
{
135+
return QString();
136+
}
137+
111138
const QString objectType = addressValues["objectType"].toString();
112139
const int address = addressValues["address"].toInt();
113-
114140
const QString typeId = _pUi->cmbType->currentData().toString();
115141

116142
deviceId_t deviceId = Device::cFirstDeviceId;

src/dialogs/registerdialog.cpp

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,8 @@ RegisterDialog::RegisterDialog(GraphDataModel* pGraphDataModel, SettingsModel* p
3939

4040
/* Except following columns */
4141
_pUi->registerView->horizontalHeader()->setSectionResizeMode(GraphDataModel::column::TEXT, QHeaderView::Stretch);
42-
_pUi->registerView->horizontalHeader()->setSectionResizeMode(GraphDataModel::column::EXPRESSION, QHeaderView::Stretch);
42+
_pUi->registerView->horizontalHeader()->setSectionResizeMode(GraphDataModel::column::EXPRESSION,
43+
QHeaderView::Stretch);
4344

4445
auto triggers = QAbstractItemView::DoubleClicked | QAbstractItemView::EditKeyPressed;
4546
_pUi->registerView->setEditTriggers(triggers);
@@ -68,20 +69,23 @@ RegisterDialog::RegisterDialog(GraphDataModel* pGraphDataModel, SettingsModel* p
6869

6970
const QStringList ids = _pSettingsModel->adapterIds();
7071
const QString adapterId = ids.isEmpty() ? QString() : ids.first();
71-
auto registerPopupMenu = new AddRegisterWidget(_pSettingsModel, adapterId, this);
72-
connect(registerPopupMenu, &AddRegisterWidget::graphDataConfigured, this, &RegisterDialog::addRegister);
72+
if (!adapterId.isEmpty())
73+
{
74+
auto registerPopupMenu = new AddRegisterWidget(_pSettingsModel, adapterId, this);
75+
connect(registerPopupMenu, &AddRegisterWidget::graphDataConfigured, this, &RegisterDialog::addRegister);
7376

74-
_registerPopupAction = std::make_unique<QWidgetAction>(this);
75-
_registerPopupAction->setDefaultWidget(registerPopupMenu);
76-
_pUi->btnAdd->addAction(_registerPopupAction.get());
77+
_registerPopupAction = std::make_unique<QWidgetAction>(this);
78+
_registerPopupAction->setDefaultWidget(registerPopupMenu);
79+
_pUi->btnAdd->addAction(_registerPopupAction.get());
80+
}
7781
}
7882

7983
RegisterDialog::~RegisterDialog()
8084
{
8185
delete _pUi;
8286
}
8387

84-
void RegisterDialog::addRegister(const GraphData &graphData)
88+
void RegisterDialog::addRegister(const GraphData& graphData)
8589
{
8690
_pGraphDataModel->add(graphData);
8791
}
@@ -93,10 +97,7 @@ void RegisterDialog::addDefaultRegister()
9397

9498
void RegisterDialog::activatedCell(QModelIndex modelIndex)
9599
{
96-
if (
97-
(modelIndex.column() == GraphDataModel::column::COLOR)
98-
&& (modelIndex.row() < _pGraphDataModel->size())
99-
)
100+
if ((modelIndex.column() == GraphDataModel::column::COLOR) && (modelIndex.row() < _pGraphDataModel->size()))
100101
{
101102
QColor color = QColorDialog::getColor(_pGraphDataModel->color(modelIndex.row()));
102103

@@ -107,7 +108,7 @@ void RegisterDialog::activatedCell(QModelIndex modelIndex)
107108
}
108109
}
109110

110-
void RegisterDialog::onRegisterInserted(const QModelIndex &parent, int first, int last)
111+
void RegisterDialog::onRegisterInserted(const QModelIndex& parent, int first, int last)
111112
{
112113
Q_UNUSED(parent);
113114
Q_UNUSED(last);
@@ -119,14 +120,14 @@ void RegisterDialog::onRegisterInserted(const QModelIndex &parent, int first, in
119120
void RegisterDialog::removeRegisterRow()
120121
{
121122
// get list of selected rows
122-
QItemSelectionModel *selected = _pUi->registerView->selectionModel();
123+
QItemSelectionModel* selected = _pUi->registerView->selectionModel();
123124
QModelIndexList rowList = selected->selectedRows();
124125

125126
// sort QModelIndexList
126127
// We need to remove the highest rows first
127128
std::sort(rowList.begin(), rowList.end(), &RegisterDialog::sortRegistersLastFirst);
128129

129-
foreach(QModelIndex rowIndex, rowList)
130+
foreach (QModelIndex rowIndex, rowList)
130131
{
131132
_pGraphDataModel->removeRow(rowIndex.row());
132133
}
@@ -169,7 +170,7 @@ int RegisterDialog::selectedRowAfterDelete(int deletedStartIndex, int rowCnt)
169170
return nextSelectedRow;
170171
}
171172

172-
bool RegisterDialog::sortRegistersLastFirst(const QModelIndex &s1, const QModelIndex &s2)
173+
bool RegisterDialog::sortRegistersLastFirst(const QModelIndex& s1, const QModelIndex& s2)
173174
{
174175
return s1.row() > s2.row();
175176
}

src/models/adapterdata.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ void AdapterData::setHasStoredConfig(bool hasStoredConfig)
4343
_hasStoredConfig = hasStoredConfig;
4444
}
4545

46+
//! \brief Sets the register schema received from adapter.registerSchema.
47+
//! \param schema The full register schema JSON object.
4648
void AdapterData::setRegisterSchema(const QJsonObject& schema)
4749
{
4850
_registerSchema = schema;
@@ -88,6 +90,8 @@ bool AdapterData::hasStoredConfig() const
8890
return _hasStoredConfig;
8991
}
9092

93+
//! \brief Returns the register schema set via setRegisterSchema.
94+
//! \return The register schema JSON object, or an empty object if not yet set.
9195
QJsonObject AdapterData::registerSchema() const
9296
{
9397
return _registerSchema;

src/util/expressiongenerator.cpp

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11

22
#include "expressiongenerator.h"
33
#include "models/device.h"
4+
#include "util/scopelogging.h"
45

56
#include <QStringLiteral>
67

@@ -22,8 +23,8 @@ QString typeSuffix(const QString& typeId)
2223
/*!
2324
* \brief Returns the single-character address prefix for an object type string.
2425
* \param objectType One of "coil", "discrete-input", "input-register", "holding-register".
25-
* \return The corresponding prefix character ("c", "d", "i", or "h").
26-
* Returns "h" for unknown values as a safe default.
26+
* \return The corresponding prefix character ("c", "d", "i", or "h"),
27+
* or an empty string if \a objectType is not a recognised value.
2728
*/
2829
QString objectTypeToAddressPrefix(const QString& objectType)
2930
{
@@ -39,7 +40,12 @@ QString objectTypeToAddressPrefix(const QString& objectType)
3940
{
4041
return QStringLiteral("i");
4142
}
42-
return QStringLiteral("h");
43+
if (objectType == QStringLiteral("holding-register"))
44+
{
45+
return QStringLiteral("h");
46+
}
47+
qCWarning(scopeGeneralInfo) << "objectTypeToAddressPrefix: unknown objectType:" << objectType;
48+
return QString();
4349
}
4450

4551
/*!
@@ -48,11 +54,16 @@ QString objectTypeToAddressPrefix(const QString& objectType)
4854
* \param address Zero-based register address within the object type space.
4955
* \param typeId Data type identifier (e.g. "16b", "f32b").
5056
* \param devId Device identifier; omitted from the string when it equals the first device ID.
51-
* \return A register expression string such as \c ${h0}, \c ${h5@2:f32b}.
57+
* \return A register expression string such as \c ${h0}, \c ${h5@2:f32b},
58+
* or an empty string if \a objectType is not recognised.
5259
*/
5360
QString constructRegisterString(const QString& objectType, int address, const QString& typeId, deviceId_t devId)
5461
{
5562
const QString prefix = objectTypeToAddressPrefix(objectType);
63+
if (prefix.isEmpty())
64+
{
65+
return QString();
66+
}
5667
const QString suffix = typeSuffix(typeId);
5768
const QString connStr = devId != Device::cFirstDeviceId ? QString("@%1").arg(devId) : QString();
5869

tests/ProtocolAdapter/tst_adapterclient.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -761,6 +761,27 @@ void TestAdapterClient::validateRegisterInvalid()
761761
QCOMPARE(spyValidate.at(0).at(1).toString(), QStringLiteral("Unknown type 'bad'"));
762762
}
763763

764+
void TestAdapterClient::validateRegisterInActiveState()
765+
{
766+
auto* mock = new MockAdapterProcess();
767+
AdapterClient client(mock);
768+
769+
QSignalSpy spyValidate(&client, &AdapterClient::validateRegisterResult);
770+
771+
driveToActive(client, mock);
772+
773+
client.validateRegister(QStringLiteral("${h0}"));
774+
775+
QCOMPARE(mock->sentRequests.last().method, QStringLiteral("adapter.validateRegister"));
776+
QCOMPARE(mock->sentRequests.last().params["expression"].toString(), QStringLiteral("${h0}"));
777+
778+
mock->injectResponse(5, "adapter.validateRegister", QJsonObject{ { "valid", true } });
779+
780+
QCOMPARE(spyValidate.count(), 1);
781+
QCOMPARE(spyValidate.at(0).at(0).toBool(), true);
782+
QCOMPARE(spyValidate.at(0).at(1).toString(), QString());
783+
}
784+
764785
void TestAdapterClient::validateRegisterInWrongStateIgnored()
765786
{
766787
auto* mock = new MockAdapterProcess();

tests/ProtocolAdapter/tst_adapterclient.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ private slots:
4141
void describeRegisterInWrongStateIgnored();
4242
void validateRegisterValid();
4343
void validateRegisterInvalid();
44+
void validateRegisterInActiveState();
4445
void validateRegisterInWrongStateIgnored();
4546
};
4647

tests/dialogs/tst_addregisterwidget.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ QJsonObject TestAddRegisterWidget::buildTestRegisterSchema()
6262

6363
void TestAddRegisterWidget::init()
6464
{
65+
_settingsModel.removeAllDevice();
6566
_settingsModel.setAdapterRegisterSchema("modbus", buildTestRegisterSchema());
6667
_settingsModel.deviceSettings(Device::cFirstDeviceId)->setAdapterId("modbus");
6768
_pRegWidget = new AddRegisterWidget(&_settingsModel, QStringLiteral("modbus"));

tests/models/tst_adapterdata.cpp

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -131,10 +131,7 @@ void TestAdapterData::settingsModelAdapterDataCreatesEntry()
131131
/* First access creates a default entry */
132132
const AdapterData* data = model.adapterData("modbus");
133133
QVERIFY(data != nullptr);
134-
if (data == nullptr)
135-
{
136-
return;
137-
}
134+
// NOLINTNEXTLINE(clang-analyzer-core.CallAndMessage) -- QVERIFY aborts if null
138135
QVERIFY(data->name().isEmpty());
139136

140137
/* updateAdapterFromDescribe updates the same entry in place */

0 commit comments

Comments
 (0)