From bc4b47a5e30386df46f5d4d51a5be33b6a9d4c2f Mon Sep 17 00:00:00 2001 From: Konstantinos Sideris Date: Sat, 7 Oct 2017 20:09:34 +0300 Subject: [PATCH] Use shared pointer for the modals --- include/ChatPage.h | 4 ++-- include/EmojiPickButton.h | 2 +- include/MainWindow.h | 4 ++-- include/RoomList.h | 4 ++-- include/TopRoomBar.h | 4 ++-- include/UserInfoWidget.h | 4 ++-- src/ChatPage.cc | 19 ++++++++++++------- src/EmojiPickButton.cc | 7 ++++--- src/MainWindow.cc | 36 ++++++++++++++++++------------------ src/RoomList.cc | 25 ++++++++++++++++--------- src/TopRoomBar.cc | 29 +++++++++++++++++++---------- src/UserInfoWidget.cc | 13 +++++++------ 12 files changed, 87 insertions(+), 64 deletions(-) diff --git a/include/ChatPage.h b/include/ChatPage.h index 8332225..f64d958 100644 --- a/include/ChatPage.h +++ b/include/ChatPage.h @@ -110,8 +110,8 @@ private: // Keeps track of the users currently typing on each room. QMap> typingUsers_; - QuickSwitcher *quickSwitcher_ = nullptr; - OverlayModal *quickSwitcherModal_ = nullptr; + QSharedPointer quickSwitcher_; + QSharedPointer quickSwitcherModal_; // Matrix Client API provider. QSharedPointer client_; diff --git a/include/EmojiPickButton.h b/include/EmojiPickButton.h index 041e25a..d422616 100644 --- a/include/EmojiPickButton.h +++ b/include/EmojiPickButton.h @@ -43,5 +43,5 @@ private: // Horizontal distance from panel's bottom right corner. int horizontal_distance_ = 70; - EmojiPanel *panel_; + QSharedPointer panel_; }; diff --git a/include/MainWindow.h b/include/MainWindow.h index 85cd5a7..a7a2b2e 100644 --- a/include/MainWindow.h +++ b/include/MainWindow.h @@ -83,8 +83,8 @@ private: ChatPage *chat_page_; // Used to hide undefined states between page transitions. - OverlayModal *progress_modal_; - LoadingIndicator *spinner_; + QSharedPointer progressModal_; + QSharedPointer spinner_; // Matrix Client API provider. QSharedPointer client_; diff --git a/include/RoomList.h b/include/RoomList.h index c2f4255..d7b201f 100644 --- a/include/RoomList.h +++ b/include/RoomList.h @@ -76,8 +76,8 @@ private: OverlayModal *joinRoomModal_; JoinRoomDialog *joinRoomDialog_; - OverlayModal *leaveRoomModal; - LeaveRoomDialog *leaveRoomDialog_; + QSharedPointer leaveRoomModal_; + QSharedPointer leaveRoomDialog_; QMap> rooms_; diff --git a/include/TopRoomBar.h b/include/TopRoomBar.h index 5d8b394..2c7af21 100644 --- a/include/TopRoomBar.h +++ b/include/TopRoomBar.h @@ -77,8 +77,8 @@ private: FlatButton *settingsBtn_; - OverlayModal *leaveRoomModal; - LeaveRoomDialog *leaveRoomDialog_; + QSharedPointer leaveRoomModal_; + QSharedPointer leaveRoomDialog_; Avatar *avatar_; diff --git a/include/UserInfoWidget.h b/include/UserInfoWidget.h index 25dc226..b2c7b87 100644 --- a/include/UserInfoWidget.h +++ b/include/UserInfoWidget.h @@ -68,8 +68,8 @@ private: QImage avatar_image_; - OverlayModal *logoutModal_; - LogoutDialog *logoutDialog_; + QSharedPointer logoutModal_; + QSharedPointer logoutDialog_; int logoutButtonSize_; }; diff --git a/src/ChatPage.cc b/src/ChatPage.cc index 0800fe3..dbffc6d 100644 --- a/src/ChatPage.cc +++ b/src/ChatPage.cc @@ -572,21 +572,26 @@ ChatPage::keyPressEvent(QKeyEvent *event) void ChatPage::showQuickSwitcher() { - if (quickSwitcher_ == nullptr) { - quickSwitcher_ = new QuickSwitcher(this); + if (quickSwitcher_.isNull()) { + quickSwitcher_ = QSharedPointer( + new QuickSwitcher(this), + [=](QuickSwitcher *switcher) { switcher->deleteLater(); }); - connect(quickSwitcher_, + connect(quickSwitcher_.data(), &QuickSwitcher::roomSelected, room_list_, &RoomList::highlightSelectedRoom); - connect(quickSwitcher_, &QuickSwitcher::closing, this, [=]() { - if (this->quickSwitcherModal_ != nullptr) + + connect(quickSwitcher_.data(), &QuickSwitcher::closing, this, [=]() { + if (!this->quickSwitcherModal_.isNull()) this->quickSwitcherModal_->fadeOut(); }); } - if (quickSwitcherModal_ == nullptr) { - quickSwitcherModal_ = new OverlayModal(MainWindow::instance(), quickSwitcher_); + if (quickSwitcherModal_.isNull()) { + quickSwitcherModal_ = QSharedPointer( + new OverlayModal(MainWindow::instance(), quickSwitcher_.data()), + [=](OverlayModal *modal) { modal->deleteLater(); }); quickSwitcherModal_->setDuration(0); quickSwitcherModal_->setColor(QColor(30, 30, 30, 170)); } diff --git a/src/EmojiPickButton.cc b/src/EmojiPickButton.cc index f3e3066..4f7dd59 100644 --- a/src/EmojiPickButton.cc +++ b/src/EmojiPickButton.cc @@ -30,9 +30,10 @@ EmojiPickButton::enterEvent(QEvent *e) { Q_UNUSED(e); - if (panel_ == nullptr) { - panel_ = new EmojiPanel(this); - connect(panel_, &EmojiPanel::emojiSelected, this, &EmojiPickButton::emojiSelected); + if (panel_.isNull()) { + panel_ = QSharedPointer(new EmojiPanel(this)); + connect( + panel_.data(), &EmojiPanel::emojiSelected, this, &EmojiPickButton::emojiSelected); } QPoint pos(rect().x(), rect().y()); diff --git a/src/MainWindow.cc b/src/MainWindow.cc index 43f4a94..8072668 100644 --- a/src/MainWindow.cc +++ b/src/MainWindow.cc @@ -15,8 +15,8 @@ * along with this program. If not, see . */ -#include "MainWindow.h" #include "Config.h" +#include "MainWindow.h" #include #include @@ -29,7 +29,7 @@ MainWindow *MainWindow::instance_ = nullptr; MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) - , progress_modal_{ nullptr } + , progressModal_{ nullptr } , spinner_{ nullptr } { QSizePolicy sizePolicy(QSizePolicy::Minimum, QSizePolicy::Minimum); @@ -132,18 +132,14 @@ MainWindow::removeOverlayProgressBar() connect(timer, &QTimer::timeout, [=]() { timer->deleteLater(); - if (progress_modal_ != nullptr) { - progress_modal_->deleteLater(); - progress_modal_->fadeOut(); - } - - if (spinner_ != nullptr) - spinner_->deleteLater(); + if (!progressModal_.isNull()) + progressModal_->fadeOut(); - spinner_->stop(); + if (!spinner_.isNull()) + spinner_->stop(); - progress_modal_ = nullptr; - spinner_ = nullptr; + progressModal_.reset(); + spinner_.reset(); }); timer->start(500); @@ -166,18 +162,22 @@ MainWindow::showChatPage(QString userid, QString homeserver, QString token) QTimer::singleShot( modalOpacityDuration + 100, this, [=]() { pageStack_->setCurrentWidget(chat_page_); }); - if (spinner_ == nullptr) { - spinner_ = new LoadingIndicator(this); + if (spinner_.isNull()) { + spinner_ = QSharedPointer( + new LoadingIndicator(this), + [=](LoadingIndicator *indicator) { indicator->deleteLater(); }); spinner_->setColor("#acc7dc"); spinner_->setFixedHeight(120); spinner_->setFixedWidth(120); spinner_->start(); } - if (progress_modal_ == nullptr) { - progress_modal_ = new OverlayModal(this, spinner_); - progress_modal_->fadeIn(); - progress_modal_->setDuration(modalOpacityDuration); + if (progressModal_.isNull()) { + progressModal_ = + QSharedPointer(new OverlayModal(this, spinner_.data()), + [=](OverlayModal *modal) { modal->deleteLater(); }); + progressModal_->fadeIn(); + progressModal_->setDuration(modalOpacityDuration); } login_page_->reset(); diff --git a/src/RoomList.cc b/src/RoomList.cc index 0383be6..90d8b83 100644 --- a/src/RoomList.cc +++ b/src/RoomList.cc @@ -168,16 +168,23 @@ RoomList::setInitialRooms(const QMap> &set void RoomList::openLeaveRoomDialog(const QString &room_id) { - leaveRoomDialog_ = new LeaveRoomDialog(this); - connect(leaveRoomDialog_, &LeaveRoomDialog::closing, this, [=](bool leaving) { - closeLeaveRoomDialog(leaving, room_id); - }); + if (leaveRoomDialog_.isNull()) { + leaveRoomDialog_ = QSharedPointer(new LeaveRoomDialog(this)); - leaveRoomModal = new OverlayModal(MainWindow::instance(), leaveRoomDialog_); - leaveRoomModal->setDuration(0); - leaveRoomModal->setColor(QColor(55, 55, 55, 170)); + connect(leaveRoomDialog_.data(), + &LeaveRoomDialog::closing, + this, + [=](bool leaving) { closeLeaveRoomDialog(leaving, room_id); }); + } + + if (leaveRoomModal_.isNull()) { + leaveRoomModal_ = QSharedPointer( + new OverlayModal(MainWindow::instance(), leaveRoomDialog_.data())); + leaveRoomModal_->setDuration(0); + leaveRoomModal_->setColor(QColor(30, 30, 30, 170)); + } - leaveRoomModal->fadeIn(); + leaveRoomModal_->fadeIn(); } void @@ -266,7 +273,7 @@ RoomList::closeJoinRoomDialog(bool isJoining, QString roomAlias) void RoomList::closeLeaveRoomDialog(bool leaving, const QString &room_id) { - leaveRoomModal->fadeOut(); + leaveRoomModal_->fadeOut(); if (leaving) { client_->leaveRoom(room_id); diff --git a/src/TopRoomBar.cc b/src/TopRoomBar.cc index f8a7e60..3f93cad 100644 --- a/src/TopRoomBar.cc +++ b/src/TopRoomBar.cc @@ -86,15 +86,24 @@ TopRoomBar::TopRoomBar(QWidget *parent) leaveRoom_ = new QAction(tr("Leave room"), this); connect(leaveRoom_, &QAction::triggered, this, [=]() { - leaveRoomDialog_ = new LeaveRoomDialog(this); - connect( - leaveRoomDialog_, SIGNAL(closing(bool)), this, SLOT(closeLeaveRoomDialog(bool))); - - leaveRoomModal = new OverlayModal(MainWindow::instance(), leaveRoomDialog_); - leaveRoomModal->setDuration(100); - leaveRoomModal->setColor(QColor(55, 55, 55, 170)); - - leaveRoomModal->fadeIn(); + if (leaveRoomDialog_.isNull()) { + leaveRoomDialog_ = + QSharedPointer(new LeaveRoomDialog(this)); + + connect(leaveRoomDialog_.data(), + SIGNAL(closing(bool)), + this, + SLOT(closeLeaveRoomDialog(bool))); + } + + if (leaveRoomModal_.isNull()) { + leaveRoomModal_ = QSharedPointer( + new OverlayModal(MainWindow::instance(), leaveRoomDialog_.data())); + leaveRoomModal_->setDuration(0); + leaveRoomModal_->setColor(QColor(30, 30, 30, 170)); + } + + leaveRoomModal_->fadeIn(); }); menu_->addAction(toggleNotifications_); @@ -117,7 +126,7 @@ TopRoomBar::TopRoomBar(QWidget *parent) void TopRoomBar::closeLeaveRoomDialog(bool leaving) { - leaveRoomModal->fadeOut(); + leaveRoomModal_->fadeOut(); if (leaving) { emit leaveRoom(); diff --git a/src/UserInfoWidget.cc b/src/UserInfoWidget.cc index b65329d..09a75a3 100644 --- a/src/UserInfoWidget.cc +++ b/src/UserInfoWidget.cc @@ -93,18 +93,19 @@ UserInfoWidget::UserInfoWidget(QWidget *parent) // Show the confirmation dialog. connect(logoutButton_, &QPushButton::clicked, this, [=]() { - if (logoutDialog_ == nullptr) { - logoutDialog_ = new LogoutDialog(this); - connect(logoutDialog_, + if (logoutDialog_.isNull()) { + logoutDialog_ = QSharedPointer(new LogoutDialog(this)); + connect(logoutDialog_.data(), SIGNAL(closing(bool)), this, SLOT(closeLogoutDialog(bool))); } - if (logoutModal_ == nullptr) { - logoutModal_ = new OverlayModal(MainWindow::instance(), logoutDialog_); + if (logoutModal_.isNull()) { + logoutModal_ = QSharedPointer( + new OverlayModal(MainWindow::instance(), logoutDialog_.data())); logoutModal_->setDuration(0); - logoutModal_->setColor(QColor(55, 55, 55, 170)); + logoutModal_->setColor(QColor(30, 30, 30, 170)); } logoutModal_->fadeIn();