forked from yydcaib/TCPClient
新增了消息提醒功能
This commit is contained in:
parent
367173d1fc
commit
0c8ec77be5
|
@ -1,4 +1,4 @@
|
|||
QT += core gui network
|
||||
QT += core gui network multimedia
|
||||
|
||||
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
|
||||
|
||||
|
@ -38,5 +38,5 @@ else: unix:!android: target.path = /opt/$${TARGET}/bin
|
|||
RESOURCES += \
|
||||
imgs/imgs.qrc
|
||||
|
||||
RC_ICON = imgs/exeicon.ico
|
||||
RC_FILE += imgs/exeicon.rc
|
||||
RC_ICON = imgs/icon.ico
|
||||
RC_FILE += imgs/icon.rc
|
||||
|
|
|
@ -47,6 +47,7 @@ void DataManager::recv_from_server(QByteArray data)
|
|||
QJsonDocument json_data = QJsonDocument::fromJson(list[i]);
|
||||
if (json_data["type"] == "message")
|
||||
{
|
||||
emit new_message();
|
||||
QString data, sendname;
|
||||
sendname = json_data["sendname"].toString();
|
||||
data = json_data["data"].toString();
|
||||
|
@ -55,6 +56,7 @@ void DataManager::recv_from_server(QByteArray data)
|
|||
}
|
||||
else if (json_data["type"] == "file")
|
||||
{
|
||||
emit new_message();
|
||||
QString filename, file_data, sendname;
|
||||
sendname = json_data["sendname"].toString();
|
||||
filename = json_data["name"].toString();
|
||||
|
|
|
@ -26,6 +26,7 @@ private:
|
|||
signals:
|
||||
void connect_to_server();
|
||||
void disconnect_from_server();
|
||||
void new_message();
|
||||
void signin_response(bool result);
|
||||
void signup_response(bool result);
|
||||
void update_text(QString sendname,QString data);
|
||||
|
|
Binary file not shown.
After Width: | Height: | Size: 908 B |
BIN
imgs/exeicon.ico
BIN
imgs/exeicon.ico
Binary file not shown.
Before Width: | Height: | Size: 12 KiB |
|
@ -1 +0,0 @@
|
|||
IDI_ICON1 ICON DISCARDABLE "exeicon.ico"
|
BIN
imgs/icon.png
BIN
imgs/icon.png
Binary file not shown.
Before Width: | Height: | Size: 9.5 KiB After Width: | Height: | Size: 9.6 KiB |
|
@ -11,5 +11,7 @@
|
|||
<file>save.png</file>
|
||||
<file>send.png</file>
|
||||
<file>text.png</file>
|
||||
<file>empty.png</file>
|
||||
<file>remind.wav</file>
|
||||
</qresource>
|
||||
</RCC>
|
||||
|
|
Binary file not shown.
|
@ -178,6 +178,7 @@ void Login::signin_response(bool result)
|
|||
connect(datamanager,&DataManager::update_file_private,mainwidget,&MainWidget::update_file_private);
|
||||
connect(datamanager,&DataManager::update_users,mainwidget,&MainWidget::update_users);
|
||||
connect(datamanager,&DataManager::disconnect_from_server,mainwidget,&MainWidget::disconnect_form_server);
|
||||
connect(datamanager,&DataManager::new_message,mainwidget,&MainWidget::slot_new_message);
|
||||
connect(mainwidget,&MainWidget::exit_client,this,&Login::exit_client);
|
||||
this->hide();
|
||||
mainwidget->show();
|
||||
|
|
183
mainwidget.cpp
183
mainwidget.cpp
|
@ -46,6 +46,12 @@ MainWidget::MainWidget(QString username,QWidget *parent) :
|
|||
ui->save_btn->setMouseTracking(true);
|
||||
ui->save_btn->setToolTip(QStringLiteral("保存聊天记录"));
|
||||
|
||||
CreateSystemTray();
|
||||
effect = new QSoundEffect(this);
|
||||
effect->setSource(QUrl("qrc:/remind.wav"));
|
||||
effect->setLoopCount(1);
|
||||
effect->setVolume(0.95f);
|
||||
|
||||
ui->msg_edit->installEventFilter(this);//设置完后自动调用其eventFilter函数
|
||||
this->username = username;
|
||||
}
|
||||
|
@ -172,8 +178,7 @@ void MainWidget::on_send_btn_clicked()
|
|||
{
|
||||
zoom_down(ui->send_btn);
|
||||
zoom_up(ui->send_btn);
|
||||
QString text;
|
||||
text = ui->msg_edit->text();
|
||||
QString text = ui->msg_edit->text();
|
||||
if(text.isEmpty())
|
||||
{
|
||||
QMessageBox::warning(this, "发送失败", "输入不能为空!");
|
||||
|
@ -342,6 +347,43 @@ void MainWidget::disconnect_form_server()
|
|||
QMessageBox::critical(this,"连接超时","请检查您的网络连接!");
|
||||
qApp->quit();
|
||||
}
|
||||
|
||||
void MainWidget::slot_new_message()
|
||||
{
|
||||
emit new_message();
|
||||
}
|
||||
|
||||
void MainWidget::message_reminder()
|
||||
{
|
||||
QApplication::alert(this);// 任务栏通知
|
||||
ui->textEdit->append("<span style=\"font-size:12px;color:LightGray;\"> "
|
||||
" "
|
||||
"—————————以下是新消息—————————\n</span>");
|
||||
disconnect(this,&MainWidget::new_message,this,&MainWidget::message_reminder);
|
||||
if (TimerFlash == NULL)
|
||||
{
|
||||
flashCount = 0;
|
||||
TimerFlash = new QTimer(this);
|
||||
connect(TimerFlash, &QTimer::timeout, this, &MainWidget::onFlashTimerOut);
|
||||
TimerFlash->start(500);
|
||||
}
|
||||
}
|
||||
|
||||
void MainWidget::onFlashTimerOut()
|
||||
{
|
||||
flashCount++;
|
||||
if (flashCount % 2 == 1)
|
||||
{
|
||||
SystemTray->setIcon(QIcon(":/empty.png"));
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
SystemTray->setIcon(QIcon(":/icon.png"));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
void MainWidget::sendFile(QString filePath,QString filename,QString name)
|
||||
{
|
||||
QFile file(filePath);
|
||||
|
@ -412,6 +454,79 @@ void MainWidget::readFile(QString filename,QByteArray data)
|
|||
}
|
||||
}
|
||||
|
||||
void MainWidget::CreateSystemTray()
|
||||
{
|
||||
//创建菜单对象和托盘图标对象
|
||||
TrayMennu = new QMenu(this);
|
||||
SystemTray = new QSystemTrayIcon(this);
|
||||
|
||||
//创建菜单项
|
||||
ActionShow = new QAction(tr("Show"), this);
|
||||
ActionQuit = new QAction(tr("Exit"), this);
|
||||
connect(ActionShow,&QAction::triggered,this,[=](){
|
||||
if(this->isMinimized())
|
||||
{
|
||||
this->showNormal();
|
||||
}
|
||||
this->show();
|
||||
//SystemTray->hide();
|
||||
});
|
||||
connect(ActionQuit,&QAction::triggered,this,[=](){
|
||||
qApp->quit();
|
||||
});
|
||||
connect(SystemTray,&QSystemTrayIcon::activated,this,[=](){
|
||||
if(this->isMinimized())
|
||||
{
|
||||
this->showNormal();
|
||||
}
|
||||
this->show();
|
||||
//SystemTray->hide();
|
||||
});
|
||||
|
||||
//添加菜单项
|
||||
TrayMennu->addAction(ActionShow);
|
||||
TrayMennu->addSeparator();
|
||||
TrayMennu->addAction(ActionQuit);
|
||||
|
||||
//为系统托盘设置菜单为m_pTrayMennu
|
||||
SystemTray->setContextMenu(TrayMennu);
|
||||
SystemTray->setIcon(QIcon(":/icon.png"));
|
||||
|
||||
SystemTray->show();
|
||||
}
|
||||
|
||||
void MainWidget::zoom_down(QPushButton* btn)
|
||||
{
|
||||
QPointer<QPropertyAnimation> animalton(new QPropertyAnimation(btn,"geometry"));
|
||||
animalton->setDuration(150);
|
||||
animalton->setStartValue(QRect(btn->x(),btn->y(),btn->width(),btn->height()));
|
||||
animalton->setEndValue(QRect(btn->x(),btn->y()+5,btn->width(),btn->height()));
|
||||
animalton->setEasingCurve(QEasingCurve::OutBounce);
|
||||
animalton->start();
|
||||
|
||||
}
|
||||
|
||||
void MainWidget::zoom_up(QPushButton* btn)
|
||||
{
|
||||
QPointer<QPropertyAnimation> animalton(new QPropertyAnimation(btn,"geometry"));
|
||||
animalton->setDuration(150);
|
||||
animalton->setStartValue(QRect(btn->x(),btn->y()+5,btn->width(),btn->height()));
|
||||
animalton->setEndValue(QRect(btn->x(),btn->y(),btn->width(),btn->height()));
|
||||
animalton->setEasingCurve(QEasingCurve::OutBounce);
|
||||
animalton->start();
|
||||
}
|
||||
|
||||
void MainWidget::changeEvent(QEvent *event)
|
||||
{
|
||||
if(event->type()!=QEvent::WindowStateChange)
|
||||
return;
|
||||
if(this->windowState()==Qt::WindowMinimized)
|
||||
{
|
||||
connect(this,&MainWidget::new_message,this,&MainWidget::message_reminder);
|
||||
connect(this,&MainWidget::new_message,effect, &QSoundEffect::play);
|
||||
}
|
||||
}
|
||||
|
||||
bool MainWidget::eventFilter(QObject *target, QEvent *event)
|
||||
{
|
||||
if(target == ui->msg_edit) //可替换
|
||||
|
@ -430,59 +545,16 @@ bool MainWidget::eventFilter(QObject *target, QEvent *event)
|
|||
return QWidget::eventFilter(target,event);
|
||||
}
|
||||
|
||||
void MainWidget::CreatSystemTray()
|
||||
void MainWidget::showEvent(QShowEvent *event)
|
||||
{
|
||||
//创建菜单对象和托盘图标对象
|
||||
QPointer<QMenu> TrayMennu(new QMenu(this));
|
||||
QPointer<QSystemTrayIcon> SystemTray(new QSystemTrayIcon(this));
|
||||
|
||||
//创建菜单项
|
||||
QPointer<QAction> ActionShow(new QAction(tr("Show"), this));
|
||||
QPointer<QAction> ActionQuit(new QAction(tr("Exit"), this));
|
||||
connect(ActionShow,&QAction::triggered,this,[=](){
|
||||
this->show();
|
||||
SystemTray->hide();
|
||||
});
|
||||
connect(ActionQuit,&QAction::triggered,this,[=](){
|
||||
this->close();
|
||||
});
|
||||
connect(SystemTray,&QSystemTrayIcon::activated,this,[=](){
|
||||
this->show();
|
||||
SystemTray->hide();
|
||||
});
|
||||
|
||||
//添加菜单项
|
||||
TrayMennu->addAction(ActionShow);
|
||||
TrayMennu->addSeparator();
|
||||
TrayMennu->addAction(ActionQuit);
|
||||
|
||||
//为系统托盘设置菜单为m_pTrayMennu
|
||||
SystemTray->setContextMenu(TrayMennu);
|
||||
SystemTray->setIcon(QIcon(":/icon.png"));
|
||||
|
||||
SystemTray->show();
|
||||
//this->setWindowFlags(Qt::Tool);
|
||||
}
|
||||
|
||||
void MainWidget::zoom_down(QPushButton* btn)
|
||||
{
|
||||
QScopedPointer<QPropertyAnimation> animalton(new QPropertyAnimation(btn,"geometry"));
|
||||
animalton->setDuration(150);
|
||||
animalton->setStartValue(QRect(btn->x(),btn->y(),btn->width(),btn->height()));
|
||||
animalton->setEndValue(QRect(btn->x(),btn->y()+5,btn->width(),btn->height()));
|
||||
animalton->setEasingCurve(QEasingCurve::OutBounce);
|
||||
animalton->start();
|
||||
|
||||
}
|
||||
|
||||
void MainWidget::zoom_up(QPushButton* btn)
|
||||
{
|
||||
QScopedPointer<QPropertyAnimation> animalton(new QPropertyAnimation(btn,"geometry"));
|
||||
animalton->setDuration(150);
|
||||
animalton->setStartValue(QRect(btn->x(),btn->y()+5,btn->width(),btn->height()));
|
||||
animalton->setEndValue(QRect(btn->x(),btn->y(),btn->width(),btn->height()));
|
||||
animalton->setEasingCurve(QEasingCurve::OutBounce);
|
||||
animalton->start();
|
||||
if(TimerFlash != NULL)
|
||||
{
|
||||
qDebug() << "有信息到来显示窗体";
|
||||
disconnect(this,&MainWidget::new_message,effect, &QSoundEffect::play);
|
||||
SystemTray->setIcon(QIcon(":/icon.png"));
|
||||
delete TimerFlash;
|
||||
TimerFlash = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
void MainWidget::closeEvent(QCloseEvent *event)
|
||||
|
@ -501,7 +573,8 @@ void MainWidget::closeEvent(QCloseEvent *event)
|
|||
{
|
||||
event->ignore();
|
||||
this->hide();
|
||||
CreatSystemTray();
|
||||
connect(this,&MainWidget::new_message,this,&MainWidget::message_reminder);
|
||||
connect(this,&MainWidget::new_message,effect, &QSoundEffect::play);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
|
25
mainwidget.h
25
mainwidget.h
|
@ -16,6 +16,8 @@
|
|||
#include <QScrollBar>
|
||||
#include <QKeyEvent>
|
||||
#include <QDate>
|
||||
#include <QSoundEffect>
|
||||
#include <QTimer>
|
||||
#include <QDesktopServices>
|
||||
#include <QPropertyAnimation>
|
||||
|
||||
|
@ -42,6 +44,8 @@ signals:
|
|||
void exit_client();
|
||||
void update_namelist(QString username);
|
||||
void account_cancellation(QString username);
|
||||
void Minimized();
|
||||
void new_message();
|
||||
|
||||
private slots:
|
||||
void on_send_text_clicked();
|
||||
|
@ -50,28 +54,39 @@ private slots:
|
|||
void on_name_list_itemDoubleClicked(QListWidgetItem *item);
|
||||
void on_exit_btn_clicked();
|
||||
void on_stop_clicked();
|
||||
void message_reminder();
|
||||
void onFlashTimerOut();
|
||||
public slots:
|
||||
void update_text(QString sendname,QString data);
|
||||
void update_file(QString sendname,QString filename,QString file_data);
|
||||
void update_file_private(QString sendname,QString filename,QString file_data);
|
||||
void update_users(int count,QList<QString>online_users);
|
||||
void disconnect_form_server();
|
||||
void slot_new_message();
|
||||
|
||||
private:
|
||||
Ui::MainWidget *ui;
|
||||
QString username;
|
||||
|
||||
QMenu* TrayMennu; //系统托盘右键菜单项
|
||||
QSystemTrayIcon* SystemTray; //系统托盘图标
|
||||
QPointer<QMenu> TrayMennu; //系统托盘右键菜单项
|
||||
QPointer<QSystemTrayIcon> SystemTray; //系统托盘图标
|
||||
|
||||
//右键菜单栏选项
|
||||
QAction* ActionShow;
|
||||
QAction* ActionQuit;
|
||||
QPointer<QAction> ActionShow;
|
||||
QPointer<QAction> ActionQuit;
|
||||
|
||||
QPointer<QTimer> TimerNotice;
|
||||
QPointer<QTimer> TimerFlash = NULL;
|
||||
uint flashCount;
|
||||
|
||||
QPointer<QSoundEffect> effect;
|
||||
|
||||
void zoom_up(QPushButton* btn);
|
||||
void zoom_down(QPushButton* btn);
|
||||
void CreatSystemTray();
|
||||
void CreateSystemTray();
|
||||
void showEvent(QShowEvent *event);
|
||||
void closeEvent(QCloseEvent *event);
|
||||
void changeEvent(QEvent *event);
|
||||
};
|
||||
|
||||
#endif // MAINWIDGET_H
|
||||
|
|
Loading…
Reference in New Issue