Skip to content

Commit

Permalink
增加语音聊天功能
Browse files Browse the repository at this point in the history
  • Loading branch information
SandTripper committed Nov 18, 2022
1 parent 06bbcef commit 7d90d2a
Show file tree
Hide file tree
Showing 62 changed files with 1,590 additions and 110 deletions.
26 changes: 0 additions & 26 deletions -- SQLite.sql

This file was deleted.

12 changes: 6 additions & 6 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
/.vscode/
#/.vscode/

# /.VSCodeCounter/
/.VSCodeCounter/

# /build-debug/
/build-debug/

# /build-release/
/build-release/

# .gitignore
.gitignore

/Release

--SQLite.sql
#--SQLite.sql
22 changes: 0 additions & 22 deletions .vscode/c_cpp_properties.json

This file was deleted.

12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,3 +94,15 @@ Todo

![](/introduction/PictureViewingWindow.jpg)

* 发起语音聊天界面

![](/introduction/VoiceChatWaiting.png)

* 语音聊天是否接听界面

![](/introduction/VoiceChatChoose.png)

* 正在语音聊天界面

![](/introduction/VoiceChatting.png)

8 changes: 8 additions & 0 deletions didactic-pancake-client/BaseTitleBar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,14 @@ void BaseTitleBar::setButtonType(ButtonType buttonType)
}
}

void BaseTitleBar::setButtonStyle(const QString &qss_btn_min, const QString &qss_btn_restore, const QString &qss_btn_max, const QString &qss_btn_close)
{
m_pButtonMin->setStyleSheet(qss_btn_min);
m_pButtonRestore->setStyleSheet(qss_btn_restore);
m_pButtonMax->setStyleSheet(qss_btn_max);
m_pButtonClose->setStyleSheet(qss_btn_close);
}

// 保存窗口最大化前窗口的位置以及大小;
void BaseTitleBar::saveRestoreInfo(const QPoint point, const QSize size)
{
Expand Down
3 changes: 3 additions & 0 deletions didactic-pancake-client/BaseTitleBar.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ class BaseTitleBar : public QWidget
// 设置标题栏上按钮类型;
void setButtonType(ButtonType buttonType);

//设置四个按钮的样式
void setButtonStyle(const QString&qss_btn_min,const QString&qss_btn_restore,const QString&qss_btn_max,const QString&qss_btn_close);

// 保存/获取 最大化前窗口的位置及大小;
void saveRestoreInfo(const QPoint point, const QSize size);
void getRestoreInfo(QPoint &point, QSize &size);
Expand Down
2 changes: 1 addition & 1 deletion didactic-pancake-client/BaseWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ void BaseWindow::paintEvent(QPaintEvent *event)
QPainter painter(this);
QPainterPath pathBack;
pathBack.setFillRule(Qt::WindingFill);
pathBack.addRoundedRect(QRect(1, 1, this->width() - 1, this->height() - 2), 3, 3);
pathBack.addRoundedRect(QRect(1, 1, this->width() - 2, this->height() - 2), 3, 3);
painter.setRenderHint(QPainter::SmoothPixmapTransform, true);
painter.fillPath(pathBack, QBrush(QColor(m_colorR, m_colorG, m_colorB)));

Expand Down
6 changes: 5 additions & 1 deletion didactic-pancake-client/ChatItem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -401,9 +401,13 @@ void ChatListWidget::selectChatItem(const QString &username)
m_items[i].second->clearUnreadNum();
SQLConnect::getInstance()->setChatItem(m_items[i].second->m_username, m_items[i].second->m_lastTime, m_items[i].second->m_unreadNum, m_items[i].second->m_content, 1);
this->setCurrentRow(i);
break;
return;
}
}

//如果不存在该item
addChatItem(username);
selectChatItem(username);
}

void ChatListWidget::hideChatItem(const QString &username)
Expand Down
6 changes: 6 additions & 0 deletions didactic-pancake-client/Config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,9 @@ QString Config::loginedUserName = "";
QString Config::loginedSessionID = "";

std::list<PictureViewingWindow *> Config::openedPictureViewingWindow;

int Config::mainWindowPosX = 0;

int Config::mainWindowPosY = 0;

bool Config::isVoiceChatting = false;
7 changes: 7 additions & 0 deletions didactic-pancake-client/Config.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,13 @@ class Config

//已打开的图片查看器
static std::list<PictureViewingWindow *> openedPictureViewingWindow;

//主窗口的x坐标
static int mainWindowPosX;
//主窗口的y坐标
static int mainWindowPosY;

static bool isVoiceChatting;
};

#endif // CONFIG_H
15 changes: 12 additions & 3 deletions didactic-pancake-client/CustomMainWindow.cpp
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
#include <windows.h>

#include "CustomMainWindow.h"
#include "ui_CustomMainWindow.h"

#include <windows.h>
#include <QSoundEffect>
#include <QMoveEvent>

#include "LoginWindow.h"
#include "SQLConnect.h"
#include "Config.h"
#include "PictureViewingWindow.h"

#include <QSoundEffect>

CustomMainWindow::CustomMainWindow(QWidget *parent) : BaseWindow(parent),
ui(new Ui::CustomMainWindow),
Expand Down Expand Up @@ -201,3 +203,10 @@ void CustomMainWindow::closeEvent(QCloseEvent *event)
}
return BaseWindow::closeEvent(event);
}

void CustomMainWindow::moveEvent(QMoveEvent *event)
{
Config::mainWindowPosX=event->pos().x();
Config::mainWindowPosY=event->pos().y();
return BaseWindow::moveEvent(event);
}
3 changes: 3 additions & 0 deletions didactic-pancake-client/CustomMainWindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ protected slots:
//重写关闭事件
void closeEvent(QCloseEvent *event);

//重写移动事件
void moveEvent(QMoveEvent *event);

private:
//初始化本窗口
void initThis();
Expand Down
93 changes: 80 additions & 13 deletions didactic-pancake-client/MessageBar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@
#include "MessageBar.h"
#include "ui_MessageBar.h"
#include "FriendItem.h"
#include "VoiceChatWindow.h"
#include "Config.h"

MessageBar::MessageBar(QWidget *parent) : QWidget(parent),
m_colorR(0), m_colorG(0), m_colorB(0),
ui(new Ui::MessageBar),
nowUser(""),
m_connect(TcpConnect::getInstance()),
m_soundPlayer(new SoundPlayer)
m_colorR(0), m_colorG(0), m_colorB(0),
ui(new Ui::MessageBar),
nowUser(""),
m_tcpConnect(TcpConnect::getInstance()),
m_soundNewMessage(new SoundPlayer(SoundPlayer::NEW_MESSAGE))
{
ui->setupUi(this);

Expand All @@ -19,7 +21,7 @@ MessageBar::MessageBar(QWidget *parent) : QWidget(parent),

MessageBar::~MessageBar()
{
delete m_soundPlayer;
delete m_soundNewMessage;
delete ui;
}

Expand All @@ -28,8 +30,9 @@ void MessageBar::initThis()
//设置窗口无边框
setWindowFlags(Qt::FramelessWindowHint);

connect(m_connect, &TcpConnect::SMApackAdd, this, &MessageBar::handleSMApackAdd);
connect(m_connect, &TcpConnect::RMApackAdd, this, &MessageBar::handleRMApackAdd);
connect(m_tcpConnect, &TcpConnect::SMApackAdd, this, &MessageBar::handleSMApackAdd);
connect(m_tcpConnect, &TcpConnect::RMApackAdd, this, &MessageBar::handleRMApackAdd);
connect(m_tcpConnect, &TcpConnect::ROCpackAdd, this, &MessageBar::handleROCpackAdd);
}

void MessageBar::initControl()
Expand Down Expand Up @@ -141,7 +144,7 @@ void MessageBar::deleteUserWidget(const QString &username)

void MessageBar::handleRMApackAdd()
{
for (const auto &data : m_connect->vec[TcpConnect::RMA])
for (const auto &data : m_tcpConnect->vec[TcpConnect::RMA])
{
if (data.content_len == 0)
continue;
Expand Down Expand Up @@ -186,14 +189,14 @@ void MessageBar::handleRMApackAdd()
m_userWidgets[username]->getListWidget()->addOtherMessage(content, time.toLongLong());

//新消息声音提示
m_soundPlayer->playNewMessageEffect();
m_soundNewMessage->playSound(0.5,1);
}
m_connect->vec[TcpConnect::RMA].clear();
m_tcpConnect->vec[TcpConnect::RMA].clear();
}

void MessageBar::handleSMApackAdd()
{
for (const auto &data : m_connect->vec[TcpConnect::SMA])
for (const auto &data : m_tcpConnect->vec[TcpConnect::SMA])
{
if (data.content_len == 0)
continue;
Expand All @@ -212,7 +215,71 @@ void MessageBar::handleSMApackAdd()
time.remove(time.length() - 2, 2);
MessageWidget::messageHasSend(msg_id.toLongLong());
}
m_connect->vec[TcpConnect::SMA].clear();
m_tcpConnect->vec[TcpConnect::SMA].clear();
}

void MessageBar::handleROCpackAdd()
{
for (const auto &data : m_tcpConnect->vec[TcpConnect::ROC])
{
if (data.content_len == 0)
continue;

QString str_id = "";

//解析用户名
QString tar_user = "";
for (int i = 0; i < data.content_len - 1; i++)
{
if (data.content[i] == '\r' && data.content[i + 1] == '\n')
{
break;
}
tar_user += data.content[i];
}
if(Config::isVoiceChatting)
break;
Config::isVoiceChatting = true;
VoiceChatWindow *vcw = new VoiceChatWindow(tar_user,VoiceChatWindow::CHOOSE_ACCEPT_OR_NOT);
vcw->move(Config::mainWindowPosX+200,Config::mainWindowPosY+30);
vcw->show();

connect(vcw,&VoiceChatWindow::closed,[=](VoiceChatWindow::CLOSE_TYPE type,const QString& formatTime)
{
handleVoiceChatWindowClosed(type,formatTime,tar_user);
});

break;
}
m_tcpConnect->vec[TcpConnect::ROC].clear();
}

void MessageBar::handleVoiceChatWindowClosed(VoiceChatWindow::CLOSE_TYPE type, const QString &formatTime, const QString &username)
{
Config::isVoiceChatting = false;
QString content;
switch (type)
{
case VoiceChatWindow::NORMAL_CALL:
content = "通话时长 "+formatTime+"";
break;
case VoiceChatWindow::REJECT:
content = "已拒绝 ☏";
break;
case VoiceChatWindow::FRIEND_CANCEL:
content = "对方已取消 ☏";
break;
default:
return;
}
if (m_userWidgets.find(username) == m_userWidgets.end()) //不存在该用户窗口
{
MessageWidget *widget = new MessageWidget(username, this);
connect(widget, &MessageWidget::messageAdd, this, &MessageBar::messageAdd);
m_userWidgets[username] = widget;
}
long long l_time = (long long)QDateTime::currentDateTime().toTime_t()*1000+QDateTime::currentDateTime().time().msec();
m_userWidgets[username]->getListWidget()->addOtherMessage(content,l_time);
}

void MessageBar::paintEvent(QPaintEvent *event)
Expand Down
9 changes: 7 additions & 2 deletions didactic-pancake-client/MessageBar.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,11 @@ private slots:

void handleSMApackAdd();

void handleROCpackAdd();

//处理语音聊天窗口退出
void handleVoiceChatWindowClosed(VoiceChatWindow::CLOSE_TYPE type,const QString& formatTime,const QString&username);

private:
//初始化本窗口
void initThis();
Expand All @@ -72,9 +77,9 @@ private slots:

QString nowUser;

TcpConnect *m_connect;
TcpConnect *m_tcpConnect;

SoundPlayer *m_soundPlayer;
SoundPlayer *m_soundNewMessage;
};

#endif // MESSAGEBAR_H
1 change: 1 addition & 0 deletions didactic-pancake-client/MessageItem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ MessageItem::MessageItem(const QString &tarUserName, QWidget *parent) : QWidget(

void MessageItem::setTextSuccess()
{
SQLConnect::getInstance()->setChatRecordisSend(message_id, 1);
m_loading->hide();
m_loadingMovie->stop();
m_isSending = true;
Expand Down
Loading

0 comments on commit 7d90d2a

Please sign in to comment.