chatwindow.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. #include "chatwindow.h"
  2. #include "appevent.h"
  3. #include "widgets/chatView/chat1/chatview.h"
  4. ChatWindow::ChatWindow(WebSocketClient *webSocketClient, QWidget *parent)
  5. : QWidget(parent)
  6. , m_webSocketClient(webSocketClient)
  7. {
  8. setWindowTitle("气泡消息示例");
  9. resize(800, 600);
  10. QVBoxLayout *mainLayout = new QVBoxLayout(this);
  11. // 创建消息视图
  12. m_messageView = new ChatView(this);
  13. mainLayout->addWidget(m_messageView);
  14. // 创建输入区域
  15. QHBoxLayout *inputLayout = new QHBoxLayout();
  16. m_inputEdit = new QLineEdit(this);
  17. m_inputEdit->setPlaceholderText("输入消息...");
  18. inputLayout->addWidget(m_inputEdit);
  19. QPushButton *sendButton = new QPushButton("发送", this);
  20. inputLayout->addWidget(sendButton);
  21. QPushButton *imageButton = new QPushButton("图片", this);
  22. // inputLayout->addWidget(imageButton);
  23. imageButton->setHidden(true);
  24. QPushButton *fileButton = new QPushButton("文件", this);
  25. // inputLayout->addWidget(fileButton);
  26. fileButton->setHidden(true);
  27. QPushButton *recallButton = new QPushButton("撤回", this);
  28. // inputLayout->addWidget(recallButton);
  29. recallButton->setHidden(true);
  30. mainLayout->addLayout(inputLayout);
  31. // 连接信号和槽
  32. connect(sendButton, &QPushButton::clicked, this, &ChatWindow::onSendClicked);
  33. connect(m_inputEdit, &QLineEdit::returnPressed, this, &ChatWindow::onSendClicked);
  34. connect(imageButton, &QPushButton::clicked, this, &ChatWindow::onImageClicked);
  35. connect(fileButton, &QPushButton::clicked, this, &ChatWindow::onFileClicked);
  36. connect(recallButton, &QPushButton::clicked, this, &ChatWindow::onRecallClicked);
  37. // // 添加一些示例消息
  38. // m_messageView->addMessage(QDateTime::currentDateTime().toString(), "", true);
  39. // m_messageView->addMessage("你好,这是一条测试文本消息!", "", true);
  40. // m_messageView->addMessage(
  41. // "你好,这是一条测试文本消息!你好,这是一条测试文本消息!你好,这是一条测试文本消息!你好,"
  42. // "这是一条测试文本消息!你好,这是一条测试文本消息!你好,这是一条测试文本消息!你好,这是一"
  43. // "条测试文本消息!你好,这是一条测试文本消息!你好,这是一条测试文本消息!",
  44. // "",
  45. // true);
  46. // initWebsocket();
  47. // m_messageView->addSuccessMessage("操作成功完成", BubbleMessage::Sent);
  48. // m_messageView->addFailureMessage("操作失败", BubbleMessage::Received);
  49. connect(m_webSocketClient,
  50. &WebSocketClient::messageReceived,
  51. this,
  52. [this](const ChatMessage &message) {
  53. qDebug() << message.text;
  54. if (message.isLeft()) {
  55. m_messageView->addMessage(message.text, "", message.senderName, true);
  56. } else if (message.isRight()) {
  57. m_messageView->addMessage(message.text, "", message.senderName, false);
  58. } else {
  59. m_messageView->addSystemMessage(message.text);
  60. }
  61. }); // 回复
  62. // connect(m_webSocketClient,
  63. // &WebSocketClient::connectionStateChanged,
  64. // this,
  65. // &ChatRoomWidget::onConnectionStateChanged); //连接状态
  66. connect(m_webSocketClient,
  67. &WebSocketClient::errorOccurred,
  68. this,
  69. [](const QString &errorMessage) { qDebug() << errorMessage; }); //错误反馈
  70. connect(m_webSocketClient, &WebSocketClient::connectionStateChanged, this, [](bool connected) {
  71. emit AppEvent::instance()->connectionStateChanged(connected);
  72. });
  73. }
  74. void ChatWindow::initWebsocket(const QString &roomId)
  75. {
  76. // 首先断开链接
  77. m_webSocketClient->disconnect();
  78. if (roomId.isEmpty()) {
  79. m_webSocketClient->connectToRoom("416a1990-85ae-48af-aef6-5168230b999f");
  80. } else {
  81. m_webSocketClient->connectToRoom(roomId);
  82. }
  83. }
  84. void ChatWindow::onSendClicked()
  85. {
  86. QString text = m_inputEdit->text().trimmed();
  87. if (text.isEmpty())
  88. return;
  89. m_webSocketClient->sendMessage(text, "room");
  90. m_messageView->addMessage(text, "", AppEvent::instance()->userName(), false);
  91. m_inputEdit->clear();
  92. }
  93. void ChatWindow::onImageClicked()
  94. {
  95. QString fileName = QFileDialog::getOpenFileName(this,
  96. "选择图片",
  97. QString(),
  98. "图片文件 (*.png *.jpg *.jpeg *.bmp)");
  99. if (fileName.isEmpty())
  100. return;
  101. QPixmap image(fileName);
  102. if (image.isNull())
  103. return;
  104. }
  105. void ChatWindow::onFileClicked()
  106. {
  107. QString fileName = QFileDialog::getOpenFileName(this, "选择文件");
  108. if (fileName.isEmpty())
  109. return;
  110. // QFileInfo fileInfo(fileName);
  111. // // 随机发送或接收文件
  112. // BubbleMessage::MessageDirection direction = (qrand() % 2 == 0) ? BubbleMessage::Sent
  113. // : BubbleMessage::Received;
  114. // m_messageView->addFileMessage(fileInfo.fileName(), fileName, fileInfo.size(), direction);
  115. }
  116. void ChatWindow::onRecallClicked()
  117. {
  118. // 随机发送或接收撤回消息
  119. // BubbleMessage::MessageDirection direction = (qrand() % 2 == 0) ? BubbleMessage::Sent
  120. // : BubbleMessage::Received;
  121. // QString name = (direction == BubbleMessage::Sent) ? "你" : "对方";
  122. // m_messageView->addRecallMessage(name, direction);
  123. }