matrixion/src/TopRoomBar.cc

183 lines
5.7 KiB
C++
Raw Normal View History

2017-04-06 02:06:42 +03:00
/*
* 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/>.
*/
#include <QStyleOption>
#include "Config.h"
#include "MainWindow.h"
2017-04-06 02:06:42 +03:00
#include "TopRoomBar.h"
TopRoomBar::TopRoomBar(QWidget *parent)
2017-08-20 13:47:22 +03:00
: QWidget(parent)
, buttonSize_{ 32 }
2017-04-06 02:06:42 +03:00
{
2017-08-26 15:36:10 +03:00
setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
setMinimumSize(QSize(0, 65));
2017-09-20 23:17:11 +03:00
setStyleSheet("background-color: #fff; color: #171919;");
2017-08-26 15:36:10 +03:00
topLayout_ = new QHBoxLayout();
topLayout_->setSpacing(10);
topLayout_->setMargin(10);
avatar_ = new Avatar(this);
avatar_->setLetter(QChar('?'));
avatar_->setBackgroundColor(QColor("#d6dde3"));
avatar_->setTextColor(QColor("#555459"));
avatar_->setSize(35);
textLayout_ = new QVBoxLayout();
textLayout_->setSpacing(0);
textLayout_->setContentsMargins(0, 0, 0, 0);
QFont roomFont("Open Sans SemiBold");
roomFont.setPixelSize(conf::topRoomBar::fonts::roomName);
nameLabel_ = new QLabel(this);
nameLabel_->setFont(roomFont);
QFont descriptionFont("Open Sans");
descriptionFont.setPixelSize(conf::topRoomBar::fonts::roomDescription);
topicLabel_ = new QLabel(this);
topicLabel_->setFont(descriptionFont);
topicLabel_->setTextFormat(Qt::RichText);
topicLabel_->setTextInteractionFlags(Qt::TextBrowserInteraction);
topicLabel_->setOpenExternalLinks(true);
textLayout_->addWidget(nameLabel_);
textLayout_->addWidget(topicLabel_);
settingsBtn_ = new FlatButton(this);
settingsBtn_->setFixedSize(buttonSize_, buttonSize_);
settingsBtn_->setCornerRadius(buttonSize_ / 2);
QIcon settings_icon;
2017-10-15 22:08:51 +03:00
settings_icon.addFile(":/icons/icons/ui/vertical-ellipsis.png");
2017-08-26 15:36:10 +03:00
settingsBtn_->setIcon(settings_icon);
settingsBtn_->setIconSize(QSize(buttonSize_ / 2, buttonSize_ / 2));
topLayout_->addWidget(avatar_);
2017-10-19 19:04:51 +03:00
topLayout_->addLayout(textLayout_, 1);
topLayout_->addWidget(settingsBtn_, 0, Qt::AlignRight);
2017-08-26 15:36:10 +03:00
menu_ = new Menu(this);
toggleNotifications_ = new QAction(tr("Disable notifications"), this);
connect(toggleNotifications_, &QAction::triggered, this, [=]() {
roomSettings_->toggleNotifications();
});
leaveRoom_ = new QAction(tr("Leave room"), this);
connect(leaveRoom_, &QAction::triggered, this, [=]() {
2017-10-07 20:09:34 +03:00
if (leaveRoomDialog_.isNull()) {
leaveRoomDialog_ =
QSharedPointer<LeaveRoomDialog>(new LeaveRoomDialog(this));
connect(leaveRoomDialog_.data(),
SIGNAL(closing(bool)),
this,
SLOT(closeLeaveRoomDialog(bool)));
}
if (leaveRoomModal_.isNull()) {
leaveRoomModal_ = QSharedPointer<OverlayModal>(
new OverlayModal(MainWindow::instance(), leaveRoomDialog_.data()));
leaveRoomModal_->setDuration(0);
leaveRoomModal_->setColor(QColor(30, 30, 30, 170));
}
leaveRoomModal_->fadeIn();
});
2017-08-26 15:36:10 +03:00
menu_->addAction(toggleNotifications_);
menu_->addAction(leaveRoom_);
2017-08-26 15:36:10 +03:00
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_));
});
setLayout(topLayout_);
2017-04-06 02:06:42 +03:00
}
void
TopRoomBar::closeLeaveRoomDialog(bool leaving)
{
2017-10-07 20:09:34 +03:00
leaveRoomModal_->fadeOut();
if (leaving) {
emit leaveRoom();
}
}
2017-08-20 13:47:22 +03:00
void
TopRoomBar::updateRoomAvatarFromName(const QString &name)
{
2017-08-26 15:36:10 +03:00
QChar letter = '?';
2017-08-26 15:36:10 +03:00
if (name.size() > 0)
letter = name[0];
2017-08-26 15:36:10 +03:00
avatar_->setLetter(letter);
2017-09-30 20:43:57 +03:00
update();
}
2017-08-20 13:47:22 +03:00
void
TopRoomBar::reset()
{
2017-08-26 15:36:10 +03:00
nameLabel_->setText("");
topicLabel_->setText("");
avatar_->setLetter(QChar('?'));
2017-10-19 19:04:51 +03:00
roomName_.clear();
roomTopic_.clear();
}
2017-08-20 13:47:22 +03:00
void
TopRoomBar::paintEvent(QPaintEvent *event)
2017-04-06 02:06:42 +03:00
{
2017-08-26 15:36:10 +03:00
Q_UNUSED(event);
2017-04-06 02:06:42 +03:00
2017-08-26 15:36:10 +03:00
QStyleOption option;
option.initFrom(this);
2017-04-06 02:06:42 +03:00
2017-08-26 15:36:10 +03:00
QPainter painter(this);
style()->drawPrimitive(QStyle::PE_Widget, &option, &painter, this);
2017-10-19 19:04:51 +03:00
QString elidedText =
QFontMetrics(nameLabel_->font()).elidedText(roomName_, Qt::ElideRight, width());
nameLabel_->setText(elidedText);
elidedText =
QFontMetrics(topicLabel_->font()).elidedText(roomTopic_, Qt::ElideRight, width());
topicLabel_->setText(elidedText);
2017-04-06 02:06:42 +03:00
}
2017-08-20 13:47:22 +03:00
void
TopRoomBar::setRoomSettings(QSharedPointer<RoomSettings> settings)
2017-05-31 17:06:03 +03:00
{
2017-08-26 15:36:10 +03:00
roomSettings_ = settings;
2017-05-31 17:06:03 +03:00
}
2017-10-01 12:11:33 +03:00
TopRoomBar::~TopRoomBar() {}