diff --git a/ComunicMessages.pro b/ComunicMessages.pro index 9084de5..c973a36 100644 --- a/ComunicMessages.pro +++ b/ComunicMessages.pro @@ -29,7 +29,8 @@ SOURCES += \ widgets/conversationwidget.cpp \ data/newconversationmessage.cpp \ helpers/conversationhelper.cpp \ - data/conversationmessage.cpp + data/conversationmessage.cpp \ + widgets/conversationmessagewidget.cpp HEADERS += \ helpers/accounthelper.h \ @@ -60,14 +61,16 @@ HEADERS += \ widgets/conversationwidget.h \ data/newconversationmessage.h \ helpers/conversationhelper.h \ - data/conversationmessage.h + data/conversationmessage.h \ + widgets/conversationmessagewidget.h FORMS += \ widgets/loginwidget.ui \ widgets/mainwindow.ui \ widgets/aboutthisappdialog.ui \ widgets/conversationitemwidget.ui \ - widgets/conversationwidget.ui + widgets/conversationwidget.ui \ + widgets/conversationmessagewidget.ui RESOURCES += \ res/ressources.qrc diff --git a/data/conversationmessage.cpp b/data/conversationmessage.cpp index 315f2a4..766a731 100644 --- a/data/conversationmessage.cpp +++ b/data/conversationmessage.cpp @@ -54,3 +54,8 @@ void ConversationMessage::setImagePath(const QString &imagePath) { mImagePath = imagePath; } + +bool ConversationMessage::operator<(const ConversationMessage &b) const +{ + return b.iD() > iD(); +} diff --git a/data/conversationmessage.h b/data/conversationmessage.h index ce49750..ad552ed 100644 --- a/data/conversationmessage.h +++ b/data/conversationmessage.h @@ -29,6 +29,8 @@ public: QString imagePath() const; void setImagePath(const QString &imagePath); + bool operator<(const ConversationMessage &b) const; + private: int mID; int mUserID; diff --git a/widgets/conversationmessagewidget.cpp b/widgets/conversationmessagewidget.cpp new file mode 100644 index 0000000..505899e --- /dev/null +++ b/widgets/conversationmessagewidget.cpp @@ -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()); +} diff --git a/widgets/conversationmessagewidget.h b/widgets/conversationmessagewidget.h new file mode 100644 index 0000000..d28b62e --- /dev/null +++ b/widgets/conversationmessagewidget.h @@ -0,0 +1,32 @@ +#ifndef CONVERSATIONMESSAGEWIDGET_H +#define CONVERSATIONMESSAGEWIDGET_H + +#include + +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 diff --git a/widgets/conversationmessagewidget.ui b/widgets/conversationmessagewidget.ui new file mode 100644 index 0000000..aacd7e6 --- /dev/null +++ b/widgets/conversationmessagewidget.ui @@ -0,0 +1,59 @@ + + + ConversationMessageWidget + + + + 0 + 0 + 183 + 35 + + + + Form + + + + + + + 1 + 0 + + + + + 75 + true + + + + Name + + + + + + + + 3 + 0 + + + + Message content + + + Qt::PlainText + + + true + + + + + + + + diff --git a/widgets/conversationwidget.cpp b/widgets/conversationwidget.cpp index 00f89e0..130601d 100644 --- a/widgets/conversationwidget.cpp +++ b/widgets/conversationwidget.cpp @@ -1,10 +1,14 @@ #include +#include #include "conversationwidget.h" #include "ui_conversationwidget.h" +#include "conversationmessagewidget.h" #include "../helpers/conversationslisthelper.h" #include "../helpers/conversationhelper.h" #include "../config.h" +#include "../data/conversationmessage.h" +#include "../utils/uiutils.h" ConversationWidget::ConversationWidget(const Conversation &conversation, const UsersList &list, QWidget *parent) : QWidget(parent), @@ -21,6 +25,10 @@ ConversationWidget::ConversationWidget(const Conversation &conversation, const U //Initalize helpers mConversationHelper = new ConversationHelper(this); 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() @@ -55,6 +63,36 @@ void ConversationWidget::sendMessage() mConversationHelper->sendMessage(newMessage); } +void ConversationWidget::getMessagesCallback(bool success, QList 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::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) { setSendMessageFormLocked(false); diff --git a/widgets/conversationwidget.h b/widgets/conversationwidget.h index 1dd9a6e..c0d6ddc 100644 --- a/widgets/conversationwidget.h +++ b/widgets/conversationwidget.h @@ -13,6 +13,7 @@ #include "../data/conversation.h" #include "../data/userslist.h" +#include "../data/conversationmessage.h" namespace Ui { class ConversationWidget; @@ -39,6 +40,14 @@ public 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 list); + /** * Method called once the server replied to a send conversation * message request @@ -66,6 +75,7 @@ private: ConversationHelper *mConversationHelper; Conversation mConversation; UsersList mUsersList; + QList mMessages; }; #endif // CONVERSATIONWIDGET_H diff --git a/widgets/conversationwidget.ui b/widgets/conversationwidget.ui index 91f4e76..b576c60 100644 --- a/widgets/conversationwidget.ui +++ b/widgets/conversationwidget.ui @@ -28,17 +28,22 @@ - - - Qt::Vertical + + + true - - - 20 - 40 - - - + + + + 0 + 0 + 380 + 224 + + + + +