-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmainwindow.cpp
167 lines (131 loc) · 4.66 KB
/
mainwindow.cpp
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#include "mainwindow.h"
#include "./ui_mainwindow.h"
#include "maintoolbar.h"
#include "preferences.h"
#include "sendrecv.h"
#include "version.h"
#include <QMessageBox>
#include <QSlider>
#define STRINGIZE_(str) #str
#define STRINGIZE(x) STRINGIZE_(x)
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
, m_mainToolbar(new MainToolBar(this))
{
setWindowIcon(QIcon(":/ico.png"));
ui->setupUi(this);
ui->toolBar->addWidget(m_mainToolbar);
m_volume = new QSlider(Qt::Horizontal, this);
m_volume->setMaximumWidth(100);
m_volume->setRange(0, 100);
m_volume->setValue(100);
statusBar()->addPermanentWidget(m_volume);
// Set the text edit widget to read-only mode
ui->chatDisplay->setReadOnly(true);
// Set the text edit widget to word wrap mode
ui->chatDisplay->setWordWrapMode(QTextOption::WrapAtWordBoundaryOrAnywhere);
// Set the line edit widget to clear its text when return is pressed
ui->chatInput->setClearButtonEnabled(true);
// Set the push button widget to be enabled only when the line edit widget has some text
ui->sendButton->setEnabled(false);
connect(m_mainToolbar, &MainToolBar::ringingCall, this, &MainWindow::onRingingCall);
connect(m_mainToolbar, &MainToolBar::hangUp, this, &MainWindow::onHangUp);
connect(m_mainToolbar, &MainToolBar::help, this, &MainWindow::onHelp);
connect(m_mainToolbar, &MainToolBar::settings, this, &MainWindow::onSettings);
connect(ui->chatInput, &QLineEdit::textChanged, this, &MainWindow::onChatInputTextChanged);
connect(ui->chatInput, &QLineEdit::returnPressed, this, &MainWindow::onChatInputReturnPressed);
connect(ui->sendButton, &QPushButton::clicked, this, &MainWindow::onSendButtonClicked);
connect(this, &MainWindow::messageReceived, this, &MainWindow::onMessageReceived);
}
MainWindow::~MainWindow()
{
cleanup_and_quit_loop("hand up", false);
delete ui;
}
void MainWindow::onRingingCall()
{
start_sendrecv(ui->videoArea->winId(), this);
}
void MainWindow::onHangUp()
{
cleanup_and_quit_loop("hand up", false);
}
void MainWindow::onHelp()
{
raise();
activateWindow();
QMessageBox::about(
this,
tr("About webrtc-ui"),
"\nVersion " STRINGIZE(GIT_COMMIT) "\n" +
QApplication::organizationDomain() + '/' + QApplication::organizationName() + '/' + QApplication::applicationName());
}
void MainWindow::onSettings()
{
raise();
activateWindow();
Preferences prefDlg(this);
prefDlg.exec();
}
// A slot that is called when the text of the chat input changes
void MainWindow::onChatInputTextChanged(const QString& text)
{
// Enable or disable the send button depending on whether the text is empty or not
ui->sendButton->setEnabled(!text.isEmpty());
}
// A slot that is called when the return key is pressed on the chat input
void MainWindow::onChatInputReturnPressed()
{
// Send the text of the chat input as a message
sendMessage(ui->chatInput->text());
}
// A slot that is called when the send button is clicked
void MainWindow::onSendButtonClicked()
{
// Send the text of the chat input as a message
sendMessage(ui->chatInput->text());
}
// A slot that is called when a message is received from another user
void MainWindow::onMessageReceived(const QString& message)
{
// Display the message on the chat display with the sender's name and a different text color
ui->chatDisplay->append(QStringLiteral("<font color=blue>%1</font>").arg(message));// , Qt::blue);
}
// A helper function that sends a message to another user
void MainWindow::sendMessage(const QString& message)
{
// Display the message on the chat display with the sender's name and a different text color
ui->chatDisplay->append(QStringLiteral("<font color=red>%1</font>").arg(message));// , Qt::red);
// Emit a signal that the message has been sent
emit messageSent(message);
// Clear the chat input
ui->chatInput->clear();
}
void MainWindow::handleRecv(uintptr_t id, const char* data)
{
if (!data || !(*data))
return;
if (id != m_channelId)
{
if (m_channelId == 0)
m_channelId = id;
else
return;
}
emit messageReceived(data);
}
QMetaObject::Connection MainWindow::setAudioVolumeLambda(std::function<void(int)> lambda)
{
return connect(m_volume, &QSlider::valueChanged, lambda);
}
QMetaObject::Connection MainWindow::setSendLambda(std::function<void(const QString&)> lambda)
{
return connect(this, &MainWindow::messageSent, lambda);
}
void MainWindow::onQuit()
{
disconnect(this, &MainWindow::messageSent, nullptr, nullptr);
m_channelId = 0;
// TODO: hang up
}