加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
sourcecode 27.06 KB
一键复制 编辑 原始数据 按行查看 历史
13692277450 提交于 2022-09-05 02:14 . client source code
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698
#include "helpdeskclientdialog.h"
#include "ui_helpdeskclientdialog.h"
#include <QDesktopServices>
#include <QHostInfo>
#include <QHostAddress>
#include <QNetworkInterface>
#include <QtNetwork/QHostInfo>
#include <QSqlDatabase>
#include <QSqlQuery>
#include <QMessageBox>
#include <QDebug>
#include <QTime>
#include <QTimer>
#include <QSystemTrayIcon>
#include <QMainWindow>
#include <QSqlField>
#include <QSqlRecord>
#include <QTest>
#include <QMenu>
#include <QAction>
#include <QUtf8StringView>
#include <QSqlTableModel>
#include <QSqlError>
#include <QFile>
#include <qjsondocument.h>
#include <qjsonobject.h>
#include <qbytearray.h>
#include <qstring.h>
#include <QFileInfo>
#include <QTextStreamManipulator>
#include <QGraphicsDropShadowEffect>
#include <QPushButton>
#include <QProcess>
#include <QPainter>
#include <QBrush>
#include <QUdpSocket>
#include <QTableView>
HelpDeskClientDialog::HelpDeskClientDialog(QWidget *parent)
: QDialog(parent)
, ui(new Ui::HelpDeskClientDialog)
{
ui->setupUi(this);
this->setWindowFlags(Qt::FramelessWindowHint); //取消标题栏
setWindowFlags(Qt::FramelessWindowHint | Qt::Tool | Qt::WindowStaysOnTopHint);
setWindowOpacity(0.7); //设置窗体透明度
loadSetting ();
connectDB ();
setWindowFlags(Qt::WindowMinimizeButtonHint|Qt::WindowCloseButtonHint); //取消login 窗口最大化;
this->setWindowIcon(QIcon(":/Icon/bird2.png"));
get_UserName ();
get_MachineName ();
getIpAdress();
initialData_Cases_Type();
initialData_Cases_Level();
initialData_Department();
on_tableView_activated();
m_udpSocket = new QUdpSocket(this);
connect(m_udpSocket,&QUdpSocket::readyRead,this,&HelpDeskClientDialog::on_readyRead);
BindPort ();
tableView_HelpDesk_Contact();
on_comboBox_Chat_UpdateIP();
}
HelpDeskClientDialog::~HelpDeskClientDialog()
{
delete ui;
}
void HelpDeskClientDialog::paintEvent(QPaintEvent *)
{
// QPainter painter(this);
// QBrush brush;
// brush.setTextureImage(QImage(":/Icon/0background4.png")); //背景图片
// painter.setBrush(brush);
// painter.setPen(Qt::black); //边框色
// painter.drawRoundedRect(this->rect(), 5, 5); //圆角5像素
}
void HelpDeskClientDialog::get_UserName()
{
WindowsUserName = qgetenv("USERNAME");
if (WindowsUserName.isEmpty())
{
WindowsUserName = qgetenv("NoName");
}
ui->lineEdit_UserName->setText (WindowsUserName);
QTest::qSleep (100);
}
void HelpDeskClientDialog::get_MachineName()
{
localHostName1 = QHostInfo::localHostName();
ui->lineEdit_HostName->setText (localHostName1);
QTest::qSleep (100);
}
QString HelpDeskClientDialog::getIpAdress()
{
QString localIPAddress = "";
QList<QHostAddress>listAddress = QNetworkInterface::allAddresses();
for(int j = 0; j < listAddress.size(); j++)
{
if(!listAddress.at(j).isNull() &&
listAddress.at(j).protocol() == QAbstractSocket::IPv4Protocol
&& listAddress.at(j) != QHostAddress::LocalHost)
{
localIPAddress = listAddress.at(j).toString();
ui->lineEdit_IPAddress->setText (localIPAddress);
return localIPAddress;
}
}
return localIPAddress;
//本地主机名;;
// QString hostName = QHostInfo::localHostName();
// //本机IP地址
// QHostInfo hostInfo = QHostInfo::fromName(hostName);
// //IP地址列表
// QList<QHostAddress> addrList = hostInfo.addresses();
// for(int i=0; i<addrList.count(); i++)
// {
// QHostAddress host = addrList.at(i);
// if(QAbstractSocket::IPv4Protocol == host.protocol())
// {
// QString ip = host.toString();
// ui->lineEdit_IPAddress->setText (ip);
// }
// }
}
void HelpDeskClientDialog::ChangeDatabaseConnection_Success_Icon()
{
ui->pushButton_DB_Connect_Status->setIcon (QIcon(":/Icon/connect2.png"));
ui->pushButton_SendMessage->setDisabled (false);
ui->pushButton_Send_Service_Request->setDisabled (false);
ui->tableView->setDisabled (false);
ui->plainTextEdit_Issue_Description->setDisabled (false);
ui->plainTextEdit_Send_Messages->setDisabled (false);
ui->pushButton_DB_Connect_Status->setToolTip ("Sucess to connected database server!");
}
void HelpDeskClientDialog::ChangeDatabaseConnection_Failure_Icon()
{
ui->pushButton_DB_Connect_Status->setIcon (QIcon(":/Icon/disconnect.png"));
ui->pushButton_SendMessage->setDisabled (true);
ui->pushButton_SendMessage->setDisabled (true);
ui->pushButton_Send_Service_Request->setDisabled (true);
ui->tableView->setDisabled (true);
ui->plainTextEdit_Issue_Description->setDisabled (true);
ui->plainTextEdit_Send_Messages->setDisabled (true);
ui->pushButton_DB_Connect_Status->setToolTip ("Sorry, failure to connect database server! pls check database server or network");
}
void HelpDeskClientDialog::initialData_Cases_Type()
{
ui->comboBox_IssueType->clear ();
QSqlQuery queryCaseType;
QString queryCaseTypeStr = "select * from issueType";
queryCaseType.exec (queryCaseTypeStr);
if(queryCaseType.exec (queryCaseTypeStr))
{
while(queryCaseType.next())
{
//把 ticket 表中的 issueStatusName 载入到下拉框
QString issueType_Name=QString("%1").arg(queryCaseType.value("issueType").toString());
ui->comboBox_IssueType->addItem(issueType_Name);
}
}
else
{
}
}
void HelpDeskClientDialog::initialData_Cases_Level()
{
ui->comboBox_IssueLevel->clear ();
QSqlQuery queryCaseLevel;
QString queryCaseLevelStr = "select * from issueLevel";
queryCaseLevel.exec (queryCaseLevelStr);
if(queryCaseLevel.exec (queryCaseLevelStr))
{
while(queryCaseLevel.next())
{
//把 ticket 表中的 issueStatusName 载入到下拉框
QString issueLevel_Name=QString("%1").arg(queryCaseLevel.value("issueLevel").toString());
ui->comboBox_IssueLevel->addItem(issueLevel_Name);
}
}
else
{
}
}
void HelpDeskClientDialog::initialData_Department()
{
// ui->lineEdit_DepartMent->setText ("No Department");
QSqlQuery queryDepartment;
QString userName = ui->lineEdit_UserName->text ().trimmed ();
QString queryDepartmentStr = tr("select employeeDepartment from Employee where employeeName = '%1' group by employeeDepartment").arg (userName);
queryDepartment.exec (queryDepartmentStr);
if(queryDepartment.exec (queryDepartmentStr))
{
do
{
QSqlRecord rec = queryDepartment.record ();
QSqlField field = rec.field (0);
QString departmentValue = field.value ().toString ().trimmed ();
ui->lineEdit_DepartMent->setText (departmentValue);
}
while (queryDepartment.next());
}
else
{
ui->lineEdit_DepartMent->setText ("No Department");
}
}
void HelpDeskClientDialog::on_pushButton_Exit_clicked()
{
db.close ();
exit(00);
}
void HelpDeskClientDialog::on_pushButton_Send_Service_Request_clicked()
{
if(ui->comboBox_IssueType->currentIndex ()==-1)
{
QMessageBox::warning (this, "Error", "Pls select case type first!");
return;
}
else if (ui->comboBox_IssueLevel->currentIndex ()==-1)
{
QMessageBox::warning (this, "Error", "Pls choose case level!");
return;
}
else if(ui->plainTextEdit_Issue_Description->document ()->isEmpty ())
{
QMessageBox::warning (this, "Error", "Pls input case description!");
return;
}
QString userName = ui->lineEdit_UserName->text ();
QString caseDescription = ui->plainTextEdit_Issue_Description->toPlainText ();
QString employeeDepartment = ui->lineEdit_DepartMent->text ();
QDateTime dateTime= QDateTime::currentDateTime();//获取系统当前的时间
QString DateTimestr = dateTime.toString("yyyy-MM-dd hh:mm:ss");
QSqlQuery query;
query.prepare("INSERT INTO Ticket (employeeName,employeeDepartment,issueType,issueLevel,issueDescription,issueStatus,issueCreateTime,itEngineer) "
"VALUES (:employeeName, :employeeDepartment, :issueType, :issueLevel, :issueDescription, :issueStatus, :issueCreateTime, :itEngineer)");
query.bindValue(":employeeName", userName);
query.bindValue(":employeeDepartment", employeeDepartment);
query.bindValue(":issueType", issueTypeStr);
query.bindValue(":issueLevel", issueLevelStr);
query.bindValue (":issueDescription", caseDescription);
query.bindValue (":issueStatus", "Open");
query.bindValue (":issueCreateTime", DateTimestr);
query.bindValue (":itEngineer", "UnOwned");
if(OpenDBResult)
{
if(query.exec ())
{
QMessageBox::information (this, "Success", "Your case request has been sent to Helpdesk, they will hand it ASAP!");
ui->comboBox_IssueType->setCurrentIndex (-1);
ui->comboBox_IssueLevel->setCurrentIndex (-1);
ui->plainTextEdit_Issue_Description->clear ();
}
else
{
QMessageBox::warning (this, "Error", "Sorry, your case request sent failure, pls try push connect DB button or check your network or call Helpdesk directly!");
}
}
else
{
connectDB ();
query.exec ();
}
on_tableView_activated();
}
void HelpDeskClientDialog::on_comboBox_IssueLevel_activated()
{
issueLevelStr = ui->comboBox_IssueLevel->currentText ().trimmed ();
}
void HelpDeskClientDialog::on_comboBox_IssueType_activated()
{
issueTypeStr = ui->comboBox_IssueType->currentText ().trimmed ();
}
void HelpDeskClientDialog::connectDB()
{
QSqlDatabase db;
QSqlDatabase::removeDatabase("QMYSQL");
if(QSqlDatabase::contains("HelpDesk"))
{
db = QSqlDatabase::database("QMYSQL");
}
else
{
db = QSqlDatabase::addDatabase("QMYSQL");
}
db.setHostName(dataBaseHostName); //连接本地主机
db.setPort(dataBasePort);
db.setDatabaseName("HelpDesk");
db.setUserName(dataBaseUserName);
db.setPassword(dataBasePassWord);
db.setConnectOptions("MYSQL_OPT_RECONNECT=1");
OpenDBResult = db.open();
if(!OpenDBResult)
{
QMessageBox::warning (this, "Database Error", QString("Could not connect to database server, pls check your connection..."));
ChangeDatabaseConnection_Failure_Icon();
}
else
{
// qDebug() << "Connected to database server successful";
ChangeDatabaseConnection_Success_Icon();
}
}
void HelpDeskClientDialog::changeEvent(QEvent *event)
{
if((event->type()==QEvent::WindowStateChange)&&isMinimized())
{
hide();
event->ignore();
//初始化两个项目
QSystemTrayIcon* trayIcon = new QSystemTrayIcon(this);
QAction* showAction = new QAction(QStringLiteral("Show Super HelpDesk"));//项1
showAction->setIcon (QIcon(":/Icon/bird2.png"));
QAction* exitAction = new QAction(QStringLiteral("EXIT"));//项2
exitAction->setIcon (QIcon(":/Icon/bird.png"));
//项1的点击槽函数
connect(showAction, &QAction::triggered, this, [=]()
{
this->showNormal ();
});
//项2的点击槽函数
connect(exitAction, &QAction::triggered, this, [=]()
{
QApplication::exit(0);
});
connect(trayIcon,SIGNAL(activated(QSystemTrayIcon::ActivationReason)),this,SLOT(on_activatedSysTrayIcon(QSystemTrayIcon::ActivationReason)));
//初始化菜单并添加项
QMenu* trayMenu = new QMenu(this);//菜单
trayMenu->addAction(showAction);
trayMenu->addAction(exitAction );
//创建一个系统托盘
if(isIconExist == true)
{
return;
}
else
{
trayIcon->setIcon(QIcon(":/Icon/bird2.png")); //设置托盘图标
trayIcon->setContextMenu(trayMenu); //设置菜单
trayIcon->setToolTip ("Super HelpDesk");
trayIcon->show();
isIconExist = true;
}
}
}
void HelpDeskClientDialog::on_activatedSysTrayIcon(QSystemTrayIcon::ActivationReason reason)
{
switch(reason)
{
case QSystemTrayIcon::Trigger:
//单击托盘图标
this->showNormal();
break;
case QSystemTrayIcon::DoubleClick:
//双击托盘图标
//双击后显示主程序窗口
this->showNormal();
break;
default:
break;
}
}
void HelpDeskClientDialog::on_pushButton_DB_Connect_Status_clicked()
{
ui->stackedWidget->setCurrentIndex (0);
loadSetting ();
connectDB ();
if(OpenDBResult)
{
get_UserName ();
get_MachineName ();
getIpAdress();
initialData_Cases_Type();
initialData_Cases_Level();
initialData_Department();
on_tableView_activated();
tableView_HelpDesk_Contact ();
ui->plainTextEdit_Issue_Description->setEnabled (true);
ui->tableView->setEnabled (true);
ui->pushButton_DB_Connect_Status->setIcon (QIcon(":/Icon/connect2.png"));
ui->pushButton_SendMessage->setEnabled (true);
ui->pushButton_SendMessage->setEnabled (true);
ui->pushButton_Send_Service_Request->setEnabled (true);
ui->plainTextEdit_Send_Messages->setEnabled (true);
ui->pushButton_DB_Connect_Status->setToolTip ("Sucess to connected database server!");
}
}
void HelpDeskClientDialog::on_tableView_activated()
{
QSqlTableModel *model = new QSqlTableModel(this);
ui->tableView->setMaximumHeight (260);
model->setTable("Ticket");
model->setEditStrategy(QSqlTableModel::OnManualSubmit);
model->setFilter((tr("employeeName = '%1' and issueStatus = 'Open'").arg (WindowsUserName)));
model->select();
ui->tableView->setModel(model);
model->setHeaderData(0,Qt::Horizontal,"ID");
model->setHeaderData(1,Qt::Horizontal,"Employee Name");
model->setHeaderData(2,Qt::Horizontal,"Department");
model->setHeaderData(3,Qt::Horizontal,"Case Type");
model->setHeaderData(4,Qt::Horizontal,"Case Level");
model->setHeaderData(5,Qt::Horizontal,"Description");
model->setHeaderData(6,Qt::Horizontal,"Create Time");
model->setHeaderData(7,Qt::Horizontal,"Status");
model->setHeaderData(8,Qt::Horizontal,"Solution");
model->setHeaderData(9,Qt::Horizontal,"Solved Time");
model->setHeaderData(10,Qt::Horizontal,"Attachment");
model->setHeaderData(11,Qt::Horizontal,"HelpDesk");
model->setHeaderData(12,Qt::Horizontal,"Remark");
ui->tableView->horizontalHeader()->setSectionResizeMode(QHeaderView::ResizeToContents);
}
void HelpDeskClientDialog::on_pushButton_Settings_clicked()
{
ui->stackedWidget->setCurrentWidget (ui->page_Settings);
ui->lineEdit_Settings_IP->setText (dataBaseHostName);
//QString dataBasePortStr = QString::number (dataBasePort);
ui->lineEdit_Settings_Port->setText (dataBasePortStr);
ui->lineEdit_Settings_LoginName->setText (dataBaseUserName);
}
void HelpDeskClientDialog::on_pushButton_Settings_Exit_clicked()
{
ui->stackedWidget->setCurrentIndex (0);
}
void HelpDeskClientDialog::on_pushButton_Settings_Save_clicked()
{
if(ui->lineEdit_Settings_IP->text ()== "" | ui->lineEdit_Settings_LoginName->text ()== "" | ui->lineEdit_Settings_Password->text ()== "" | ui->lineEdit_Settings_Port->text ()== "")
{
QMessageBox::warning (this, "Error", "IP address or login name or password or port could not be empty!");
return;
}
QString portInt = ui->lineEdit_Settings_Port->text ();
QString filepath = QCoreApplication::applicationDirPath();
QFile settingFile2(filepath + "/setting/setting.json");
QFileInfo info(settingFile2);
if(settingFile2.open(QIODevice::WriteOnly|QIODevice::Text))
{
QJsonObject jsonObject;//构建json对象json
QString encyPassword = EncryptionStr (ui->lineEdit_Settings_Password->text ());
jsonObject.insert("dataBaseHostName", ui->lineEdit_Settings_IP->text ());
jsonObject.insert("dataBasePort", portInt);
jsonObject.insert("dataBaseUserName", ui->lineEdit_Settings_LoginName->text ());//ui->lineEdit_LoginName->text ());
jsonObject.insert ("dataBasePassWord", encyPassword);
QJsonDocument document;
document.setObject(jsonObject);
QString jsonStr(document.toJson(QJsonDocument::Indented));
settingFile2.write(jsonStr.toUtf8 ());
settingFile2.flush();
QMessageBox::information (this, "Success", "Your configuration were saved successful, The application will auto exit and auto restart again!");
}
else
{
QMessageBox::warning (this, "Error", "Sorry, your configuration were saved failure, pls double check!" );
}
settingFile2.close();
qApp->quit ();
QProcess::startDetached(qApp->applicationFilePath(), QStringList());
}
void HelpDeskClientDialog::loadSetting()
{
// QFile settingFile;
// settingFile.setFileName(":/setting/setting.json");
QString filepath = QCoreApplication::applicationDirPath();
QFile settingFile(filepath + "/setting/setting.json");
if(settingFile.open(QFile::ReadOnly))
{
// 读取json文件的内容,当前读取的内容可能是错误的,因为json文件不能有注释,而我在json中添加了注释,所以需要把注释内容去掉
QByteArray setting = settingFile.readAll().trimmed();
// 循环删除掉注释内容
while (setting.contains("/*") && setting.contains("*/"))
{
int start = setting.indexOf("/*"); // 注释左索引(这里会返回左斜杠所在索引值)
int end = setting.indexOf("*/") + 1; // 注释右索引(这里会返回*所在索引值,所以我们需要+1,将索引值切换到左斜杠位置)
setting = setting.remove(start, (end - start) + 1); // 移除 /* xxxx */ 这一块的注释内容,(结束索引-开始索引+1)是整个注释内容的长度
}
QJsonParseError jsonError;
QJsonDocument jsonDoc(QJsonDocument::fromJson(setting, &jsonError));
if(jsonError.error == QJsonParseError::NoError)
{
QJsonObject rootObj = jsonDoc.object();
dataBaseHostName = rootObj.value("dataBaseHostName").toString (); // 读取配置文件中数据库连接ip地址
dataBasePort = rootObj.value("dataBasePort").toInt(); // 读取配置文件中数据库连接端口
dataBasePortStr = rootObj.value ("dataBasePort").toString ();
dataBaseUserName = rootObj.value("dataBaseUserName").toString(); // 读取配置文件中数据库连接用户名
dataBasePassWord = EncryptionStr(rootObj.value("dataBasePassWord").toString()); // 读取配置文件中数据库连接密码
// dataBasePassWord = EncryptionStr(dataBasePassWord); // 读取配置文件中数据库连接密码
}
else
{
qDebug() << "Configure json file error!" << jsonError.errorString();
}
settingFile.close ();
}
}
QString HelpDeskClientDialog::EncryptionStr(QString str)
{
std::wstring wString = str.toStdWString();
static QByteArray key = "86868686m$M&NOS";
for (int i = 0; i < wString.size(); i++)
{
wString[i] = wString[i] ^ key[i % key.size()];
}
return QString::fromStdWString(wString).toUtf8();
}
void HelpDeskClientDialog::on_pushButton_Service_clicked()
{
ui->stackedWidget->setCurrentWidget (ui->page_SendMessage);
}
void HelpDeskClientDialog::on_pushButton_Chat_clicked()
{
ui->stackedWidget->setCurrentWidget (ui->page_Chat);
}
void HelpDeskClientDialog::on_pushButton_News_clicked()
{
ui->stackedWidget->setCurrentWidget (ui->page_News);
}
void HelpDeskClientDialog::on_pushButton_Trainning_clicked()
{
ui->stackedWidget->setCurrentWidget (ui->page_Trainning);
}
void HelpDeskClientDialog::on_pushButton_Information_clicked()
{
ui->stackedWidget->setCurrentWidget (ui->page_Information);
}
//Chat module start..................................................................
void HelpDeskClientDialog::BindPort()
{
//ui->lineEdit_LocalPort->setText ("3000");
m_udpSocket->abort();
qint16 port =8686; // = ui->lineEdit_LocalPort->text ().toInt ();
QString portString = "8686";
if(m_udpSocket->bind(port))
{
// ui->plainTextEdit_Chat_Messages->appendPlainText ("**Sucessed to bind port**");
QString portAdd = "**Succeeded bind port: "+QString::number(m_udpSocket->localPort())+"**";
ui->plainTextEdit_Chat_Messages->appendPlainText (portAdd);
// ui->pushButton_BindPort->setEnabled(false);
// ui->pushButton_UnBindPort->setEnabled(true);
}
else
{
ui->plainTextEdit_Chat_Messages->appendPlainText("**Failure to bind port: " + portString + "**");
}
}
void HelpDeskClientDialog::UnBindPort()
{
//解除绑定
m_udpSocket->abort();
//ui->pushButton_BindPort->setEnabled(true);
// ui->pushButton_UnBindPort->setEnabled(false);
ui->plainTextEdit_Chat_Messages->appendPlainText("**UnBind port already**");
}
void HelpDeskClientDialog::on_readyRead()
{
//是否还有待读取的传入数据报
while(m_udpSocket->hasPendingDatagrams())
{
QByteArray data;
//返回待读取的数据报的字节数
data.resize(m_udpSocket->pendingDatagramSize());
QHostAddress peerAddr;
quint16 peerPort;
//读取数据报的内容
m_udpSocket->readDatagram(data.data(),data.size(),&peerAddr,&peerPort);
QString str = data.data();
QStringList list = str.split("///");
QString str1 = list.at(list.length()-1);
QString strReceiveWithoutName = list.at(list.length ()-2);
// QString peer = "[From ] +"+peerAddr.toString()+":"+QString::number(peerPort)+"] ";
QString peer1 = "[From "+str1+"]: ";
// ui->plainTextEdit_Chat_Messages->appendPlainText(peer+str);
QTextCharFormat fmt;
//字体色
fmt.setForeground(QBrush(Qt::blue));
ui->plainTextEdit_Chat_Messages->mergeCurrentCharFormat(fmt);
ui->plainTextEdit_Chat_Messages->appendPlainText(peer1+strReceiveWithoutName);
}
}
void HelpDeskClientDialog::on_pushButton_SendMessage_clicked()
{
if(ui->plainTextEdit_Send_Messages->document ()->isEmpty ())
{
QMessageBox::warning (this, "Remind", "You could not send empty message out!");
return;
}
QString dstIp = helpDeskIP; // = ui->lineEdit_SendTo_IP->text ();
QString chatWithName = ui->label_ChatWithName->text ();
QHostAddress dstAddr(dstIp);
//目标端口
quint16 dstPort = 8686; //= ui->lineEdit_SendTo_Port->text().toInt ();
QString msg = ui->plainTextEdit_Send_Messages->toPlainText () + "///" + WindowsUserName;
QByteArray str = msg.toUtf8();
//发出数据报
m_udpSocket->writeDatagram(str,dstAddr,dstPort);
QTextCharFormat fmt;
//字体色
fmt.setForeground(QBrush(Qt::darkRed));
QString msgLocal = ui->plainTextEdit_Send_Messages->toPlainText ();
ui->plainTextEdit_Chat_Messages->mergeCurrentCharFormat(fmt);
ui->plainTextEdit_Chat_Messages->appendPlainText("[" +WindowsUserName + " To " + chatWithName + "]: "+msgLocal);
ui->plainTextEdit_Send_Messages->clear ();
}
//Chat module end.....................................................................
void HelpDeskClientDialog::on_tableView_HelpDesk_Contact_doubleClicked(const QModelIndex &index)
{
QSqlRecord record = model_HelpDesk_Contact->record(index.row ()); //关键步骤
//update data
ui->label_ChatWithName->setText (record.value ("employeeName").toString ());
helpDeskIP = record.value ("ipAddress").toString ();
}
void HelpDeskClientDialog::tableView_HelpDesk_Contact()
{
// model_HelpDesk_Contact = new QSqlTableModel(this);
model_HelpDesk_Contact = new QSqlTableModel(this);
//ui->tableView_HelpDesk_Contact->setMaximumWidth (200);
model_HelpDesk_Contact->setTable("ChatList");
model_HelpDesk_Contact->setEditStrategy(QSqlTableModel::OnManualSubmit);
// model_HelpDesk_Contact->setFilter("select userName and ipAddress");
model_HelpDesk_Contact->select();
ui->tableView_HelpDesk_Contact->setModel(model_HelpDesk_Contact);
model_HelpDesk_Contact->setHeaderData(1,Qt::Horizontal,"User Name");
model_HelpDesk_Contact->setHeaderData(2,Qt::Horizontal,"IP Address");
ui->tableView_HelpDesk_Contact->hideColumn (0);
ui->tableView_HelpDesk_Contact->hideColumn (5);
ui->tableView_HelpDesk_Contact->hideColumn (3);
ui->tableView_HelpDesk_Contact->hideColumn (4);
ui->tableView_HelpDesk_Contact->hideColumn (6);
ui->tableView_HelpDesk_Contact->verticalHeader ()->setVisible (false);
ui->tableView_HelpDesk_Contact->setModel(model_HelpDesk_Contact);
ui->AllCasestableView->horizontalHeader()->setStyleSheet("QHeaderView::section {"
"color: blue;padding-left: 4px;border: 1px solid #6c6c6c;}");
ui->tableView_AllCases->verticalHeader ()->setDefaultAlignment (Qt::AlignVCenter);
ui->tableView_HelpDesk_Contact->horizontalHeader()->setSectionResizeMode(QHeaderView::ResizeToContents);
}
void HelpDeskClientDialog::closeEvent (QCloseEvent *)
{
QApplication* app;
app->quit ();
//exit;;;;;;;;
}
void HelpDeskClientDialog::on_pushButton_Chat_RefreshList_clicked()
{
tableView_HelpDesk_Contact();
}
void HelpDeskClientDialog::on_comboBox_Chat_UpdateIP()
{
ui->comboBox_Chat_UpdateIP->clear ();
localIPAddressUpdateIP = "";
QList<QHostAddress>listAddress = QNetworkInterface::allAddresses();
for(int j = 0; j < listAddress.size(); j++)
{
if(!listAddress.at(j).isNull() &&
listAddress.at(j).protocol() == QAbstractSocket::IPv4Protocol
&& listAddress.at(j) != QHostAddress::LocalHost)
{
localIPAddressUpdateIP = listAddress.at(j).toString();
ui->comboBox_Chat_UpdateIP->addItem (localIPAddressUpdateIP );
}
}
}
void HelpDeskClientDialog::on_pushButton_Chat_UpdateIP_clicked()
{
QSqlQuery sqlQuery;
QString sqlStr = tr("update ChatList SET ipAddress = '%1', computerName = '%2' where employeeName = %3").arg (ui->comboBox_Chat_UpdateIP->currentText ().trimmed ()).arg (localHostName1).arg (WindowsUserName);
if(sqlQuery.exec (sqlStr))
{
tableView_HelpDesk_Contact ();
ui->plainTextEdit_Chat_Messages->appendPlainText ("Your new IP address has been updated to server and all users will auto update in 10 minutes.");
}
else
{
ui->plainTextEdit_Chat_Messages->appendPlainText ("Sorry, update IP address failure, pls contact with IT.");
}
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化