#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QtNetwork>
#include <QtWidgets/QApplication>
#include <qdebug.h>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent)
{
setupUi(this);
rowClicked = NULL;
statusLabel->setText(tr("Click REFRESH"));
BookButton->setDisabled(true);
LeaveButton->setDisabled(true);
tableWidget->verticalHeader()->hide();
tableWidget->setEditTriggers(QAbstractItemView::NoEditTriggers);
tableWidget->setSelectionBehavior(QTableWidget::SelectRows);//一次选中一行
tableWidget->setSelectionMode(QAbstractItemView::SingleSelection);//只能单选
connect(RefreshButton,SIGNAL(clicked(bool)),
this,SLOT(connectToServer()));
connect(BookButton,SIGNAL(clicked(bool)),
this,SLOT(connectToServer()));
connect(LeaveButton,SIGNAL(clicked(bool)),
this,SLOT(connectToServer()));
connect(StopButton,SIGNAL(clicked(bool)),
this,SLOT(connectionClosedByServer()));
connect(RefreshButton,SIGNAL(clicked(bool)),
this,SLOT(sendRefreshRequest()));
connect(&tcpSocket,SIGNAL(readyRead()),
this,SLOT(updateTableWidget()));
connect(BookButton,SIGNAL(clicked(bool)),
this,SLOT(sendBookRequest()));
connect(LeaveButton,SIGNAL(clicked(bool)),
this,SLOT(sendLeaveRequest()));
connect(&tcpSocket,SIGNAL(error(QAbstractSocket::SocketError)),
this,SLOT(error()));
}
void MainWindow::sendRefreshRequest()
{
tableWidget->setRowCount(0);
tableWidget->clearContents();
QByteArray block;
QDataStream out(&block, QIODevice::WriteOnly);
out.setVersion(QDataStream::Qt_4_3);
out<< quint16(0)<<quint8('R')<< comboBox->currentText();
out.device()->seek(0);
out<<quint16(block.size() - sizeof(quint16));
tcpSocket.write(block);
nextBlockSize = 0;
rowClicked = NULL;
statusLabel->setText(tr("Ready"));
}
void MainWindow::sendBookRequest()
{
checkNULL();
QByteArray block;
QDataStream out(&block, QIODevice::WriteOnly);
out.setVersion(QDataStream::Qt_4_3);
out<< quint16(0)<<quint8('B')<<rowClassroom;
out.device()->seek(0);
out<<quint16(block.size() - sizeof(quint16));
tcpSocket.write(block);
QString str = QString::number( --rowRemains);
tableWidget->setItem(rowClicked,1,new QTableWidgetItem(str));
statusLabel->setText(tr("Don't forget to click LEAVE"));
}
void MainWindow::sendLeaveRequest()
{
checkNULL();
QByteArray block;
QDataStream out(&block, QIODevice::WriteOnly);
out.setVersion(QDataStream::Qt_4_3);
out<< quint16(0)<<quint8('L')<<rowClassroom;
out.device()->seek(0);
out<<quint16(block.size() - sizeof(quint16));
tcpSocket.write(block);
QString str = QString::number(++rowRemains );
tableWidget->setItem(rowClicked,1,new QTableWidgetItem(str));
statusLabel->setText(tr("Have a good day"));
}
void MainWindow::connectToServer()
{
tcpSocket.connectToHost(QHostAddress::LocalHost, 12345);
nextBlockSize = 0;
statusLabel->setText(tr("Connecting"));
}
void MainWindow::updateTableWidget()
{
QDataStream in(&tcpSocket);
in.setVersion(QDataStream::Qt_4_3);
forever{
int row = tableWidget->rowCount();
if(nextBlockSize == 0){
if(tcpSocket.bytesAvailable() < sizeof(quint16)){
qDebug()<<"bBreak";
qDebug()<<tcpSocket.bytesAvailable()<<","<<sizeof(quint16);
break;
}
in >> nextBlockSize;
}
if (nextBlockSize == 0xFFFF) {
qDebug()<<"0Break";
break;
}
if(tcpSocket.bytesAvailable() < nextBlockSize){
qDebug()<<"nBreak";
break;
}
QString classroom;
QString remains;
in >> classroom >>remains;
tableWidget->setRowCount(row + 1);
QStringList list;
list << classroom << remains;
for(int i = 0;i<list.count();++i)
tableWidget->setItem(row,i,new QTableWidgetItem(list[i]));
nextBlockSize = 0;
}
BookButton->setEnabled(true);
LeaveButton->setEnabled(true);
}
void MainWindow::connectionClosedByServer()
{
closeConnection();
}
void MainWindow::error()
{
statusLabel_2->setText(tcpSocket.errorString());
closeConnection();
}
void MainWindow::checkNULL()
{
if (rowClicked == NULL)
{
rowClicked = 0;
rowClassroom = tableWidget->item(0,0)->text().toInt();
rowRemains = tableWidget->item(0,1)->text().toInt();
}
}
void MainWindow::closeConnection()
{
tcpSocket.close();
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_tableWidget_cellClicked(int row, int column)
{
rowClassroom = tableWidget->item(row,0)->text().toInt();
rowRemains = tableWidget->item(row,1)->text().toInt();
rowClicked = row;
}