#include "widget.h"
#include "tcpserver.h"
#include "logmanagewidget.h"
#include "globalconstant.h"
#include <QLabel>
#include <QGridLayout>
#include <QComboBox>
#include <QSettings>
#include <QNetworkInterface>
#include <QDebug>
#include <QLineEdit>
#include <QCloseEvent>
#include <QPushButton>
#include <QMessageBox>
#include <QListView>
#define ANDROID_DB_SERV_PORT "AndroidDBServer/ServPort"
#define DEFAULT_PORT "4002"
Widget::Widget(QWidget *parent)
: QWidget(parent)
{
//1、初始化界面元素
initWidget();
//2、初始化布局
initLayout();
//3、新增TCP服务器
m_tcpServer = new TcpServer(this);
if (m_tcpServer->listen(QHostAddress::Any, m_tcpServerPort->text().toInt()))
{
m_tcpServerStatus->setText(tr("当前正在监听端口号:%1").arg(m_tcpServerPort->text()));
}
else
{
m_tcpServerStatus->setText(tr("监听端口号:%1失败").arg(m_tcpServerPort->text()));
}
connect(m_tcpServer, &TcpServer::clientConnect, this, &Widget::onClientConnect);
connect(m_tcpServer, &TcpServer::clientDisconnect, this, &Widget::onClientDisconnect);
connect(m_tcpServerPort, &QLineEdit::editingFinished, this, &Widget::onTcpPortEditingFinished);
connect(m_moreButton, &QPushButton::toggled, this, &Widget::onMoreButtonToggled);
}
Widget::~Widget()
{
}
// 界面修改TCP服务端PORT
void Widget::onTcpPortEditingFinished()
{
QSettings settings("setconfig.ini", QSettings::IniFormat);
settings.setValue(ANDROID_DB_SERV_PORT, m_tcpServerPort->text());
m_tcpServer->close();// 停止监听
if (m_tcpServer->listen(QHostAddress::Any, m_tcpServerPort->text().toInt()))
{
m_tcpServerStatus->setText(tr("当前正在监听端口号:%1").arg(m_tcpServerPort->text()));
}
else
{
m_tcpServerStatus->setText(tr("监听端口号:%1失败").arg(m_tcpServerPort->text()));
}
}
// TCP客户端新增连接
void Widget::onClientConnect(qintptr socketDescriptor, QString clientIP, int clientPort)
{
qDebug() << "Widget::onClientConnect socketDescriptor=" << socketDescriptor << "clientIP=" << clientIP << "clientPort=" << clientPort;
QString str = QString(tr("%1:%2:%3").arg(socketDescriptor).arg(clientIP).arg(clientPort));
m_connectedClient->addItem(str);
qDebug() << "Widget::onClientConnect end";
}
// TCP客户端下线
void Widget::onClientDisconnect(qintptr socketDescriptor, QString clientIP, int clientPort)
{
qDebug() << "Widget::onClientDisconnect socketDescriptor=" << socketDescriptor << "clientIP=" << clientIP << "clientPort=" << clientPort;
m_connectedClient->removeItem(m_connectedClient->findText(tr("%1:%2:%3").arg(socketDescriptor).arg(clientIP).arg(clientPort)));
qDebug() << "Widget::onClientDisconnect end";
}
// 点击更多按钮进行切换
void Widget::onMoreButtonToggled(bool flag)
{
m_logManage->setVisible(flag);
m_logLabel->setVisible(flag);
setFixedHeight(200);
if (flag)
{
setFixedHeight(500);
}
}
// 接收到外围指令--暂时没有用到
void Widget::onReceiveOrder(qintptr sock, QString type)
{
qDebug() << "Widget::onReceiveOrder sock=" << sock << "type=" << type;
}
// 关闭主界面时发射信号触发子线程关闭
void Widget::closeEvent(QCloseEvent *event)
{
int choose = QMessageBox::question(this, tr("退出程序"),
QString(tr("您确认退出程序?")),
QMessageBox::Yes | QMessageBox::No);
if (choose == QMessageBox::No)
{
event->ignore(); //忽略,程序继续运行
}
else if (choose == QMessageBox::Yes)
{
emit closeWidget();
event->accept(); //程序退出
}
}
// 初始化界面元素
void Widget::initWidget()
{
setWindowTitle(tr("Android数据库服务器"));
m_serverLabel = new QLabel(tr("服务器信息:"));
m_tcpServerIpLabel = new QLabel(tr("服务端IP(本机IP):"));
m_tcpServerPortLabel = new QLabel(tr("服务端Port:"));
m_tcpServerIP = new QLineEdit;// 此处只是做展示,方便提供给客户端连接
m_tcpServerPort = new QLineEdit;// 监听此端口号
m_connectLabel = new QLabel(tr("已连接的客户端:"));
m_tcpServerStatus = new QLabel;
m_connectedClient = new QComboBox;
m_moreButton = new QPushButton(tr("更多"));
m_logLabel = new QLabel(tr("日志管理:"));
m_logManage = new LogManageWidget;
m_tcpServerIP->setEnabled(false);
m_moreButton->setCheckable(true);
m_logLabel->setVisible(false);
m_logManage->setVisible(false);
m_connectedClient->setToolTip(tr("套接字:客户端IP:客户端Port"));
QFont font = m_serverLabel->font();
font.setBold(true);
m_serverLabel->setFont(font);
m_logLabel->setFont(font);
QString hostAddress = getLocalHostIP();// 获取本机服务器IP
m_tcpServerIP->setText(hostAddress);
QSettings settings("setconfig.ini", QSettings::IniFormat);
m_tcpServerPort->setText(settings.value(ANDROID_DB_SERV_PORT, DEFAULT_PORT).toString());
QListView *listView = new QListView;
m_connectedClient->setView(listView);
m_connectedClient->setModel(listView->model());
m_tcpServerIP->setProperty("dialoglineedit", "true");
m_tcpServerPort->setProperty("dialoglineedit", "true");
m_moreButton->setProperty("dialogbutton", "true");
}
// 初始界面布局
void Widget::initLayout()
{
setMinimumWidth(450);
setMinimumHeight(200);
m_tcpServerIP->setFixedSize(200, 30);
m_tcpServerPort->setFixedSize(200, 30);
m_connectedClient->setFixedSize(200, 30);
m_moreButton->setFixedSize(50, 30);
m_tcpServerStatus->setFixedHeight(30);
QGridLayout *tcpLayout = new QGridLayout;
tcpLayout->addWidget(m_tcpServerIpLabel, 0, 0, 1, 1);
tcpLayout->addWidget(m_tcpServerIP, 0, 1, 1, 1);
tcpLayout->addWidget(m_tcpServerPortLabel, 1, 0, 1, 1);
tcpLayout->addWidget(m_tcpServerPort, 1, 1, 1, 1);
tcpLayout->addWidget(m_connectLabel, 2, 0, 1, 1);
tcpLayout->addWidget(m_connectedClient, 2, 1, 1, 1);
tcpLayout->addWidget(m_tcpServerStatus, 3, 0, 1, 2);
tcpLayout->setColumnStretch(0, 0);
tcpLayout->setColumnStretch(1, 1);
QHBoxLayout *tcpHlayout = new QHBoxLayout;
tcpHlayout->addStretch();
tcpHlayout->addLayout(tcpLayout);
tcpHlayout->addStretch();
QHBoxLayout *hLayout = new QHBoxLayout;
hLayout->addStretch();
hLayout->addWidget(m_moreButton);
QVBoxLayout *layout = new QVBoxLayout;
layout->addWidget(m_serverLabel);
layout->addLayout(tcpHlayout);
layout->addLayout(hLayout);
layout->addWidget(m_logLabel);
layout->addWidget(m_logManage);
setLayout(layout);
}
// 获取本机IP
QString Widget::getLocalHostIP()
{
QString ipEnterNet;
// 1. 获取所有网络接口
QList<QNetworkInterface> interfaces = QNetworkInterface::allInterfaces();
QList<QNetworkAddressEntry> entry;
foreach(QNetworkInterface inter, interfaces)
{
// 过滤掉不需要的网卡信息
if (is_virtual_network_card_or_loopback(inter.humanReadableName()))
{
continue;
}
if (inter.flags() & (QNetworkInterface::IsUp | QNetworkInterface::IsRunning))
{
entry = inter.addressEntries();
// entry.at(0) 是IPv6信息
if (entry.count() > 1 && entry.at(1).ip().protocol() == QAbstractSocket::IPv4Protocol)
{
if (-1 != inter.name().indexOf("wireless"))
{
ipEnterNet = entry.at(1).ip().toString();
qDebug() << inter.humanReadableName() << inter.name() << " 无线网IP: " << entry.at(1).ip().toString();
break;