chore: update files of project

This commit is contained in:
x-tools-author 2024-03-30 13:15:01 +08:00
parent 1ebcda28c7
commit 5e2f9f624d
13 changed files with 258 additions and 89 deletions

View File

@ -18,25 +18,25 @@ xToolsCheckBox::xToolsCheckBox(QWidget* parent)
void xToolsCheckBox::setGroupKey(const QString& group, const QString& key)
{
mKey = group + "/" + key;
m_key = group + "/" + key;
readFromSettingsFile();
}
void xToolsCheckBox::readFromSettingsFile()
{
if (mKey.isEmpty()) {
if (m_key.isEmpty()) {
return;
}
bool ret = xToolsSettings::instance()->value(mKey).toBool();
bool ret = xToolsSettings::instance()->value(m_key).toBool();
setChecked(ret);
}
void xToolsCheckBox::writeToSettingsFile()
{
if (mKey.isEmpty()) {
if (m_key.isEmpty()) {
return;
}
xToolsSettings::instance()->setValue(mKey, isChecked());
xToolsSettings::instance()->setValue(m_key, isChecked());
}

View File

@ -18,7 +18,7 @@ public:
void setGroupKey(const QString &group, const QString &key);
private:
QString mKey;
QString m_key;
private:
void readFromSettingsFile();

View File

@ -36,7 +36,6 @@
#include "xToolsToolBoxUiOutputMenu.h"
#include "xToolsToolFactory.h"
#include "xToolsUdpTransmitterToolUi.h"
#include "xToolsApplication.h"
#include "xToolsWebSocketTransmitterToolUi.h"
#ifdef X_TOOLS_IMPORT_MODULE_BLUETOOTH
@ -68,7 +67,7 @@ xToolsToolBoxUi::~xToolsToolBoxUi()
QList<int> xToolsToolBoxUi::supportedCommunicationTools()
{
QList<int> list;
list << xToolsToolFactory::SerialportTool
list << xToolsToolFactory::SerialPortTool
#ifdef X_TOOLS_IMPORT_MODULE_BLUETOOTH
<< xToolsToolFactory::BleCentralTool
#endif
@ -80,7 +79,7 @@ QList<int> xToolsToolBoxUi::supportedCommunicationTools()
QString xToolsToolBoxUi::communicationToolName(int type)
{
if (type == xToolsToolFactory::SerialportTool) {
if (type == xToolsToolFactory::SerialPortTool) {
return tr("SerialPort");
} else if (type == xToolsToolFactory::UdpClientTool) {
return tr("UDP Client");
@ -104,7 +103,7 @@ QString xToolsToolBoxUi::communicationToolName(int type)
QIcon xToolsToolBoxUi::communicationToolIcon(int type)
{
QString fileName;
if (type == xToolsToolFactory::SerialportTool) {
if (type == xToolsToolFactory::SerialPortTool) {
fileName = ":/Resources/Icons/IconSerialPort.svg";
} else if (type == xToolsToolFactory::UdpClientTool) {
fileName = ":/Resources/Icons/IconUdpClient.svg";
@ -154,7 +153,7 @@ void xToolsToolBoxUi::initialize(int type)
xToolsCommunicationToolUi* xToolsToolBoxUi::communicationToolUi(int type)
{
xToolsCommunicationToolUi* w = nullptr;
if (type == xToolsToolFactory::SerialportTool) {
if (type == xToolsToolFactory::SerialPortTool) {
w = new xToolsSerialPortToolUi();
} else if (type == xToolsToolFactory::UdpClientTool) {
w = new xToolsSocketClientToolUi();
@ -265,7 +264,7 @@ void xToolsToolBoxUi::output2ui(const QByteArray& bytes, const QString& flag, bo
QString xToolsToolBoxUi::settingsGroup()
{
if (m_communicationType == xToolsToolFactory::SerialportTool) {
if (m_communicationType == xToolsToolFactory::SerialPortTool) {
return "SerialportToolBox";
} else if (m_communicationType == xToolsToolFactory::UdpClientTool) {
return "UdpClientToolBox";
@ -295,7 +294,7 @@ QByteArray xToolsToolBoxUi::calculateCrc(const QByteArray& bytes, bool fixedOrig
int format = ui->comboBoxInputFormat->currentData().toInt();
QString input = ui->comboBoxInputText->currentText();
int esc = ctx.escapeCharacter;
input = xToolsDataStructure::cookEscapeCharacter(esc, input);
inputBytes = xToolsDataStructure::stringToByteArray(input, format);
}

View File

@ -81,3 +81,36 @@ void xToolsSocketClientTool::setMessageType(int type)
m_messageType = type;
emit messageTypeChanged();
}
bool xToolsSocketClientTool::authentication()
{
return m_authentication;
}
void xToolsSocketClientTool::setAuthentication(bool authentication)
{
m_authentication = authentication;
emit authenticationChanged();
}
QString xToolsSocketClientTool::userName()
{
return m_userName;
}
void xToolsSocketClientTool::setUserName(const QString &userName)
{
m_userName = userName;
emit userNameChanged();
}
QString xToolsSocketClientTool::password()
{
return m_password;
}
void xToolsSocketClientTool::setPassword(const QString &password)
{
m_password = password;
emit passwordChanged();
}

View File

@ -22,6 +22,9 @@ class xToolsSocketClientTool : public xToolsCommunicationTool
Q_PROPERTY(QString bindingIpPort READ bindingIpPort NOTIFY bindingIpPortChanged)
// Just for web socket client.
Q_PROPERTY(int messageType READ messageType WRITE setMessageType NOTIFY messageTypeChanged)
Q_PROPERTY(bool authentication READ authentication WRITE setAuthentication NOTIFY authenticationChanged)
Q_PROPERTY(QString userName READ userName WRITE setUserName NOTIFY userNameChanged)
Q_PROPERTY(QString password READ password WRITE setPassword NOTIFY passwordChanged)
// clang-format on
public:
explicit xToolsSocketClientTool(QObject *parent = nullptr);
@ -40,6 +43,12 @@ public:
QString bindingIpPort();
int messageType();
void setMessageType(int type);
bool authentication();
void setAuthentication(bool authentication);
QString userName();
void setUserName(const QString &userName);
QString password();
void setPassword(const QString &password);
protected:
QString m_clientIp;
@ -49,6 +58,10 @@ protected:
int m_serverPort;
QString m_bindingIpPort;
qint8 m_messageType;
QString m_peerInfo;
QString m_userName;
QString m_password;
bool m_authentication;
signals:
void clientIpChanged();
@ -58,4 +71,7 @@ signals:
void serverPortChanged();
void bindingIpPortChanged();
void messageTypeChanged();
void authenticationChanged();
void userNameChanged();
void passwordChanged();
};

View File

@ -50,7 +50,7 @@ xToolsBaseTool *xToolsToolFactory::createTool(int type)
xToolsBaseTool *tool{nullptr};
if (AnalyzerTool == type) {
tool = new xToolsAnalyzerTool();
} else if (SerialportTool == type) {
} else if (SerialPortTool == type) {
tool = new xToolsSerialPortTool();
} else if (EmitterTool == type) {
tool = new xToolsEmitterTool();
@ -119,7 +119,7 @@ QString xToolsToolFactory::toolName(int type)
static QMap<int, QString> map;
if (map.isEmpty()) {
map.insert(AnalyzerTool, tr("Analyzer"));
map.insert(SerialportTool, tr("Serialport"));
map.insert(SerialPortTool, tr("Serialport"));
map.insert(EmitterTool, tr("Emitter"));
map.insert(MaskerTool, tr("Masker"));
map.insert(ResponserTool, tr("Responser"));

View File

@ -18,7 +18,7 @@ class xToolsToolFactory : public QObject
public:
enum ToolsType {
AnalyzerTool,
SerialportTool,
SerialPortTool,
EmitterTool,
MaskerTool,
ResponserTool,

View File

@ -58,10 +58,21 @@ bool xToolsWebSocketClientTool::initialize(QString &errStr)
QString errStr = m_webSocket->errorString();
emit errorOccurred(errStr);
});
QString address = "ws://" + m_serverIp + ":" + QString::number(m_serverPort);
qDebug() << "Server url: " + address;
m_webSocket->open(address);
qInfo() << "Server url: " + address;
if (m_authentication) {
QNetworkRequest request(address);
QString username = m_userName;
QString password = m_password;
QString concatenated = username + ":" + password;
QByteArray data = concatenated.toLocal8Bit().toBase64();
QString headerData = "Basic " + data;
request.setRawHeader("Authorization", headerData.toLocal8Bit());
m_webSocket->open(request);
} else {
m_webSocket->open(address);
}
return true;
}
@ -70,6 +81,7 @@ void xToolsWebSocketClientTool::writeBytes(const QByteArray &bytes)
{
qint64 ret = -1;
QString hex;
if (m_messageType == 0) {
hex = QString::fromLatin1(xToolsByteArrayToHex(bytes, ' '));
ret = m_webSocket->sendBinaryMessage(bytes);

View File

@ -25,5 +25,4 @@ protected:
private:
QWebSocket *m_webSocket{Q_NULLPTR};
QString m_peerInfo;
};

View File

@ -18,6 +18,7 @@ xToolsSocketClientToolUi::xToolsSocketClientToolUi(QWidget *parent)
, ui(new Ui::xToolsSocketClientToolUi)
{
ui->setupUi(this);
ui->widgetAuthentication->hide();
connect(ui->comboBoxClientAddress,
static_cast<void (QComboBox::*)(int)>(&QComboBox::activated),
@ -39,6 +40,19 @@ xToolsSocketClientToolUi::xToolsSocketClientToolUi(QWidget *parent)
&QCheckBox::clicked,
this,
&xToolsSocketClientToolUi::onCheckBoxSpecifyIpAndPortClicked);
connect(ui->checkBoxAuthentication,
&QCheckBox::clicked,
this,
&xToolsSocketClientToolUi::onAuthenticationCheckBoxClicked);
connect(ui->lineEditUserName,
&QLineEdit::textChanged,
this,
&xToolsSocketClientToolUi::onUserNameLineEditTextChanged);
connect(ui->lineEditPassword,
&QLineEdit::textChanged,
this,
&xToolsSocketClientToolUi::onPasswordLineEditTextChanged);
}
xToolsSocketClientToolUi::~xToolsSocketClientToolUi()
@ -53,7 +67,8 @@ void xToolsSocketClientToolUi::onIsWorkingChanged(bool isWorking)
ui->checkBoxSpecifyIpAndPort->setEnabled(!isWorking);
}
void xToolsSocketClientToolUi::onBaseToolUiInitialized(xToolsBaseTool *tool, const QString &settingsGroup)
void xToolsSocketClientToolUi::onBaseToolUiInitialized(xToolsBaseTool *tool,
const QString &settingsGroup)
{
if (!tool) {
return;
@ -67,6 +82,8 @@ void xToolsSocketClientToolUi::onBaseToolUiInitialized(xToolsBaseTool *tool, con
if (!tool->inherits("xToolsWebSocketClientTool")) {
ui->labelMessageType->hide();
ui->comboBoxMessageType->hide();
} else {
ui->widgetAuthentication->show();
}
mTool = qobject_cast<xToolsSocketClientTool *>(tool);
@ -81,6 +98,9 @@ void xToolsSocketClientToolUi::onBaseToolUiInitialized(xToolsBaseTool *tool, con
ui->comboBoxServerAddress->setGroupKey(settingsGroup, "serverAddress");
ui->spinBoxServerPort->setGroupKey(settingsGroup, "serverPort");
ui->comboBoxMessageType->setGroupKey(settingsGroup, "messageType");
ui->checkBoxAuthentication->setGroupKey(settingsGroup, "authentication");
ui->lineEditUserName->setGroupKey(settingsGroup, "username");
ui->lineEditPassword->setGroupKey(settingsGroup, "password");
mTool->setClientIp(ui->comboBoxClientAddress->currentText().trimmed());
mTool->setClientPort(ui->spinBoxClientPort->value());
@ -97,7 +117,7 @@ void xToolsSocketClientToolUi::onBaseToolUiInitialized(xToolsBaseTool *tool, con
});
}
void xToolsSocketClientToolUi::onComboBoxClientAddressActived()
void xToolsSocketClientToolUi::onComboBoxClientAddressActivated()
{
if (mTool) {
QString ip = ui->comboBoxClientAddress->currentText().trimmed();
@ -134,3 +154,25 @@ void xToolsSocketClientToolUi::onCheckBoxSpecifyIpAndPortClicked()
mTool->setSpecifyClientIpPort(checked);
}
}
void xToolsSocketClientToolUi::onAuthenticationCheckBoxClicked()
{
if (mTool) {
bool checked = ui->checkBoxAuthentication->isChecked();
mTool->setAuthentication(checked);
}
}
void xToolsSocketClientToolUi::onUserNameLineEditTextChanged(const QString &text)
{
if (mTool) {
mTool->setUserName(text);
}
}
void xToolsSocketClientToolUi::onPasswordLineEditTextChanged(const QString &text)
{
if (mTool) {
mTool->setPassword(text);
}
}

View File

@ -32,9 +32,13 @@ private:
private:
Ui::xToolsSocketClientToolUi *ui{nullptr};
void onComboBoxClientAddressActived();
public slots:
void onComboBoxClientAddressActivated();
void onSpinBoxClientPortValueChanged(int value);
void onComboBoxServerAddressCurrentTextChanged();
void onSpinBoxServerPortValueChanged(int value);
void onCheckBoxSpecifyIpAndPortClicked();
void onAuthenticationCheckBoxClicked();
void onUserNameLineEditTextChanged(const QString &text);
void onPasswordLineEditTextChanged(const QString &text);
};

View File

@ -6,8 +6,8 @@
<rect>
<x>0</x>
<y>0</y>
<width>165</width>
<height>217</height>
<width>141</width>
<height>247</height>
</rect>
</property>
<property name="windowTitle">
@ -26,17 +26,7 @@
<property name="bottomMargin">
<number>0</number>
</property>
<item row="2" column="1">
<widget class="xToolsSpinBox" name="spinBoxClientPort">
<property name="maximum">
<number>65535</number>
</property>
<property name="value">
<number>44444</number>
</property>
</widget>
</item>
<item row="7" column="1">
<item row="11" column="1">
<widget class="xToolsIpComboBox" name="comboBoxServerAddress">
<property name="sizePolicy">
<sizepolicy hsizetype="Ignored" vsizetype="Fixed">
@ -49,13 +39,6 @@
</property>
</widget>
</item>
<item row="6" column="0" colspan="2">
<widget class="Line" name="line">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
</widget>
</item>
<item row="2" column="0">
<widget class="QLabel" name="label_2">
<property name="sizePolicy">
@ -69,39 +52,6 @@
</property>
</widget>
</item>
<item row="5" column="1">
<widget class="QLabel" name="labelContext">
<property name="text">
<string>(closed)</string>
</property>
</widget>
</item>
<item row="7" column="0">
<widget class="QLabel" name="label_3">
<property name="sizePolicy">
<sizepolicy hsizetype="Fixed" vsizetype="Preferred">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="text">
<string>Server IP</string>
</property>
</widget>
</item>
<item row="8" column="0">
<widget class="QLabel" name="label_4">
<property name="sizePolicy">
<sizepolicy hsizetype="Fixed" vsizetype="Preferred">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="text">
<string>Server port</string>
</property>
</widget>
</item>
<item row="5" column="0">
<widget class="QLabel" name="label_5">
<property name="sizePolicy">
@ -111,17 +61,7 @@
</sizepolicy>
</property>
<property name="text">
<string>Context</string>
</property>
</widget>
</item>
<item row="1" column="1">
<widget class="xToolsIpComboBox" name="comboBoxClientAddress">
<property name="sizePolicy">
<sizepolicy hsizetype="Ignored" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
<string>Binding info</string>
</property>
</widget>
</item>
@ -138,6 +78,19 @@
</property>
</widget>
</item>
<item row="11" column="0">
<widget class="QLabel" name="label_3">
<property name="sizePolicy">
<sizepolicy hsizetype="Fixed" vsizetype="Preferred">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="text">
<string>Server IP</string>
</property>
</widget>
</item>
<item row="1" column="0">
<widget class="QLabel" name="label">
<property name="sizePolicy">
@ -151,10 +104,43 @@
</property>
</widget>
</item>
<item row="2" column="1">
<widget class="xToolsSpinBox" name="spinBoxClientPort">
<property name="maximum">
<number>65535</number>
</property>
<property name="value">
<number>44444</number>
</property>
</widget>
</item>
<item row="1" column="1">
<widget class="xToolsIpComboBox" name="comboBoxClientAddress">
<property name="sizePolicy">
<sizepolicy hsizetype="Ignored" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
</widget>
</item>
<item row="12" column="0">
<widget class="QLabel" name="label_4">
<property name="sizePolicy">
<sizepolicy hsizetype="Fixed" vsizetype="Preferred">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="text">
<string>Server port</string>
</property>
</widget>
</item>
<item row="3" column="1">
<widget class="xToolsWebSocketMessageTypeComboBox" name="comboBoxMessageType"/>
</item>
<item row="8" column="1">
<item row="12" column="1">
<widget class="xToolsSpinBox" name="spinBoxServerPort">
<property name="maximum">
<number>65535</number>
@ -167,6 +153,20 @@
</property>
</widget>
</item>
<item row="5" column="1">
<widget class="QLabel" name="labelContext">
<property name="text">
<string>(closed)</string>
</property>
</widget>
</item>
<item row="10" column="0" colspan="2">
<widget class="Line" name="line">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
</widget>
</item>
<item row="4" column="0" colspan="2">
<widget class="xToolsCheckBox" name="checkBoxSpecifyIpAndPort">
<property name="text">
@ -174,6 +174,65 @@
</property>
</widget>
</item>
<item row="6" column="0" colspan="2">
<widget class="QWidget" name="widgetAuthentication" native="true">
<layout class="QGridLayout" name="gridLayout_4">
<property name="leftMargin">
<number>0</number>
</property>
<property name="topMargin">
<number>0</number>
</property>
<property name="rightMargin">
<number>0</number>
</property>
<property name="bottomMargin">
<number>0</number>
</property>
<item row="0" column="1" colspan="2">
<widget class="xToolsCheckBox" name="checkBoxAuthentication">
<property name="text">
<string>Authentication</string>
</property>
</widget>
</item>
<item row="1" column="2">
<widget class="xToolsLineEdit" name="lineEditUserName">
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
</widget>
</item>
<item row="1" column="1">
<widget class="QLabel" name="label_6">
<property name="text">
<string>User name</string>
</property>
</widget>
</item>
<item row="2" column="1">
<widget class="QLabel" name="label_7">
<property name="text">
<string>Password</string>
</property>
</widget>
</item>
<item row="2" column="2">
<widget class="xToolsLineEdit" name="lineEditPassword">
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
</widget>
</item>
</layout>
</widget>
</item>
</layout>
</widget>
<customwidgets>
@ -197,6 +256,11 @@
<extends>QComboBox</extends>
<header location="global">xToolsWebSocketMessageTypeComboBox.h</header>
</customwidget>
<customwidget>
<class>xToolsLineEdit</class>
<extends>QLineEdit</extends>
<header>xToolsLineEdit.h</header>
</customwidget>
</customwidgets>
<resources/>
<connections/>

View File

@ -52,7 +52,7 @@ xToolsBaseToolUi *xToolsToolUiFactory::createToolUi(int type)
switch (type) {
case xToolsToolFactory::AnalyzerTool:
return new xToolsAnalyzerToolUi();
case xToolsToolFactory::SerialportTool:
case xToolsToolFactory::SerialPortTool:
return new xToolsSerialPortToolUi();
case xToolsToolFactory::EmitterTool:
return new xToolsEmitterToolUi();