/***********************************ui_avchat.h***********************/
#ifndef UI_AVCHAT_H
#define UI_AVCHAT_H
#include <QtGui/QWidget>
#include <QLabel>
#include <QPushButton>
#include <QGroupBox>
#include <QLayout>
#include <QtNetwork>
#include <QTextEdit>
#include "vms_videocaptureview.h"
class MyBtn : public QPushButton
{
Q_OBJECT
private:
QString m_text;
public:
MyBtn(QString text = "",QWidget* parent = 0):QPushButton(text,parent)
{
this->setFont(QFont("Helvetica [Cronyx]",16,55));
m_text = text;
connect(this,SIGNAL(clicked()),this,SLOT(clickSlot()));
}
signals:
void pressing(QString text);
private slots:
void clickSlot()
{
m_text = this->text();
emit pressing(m_text);
}
};
class UI_AVchat : public QWidget
{
Q_OBJECT
private:
QLabel* m_lbPeer;
QTextEdit* m_teHint;
VideoCaptureView* m_view;
QPushButton* m_btnControl;
QPushButton* m_btnUp;
QPushButton* m_btnDown;
QList<MyBtn*> m_btnList;
QList<QString> m_strList;
QGroupBox* m_box;
QUdpSocket* m_socket;
QString m_strHost;
// QTimer* m_timer;
QTimer* m_teTimer;
QImage m_p_w_picpath;
int m_iCurrent;
bool m_bIsChatting;
const static quint16 PORT = 6163;
const static int INTERVAL = 30;//send an p_w_picpath data per 40ms;
const static int WAITING = 3000;//m_teHint waits to be cleared;
public:
UI_AVchat(QWidget *parent = 0);
~UI_AVchat();
private slots:
void upSlot();
void downSlot();
void connectSlot(QString hostAddress);
void controlSlot();
void getImage(QImage p_w_picpath);
// void timeoutSlot();
void readData();
void teControlSlot();//textEdit
};
#endif // UI_AVCHAT_H
/*****************************************ui_avchat.cpp****************************************/
#include "ui_avchat.h"
#include <QtSql>
UI_AVchat::UI_AVchat(QWidget *parent)
: QWidget(parent)
{
this->setFixedSize(800,700);
this->setFocusPolicy(Qt::NoFocus);
m_iCurrent = -1;
m_view = NULL;
m_bIsChatting = false;
QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
db.setDatabaseName("spiderman3");
db.setHostName("192.168.1.16");
db.setUserName("celanc");
db.setPassword("celanc");
if(!db.open())
{
QMessageBox::critical(0, tr("DB operation error"), db.lastError().text());
return;
}
QSqlQuery query;
query.exec(QString("select clientip from client_ip where clientip!='%1'")
.arg(QNetworkInterface::allAddresses().value(0).toString()));
while(query.next())
{
m_strList.push_back(query.value(0).toString());
}
m_lbPeer = new QLabel(this);
m_lbPeer->setFixedSize(640,480);
m_lbPeer->move(10,0);
m_lbPeer->setPixmap(QPixmap("./p_w_picpaths/celanc1.png").scaled(m_lbPeer->size()));
m_lbPeer->show();
m_teHint = new QTextEdit(this);
m_teHint->setStyleSheet("border:0px;background-color:rgba(255,255,255,0);");
m_teHint->setEnabled(false);
// m_teHint->setReadOnly(true);
// m_teHint->setUndoRedoEnabled(false);
m_teHint->setFont(QFont("Helvetica [Cronyx]",16,55));
m_teHint->setFixedSize(310,150);
m_teHint->move(340,575);
m_teHint->setVisible(true);
m_teHint->show();
m_view = new VideoCaptureView(this);
m_view->setFixedSize(320,240);
m_view->move(10,480);
m_view->show();
connect(m_view,SIGNAL(p_w_picpathChanged(QImage)),this,SLOT(getImage(QImage)));
m_btnControl = new QPushButton(this);
m_btnControl->setFont(QFont("Helvetica [Cronyx]",16,55));
m_btnControl->setText(tr("hangup"));
m_btnControl->setFixedSize(310,80); //310,205
m_btnControl->setStyleSheet("border:1px solid gray;border-radius:5px;background-color:rgba(255,255,255,100);");
m_btnControl->move(340,490);
m_btnControl->setVisible(false);
connect(m_btnControl,SIGNAL(clicked()),this,SLOT(controlSlot()));
m_box = new QGroupBox(this);
m_box->setFocusPolicy(Qt::NoFocus);
m_box->setFixedSize(150,700);
m_box->move(650,0);
QVBoxLayout* layout = new QVBoxLayout(m_box);
m_btnUp = new QPushButton;
m_btnUp->setFixedSize(130,114);
m_btnUp->setIcon(QIcon(QPixmap("./p_w_picpaths/up.png")));
m_btnUp->setIconSize(QPixmap("./p_w_picpaths/up.png").size());
m_btnUp->setStyleSheet("QPushButton{border:0px;}"
"QPushButton:pressed{background:qradialgradient(spread:reflectspread, cx:0.5, cy:1.0, "
"radius:1.0, fx:0.5, fy:0.0, stop:0 darkblue, stop:0.3 white, stop:0.4 blue, "
"stop:0.5 white, stop:0.6 darkcyan,stop:0.7 white,stop:0.8 cyan,stop:0.9 white,stop:1.0 lightblue );}");
connect(m_btnUp,SIGNAL(clicked()),this,SLOT(upSlot()));
layout->addWidget(m_btnUp);
if(m_strList.size()>=1)
{
m_iCurrent = 0;
MyBtn* m_btn1 = new MyBtn;
m_btn1->setText(m_strList.value(0));
m_btn1->setFixedSize(130,100);
m_btn1->setStyleSheet("QPushButton{border:1px solid gray;border-radius:5px;border-style:inset;}"
"QPushButton:pressed{border-radius:10px;border-style:outset;}");
connect(m_btn1,SIGNAL(pressing(QString)),this,SLOT(connectSlot(QString)));
m_btnList.push_back(m_btn1);
layout->addWidget(m_btn1);
if(m_strList.size()>=2)
{
MyBtn* m_btn2 = new MyBtn;
m_btn2->setText(m_strList.value(1));
m_btn2->setFixedSize(130,100);
m_btn2->setStyleSheet("QPushButton{border:1px solid gray;border-radius:5px;border-style:inset;}"
"QPushButton:pressed{border-radius:10px;border-style:outset;}");
connect(m_btn2,SIGNAL(pressing(QString)),this,SLOT(connectSlot(QString)));
m_btnList.push_back(m_btn2);
layout->addWidget(m_btn2);
if(m_strList.size()>=3)
{
MyBtn* m_btn3 = new MyBtn;
m_btn3->setText(m_strList.value(2));
m_btn3->setFixedSize(130,100);
m_btn3->setStyleSheet("QPushButton{border:1px solid gray;border-radius:5px;border-style:inset;}"
"QPushButton:pressed{border-radius:10px;border-style:outset;}");
connect(m_btn3,SIGNAL(pressing(QString)),this,SLOT(connectSlot(QString)));
m_btnList.push_back(m_btn3);
layout->addWidget(m_btn3);
}
}
}
m_btnDown = new QPushButton;
m_btnDown->setFixedSize(130,114);
m_btnDown->setIcon(QIcon(QPixmap("./p_w_picpaths/down.png")));
m_btnDown->setIconSize(QPixmap("./p_w_picpaths/down.png").size());
m_btnDown->setStyleSheet("QPushButton{border:0px;}"
"QPushButton:pressed{background:qradialgradient(spread:reflectspread, cx:0.5, cy:0.0, "
"radius:1.0, fx:0.5, fy:1.0, stop:0 darkblue, stop:0.3 white, stop:0.4 blue, "
"stop:0.5 white, stop:0.6 darkcyan,stop:0.7 white,stop:0.8 cyan,stop:0.9 white,stop:1.0 lightblue );}");
connect(m_btnDown,SIGNAL(clicked()),this,SLOT(downSlot()));
layout->addWidget(m_btnDown);
m_btnUp->setEnabled(false);
if(m_strList.size()<=3)
{
m_btnDown->setEnabled(false);
}
m_box->setLayout(layout);
m_socket = new QUdpSocket(this);
bool result = m_socket->bind(PORT);
connect(m_socket,SIGNAL(readyRead()),this,SLOT(readData()));
if(!result)
{
QMessageBox::critical(0,"error","Bind Error!");
return;
}
// m_timer = new QTimer(this);
// m_timer->setInterval(INTERVAL);
// connect(m_timer,SIGNAL(timeout()),this,SLOT(timeoutSlot()));
m_teTimer = new QTimer(this);
m_teTimer->setInterval(WAITING);
m_teTimer->stop();
connect(m_teTimer,SIGNAL(timeout()),this,SLOT(teControlSlot()));
}
UI_AVchat::~UI_AVchat()
{
}
void UI_AVchat::upSlot()
{
m_btnDown->setEnabled(true);
m_iCurrent--;
if(m_iCurrent <= 0)
{
m_btnUp->setEnabled(false);
}
for(int i=0;i<m_btnList.size();i++)
{
m_btnList.at(i)->setText(m_strList.value(m_iCurrent+i));
}
}
void UI_AVchat::downSlot()
{
m_btnUp->setEnabled(true);
m_iCurrent++;
if(m_iCurrent+3 >= m_strList.size())
{
m_btnDown->setEnabled(false);
}
for(int i=0;i<m_btnList.size();i++)
{
m_btnList.at(i)->setText(m_strList.value(m_iCurrent+i));
}
}
void UI_AVchat::connectSlot(QString hostAddress)
{
m_btnControl->setVisible(true);
m_box->setEnabled(false);
m_teHint->setText(tr("Sending connection request,please wait..."));
// m_socket->connectToHost(QHostAddress(hostAddress),PORT);
m_strHost = hostAddress;
QByteArray ba;
ba.append(QString("connection request:%1").arg(QNetworkInterface::allAddresses().value(0).toString()));
if(m_socket->writeDatagram(ba,QHostAddress(m_strHost),PORT)!= ba.length())
{
connectSlot(hostAddress);
}
else
{
qDebug()<<m_socket->peerAddress();
}
}
void UI_AVchat::controlSlot()
{
if(m_btnControl->text()==tr("hangup"))
{
m_bIsChatting = false;
m_btnControl->setVisible(false);
m_box->setEnabled(true);
m_teHint->clear();
m_socket->writeDatagram("hangup",QHostAddress(m_strHost),PORT);
// m_timer->stop();
m_lbPeer->setPixmap(QPixmap("./p_w_picpaths/celanc1.png").scaled(m_lbPeer->size()));
}
else
{
}
}
void UI_AVchat::getImage(QImage p_w_picpath)
{
m_p_w_picpath = p_w_picpath;
if(m_bIsChatting)
{
QByteArray ba;
QBuffer buffer(&ba);
buffer.open(QIODevice::ReadWrite);
m_p_w_picpath.save(&buffer, "JPG");
if(m_socket->writeDatagram(ba,QHostAddress(m_strHost),PORT)!= ba.length())
{
return;
}
}
}
//void UI_AVchat::timeoutSlot()
//{
// QByteArray ba;
// QBuffer buffer(&ba);
// buffer.open(QIODevice::ReadWrite);
// m_p_w_picpath.save(&buffer, "JPG");
// if(m_socket->writeDatagram(ba,QHostAddress(m_strHost),PORT)!= ba.length())
// {
// return;
// }
//}
void UI_AVchat::readData()
{
while (m_socket->hasPendingDatagrams())
{
QByteArray datagram;
datagram.resize(m_socket->pendingDatagramSize());
m_socket->readDatagram(datagram.data(), datagram.size());
// qDebug()<<datagram;
if(datagram == "accepted")
{
m_btnControl->setVisible(true);
m_teHint->clear();
m_teHint->setText(tr("Connected successfully!"));
m_teTimer->start(WAITING);
m_bIsChatting = true;
// m_timer->start();
}
else if(datagram.contains("connection request"))
{
QString host = datagram.mid(datagram.indexOf("t:")+2);
QMessageBox msg(QMessageBox::NoIcon,tr("Request"),
tr("Connection request from ")+host+tr(",accept it?"),
QMessageBox::Ok|QMessageBox::Cancel);
msg.setWindowFlags(Qt::WindowStaysOnTopHint);
if(msg.exec()== 0x400)
{
m_strHost = host;
m_box->setEnabled(false);
m_btnControl->setVisible(true);
m_teHint->clear();
m_teHint->setText(tr("Connected successfully."));
m_teTimer->start(WAITING);
// m_timer->start();
m_bIsChatting = true;
m_socket->writeDatagram("accepted",QHostAddress(m_strHost),PORT);
}
else
{
m_bIsChatting = false;
qDebug()<<m_socket->writeDatagram("rejected",QHostAddress(m_strHost),PORT);
}
}
else if(datagram == "rejected")
{
m_btnControl->setVisible(false);
m_box->setEnabled(true);
m_teHint->clear();
m_teHint->setText(tr("Connection request was denied!"));
m_teTimer->start(WAITING);
m_bIsChatting = false;
}
else if(datagram == "hangup")
{
m_btnControl->setVisible(false);
m_box->setEnabled(true);
m_teHint->clear();
m_teHint->setPlainText(tr("Connection aborted."));
m_teTimer->start(WAITING);
m_bIsChatting = false;
m_lbPeer->setPixmap(QPixmap("./p_w_picpaths/celanc1.png").scaled(m_lbPeer->size()));
}
else
{
QImage p_w_picpath;
m_lbPeer->clear();
if(!p_w_picpath.loadFromData(datagram) || !m_bIsChatting)
{
m_lbPeer->setPixmap(QPixmap("./p_w_picpaths/celanc1.png").scaled(m_lbPeer->size()));
}
else
{
m_lbPeer->setPixmap(QPixmap::fromImage(p_w_picpath));
}
// this->update();
}
}
}
void UI_AVchat::teControlSlot()
{
// qDebug()<<QTime::currentTime();
m_teTimer->stop();
m_teHint->clear();
}
转载于:https://blog.51cto.com/no001/411985