nheko/src/dialogs/RoomSettings.cpp

218 lines
6.8 KiB
C++
Raw Normal View History

2018-04-30 20:41:47 +02:00
#include "Avatar.h"
#include "Config.h"
#include "FlatButton.h"
#include "Painter.h"
#include "Utils.h"
#include "dialogs/RoomSettings.hpp"
#include <QComboBox>
#include <QLabel>
#include <QPainter>
#include <QPixmap>
#include <QSharedPointer>
#include <QStyleOption>
#include <QVBoxLayout>
using namespace dialogs;
TopSection::TopSection(const RoomInfo &info, const QImage &img, QWidget *parent)
: QWidget{parent}
, info_{std::move(info)}
{
textColor_ = palette().color(QPalette::Text);
avatar_ = utils::scaleImageToPixmap(img, AvatarSize);
}
QSize
TopSection::sizeHint() const
{
QFont font;
font.setPixelSize(18);
return QSize(200, AvatarSize + QFontMetrics(font).ascent() + 6 * Padding);
}
2018-05-08 19:30:09 +02:00
RoomSettings::RoomSettings(const QString &room_id, QWidget *parent)
2018-04-30 20:41:47 +02:00
: QFrame(parent)
, room_id_{std::move(room_id)}
{
setMaximumWidth(385);
try {
info_ = cache::client()->singleRoomInfo(room_id_.toStdString());
2018-04-30 20:41:47 +02:00
2018-05-08 19:30:09 +02:00
setAvatar(QImage::fromData(cache::client()->image(info_.avatar_url)));
2018-04-30 20:41:47 +02:00
} catch (const lmdb::error &e) {
qWarning() << "failed to retrieve room info from cache" << room_id;
}
auto layout = new QVBoxLayout(this);
layout->setSpacing(30);
layout->setMargin(20);
saveBtn_ = new FlatButton("SAVE", this);
saveBtn_->setFontSize(conf::btn::fontSize);
cancelBtn_ = new FlatButton(tr("CANCEL"), this);
cancelBtn_->setFontSize(conf::btn::fontSize);
auto btnLayout = new QHBoxLayout();
btnLayout->setSpacing(0);
btnLayout->setMargin(0);
btnLayout->addStretch(1);
btnLayout->addWidget(saveBtn_);
btnLayout->addWidget(cancelBtn_);
auto notifOptionLayout_ = new QHBoxLayout;
notifOptionLayout_->setMargin(5);
2018-05-01 18:35:28 +02:00
auto notifLabel = new QLabel(tr("Notifications"), this);
auto notifCombo = new QComboBox(this);
notifCombo->setDisabled(true);
notifCombo->addItem(tr("Muted"));
notifCombo->addItem(tr("Mentions only"));
notifCombo->addItem(tr("All messages"));
notifLabel->setStyleSheet("font-size: 15px;");
2018-04-30 20:41:47 +02:00
2018-05-01 18:35:28 +02:00
notifOptionLayout_->addWidget(notifLabel);
2018-04-30 20:41:47 +02:00
notifOptionLayout_->addWidget(notifCombo, 0, Qt::AlignBottom | Qt::AlignRight);
auto accessOptionLayout = new QHBoxLayout();
accessOptionLayout->setMargin(5);
auto accessLabel = new QLabel(tr("Room access"), this);
accessCombo = new QComboBox(this);
accessCombo->addItem(tr("Anyone and guests"));
accessCombo->addItem(tr("Anyone"));
accessCombo->addItem(tr("Invited users"));
accessCombo->setDisabled(true);
accessLabel->setStyleSheet("font-size: 15px;");
if(info_.join_rule == JoinRule::Public)
{
if(info_.guest_access)
{
accessCombo->setCurrentIndex(0);
}
else
{
accessCombo->setCurrentIndex(1);
}
}
else
{
accessCombo->setCurrentIndex(2);
}
accessOptionLayout->addWidget(accessLabel);
accessOptionLayout->addWidget(accessCombo);
2018-04-30 20:41:47 +02:00
layout->addWidget(new TopSection(info_, avatarImg_, this));
layout->addLayout(notifOptionLayout_);
2018-05-01 18:35:28 +02:00
layout->addLayout(notifOptionLayout_);
layout->addLayout(accessOptionLayout);
2018-04-30 20:41:47 +02:00
layout->addLayout(btnLayout);
connect(cancelBtn_, &FlatButton::clicked, this, &RoomSettings::closing);
connect(saveBtn_, &FlatButton::clicked, this, &RoomSettings::save_and_close);
}
void
RoomSettings::save_and_close() {
// TODO: Save access changes to the room
if (accessCombo->currentIndex()<2) {
if(info_.join_rule != JoinRule::Public) {
// Make join_rule Public
}
if(accessCombo->currentIndex()==0) {
if(!info_.guest_access) {
// Make guest_access CanJoin
}
}
}
else {
if(info_.join_rule != JoinRule::Invite) {
// Make join_rule invite
}
if(info_.guest_access) {
// Make guest_access forbidden
}
}
closing();
2018-04-30 20:41:47 +02:00
}
void
RoomSettings::paintEvent(QPaintEvent *)
{
QStyleOption opt;
opt.init(this);
QPainter p(this);
style()->drawPrimitive(QStyle::PE_Widget, &opt, &p, this);
}
void
TopSection::paintEvent(QPaintEvent *)
{
Painter p(this);
PainterHighQualityEnabler hq(p);
constexpr int textPadding = 23;
constexpr int textStartX = AvatarSize + 5 * Padding;
const int availableTextWidth = width() - textStartX;
constexpr int nameFont = 15;
constexpr int membersFont = 14;
constexpr int labelFont = 18;
QFont font;
font.setPixelSize(labelFont);
font.setWeight(70);
p.setFont(font);
p.setPen(textColor());
p.drawTextLeft(Padding, Padding, "Room settings");
p.translate(0, textPadding + QFontMetrics(p.font()).ascent());
p.save();
p.translate(textStartX, 2 * Padding);
// Draw the name.
font.setPixelSize(membersFont);
const auto members = QString("%1 members").arg(info_.member_count);
font.setPixelSize(nameFont);
const auto name = QFontMetrics(font).elidedText(
QString::fromStdString(info_.name), Qt::ElideRight, availableTextWidth - 4 * Padding);
font.setWeight(60);
p.setFont(font);
p.drawTextLeft(0, 0, name);
// Draw the number of members
p.translate(0, QFontMetrics(p.font()).ascent() + 2 * Padding);
font.setPixelSize(membersFont);
font.setWeight(50);
p.setFont(font);
p.drawTextLeft(0, 0, members);
p.restore();
if (avatar_.isNull()) {
font.setPixelSize(AvatarSize / 2);
font.setWeight(60);
p.setFont(font);
p.translate(Padding, Padding);
p.drawLetterAvatar(utils::firstChar(name),
QColor("white"),
QColor("black"),
AvatarSize + Padding,
AvatarSize + Padding,
AvatarSize);
} else {
QRect avatarRegion(Padding, Padding, AvatarSize, AvatarSize);
QPainterPath pp;
pp.addEllipse(avatarRegion.center(), AvatarSize, AvatarSize);
p.setClipPath(pp);
p.drawPixmap(avatarRegion, avatar_);
}
}