From 19626dbb105e31bc3bedaa8921e846e366b3a5ef Mon Sep 17 00:00:00 2001 From: Konstantinos Sideris Date: Sat, 8 Sep 2018 12:29:49 +0300 Subject: [PATCH] Small refactoring on the EditModal --- src/dialogs/RoomSettings.cpp | 122 ++++++++++++++++------------------- src/dialogs/RoomSettings.h | 25 ++++++- 2 files changed, 80 insertions(+), 67 deletions(-) diff --git a/src/dialogs/RoomSettings.cpp b/src/dialogs/RoomSettings.cpp index 4e56b13..a9739f3 100644 --- a/src/dialogs/RoomSettings.cpp +++ b/src/dialogs/RoomSettings.cpp @@ -90,77 +90,69 @@ EditModal::EditModal(const QString &roomId, QWidget *parent) 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(); + connect(applyBtn_, &QPushButton::clicked, this, &EditModal::applyClicked); + connect(cancelBtn_, &QPushButton::clicked, this, &EditModal::close); - errorField_->hide(); + auto window = QApplication::activeWindow(); + auto center = window->frameGeometry().center(); + move(center.x() - (width() * 0.5), center.y() - (height() * 0.5)); +} - if (newName == initialName_ && newTopic == initialTopic_) { - close(); - return; - } +void +EditModal::applyClicked() +{ + // Check if the values are changed from the originals. + auto newName = nameInput_->text().trimmed(); + auto newTopic = topicInput_->text().trimmed(); - using namespace mtx::events; - auto proxy = std::make_shared(); - connect(proxy.get(), &ThreadProxy::topicEventSent, this, [this]() { - errorField_->hide(); - close(); - }); - connect( - proxy.get(), &ThreadProxy::nameEventSent, this, [this](const QString &newName) { - errorField_->hide(); - emit nameChanged(newName); - close(); - }); - connect(proxy.get(), &ThreadProxy::error, this, [this](const QString &msg) { - errorField_->setText(msg); - errorField_->show(); - }); + errorField_->hide(); - if (newName != initialName_ && !newName.isEmpty()) { - state::Name body; - body.name = newName.toStdString(); - - http::client()->send_state_event( - roomId_.toStdString(), - body, - [proxy, newName](const mtx::responses::EventId &, - mtx::http::RequestErr err) { - if (err) { - emit proxy->error( - QString::fromStdString(err->matrix_error.error)); - return; - } - - emit proxy->nameEventSent(newName); - }); - } + if (newName == initialName_ && newTopic == initialTopic_) { + close(); + return; + } - if (newTopic != initialTopic_ && !newTopic.isEmpty()) { - state::Topic body; - body.topic = newTopic.toStdString(); - - http::client()->send_state_event( - roomId_.toStdString(), - body, - [proxy](const mtx::responses::EventId &, mtx::http::RequestErr err) { - if (err) { - emit proxy->error( - QString::fromStdString(err->matrix_error.error)); - return; - } - - emit proxy->topicEventSent(); - }); - } - }); - connect(cancelBtn_, &QPushButton::clicked, this, &EditModal::close); + using namespace mtx::events; + auto proxy = std::make_shared(); + connect(proxy.get(), &ThreadProxy::topicEventSent, this, &EditModal::topicEventSent); + connect(proxy.get(), &ThreadProxy::nameEventSent, this, &EditModal::nameEventSent); + connect(proxy.get(), &ThreadProxy::error, this, &EditModal::error); + + if (newName != initialName_ && !newName.isEmpty()) { + state::Name body; + body.name = newName.toStdString(); + + http::client()->send_state_event( + roomId_.toStdString(), + body, + [proxy, newName](const mtx::responses::EventId &, mtx::http::RequestErr err) { + if (err) { + emit proxy->error( + QString::fromStdString(err->matrix_error.error)); + return; + } + + emit proxy->nameEventSent(newName); + }); + } - auto window = QApplication::activeWindow(); - auto center = window->frameGeometry().center(); - move(center.x() - (width() * 0.5), center.y() - (height() * 0.5)); + if (newTopic != initialTopic_ && !newTopic.isEmpty()) { + state::Topic body; + body.topic = newTopic.toStdString(); + + http::client()->send_state_event( + roomId_.toStdString(), + body, + [proxy](const mtx::responses::EventId &, mtx::http::RequestErr err) { + if (err) { + emit proxy->error( + QString::fromStdString(err->matrix_error.error)); + return; + } + + emit proxy->topicEventSent(); + }); + } } void diff --git a/src/dialogs/RoomSettings.h b/src/dialogs/RoomSettings.h index dd72925..c2c81cd 100644 --- a/src/dialogs/RoomSettings.h +++ b/src/dialogs/RoomSettings.h @@ -3,6 +3,7 @@ #include #include #include +#include #include "Cache.h" @@ -12,8 +13,6 @@ class QComboBox; class QHBoxLayout; class QShowEvent; class LoadingIndicator; -class QLabel; -class QLabel; class QLayout; class QPixmap; class TextField; @@ -69,6 +68,28 @@ public: signals: void nameChanged(const QString &roomName); +private slots: + void topicEventSent() + { + errorField_->hide(); + close(); + } + + void nameEventSent(const QString &name) + { + errorField_->hide(); + emit nameChanged(name); + close(); + } + + void error(const QString &msg) + { + errorField_->setText(msg); + errorField_->show(); + } + + void applyClicked(); + private: QString roomId_; QString initialName_;