Browse Source

fix(UI): Centre message boxes on main window

Setting the parent of MessageBoxManager to Widget has a dual effect of
centreing the QMessageBox's on Widget' window which we want, but also
giving ownership of MessageBoxManager to Widget, which causes Widget to
destroy it in Widget's destructor. Since the original MessageBoxManager
must outlive Widget to be used in Settings and when loading Profiles
before Widget is constructed, we don't want Widget to destroy it.

Instead of juggling MessageBoxManager's parent around dynamically when
Nexus changes the active Window, just construct a second one in Widget
to be used by all its children with the parent set on Widget, centreing
the window and allowing it to have ownership.

Settings and Profile still use the original parent-less
MessageBoxManager since they show popups before Widget is constructed.
reviewable/pr6598/r1
Anthony Bilinski 3 years ago
parent
commit
c5fdb78676
No known key found for this signature in database
GPG Key ID: 2AA8E0DA1B31FB3C
  1. 2
      src/main.cpp
  2. 2
      src/nexus.cpp
  3. 13
      src/persistence/profile.cpp
  4. 5
      src/persistence/profile.h
  5. 5
      src/widget/tool/messageboxmanager.cpp
  6. 1
      src/widget/tool/messageboxmanager.h
  7. 24
      src/widget/widget.cpp
  8. 5
      src/widget/widget.h

2
src/main.cpp

@ -237,7 +237,7 @@ int main(int argc, char* argv[])
qWarning() << "Couldn't load font"; qWarning() << "Couldn't load font";
} }
messageBoxManager = std::unique_ptr<MessageBoxManager>(new MessageBoxManager()); messageBoxManager = std::unique_ptr<MessageBoxManager>(new MessageBoxManager(nullptr));
settings = std::unique_ptr<Settings>(new Settings(*messageBoxManager)); settings = std::unique_ptr<Settings>(new Settings(*messageBoxManager));
QString locale = settings->getTranslation(); QString locale = settings->getTranslation();

2
src/nexus.cpp

@ -235,7 +235,7 @@ void Nexus::showMainGUI()
assert(profile); assert(profile);
// Create GUI // Create GUI
widget = new Widget(*profile, *audioControl, *cameraSource, *settings, *style, *messageBoxManager); widget = new Widget(*profile, *audioControl, *cameraSource, *settings, *style);
// Start GUI // Start GUI
widget->init(); widget->init();

13
src/persistence/profile.cpp

@ -295,14 +295,13 @@ void Profile::initCore(const QByteArray& toxsave, Settings& s, bool isNewProfile
} }
Profile::Profile(const QString& name_, std::unique_ptr<ToxEncrypt> passkey_, Paths& paths_, Profile::Profile(const QString& name_, std::unique_ptr<ToxEncrypt> passkey_, Paths& paths_,
Settings& settings_, IMessageBoxManager& messageBoxManager_) Settings& settings_)
: name{name_} : name{name_}
, passkey{std::move(passkey_)} , passkey{std::move(passkey_)}
, isRemoved{false} , isRemoved{false}
, encrypted{passkey != nullptr} , encrypted{passkey != nullptr}
, paths{paths_} , paths{paths_}
, settings{settings_} , settings{settings_}
, messageBoxManager{messageBoxManager_}
{} {}
/** /**
@ -337,14 +336,14 @@ Profile* Profile::loadProfile(const QString& name, const QString& password, Sett
return nullptr; return nullptr;
} }
Profile* p = new Profile(name, std::move(tmpKey), paths, settings, messageBoxManager); Profile* p = new Profile(name, std::move(tmpKey), paths, settings);
// Core settings are saved per profile, need to load them before starting Core // Core settings are saved per profile, need to load them before starting Core
constexpr bool isNewProfile = false; constexpr bool isNewProfile = false;
settings.updateProfileData(p, parser, isNewProfile); settings.updateProfileData(p, parser, isNewProfile);
p->initCore(toxsave, settings, isNewProfile, cameraSource); p->initCore(toxsave, settings, isNewProfile, cameraSource);
p->loadDatabase(password); p->loadDatabase(password, messageBoxManager);
return p; return p;
} }
@ -371,13 +370,13 @@ Profile* Profile::createProfile(const QString& name, const QString& password, Se
} }
settings.createPersonal(name); settings.createPersonal(name);
Profile* p = new Profile(name, std::move(tmpKey), paths, settings, messageBoxManager); Profile* p = new Profile(name, std::move(tmpKey), paths, settings);
constexpr bool isNewProfile = true; constexpr bool isNewProfile = true;
settings.updateProfileData(p, parser, isNewProfile); settings.updateProfileData(p, parser, isNewProfile);
p->initCore(QByteArray(), settings, isNewProfile, cameraSource); p->initCore(QByteArray(), settings, isNewProfile, cameraSource);
p->loadDatabase(password); p->loadDatabase(password, messageBoxManager);
return p; return p;
} }
@ -621,7 +620,7 @@ QByteArray Profile::loadAvatarData(const ToxPk& owner)
return pic; return pic;
} }
void Profile::loadDatabase(QString password) void Profile::loadDatabase(QString password, IMessageBoxManager& messageBoxManager)
{ {
assert(core); assert(core);

5
src/persistence/profile.h

@ -99,7 +99,7 @@ public slots:
void onRequestSent(const ToxPk& friendPk, const QString& message); void onRequestSent(const ToxPk& friendPk, const QString& message);
private slots: private slots:
void loadDatabase(QString password); void loadDatabase(QString password, IMessageBoxManager& messageBoxManager);
void saveAvatar(const ToxPk& owner, const QByteArray& avatar); void saveAvatar(const ToxPk& owner, const QByteArray& avatar);
void removeAvatar(const ToxPk& owner); void removeAvatar(const ToxPk& owner);
void onSaveToxSave(); void onSaveToxSave();
@ -108,7 +108,7 @@ private slots:
private: private:
Profile(const QString& name_, std::unique_ptr<ToxEncrypt> passkey_, Paths& paths_, Profile(const QString& name_, std::unique_ptr<ToxEncrypt> passkey_, Paths& paths_,
Settings &settings_, IMessageBoxManager& messageBoxManager); Settings &settings_);
static QStringList getFilesByExt(QString extension, Settings& settings); static QStringList getFilesByExt(QString extension, Settings& settings);
QString avatarPath(const ToxPk& owner, bool forceUnencrypted = false); QString avatarPath(const ToxPk& owner, bool forceUnencrypted = false);
bool saveToxSave(QByteArray data); bool saveToxSave(QByteArray data);
@ -128,5 +128,4 @@ private:
std::unique_ptr<BootstrapNodeUpdater> bootstrapNodes; std::unique_ptr<BootstrapNodeUpdater> bootstrapNodes;
Paths& paths; Paths& paths;
Settings& settings; Settings& settings;
IMessageBoxManager& messageBoxManager;
}; };

5
src/widget/tool/messageboxmanager.cpp

@ -27,6 +27,11 @@
#include <QDesktopServices> #include <QDesktopServices>
#include <QUrl> #include <QUrl>
MessageBoxManager::MessageBoxManager(QWidget* parent)
: QWidget(parent)
{
}
/** /**
* @brief Show some text to the user. * @brief Show some text to the user.
* @param title Title of information window. * @param title Title of information window.

1
src/widget/tool/messageboxmanager.h

@ -30,6 +30,7 @@ class MessageBoxManager : public QWidget, public IMessageBoxManager
{ {
Q_OBJECT Q_OBJECT
public: public:
explicit MessageBoxManager(QWidget* parent);
~MessageBoxManager() override = default; ~MessageBoxManager() override = default;
void showInfo(const QString& title, const QString& msg) override; void showInfo(const QString& title, const QString& msg) override;
void showWarning(const QString& title, const QString& msg) override; void showWarning(const QString& title, const QString& msg) override;

24
src/widget/widget.cpp

@ -78,7 +78,7 @@
#include "src/widget/form/settingswidget.h" #include "src/widget/form/settingswidget.h"
#include "src/widget/style.h" #include "src/widget/style.h"
#include "src/widget/translator.h" #include "src/widget/translator.h"
#include "src/widget/tool/imessageboxmanager.h" #include "src/widget/tool/messageboxmanager.h"
#include "tool/removechatdialog.h" #include "tool/removechatdialog.h"
#include "src/persistence/smileypack.h" #include "src/persistence/smileypack.h"
@ -142,7 +142,7 @@ void Widget::acceptFileTransfer(const ToxFile& file, const QString& path)
Widget* Widget::instance{nullptr}; Widget* Widget::instance{nullptr};
Widget::Widget(Profile &profile_, IAudioControl& audio_, CameraSource& cameraSource_, Widget::Widget(Profile &profile_, IAudioControl& audio_, CameraSource& cameraSource_,
Settings& settings_, Style& style_, IMessageBoxManager& messageBoxManager_, QWidget* parent) Settings& settings_, Style& style_, QWidget* parent)
: QMainWindow(parent) : QMainWindow(parent)
, profile{profile_} , profile{profile_}
, trayMenu{nullptr} , trayMenu{nullptr}
@ -159,7 +159,7 @@ Widget::Widget(Profile &profile_, IAudioControl& audio_, CameraSource& cameraSou
, documentCache(new DocumentCache(*smileyPack, settings)) , documentCache(new DocumentCache(*smileyPack, settings))
, cameraSource{cameraSource_} , cameraSource{cameraSource_}
, style{style_} , style{style_}
, messageBoxManager(messageBoxManager_) , messageBoxManager(new MessageBoxManager(this))
, friendList(new FriendList()) , friendList(new FriendList())
, contentDialogManager(new ContentDialogManager(*friendList)) , contentDialogManager(new ContentDialogManager(*friendList))
{ {
@ -268,7 +268,7 @@ void Widget::init()
sharedMessageProcessorParams.reset(new MessageProcessor::SharedParams(core->getMaxMessageSize(), coreExt->getMaxExtendedMessageSize())); sharedMessageProcessorParams.reset(new MessageProcessor::SharedParams(core->getMaxMessageSize(), coreExt->getMaxExtendedMessageSize()));
chatListWidget = new FriendListWidget(*core, this, settings, style, chatListWidget = new FriendListWidget(*core, this, settings, style,
messageBoxManager, *friendList, settings.getGroupchatPosition()); *messageBoxManager, *friendList, settings.getGroupchatPosition());
connect(chatListWidget, &FriendListWidget::searchCircle, this, &Widget::searchCircle); connect(chatListWidget, &FriendListWidget::searchCircle, this, &Widget::searchCircle);
connect(chatListWidget, &FriendListWidget::connectCircleWidget, this, connect(chatListWidget, &FriendListWidget::connectCircleWidget, this,
&Widget::connectCircleWidget); &Widget::connectCircleWidget);
@ -296,9 +296,9 @@ void Widget::init()
style.setThemeColor(settings, settings.getThemeColor()); style.setThemeColor(settings, settings.getThemeColor());
CoreFile* coreFile = core->getCoreFile(); CoreFile* coreFile = core->getCoreFile();
filesForm = new FilesForm(*coreFile, settings, style, messageBoxManager, *friendList); filesForm = new FilesForm(*coreFile, settings, style, *messageBoxManager, *friendList);
addFriendForm = new AddFriendForm(core->getSelfId(), settings, style, addFriendForm = new AddFriendForm(core->getSelfId(), settings, style,
messageBoxManager, *core); *messageBoxManager, *core);
groupInviteForm = new GroupInviteForm(settings, *core); groupInviteForm = new GroupInviteForm(settings, *core);
#if UPDATE_CHECK_ENABLED #if UPDATE_CHECK_ENABLED
@ -306,13 +306,13 @@ void Widget::init()
connect(updateCheck.get(), &UpdateCheck::updateAvailable, this, &Widget::onUpdateAvailable); connect(updateCheck.get(), &UpdateCheck::updateAvailable, this, &Widget::onUpdateAvailable);
#endif #endif
settingsWidget = new SettingsWidget(updateCheck.get(), audio, core, *smileyPack, settingsWidget = new SettingsWidget(updateCheck.get(), audio, core, *smileyPack,
cameraSource, settings, style, messageBoxManager, this); cameraSource, settings, style, *messageBoxManager, this);
#if UPDATE_CHECK_ENABLED #if UPDATE_CHECK_ENABLED
updateCheck->checkForUpdate(); updateCheck->checkForUpdate();
#endif #endif
profileInfo = new ProfileInfo(core, &profile, settings); profileInfo = new ProfileInfo(core, &profile, settings);
profileForm = new ProfileForm(profileInfo, settings, style, messageBoxManager); profileForm = new ProfileForm(profileInfo, settings, style, *messageBoxManager);
#if DESKTOP_NOTIFICATIONS #if DESKTOP_NOTIFICATIONS
notificationGenerator.reset(new NotificationGenerator(settings, &profile)); notificationGenerator.reset(new NotificationGenerator(settings, &profile));
@ -1159,7 +1159,7 @@ void Widget::addFriend(uint32_t friendId, const ToxPk& friendPk)
auto rawChatroom = new FriendChatroom(newfriend, contentDialogManager.get(), *core, settings); auto rawChatroom = new FriendChatroom(newfriend, contentDialogManager.get(), *core, settings);
std::shared_ptr<FriendChatroom> chatroom(rawChatroom); std::shared_ptr<FriendChatroom> chatroom(rawChatroom);
const auto compact = settings.getCompactLayout(); const auto compact = settings.getCompactLayout();
auto widget = new FriendWidget(chatroom, compact, settings, style, messageBoxManager); auto widget = new FriendWidget(chatroom, compact, settings, style, *messageBoxManager);
connectFriendWidget(*widget); connectFriendWidget(*widget);
auto history = profile.getHistory(); auto history = profile.getHistory();
@ -1174,7 +1174,7 @@ void Widget::addFriend(uint32_t friendId, const ToxPk& friendPk)
*friendMessageDispatcher, *friendList); *friendMessageDispatcher, *friendList);
auto friendForm = new ChatForm(profile, newfriend, *chatHistory, auto friendForm = new ChatForm(profile, newfriend, *chatHistory,
*friendMessageDispatcher, *documentCache, *smileyPack, cameraSource, *friendMessageDispatcher, *documentCache, *smileyPack, cameraSource,
settings, style, messageBoxManager, *contentDialogManager, *friendList); settings, style, *messageBoxManager, *contentDialogManager, *friendList);
connect(friendForm, &ChatForm::updateFriendActivity, this, &Widget::updateFriendActivity); connect(friendForm, &ChatForm::updateFriendActivity, this, &Widget::updateFriendActivity);
friendMessageDispatchers[friendPk] = friendMessageDispatcher; friendMessageDispatchers[friendPk] = friendMessageDispatcher;
@ -1831,7 +1831,7 @@ void Widget::onUpdateAvailable()
ContentDialog* Widget::createContentDialog() const ContentDialog* Widget::createContentDialog() const
{ {
ContentDialog* contentDialog = new ContentDialog(*core, settings, style, ContentDialog* contentDialog = new ContentDialog(*core, settings, style,
messageBoxManager, *friendList); *messageBoxManager, *friendList);
registerContentDialog(*contentDialog); registerContentDialog(*contentDialog);
return contentDialog; return contentDialog;
@ -2157,7 +2157,7 @@ Group* Widget::createGroup(uint32_t groupnumber, const GroupId& groupId)
groupAlertConnections.insert(groupId, notifyReceivedConnection); groupAlertConnections.insert(groupId, notifyReceivedConnection);
auto form = new GroupChatForm(*core, newgroup, *chatHistory, *messageDispatcher, auto form = new GroupChatForm(*core, newgroup, *chatHistory, *messageDispatcher,
settings, *documentCache, *smileyPack, style, messageBoxManager, *friendList); settings, *documentCache, *smileyPack, style, *messageBoxManager, *friendList);
connect(&settings, &Settings::nameColorsChanged, form, &GenericChatForm::setColorizedNames); connect(&settings, &Settings::nameColorsChanged, form, &GenericChatForm::setColorizedNames);
form->setColorizedNames(settings.getEnableGroupChatsColor()); form->setColorizedNames(settings.getEnableGroupChatsColor());
groupMessageDispatchers[groupId] = messageDispatcher; groupMessageDispatchers[groupId] = messageDispatcher;

5
src/widget/widget.h

@ -124,8 +124,7 @@ private:
public: public:
Widget(Profile& profile_, IAudioControl& audio_, CameraSource& cameraSource, Widget(Profile& profile_, IAudioControl& audio_, CameraSource& cameraSource,
Settings& settings, Style& style, IMessageBoxManager& messageBoxManager, Settings& settings, Style& style, QWidget* parent = nullptr);
QWidget* parent = nullptr);
~Widget() override; ~Widget() override;
void init(); void init();
void setCentralWidget(QWidget* widget, const QString& widgetName); void setCentralWidget(QWidget* widget, const QString& widgetName);
@ -393,7 +392,7 @@ private:
std::unique_ptr<DocumentCache> documentCache; std::unique_ptr<DocumentCache> documentCache;
CameraSource& cameraSource; CameraSource& cameraSource;
Style& style; Style& style;
IMessageBoxManager& messageBoxManager; IMessageBoxManager* messageBoxManager = nullptr; // freed by Qt on destruction
std::unique_ptr<FriendList> friendList; std::unique_ptr<FriendList> friendList;
std::unique_ptr<ContentDialogManager> contentDialogManager; std::unique_ptr<ContentDialogManager> contentDialogManager;
}; };

Loading…
Cancel
Save