Add set ip with new IpSettingsDialog.

This commit is contained in:
sunwen
2024-05-08 15:20:28 +08:00
parent eb041549aa
commit 638a40343f
7 changed files with 75 additions and 15 deletions

View File

@@ -3,9 +3,12 @@
#include "components/ImageSwitch.h"
#include "components/ULineEdit.h"
#include "utilities/InputFormatValidator.h"
#include "device/networkmanager.h"
#include "DialogManager.h"
#include <QLabel>
#include <QVBoxLayout>
#include <QThread>
IpSettingsDialog::IpSettingsDialog(QWidget* aParent, Qt::WindowFlags aFlag)
: GUIFormBaseDialog(aParent, aFlag)
@@ -14,12 +17,20 @@ IpSettingsDialog::IpSettingsDialog(QWidget* aParent, Qt::WindowFlags aFlag)
, mIpAddress(new ULineEdit(this))
, mSubnetMask(new ULineEdit(this))
, mGateway(new ULineEdit(this))
, mThread(nullptr)
{
IpAddr addr = JsonObject::Instance()->getDefaultIpAddr();
mDhcpButton->setChecked(addr.dhcp);
mIpAddress->setText(addr.ip);
mSubnetMask->setText(addr.mask);
mGateway->setText(addr.gateway);
if(addr.dhcp)
{
mIpAddress->setEnabled(false);
mSubnetMask->setEnabled(false);
mGateway->setEnabled(false);
}
connect(mDhcpButton, &ImageSwitch::clicked, this, &IpSettingsDialog::handleDhcpClicked);
init();
}
@@ -28,29 +39,61 @@ IpSettingsDialog::~IpSettingsDialog()
}
void IpSettingsDialog::handleDhcpClicked()
{
bool isChecked = mDhcpButton->getChecked();
mIpAddress->setEnabled(!isChecked);
mSubnetMask->setEnabled(!isChecked);
mGateway->setEnabled(!isChecked);
}
bool IpSettingsDialog::updateReferenceData()
{
if(!InputFormatValidator::ValidateIpAddressFormat(mIpAddress->text()))
{
mErrorText->show();
mErrorText->setText("Ip Address is not valid");
mErrorText->setText(tr("Ip Address is not valid"));
return false;
}
if(!InputFormatValidator::ValidateIpAddressFormat(mSubnetMask->text()))
{
mErrorText->show();
mErrorText->setText("Subnet Mask is not valid");
mErrorText->setText(tr("Subnet Mask is not valid"));
return false;
}
if(!InputFormatValidator::ValidateIpAddressFormat(mGateway->text()))
{
mErrorText->show();
mErrorText->setText("Gateway is not valid");
mErrorText->setText(tr("Gateway is not valid"));
return false;
}
return true;
IpAddr ip;
ip.ip = mIpAddress->text();
ip.dhcp = mDhcpButton->getChecked();
ip.mask = mSubnetMask->text();
ip.gateway = mGateway->text();
JsonObject::Instance()->setDefaultIpAddr(ip);
mThread = QThread::create([ip]()
{
if (ip.dhcp)
{
bool result = NetworkManager::getInstance()->setDHCP();
}
else
{
QString error;
bool result = NetworkManager::getInstance()->setIpAddr(ip, error);
}
});
connect(mThread, &QThread::started, this, &IpSettingsDialog::handleThreadStart);
connect(mThread, &QThread::finished, this, &IpSettingsDialog::handleThreadExit);
mThread->start();
return false;
}
void IpSettingsDialog::init()
@@ -97,3 +140,17 @@ void IpSettingsDialog::init()
mErrorText->hide();
}
void IpSettingsDialog::handleThreadStart()
{
DialogManager::Default()->raiseSyncDialog("Saving Network Configuration...");
}
void IpSettingsDialog::handleThreadExit()
{
DialogManager::Default()->hideTopSyncDialog();
NetworkManager::getInstance()->initNetworkInfo();
disconnect(mThread, &QThread::started, this, &IpSettingsDialog::handleThreadStart);
disconnect(mThread, &QThread::finished, this, &IpSettingsDialog::handleThreadExit);
mThread->deleteLater();
accept();
}

View File

@@ -20,12 +20,18 @@ protected:
private:
void init();
private slots:
void handleDhcpClicked();
void handleThreadStart();
void handleThreadExit();
private:
QLabel* mErrorText;
ImageSwitch* mDhcpButton;
ULineEdit* mIpAddress;
ULineEdit* mSubnetMask;
ULineEdit* mGateway;
QThread* mThread;
};