Add context menu on RoomList items

This commit is contained in:
Konstantinos Sideris 2017-05-31 19:42:07 +03:00
parent c184362044
commit af80c251d7
6 changed files with 70 additions and 17 deletions

View File

@ -17,9 +17,13 @@
#pragma once
#include <QAction>
#include <QSharedPointer>
#include <QWidget>
#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<RoomSettings> 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> roomSettings_;
bool isPressed_ = false;
int maxHeight_;

View File

@ -35,7 +35,8 @@ public:
RoomList(QSharedPointer<MatrixClient> client, QWidget *parent = 0);
~RoomList();
void setInitialRooms(const QMap<QString, RoomState> &states);
void setInitialRooms(const QMap<QString, QSharedPointer<RoomSettings>> &settings,
const QMap<QString, RoomState> &states);
void sync(const QMap<QString, RoomState> &states);
void clear();

View File

@ -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_);
}

View File

@ -24,10 +24,14 @@
#include "RoomState.h"
#include "Theme.h"
RoomInfoListItem::RoomInfoListItem(RoomState state, QString room_id, QWidget *parent)
RoomInfoListItem::RoomInfoListItem(QSharedPointer<RoomSettings> 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);

View File

@ -92,10 +92,17 @@ void RoomList::calculateUnreadMessageCount()
emit totalUnreadMessageCountUpdated(total_unread_msgs);
}
void RoomList::setInitialRooms(const QMap<QString, RoomState> &states)
void RoomList::setInitialRooms(const QMap<QString, QSharedPointer<RoomSettings>> &settings,
const QMap<QString, RoomState> &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<QString, RoomState> &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<RoomInfoListItem>(room_item));

View File

@ -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<RoomSettings> settings)
{
roomSettings_ = settings;
if (roomSettings_->isNotificationsEnabled())
toggleNotifications_->setText("Disable notifications");
else
toggleNotifications_->setText("Enable notifications");
}
TopRoomBar::~TopRoomBar()