Repository navigation
Expand file tree
/
Copy pathchatbox.cpp
More file actions
112 lines (82 loc) · 3.1 KB
/
Copy pathchatbox.cpp
File metadata and controls
112 lines (82 loc) · 3.1 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
#include "chatbox.h"
#include "qdatetime.h"
#include "ui_chatbox.h"
#include "mainwindow.h"
#include "qsqlerror.h"
#include "qsqlquery.h"
#include "qurlquery.h"
QString friendN;
ChatBox::ChatBox(QWidget *parent) :
QWidget(parent),
ui(new Ui::ChatBox)
{
ui->setupUi(this);
setstyle();
}
ChatBox::~ChatBox()
{
delete ui;
}
void ChatBox::fillChat(const QString &friendName){
qDebug() << "Filling chat with" << friendName;
friendN=friendName;
// Replace the loop with SQL query execution
QSqlQuery query;
query.prepare("EXEC GetChatsBetweenUser @sender_id = :sender_id, @receiver_username = :receiver_username");
query.bindValue(":sender_id", ID); // Assuming you have currentUserID defined somewhere
query.bindValue(":receiver_username", friendName);
if (!query.exec()) {
QMessageBox::critical(this, "Error", "Failed to retrieve chats: " + query.lastError().text());
return;
}
QWidget *contentWidget = new QWidget(this);
QVBoxLayout *layout = new QVBoxLayout(contentWidget);
// Iterate through the results and populate the layout
while (query.next()) {
QString messageContent = query.value("message_content").toString();
QDateTime sentAt = query.value("sent_at").toDateTime();
QHBoxLayout *hLayout = new QHBoxLayout();
QLabel *messageLabel = new QLabel(messageContent + " (" + sentAt.toString() + ")", this);
hLayout->addWidget(messageLabel);
layout ->addLayout(hLayout);
}
// Set the content widget as the scroll area's widget
//this->setLayout(layout);
// Set the content widget as the scroll area's widget
ui->scrollAreachat->setWidget(contentWidget);
}
void ChatBox::on_Back_clicked()
{
emit goBack();
}
void ChatBox::setstyle(){
QString styleSheet = R"(
/* Global font style */
* {
font: 700 12pt "UD Digi Kyokasho NK-B";
}
)";
// Apply the stylesheet to all child widgets of this widget
this->setStyleSheet(styleSheet);
}
void ChatBox::on_send_button_clicked()
{
QString friendName = friendN;
QString chatContent = ui->message->toPlainText();
if (!chatContent.isEmpty() && !friendName.isEmpty()) {
QSqlQuery query;
query.prepare("EXEC SendMessageS @SenderID=:SenderID, @ReceiverUsername=:ReceiverUsername, @MessageContent=:MessageContent");
query.bindValue(":SenderID", ID);
query.bindValue(":ReceiverUsername", friendName);
query.bindValue(":MessageContent", chatContent);
if (query.exec()) {
QMessageBox::information(this, "Message Sent", "Your message has been sent successfully.");
ui->message->clear();
} else {
QMessageBox::critical(this, "Error", "Failed to send message: " + query.lastError().text());
}
} else {
QMessageBox::warning(this, "Input Error", "Please enter a message and select a friend.");
}
fillChat(friendN);
}