11#include " addregisterwidget.h"
22#include " ui_addregisterwidget.h"
33
4+ #include " communication/modbuspoll.h"
45#include " customwidgets/schemaformwidget.h"
56#include " models/adapterdata.h"
67#include " models/device.h"
78#include " models/settingsmodel.h"
8- #include " util/expressiongenerator.h"
99
1010#include < QJsonArray>
1111#include < QVBoxLayout>
1414 * \brief Constructs the widget and populates it from the adapter's register schema.
1515 * \param pSettingsModel Pointer to the application settings model.
1616 * \param adapterId Identifier of the adapter whose register schema to use.
17+ * \param pModbusPoll Pointer to the Modbus poll instance used to build expression strings.
1718 * \param parent Optional parent widget.
1819 */
19- AddRegisterWidget::AddRegisterWidget (SettingsModel* pSettingsModel, const QString& adapterId, QWidget* parent)
20+ AddRegisterWidget::AddRegisterWidget (SettingsModel* pSettingsModel,
21+ const QString& adapterId,
22+ ModbusPoll* pModbusPoll,
23+ QWidget* parent)
2024 : QWidget(parent),
2125 _pUi(new Ui::AddRegisterWidget),
2226 _pAddressForm(new SchemaFormWidget(this )),
23- _pSettingsModel(pSettingsModel)
27+ _pSettingsModel(pSettingsModel),
28+ _pModbusPoll(pModbusPoll)
2429{
2530 _pUi->setupUi (this );
2631
@@ -72,6 +77,7 @@ AddRegisterWidget::AddRegisterWidget(SettingsModel* pSettingsModel, const QStrin
7277 }
7378
7479 connect (_pUi->btnAdd , &QPushButton::clicked, this , &AddRegisterWidget::handleResultAccept);
80+ connect (_pModbusPoll, &ModbusPoll::buildExpressionResult, this , &AddRegisterWidget::onBuildExpressionResult);
7581
7682 _axisGroup.setExclusive (true );
7783 _axisGroup.addButton (_pUi->radioPrimary );
@@ -87,28 +93,38 @@ AddRegisterWidget::~AddRegisterWidget()
8793
8894void AddRegisterWidget::handleResultAccept ()
8995{
90- const QString expression = generateExpression ();
91- if (expression.isEmpty ())
96+ if (_pUi->cmbDevice ->count () == 0 )
9297 {
9398 return ;
9499 }
95100
96- GraphData graphData ;
101+ collectPendingGraphData () ;
97102
98- graphData.setLabel (_pUi->lineName ->text ());
103+ const QJsonObject addressValues = _pAddressForm->values ();
104+ const QString typeId = _pUi->cmbType ->currentData ().toString ();
99105
100- if (_pUi->radioSecondary ->isChecked ())
106+ deviceId_t deviceId = Device::cFirstDeviceId;
107+ const QVariant devData = _pUi->cmbDevice ->currentData ();
108+ if (devData.canConvert <deviceId_t>())
101109 {
102- graphData. setValueAxis (GraphData::VALUE_AXIS_SECONDARY );
110+ deviceId = devData. value <deviceId_t>( );
103111 }
104- else
112+
113+ _pUi->btnAdd ->setEnabled (false );
114+ _pModbusPoll->buildExpression (addressValues, typeId, deviceId);
115+ }
116+
117+ void AddRegisterWidget::onBuildExpressionResult (const QString& expression)
118+ {
119+ _pUi->btnAdd ->setEnabled (true );
120+
121+ if (expression.isEmpty ())
105122 {
106- graphData. setValueAxis (GraphData::VALUE_AXIS_PRIMARY) ;
123+ return ;
107124 }
108125
109- graphData.setExpression (expression);
110-
111- emit graphDataConfigured (graphData);
126+ _pendingGraphData.setExpression (expression);
127+ emit graphDataConfigured (_pendingGraphData);
112128
113129 resetFields ();
114130}
@@ -122,29 +138,23 @@ void AddRegisterWidget::resetFields()
122138 _pAddressForm->setSchema (_addressSchema, QJsonObject ());
123139}
124140
125- QString AddRegisterWidget::generateExpression ()
141+ /* !
142+ * \brief Captures the current non-expression fields into \a _pendingGraphData.
143+ *
144+ * Called just before the async adapter.buildExpression request is sent, so that
145+ * the label and value axis are snapshotted at click time.
146+ */
147+ void AddRegisterWidget::collectPendingGraphData ()
126148{
127- if (_pUi->cmbDevice ->count () == 0 )
128- {
129- return QString ();
130- }
149+ _pendingGraphData = GraphData ();
150+ _pendingGraphData.setLabel (_pUi->lineName ->text ());
131151
132- const QJsonObject addressValues = _pAddressForm->values ();
133- if (!addressValues.contains (" objectType" ) || !addressValues.contains (" address" ))
152+ if (_pUi->radioSecondary ->isChecked ())
134153 {
135- return QString ( );
154+ _pendingGraphData. setValueAxis (GraphData::VALUE_AXIS_SECONDARY );
136155 }
137-
138- const QString objectType = addressValues[" objectType" ].toString ();
139- const int address = addressValues[" address" ].toInt ();
140- const QString typeId = _pUi->cmbType ->currentData ().toString ();
141-
142- deviceId_t deviceId = Device::cFirstDeviceId;
143- const QVariant devData = _pUi->cmbDevice ->currentData ();
144- if (devData.canConvert <deviceId_t>())
156+ else
145157 {
146- deviceId = devData. value <deviceId_t>( );
158+ _pendingGraphData. setValueAxis (GraphData::VALUE_AXIS_PRIMARY );
147159 }
148-
149- return ExpressionGenerator::constructRegisterString (objectType, address, typeId, deviceId);
150160}
0 commit comments