Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/dev'
Browse files Browse the repository at this point in the history
  • Loading branch information
sanny32 committed Dec 5, 2024
2 parents 28c370e + 3eae75b commit f7cb79c
Show file tree
Hide file tree
Showing 14 changed files with 205 additions and 96 deletions.
7 changes: 6 additions & 1 deletion omodsim/connectiondetails.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,18 @@
struct TcpConnectionParams
{
quint16 ServicePort = 502;
const QString IPAddress = "0.0.0.0";
QString IPAddress = "0.0.0.0";

void normalize()
{
const auto addr = QHostAddress(IPAddress);
IPAddress = addr.isNull() ? "0.0.0.0" : addr.toString();
ServicePort = qMax<quint16>(1, ServicePort);
}

TcpConnectionParams& operator=(const TcpConnectionParams& params)
{
IPAddress = params.IPAddress;
ServicePort = params.ServicePort;
return *this;
}
Expand All @@ -43,6 +46,7 @@ Q_DECLARE_METATYPE(TcpConnectionParams)
inline QDataStream& operator <<(QDataStream& out, const TcpConnectionParams& params)
{
out << params.ServicePort;
out << params.IPAddress;
return out;
}

Expand All @@ -55,6 +59,7 @@ inline QDataStream& operator <<(QDataStream& out, const TcpConnectionParams& par
inline QDataStream& operator >>(QDataStream& in, TcpConnectionParams& params)
{
in >> params.ServicePort;
in >> params.IPAddress;
params.normalize();
return in;
}
Expand Down
17 changes: 8 additions & 9 deletions omodsim/controls/mainstatusbar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ MainStatusBar::MainStatusBar(const ModbusMultiServer& server, QWidget* parent)
auto label = new QLabel(this);
label->setFrameShadow(QFrame::Sunken);
label->setFrameShape(QFrame::Panel);
label->setMinimumWidth(120);
label->setMinimumWidth(80);
label->setProperty("ConnectionDetails", QVariant::fromValue(cd));

updateConnectionInfo(label, cd);
Expand All @@ -60,9 +60,8 @@ MainStatusBar::MainStatusBar(const ModbusMultiServer& server, QWidget* parent)
{
if(cd == label->property("ConnectionDetails").value<ConnectionDetails>())
{
_labels.removeOne(label);
removeWidget(label);
delete label;
_labels.removeOne(label);

break;
}
Expand Down Expand Up @@ -107,15 +106,15 @@ void MainStatusBar::updateConnectionInfo(QLabel* label, const ConnectionDetails&
switch(cd.Type)
{
case ConnectionType::Tcp:
label->setText(QString(tr("Modbus/TCP Srv: %1")).arg(cd.TcpParams.ServicePort));
label->setText(QString(tr("Modbus/TCP Srv %1:%2")).arg(cd.TcpParams.IPAddress, QString::number(cd.TcpParams.ServicePort)));
break;

case ConnectionType::Serial:
label->setText(QString(tr("Port %1:%2:%3:%4:%5 ")).arg(cd.SerialParams.PortName,
QString::number(cd.SerialParams.BaudRate),
QString::number(cd.SerialParams.WordLength),
Parity_toString(cd.SerialParams.Parity),
QString::number(cd.SerialParams.StopBits)));
label->setText(QString(tr("Port %1:%2:%3:%4:%5")).arg(cd.SerialParams.PortName,
QString::number(cd.SerialParams.BaudRate),
QString::number(cd.SerialParams.WordLength),
Parity_toString(cd.SerialParams.Parity),
QString::number(cd.SerialParams.StopBits)));
break;
}
}
4 changes: 2 additions & 2 deletions omodsim/controls/numericlineedit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -441,7 +441,7 @@ void NumericLineEdit::on_rangeChanged(const QVariant& bottom, const QVariant& to
{
const int nums = QString::number(top.toInt()).length();
_paddingZeroWidth = qMax(1, nums);
setMaxLength(qMax(1, nums));
setMaxLength(qMax(2, nums + 1));
setValidator(new QIntValidator(bottom.toInt(), top.toInt(), this));

}
Expand Down Expand Up @@ -475,7 +475,7 @@ void NumericLineEdit::on_rangeChanged(const QVariant& bottom, const QVariant& to
{
const int nums = QString::number(top.toLongLong()).length();
_paddingZeroWidth = qMax(1, nums);
setMaxLength(qMax(1, nums));
setMaxLength(qMax(2, nums + 1));
setValidator(new QInt64Validator(bottom.toLongLong(), top.toLongLong(), this));
}
break;
Expand Down
33 changes: 29 additions & 4 deletions omodsim/dialogs/dialogselectserviceport.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#include <QNetworkInterface>
#include "dialogselectserviceport.h"
#include "ui_dialogselectserviceport.h"

Expand All @@ -6,14 +7,37 @@
/// \param port
/// \param parent
///
DialogSelectServicePort::DialogSelectServicePort(quint16& port, QWidget *parent)
DialogSelectServicePort::DialogSelectServicePort(TcpConnectionParams& params, QWidget *parent)
: QFixedSizeDialog(parent)
, ui(new Ui::DialogSelectServicePort)
,_port(port)
,_params(params)
{
ui->setupUi(this);
ui->lineEditPort->setInputRange(0, 65535);
ui->lineEditPort->setValue(_port);
ui->lineEditPort->setValue(params.ServicePort);

ui->comboBoxIp->addItem("0.0.0.0");
for(auto&& nic : QNetworkInterface::allInterfaces())
{
if(!(nic.flags() & QNetworkInterface::IsRunning))
{
continue;
}

for(auto&& entry : nic.addressEntries())
{
const auto addr = entry.ip();

if(addr.isNull() || addr.isMulticast() || addr.protocol() != QAbstractSocket::IPv4Protocol)
{
continue;
}

ui->comboBoxIp->addItem(addr.toString());
}
}

ui->comboBoxIp->setCurrentText(_params.IPAddress);
}

///
Expand All @@ -29,6 +53,7 @@ DialogSelectServicePort::~DialogSelectServicePort()
///
void DialogSelectServicePort::accept()
{
_port = ui->lineEditPort->value<int>();
_params.ServicePort = ui->lineEditPort->value<int>();
_params.IPAddress = ui->comboBoxIp->currentText();
QFixedSizeDialog::accept();
}
5 changes: 3 additions & 2 deletions omodsim/dialogs/dialogselectserviceport.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#define DIALOGSELECTSERVICEPORT_H

#include "qfixedsizedialog.h"
#include "connectiondetails.h"

namespace Ui {
class DialogSelectServicePort;
Expand All @@ -12,14 +13,14 @@ class DialogSelectServicePort : public QFixedSizeDialog
Q_OBJECT

public:
explicit DialogSelectServicePort(quint16& port, QWidget *parent = nullptr);
explicit DialogSelectServicePort(TcpConnectionParams& params, QWidget *parent = nullptr);
~DialogSelectServicePort();

void accept() override;

private:
Ui::DialogSelectServicePort *ui;
quint16& _port;
TcpConnectionParams& _params;
};

#endif // DIALOGSELECTSERVICEPORT_H
59 changes: 23 additions & 36 deletions omodsim/dialogs/dialogselectserviceport.ui
Original file line number Diff line number Diff line change
Expand Up @@ -6,40 +6,20 @@
<rect>
<x>0</x>
<y>0</y>
<width>240</width>
<width>328</width>
<height>116</height>
</rect>
</property>
<property name="windowTitle">
<string>Select Service Port</string>
<string>Select Service IP Address and Port</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<widget class="QLabel" name="label">
<property name="text">
<string>Modbus/TCP Service Port</string>
<layout class="QFormLayout" name="formLayout">
<property name="labelAlignment">
<set>Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter</set>
</property>
<property name="alignment">
<set>Qt::AlignCenter</set>
</property>
</widget>
</item>
<item>
<layout class="QHBoxLayout" name="horizontalLayout">
<item>
<spacer name="horizontalSpacer">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
<item>
<item row="1" column="1">
<widget class="NumericLineEdit" name="lineEditPort">
<property name="sizePolicy">
<sizepolicy hsizetype="Fixed" vsizetype="Fixed">
Expand All @@ -58,18 +38,25 @@
</property>
</widget>
</item>
<item>
<spacer name="horizontalSpacer_2">
<property name="orientation">
<enum>Qt::Horizontal</enum>
<item row="1" column="0">
<widget class="QLabel" name="label">
<property name="text">
<string>Modbus/TCP Service Port</string>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<height>20</height>
</size>
<property name="alignment">
<set>Qt::AlignCenter</set>
</property>
</spacer>
</widget>
</item>
<item row="0" column="1">
<widget class="QComboBox" name="comboBoxIp"/>
</item>
<item row="0" column="0">
<widget class="QLabel" name="label_2">
<property name="text">
<string>Service IP Address</string>
</property>
</widget>
</item>
</layout>
</item>
Expand Down
3 changes: 2 additions & 1 deletion omodsim/formmodsim.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,6 @@ FormModSim::FormModSim(int id, ModbusMultiServer& server, QSharedPointer<DataSim
///
FormModSim::~FormModSim()
{
_mbMultiServer.removeUnitMap(formId());
delete ui;
}

Expand All @@ -94,6 +93,8 @@ void FormModSim::changeEvent(QEvent* e)
///
void FormModSim::closeEvent(QCloseEvent *event)
{
_mbMultiServer.removeUnitMap(formId());

emit closing();
QWidget::closeEvent(event);
}
Expand Down
13 changes: 7 additions & 6 deletions omodsim/mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -453,7 +453,7 @@ void MainWindow::on_connectAction(ConnectionDetails& cd)
{
case ConnectionType::Tcp:
{
DialogSelectServicePort dlg(cd.TcpParams.ServicePort, this);
DialogSelectServicePort dlg(cd.TcpParams, this);
if(dlg.exec() == QDialog::Accepted) _mbMultiServer.connectDevice(cd);
}
break;
Expand Down Expand Up @@ -1255,11 +1255,6 @@ void MainWindow::loadConfig(const QString& filename)
return;

ui->mdiArea->closeAllSubWindows();
for(auto&& filename: listFilename)
{
if(!filename.isEmpty())
openFile(filename);
}

auto menu = qobject_cast<MenuConnect*>(ui->actionConnect->menu());
menu->updateConnectionDetails(conns);
Expand All @@ -1269,6 +1264,12 @@ void MainWindow::loadConfig(const QString& filename)
if(menu->canConnect(cd))
_mbMultiServer.connectDevice(cd);
}

for(auto&& filename: listFilename)
{
if(!filename.isEmpty())
openFile(filename);
}
}

///
Expand Down
Loading

0 comments on commit f7cb79c

Please sign in to comment.