338 lines
11 KiB
C++
338 lines
11 KiB
C++
#include "NetworkCfgDialog.h"
|
|
|
|
#include <QItemSelectionModel>
|
|
#include <QGraphicsDropShadowEffect>
|
|
#include <QRegularExpression>
|
|
#include <QThread>
|
|
|
|
#include "ui_NetworkCfgDialog.h"
|
|
#include "device/networkmanager.h"
|
|
#include "dialogs/DialogManager.h"
|
|
#include "NetCfgTableModel.h"
|
|
|
|
namespace
|
|
{
|
|
const char* boolToStr(bool b)
|
|
{
|
|
return b ? "sucess" : "failed";
|
|
}
|
|
}
|
|
|
|
NetworkCfgDialog::NetworkCfgDialog(QWidget* parent)
|
|
: QDialog(parent)
|
|
, mUi(new Ui::NetworkCfgDialog)
|
|
, mModelAddress(new NetCfgTableModel(this))
|
|
, mModelRoute(new NetCfgTableModel(this))
|
|
|
|
{
|
|
mUi->setupUi(this);
|
|
this->setObjectName("formDialog");
|
|
this->setWindowFlags(Qt::Dialog | Qt::FramelessWindowHint);
|
|
setFocusPolicy(Qt::ClickFocus);
|
|
|
|
bool isDHCP = NetworkManager::getInstance()->isDHCP();
|
|
mUi->sw_dhcp->setChecked(isDHCP);
|
|
mUi->addr_ip->setEnabled(!isDHCP);
|
|
mUi->addr_mask->setEnabled(!isDHCP);
|
|
//ui->sw_dhcp->setButtonStyle(ImageSwitch::ButtonStyle_1);
|
|
|
|
QStringList headerAddr;
|
|
headerAddr << tr("IP Address") << tr("Netmask");
|
|
mModelAddress->setHeader(headerAddr);
|
|
QStringList routeAddr;
|
|
routeAddr << tr("Destination") << tr("Gateway") << tr("Netmask");
|
|
mModelRoute->setHeader(routeAddr);
|
|
|
|
mUi->mButtonGroup->button(QDialogButtonBox::Apply)->setText(tr("Apply"));
|
|
mUi->mButtonGroup->button(QDialogButtonBox::Cancel)->setText(tr("Cancel"));
|
|
|
|
loadData();
|
|
|
|
mUi->tbl_addr->setModel(mModelAddress);
|
|
mUi->tbl_route->setModel(mModelRoute);
|
|
|
|
//ui->tbl_addr->setAlternatingRowColors(true);
|
|
mUi->tbl_addr->setSelectionMode(QAbstractItemView::SingleSelection);
|
|
mUi->tbl_addr->setSelectionBehavior(QAbstractItemView::SelectRows);
|
|
mUi->tbl_addr->setEditTriggers(QAbstractItemView::NoEditTriggers);
|
|
//ui->tbl_addr->verticalHeader()->setDefaultSectionSize(38);
|
|
mUi->tbl_addr->horizontalHeader()->setStretchLastSection(true);
|
|
mUi->tbl_addr->setColumnWidth(0, 345);
|
|
mUi->tbl_addr->setColumnWidth(1, 345);
|
|
mUi->tbl_addr->horizontalHeader()->setFixedHeight(38);
|
|
|
|
//ui->tbl_route->setAlternatingRowColors(true);
|
|
mUi->tbl_route->setSelectionMode(QAbstractItemView::SingleSelection);
|
|
mUi->tbl_route->setSelectionBehavior(QAbstractItemView::SelectRows);
|
|
mUi->tbl_route->setEditTriggers(QAbstractItemView::NoEditTriggers);
|
|
mUi->tbl_route->verticalHeader()->setDefaultSectionSize(38);
|
|
mUi->tbl_route->horizontalHeader()->setStretchLastSection(true);
|
|
mUi->tbl_route->setColumnWidth(0, 230);
|
|
mUi->tbl_route->setColumnWidth(1, 230);
|
|
mUi->tbl_route->setColumnWidth(2, 230);
|
|
mUi->tbl_route->horizontalHeader()->setFixedHeight(38);
|
|
|
|
//set ip input restrictions
|
|
QRegularExpression regex("^(?:(?:25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]\\d|\\d)\\.){3}(?:25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]\\d|\\d)$");
|
|
QRegularExpressionValidator* validator = new QRegularExpressionValidator(regex, this);
|
|
mUi->addr_ip->setValidator(validator);
|
|
mUi->addr_mask->setValidator(validator);
|
|
mUi->led_gw->setValidator(validator);
|
|
|
|
connect(mUi->btn_addr_add, &QPushButton::clicked, [=]()
|
|
{
|
|
DialogResult result = DialogManager::Default()->requestEditIpAndNetMask();
|
|
if (result.ResultCode == QDialog::Accepted)
|
|
{
|
|
mModelAddress->addRow(result.ResultData.toStringList());
|
|
//ui->tbl_addr->selectRow(0);
|
|
}
|
|
});
|
|
|
|
connect(mUi->btn_addr_edit, &QPushButton::clicked, [=]()
|
|
{
|
|
QItemSelectionModel* select = mUi->tbl_addr->selectionModel();
|
|
if (select->hasSelection())
|
|
{
|
|
QModelIndexList index = select->selectedRows();
|
|
if (!index.empty())
|
|
{
|
|
const QStringList ipdata = mModelAddress->rowData(index.at(0));
|
|
DialogResult result = DialogManager::Default()->requestEditIpAndNetMask(ipdata);
|
|
if (result.ResultCode == QDialog::Accepted)
|
|
{
|
|
mModelAddress->removeRow(index.at(0).row());
|
|
mModelAddress->insertRow(index.at(0).row(), result.ResultData.toStringList());
|
|
}
|
|
}
|
|
}
|
|
});
|
|
|
|
connect(mUi->btn_addr_del, &QPushButton::clicked, [=]()
|
|
{
|
|
QItemSelectionModel* select = mUi->tbl_addr->selectionModel();
|
|
if (select->hasSelection())
|
|
{
|
|
QModelIndexList index = select->selectedRows();
|
|
if (!index.empty())
|
|
{
|
|
mModelAddress->removeRow(index.at(0).row());
|
|
|
|
}
|
|
}
|
|
});
|
|
|
|
|
|
connect(mUi->btn_route_add, &QPushButton::clicked, [=]()
|
|
{
|
|
DialogResult result = DialogManager::Default()->requestEditRouteInfo();
|
|
if (result.ResultCode == QDialog::Accepted)
|
|
{
|
|
mModelRoute->addRow(result.ResultData.toStringList());
|
|
}
|
|
|
|
});
|
|
connect(mUi->btn_route_edit, &QPushButton::clicked, [=]()
|
|
{
|
|
QItemSelectionModel* select = mUi->tbl_route->selectionModel();
|
|
if (select->hasSelection())
|
|
{
|
|
QModelIndexList index = select->selectedRows();
|
|
if (!index.empty())
|
|
{
|
|
const QStringList ipdata = mModelRoute->rowData(index.at(0));
|
|
|
|
DialogResult result = DialogManager::Default()->requestEditRouteInfo(ipdata);;
|
|
if (result.ResultCode == QDialog::Accepted)
|
|
{
|
|
mModelRoute->removeRow(index.at(0).row());
|
|
mModelRoute->insertRow(index.at(0).row(), result.ResultData.toStringList());
|
|
}
|
|
}
|
|
}
|
|
});
|
|
|
|
connect(mUi->btn_route_del, &QPushButton::clicked, [=]()
|
|
{
|
|
QItemSelectionModel* select = mUi->tbl_route->selectionModel();
|
|
if (select->hasSelection())
|
|
{
|
|
QModelIndexList index = select->selectedRows();
|
|
if (!index.empty())
|
|
{
|
|
mModelRoute->removeRow(index.at(0).row());
|
|
}
|
|
}
|
|
});
|
|
connect(mUi->mButtonGroup->button(QDialogButtonBox::Apply), &QPushButton::clicked, [=]()
|
|
{
|
|
applyData();
|
|
});
|
|
connect(mUi->mButtonGroup->button(QDialogButtonBox::Cancel), &QPushButton::clicked, [=]()
|
|
{
|
|
reject();
|
|
});
|
|
connect(mUi->sw_dhcp, &ImageSwitch::clicked, [&]()
|
|
{
|
|
mUi->addr_ip->setEnabled(!mUi->sw_dhcp->getChecked());
|
|
mUi->addr_mask->setEnabled(!mUi->sw_dhcp->getChecked());
|
|
});
|
|
|
|
mUi->tabWidget->setCurrentIndex(0);
|
|
}
|
|
|
|
|
|
|
|
|
|
NetworkCfgDialog::~NetworkCfgDialog()
|
|
{
|
|
delete mUi;
|
|
}
|
|
|
|
|
|
|
|
void NetworkCfgDialog::loadData()
|
|
{
|
|
mUi->led_inface->setText(NetworkManager::interfaceName());
|
|
const IpAddr& defaultIpAddress = NetworkManager::getInstance()->getDefaultIpAddr();
|
|
mUi->addr_ip->setText(defaultIpAddress.ip);
|
|
mUi->addr_mask->setText(defaultIpAddress.mask);
|
|
mUi->led_gw->setText(NetworkManager::getDefaultGateway());
|
|
mModelAddress->loadData(NetworkManager::getIpAddrList());
|
|
mModelRoute->loadData(NetworkManager::getIpRouteList());
|
|
|
|
loadJsonData();
|
|
}
|
|
|
|
bool NetworkCfgDialog::isJsonModified()
|
|
{
|
|
const host& hostInfo = NetworkManager::getLocalHost();
|
|
if (mUi->daq_AE->text() != hostInfo.ae)
|
|
{
|
|
return true;
|
|
}
|
|
if (mUi->daq_Name->text() != hostInfo.name)
|
|
{
|
|
return true;
|
|
}
|
|
if (mUi->daq_Port->text() != hostInfo.port)
|
|
{
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
void NetworkCfgDialog::loadJsonData()
|
|
{
|
|
const host& hostInfo = NetworkManager::getLocalHost();
|
|
mUi->daq_AE->setText(hostInfo.ae);
|
|
mUi->daq_IP->setText(hostInfo.ip);
|
|
mUi->daq_Name->setText(hostInfo.name);
|
|
mUi->daq_Port->setText(hostInfo.port);
|
|
}
|
|
|
|
void NetworkCfgDialog::saveJsonData()
|
|
{
|
|
host hostInfo;
|
|
hostInfo.ip = mUi->daq_IP->text();
|
|
hostInfo.ae = mUi->daq_AE->text();
|
|
hostInfo.name = mUi->daq_Name->text();
|
|
hostInfo.port = mUi->daq_Port->text();
|
|
NetworkManager::setLocalHost(hostInfo);
|
|
}
|
|
|
|
bool NetworkCfgDialog::isNetModified()
|
|
{
|
|
if (mUi->sw_dhcp->getChecked() != NetworkManager::getInstance()->isDHCP())
|
|
{
|
|
return true;
|
|
}
|
|
|
|
const IpAddr& defaultIpAddress = NetworkManager::getInstance()->getDefaultIpAddr();
|
|
if (mUi->addr_ip->text() != defaultIpAddress.ip)
|
|
{
|
|
return true;
|
|
}
|
|
if (mUi->addr_mask->text() != defaultIpAddress.mask)
|
|
{
|
|
return true;
|
|
}
|
|
if (mUi->led_gw->text() != NetworkManager::getDefaultGateway())
|
|
{
|
|
return true;
|
|
}
|
|
|
|
if (mModelAddress->getData() != NetworkManager::getIpAddrList())
|
|
{
|
|
return true;
|
|
}
|
|
if (mModelRoute->getData() != NetworkManager::getIpRouteList())
|
|
{
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
void NetworkCfgDialog::handleThreadStart()
|
|
{
|
|
DialogManager::Default()->raiseSyncDialog("Saving Network Configuration...");
|
|
}
|
|
void NetworkCfgDialog::handleThreadExit()
|
|
{
|
|
DialogManager::Default()->hideTopSyncDialog();
|
|
NetworkManager::getInstance()->initNetworkInfo();
|
|
bool isDHCP = NetworkManager::getInstance()->isDHCP();
|
|
mUi->sw_dhcp->setChecked(isDHCP);
|
|
mUi->addr_ip->setText(NetworkManager::getInstance()->getDefaultIpAddr().ip);
|
|
mUi->addr_mask->setText(NetworkManager::getInstance()->getDefaultIpAddr().mask);
|
|
mUi->addr_ip->setEnabled(!isDHCP);
|
|
mUi->addr_mask->setEnabled(!isDHCP);
|
|
mUi->output->setPlainText(mError);
|
|
if (0 != mThread)
|
|
{
|
|
mThread->deleteLater();
|
|
}
|
|
}
|
|
|
|
void NetworkCfgDialog::applyData()
|
|
{
|
|
if (isJsonModified())
|
|
{
|
|
saveJsonData();
|
|
}
|
|
//if it is the same as the old, just exit
|
|
if (!isNetModified())
|
|
{
|
|
accept();
|
|
return;
|
|
}
|
|
mUi->tabWidget->setCurrentIndex(2);
|
|
|
|
mThread = QThread::create([=]()
|
|
{
|
|
mError.clear();
|
|
if (mUi->sw_dhcp->getChecked())
|
|
{
|
|
mError.append("network setting\t");
|
|
bool result = NetworkManager::getInstance()->setDHCP();
|
|
|
|
mError.append(boolToStr(result));
|
|
}
|
|
else
|
|
{
|
|
IpAddr deIpAddress;
|
|
deIpAddress.ip = mUi->addr_ip->text();
|
|
deIpAddress.mask = mUi->addr_mask->text();
|
|
deIpAddress.gateway = mUi->led_gw->text();
|
|
mError.append("network settings\t");
|
|
bool result = NetworkManager::getInstance()->setIpAddr(deIpAddress, mError);
|
|
mError.append(boolToStr(result));
|
|
}
|
|
});
|
|
|
|
connect(mThread, SIGNAL(started()), this, SLOT(handleThreadStart()));
|
|
connect(mThread, SIGNAL(finished()), this, SLOT(handleThreadExit()));
|
|
|
|
mThread->start();
|
|
}
|