-
Notifications
You must be signed in to change notification settings - Fork 1
/
chatpage.cpp
187 lines (147 loc) · 4.87 KB
/
chatpage.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
#include "chatpage.h"
#include "ui_chatpage.h"
#include "QPixmap"
#include "QString"
#include "QDateTime"
#include "addfriendpage.h"
#include "QTreeWidgetItem"
#include "protocol.h"
ChatPage::ChatPage(QWidget *parent) :
QDialog(parent),
ui(new Ui::ChatPage)
{
ui->setupUi(this);
QString *headPath = new \
QString("/var/ftp/pub/apue/qq/imgs/women.png");
QPixmap *headMap = new QPixmap(*headPath);
*headMap = headMap->scaled(ui->headLabel->size());
ui->headLabel->setPixmap(*headMap);
QString *setupPath = new \
QString("/var/ftp/pub/apue/qq/imgs/setup.png");
QPixmap *setupMap = new QPixmap(*setupPath);
*setupMap = setupMap->scaled(ui->setupPushButton->size());
ui->setupPushButton->setIcon(QIcon(*setupMap));
QString *addPath = new \
QString("/var/ftp/pub/apue/qq/imgs/add.png");
QPixmap *addMap = new QPixmap(*addPath);
*addMap = addMap->scaled(ui->addPushButton->size());
ui->addPushButton->setIcon(QIcon(*addMap));
//创建时钟对象
tm = new QTimer();
//链接信号与槽
connect(tm, SIGNAL(timeout()), this, SLOT(timerHandler()));
tm->start(1000); //alarm(1)
socket = new QUdpSocket();
socket->bind(0);
connect(socket, SIGNAL(readyRead()), this, SLOT(readDataHandler()));
}
void ChatPage::readDataHandler()
{
struct chat_st rcvbuf;
QTreeWidgetItem *item;
QString sndmsg;
socket->readDatagram((char *)&rcvbuf, sizeof(rcvbuf));
qDebug() << rcvbuf.status << endl;
switch(rcvbuf.status) {
case CHAT_STATUS_LIST:
//remmeber address server 发送的好友列表
item = new QTreeWidgetItem(ui->friendListTreeWidget);
item->setIcon(0, QIcon("/var/ftp/pub/apue/qq/imgs/women.png"));
item->setText(0, QString(rcvbuf.dst_count));
break;
case CHAT_STATUS_MSG:
//聊天服务器发送的聊天消息
//[one >>> two]
qDebug() << "the msg coming" << endl;
sndmsg.clear();
sndmsg.append("<font size=5 color=blue>");
sndmsg.append("[");
sndmsg.append(rcvbuf.src_count);
sndmsg.append(" >>> ");
sndmsg.append(rcvbuf.dst_count);
sndmsg.append("] ");
sndmsg.append(rcvbuf.msg);
sndmsg.append("</font>");
ui->chatTextEdit->append(sndmsg);
break;
default:
break;
}
}
void ChatPage::timerHandler()
{
QDateTime date = QDateTime::currentDateTime();
QString str = date.toString(QString("yy-MM-dd hh:mm:ss"));
ui->lcdNumber->display(str);
}
//重写show方法Str
void ChatPage::chatPageShow(QString curcount)
{
struct remember_st sndbuf;
myCount = curcount;
/*
QTreeWidgetItem *item = new QTreeWidgetItem(ui->friendListTreeWidget);
item->setIcon(0, QIcon("/var/ftp/pub/apue/qq/imgs/women.png"));
item->setText(0, curcount);
*/
ui->cntLabel->setText(myCount);
//将聊天用户的帐号和地址发送记住地址服务器
QByteArray ba = curcount.toAscii();
const char *c = ba.data();
qsnprintf(sndbuf.count, COUNT_SIZE, "%s", c);
QHostAddress *address = new\
QHostAddress(QString(REMEMBER_ADDR_IP));
socket->writeDatagram((const char *)&sndbuf, sizeof(sndbuf),\
*address, REMEMBER_ADDR_PORT);
timerHandler();
show();
}
ChatPage::~ChatPage()
{
delete ui;
}
void ChatPage::on_addPushButton_clicked()
{
AddFriendPage *add = new AddFriendPage();
add->setAttribute(Qt::WA_DeleteOnClose);
add->myCount = myCount;
add->friendWidget = ui->friendListTreeWidget;
add->show();
}
void ChatPage::on_friendListTreeWidget_doubleClicked(const QModelIndex &index)
{
QTreeWidgetItem *item= ui->friendListTreeWidget->currentItem();
ui->cntLabel->setText(item->text(0));
}
void ChatPage::on_sendPushButton_clicked()
{
QString sndmsg;
QString msg = ui->inputTextEdit->toPlainText();
//[one >>> two]
sndmsg.clear();
sndmsg.append("<font size=5 color=red>");
sndmsg.append("[");
sndmsg.append(myCount);
sndmsg.append(" >>> ");
sndmsg.append(ui->cntLabel->text());
sndmsg.append("] ");
sndmsg.append(msg);
sndmsg.append("</font>");
ui->chatTextEdit->append(sndmsg);
ui->inputTextEdit->clear();
//将聊天内容发送给服务器
struct chat_st sndbuf;
QByteArray ba = myCount.toAscii();
const char *c = ba.data();
qsnprintf(sndbuf.src_count, COUNT_SIZE, "%s", c);
QByteArray ba1 = ui->cntLabel->text().toAscii();
const char *d = ba1.data();
qsnprintf(sndbuf.dst_count, COUNT_SIZE, "%s", d);
QByteArray ba2 = msg.toAscii();
const char *s = ba2.data();
qsnprintf(sndbuf.msg, COUNT_SIZE, "%s", s);
QHostAddress *address = new QHostAddress\
(QString(CHAT_SERVER_IP));
socket->writeDatagram((const char *)&sndbuf, sizeof(sndbuf),\
*address, CHAT_SERVER_PORT);
}