chatmessage.h 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. #ifndef CHATMESSAGE_H
  2. #define CHATMESSAGE_H
  3. #include <QDateTime>
  4. #include <QObject>
  5. #include <QString>
  6. #include <QSize>
  7. namespace ChatConstants {
  8. static constexpr int AVATAR_SIZE = 40;
  9. static constexpr int BUBBLE_PADDING = 12;
  10. static constexpr int BUBBLE_SPACING = 8;
  11. static constexpr int BUBBLE_RADIUS = 8;
  12. static constexpr int TIMESTAMP_HEIGHT = 20;
  13. // static constexpr int MAX_WIDTH = 600;
  14. } // namespace ChatConstants
  15. // 添加消息类型枚举
  16. enum class MessageType {
  17. Left, // 接收到的消息
  18. Right, // 发送的消息
  19. System, // 系统消息
  20. Private // 私聊消息
  21. };
  22. // 添加内容类型枚举
  23. enum class ContentType {
  24. Text, // 纯文本消息
  25. Image, // 图片消息
  26. Mixed // 混合内容(文本+图片)
  27. };
  28. class ChatMessage
  29. {
  30. public:
  31. QString id;
  32. QString text;
  33. QString senderId;
  34. QString senderName;
  35. QString roomId;
  36. QString avatar;
  37. MessageType type = MessageType::Left;
  38. ContentType contentType = ContentType::Text; // 内容类型
  39. QString imagePath; // 图片路径
  40. QSize imageSize; // 图片尺寸
  41. QString thumbnailPath; // 缩略图路径
  42. QDateTime timestamp = QDateTime::currentDateTime();
  43. bool isRead = false;
  44. ChatMessage(const QString &text = QString(),
  45. MessageType type = MessageType::Left,
  46. const QDateTime &timestamp = QDateTime::currentDateTime())
  47. : text(text)
  48. , type(type)
  49. , timestamp(timestamp)
  50. {}
  51. // 图片消息构造函数
  52. ChatMessage(const QString &imagePath,
  53. const QSize &imageSize,
  54. MessageType type = MessageType::Left,
  55. const QString &text = QString())
  56. : text(text)
  57. , type(type)
  58. , contentType(text.isEmpty() ? ContentType::Image : ContentType::Mixed)
  59. , imagePath(imagePath)
  60. , imageSize(imageSize)
  61. {}
  62. bool isLeft() const { return type == MessageType::Left; }
  63. bool isRight() const { return type == MessageType::Right; }
  64. bool isSystem() const { return type == MessageType::System; }
  65. bool hasImage() const { return contentType == ContentType::Image || contentType == ContentType::Mixed; }
  66. bool hasText() const { return contentType == ContentType::Text || contentType == ContentType::Mixed; }
  67. bool isImageOnly() const { return contentType == ContentType::Image; }
  68. };
  69. Q_DECLARE_METATYPE(ChatMessage)
  70. #endif // CHATMESSAGE_H