ConversationsListWidget emits a signal to request a conversation to be

opened.
This commit is contained in:
Pierre HUBERT 2018-12-09 21:25:01 +01:00 committed by Pierre HUBERT
parent 1df359735d
commit 2b5cd7d86b
5 changed files with 115 additions and 7 deletions

View File

@ -17,6 +17,8 @@ ConversationItemWidget::~ConversationItemWidget()
void ConversationItemWidget::setConversation(const Conversation &conv, const UsersList &list) void ConversationItemWidget::setConversation(const Conversation &conv, const UsersList &list)
{ {
mCurrentConversation = conv;
ui->nameLabel->setText(ConversationsListHelper::getConversationDisplayName(conv, list)); ui->nameLabel->setText(ConversationsListHelper::getConversationDisplayName(conv, list));
QFont font = ui->nameLabel->font(); QFont font = ui->nameLabel->font();
font.setBold(!conv.sawLastMessage()); 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->numberMembersLabel->setText(tr("%1 members").arg(conv.members().size()));
ui->lastActivityLabel->setText(TimeUtils::TimeDiffToString(conv.lastActive())); 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);
} }

View File

@ -10,11 +10,12 @@
#include <QWidget> #include <QWidget>
#include "../data/conversation.h"
namespace Ui { namespace Ui {
class ConversationItemWidget; class ConversationItemWidget;
} }
class Conversation;
class User; class User;
class UsersList; class UsersList;
@ -24,7 +25,7 @@ class ConversationItemWidget : public QWidget
public: public:
explicit ConversationItemWidget(QWidget *parent = nullptr); explicit ConversationItemWidget(QWidget *parent = nullptr);
~ConversationItemWidget(); ~ConversationItemWidget() override;
/** /**
* Apply a conversation to the widget * Apply a conversation to the widget
@ -34,8 +35,35 @@ public:
*/ */
void setConversation(const Conversation &conv, const UsersList &list); 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: private:
Ui::ConversationItemWidget *ui; Ui::ConversationItemWidget *ui;
Conversation mCurrentConversation;
}; };
#endif // CONVERSATIONITEMWIDGET_H #endif // CONVERSATIONITEMWIDGET_H

View File

@ -32,13 +32,13 @@
<item> <item>
<layout class="QHBoxLayout" name="horizontalLayout"> <layout class="QHBoxLayout" name="horizontalLayout">
<property name="spacing"> <property name="spacing">
<number>8</number> <number>0</number>
</property> </property>
<property name="sizeConstraint"> <property name="sizeConstraint">
<enum>QLayout::SetMinimumSize</enum> <enum>QLayout::SetMinimumSize</enum>
</property> </property>
<item> <item>
<widget class="QLabel" name="label_2"> <widget class="QLabel" name="membersIconLabel">
<property name="sizePolicy"> <property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Preferred"> <sizepolicy hsizetype="Preferred" vsizetype="Preferred">
<horstretch>0</horstretch> <horstretch>0</horstretch>
@ -60,6 +60,9 @@
<property name="scaledContents"> <property name="scaledContents">
<bool>true</bool> <bool>true</bool>
</property> </property>
<property name="margin">
<number>0</number>
</property>
</widget> </widget>
</item> </item>
<item> <item>
@ -78,6 +81,9 @@
<property name="text"> <property name="text">
<string>TextLabel</string> <string>TextLabel</string>
</property> </property>
<property name="margin">
<number>0</number>
</property>
</widget> </widget>
</item> </item>
</layout> </layout>
@ -85,10 +91,10 @@
<item> <item>
<layout class="QHBoxLayout" name="horizontalLayout_2"> <layout class="QHBoxLayout" name="horizontalLayout_2">
<property name="spacing"> <property name="spacing">
<number>8</number> <number>0</number>
</property> </property>
<item> <item>
<widget class="QLabel" name="label_3"> <widget class="QLabel" name="lastUpdateLabel">
<property name="maximumSize"> <property name="maximumSize">
<size> <size>
<width>15</width> <width>15</width>

View File

@ -66,6 +66,29 @@ void ConversationsListWidget::onGotUsersInfo(bool success, const UsersList &user
for(Conversation conv : mCurrList){ for(Conversation conv : mCurrList){
ConversationItemWidget *item = new ConversationItemWidget; ConversationItemWidget *item = new ConversationItemWidget;
item->setConversation(conv, users); item->setConversation(conv, users);
connect(item, &ConversationItemWidget::openConversation, this, &ConversationsListWidget::onRequestOpenConversation);
layout()->addWidget(item); layout()->addWidget(item);
} }
} }
void ConversationsListWidget::onRequestOpenConversation()
{
Conversation conversation = qobject_cast<ConversationItemWidget *>(sender())->currentConversation();
//Notify ourselves
setCurrentConversation(conversation);
//Notify everybody
emit openConversation(conversation, mCurrList.getMembersInformation());
}
void ConversationsListWidget::setCurrentConversation(const Conversation &currentConversation)
{
mCurrentConversation = currentConversation;
//Update UI
for(int i = 0; i < layout()->count(); i++){
ConversationItemWidget *widget = qobject_cast<ConversationItemWidget *>(layout()->itemAt(i)->widget());
widget->setActive(widget->currentConversation().iD() == currentConversation.iD());
}
}

View File

@ -23,6 +23,23 @@ public:
*/ */
void refresh(); void refresh();
/**
* This method is used to update the currently active conversation
*
* @param currentConversation The current conversation
*/
void setCurrentConversation(const Conversation &currentConversation);
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: private slots:
/** /**
@ -41,12 +58,20 @@ private slots:
*/ */
void onGotUsersInfo(bool success, const UsersList &users); void onGotUsersInfo(bool success, const UsersList &users);
/**
* Triggered when a conversation item request a conversation to opened
*/
void onRequestOpenConversation();
private: private:
ConversationsListHelper *mConversationsList; ConversationsListHelper *mConversationsList;
UsersHelper *mUsersHelper; UsersHelper *mUsersHelper;
//Current conversation in cache //Current conversations list in cache
ConversationsList mCurrList; ConversationsList mCurrList;
//Current opened conversation
Conversation mCurrentConversation;
}; };
#endif // CONVERSATIONSLISTWIDGET_H #endif // CONVERSATIONSLISTWIDGET_H