Small refactoring on the EditModal

This commit is contained in:
Konstantinos Sideris 2018-09-08 12:29:49 +03:00
parent 079faa67e6
commit 19626dbb10
2 changed files with 81 additions and 68 deletions

View file

@ -90,7 +90,17 @@ EditModal::EditModal(const QString &roomId, QWidget *parent)
labelLayout->addWidget(errorField_); labelLayout->addWidget(errorField_);
layout->addLayout(labelLayout); layout->addLayout(labelLayout);
connect(applyBtn_, &QPushButton::clicked, [this]() { connect(applyBtn_, &QPushButton::clicked, this, &EditModal::applyClicked);
connect(cancelBtn_, &QPushButton::clicked, this, &EditModal::close);
auto window = QApplication::activeWindow();
auto center = window->frameGeometry().center();
move(center.x() - (width() * 0.5), center.y() - (height() * 0.5));
}
void
EditModal::applyClicked()
{
// Check if the values are changed from the originals. // Check if the values are changed from the originals.
auto newName = nameInput_->text().trimmed(); auto newName = nameInput_->text().trimmed();
auto newTopic = topicInput_->text().trimmed(); auto newTopic = topicInput_->text().trimmed();
@ -104,20 +114,9 @@ EditModal::EditModal(const QString &roomId, QWidget *parent)
using namespace mtx::events; using namespace mtx::events;
auto proxy = std::make_shared<ThreadProxy>(); auto proxy = std::make_shared<ThreadProxy>();
connect(proxy.get(), &ThreadProxy::topicEventSent, this, [this]() { connect(proxy.get(), &ThreadProxy::topicEventSent, this, &EditModal::topicEventSent);
errorField_->hide(); connect(proxy.get(), &ThreadProxy::nameEventSent, this, &EditModal::nameEventSent);
close(); connect(proxy.get(), &ThreadProxy::error, this, &EditModal::error);
});
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();
});
if (newName != initialName_ && !newName.isEmpty()) { if (newName != initialName_ && !newName.isEmpty()) {
state::Name body; state::Name body;
@ -126,8 +125,7 @@ EditModal::EditModal(const QString &roomId, QWidget *parent)
http::client()->send_state_event<state::Name, EventType::RoomName>( http::client()->send_state_event<state::Name, EventType::RoomName>(
roomId_.toStdString(), roomId_.toStdString(),
body, body,
[proxy, newName](const mtx::responses::EventId &, [proxy, newName](const mtx::responses::EventId &, mtx::http::RequestErr err) {
mtx::http::RequestErr err) {
if (err) { if (err) {
emit proxy->error( emit proxy->error(
QString::fromStdString(err->matrix_error.error)); QString::fromStdString(err->matrix_error.error));
@ -155,12 +153,6 @@ EditModal::EditModal(const QString &roomId, QWidget *parent)
emit proxy->topicEventSent(); emit proxy->topicEventSent();
}); });
} }
});
connect(cancelBtn_, &QPushButton::clicked, this, &EditModal::close);
auto window = QApplication::activeWindow();
auto center = window->frameGeometry().center();
move(center.x() - (width() * 0.5), center.y() - (height() * 0.5));
} }
void void

View file

@ -3,6 +3,7 @@
#include <QEvent> #include <QEvent>
#include <QFrame> #include <QFrame>
#include <QImage> #include <QImage>
#include <QLabel>
#include "Cache.h" #include "Cache.h"
@ -12,8 +13,6 @@ class QComboBox;
class QHBoxLayout; class QHBoxLayout;
class QShowEvent; class QShowEvent;
class LoadingIndicator; class LoadingIndicator;
class QLabel;
class QLabel;
class QLayout; class QLayout;
class QPixmap; class QPixmap;
class TextField; class TextField;
@ -69,6 +68,28 @@ public:
signals: signals:
void nameChanged(const QString &roomName); 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: private:
QString roomId_; QString roomId_;
QString initialName_; QString initialName_;