Newer
Older
SurgeTankControl / mainwindow.cpp
[wangxitong] on 27 Jan 2022 4 KB first commit
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "navmodel.h"
#include "navdelegate.h"
#include <QDir>
#include "alarmlistfrom.h"
#include "devicelistform.h"
#include "realtimeform.h"
#include "loginlistform.h"
#include "operationlistform.h"
#include "overviewform.h"
#include "editpwdform.h"
#include "alarmstatisticsform.h"
#include "OrderForm.h"
#include "navmodel.h"
#include <QMessageBox>
#include "QTextCodec"
#include <QLibrary>
#include <QProcess>

QString MainWindow::ip = "127.0.0.1";
MainWindow::MainWindow(QString id,QString username,QWidget *parent)
    : QMainWindow(parent),
       id(id),
       username(username),
       ui(new Ui::MainWindow)
{
    ui->setupUi(this);
//    setWindowFlags(Qt::FramelessWindowHint);
    this->setWindowFlags(Qt::X11BypassWindowManagerHint  | Qt::FramelessWindowHint);
    init();
    tcpServer = new QTcpServer(this);
    connect(tcpServer, SIGNAL(newConnection()), this, SLOT(NewConnectionSlot()));
    bool ok = tcpServer->listen(QHostAddress::Any, 14000);
}
void MainWindow::NewConnectionSlot()
{
    currentClient = tcpServer->nextPendingConnection();
    tcpClient.append(currentClient);
    connect(currentClient, SIGNAL(readyRead()), this, SLOT(ReadData()));
//    connect(currentClient, SIGNAL(disconnected()), this, SLOT(disconnectedSlot()));
}
void MainWindow::ReadData()
{
    // 由于readyRead信号并未提供SocketDecriptor,所以需要遍历所有客户端
    for(int i=0; i<tcpClient.length(); i++)
    {
        QByteArray buffer = tcpClient[i]->readAll();
        if(buffer.isEmpty())    continue;

        static QString IP_Port, IP_Port_Pre;
        IP_Port = tr("[%1:%2]:").arg(tcpClient[i]->peerAddress().toString().split("::ffff:")[1])\
                                     .arg(tcpClient[i]->peerPort());

//            // 若此次消息的地址与上次不同,则需显示此次消息的客户端地址
//            if(IP_Port != IP_Port_Pre)
//                ui->edtRecv->append(IP_Port);

//            ui->edtRecv->append(buffer);

        //更新ip_port
        IP_Port_Pre = IP_Port;
        QTcpSocket  *tcpsocket = new QTcpSocket();
        tcpsocket->connectToHost("127.0.0.1", 15000);
        tcpsocket->write(buffer);
        tcpsocket->flush();
    }
}
void MainWindow::init()
{
        NavModel* model = new NavModel(this);
        QString str = QCoreApplication::applicationDirPath();
        model->ReadDataFromConfig(QCoreApplication::applicationDirPath() + "/config.xml");
        NavDelegate* delegate = new NavDelegate(this);
        ui->listView->setModel(model);
        ui->listView->setItemDelegate(delegate);
        ui->listView->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
        connect(ui->listView, SIGNAL(clicked(const QModelIndex &)), model, SLOT(Collapse(const QModelIndex&)));
        connect(ui->listView,SIGNAL(clicked(QModelIndex)),this,SLOT(showClick(QModelIndex)));
//        connect(ui->listView, SIGNAL(clicked(const QModelIndex &)), model, SLOT(putwidget(const QModelIndex&)));

        AlarmListFrom *widget=new AlarmListFrom(username);
        ui->pHBoxLayout->addWidget(widget);
}

void MainWindow::showClick(QModelIndex index)
{
    int pInt = QProcess::execute("taskkill /im osk.exe /f");
    QString strTemp;
    strTemp = index.data().toString();
    QWidget *widget;
    QTextCodec::setCodecForLocale(QTextCodec::codecForName("GB2312"));
    if(strTemp == QString::fromLocal8Bit("预警记录")){
        widget = new AlarmListFrom(username);
    }else if(strTemp ==QString::fromLocal8Bit("设备管理")){
        widget = new DeviceListForm(username);
    }else if(strTemp == QString::fromLocal8Bit("数据管理")){
        widget = new RealTimeForm(username);
    }else if(strTemp == QString::fromLocal8Bit("账户设置")){
        widget = new EditPwdForm(id);
    }else if(strTemp == QString::fromLocal8Bit("登录日志")){
        widget = new LoginListForm();
    }else if(strTemp == QString::fromLocal8Bit("操作日志")){
        widget = new OperationListForm();
    }else if(strTemp == QString::fromLocal8Bit("预警统计")){
        widget = new AlarmStatisticsForm(username);
    }else if (strTemp == QString::fromLocal8Bit("运行总览")){
        widget = new OverViewForm(username);
    }else if (strTemp == QString::fromLocal8Bit("下发指令")){
        widget = new OrderForm();
    }else{
        return;
    }
    putwidget(widget);
}
void MainWindow::deleteAllitemsOfLayout(QLayout* layout){
    QLayoutItem *child;
    while ((child = layout->takeAt(0)) != nullptr)
    {
       //setParent为NULL,防止删除之后界面不消失
        if(child->widget())
        {
            child->widget()->setParent(nullptr);
        }else if(child->layout()){
            deleteAllitemsOfLayout(child->layout());
        }
        delete child;
    }
}
void MainWindow::putwidget( QWidget *widget )
{
    deleteAllitemsOfLayout( ui->pHBoxLayout);
     ui->pHBoxLayout->addWidget(widget);
}
MainWindow::~MainWindow()
{
    delete ui;
}


void MainWindow::on_ip_textChanged(const QString &arg1)
{
    MainWindow::ip = ui->ip->text();
}