Repository navigation
Expand file tree
/
Copy pathchatwindow.cpp
More file actions
133 lines (93 loc) · 3.21 KB
/
Copy pathchatwindow.cpp
File metadata and controls
133 lines (93 loc) · 3.21 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
#include "chatwindow.h"
#include "ui_chatwindow.h"
#include <Qt>
#include <QScrollBar>
#include <QMessageBox>
#include <ctime>
ChatWindow::ChatWindow(User &user, LocalHost* local_host, RemoteHost* remote_host, QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::ChatWindow)
, user(user)
, local_host(local_host)
, remote_host(remote_host)
{
// Get Day and Time
char str_time[64];
std::time_t now = std::time(nullptr);
std::tm* localtm = std::localtime(&now);
std::strftime(str_time, sizeof(str_time), "%A %d-%B-%Y", localtm);
ui->setupUi(this);
this->setWindowTitle(user.name);
ui->label_day->setText(str_time);
ui->verticalLayout->setSpacing(5);
ui->verticalLayout->setContentsMargins(0, 0, 0, 0);
ui->verticalLayout->addStretch();
}
void ChatWindow::keyPressEvent(QKeyEvent *e)
{
QMainWindow::keyPressEvent(e);
if (e->key() == Qt::Key_Return)
on_btn_send_clicked();
}
void ChatWindow::resizeEvent(QResizeEvent *e)
{
QMainWindow::resizeEvent(e);
int desired_width = ui->scrollAreaWidgetContents->width() / 3 * 2;
for (int i = 1; i < ui->verticalLayout->count() - 1; ++i)
{
QWidget *label_msg = ui->verticalLayout->itemAt(i)->widget();
if (!label_msg)
continue;
label_msg->setFixedWidth(desired_width);
label_msg->setMaximumWidth(desired_width);
label_msg->adjustSize();
}
QScrollBar *bar = ui->scrollArea->verticalScrollBar();
bar->setValue(bar->maximum());
// FIX: After the window is stretched, labels take lesser space and scrollArea shows empty
// space underneath. It doesn't adjust correctly.
}
ChatWindow::~ChatWindow()
{
delete ui;
}
void ChatWindow::show_msg_label(QString& msg, int type)
{
QLabel *label_msg = new QLabel(msg, ui->scrollAreaWidgetContents);
label_msg->setProperty("type", type);
int desired_width = ui->scrollAreaWidgetContents->width() / 3 * 2;
label_msg->setFixedWidth(desired_width);
label_msg->setMaximumWidth(desired_width);
label_msg->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
label_msg->setWordWrap(true);
label_msg->setAlignment(Qt::AlignLeft);
Qt::Alignment alignment = type == 1 ? Qt::AlignRight : Qt::AlignLeft ;
ui->verticalLayout->insertWidget(ui->verticalLayout->count() - 1, label_msg, 0 , alignment);
label_msg->adjustSize();
QScrollBar *bar = ui->scrollArea->verticalScrollBar();
bar->setValue(bar->maximum());
}
void ChatWindow::on_btn_send_clicked()
{
if (!remote_host->is_connected())
{
QMessageBox::warning(this, this->windowTitle(), "Not connected to any user.\nConnect to send messages.", QMessageBox::Ok);
return;
}
QString msg = ui->lineEdit_msg->text();
if (msg.count() == 0)
return;
std::string buf = msg.toStdString();
remote_host->send_message(buf.c_str(), buf.size());
msg = "You: " + msg;
std::cout << msg.toStdString() << std::endl; // Debug
show_msg_label(msg, 1);
}
void ChatWindow::on_msg_recieved(const char *message)
{
QString msg = message;
QString username = user.name;
msg = username + ": " + msg;
std::cout << msg.toStdString() << std::endl;
show_msg_label(msg, 2);
}