Display conversation messages.

This commit is contained in:
Pierre HUBERT 2018-12-16 21:22:25 +01:00
parent 640da86c0d
commit 41cd02dfec
9 changed files with 189 additions and 13 deletions

View File

@ -29,7 +29,8 @@ SOURCES += \
widgets/conversationwidget.cpp \ widgets/conversationwidget.cpp \
data/newconversationmessage.cpp \ data/newconversationmessage.cpp \
helpers/conversationhelper.cpp \ helpers/conversationhelper.cpp \
data/conversationmessage.cpp data/conversationmessage.cpp \
widgets/conversationmessagewidget.cpp
HEADERS += \ HEADERS += \
helpers/accounthelper.h \ helpers/accounthelper.h \
@ -60,14 +61,16 @@ HEADERS += \
widgets/conversationwidget.h \ widgets/conversationwidget.h \
data/newconversationmessage.h \ data/newconversationmessage.h \
helpers/conversationhelper.h \ helpers/conversationhelper.h \
data/conversationmessage.h data/conversationmessage.h \
widgets/conversationmessagewidget.h
FORMS += \ FORMS += \
widgets/loginwidget.ui \ widgets/loginwidget.ui \
widgets/mainwindow.ui \ widgets/mainwindow.ui \
widgets/aboutthisappdialog.ui \ widgets/aboutthisappdialog.ui \
widgets/conversationitemwidget.ui \ widgets/conversationitemwidget.ui \
widgets/conversationwidget.ui widgets/conversationwidget.ui \
widgets/conversationmessagewidget.ui
RESOURCES += \ RESOURCES += \
res/ressources.qrc res/ressources.qrc

View File

@ -54,3 +54,8 @@ void ConversationMessage::setImagePath(const QString &imagePath)
{ {
mImagePath = imagePath; mImagePath = imagePath;
} }
bool ConversationMessage::operator<(const ConversationMessage &b) const
{
return b.iD() > iD();
}

View File

@ -29,6 +29,8 @@ public:
QString imagePath() const; QString imagePath() const;
void setImagePath(const QString &imagePath); void setImagePath(const QString &imagePath);
bool operator<(const ConversationMessage &b) const;
private: private:
int mID; int mID;
int mUserID; int mUserID;

View File

@ -0,0 +1,22 @@
#include "conversationmessagewidget.h"
#include "ui_conversationmessagewidget.h"
#include "../data/user.h"
#include "../data/conversationmessage.h"
ConversationMessageWidget::ConversationMessageWidget(QWidget *parent) :
QWidget(parent),
ui(new Ui::ConversationMessageWidget)
{
ui->setupUi(this);
}
ConversationMessageWidget::~ConversationMessageWidget()
{
delete ui;
}
void ConversationMessageWidget::setMessage(const ConversationMessage &message, const User &user)
{
ui->nameLabel->setText(user.displayName());
ui->messageLabel->setText(message.message());
}

View File

@ -0,0 +1,32 @@
#ifndef CONVERSATIONMESSAGEWIDGET_H
#define CONVERSATIONMESSAGEWIDGET_H
#include <QWidget>
namespace Ui {
class ConversationMessageWidget;
}
class ConversationMessage;
class User;
class ConversationMessageWidget : public QWidget
{
Q_OBJECT
public:
explicit ConversationMessageWidget(QWidget *parent = nullptr);
~ConversationMessageWidget();
/**
* Apply a new conversation message to the widget
*
* @param message The message to apply to the widget
*/
void setMessage(const ConversationMessage &message, const User &user);
private:
Ui::ConversationMessageWidget *ui;
};
#endif // CONVERSATIONMESSAGEWIDGET_H

View File

@ -0,0 +1,59 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>ConversationMessageWidget</class>
<widget class="QWidget" name="ConversationMessageWidget">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>183</width>
<height>35</height>
</rect>
</property>
<property name="windowTitle">
<string>Form</string>
</property>
<layout class="QHBoxLayout" name="horizontalLayout">
<item>
<widget class="QLabel" name="nameLabel">
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Preferred">
<horstretch>1</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="font">
<font>
<weight>75</weight>
<bold>true</bold>
</font>
</property>
<property name="text">
<string>Name</string>
</property>
</widget>
</item>
<item>
<widget class="QLabel" name="messageLabel">
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Preferred">
<horstretch>3</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="text">
<string>Message content</string>
</property>
<property name="textFormat">
<enum>Qt::PlainText</enum>
</property>
<property name="wordWrap">
<bool>true</bool>
</property>
</widget>
</item>
</layout>
</widget>
<resources/>
<connections/>
</ui>

View File

@ -1,10 +1,14 @@
#include <QMessageBox> #include <QMessageBox>
#include <algorithm>
#include "conversationwidget.h" #include "conversationwidget.h"
#include "ui_conversationwidget.h" #include "ui_conversationwidget.h"
#include "conversationmessagewidget.h"
#include "../helpers/conversationslisthelper.h" #include "../helpers/conversationslisthelper.h"
#include "../helpers/conversationhelper.h" #include "../helpers/conversationhelper.h"
#include "../config.h" #include "../config.h"
#include "../data/conversationmessage.h"
#include "../utils/uiutils.h"
ConversationWidget::ConversationWidget(const Conversation &conversation, const UsersList &list, QWidget *parent) : ConversationWidget::ConversationWidget(const Conversation &conversation, const UsersList &list, QWidget *parent) :
QWidget(parent), QWidget(parent),
@ -21,6 +25,10 @@ ConversationWidget::ConversationWidget(const Conversation &conversation, const U
//Initalize helpers //Initalize helpers
mConversationHelper = new ConversationHelper(this); mConversationHelper = new ConversationHelper(this);
connect(mConversationHelper, &ConversationHelper::sendMessageCallback, this, &ConversationWidget::sendMessageCallback); connect(mConversationHelper, &ConversationHelper::sendMessageCallback, this, &ConversationWidget::sendMessageCallback);
connect(mConversationHelper, &ConversationHelper::getMessagesCallback, this, &ConversationWidget::getMessagesCallback);
//Get the latest message of the conversation
mConversationHelper->getMessages(conversation.iD());
} }
ConversationWidget::~ConversationWidget() ConversationWidget::~ConversationWidget()
@ -55,6 +63,36 @@ void ConversationWidget::sendMessage()
mConversationHelper->sendMessage(newMessage); mConversationHelper->sendMessage(newMessage);
} }
void ConversationWidget::getMessagesCallback(bool success, QList<ConversationMessage> list)
{
if(!success){
QMessageBox::warning(this, tr("Error while getting messages list"), tr("Could not refresh messages list!"));
return;
}
//Stop now if the list of messages is empty
if(list.empty())
return;
mMessages.append(list);
std::sort(mMessages.begin(), mMessages.end());
//Remove previous list of messages
UiUtils::emptyLayout(ui->messagesLayout);
//Apply the list of messages
QList<ConversationMessage>::iterator it = mMessages.begin();
while(it != mMessages.end()){
ConversationMessageWidget *convMessageWidget = new ConversationMessageWidget();
convMessageWidget->setMessage(*it, mUsersList.get(it->userID()));
ui->messagesLayout->addWidget(convMessageWidget);
it++;
}
}
void ConversationWidget::sendMessageCallback(bool success) void ConversationWidget::sendMessageCallback(bool success)
{ {
setSendMessageFormLocked(false); setSendMessageFormLocked(false);

View File

@ -13,6 +13,7 @@
#include "../data/conversation.h" #include "../data/conversation.h"
#include "../data/userslist.h" #include "../data/userslist.h"
#include "../data/conversationmessage.h"
namespace Ui { namespace Ui {
class ConversationWidget; class ConversationWidget;
@ -39,6 +40,14 @@ public slots:
private slots: private slots:
/**
* Method called once we have got the list of messages
*
* @param success TRUE in case of success of the operation / FALSE else
* @param list The list of downloaded messages
*/
void getMessagesCallback(bool success, QList<ConversationMessage> list);
/** /**
* Method called once the server replied to a send conversation * Method called once the server replied to a send conversation
* message request * message request
@ -66,6 +75,7 @@ private:
ConversationHelper *mConversationHelper; ConversationHelper *mConversationHelper;
Conversation mConversation; Conversation mConversation;
UsersList mUsersList; UsersList mUsersList;
QList<ConversationMessage> mMessages;
}; };
#endif // CONVERSATIONWIDGET_H #endif // CONVERSATIONWIDGET_H

View File

@ -28,17 +28,22 @@
</widget> </widget>
</item> </item>
<item> <item>
<spacer name="verticalSpacer"> <widget class="QScrollArea" name="scrollArea">
<property name="orientation"> <property name="widgetResizable">
<enum>Qt::Vertical</enum> <bool>true</bool>
</property> </property>
<property name="sizeHint" stdset="0"> <widget class="QWidget" name="conversationMessages">
<size> <property name="geometry">
<width>20</width> <rect>
<height>40</height> <x>0</x>
</size> <y>0</y>
</property> <width>380</width>
</spacer> <height>224</height>
</rect>
</property>
<layout class="QVBoxLayout" name="messagesLayout"/>
</widget>
</widget>
</item> </item>
<item> <item>
<layout class="QHBoxLayout" name="horizontalLayout"> <layout class="QHBoxLayout" name="horizontalLayout">