diff --git a/src/dialogs/RoomSettings.cpp b/src/dialogs/RoomSettings.cpp
index 9bf4cf26..4a7ea4fc 100644
--- a/src/dialogs/RoomSettings.cpp
+++ b/src/dialogs/RoomSettings.cpp
@@ -1,7 +1,9 @@
#include "Avatar.h"
#include "Config.h"
#include "FlatButton.h"
+#include "MatrixClient.h"
#include "Painter.h"
+#include "TextField.h"
#include "Utils.h"
#include "dialogs/RoomSettings.hpp"
@@ -15,12 +17,146 @@
using namespace dialogs;
+EditModal::EditModal(const QString &roomId, QWidget *parent)
+ : QWidget(parent)
+ , roomId_{roomId}
+{
+ setMinimumWidth(360);
+ setAutoFillBackground(true);
+ setAttribute(Qt::WA_DeleteOnClose, true);
+ setWindowFlags(Qt::Tool | Qt::WindowStaysOnTopHint);
+ setWindowModality(Qt::WindowModal);
+
+ auto layout = new QVBoxLayout(this);
+
+ applyBtn_ = new FlatButton(tr("APPLY"), this);
+ applyBtn_->setFontSize(conf::btn::fontSize);
+ cancelBtn_ = new FlatButton(tr("CANCEL"), this);
+ cancelBtn_->setFontSize(conf::btn::fontSize);
+
+ auto btnLayout = new QHBoxLayout;
+ btnLayout->setContentsMargins(5, 20, 5, 5);
+ btnLayout->addWidget(applyBtn_);
+ btnLayout->addWidget(cancelBtn_);
+
+ nameInput_ = new TextField(this);
+ nameInput_->setLabel(tr("Name"));
+ topicInput_ = new TextField(this);
+ topicInput_->setLabel(tr("Topic"));
+
+ QFont font;
+ font.setPixelSize(conf::modals::errorFont);
+
+ errorField_ = new QLabel(this);
+ errorField_->setFont(font);
+ errorField_->setWordWrap(true);
+ errorField_->hide();
+
+ layout->addWidget(nameInput_);
+ layout->addWidget(topicInput_);
+ layout->addLayout(btnLayout, 1);
+
+ auto labelLayout = new QHBoxLayout;
+ labelLayout->setAlignment(Qt::AlignHCenter);
+ labelLayout->addWidget(errorField_);
+ layout->addLayout(labelLayout);
+
+ connect(applyBtn_, &QPushButton::clicked, [this]() {
+ // Check if the values are changed from the originals.
+ auto newName = nameInput_->text().trimmed();
+ auto newTopic = topicInput_->text().trimmed();
+
+ errorField_->hide();
+
+ if (newName == initialName_ && newTopic == initialTopic_) {
+ close();
+ return;
+ }
+
+ using namespace mtx::events;
+
+ if (newName != initialName_ && !newName.isEmpty()) {
+ state::Name body;
+ body.name = newName.toStdString();
+
+ auto proxy =
+ http::client()->sendStateEvent<state::Name, EventType::RoomName>(body,
+ roomId_);
+ connect(proxy.get(),
+ &StateEventProxy::stateEventSent,
+ this,
+ [this, proxy, newName]() {
+ proxy->deleteLater();
+ errorField_->hide();
+ emit nameChanged(newName);
+ close();
+ });
+
+ connect(proxy.get(),
+ &StateEventProxy::stateEventError,
+ this,
+ [this, proxy, newName](const QString &msg) {
+ proxy->deleteLater();
+ errorField_->setText(msg);
+ errorField_->show();
+ });
+ }
+
+ if (newTopic != initialTopic_ && !newTopic.isEmpty()) {
+ state::Topic body;
+ body.topic = newTopic.toStdString();
+
+ auto proxy =
+ http::client()->sendStateEvent<state::Topic, EventType::RoomTopic>(
+ body, roomId_);
+ connect(proxy.get(),
+ &StateEventProxy::stateEventSent,
+ this,
+ [this, proxy, newTopic]() {
+ proxy->deleteLater();
+ errorField_->hide();
+ close();
+ });
+
+ connect(proxy.get(),
+ &StateEventProxy::stateEventError,
+ this,
+ [this, proxy, newTopic](const QString &msg) {
+ proxy->deleteLater();
+ errorField_->setText(msg);
+ errorField_->show();
+ });
+ }
+ });
+ connect(cancelBtn_, &QPushButton::clicked, this, &EditModal::close);
+}
+
+void
+EditModal::setFields(const QString &roomName, const QString &roomTopic)
+{
+ initialName_ = roomName;
+ initialTopic_ = roomTopic;
+
+ nameInput_->setText(roomName);
+ topicInput_->setText(roomTopic);
+}
+
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);
+
+ QSizePolicy policy(QSizePolicy::Minimum, QSizePolicy::Minimum);
+ setSizePolicy(policy);
+}
+
+void
+TopSection::setRoomName(const QString &name)
+{
+ info_.name = name.toStdString();
+ update();
}
QSize
@@ -28,14 +164,14 @@ TopSection::sizeHint() const
{
QFont font;
font.setPixelSize(18);
- return QSize(200, AvatarSize + QFontMetrics(font).ascent() + 6 * Padding);
+ return QSize(340, AvatarSize + QFontMetrics(font).ascent());
}
RoomSettings::RoomSettings(const QString &room_id, QWidget *parent)
: QFrame(parent)
, room_id_{std::move(room_id)}
{
- setMaximumWidth(385);
+ setMaximumWidth(420);
try {
info_ = cache::client()->singleRoomInfo(room_id_.toStdString());
@@ -45,8 +181,10 @@ RoomSettings::RoomSettings(const QString &room_id, QWidget *parent)
qWarning() << "failed to retrieve room info from cache" << room_id;
}
+ constexpr int SettingsMargin = 2;
+
auto layout = new QVBoxLayout(this);
- layout->setSpacing(30);
+ layout->setSpacing(15);
layout->setMargin(20);
saveBtn_ = new FlatButton("SAVE", this);
@@ -62,7 +200,7 @@ RoomSettings::RoomSettings(const QString &room_id, QWidget *parent)
btnLayout->addWidget(cancelBtn_);
auto notifOptionLayout_ = new QHBoxLayout;
- notifOptionLayout_->setMargin(5);
+ notifOptionLayout_->setMargin(SettingsMargin);
auto notifLabel = new QLabel(tr("Notifications"), this);
auto notifCombo = new QComboBox(this);
notifCombo->setDisabled(true);
@@ -75,62 +213,92 @@ RoomSettings::RoomSettings(const QString &room_id, QWidget *parent)
notifOptionLayout_->addWidget(notifCombo, 0, Qt::AlignBottom | Qt::AlignRight);
auto accessOptionLayout = new QHBoxLayout();
- accessOptionLayout->setMargin(5);
+ accessOptionLayout->setMargin(SettingsMargin);
auto accessLabel = new QLabel(tr("Room access"), this);
- accessCombo = new QComboBox(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)
- {
+ if (info_.join_rule == JoinRule::Public) {
+ if (info_.guest_access) {
accessCombo->setCurrentIndex(0);
- }
- else
- {
+ } else {
accessCombo->setCurrentIndex(1);
}
- }
- else
- {
+ } else {
accessCombo->setCurrentIndex(2);
}
accessOptionLayout->addWidget(accessLabel);
accessOptionLayout->addWidget(accessCombo);
- layout->addWidget(new TopSection(info_, avatarImg_, this));
- layout->addLayout(notifOptionLayout_);
+ QFont font;
+ font.setPixelSize(18);
+ font.setWeight(70);
+
+ auto menuLabel = new QLabel("Room Settings", this);
+ menuLabel->setFont(font);
+
+ constexpr int buttonSize = 36;
+ constexpr int iconSize = buttonSize / 2;
+
+ QIcon editIcon;
+ editIcon.addFile(":/icons/icons/ui/edit.svg");
+ editFieldsBtn_ = new FlatButton(this);
+ editFieldsBtn_->setFixedSize(buttonSize, buttonSize);
+ editFieldsBtn_->setCornerRadius(iconSize);
+ editFieldsBtn_->setIcon(editIcon);
+ editFieldsBtn_->setIcon(editIcon);
+ editFieldsBtn_->setIconSize(QSize(iconSize, iconSize));
+
+ connect(editFieldsBtn_, &QPushButton::clicked, this, [this]() {
+ auto modal = new EditModal(room_id_, this->parentWidget());
+ modal->setFields(QString::fromStdString(info_.name),
+ QString::fromStdString(info_.topic));
+ modal->show();
+ connect(modal, &EditModal::nameChanged, this, [this](const QString &newName) {
+ topSection_->setRoomName(newName);
+ });
+ });
+
+ topSection_ = new TopSection(info_, avatarImg_, this);
+
+ auto editLayout = new QHBoxLayout;
+ editLayout->setMargin(0);
+ editLayout->addWidget(topSection_);
+ editLayout->addWidget(editFieldsBtn_, 0, Qt::AlignRight | Qt::AlignTop);
+
+ layout->addWidget(menuLabel);
+ layout->addLayout(editLayout);
layout->addLayout(notifOptionLayout_);
layout->addLayout(accessOptionLayout);
layout->addLayout(btnLayout);
- connect(cancelBtn_, &FlatButton::clicked, this, &RoomSettings::closing);
- connect(saveBtn_, &FlatButton::clicked, this, &RoomSettings::save_and_close);
+ connect(cancelBtn_, &QPushButton::clicked, this, &RoomSettings::closing);
+ connect(saveBtn_, &QPushButton::clicked, this, &RoomSettings::saveSettings);
}
void
-RoomSettings::save_and_close() {
+RoomSettings::saveSettings()
+{
// TODO: Save access changes to the room
- if (accessCombo->currentIndex()<2) {
- if(info_.join_rule != JoinRule::Public) {
+ if (accessCombo->currentIndex() < 2) {
+ if (info_.join_rule != JoinRule::Public) {
// Make join_rule Public
}
- if(accessCombo->currentIndex()==0) {
- if(!info_.guest_access) {
+ if (accessCombo->currentIndex() == 0) {
+ if (!info_.guest_access) {
// Make guest_access CanJoin
}
}
- }
- else {
- if(info_.join_rule != JoinRule::Invite) {
+ } else {
+ if (info_.join_rule != JoinRule::Invite) {
// Make join_rule invite
}
- if(info_.guest_access) {
+ if (info_.guest_access) {
// Make guest_access forbidden
}
}
@@ -152,27 +320,18 @@ 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.setPen(textColor());
p.translate(textStartX, 2 * Padding);
// Draw the name.
+ QFont font;
font.setPixelSize(membersFont);
const auto members = QString("%1 members").arg(info_.member_count);
|