mirror of
https://gitea.msk.dinamika-avia.ru/Constanta-Design/RRJServer.git
synced 2026-03-27 19:45:43 +03:00
refact1
This commit is contained in:
168
LibInstructorsAndTrainees/messanger/messangercontroller.cpp
Normal file
168
LibInstructorsAndTrainees/messanger/messangercontroller.cpp
Normal file
@@ -0,0 +1,168 @@
|
||||
#include "messangercontroller.h"
|
||||
|
||||
MessangerController::MessangerController(ConnectorToServer* connectorToServer, QObject *parent) :
|
||||
QObject(parent),
|
||||
connectorToServer(connectorToServer),
|
||||
userLocalGUI_ID(0)
|
||||
{
|
||||
connect(connectorToServer, &ConnectorToServer::signal_receiveMessage, this, &MessangerController::slot_receiveMessage);
|
||||
}
|
||||
|
||||
MessangerController::~MessangerController()
|
||||
{
|
||||
deleteAllWidgets();
|
||||
}
|
||||
|
||||
MessangerWidget *MessangerController::newWidget(QWidget *parent, User* user, QVBoxLayout* boxLayout)
|
||||
{
|
||||
MessangerWidget *msgWdgt = new MessangerWidget(parent);
|
||||
|
||||
if(boxLayout)
|
||||
boxLayout->addWidget(msgWdgt);
|
||||
|
||||
connect(msgWdgt, &MessangerWidget::signal_sendMessage, this, &MessangerController::slot_sendMessage);
|
||||
|
||||
msgWdgt->initUserLocalGUI(connectorToServer->getInstructor(userLocalGUI_ID));
|
||||
|
||||
if(user)
|
||||
{
|
||||
msgWdgt->initUserRemote(*user);
|
||||
|
||||
//Заполнение предыстории
|
||||
int id_user = user->getID();
|
||||
QList<MessageOfMessanger>* list = nullptr;
|
||||
if(mapAlldialogsWithClients.contains(id_user))
|
||||
{//Уже есть
|
||||
list = mapAlldialogsWithClients.value(id_user);
|
||||
|
||||
for(MessageOfMessanger msg : *list)
|
||||
{
|
||||
if(msg.flLocal)
|
||||
msgWdgt->showSendedMessage(ClientMessage(QString::number(userLocalGUI_ID), QString::number(id_user), msg.text, msg.time.toString("hh:mm")));
|
||||
else
|
||||
msgWdgt->showReceivedMessage(ClientMessage(QString::number(id_user), QString::number(userLocalGUI_ID), msg.text, msg.time.toString("hh:mm")));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
listWidgets.append(msgWdgt);
|
||||
|
||||
return msgWdgt;
|
||||
}
|
||||
|
||||
void MessangerController::deleteWidget(MessangerWidget *msgWdgt)
|
||||
{
|
||||
for(int i = 0; i < listWidgets.count(); i++)
|
||||
{
|
||||
if(listWidgets.at(i) == msgWdgt)
|
||||
{
|
||||
delete msgWdgt;
|
||||
listWidgets.removeAt(i);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void MessangerController::deleteAllWidgets()
|
||||
{
|
||||
for(MessangerWidget* widget : listWidgets)
|
||||
delete widget;
|
||||
listWidgets.clear();
|
||||
|
||||
mapAlldialogsWithClients.clear();
|
||||
}
|
||||
|
||||
void MessangerController::setUserLocalGUI_ID(int id)
|
||||
{
|
||||
userLocalGUI_ID = id;
|
||||
|
||||
for(MessangerWidget* widget : listWidgets)
|
||||
{
|
||||
widget->initUserLocalGUI(connectorToServer->getInstructor(userLocalGUI_ID));
|
||||
}
|
||||
}
|
||||
|
||||
void MessangerController::slot_sendMessage(ClientMessage clientMessage)
|
||||
{
|
||||
connectorToServer->slot_sendMessage(clientMessage);
|
||||
|
||||
showMessageInAllWidgets(clientMessage);
|
||||
}
|
||||
|
||||
void MessangerController::slot_receiveMessage(ClientMessage clientMessage)
|
||||
{
|
||||
int id_user = clientMessage.fromId.toInt();
|
||||
|
||||
Instructor instructor = connectorToServer->getInstructor(id_user);
|
||||
if(instructor.getID())
|
||||
emit signal_receiveMessage_fromInstructor(clientMessage);
|
||||
else
|
||||
{
|
||||
Trainee trainee = connectorToServer->getTrainee(id_user);
|
||||
if(trainee.getID())
|
||||
emit signal_receiveMessage_fromTrainee(clientMessage);
|
||||
}
|
||||
|
||||
showMessageInAllWidgets(clientMessage);
|
||||
}
|
||||
|
||||
void MessangerController::slot_reinitMessangers(QList<User> listUsers)
|
||||
{
|
||||
for(MessangerWidget* widget : listWidgets)
|
||||
{
|
||||
for(User user : listUsers)
|
||||
{
|
||||
if(user.getID() == widget->getIDuserRemote())
|
||||
{
|
||||
widget->reinitMessangerWidget(user);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void MessangerController::showMessageInAllWidgets(ClientMessage clientMessage)
|
||||
{
|
||||
int id_user = 0;
|
||||
MessageOfMessanger msg;
|
||||
|
||||
if(clientMessage.fromId == QString::number(userLocalGUI_ID))
|
||||
{//Send
|
||||
id_user = clientMessage.toId.toInt();
|
||||
msg.flLocal = true;
|
||||
}
|
||||
else
|
||||
{//Receive
|
||||
id_user = clientMessage.fromId.toInt();
|
||||
msg.flLocal = false;
|
||||
}
|
||||
|
||||
|
||||
QList<MessageOfMessanger>* list = nullptr;
|
||||
if(mapAlldialogsWithClients.contains(id_user))
|
||||
{//Уже есть
|
||||
list = mapAlldialogsWithClients.value(id_user);
|
||||
}
|
||||
else
|
||||
{//Новый
|
||||
list = new QList<MessageOfMessanger>();
|
||||
mapAlldialogsWithClients.insert(id_user, list);
|
||||
}
|
||||
|
||||
msg.text = clientMessage.Text;
|
||||
msg.time = QTime::currentTime();
|
||||
list->append(msg);
|
||||
|
||||
clientMessage.timeStr = msg.time.toString("hh:mm");
|
||||
|
||||
for(MessangerWidget* widget : listWidgets)
|
||||
{
|
||||
if(widget->getIDuserRemote() == id_user)
|
||||
{
|
||||
if(msg.flLocal)
|
||||
widget->showSendedMessage(clientMessage);
|
||||
else
|
||||
widget->showReceivedMessage(clientMessage);
|
||||
}
|
||||
}
|
||||
}
|
||||
52
LibInstructorsAndTrainees/messanger/messangercontroller.h
Normal file
52
LibInstructorsAndTrainees/messanger/messangercontroller.h
Normal file
@@ -0,0 +1,52 @@
|
||||
#ifndef MESSANGERCONTROLLER_H
|
||||
#define MESSANGERCONTROLLER_H
|
||||
|
||||
#include <QObject>
|
||||
#include <QVBoxLayout>
|
||||
#include "messangerwidget.h"
|
||||
#include "connectortoserver.h"
|
||||
|
||||
struct MessageOfMessanger
|
||||
{
|
||||
QString text;
|
||||
bool flLocal;
|
||||
QTime time;
|
||||
};
|
||||
|
||||
class MessangerController : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
explicit MessangerController(ConnectorToServer* connectorToServer, QObject *parent = nullptr);
|
||||
~MessangerController();
|
||||
|
||||
public:
|
||||
MessangerWidget* newWidget(QWidget *parent, User* user, QVBoxLayout* boxLayout);
|
||||
void deleteWidget(MessangerWidget* msgWdgt);
|
||||
void deleteAllWidgets();
|
||||
|
||||
void setUserLocalGUI_ID(int id);
|
||||
|
||||
public slots:
|
||||
void slot_sendMessage(ClientMessage clientMessage);
|
||||
void slot_receiveMessage(ClientMessage clientMessage);
|
||||
|
||||
void slot_reinitMessangers(QList<User> listUsers);
|
||||
|
||||
signals:
|
||||
void signal_receiveMessage_fromInstructor(ClientMessage clientMessage);
|
||||
void signal_receiveMessage_fromTrainee(ClientMessage clientMessage);
|
||||
|
||||
private:
|
||||
void showMessageInAllWidgets(ClientMessage clientMessage);
|
||||
|
||||
private:
|
||||
ConnectorToServer* connectorToServer;
|
||||
QList <MessangerWidget*> listWidgets;
|
||||
|
||||
int userLocalGUI_ID;
|
||||
|
||||
QMap <int, QList<MessageOfMessanger>*> mapAlldialogsWithClients;
|
||||
};
|
||||
|
||||
#endif // MESSANGERCONTROLLER_H
|
||||
172
LibInstructorsAndTrainees/messanger/messangerwidget.cpp
Normal file
172
LibInstructorsAndTrainees/messanger/messangerwidget.cpp
Normal file
@@ -0,0 +1,172 @@
|
||||
#include <QListWidget>
|
||||
#include <QKeyEvent>
|
||||
#include <QAbstractScrollArea>
|
||||
#include <QMessageBox>
|
||||
#include <QLabel>
|
||||
#include "instructor.h"
|
||||
#include "messangerwidget.h"
|
||||
#include "ui_messangerwidget.h"
|
||||
|
||||
|
||||
MessangerWidget::MessangerWidget(QWidget *parent) :
|
||||
QWidget(parent),
|
||||
oneDialogMessenger(nullptr),
|
||||
ui(new Ui::MessangerWidget),
|
||||
myTextEdit(nullptr)
|
||||
{
|
||||
ui->setupUi(this);
|
||||
|
||||
ui->btnSend->setObjectName("btnSend");
|
||||
|
||||
ui->lblUser->setObjectName("MessangerWidgetLblUser");
|
||||
ui->lblAvatar->setObjectName("MessangerWidgetLblAvatar");
|
||||
ui->lblLoggedIn->setObjectName("MessangerWidgetLblLoggedIn");
|
||||
|
||||
myTextEdit = new MyTextEdit();
|
||||
connect(myTextEdit, &MyTextEdit::signal_pressEnter, this, &MessangerWidget::on_btnSend_clicked);
|
||||
|
||||
myTextEdit->setObjectName("editMsg");
|
||||
myTextEdit->setSizePolicy(QSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed));
|
||||
myTextEdit->setFixedHeight(65);
|
||||
|
||||
ui->horizontalLayout_EditMsg->addWidget(myTextEdit);
|
||||
}
|
||||
|
||||
MessangerWidget::~MessangerWidget()
|
||||
{
|
||||
delete oneDialogMessenger;
|
||||
delete ui;
|
||||
}
|
||||
|
||||
void MessangerWidget::initUserRemote(User user)
|
||||
{
|
||||
userRemote = user;
|
||||
|
||||
addTabDialogMessenger(userRemote);
|
||||
}
|
||||
|
||||
void MessangerWidget::reinitMessangerWidget(User user)
|
||||
{
|
||||
if(userRemote.getID() == user.getID())
|
||||
{
|
||||
userRemote = user;
|
||||
|
||||
//Обновляем заголовок
|
||||
updateHeader(userRemote);
|
||||
}
|
||||
}
|
||||
|
||||
void MessangerWidget::initUserLocalGUI(User user)
|
||||
{
|
||||
userLocalGUI = user;
|
||||
}
|
||||
|
||||
int MessangerWidget::getIDuserRemote()
|
||||
{
|
||||
return userRemote.getID();
|
||||
}
|
||||
|
||||
void MessangerWidget::showSendedMessage(ClientMessage clientMessage)
|
||||
{
|
||||
if(userLocalGUI.getID() == clientMessage.fromId.toInt())
|
||||
oneDialogMessenger->addMsgWidgetLocal(clientMessage.Text, clientMessage.timeStr, userLocalGUI.getName());
|
||||
}
|
||||
|
||||
void MessangerWidget::showReceivedMessage(ClientMessage clientMessage)
|
||||
{
|
||||
if(userRemote.getID() == clientMessage.fromId.toInt())
|
||||
oneDialogMessenger->addMsgWidgetRemote(clientMessage.Text, clientMessage.timeStr, userRemote.getName());
|
||||
}
|
||||
|
||||
void MessangerWidget::addTabDialogMessenger(User user)
|
||||
{
|
||||
if(!oneDialogMessenger)
|
||||
{//Диалога еще не существует
|
||||
|
||||
oneDialogMessenger = new OneDialogMessenger(userLocalGUI, userRemote, this);
|
||||
|
||||
ui->verticalLayout_ListMsg->addWidget(oneDialogMessenger);
|
||||
|
||||
//Обновляем заголовок
|
||||
updateHeader(user);
|
||||
}
|
||||
else
|
||||
{//Диалог уже существует
|
||||
|
||||
//Проверяем наличие диалога именно с этим клиентом
|
||||
if(oneDialogMessenger->getUserRemoteId() == user.getID())
|
||||
{
|
||||
//Обновляем заголовок
|
||||
updateHeader(user);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void MessangerWidget::updateHeader(User user)
|
||||
{
|
||||
//Обновляем статус залогинивания
|
||||
if(user.getLoggedIn())
|
||||
ui->lblLoggedIn->setPixmap(QPixmap(QStringLiteral(":/resources/icons/circleGreen.png")));
|
||||
else
|
||||
ui->lblLoggedIn->setPixmap(QPixmap(QStringLiteral(":/resources/icons/circleGray.png")));
|
||||
|
||||
|
||||
QString typeUserStr = "";
|
||||
|
||||
if(user.getTypeUserDB() == User::TypeUserDBTrainee)
|
||||
{
|
||||
ui->lblAvatar->setPixmap(QPixmap(QStringLiteral(":/resources/icons/trainee.png")));
|
||||
|
||||
typeUserStr = tr("Trainee");
|
||||
}
|
||||
else
|
||||
{
|
||||
if(static_cast<Instructor*>(&user)->getIsAdmin())
|
||||
ui->lblAvatar->setPixmap(QPixmap(QStringLiteral(":/resources/icons/admin.png")));
|
||||
else
|
||||
ui->lblAvatar->setPixmap(QPixmap(QStringLiteral(":/resources/icons/instructor.png")));
|
||||
|
||||
typeUserStr = tr("Instructor");
|
||||
}
|
||||
|
||||
QString fullName = typeUserStr + ": " + user.getName() + " (" + user.getLogin() + ")";
|
||||
ui->lblUser->setText(fullName);
|
||||
}
|
||||
|
||||
void MessangerWidget::on_btnSend_clicked()
|
||||
{
|
||||
QString text = myTextEdit->toPlainText();
|
||||
QString from = QString::number(userLocalGUI.getID());
|
||||
QString to = QString::number(userRemote.getID());
|
||||
|
||||
ClientMessage message = ClientMessage(from, to, text);
|
||||
emit signal_sendMessage(message);
|
||||
|
||||
myTextEdit->clear();
|
||||
}
|
||||
|
||||
void MessangerWidget::changeEvent(QEvent *event)
|
||||
{
|
||||
// В случае получения события изменения языка приложения
|
||||
if (event->type() == QEvent::LanguageChange)
|
||||
{
|
||||
ui->retranslateUi(this); // переведём окно заново
|
||||
}
|
||||
}
|
||||
|
||||
void MyTextEdit::keyPressEvent(QKeyEvent *event)
|
||||
{
|
||||
if (event->key() == Qt::Key_Return || event->key() == Qt::Key_Enter)
|
||||
{
|
||||
if(QApplication::keyboardModifiers().testFlag(Qt::ControlModifier))
|
||||
{
|
||||
this->insertPlainText("\n");
|
||||
}
|
||||
else
|
||||
{
|
||||
emit signal_pressEnter();
|
||||
return;
|
||||
}
|
||||
}
|
||||
QTextEdit::keyPressEvent(event);
|
||||
}
|
||||
73
LibInstructorsAndTrainees/messanger/messangerwidget.h
Normal file
73
LibInstructorsAndTrainees/messanger/messangerwidget.h
Normal file
@@ -0,0 +1,73 @@
|
||||
#ifndef MESSANGERWIDGET_H
|
||||
#define MESSANGERWIDGET_H
|
||||
|
||||
#include <QWidget>
|
||||
#include <QTranslator>
|
||||
#include <user.h>
|
||||
#include "Datas.h"
|
||||
#include "tabdialogmessenger.h"
|
||||
#include <QTextEdit>
|
||||
|
||||
class MyTextEdit : public QTextEdit
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
MyTextEdit(){};
|
||||
|
||||
signals:
|
||||
void signal_pressEnter();
|
||||
|
||||
private:
|
||||
virtual void keyPressEvent(QKeyEvent *event) override;
|
||||
};
|
||||
|
||||
namespace Ui {
|
||||
class MessangerWidget;
|
||||
}
|
||||
|
||||
class MessangerWidget : public QWidget
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
explicit MessangerWidget(QWidget *parent = nullptr);
|
||||
~MessangerWidget();
|
||||
|
||||
public:
|
||||
void initUserRemote(User user);
|
||||
void reinitMessangerWidget(User user);
|
||||
void initUserLocalGUI(User user);
|
||||
int getIDuserRemote();
|
||||
|
||||
void showSendedMessage(ClientMessage clientMessage);
|
||||
void showReceivedMessage(ClientMessage clientMessage);
|
||||
|
||||
private:
|
||||
void addTabDialogMessenger(User user);
|
||||
void updateHeader(User user);
|
||||
|
||||
private slots:
|
||||
void on_btnSend_clicked();
|
||||
|
||||
signals:
|
||||
//сигнал о готовности нового сообщения на отправку клиенту
|
||||
void signal_sendMessage(ClientMessage clientMessage);
|
||||
|
||||
protected:
|
||||
void changeEvent(QEvent * event) override;
|
||||
|
||||
private:
|
||||
OneDialogMessenger* oneDialogMessenger;
|
||||
User userLocalGUI;
|
||||
User userRemote;
|
||||
|
||||
QTranslator qtLanguageTranslator;
|
||||
|
||||
private:
|
||||
Ui::MessangerWidget *ui;
|
||||
|
||||
MyTextEdit* myTextEdit;
|
||||
};
|
||||
|
||||
#endif // MESSANGERWIDGET_H
|
||||
155
LibInstructorsAndTrainees/messanger/messangerwidget.ui
Normal file
155
LibInstructorsAndTrainees/messanger/messangerwidget.ui
Normal file
@@ -0,0 +1,155 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<ui version="4.0">
|
||||
<class>MessangerWidget</class>
|
||||
<widget class="QWidget" name="MessangerWidget">
|
||||
<property name="geometry">
|
||||
<rect>
|
||||
<x>0</x>
|
||||
<y>0</y>
|
||||
<width>344</width>
|
||||
<height>300</height>
|
||||
</rect>
|
||||
</property>
|
||||
<property name="font">
|
||||
<font>
|
||||
<family>Tahoma</family>
|
||||
<pointsize>10</pointsize>
|
||||
</font>
|
||||
</property>
|
||||
<property name="windowTitle">
|
||||
<string>Form</string>
|
||||
</property>
|
||||
<layout class="QGridLayout" name="gridLayout_2">
|
||||
<item row="0" column="0">
|
||||
<layout class="QVBoxLayout" name="verticalLayout_Main">
|
||||
<item>
|
||||
<layout class="QVBoxLayout" name="verticalLayout_Dialog" stretch="0,0">
|
||||
<property name="sizeConstraint">
|
||||
<enum>QLayout::SetDefaultConstraint</enum>
|
||||
</property>
|
||||
<item>
|
||||
<layout class="QHBoxLayout" name="horizontalLayout_User">
|
||||
<item>
|
||||
<widget class="QLabel" name="lblAvatar">
|
||||
<property name="sizePolicy">
|
||||
<sizepolicy hsizetype="Fixed" vsizetype="Fixed">
|
||||
<horstretch>0</horstretch>
|
||||
<verstretch>0</verstretch>
|
||||
</sizepolicy>
|
||||
</property>
|
||||
<property name="minimumSize">
|
||||
<size>
|
||||
<width>32</width>
|
||||
<height>32</height>
|
||||
</size>
|
||||
</property>
|
||||
<property name="maximumSize">
|
||||
<size>
|
||||
<width>32</width>
|
||||
<height>32</height>
|
||||
</size>
|
||||
</property>
|
||||
<property name="text">
|
||||
<string>TextLabel</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item>
|
||||
<widget class="QLabel" name="lblUser">
|
||||
<property name="text">
|
||||
<string>TextLabel</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item>
|
||||
<widget class="QLabel" name="lblLoggedIn">
|
||||
<property name="sizePolicy">
|
||||
<sizepolicy hsizetype="Fixed" vsizetype="Fixed">
|
||||
<horstretch>0</horstretch>
|
||||
<verstretch>0</verstretch>
|
||||
</sizepolicy>
|
||||
</property>
|
||||
<property name="minimumSize">
|
||||
<size>
|
||||
<width>32</width>
|
||||
<height>32</height>
|
||||
</size>
|
||||
</property>
|
||||
<property name="maximumSize">
|
||||
<size>
|
||||
<width>32</width>
|
||||
<height>32</height>
|
||||
</size>
|
||||
</property>
|
||||
<property name="text">
|
||||
<string/>
|
||||
</property>
|
||||
<property name="pixmap">
|
||||
<pixmap resource="../../ServerLMS/ServerLMS.qrc">:/resources/icons/circleGray.png</pixmap>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
</layout>
|
||||
</item>
|
||||
<item>
|
||||
<layout class="QVBoxLayout" name="verticalLayout_ListMsg"/>
|
||||
</item>
|
||||
</layout>
|
||||
</item>
|
||||
<item>
|
||||
<layout class="QHBoxLayout" name="horizontalLayout_Input">
|
||||
<property name="sizeConstraint">
|
||||
<enum>QLayout::SetDefaultConstraint</enum>
|
||||
</property>
|
||||
<item>
|
||||
<layout class="QHBoxLayout" name="horizontalLayout_EditMsg"/>
|
||||
</item>
|
||||
<item>
|
||||
<widget class="QToolButton" name="btnSend">
|
||||
<property name="sizePolicy">
|
||||
<sizepolicy hsizetype="Preferred" vsizetype="Preferred">
|
||||
<horstretch>0</horstretch>
|
||||
<verstretch>0</verstretch>
|
||||
</sizepolicy>
|
||||
</property>
|
||||
<property name="minimumSize">
|
||||
<size>
|
||||
<width>65</width>
|
||||
<height>54</height>
|
||||
</size>
|
||||
</property>
|
||||
<property name="maximumSize">
|
||||
<size>
|
||||
<width>1000</width>
|
||||
<height>1000</height>
|
||||
</size>
|
||||
</property>
|
||||
<property name="text">
|
||||
<string>Send</string>
|
||||
</property>
|
||||
<property name="icon">
|
||||
<iconset>
|
||||
<normaloff>:/resources/icons/sendMsg.png</normaloff>:/resources/icons/sendMsg.png</iconset>
|
||||
</property>
|
||||
<property name="iconSize">
|
||||
<size>
|
||||
<width>32</width>
|
||||
<height>32</height>
|
||||
</size>
|
||||
</property>
|
||||
<property name="toolButtonStyle">
|
||||
<enum>Qt::ToolButtonTextUnderIcon</enum>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
</layout>
|
||||
</item>
|
||||
</layout>
|
||||
</item>
|
||||
</layout>
|
||||
</widget>
|
||||
<resources>
|
||||
<include location="../../ServerLMS/ServerLMS.qrc"/>
|
||||
</resources>
|
||||
<connections/>
|
||||
</ui>
|
||||
143
LibInstructorsAndTrainees/messanger/msgwidget.cpp
Normal file
143
LibInstructorsAndTrainees/messanger/msgwidget.cpp
Normal file
@@ -0,0 +1,143 @@
|
||||
#include "msgwidget.h"
|
||||
#include "ui_msgwidget.h"
|
||||
|
||||
MsgWidget::MsgWidget(QString avatar, AligneAvatar aligneAvatar, int width, QWidget *parent) :
|
||||
QWidget(parent),
|
||||
ui(new Ui::MsgWidget),
|
||||
widthEdit(100)
|
||||
{
|
||||
ui->setupUi(this);
|
||||
|
||||
setAvatar(avatar);
|
||||
|
||||
if(aligneAvatar == AligneAvatar::Left)
|
||||
setAligneAvatarLeft();
|
||||
else
|
||||
setAligneAvatarRight();
|
||||
|
||||
setWidth(width);
|
||||
|
||||
ui->lblAvatar->setObjectName("MsgWidgetLblAvatar");
|
||||
}
|
||||
|
||||
MsgWidget::~MsgWidget()
|
||||
{
|
||||
delete ui;
|
||||
}
|
||||
|
||||
void MsgWidget::setAligneAvatarLeft()
|
||||
{
|
||||
ui->horizontalLayout_Main->removeItem(ui->verticalLayout_Avatar);
|
||||
ui->horizontalLayout_Main->removeItem(ui->verticalLayout_Text);
|
||||
ui->horizontalLayout_Main->removeItem(ui->horizontalLayout_Add);
|
||||
ui->horizontalLayout_EditText->removeItem(ui->horizontalSpacer_EditText);
|
||||
ui->horizontalLayout_EditText->removeWidget(ui->editText);
|
||||
|
||||
ui->horizontalLayout_Main->addLayout(ui->verticalLayout_Avatar);
|
||||
ui->horizontalLayout_Main->addLayout(ui->verticalLayout_Text);
|
||||
ui->horizontalLayout_Main->addLayout(ui->horizontalLayout_Add);
|
||||
|
||||
ui->horizontalLayout_EditText->addWidget(ui->editText);
|
||||
ui->horizontalLayout_EditText->addItem(ui->horizontalSpacer_EditText);
|
||||
|
||||
ui->editText->setObjectName("MsgWidgetEditTextRemote");
|
||||
ui->lblTime->setObjectName("MsgWidgetLblTimeRemote");
|
||||
ui->lblName->setObjectName("MsgWidgetLblNameRemote");
|
||||
|
||||
ui->lblTime->setAlignment(Qt::AlignLeft);
|
||||
ui->lblName->setAlignment(Qt::AlignLeft | Qt::AlignBottom);
|
||||
//ui->editText->setAlignment(Qt::AlignLeft);
|
||||
}
|
||||
|
||||
void MsgWidget::setAligneAvatarRight()
|
||||
{
|
||||
ui->horizontalLayout_Main->removeItem(ui->verticalLayout_Avatar);
|
||||
ui->horizontalLayout_Main->removeItem(ui->verticalLayout_Text);
|
||||
ui->horizontalLayout_Main->removeItem(ui->horizontalLayout_Add);
|
||||
ui->horizontalLayout_EditText->removeItem(ui->horizontalSpacer_EditText);
|
||||
ui->horizontalLayout_EditText->removeWidget(ui->editText);
|
||||
|
||||
ui->horizontalLayout_Main->addLayout(ui->horizontalLayout_Add);
|
||||
ui->horizontalLayout_Main->addLayout(ui->verticalLayout_Text);
|
||||
ui->horizontalLayout_Main->addLayout(ui->verticalLayout_Avatar);
|
||||
|
||||
ui->horizontalLayout_EditText->addItem(ui->horizontalSpacer_EditText);
|
||||
ui->horizontalLayout_EditText->addWidget(ui->editText);
|
||||
|
||||
ui->editText->setObjectName("MsgWidgetEditTextLocal");
|
||||
ui->lblTime->setObjectName("MsgWidgetLblTimeLocal");
|
||||
ui->lblName->setObjectName("MsgWidgetLblNameLocal");
|
||||
|
||||
ui->lblTime->setAlignment(Qt::AlignRight);
|
||||
ui->lblName->setAlignment(Qt::AlignRight | Qt::AlignBottom);
|
||||
//ui->editText->setAlignment(Qt::AlignRight);
|
||||
}
|
||||
|
||||
|
||||
void MsgWidget::setAvatar(QString avatar)
|
||||
{
|
||||
QPixmap pix(avatar);
|
||||
ui->lblAvatar->setPixmap( pix.scaled(32,32) );
|
||||
}
|
||||
|
||||
void MsgWidget::setWidth(int width)
|
||||
{
|
||||
this->setFixedWidth(width);
|
||||
widthEdit = width - 200;
|
||||
}
|
||||
|
||||
void MsgWidget::setText(QString text)
|
||||
{
|
||||
ui->editText->setText(text);
|
||||
}
|
||||
|
||||
void MsgWidget::setTime(QString timeStr)
|
||||
{
|
||||
ui->lblTime->setText(timeStr);
|
||||
}
|
||||
|
||||
void MsgWidget::setName(QString name)
|
||||
{
|
||||
ui->lblName->setText(name);
|
||||
}
|
||||
|
||||
void MsgWidget::on_editText_textChanged()
|
||||
{
|
||||
QString text = ui->editText->toPlainText();
|
||||
QFontMetrics metricsFont(ui->editText->font()); //метрики шрифта
|
||||
|
||||
//геометрические параметры текста (высота/ширина в пикселях). В одну строку
|
||||
QRect textRect = metricsFont.boundingRect(QRect(0, 0, 0, 0), 0, text);
|
||||
|
||||
int X = 10; // отступы
|
||||
|
||||
if(textRect.width() > widthEdit)
|
||||
{//Не помещается в одну строку
|
||||
|
||||
textRect = metricsFont.boundingRect(QRect(0, 0, widthEdit, 1000), Qt::TextWordWrap, text);
|
||||
|
||||
if(textRect.width() > widthEdit)
|
||||
{
|
||||
QRect textRect1str = metricsFont.boundingRect(QRect(0, 0, widthEdit, 1000), 0, "string");
|
||||
int ost = textRect.width() % widthEdit;
|
||||
int cnt = textRect.width() / widthEdit;
|
||||
//if(ost > 0)
|
||||
cnt += 1;
|
||||
|
||||
this->setFixedHeight(textRect1str.height() * cnt + (ost? textRect1str.height() : 0) + X + 20 + 30 + 30);
|
||||
ui->editText->setFixedHeight(textRect1str.height() * cnt + (ost? textRect1str.height() : 0) + X);
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
this->setFixedHeight(textRect.height() + X + 20 + 30 + 30);
|
||||
ui->editText->setFixedHeight(textRect.height() + X);
|
||||
}
|
||||
}
|
||||
else
|
||||
{//В одну строку
|
||||
this->setFixedHeight(textRect.height() + X + 20 + 30 + 30);
|
||||
ui->editText->setFixedWidth(textRect.width() + X);
|
||||
ui->editText->setFixedHeight(textRect.height() + X);
|
||||
}
|
||||
}
|
||||
50
LibInstructorsAndTrainees/messanger/msgwidget.h
Normal file
50
LibInstructorsAndTrainees/messanger/msgwidget.h
Normal file
@@ -0,0 +1,50 @@
|
||||
#ifndef MSGWIDGET_H
|
||||
#define MSGWIDGET_H
|
||||
|
||||
#include <QWidget>
|
||||
|
||||
namespace Ui {
|
||||
class MsgWidget;
|
||||
}
|
||||
|
||||
//Виджет одного сообщения для/от клиента
|
||||
|
||||
class MsgWidget : public QWidget
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
enum DirectionMsg
|
||||
{
|
||||
Incoming, //Входящее
|
||||
Outgoing //Исходящее
|
||||
};
|
||||
enum AligneAvatar
|
||||
{
|
||||
Left,
|
||||
Right
|
||||
};
|
||||
|
||||
public:
|
||||
explicit MsgWidget(QString avatar, AligneAvatar aligneAvatar, int width, QWidget *parent = nullptr);
|
||||
~MsgWidget();
|
||||
|
||||
void setText(QString text);
|
||||
void setTime(QString timeStr);
|
||||
void setName(QString name);
|
||||
|
||||
private:
|
||||
void setAligneAvatarLeft();
|
||||
void setAligneAvatarRight();
|
||||
void setAvatar(QString avatar);
|
||||
void setWidth(int width);
|
||||
|
||||
private slots:
|
||||
void on_editText_textChanged();
|
||||
|
||||
private:
|
||||
Ui::MsgWidget *ui;
|
||||
|
||||
int widthEdit;
|
||||
};
|
||||
|
||||
#endif // MSGWIDGET_H
|
||||
187
LibInstructorsAndTrainees/messanger/msgwidget.ui
Normal file
187
LibInstructorsAndTrainees/messanger/msgwidget.ui
Normal file
@@ -0,0 +1,187 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<ui version="4.0">
|
||||
<class>MsgWidget</class>
|
||||
<widget class="QWidget" name="MsgWidget">
|
||||
<property name="geometry">
|
||||
<rect>
|
||||
<x>0</x>
|
||||
<y>0</y>
|
||||
<width>400</width>
|
||||
<height>257</height>
|
||||
</rect>
|
||||
</property>
|
||||
<property name="sizePolicy">
|
||||
<sizepolicy hsizetype="Expanding" vsizetype="Expanding">
|
||||
<horstretch>0</horstretch>
|
||||
<verstretch>0</verstretch>
|
||||
</sizepolicy>
|
||||
</property>
|
||||
<property name="windowTitle">
|
||||
<string>Form</string>
|
||||
</property>
|
||||
<layout class="QGridLayout" name="gridLayout">
|
||||
<item row="1" column="0">
|
||||
<layout class="QHBoxLayout" name="horizontalLayout_Main">
|
||||
<item>
|
||||
<layout class="QVBoxLayout" name="verticalLayout_Avatar">
|
||||
<item>
|
||||
<widget class="QLabel" name="label">
|
||||
<property name="sizePolicy">
|
||||
<sizepolicy hsizetype="Preferred" vsizetype="Fixed">
|
||||
<horstretch>0</horstretch>
|
||||
<verstretch>0</verstretch>
|
||||
</sizepolicy>
|
||||
</property>
|
||||
<property name="minimumSize">
|
||||
<size>
|
||||
<width>0</width>
|
||||
<height>6</height>
|
||||
</size>
|
||||
</property>
|
||||
<property name="maximumSize">
|
||||
<size>
|
||||
<width>16777215</width>
|
||||
<height>6</height>
|
||||
</size>
|
||||
</property>
|
||||
<property name="text">
|
||||
<string/>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item>
|
||||
<widget class="QLabel" name="lblAvatar">
|
||||
<property name="minimumSize">
|
||||
<size>
|
||||
<width>32</width>
|
||||
<height>32</height>
|
||||
</size>
|
||||
</property>
|
||||
<property name="maximumSize">
|
||||
<size>
|
||||
<width>32</width>
|
||||
<height>32</height>
|
||||
</size>
|
||||
</property>
|
||||
<property name="font">
|
||||
<font>
|
||||
<family>Tahoma</family>
|
||||
<pointsize>10</pointsize>
|
||||
</font>
|
||||
</property>
|
||||
<property name="text">
|
||||
<string>TextLabel</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item>
|
||||
<spacer name="verticalSpacer_Avatar">
|
||||
<property name="orientation">
|
||||
<enum>Qt::Vertical</enum>
|
||||
</property>
|
||||
<property name="sizeHint" stdset="0">
|
||||
<size>
|
||||
<width>20</width>
|
||||
<height>40</height>
|
||||
</size>
|
||||
</property>
|
||||
</spacer>
|
||||
</item>
|
||||
</layout>
|
||||
</item>
|
||||
<item>
|
||||
<layout class="QVBoxLayout" name="verticalLayout_Text">
|
||||
<item>
|
||||
<widget class="QLabel" name="lblName">
|
||||
<property name="text">
|
||||
<string>Name</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item>
|
||||
<layout class="QHBoxLayout" name="horizontalLayout_EditText">
|
||||
<item>
|
||||
<spacer name="horizontalSpacer_EditText">
|
||||
<property name="orientation">
|
||||
<enum>Qt::Horizontal</enum>
|
||||
</property>
|
||||
<property name="sizeHint" stdset="0">
|
||||
<size>
|
||||
<width>40</width>
|
||||
<height>20</height>
|
||||
</size>
|
||||
</property>
|
||||
</spacer>
|
||||
</item>
|
||||
<item>
|
||||
<widget class="QTextEdit" name="editText">
|
||||
<property name="sizePolicy">
|
||||
<sizepolicy hsizetype="Fixed" vsizetype="Fixed">
|
||||
<horstretch>0</horstretch>
|
||||
<verstretch>0</verstretch>
|
||||
</sizepolicy>
|
||||
</property>
|
||||
<property name="font">
|
||||
<font>
|
||||
<family>Tahoma</family>
|
||||
<pointsize>10</pointsize>
|
||||
</font>
|
||||
</property>
|
||||
<property name="verticalScrollBarPolicy">
|
||||
<enum>Qt::ScrollBarAlwaysOff</enum>
|
||||
</property>
|
||||
<property name="horizontalScrollBarPolicy">
|
||||
<enum>Qt::ScrollBarAlwaysOff</enum>
|
||||
</property>
|
||||
<property name="sizeAdjustPolicy">
|
||||
<enum>QAbstractScrollArea::AdjustIgnored</enum>
|
||||
</property>
|
||||
<property name="readOnly">
|
||||
<bool>true</bool>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
</layout>
|
||||
</item>
|
||||
<item>
|
||||
<widget class="QLabel" name="lblTime">
|
||||
<property name="font">
|
||||
<font>
|
||||
<family>Tahoma</family>
|
||||
<pointsize>10</pointsize>
|
||||
</font>
|
||||
</property>
|
||||
<property name="text">
|
||||
<string>--:--</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
</layout>
|
||||
</item>
|
||||
<item>
|
||||
<layout class="QHBoxLayout" name="horizontalLayout_Add">
|
||||
<item>
|
||||
<spacer name="horizontalSpacer_Add">
|
||||
<property name="orientation">
|
||||
<enum>Qt::Horizontal</enum>
|
||||
</property>
|
||||
<property name="sizeType">
|
||||
<enum>QSizePolicy::Expanding</enum>
|
||||
</property>
|
||||
<property name="sizeHint" stdset="0">
|
||||
<size>
|
||||
<width>40</width>
|
||||
<height>20</height>
|
||||
</size>
|
||||
</property>
|
||||
</spacer>
|
||||
</item>
|
||||
</layout>
|
||||
</item>
|
||||
</layout>
|
||||
</item>
|
||||
</layout>
|
||||
</widget>
|
||||
<resources/>
|
||||
<connections/>
|
||||
</ui>
|
||||
85
LibInstructorsAndTrainees/messanger/tabdialogmessenger.cpp
Normal file
85
LibInstructorsAndTrainees/messanger/tabdialogmessenger.cpp
Normal file
@@ -0,0 +1,85 @@
|
||||
#include <QScrollBar>
|
||||
#include "tabdialogmessenger.h"
|
||||
#include "msgwidget.h"
|
||||
#include "user.h"
|
||||
#include "instructor.h"
|
||||
|
||||
|
||||
OneDialogMessenger::OneDialogMessenger(User userLocalGUI, User userRemote, QWidget *parent):
|
||||
QListWidget(parent)
|
||||
{
|
||||
this->setObjectName("oneDialogMessenger");
|
||||
|
||||
this->userLocalGUI = userLocalGUI;
|
||||
this->userRemote = userRemote;
|
||||
|
||||
setWordWrap(true);
|
||||
setVerticalScrollBarPolicy(Qt::ScrollBarPolicy::ScrollBarAlwaysOn);
|
||||
}
|
||||
|
||||
OneDialogMessenger::~OneDialogMessenger()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
void OneDialogMessenger::addMsgWidgetLocal(QString text, QString timeStr, QString name)
|
||||
{
|
||||
addMsgWidget(text, timeStr, MsgWidget::DirectionMsg::Outgoing, name);
|
||||
}
|
||||
|
||||
void OneDialogMessenger::addMsgWidgetRemote(QString text, QString timeStr, QString name)
|
||||
{
|
||||
addMsgWidget(text, timeStr, MsgWidget::DirectionMsg::Incoming, name);
|
||||
}
|
||||
|
||||
void OneDialogMessenger::addMsgWidget(QString text, QString timeStr, MsgWidget::DirectionMsg direction, QString name)
|
||||
{
|
||||
QListWidgetItem *listWidgetItem = new QListWidgetItem();
|
||||
|
||||
QString avatar = "";
|
||||
MsgWidget::AligneAvatar aligneAvatar = MsgWidget::AligneAvatar::Left;
|
||||
|
||||
if(direction == MsgWidget::DirectionMsg::Outgoing)
|
||||
{//Исходящее
|
||||
if(static_cast<Instructor*>(&userLocalGUI)->getIsAdmin())
|
||||
avatar = ":/resources/icons/admin.png";
|
||||
else
|
||||
avatar = ":/resources/icons/instructor.png";
|
||||
|
||||
aligneAvatar = MsgWidget::AligneAvatar::Right;
|
||||
}
|
||||
else
|
||||
{//Входящее
|
||||
if(userRemote.getTypeUserDB() == User::TypeUserDBTrainee)
|
||||
avatar = ":/resources/icons/trainee.png";
|
||||
else
|
||||
{
|
||||
if(static_cast<Instructor*>(&userRemote)->getIsAdmin())
|
||||
avatar = ":/resources/icons/admin.png";
|
||||
else
|
||||
avatar = ":/resources/icons/instructor.png";
|
||||
}
|
||||
|
||||
aligneAvatar = MsgWidget::AligneAvatar::Left;
|
||||
}
|
||||
|
||||
int widthW = width();
|
||||
int widthSB = verticalScrollBar()->size().width();
|
||||
//TODO Времянка
|
||||
widthW = 450;
|
||||
widthSB = 17;
|
||||
|
||||
MsgWidget *msgWidget = new MsgWidget(avatar, aligneAvatar, widthW - widthSB);
|
||||
|
||||
addItem(listWidgetItem);
|
||||
setItemWidget(listWidgetItem, msgWidget);
|
||||
|
||||
msgWidget->setText(text);
|
||||
msgWidget->setTime(timeStr);
|
||||
msgWidget->setName(name);
|
||||
|
||||
//Корректировка высоты item
|
||||
listWidgetItem->setSizeHint (QSize(10, msgWidget->height()));
|
||||
|
||||
scrollToItem(listWidgetItem);
|
||||
}
|
||||
29
LibInstructorsAndTrainees/messanger/tabdialogmessenger.h
Normal file
29
LibInstructorsAndTrainees/messanger/tabdialogmessenger.h
Normal file
@@ -0,0 +1,29 @@
|
||||
#ifndef TABDIALOGMESSENGER_H
|
||||
#define TABDIALOGMESSENGER_H
|
||||
|
||||
#include <QListWidget>
|
||||
#include "msgwidget.h"
|
||||
#include "user.h"
|
||||
|
||||
class OneDialogMessenger : public QListWidget
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
OneDialogMessenger(User userLocalGUI, User userRemote, QWidget *parent = nullptr);
|
||||
~OneDialogMessenger();
|
||||
|
||||
int getUserRemoteId() {return userRemote.getID();}
|
||||
|
||||
void addMsgWidgetLocal(QString text, QString timeStr = "", QString name = "");
|
||||
void addMsgWidgetRemote(QString text, QString timeStr = "", QString name = "");
|
||||
|
||||
private:
|
||||
void addMsgWidget(QString text, QString timeStr, MsgWidget::DirectionMsg direction, QString name = "");
|
||||
|
||||
private:
|
||||
User userLocalGUI;
|
||||
User userRemote;
|
||||
};
|
||||
|
||||
#endif // TABDIALOGMESSENGER_H
|
||||
Reference in New Issue
Block a user