From c184362044a222546c1c3a0519f47754067742e6 Mon Sep 17 00:00:00 2001 From: Konstantinos Sideris Date: Wed, 31 May 2017 17:06:03 +0300 Subject: [PATCH] Add menu to toggle notifications --- include/ChatPage.h | 2 + include/RoomSettings.h | 55 +++++++++++++++++++++++ include/TopRoomBar.h | 16 ++++++- include/ui/Menu.h | 25 +++++++++++ resources/icons/vertical-ellipsis.png | Bin 0 -> 509 bytes resources/res.qrc | 1 + src/ChatPage.cc | 31 +++++++++---- src/EmojiPanel.cc | 2 +- src/TopRoomBar.cc | 62 +++++++++++++++++--------- 9 files changed, 163 insertions(+), 31 deletions(-) create mode 100644 include/RoomSettings.h create mode 100644 include/ui/Menu.h create mode 100644 resources/icons/vertical-ellipsis.png diff --git a/include/ChatPage.h b/include/ChatPage.h index 68495276..74db6b15 100644 --- a/include/ChatPage.h +++ b/include/ChatPage.h @@ -23,6 +23,7 @@ #include "MatrixClient.h" #include "RoomList.h" +#include "RoomSettings.h" #include "RoomState.h" #include "Splitter.h" #include "TextInputWidget.h" @@ -92,6 +93,7 @@ private: UserInfoWidget *user_info_widget_; QMap state_manager_; + QMap> settingsManager_; // Matrix Client API provider. QSharedPointer client_; diff --git a/include/RoomSettings.h b/include/RoomSettings.h new file mode 100644 index 00000000..ee74b9eb --- /dev/null +++ b/include/RoomSettings.h @@ -0,0 +1,55 @@ +/* + * nheko Copyright (C) 2017 Konstantinos Sideris + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#pragma once + +#include + +class RoomSettings +{ +public: + RoomSettings(QString room_id) + { + path_ = QString("notifications/%1").arg(room_id); + isNotificationsEnabled_ = true; + + QSettings settings; + + if (settings.contains(path_)) + isNotificationsEnabled_ = settings.value(path_).toBool(); + else + settings.setValue(path_, isNotificationsEnabled_); + }; + + bool isNotificationsEnabled() + { + return isNotificationsEnabled_; + }; + + void toggleNotifications() + { + isNotificationsEnabled_ = !isNotificationsEnabled_; + + QSettings settings; + settings.setValue(path_, isNotificationsEnabled_); + } + +private: + QString path_; + + bool isNotificationsEnabled_; +}; diff --git a/include/TopRoomBar.h b/include/TopRoomBar.h index 304ec320..6e4bfe7a 100644 --- a/include/TopRoomBar.h +++ b/include/TopRoomBar.h @@ -17,15 +17,20 @@ #pragma once +#include +#include #include #include #include #include +#include #include #include #include "Avatar.h" #include "FlatButton.h" +#include "Menu.h" +#include "RoomSettings.h" class TopRoomBar : public QWidget { @@ -39,6 +44,7 @@ public: inline void updateRoomName(const QString &name); inline void updateRoomTopic(const QString &topic); void updateRoomAvatarFromName(const QString &name); + void setRoomSettings(QSharedPointer settings); void reset(); @@ -52,10 +58,16 @@ private: QLabel *name_label_; QLabel *topic_label_; - FlatButton *search_button_; - FlatButton *settings_button_; + QSharedPointer roomSettings_; + + QMenu *menu_; + QAction *toggleNotifications_; + + FlatButton *settingsBtn_; Avatar *avatar_; + + int buttonSize_; }; inline void TopRoomBar::updateRoomAvatar(const QImage &avatar_image) diff --git a/include/ui/Menu.h b/include/ui/Menu.h new file mode 100644 index 00000000..78a35b43 --- /dev/null +++ b/include/ui/Menu.h @@ -0,0 +1,25 @@ +#pragma once + +#include + +class Menu : public QMenu +{ +public: + Menu(QWidget *parent = nullptr) + : QMenu(parent) + { + setFont(QFont("Open Sans", 10)); + setStyleSheet( + "QMenu { background-color: white; margin: 0px;}" + "QMenu::item { padding: 7px 20px; border: 1px solid transparent; margin: 2px 0px; }" + "QMenu::item:selected { background: rgba(180, 180, 180, 100); }"); + }; + +protected: + void leaveEvent(QEvent *e) + { + Q_UNUSED(e); + + hide(); + } +}; diff --git a/resources/icons/vertical-ellipsis.png b/resources/icons/vertical-ellipsis.png new file mode 100644 index 0000000000000000000000000000000000000000..82714451d31182dff0ac1748d05c1a0ffb0d860f GIT binary patch literal 509 zcmeAS@N?(olHy`uVBq!ia0vp^3LwnE0wix1Z>k4UEa{HEjtmSN`?>!lvVtU&J%W50 z7^>757#dm_7=8hT8eT9klo~KFyh>nTu$sZZAYL$MSD+10f+@+{-GzZ+Rj;xUkjGiz z5m^kh={g8AI%&+V01C2~c>21sKV%i-H5HI%SYiqkI_>G=7~*mK?9`2%hYbW=>wBb} z)e5BVq%u2d^*@mHQ9EUnx7pEZv1klyjKm@nB?b58f0Q+z?A&;+oL^2D?NY%?PN}v7CMhd8i!PC{xWt~$(695Rp BspbFx literal 0 HcmV?d00001 diff --git a/resources/res.qrc b/resources/res.qrc index be7d8770..56bf7144 100644 --- a/resources/res.qrc +++ b/resources/res.qrc @@ -22,6 +22,7 @@ icons/emoji-categories/symbols.png icons/emoji-categories/flags.png + icons/vertical-ellipsis.png diff --git a/src/ChatPage.cc b/src/ChatPage.cc index d318d086..2b92ac7d 100644 --- a/src/ChatPage.cc +++ b/src/ChatPage.cc @@ -127,10 +127,16 @@ ChatPage::ChatPage(QSharedPointer client, QWidget *parent) connect(room_list_, &RoomList::roomChanged, this, &ChatPage::changeTopRoomInfo); connect(room_list_, &RoomList::roomChanged, view_manager_, &TimelineViewManager::setHistoryView); - connect(view_manager_, - SIGNAL(unreadMessages(const QString &, int)), - room_list_, - SLOT(updateUnreadMessageCount(const QString &, int))); + connect(view_manager_, &TimelineViewManager::unreadMessages, this, [=](const QString &roomid, int count) { + if (!settingsManager_.contains(roomid)) { + qWarning() << "RoomId does not have settings" << roomid; + room_list_->updateUnreadMessageCount(roomid, count); + return; + } + + if (settingsManager_[roomid]->isNotificationsEnabled()) + room_list_->updateUnreadMessageCount(roomid, count); + }); connect(room_list_, SIGNAL(totalUnreadMessageCountUpdated(int)), @@ -173,11 +179,17 @@ void ChatPage::logout() { sync_timer_->stop(); + // Delete all config parameters. QSettings settings; - settings.remove("auth/access_token"); - settings.remove("auth/home_server"); - settings.remove("auth/user_id"); - settings.remove("client/transaction_id"); + settings.beginGroup("auth"); + settings.remove(""); + settings.endGroup(); + settings.beginGroup("client"); + settings.remove(""); + settings.endGroup(); + settings.beginGroup("notifications"); + settings.remove(""); + settings.endGroup(); // Clear the environment. room_list_->clear(); @@ -188,6 +200,7 @@ void ChatPage::logout() client_->reset(); state_manager_.clear(); + settingsManager_.clear(); room_avatars_.clear(); emit close(); @@ -286,6 +299,7 @@ void ChatPage::initialSyncCompleted(const SyncResponse &response) updateDisplayNames(room_state); state_manager_.insert(it.key(), room_state); + settingsManager_.insert(it.key(), QSharedPointer(new RoomSettings(it.key()))); } view_manager_->initialize(response.rooms()); @@ -325,6 +339,7 @@ void ChatPage::changeTopRoomInfo(const QString &room_id) top_bar_->updateRoomName(state.getName()); top_bar_->updateRoomTopic(state.getTopic()); + top_bar_->setRoomSettings(settingsManager_[room_id]); if (room_avatars_.contains(room_id)) top_bar_->updateRoomAvatar(room_avatars_.value(room_id).toImage()); diff --git a/src/EmojiPanel.cc b/src/EmojiPanel.cc index dde44369..53b3f8d1 100644 --- a/src/EmojiPanel.cc +++ b/src/EmojiPanel.cc @@ -28,7 +28,7 @@ EmojiPanel::EmojiPanel(QWidget *parent) : QWidget(parent) - , shadowMargin_{3} + , shadowMargin_{2} , width_{370} , height_{350} , animationDuration_{100} diff --git a/src/TopRoomBar.cc b/src/TopRoomBar.cc index b35291cb..ce0693f2 100644 --- a/src/TopRoomBar.cc +++ b/src/TopRoomBar.cc @@ -21,6 +21,7 @@ TopRoomBar::TopRoomBar(QWidget *parent) : QWidget(parent) + , buttonSize_{32} { setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed); setMinimumSize(QSize(0, 65)); @@ -28,7 +29,7 @@ TopRoomBar::TopRoomBar(QWidget *parent) top_layout_ = new QHBoxLayout(); top_layout_->setSpacing(10); - top_layout_->setContentsMargins(10, 10, 0, 10); + top_layout_->setMargin(10); avatar_ = new Avatar(this); avatar_->setLetter(QChar('?')); @@ -49,31 +50,42 @@ TopRoomBar::TopRoomBar(QWidget *parent) text_layout_->addWidget(name_label_); text_layout_->addWidget(topic_label_); - settings_button_ = new FlatButton(this); - settings_button_->setForegroundColor(QColor("#acc7dc")); - settings_button_->setCursor(QCursor(Qt::PointingHandCursor)); - settings_button_->setStyleSheet("width: 30px; height: 30px;"); + settingsBtn_ = new FlatButton(this); + settingsBtn_->setForegroundColor(QColor("#acc7dc")); + settingsBtn_->setCursor(QCursor(Qt::PointingHandCursor)); + settingsBtn_->setFixedSize(buttonSize_, buttonSize_); + settingsBtn_->setCornerRadius(buttonSize_ / 2); QIcon settings_icon; - settings_icon.addFile(":/icons/icons/cog.png", QSize(), QIcon::Normal, QIcon::Off); - settings_button_->setIcon(settings_icon); - settings_button_->setIconSize(QSize(16, 16)); - - search_button_ = new FlatButton(this); - search_button_->setForegroundColor(QColor("#acc7dc")); - search_button_->setCursor(QCursor(Qt::PointingHandCursor)); - search_button_->setStyleSheet("width: 30px; height: 30px;"); - - QIcon search_icon; - search_icon.addFile(":/icons/icons/search.png", QSize(), QIcon::Normal, QIcon::Off); - search_button_->setIcon(search_icon); - search_button_->setIconSize(QSize(16, 16)); + settings_icon.addFile(":/icons/icons/vertical-ellipsis.png", QSize(), QIcon::Normal, QIcon::Off); + settingsBtn_->setIcon(settings_icon); + settingsBtn_->setIconSize(QSize(buttonSize_ / 2, buttonSize_ / 2)); top_layout_->addWidget(avatar_); top_layout_->addLayout(text_layout_); top_layout_->addStretch(1); - top_layout_->addWidget(search_button_); - top_layout_->addWidget(settings_button_); + top_layout_->addWidget(settingsBtn_); + + menu_ = new Menu(this); + + 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, [=]() { + auto pos = mapToGlobal(settingsBtn_->pos()); + menu_->popup(QPoint(pos.x() + buttonSize_ - menu_->sizeHint().width(), + pos.y() + buttonSize_)); + }); setLayout(top_layout_); } @@ -106,6 +118,16 @@ void TopRoomBar::paintEvent(QPaintEvent *event) style()->drawPrimitive(QStyle::PE_Widget, &option, &painter, this); } +void TopRoomBar::setRoomSettings(QSharedPointer settings) +{ + roomSettings_ = settings; + + if (roomSettings_->isNotificationsEnabled()) + toggleNotifications_->setText("Disable notifications"); + else + toggleNotifications_->setText("Enable notifications"); +} + TopRoomBar::~TopRoomBar() { }