From 2b5cd7d86b21f7252e736dd631eb83bb5c9f3f31 Mon Sep 17 00:00:00 2001 From: Pierre HUBERT Date: Sun, 9 Dec 2018 21:25:01 +0100 Subject: [PATCH] ConversationsListWidget emits a signal to request a conversation to be opened. --- widgets/conversationitemwidget.cpp | 26 +++++++++++++++++++++++ widgets/conversationitemwidget.h | 32 +++++++++++++++++++++++++++-- widgets/conversationitemwidget.ui | 14 +++++++++---- widgets/conversationslistwidget.cpp | 23 +++++++++++++++++++++ widgets/conversationslistwidget.h | 27 +++++++++++++++++++++++- 5 files changed, 115 insertions(+), 7 deletions(-) diff --git a/widgets/conversationitemwidget.cpp b/widgets/conversationitemwidget.cpp index ac6b943..40ec031 100644 --- a/widgets/conversationitemwidget.cpp +++ b/widgets/conversationitemwidget.cpp @@ -17,6 +17,8 @@ ConversationItemWidget::~ConversationItemWidget() void ConversationItemWidget::setConversation(const Conversation &conv, const UsersList &list) { + mCurrentConversation = conv; + ui->nameLabel->setText(ConversationsListHelper::getConversationDisplayName(conv, list)); QFont font = ui->nameLabel->font(); font.setBold(!conv.sawLastMessage()); @@ -28,4 +30,28 @@ void ConversationItemWidget::setConversation(const Conversation &conv, const Use ui->numberMembersLabel->setText(tr("%1 members").arg(conv.members().size())); ui->lastActivityLabel->setText(TimeUtils::TimeDiffToString(conv.lastActive())); + + //Conversation not active by default + setActive(false); +} + +void ConversationItemWidget::mousePressEvent(QMouseEvent *) +{ + emit openConversation(); +} + +Conversation ConversationItemWidget::currentConversation() const +{ + return mCurrentConversation; +} + +void ConversationItemWidget::setActive(bool active) +{ + QString styleContainer = active ? "background: black; color: white" : ""; + setStyleSheet(styleContainer); + + QString styleLabels = "padding-left: 5px;"; + ui->numberMembersLabel->setStyleSheet(styleLabels); + ui->lastActivityLabel->setStyleSheet(styleLabels); + } diff --git a/widgets/conversationitemwidget.h b/widgets/conversationitemwidget.h index cda8965..9d0fb04 100644 --- a/widgets/conversationitemwidget.h +++ b/widgets/conversationitemwidget.h @@ -10,11 +10,12 @@ #include +#include "../data/conversation.h" + namespace Ui { class ConversationItemWidget; } -class Conversation; class User; class UsersList; @@ -24,7 +25,7 @@ class ConversationItemWidget : public QWidget public: explicit ConversationItemWidget(QWidget *parent = nullptr); - ~ConversationItemWidget(); + ~ConversationItemWidget() override; /** * Apply a conversation to the widget @@ -34,8 +35,35 @@ public: */ void setConversation(const Conversation &conv, const UsersList &list); + /** + * Get the current conversation represented by this widget + * + * @return The current conversation of this widget + */ + Conversation currentConversation() const; + + /** + * Mark the current conversation as active or not + * + * @param active TRUE to mark the conversation as active / + * FALSE else + */ + void setActive(bool active); + +protected: + + void mousePressEvent(QMouseEvent *) override; + +signals: + + /** + * Request the conversation represented by this widget to be opened + */ + void openConversation(); + private: Ui::ConversationItemWidget *ui; + Conversation mCurrentConversation; }; #endif // CONVERSATIONITEMWIDGET_H diff --git a/widgets/conversationitemwidget.ui b/widgets/conversationitemwidget.ui index 2f1830b..db4d5cc 100644 --- a/widgets/conversationitemwidget.ui +++ b/widgets/conversationitemwidget.ui @@ -32,13 +32,13 @@ - 8 + 0 QLayout::SetMinimumSize - + 0 @@ -60,6 +60,9 @@ true + + 0 + @@ -78,6 +81,9 @@ TextLabel + + 0 + @@ -85,10 +91,10 @@ - 8 + 0 - + 15 diff --git a/widgets/conversationslistwidget.cpp b/widgets/conversationslistwidget.cpp index 8172e5c..f863fa5 100644 --- a/widgets/conversationslistwidget.cpp +++ b/widgets/conversationslistwidget.cpp @@ -66,6 +66,29 @@ void ConversationsListWidget::onGotUsersInfo(bool success, const UsersList &user for(Conversation conv : mCurrList){ ConversationItemWidget *item = new ConversationItemWidget; item->setConversation(conv, users); + connect(item, &ConversationItemWidget::openConversation, this, &ConversationsListWidget::onRequestOpenConversation); layout()->addWidget(item); } } + +void ConversationsListWidget::onRequestOpenConversation() +{ + Conversation conversation = qobject_cast(sender())->currentConversation(); + + //Notify ourselves + setCurrentConversation(conversation); + + //Notify everybody + emit openConversation(conversation, mCurrList.getMembersInformation()); +} + +void ConversationsListWidget::setCurrentConversation(const Conversation ¤tConversation) +{ + mCurrentConversation = currentConversation; + + //Update UI + for(int i = 0; i < layout()->count(); i++){ + ConversationItemWidget *widget = qobject_cast(layout()->itemAt(i)->widget()); + widget->setActive(widget->currentConversation().iD() == currentConversation.iD()); + } +} diff --git a/widgets/conversationslistwidget.h b/widgets/conversationslistwidget.h index 14a43f9..a7fa2ae 100644 --- a/widgets/conversationslistwidget.h +++ b/widgets/conversationslistwidget.h @@ -23,6 +23,23 @@ public: */ void refresh(); + /** + * This method is used to update the currently active conversation + * + * @param currentConversation The current conversation + */ + void setCurrentConversation(const Conversation ¤tConversation); + +signals: + + /** + * Signal emitted when a conversation is requested to be opened + * + * @param conversation The conversation to open + * @param list Information about potential required users + */ + void openConversation(Conversation conversation, UsersList list); + private slots: /** @@ -41,12 +58,20 @@ private slots: */ void onGotUsersInfo(bool success, const UsersList &users); + /** + * Triggered when a conversation item request a conversation to opened + */ + void onRequestOpenConversation(); + private: ConversationsListHelper *mConversationsList; UsersHelper *mUsersHelper; - //Current conversation in cache + //Current conversations list in cache ConversationsList mCurrList; + + //Current opened conversation + Conversation mCurrentConversation; }; #endif // CONVERSATIONSLISTWIDGET_H