Use shared pointer for the modals

This commit is contained in:
Konstantinos Sideris 2017-10-07 20:09:34 +03:00
parent 800d9ecff3
commit bc4b47a5e3
12 changed files with 85 additions and 62 deletions

View File

@ -110,8 +110,8 @@ private:
// Keeps track of the users currently typing on each room. // Keeps track of the users currently typing on each room.
QMap<QString, QList<QString>> typingUsers_; QMap<QString, QList<QString>> typingUsers_;
QuickSwitcher *quickSwitcher_ = nullptr; QSharedPointer<QuickSwitcher> quickSwitcher_;
OverlayModal *quickSwitcherModal_ = nullptr; QSharedPointer<OverlayModal> quickSwitcherModal_;
// Matrix Client API provider. // Matrix Client API provider.
QSharedPointer<MatrixClient> client_; QSharedPointer<MatrixClient> client_;

View File

@ -43,5 +43,5 @@ private:
// Horizontal distance from panel's bottom right corner. // Horizontal distance from panel's bottom right corner.
int horizontal_distance_ = 70; int horizontal_distance_ = 70;
EmojiPanel *panel_; QSharedPointer<EmojiPanel> panel_;
}; };

View File

@ -83,8 +83,8 @@ private:
ChatPage *chat_page_; ChatPage *chat_page_;
// Used to hide undefined states between page transitions. // Used to hide undefined states between page transitions.
OverlayModal *progress_modal_; QSharedPointer<OverlayModal> progressModal_;
LoadingIndicator *spinner_; QSharedPointer<LoadingIndicator> spinner_;
// Matrix Client API provider. // Matrix Client API provider.
QSharedPointer<MatrixClient> client_; QSharedPointer<MatrixClient> client_;

View File

@ -76,8 +76,8 @@ private:
OverlayModal *joinRoomModal_; OverlayModal *joinRoomModal_;
JoinRoomDialog *joinRoomDialog_; JoinRoomDialog *joinRoomDialog_;
OverlayModal *leaveRoomModal; QSharedPointer<OverlayModal> leaveRoomModal_;
LeaveRoomDialog *leaveRoomDialog_; QSharedPointer<LeaveRoomDialog> leaveRoomDialog_;
QMap<QString, QSharedPointer<RoomInfoListItem>> rooms_; QMap<QString, QSharedPointer<RoomInfoListItem>> rooms_;

View File

@ -77,8 +77,8 @@ private:
FlatButton *settingsBtn_; FlatButton *settingsBtn_;
OverlayModal *leaveRoomModal; QSharedPointer<OverlayModal> leaveRoomModal_;
LeaveRoomDialog *leaveRoomDialog_; QSharedPointer<LeaveRoomDialog> leaveRoomDialog_;
Avatar *avatar_; Avatar *avatar_;

View File

@ -68,8 +68,8 @@ private:
QImage avatar_image_; QImage avatar_image_;
OverlayModal *logoutModal_; QSharedPointer<OverlayModal> logoutModal_;
LogoutDialog *logoutDialog_; QSharedPointer<LogoutDialog> logoutDialog_;
int logoutButtonSize_; int logoutButtonSize_;
}; };

View File

@ -572,21 +572,26 @@ ChatPage::keyPressEvent(QKeyEvent *event)
void void
ChatPage::showQuickSwitcher() ChatPage::showQuickSwitcher()
{ {
if (quickSwitcher_ == nullptr) { if (quickSwitcher_.isNull()) {
quickSwitcher_ = new QuickSwitcher(this); quickSwitcher_ = QSharedPointer<QuickSwitcher>(
new QuickSwitcher(this),
[=](QuickSwitcher *switcher) { switcher->deleteLater(); });
connect(quickSwitcher_, connect(quickSwitcher_.data(),
&QuickSwitcher::roomSelected, &QuickSwitcher::roomSelected,
room_list_, room_list_,
&RoomList::highlightSelectedRoom); &RoomList::highlightSelectedRoom);
connect(quickSwitcher_, &QuickSwitcher::closing, this, [=]() {
if (this->quickSwitcherModal_ != nullptr) connect(quickSwitcher_.data(), &QuickSwitcher::closing, this, [=]() {
if (!this->quickSwitcherModal_.isNull())
this->quickSwitcherModal_->fadeOut(); this->quickSwitcherModal_->fadeOut();
}); });
} }
if (quickSwitcherModal_ == nullptr) { if (quickSwitcherModal_.isNull()) {
quickSwitcherModal_ = new OverlayModal(MainWindow::instance(), quickSwitcher_); quickSwitcherModal_ = QSharedPointer<OverlayModal>(
new OverlayModal(MainWindow::instance(), quickSwitcher_.data()),
[=](OverlayModal *modal) { modal->deleteLater(); });
quickSwitcherModal_->setDuration(0); quickSwitcherModal_->setDuration(0);
quickSwitcherModal_->setColor(QColor(30, 30, 30, 170)); quickSwitcherModal_->setColor(QColor(30, 30, 30, 170));
} }

View File

@ -30,9 +30,10 @@ EmojiPickButton::enterEvent(QEvent *e)
{ {
Q_UNUSED(e); Q_UNUSED(e);
if (panel_ == nullptr) { if (panel_.isNull()) {
panel_ = new EmojiPanel(this); panel_ = QSharedPointer<EmojiPanel>(new EmojiPanel(this));
connect(panel_, &EmojiPanel::emojiSelected, this, &EmojiPickButton::emojiSelected); connect(
panel_.data(), &EmojiPanel::emojiSelected, this, &EmojiPickButton::emojiSelected);
} }
QPoint pos(rect().x(), rect().y()); QPoint pos(rect().x(), rect().y());

View File

@ -15,8 +15,8 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>. * along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
#include "MainWindow.h"
#include "Config.h" #include "Config.h"
#include "MainWindow.h"
#include <QApplication> #include <QApplication>
#include <QLayout> #include <QLayout>
@ -29,7 +29,7 @@ MainWindow *MainWindow::instance_ = nullptr;
MainWindow::MainWindow(QWidget *parent) MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent) : QMainWindow(parent)
, progress_modal_{ nullptr } , progressModal_{ nullptr }
, spinner_{ nullptr } , spinner_{ nullptr }
{ {
QSizePolicy sizePolicy(QSizePolicy::Minimum, QSizePolicy::Minimum); QSizePolicy sizePolicy(QSizePolicy::Minimum, QSizePolicy::Minimum);
@ -132,18 +132,14 @@ MainWindow::removeOverlayProgressBar()
connect(timer, &QTimer::timeout, [=]() { connect(timer, &QTimer::timeout, [=]() {
timer->deleteLater(); timer->deleteLater();
if (progress_modal_ != nullptr) { if (!progressModal_.isNull())
progress_modal_->deleteLater(); progressModal_->fadeOut();
progress_modal_->fadeOut();
}
if (spinner_ != nullptr) if (!spinner_.isNull())
spinner_->deleteLater(); spinner_->stop();
spinner_->stop(); progressModal_.reset();
spinner_.reset();
progress_modal_ = nullptr;
spinner_ = nullptr;
}); });
timer->start(500); timer->start(500);
@ -166,18 +162,22 @@ MainWindow::showChatPage(QString userid, QString homeserver, QString token)
QTimer::singleShot( QTimer::singleShot(
modalOpacityDuration + 100, this, [=]() { pageStack_->setCurrentWidget(chat_page_); }); modalOpacityDuration + 100, this, [=]() { pageStack_->setCurrentWidget(chat_page_); });
if (spinner_ == nullptr) { if (spinner_.isNull()) {
spinner_ = new LoadingIndicator(this); spinner_ = QSharedPointer<LoadingIndicator>(
new LoadingIndicator(this),
[=](LoadingIndicator *indicator) { indicator->deleteLater(); });
spinner_->setColor("#acc7dc"); spinner_->setColor("#acc7dc");
spinner_->setFixedHeight(120); spinner_->setFixedHeight(120);
spinner_->setFixedWidth(120); spinner_->setFixedWidth(120);
spinner_->start(); spinner_->start();
} }
if (progress_modal_ == nullptr) { if (progressModal_.isNull()) {
progress_modal_ = new OverlayModal(this, spinner_); progressModal_ =
progress_modal_->fadeIn(); QSharedPointer<OverlayModal>(new OverlayModal(this, spinner_.data()),
progress_modal_->setDuration(modalOpacityDuration); [=](OverlayModal *modal) { modal->deleteLater(); });
progressModal_->fadeIn();
progressModal_->setDuration(modalOpacityDuration);
} }
login_page_->reset(); login_page_->reset();

View File

@ -168,16 +168,23 @@ RoomList::setInitialRooms(const QMap<QString, QSharedPointer<RoomSettings>> &set
void void
RoomList::openLeaveRoomDialog(const QString &room_id) RoomList::openLeaveRoomDialog(const QString &room_id)
{ {
leaveRoomDialog_ = new LeaveRoomDialog(this); if (leaveRoomDialog_.isNull()) {
connect(leaveRoomDialog_, &LeaveRoomDialog::closing, this, [=](bool leaving) { leaveRoomDialog_ = QSharedPointer<LeaveRoomDialog>(new LeaveRoomDialog(this));
closeLeaveRoomDialog(leaving, room_id);
});
leaveRoomModal = new OverlayModal(MainWindow::instance(), leaveRoomDialog_); connect(leaveRoomDialog_.data(),
leaveRoomModal->setDuration(0); &LeaveRoomDialog::closing,
leaveRoomModal->setColor(QColor(55, 55, 55, 170)); this,
[=](bool leaving) { closeLeaveRoomDialog(leaving, room_id); });
}
leaveRoomModal->fadeIn(); if (leaveRoomModal_.isNull()) {
leaveRoomModal_ = QSharedPointer<OverlayModal>(
new OverlayModal(MainWindow::instance(), leaveRoomDialog_.data()));
leaveRoomModal_->setDuration(0);
leaveRoomModal_->setColor(QColor(30, 30, 30, 170));
}
leaveRoomModal_->fadeIn();
} }
void void
@ -266,7 +273,7 @@ RoomList::closeJoinRoomDialog(bool isJoining, QString roomAlias)
void void
RoomList::closeLeaveRoomDialog(bool leaving, const QString &room_id) RoomList::closeLeaveRoomDialog(bool leaving, const QString &room_id)
{ {
leaveRoomModal->fadeOut(); leaveRoomModal_->fadeOut();
if (leaving) { if (leaving) {
client_->leaveRoom(room_id); client_->leaveRoom(room_id);

View File

@ -86,15 +86,24 @@ TopRoomBar::TopRoomBar(QWidget *parent)
leaveRoom_ = new QAction(tr("Leave room"), this); leaveRoom_ = new QAction(tr("Leave room"), this);
connect(leaveRoom_, &QAction::triggered, this, [=]() { connect(leaveRoom_, &QAction::triggered, this, [=]() {
leaveRoomDialog_ = new LeaveRoomDialog(this); if (leaveRoomDialog_.isNull()) {
connect( leaveRoomDialog_ =
leaveRoomDialog_, SIGNAL(closing(bool)), this, SLOT(closeLeaveRoomDialog(bool))); QSharedPointer<LeaveRoomDialog>(new LeaveRoomDialog(this));
leaveRoomModal = new OverlayModal(MainWindow::instance(), leaveRoomDialog_); connect(leaveRoomDialog_.data(),
leaveRoomModal->setDuration(100); SIGNAL(closing(bool)),
leaveRoomModal->setColor(QColor(55, 55, 55, 170)); this,
SLOT(closeLeaveRoomDialog(bool)));
}
leaveRoomModal->fadeIn(); if (leaveRoomModal_.isNull()) {
leaveRoomModal_ = QSharedPointer<OverlayModal>(
new OverlayModal(MainWindow::instance(), leaveRoomDialog_.data()));
leaveRoomModal_->setDuration(0);
leaveRoomModal_->setColor(QColor(30, 30, 30, 170));
}
leaveRoomModal_->fadeIn();
}); });
menu_->addAction(toggleNotifications_); menu_->addAction(toggleNotifications_);
@ -117,7 +126,7 @@ TopRoomBar::TopRoomBar(QWidget *parent)
void void
TopRoomBar::closeLeaveRoomDialog(bool leaving) TopRoomBar::closeLeaveRoomDialog(bool leaving)
{ {
leaveRoomModal->fadeOut(); leaveRoomModal_->fadeOut();
if (leaving) { if (leaving) {
emit leaveRoom(); emit leaveRoom();

View File

@ -93,18 +93,19 @@ UserInfoWidget::UserInfoWidget(QWidget *parent)
// Show the confirmation dialog. // Show the confirmation dialog.
connect(logoutButton_, &QPushButton::clicked, this, [=]() { connect(logoutButton_, &QPushButton::clicked, this, [=]() {
if (logoutDialog_ == nullptr) { if (logoutDialog_.isNull()) {
logoutDialog_ = new LogoutDialog(this); logoutDialog_ = QSharedPointer<LogoutDialog>(new LogoutDialog(this));
connect(logoutDialog_, connect(logoutDialog_.data(),
SIGNAL(closing(bool)), SIGNAL(closing(bool)),
this, this,
SLOT(closeLogoutDialog(bool))); SLOT(closeLogoutDialog(bool)));
} }
if (logoutModal_ == nullptr) { if (logoutModal_.isNull()) {
logoutModal_ = new OverlayModal(MainWindow::instance(), logoutDialog_); logoutModal_ = QSharedPointer<OverlayModal>(
new OverlayModal(MainWindow::instance(), logoutDialog_.data()));
logoutModal_->setDuration(0); logoutModal_->setDuration(0);
logoutModal_->setColor(QColor(55, 55, 55, 170)); logoutModal_->setColor(QColor(30, 30, 30, 170));
} }
logoutModal_->fadeIn(); logoutModal_->fadeIn();