From ae7b47342db165d79986c34cba315f424392b018 Mon Sep 17 00:00:00 2001 From: Diadlo Date: Sun, 8 Oct 2017 00:11:46 +0300 Subject: [PATCH] refactor(profile): Move core connect from form to model --- src/model/profile/iprofileinfo.h | 5 +++++ src/model/profile/profileinfo.cpp | 9 ++++++--- src/model/profile/profileinfo.h | 5 +++-- src/widget/form/profileform.cpp | 17 ++++++++--------- src/widget/form/profileform.h | 1 - src/widget/widget.cpp | 4 ++-- 6 files changed, 24 insertions(+), 17 deletions(-) diff --git a/src/model/profile/iprofileinfo.h b/src/model/profile/iprofileinfo.h index 1fdd31a71..0c955f6ec 100644 --- a/src/model/profile/iprofileinfo.h +++ b/src/model/profile/iprofileinfo.h @@ -57,4 +57,9 @@ public: virtual SetAvatarResult setAvatar(const QString& path) = 0; virtual void removeAvatar() = 0; + +signals: + void idChanged(const ToxId& id); + void usernameChanged(const QString& username); + void statusMessageChanged(const QString& message); }; diff --git a/src/model/profile/profileinfo.cpp b/src/model/profile/profileinfo.cpp index 9032e6d38..66dee48c1 100644 --- a/src/model/profile/profileinfo.cpp +++ b/src/model/profile/profileinfo.cpp @@ -37,13 +37,17 @@ /** * @brief ProfileInfo constructor. + * @param core Pointer to Tox Core. * @param profile Pointer to Profile. * @note All pointers parameters shouldn't be null. */ - -ProfileInfo::ProfileInfo(Profile *profile) +ProfileInfo::ProfileInfo(Core* core, Profile *profile) : profile{profile} + , core{core} { + connect(core, &Core::idSet, this, &ProfileInfo::idChanged); + connect(core, &Core::usernameSet, this, &ProfileInfo::usernameChanged); + connect(core, &Core::statusMessageSet, this, &ProfileInfo::usernameChanged); } /** @@ -324,7 +328,6 @@ IProfileInfo::SetAvatarResult ProfileInfo::setAvatar(const QString &path) return SetAvatarResult::TooLarge; } - Core* core = Core::getInstance(); profile->setAvatar(bytes, core->getSelfPublicKey()); return SetAvatarResult::OK; } diff --git a/src/model/profile/profileinfo.h b/src/model/profile/profileinfo.h index 3786e9270..3e5a64950 100644 --- a/src/model/profile/profileinfo.h +++ b/src/model/profile/profileinfo.h @@ -19,14 +19,14 @@ #include "iprofileinfo.h" -class ToxId; +class Core; class QPoint; class Profile; class ProfileInfo : public IProfileInfo { public: - ProfileInfo(Profile* profile); + ProfileInfo(Core* core, Profile* profile); bool setPassword(const QString& password) override; bool deletePassword() override; @@ -51,4 +51,5 @@ public: private: Profile* const profile; + Core* const core; }; diff --git a/src/widget/form/profileform.cpp b/src/widget/form/profileform.cpp index 65f40577d..5cc10e911 100644 --- a/src/widget/form/profileform.cpp +++ b/src/widget/form/profileform.cpp @@ -105,7 +105,6 @@ ProfileForm::ProfileForm(IProfileInfo* profileInfo, QWidget* parent) { bodyUI = new Ui::IdentitySettings; bodyUI->setupUi(this); - core = Core::getInstance(); const uint32_t maxNameLength = tox_max_name_length(); const QString toolTip = tr("Tox user names cannot exceed %1 characters.").arg(maxNameLength); @@ -161,8 +160,7 @@ ProfileForm::ProfileForm(IProfileInfo* profileInfo, QWidget* parent) connect(bodyUI->toxIdLabel, &CroppingLabel::clicked, this, &ProfileForm::copyIdClicked); connect(toxId, &ClickableTE::clicked, this, &ProfileForm::copyIdClicked); - // TODO: Move to model - connect(core, &Core::idSet, this, &ProfileForm::setToxId); + connect(profileInfo, &IProfileInfo::idChanged, this, &ProfileForm::setToxId); connect(bodyUI->userName, &QLineEdit::editingFinished, this, &ProfileForm::onUserNameEdited); connect(bodyUI->statusMessage, &QLineEdit::editingFinished, this, &ProfileForm::onStatusMessageEdited); @@ -185,10 +183,9 @@ ProfileForm::ProfileForm(IProfileInfo* profileInfo, QWidget* parent) connect(bodyUI->toxmeUpdateButton, &QPushButton::clicked, this, &ProfileForm::onRegisterButtonClicked); - // TODO: Move to model - connect(core, &Core::usernameSet, this, + connect(profileInfo, &IProfileInfo::usernameChanged, this, [=](const QString& val) { bodyUI->userName->setText(val); }); - connect(core, &Core::statusMessageSet, this, + connect(profileInfo, &IProfileInfo::statusMessageChanged, this, [=](const QString& val) { bodyUI->statusMessage->setText(val); }); for (QComboBox* cb : findChildren()) { @@ -296,8 +293,10 @@ void ProfileForm::onSelfAvatarLoaded(const QPixmap& pic) void ProfileForm::setToxId(const ToxId& id) { - auto idString = id.toString(); - static const QString ToxIdColor = QStringLiteral("%1%2%3"); + QString idString = id.toString(); + static const QString ToxIdColor = QStringLiteral("%1" + "%2" + "%3"); toxId->setText(ToxIdColor .arg(idString.mid(0, 64)) .arg(idString.mid(64, 8)) @@ -305,7 +304,7 @@ void ProfileForm::setToxId(const ToxId& id) delete qr; qr = new QRWidget(); - qr->setQRData("tox:" + id.toString()); + qr->setQRData("tox:" + idString); bodyUI->qrCode->setPixmap(QPixmap::fromImage(qr->getImage()->scaledToWidth(150))); } diff --git a/src/widget/form/profileform.h b/src/widget/form/profileform.h index 3851b2a21..17cbddc82 100644 --- a/src/widget/form/profileform.h +++ b/src/widget/form/profileform.h @@ -93,7 +93,6 @@ private: void refreshProfiles(); Ui::IdentitySettings* bodyUI; MaskablePixmapWidget* profilePicture; - Core* core; QTimer timer; bool hasCheck = false; QRWidget* qr; diff --git a/src/widget/widget.cpp b/src/widget/widget.cpp index b1a3f64dd..915bed760 100644 --- a/src/widget/widget.cpp +++ b/src/widget/widget.cpp @@ -231,8 +231,9 @@ void Widget::init() addFriendForm = new AddFriendForm; groupInviteForm = new GroupInviteForm; + Core* core = Nexus::getCore(); Profile* profile = Nexus::getProfile(); - profileInfo = new ProfileInfo(profile); + profileInfo = new ProfileInfo(core, profile); profileForm = new ProfileForm(profileInfo); // connect logout tray menu action @@ -241,7 +242,6 @@ void Widget::init() connect(profile, &Profile::selfAvatarChanged, profileForm, &ProfileForm::onSelfAvatarLoaded); const Settings& s = Settings::getInstance(); - Core* core = Nexus::getCore(); CoreAV* av = core->getAv(); connect(av, &CoreAV::avEnd, this, &Widget::onCallEnd);