Add menu to toggle notifications

This commit is contained in:
Konstantinos Sideris 2017-05-31 17:06:03 +03:00
parent 5197f8a886
commit c184362044
9 changed files with 163 additions and 31 deletions

View File

@ -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<QString, RoomState> state_manager_;
QMap<QString, QSharedPointer<RoomSettings>> settingsManager_;
// Matrix Client API provider.
QSharedPointer<MatrixClient> client_;

55
include/RoomSettings.h Normal file
View File

@ -0,0 +1,55 @@
/*
* nheko Copyright (C) 2017 Konstantinos Sideris <siderisk@auth.gr>
*
* 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 <http://www.gnu.org/licenses/>.
*/
#pragma once
#include <QSettings>
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_;
};

View File

@ -17,15 +17,20 @@
#pragma once
#include <QAction>
#include <QDebug>
#include <QIcon>
#include <QImage>
#include <QLabel>
#include <QPaintEvent>
#include <QSharedPointer>
#include <QVBoxLayout>
#include <QWidget>
#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<RoomSettings> settings);
void reset();
@ -52,10 +58,16 @@ private:
QLabel *name_label_;
QLabel *topic_label_;
FlatButton *search_button_;
FlatButton *settings_button_;
QSharedPointer<RoomSettings> roomSettings_;
QMenu *menu_;
QAction *toggleNotifications_;
FlatButton *settingsBtn_;
Avatar *avatar_;
int buttonSize_;
};
inline void TopRoomBar::updateRoomAvatar(const QImage &avatar_image)

25
include/ui/Menu.h Normal file
View File

@ -0,0 +1,25 @@
#pragma once
#include <QMenu>
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();
}
};

Binary file not shown.

After

Width:  |  Height:  |  Size: 509 B

View File

@ -22,6 +22,7 @@
<file>icons/emoji-categories/symbols.png</file>
<file>icons/emoji-categories/flags.png</file>
<file>icons/vertical-ellipsis.png</file>
</qresource>
<qresource prefix="/logos">

View File

@ -127,10 +127,16 @@ ChatPage::ChatPage(QSharedPointer<MatrixClient> 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<RoomSettings>(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());

View File

@ -28,7 +28,7 @@
EmojiPanel::EmojiPanel(QWidget *parent)
: QWidget(parent)
, shadowMargin_{3}
, shadowMargin_{2}
, width_{370}
, height_{350}
, animationDuration_{100}

View File

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