Skip to content

Commit

Permalink
Merge pull request #27 from Force-quit/rc/v1.0.0
Browse files Browse the repository at this point in the history
Rc/v1.0.0
  • Loading branch information
CmD0 authored Apr 1, 2023
2 parents 1803a5d + 1782193 commit 267ee79
Show file tree
Hide file tree
Showing 19 changed files with 351 additions and 229 deletions.
160 changes: 82 additions & 78 deletions Client/QChatRoomMainWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,124 +14,128 @@
#include <QStringList>
#include <QInputDialog>
#include <QMessageBox>
#include <QGroupBox>
#include "QServerConnection.h"
#include "QChatlisMenuBar.h"
#include "QChatWidget.h"

QChatRoomMainWindow::QChatRoomMainWindow(QWidget* parent)
: QMainWindow(parent), serverConnection{ new QServerConnection(this) }
: QMainWindow(parent), client(true), serverConnection{ new QServerConnection(this) },
userDisplayName{}, participantsPanel{}, chatWidget{ new QChatWidget(this, client) }
{
QWidget* centralWidget{ new QWidget };
QVBoxLayout* centralLayout{ new QVBoxLayout };

QChatlisMenuBar* topMenuBar{ new QChatlisMenuBar(this) };
setMenuBar(topMenuBar);

QSplitter* topLayout{ new QSplitter };
topLayout->setChildrenCollapsible(false);
QChatbox* chatbox{ new QChatbox(this) };
QWidget* usersWidget{ new QWidget };
QVBoxLayout* usersLayout{ new QVBoxLayout };
QLabel* userLabel{ new QLabel("Your name: " + serverConnection->getUsername() + '@' + serverConnection->getComputerName()) };
QParticipantsPanel* participantsPanel{ new QParticipantsPanel };
usersLayout->addWidget(userLabel);
usersLayout->addWidget(participantsPanel);
usersWidget->setLayout(usersLayout);
topLayout->addWidget(chatbox);
topLayout->addWidget(usersWidget);

QHBoxLayout* textInputLayout{ new QHBoxLayout };
QLabel* textInputLabel{ new QLabel(tr("Message :")) };
QLineEdit* textInput{ new QLineEdit };
textInputLayout->addWidget(textInputLabel);
textInputLayout->addWidget(textInput);

centralLayout->addWidget(topLayout);
centralLayout->addLayout(textInputLayout);
centralWidget->setLayout(centralLayout);
setCentralWidget(centralWidget);

connect(topMenuBar, &QChatlisMenuBar::actionConnectToServer, this, &QChatRoomMainWindow::tryConnectToServer);
connect(topMenuBar, &QChatlisMenuBar::actionDisconnectFromServer, this, &QChatRoomMainWindow::tryDisconnectFromServer);
connect(topMenuBar, &QChatlisMenuBar::actionChangeUsername, this, &QChatRoomMainWindow::tryChangeUsername);
connect(topMenuBar, &QChatlisMenuBar::actionChangeComputerName, this, &QChatRoomMainWindow::tryChangeComputerName);

connect(textInput, &QLineEdit::returnPressed, [=]() {
QString currentText(textInput->text().simplified());
if (!currentText.isEmpty())
{
chatbox->appendUserMessage(serverConnection->getUsername(), currentText);
serverConnection->sendNewChatMessage(currentText);
}
else
chatbox->appendSystemMessage("Can't send empty messages -_-");

textInput->clear();
});
QSplitter* splitter{ new QSplitter(this) };
splitter->setChildrenCollapsible(false);

splitter->addWidget(chatWidget);
splitter->addWidget(initParticipantsWidget());

resize(1000, 400);
setCentralWidget(splitter);
setWindowTitle("Chatlis");
setWindowIcon(QIcon("group-chat.png"));

connect(topMenuBar, &QChatlisMenuBar::actionConnectToServer, this, &QChatRoomMainWindow::actionConnectToServer);
connect(topMenuBar, &QChatlisMenuBar::actionDisconnectFromServer, this, &QChatRoomMainWindow::actionDisconnectFromServer);
connect(topMenuBar, &QChatlisMenuBar::actionChangeUsername, this, &QChatRoomMainWindow::actionChangeUsername);
connect(topMenuBar, &QChatlisMenuBar::actionChangeComputerName, this, &QChatRoomMainWindow::actionChangeComputerName);

connect(serverConnection, &QServerConnection::appendSystemMessage, chatWidget, &QChatWidget::appendSystemMessage);
connect(serverConnection, &QServerConnection::addMessageToChatbox, chatWidget, &QChatWidget::appendUserMessage);
connect(serverConnection, &QServerConnection::appendServerMessage, chatWidget, &QChatWidget::appendServerMessage);
connect(chatWidget, &QChatWidget::sendMessage, serverConnection, &QServerConnection::sendNewChatMessage);

connect(serverConnection, &QServerConnection::clearChatbox, chatbox, &QChatbox::clearChat);
connect(serverConnection, &QServerConnection::appendSystemMessage, chatbox, &QChatbox::appendSystemMessage);
connect(serverConnection, &QServerConnection::addMessageToChatbox, chatbox, &QChatbox::appendUserMessage);
connect(serverConnection, &QServerConnection::appendServerMessage, chatbox, &QChatbox::appendServerMessage);

connect(serverConnection, &QServerConnection::newClient, participantsPanel, &QParticipantsPanel::addParticipant);
connect(serverConnection, &QServerConnection::serverDisconnected, participantsPanel, &QParticipantsPanel::clear);
connect(serverConnection, &QServerConnection::clientChangedUsername, this, [=](const QString& newUsername) {
userLabel->setText("Your name: " + newUsername + '@' + serverConnection->getComputerName());
});
connect(serverConnection, &QServerConnection::clientChangedComputerName, this, [=](const QString& newComputerName) {
userLabel->setText("Your name: " + serverConnection->getUsername() + '@' + newComputerName);
});
connect(serverConnection, &QServerConnection::otherClientChangedUsername, participantsPanel, &QParticipantsPanel::otherClientChangedUsername);
connect(serverConnection, &QServerConnection::otherClientChangedComputerName, participantsPanel, &QParticipantsPanel::otherClientChangedComputerName);


connect(serverConnection, &QServerConnection::removeClient, participantsPanel, &QParticipantsPanel::removeParticipant);

resize(650, 350);
setCentralWidget(centralWidget);
setWindowTitle("Chatlis");
setWindowIcon(QIcon("group-chat.png"));
}

void QChatRoomMainWindow::tryConnectToServer()
void QChatRoomMainWindow::actionConnectToServer()
{
QString serverAddress(QInputDialog::getText(this,
tr("Chatlis server connection"),
tr("Server address (ip:port)")));
QString serverAddress(QInputDialog::getText(this, tr("Chatlis server connection"), tr("Server address (ip:port)")));
if (!serverAddress.isEmpty())
{
QStringList ipAndPort = serverAddress.split(':');
if (ipAndPort.size() == 2 && !ipAndPort[0].isEmpty() && !ipAndPort[1].isEmpty())
serverConnection->connectToServer(ipAndPort[0], ipAndPort[1]);
{
chatWidget->clearMessages();
serverConnection->connectToServer(ipAndPort[0], ipAndPort[1], client.getUsername(), client.getComputerName());
}
else
QMessageBox::critical(this, "Wrong format", "Address and port of Chatlis server should be XXX.XXX.XXX.XXX:PPPPP");
}
}

void QChatRoomMainWindow::tryDisconnectFromServer()
void QChatRoomMainWindow::actionDisconnectFromServer()
{
serverConnection->disconnectFromHost();
}

void QChatRoomMainWindow::tryChangeUsername()
void QChatRoomMainWindow::actionChangeUsername()
{
QString newUsername(QInputDialog::getText(this,
tr("Change username"),
tr("Username to display")));
QString newUsername(QInputDialog::getText(this, tr("Change username"), tr("Username to display")));
if (!newUsername.isEmpty())
serverConnection->changeUserName(newUsername);
{
if (newUsername.size() > 20)
QMessageBox::critical(this, "Username too long", "Displayed username can't be longer than 20");
else
{
client.setUsername(newUsername);
serverConnection->changeUserName(newUsername);
userDisplayName->setText(newUsername + '@' + client.getComputerName());
}
}
}

void QChatRoomMainWindow::tryChangeComputerName()
void QChatRoomMainWindow::actionChangeComputerName()
{
QString newComputerName(QInputDialog::getText(this,
tr("Change computer name"),
tr("Computer name to display")));
QString newComputerName(QInputDialog::getText(this, tr("Change computer name"), tr("Computer name to display")));
if (!newComputerName.isEmpty())
serverConnection->changeComputerName(newComputerName);
{
if (newComputerName.size() > 20)
QMessageBox::critical(this, "Computer name too long", "Displayed computer name can't be longer than 20");
else
{
client.setComputerName(newComputerName);
serverConnection->changeComputerName(newComputerName);
userDisplayName->setText(client.getUsername() + '@' + newComputerName);
}
}
}

QWidget* QChatRoomMainWindow::initParticipantsWidget()
{
QWidget* usersWidget{ new QWidget };
QSizePolicy modifiedPolicy{ usersWidget->sizePolicy() };
modifiedPolicy.setHorizontalStretch(1);
usersWidget->setSizePolicy(modifiedPolicy);
QVBoxLayout* usersLayout{ new QVBoxLayout };

QGroupBox* userDisplayNameGroupBox{ new QGroupBox("Your name") };
QVBoxLayout* userDisplayNameLayout{ new QVBoxLayout };
userDisplayName = new QLabel(client.getUsername() + '@' + client.getComputerName());
userDisplayNameLayout->addWidget(userDisplayName);
userDisplayNameGroupBox->setLayout(userDisplayNameLayout);

QGroupBox* participantsGroupBox{ new QGroupBox("Participants") };
QVBoxLayout* participantsLayout{ new QVBoxLayout };
participantsPanel = new QParticipantsPanel;
participantsLayout->addWidget(participantsPanel);
participantsGroupBox->setLayout(participantsLayout);

usersLayout->addWidget(userDisplayNameGroupBox);
usersLayout->addWidget(participantsGroupBox);
usersWidget->setLayout(usersLayout);
return usersWidget;
}

QChatRoomMainWindow::~QChatRoomMainWindow()
{
delete serverConnection;
}
}
19 changes: 14 additions & 5 deletions Client/QChatRoomMainWindow.h
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
#pragma once

#include <QMainWindow>
#include "QChatbox.h"
#include "QChatWidget.h"
#include <QMenuBar>
#include <QMenu>
#include "QServerConnection.h"
#include "QParticipantsPanel.h"
#include <QLabel>
#include <QVBoxLayout>

class QChatRoomMainWindow : public QMainWindow
{
Expand All @@ -15,11 +18,17 @@ class QChatRoomMainWindow : public QMainWindow
~QChatRoomMainWindow();

private slots:
void tryConnectToServer();
void tryDisconnectFromServer();
void tryChangeUsername();
void tryChangeComputerName();
void actionConnectToServer();
void actionDisconnectFromServer();
void actionChangeUsername();
void actionChangeComputerName();

private:
QClientInfo client;

QServerConnection* serverConnection;
QLabel* userDisplayName;
QParticipantsPanel* participantsPanel;
QChatWidget* chatWidget;
QWidget* initParticipantsWidget();
};
55 changes: 55 additions & 0 deletions Client/QChatWidget.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#include "QChatWidget.h"
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QPushButton>
#include <QGroupBox>
#include <QLineEdit>

QChatWidget::QChatWidget(QWidget *parent, const QClientInfo& clientInfo)
: QWidget(parent), chatBox{ new QChatbox(this) }, textInput{ new QLineEdit(this) }, clientInfo(clientInfo)
{
QSizePolicy modifiedPolicy{ sizePolicy() };
modifiedPolicy.setHorizontalStretch(2);
setSizePolicy(modifiedPolicy);

QVBoxLayout* mainLayout{ new QVBoxLayout };

QGroupBox* chatBoxGroupBox{ new QGroupBox("Chat") };
QVBoxLayout* chatBoxGroupBoxLayout{ new QVBoxLayout };
chatBoxGroupBoxLayout->addWidget(chatBox);
chatBoxGroupBox->setLayout(chatBoxGroupBoxLayout);

QHBoxLayout* textInputLayout{ new QHBoxLayout };
QPushButton* sendMessageButton{ new QPushButton("Send", this) };
textInputLayout->addWidget(textInput);
textInputLayout->addWidget(sendMessageButton);

mainLayout->addWidget(chatBoxGroupBox);
mainLayout->addLayout(textInputLayout);
setLayout(mainLayout);

connect(this, &QChatWidget::appendSystemMessage, chatBox, &QChatbox::appendSystemMessage);
connect(this, &QChatWidget::appendUserMessage, chatBox, &QChatbox::appendUserMessage);
connect(this, &QChatWidget::appendServerMessage, chatBox, &QChatbox::appendServerMessage);

connect(sendMessageButton, &QPushButton::clicked, textInput, &QLineEdit::returnPressed);
connect(textInput, &QLineEdit::returnPressed, this, &QChatWidget::handleReturnPressed);
}

void QChatWidget::handleReturnPressed()
{
QString currentText(textInput->text().simplified());
if (!currentText.isEmpty())
{
appendUserMessage(clientInfo.getUsername(), currentText);
emit sendMessage(currentText);
}
textInput->clear();
}

void QChatWidget::clearMessages()
{
chatBox->clear();
}

QChatWidget::~QChatWidget() {}
31 changes: 31 additions & 0 deletions Client/QChatWidget.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#pragma once

#include <QWidget>
#include "QChatbox.h"
#include <QLineEdit>
#include "../QClientInfo.h"

class QChatWidget : public QWidget
{
Q_OBJECT

public:
QChatWidget(QWidget* parent, const QClientInfo& clientInfo);
~QChatWidget();

void clearMessages();

signals:
void appendUserMessage(const QString& from, const QString& message);
void appendSystemMessage(const QString message);
void appendServerMessage(const QString message);
void sendMessage(const QString& message);

private slots:
void handleReturnPressed();

private:
QChatbox* chatBox;
QLineEdit* textInput;
const QClientInfo& clientInfo;
};
9 changes: 0 additions & 9 deletions Client/QChatbox.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,6 @@ const QColor QChatbox::SYSTEM_MESSAGE_TEXT_COLOR{ Qt::red };
QChatbox::QChatbox(QWidget *parent)
: QTextEdit(parent)
{
QSizePolicy modifiedPolicy{ sizePolicy() };
modifiedPolicy.setHorizontalStretch(4);
setSizePolicy(modifiedPolicy);

setFocusPolicy(Qt::NoFocus);
setReadOnly(true);
}
Expand Down Expand Up @@ -54,9 +50,4 @@ void QChatbox::appendUserMessage(const QString& from, const QString& message)
bar->setValue(bar->maximum());
}

void QChatbox::clearChat()
{
clear();
}

QChatbox::~QChatbox() {}
4 changes: 1 addition & 3 deletions Client/QChatbox.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,8 @@ class QChatbox : public QTextEdit
QChatbox(QWidget *parent = nullptr);
~QChatbox();

void appendUserMessage(const QString& from, const QString& message);

public slots:
void clearChat();
void appendUserMessage(const QString& from, const QString& message);
void appendSystemMessage(const QString message);
void appendServerMessage(const QString message);

Expand Down
4 changes: 0 additions & 4 deletions Client/QParticipantsPanel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,6 @@
QParticipantsPanel::QParticipantsPanel(QWidget* parent)
: QListView(parent), model{}
{
QSizePolicy modifiedPolicy{ sizePolicy() };
modifiedPolicy.setHorizontalStretch(1);
setSizePolicy(modifiedPolicy);

model = new QStringListModel(this);
setModel(model);
setEditTriggers(QAbstractItemView::NoEditTriggers);
Expand Down
Loading

0 comments on commit 267ee79

Please sign in to comment.