From af80c251d75e5e8bf7e8c6f870f8c8b069a822e1 Mon Sep 17 00:00:00 2001 From: Konstantinos Sideris Date: Wed, 31 May 2017 19:42:07 +0300 Subject: [PATCH] Add context menu on RoomList items --- include/RoomInfoListItem.h | 18 +++++++++++++++++- include/RoomList.h | 3 ++- src/ChatPage.cc | 2 +- src/RoomInfoListItem.cc | 37 ++++++++++++++++++++++++++++++++++++- src/RoomList.cc | 11 +++++++++-- src/TopRoomBar.cc | 16 +++++----------- 6 files changed, 70 insertions(+), 17 deletions(-) diff --git a/include/RoomInfoListItem.h b/include/RoomInfoListItem.h index 17c75fc3..40a1cbb0 100644 --- a/include/RoomInfoListItem.h +++ b/include/RoomInfoListItem.h @@ -17,9 +17,13 @@ #pragma once +#include +#include #include +#include "Menu.h" #include "RippleOverlay.h" +#include "RoomSettings.h" #include "RoomState.h" class RoomInfoListItem : public QWidget @@ -27,7 +31,11 @@ class RoomInfoListItem : public QWidget Q_OBJECT public: - RoomInfoListItem(RoomState state, QString room_id, QWidget *parent = 0); + RoomInfoListItem(QSharedPointer settings, + RoomState state, + QString room_id, + QWidget *parent = 0); + ~RoomInfoListItem(); void updateUnreadMessageCount(int count); @@ -48,8 +56,11 @@ public slots: protected: void mousePressEvent(QMouseEvent *event) override; void paintEvent(QPaintEvent *event) override; + void contextMenuEvent(QContextMenuEvent *event) override; private: + QString notificationText(); + const int Padding = 7; const int IconSize = 46; @@ -64,6 +75,11 @@ private: QPixmap roomAvatar_; + Menu *menu_; + QAction *toggleNotifications_; + + QSharedPointer roomSettings_; + bool isPressed_ = false; int maxHeight_; diff --git a/include/RoomList.h b/include/RoomList.h index 489083d1..417ed363 100644 --- a/include/RoomList.h +++ b/include/RoomList.h @@ -35,7 +35,8 @@ public: RoomList(QSharedPointer client, QWidget *parent = 0); ~RoomList(); - void setInitialRooms(const QMap &states); + void setInitialRooms(const QMap> &settings, + const QMap &states); void sync(const QMap &states); void clear(); diff --git a/src/ChatPage.cc b/src/ChatPage.cc index 2b92ac7d..0b09693b 100644 --- a/src/ChatPage.cc +++ b/src/ChatPage.cc @@ -303,7 +303,7 @@ void ChatPage::initialSyncCompleted(const SyncResponse &response) } view_manager_->initialize(response.rooms()); - room_list_->setInitialRooms(state_manager_); + room_list_->setInitialRooms(settingsManager_, state_manager_); sync_timer_->start(sync_interval_); } diff --git a/src/RoomInfoListItem.cc b/src/RoomInfoListItem.cc index 7753536e..3d4c5355 100644 --- a/src/RoomInfoListItem.cc +++ b/src/RoomInfoListItem.cc @@ -24,10 +24,14 @@ #include "RoomState.h" #include "Theme.h" -RoomInfoListItem::RoomInfoListItem(RoomState state, QString room_id, QWidget *parent) +RoomInfoListItem::RoomInfoListItem(QSharedPointer settings, + RoomState state, + QString room_id, + QWidget *parent) : QWidget(parent) , state_(state) , roomId_(room_id) + , roomSettings_{settings} , isPressed_(false) , maxHeight_(IconSize + 2 * Padding) , unreadMsgCount_(0) @@ -44,6 +48,24 @@ RoomInfoListItem::RoomInfoListItem(RoomState state, QString room_id, QWidget *pa ripple_overlay_ = new RippleOverlay(this); ripple_overlay_->setClipPath(path); ripple_overlay_->setClipping(true); + + menu_ = new Menu(this); + + toggleNotifications_ = new QAction(notificationText(), this); + + connect(toggleNotifications_, &QAction::triggered, this, [=]() { + roomSettings_->toggleNotifications(); + }); + + menu_->addAction(toggleNotifications_); +} + +QString RoomInfoListItem::notificationText() +{ + if (roomSettings_.isNull() || roomSettings_->isNotificationsEnabled()) + return QString(tr("Disable notifications")); + + return tr("Enable notifications"); } void RoomInfoListItem::paintEvent(QPaintEvent *event) @@ -193,8 +215,21 @@ void RoomInfoListItem::setState(const RoomState &new_state) repaint(); } +void RoomInfoListItem::contextMenuEvent(QContextMenuEvent *event) +{ + Q_UNUSED(event); + + toggleNotifications_->setText(notificationText()); + menu_->popup(event->globalPos()); +} + void RoomInfoListItem::mousePressEvent(QMouseEvent *event) { + if (event->buttons() == Qt::RightButton) { + QWidget::mousePressEvent(event); + return; + } + emit clicked(roomId_); setPressedState(true); diff --git a/src/RoomList.cc b/src/RoomList.cc index 6d0e185b..55c71b19 100644 --- a/src/RoomList.cc +++ b/src/RoomList.cc @@ -92,10 +92,17 @@ void RoomList::calculateUnreadMessageCount() emit totalUnreadMessageCountUpdated(total_unread_msgs); } -void RoomList::setInitialRooms(const QMap &states) +void RoomList::setInitialRooms(const QMap> &settings, + const QMap &states) { rooms_.clear(); + if (settings.size() != states.size()) { + qWarning() << "Initializing room list"; + qWarning() << "Different number of room states and room settings"; + return; + } + for (auto it = states.constBegin(); it != states.constEnd(); it++) { auto room_id = it.key(); auto state = it.value(); @@ -103,7 +110,7 @@ void RoomList::setInitialRooms(const QMap &states) if (!state.getAvatar().toString().isEmpty()) client_->fetchRoomAvatar(room_id, state.getAvatar()); - RoomInfoListItem *room_item = new RoomInfoListItem(state, room_id, scrollArea_); + RoomInfoListItem *room_item = new RoomInfoListItem(settings[room_id], state, room_id, scrollArea_); connect(room_item, &RoomInfoListItem::clicked, this, &RoomList::highlightSelectedRoom); rooms_.insert(room_id, QSharedPointer(room_item)); diff --git a/src/TopRoomBar.cc b/src/TopRoomBar.cc index ce0693f2..2f04129e 100644 --- a/src/TopRoomBar.cc +++ b/src/TopRoomBar.cc @@ -71,17 +71,16 @@ TopRoomBar::TopRoomBar(QWidget *parent) toggleNotifications_ = new QAction(tr("Disable notifications"), this); connect(toggleNotifications_, &QAction::triggered, this, [=]() { roomSettings_->toggleNotifications(); - - if (roomSettings_->isNotificationsEnabled()) - toggleNotifications_->setText("Disable notifications"); - else - toggleNotifications_->setText("Enable notifications"); - }); menu_->addAction(toggleNotifications_); connect(settingsBtn_, &QPushButton::clicked, this, [=]() { + if (roomSettings_->isNotificationsEnabled()) + toggleNotifications_->setText(tr("Disable notifications")); + else + toggleNotifications_->setText(tr("Enable notifications")); + auto pos = mapToGlobal(settingsBtn_->pos()); menu_->popup(QPoint(pos.x() + buttonSize_ - menu_->sizeHint().width(), pos.y() + buttonSize_)); @@ -121,11 +120,6 @@ void TopRoomBar::paintEvent(QPaintEvent *event) void TopRoomBar::setRoomSettings(QSharedPointer settings) { roomSettings_ = settings; - - if (roomSettings_->isNotificationsEnabled()) - toggleNotifications_->setText("Disable notifications"); - else - toggleNotifications_->setText("Enable notifications"); } TopRoomBar::~TopRoomBar()