-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmessagebubblewidget.cpp
More file actions
141 lines (116 loc) · 4.57 KB
/
messagebubblewidget.cpp
File metadata and controls
141 lines (116 loc) · 4.57 KB
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
#include "messagebubblewidget.h"
#include <QDateTime>
#include <QFrame>
#include<QJsonDocument>
#include<QJsonObject>
#include<QJsonArray>
MessageBubbleWidget::MessageBubbleWidget(const ChatMessage& message, const QString& senderName, QWidget *parent)
: QWidget(parent), m_message(message), m_senderName(senderName), m_isFromMe(message.isFromMe),
m_contentLabel(nullptr),
m_timeLabel(nullptr),
senderLabel(nullptr)
{
// Main horizontal layout for alignment
QHBoxLayout* mainLayout = new QHBoxLayout(this);
mainLayout->setContentsMargins(10, 5, 10, 5);
// Vertical layout for sender label and bubble
QVBoxLayout* verticalLayout = new QVBoxLayout();
verticalLayout->setSpacing(2);
verticalLayout->setContentsMargins(0, 0, 0, 0);
// Sender label (only for received messages, but you can show for all if you want)
if (!m_isFromMe) {
senderLabel = new QLabel(senderName, this);
senderLabel->setStyleSheet("font-size: 11px; color: #888; font-weight: bold;");
verticalLayout->addWidget(senderLabel, 0, Qt::AlignLeft);
}else{
senderLabel = nullptr;
}
// Create bubble container
QFrame* bubbleContainer = new QFrame(this);
bubbleContainer->setMinimumWidth(0); // was 100
bubbleContainer->setMaximumWidth(500); // was 400
bubbleContainer->setSizePolicy(QSizePolicy::Maximum, QSizePolicy::Minimum);
bubbleContainer->setStyleSheet(
m_isFromMe
? "QFrame { background-color: #007AFF; border-radius: 18px; }"
: "QFrame { background-color: #28A745; border-radius: 18px; }"
);
// Create bubble layout
QVBoxLayout* bubbleLayout = new QVBoxLayout(bubbleContainer);
bubbleLayout->setContentsMargins(12, 8, 12, 8);
bubbleLayout->setSpacing(2);
// Message content label
m_contentLabel = new QLabel(message.content, bubbleContainer);
m_contentLabel->setTextInteractionFlags(Qt::TextSelectableByMouse | Qt::TextSelectableByKeyboard);
m_contentLabel->setMinimumSize(0, 0); // 50,20
m_contentLabel->setMaximumWidth(450); //350
m_contentLabel->setWordWrap(true);
m_contentLabel->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Minimum);
m_contentLabel->setStyleSheet("color: white; font-size: 14px; font-weight: normal;");
bubbleLayout->addWidget(m_contentLabel);
// Time label
//m_timeLabel = new QLabel(message.timestamp.toString("hh:mm:ss"), bubbleContainer); //might leak senders timezone
// ALWAYS show receiver's local time for ALL messages
m_timeLabel = new QLabel(QDateTime::currentDateTime().toString("hh:mm:ss"), bubbleContainer);
m_timeLabel->setStyleSheet("color: rgba(255, 255, 255, 180); font-size: 11px;");
m_timeLabel->setAlignment(Qt::AlignRight);
m_timeLabel->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed);
bubbleLayout->addWidget(m_timeLabel);
// Add bubble to vertical layout
verticalLayout->addWidget(bubbleContainer, 0, m_isFromMe ? Qt::AlignRight : Qt::AlignLeft);
// Align the whole block left or right
if (m_isFromMe) {
mainLayout->addStretch();
mainLayout->addLayout(verticalLayout);
} else {
mainLayout->addLayout(verticalLayout);
mainLayout->addStretch();
}
//
this->setContextMenuPolicy(Qt::DefaultContextMenu);
// Set stylesheet for the context menu
QString lightMenuStyle =
"QMenu {"
" background-color: #ffffff;"
" border: 1px solid #c0c0c0;"
" color: #000000;"
"}"
"QMenu::item {"
" background-color: transparent;"
" padding: 2px 16px 2px 16px;" // Minimal padding
" color: #000000;"
"}"
"QMenu::item:selected {"
" background-color: #e0e0e0;"
" color: #000000;"
"}";
this->setStyleSheet(lightMenuStyle);
//
setLayout(mainLayout);
}
void MessageBubbleWidget::paintEvent(QPaintEvent *event)
{
QWidget::paintEvent(event);
}
QLabel *MessageBubbleWidget::timeLabel() const
{
return m_timeLabel;
}
void MessageBubbleWidget::setFontSize(int size)
{
if (m_contentLabel) {
m_contentLabel->setStyleSheet(
QString("color: white; font-weight: normal; font-size: %1pt;").arg(size)
);
}
if (m_timeLabel) {
m_timeLabel->setStyleSheet(
QString("color: rgba(255,255,255,180); font-size: %1pt;").arg(std::max(8, size - 4))
);
}
if (senderLabel) {
senderLabel->setStyleSheet(
QString("color: #888; font-weight: bold; font-size: %1pt;").arg(std::max(8, size - 4))
);
}
}