#include "home.h"
#include "ui_home.h"Home::Home(QWidget *parent): QWidget(parent), ui(new Ui::Home)
{ui->setupUi(this);QSettings settings("kim", "ad");username = settings.value("username").toString();usertype = settings.value("usertype").toString();ui->usertypelabel->setText(usertype);if(usertype=="学生"){ui->listframe->hide();ui->setframe->hide();ui->mainframe->hide();QFrame *stuframe = new QFrame();stuframe->setParent(this);stuframe->setStyleSheet("width:100px;height:100px;background-color: rgb(250, 250, 250);");stuframe->move(100,0);teachEdit = new QLineEdit(this);teachEdit->move(200,50);teachEdit->setStyleSheet("background-color:#ffffff;color:#000000;width:200px;height:30px;border-radius:7px;color:#000000;padding-left:10px;");teachEdit->setPlaceholderText("请输入教师账号");adkeyEdit = new QLineEdit(this);adkeyEdit->move(200,90);adkeyEdit->setStyleSheet("background-color:#ffffff;color:#000000;width:200px;height:30px;border-radius:7px;color:#000000;padding-left:10px;");adkeyEdit->setPlaceholderText("请输入签到验证码");adkeybtn = new QPushButton(this);adkeybtn->move(200,170);adkeybtn->setText("签到");adkeybtn->setStyleSheet("background-color:#000000;color:#ffffff;width:200px;height:40px;border-radius:7px;");connect(adkeybtn, &QPushButton::clicked, this, &Home::onAdKeyBtnClicked);stuHintLable = new QLabel(this);stuHintLable->move(210,140);stuHintLable->setFixedWidth(200);stuHintLable->setStyleSheet("color:rgb(222, 60, 51);height:40px;");}ui->usernamelabel->setText(username);ui->usertypelabel->setAlignment(Qt::AlignCenter);closeAllpage();openAdPage();
}Home::~Home()
{delete ui;
}void Home::onAdKeyBtnClicked() {if(teachEdit->text()==""){stuHintLable->setText("教师账号不能为空");stuHintLable->setStyleSheet("color:rgb(222, 60, 51);height:40px;");}else if(adkeyEdit->text()==""){stuHintLable->setText("签到激活码不能为空");stuHintLable->setStyleSheet("color:rgb(222, 60, 51);height:40px;");}else{socket = new QTcpSocket(this);socket->connectToHost(ip,port);connect(socket,&QTcpSocket::connected,this,&Home::stuad_connected_slot);connect(socket,&QTcpSocket::readyRead,this,&Home::stuad_readyRead_slot);}}void Home::startad_connected_slot()
{struct data user;user.type=STARTAD;user.username=username;user.value=generateRandomString(6);ui->hintlable_3->setText("签到码:"+user.value);ui->startattendancebtn->setStyleSheet("background-color:#ffffff;border-radius:7px;border:1px solid #000000;color:#000000;");socket->write(user.toByteArray());
}void Home::startad_readyRead_slot()
{QByteArray rawData = socket->readAll();const char* rawCharArray = rawData.constData();socketinfo receivedInfo;memcpy(&receivedInfo, rawCharArray, sizeof(socketinfo));outputlog(receivedInfo.data);socket->disconnectFromHost();
}void Home::endad_connected_slot()
{struct data user;user.type=ENDAD;user.username=username;user.value="123";ui->hintlable_3->setText("");ui->startattendancebtn->setStyleSheet("background-color:#000000;border-radius:7px;color:#ffffff;");socket->write(user.toByteArray());
}void Home::endad_readyRead_slot()
{QByteArray rawData = socket->readAll();const char* rawCharArray = rawData.constData();socketinfo receivedInfo;memcpy(&receivedInfo, rawCharArray, sizeof(socketinfo));outputlog(receivedInfo.data);socket->disconnectFromHost();
}void Home::addname_connected_slot()
{struct data user;user.type=NAMEADD;user.username=username;user.value=ui->funcinput->text();socket->write(user.toByteArray());
}void Home::addname_readyRead_slot()
{QByteArray rawData = socket->readAll();const char* rawCharArray = rawData.constData();socketinfo receivedInfo;memcpy(&receivedInfo, rawCharArray, sizeof(socketinfo));outputlog("【"+ui->funcinput->text()+"】"+receivedInfo.data);ui->funcinput->clear();socket->disconnectFromHost();
}void Home::getAllName_connected_slot()
{struct data user;user.type=NAMEGETALL;user.username=username;user.value="123";socket->write(user.toByteArray());
}void Home::getAllName_readyRead_slot()
{QByteArray rawData = socket->readAll();const char* rawCharArray = rawData.constData();socketinfo receivedInfo;memcpy(&receivedInfo, rawCharArray, sizeof(socketinfo));QString receivedData(receivedInfo.data);QStringList records = receivedData.split("|", Qt::SkipEmptyParts);ui->settable->setRowCount(records.size());for (int row = 0; row < records.size(); ++row) {QStringList lines = records[row].split("\n", Qt::SkipEmptyParts);for (int col = 0; col < lines.size(); ++col) {QStringList keyValue = lines[col].split("&", Qt::SkipEmptyParts);QString key = keyValue[0].trimmed();QString value = keyValue[1].trimmed();QTableWidgetItem *item = new QTableWidgetItem(value);ui->settable->setItem(row, col, item);item->setTextAlignment(Qt::AlignCenter);}}ui->settable->setColumnWidth(0, 50);ui->settable->setColumnWidth(1, 100);ui->settable->setColumnWidth(2, 180);socket->disconnectFromHost();
}void Home::deletename_connected_slot()
{QList<QTableWidgetItem *> selectedItems = ui->settable->selectedItems();if (!selectedItems.isEmpty()) {int selectedRow = selectedItems.first()->row();QTableWidgetItem *item1 = ui->settable->item(selectedRow, 0);QString content1 = item1->text();struct data user;user.type=NAMEDELETE;user.username=username;user.value=content1;socket->write(user.toByteArray());} else {qDebug() << "No row selected.";}
}void Home::deletename_readyRead_slot()
{QByteArray rawData = socket->readAll();const char* rawCharArray = rawData.constData();socketinfo receivedInfo;memcpy(&receivedInfo, rawCharArray, sizeof(socketinfo));outputlog(receivedInfo.data);socket->disconnectFromHost();
}void Home::searchname_connected_slot()
{struct data user;user.type=NAMESEARCH;user.username=username;user.value=ui->funcinput->text();socket->write(user.toByteArray());
}void Home::searchname_readyRead_slot()
{QByteArray rawData = socket->readAll();const char* rawCharArray = rawData.constData();socketinfo receivedInfo;memcpy(&receivedInfo, rawCharArray, sizeof(socketinfo));QString receivedData(receivedInfo.data);QStringList records = receivedData.split("|", Qt::SkipEmptyParts);ui->settable->setRowCount(records.size());for (int row = 0; row < records.size(); ++row) {QStringList lines = records[row].split("\n", Qt::SkipEmptyParts);for (int col = 0; col < lines.size(); ++col) {QStringList keyValue = lines[col].split("&", Qt::SkipEmptyParts);QString key = keyValue[0].trimmed();QString value = keyValue[1].trimmed();QTableWidgetItem *item = new QTableWidgetItem(value);ui->settable->setItem(row, col, item);item->setTextAlignment(Qt::AlignCenter);}}ui->settable->setColumnWidth(0, 50);ui->settable->setColumnWidth(1, 100);ui->settable->setColumnWidth(2, 180);socket->disconnectFromHost();
}void Home::getadList_connected_slot()
{struct data user;user.type=NAMEHISTORY;user.username=username;user.value="123";socket->write(user.toByteArray());
}void Home::getadList_readyRead_slot()
{QByteArray rawData = socket->readAll();const char* rawCharArray = rawData.constData();socketinfo receivedInfo;memcpy(&receivedInfo, rawCharArray, sizeof(socketinfo));QString receivedData(receivedInfo.data);QStringList records = receivedData.split("|", Qt::SkipEmptyParts);ui->settable->setRowCount(records.size());for (int row = 0; row < records.size(); ++row) {QStringList lines = records[row].split("\n", Qt::SkipEmptyParts);for (int col = 0; col < lines.size(); ++col) {QStringList keyValue = lines[col].split("&", Qt::SkipEmptyParts);QString key = keyValue[0].trimmed();QString value = keyValue[1].trimmed();QTableWidgetItem *item = new QTableWidgetItem(value);ui->settable->setItem(row, col, item);item->setTextAlignment(Qt::AlignCenter);}}ui->settable->setColumnWidth(0, 50);ui->settable->setColumnWidth(1, 80);ui->settable->setColumnWidth(2, 80);ui->settable->setColumnWidth(3, 160);socket->disconnectFromHost();
}void Home::stuad_connected_slot()
{struct data user;user.type=STUAD;user.username=username;user.value=teachEdit->text();user.value1=adkeyEdit->text();socket->write(user.toByteArray());
}void Home::callname_connected_slot()
{struct data user;user.type=NAMECALL;user.username=username;user.value="123";user.value1="123";socket->write(user.toByteArray());
}void Home::callname_readyRead_slot()
{QByteArray rawData = socket->readAll();const char* rawCharArray = rawData.constData();socketinfo receivedInfo;memcpy(&receivedInfo, rawCharArray, sizeof(socketinfo));outputlog(receivedInfo.data);socket->disconnectFromHost();
}void Home::stuad_readyRead_slot()
{QByteArray rawData = socket->readAll();const char* rawCharArray = rawData.constData();socketinfo receivedInfo;memcpy(&receivedInfo, rawCharArray, sizeof(socketinfo));stuHintLable->setText(receivedInfo.data);stuHintLable->setStyleSheet("color:rgb(26, 136, 35);height:40px;");socket->disconnectFromHost();
}void Home::on_startattendancebtn_clicked()
{if(ui->startattendancebtn->text()=="发起签到"){socket = new QTcpSocket(this);socket->connectToHost(ip,port);connect(socket,&QTcpSocket::connected,this,&Home::startad_connected_slot);connect(socket,&QTcpSocket::readyRead,this,&Home::startad_readyRead_slot);ui->startattendancebtn->setText("关闭签到");}else{socket = new QTcpSocket(this);socket->connectToHost(ip,port);connect(socket,&QTcpSocket::connected,this,&Home::endad_connected_slot);connect(socket,&QTcpSocket::readyRead,this,&Home::endad_readyRead_slot);ui->startattendancebtn->setText("发起签到");ui->startattendancebtn->setStyleSheet("background-color:#000000;border-radius:7px;color:#ffffff;");}}void Home::outputlog(QString msg)
{ui->adEdit->setText("【"+getCurretTime()+"】"+msg+"\n"+ui->adEdit->toPlainText());
}QString Home::getCurretTime()
{QTime systime = QTime::currentTime();QString s = systime.toString("hh:mm:ss");return s;
}QString Home::generateRandomString(int length) {const QString charset = "123456789abcdefghijklmnpqrstuvwxyz";QString randomString;for (int i = 0; i < length; ++i) {int randomIndex = QRandomGenerator::global()->bounded(charset.length());randomString.append(charset.at(randomIndex));}return randomString;
}void Home::closeAllpage()
{ui->hintframe->hide();ui->hintframe_2->hide();ui->startattendancebtn->hide();ui->setmainframe->hide();ui->callnamebtn->hide();
}void Home::openAdPage()
{ui->hintframe->show();ui->hintframe_2->show();ui->startattendancebtn->show();ui->callnamebtn->show();this->setWindowTitle("签到管理系统v1.0-签到");
}void Home::openListPage()
{this->setWindowTitle("签到管理系统v1.0-记录");ui->setmainframe->show();ui->funcinput->hide();ui->addbtn->hide();ui->searchbtn->hide();ui->deletebtn->hide();ui->backbtn->hide();QStringList headerLabels;headerLabels << "ID" << "姓名" << "签到码" << "签到时间";ui->settable->setHorizontalHeaderLabels(headerLabels);ui->settable->verticalHeader()->setVisible(false);ui->settable->setColumnCount(4);ui->settable->setStyleSheet("QScrollBar:vertical {"" background: #ffffff;"" width: 10px;"" margin: 16px 0px 16px 0px;""}""QScrollBar::handle:vertical {"" background: rgb(78, 79, 79);"" min-height: 10px;""}""QScrollBar::add-line:vertical, QScrollBar::sub-line:vertical {"" border: none;"" background: none;""}""QScrollBar::add-page:vertical, QScrollBar::sub-page:vertical {"" background: none;""}""#settable{background-color:#ffffff;color:#000000;}""QTableWidget::item:selected {"" background-color: #eeeeee;""color:#000000;""}");socket = new QTcpSocket(this);socket->connectToHost(ip,port);connect(socket,&QTcpSocket::connected,this,&Home::getadList_connected_slot);connect(socket,&QTcpSocket::readyRead,this,&Home::getadList_readyRead_slot);for (int row = 0; row < ui->settable->rowCount(); ++row) {for (int col = 0; col < ui->settable->columnCount(); ++col) {QTableWidgetItem *item = new QTableWidgetItem(QString("Row %1, Col %2").arg(row + 1).arg(col + 1));item->setTextAlignment(Qt::AlignCenter);}}
}void Home::openSetPage()
{this->setWindowTitle("签到管理系统v1.0-设置");ui->setmainframe->show();ui->funcinput->show();ui->addbtn->show();ui->searchbtn->show();ui->deletebtn->show();ui->backbtn->show();QStringList headerLabels;headerLabels << "ID" << "姓名" << "添加时间";ui->settable->setHorizontalHeaderLabels(headerLabels);ui->settable->verticalHeader()->setVisible(false);ui->settable->setColumnCount(3);ui->settable->setColumnWidth(2, 500);ui->settable->setStyleSheet("QScrollBar:vertical {"" background: #ffffff;"" width: 10px;"" margin: 16px 0px 16px 0px;""}""QScrollBar::handle:vertical {"" background: rgb(78, 79, 79);"" min-height: 10px;""}""QScrollBar::add-line:vertical, QScrollBar::sub-line:vertical {"" border: none;"" background: none;""}""QScrollBar::add-page:vertical, QScrollBar::sub-page:vertical {"" background: none;""}""#settable{background-color:#ffffff;color:#000000;}""QTableWidget::item:selected {"" background-color: #eeeeee;""color:#000000;""}");socket = new QTcpSocket(this);socket->connectToHost(ip,port);connect(socket,&QTcpSocket::connected,this,&Home::getAllName_connected_slot);connect(socket,&QTcpSocket::readyRead,this,&Home::getAllName_readyRead_slot);for (int row = 0; row < ui->settable->rowCount(); ++row) {for (int col = 0; col < ui->settable->columnCount(); ++col) {QTableWidgetItem *item = new QTableWidgetItem(QString("Row %1, Col %2").arg(row + 1).arg(col + 1));item->setTextAlignment(Qt::AlignCenter);}}}void Home::on_callbtn_2_clicked()
{closeAllpage();openListPage();
}void Home::on_callbtn_clicked()
{closeAllpage();openAdPage();
}void Home::on_callbtn_3_clicked()
{closeAllpage();openSetPage();
}void Home::on_callnamebtn_clicked()
{socket = new QTcpSocket(this);socket->connectToHost(ip,port);connect(socket,&QTcpSocket::connected,this,&Home::callname_connected_slot);connect(socket,&QTcpSocket::readyRead,this,&Home::callname_readyRead_slot);
}void Home::on_addbtn_clicked()
{socket = new QTcpSocket(this);socket->connectToHost(ip,port);connect(socket,&QTcpSocket::connected,this,&Home::addname_connected_slot);connect(socket,&QTcpSocket::readyRead,this,&Home::addname_readyRead_slot);
}void Home::on_deletebtn_clicked()
{socket = new QTcpSocket(this);socket->connectToHost(ip,port);connect(socket,&QTcpSocket::connected,this,&Home::deletename_connected_slot);connect(socket,&QTcpSocket::readyRead,this,&Home::deletename_readyRead_slot);}void Home::on_backbtn_clicked()
{Widget *w = new Widget();w->show();this->close();
}void Home::on_searchbtn_clicked()
{socket = new QTcpSocket(this);socket->connectToHost(ip,port);connect(socket,&QTcpSocket::connected,this,&Home::searchname_connected_slot);connect(socket,&QTcpSocket::readyRead,this,&Home::searchname_readyRead_slot);
}