Simplify dialog creation

This commit is contained in:
Konstantinos Sideris 2018-08-11 13:50:56 +03:00
parent a339f5062f
commit aa2f6ce77d
16 changed files with 179 additions and 272 deletions

View file

@ -835,31 +835,15 @@ ChatPage::loadStateFromCache()
void
ChatPage::showQuickSwitcher()
{
if (quickSwitcher_.isNull()) {
quickSwitcher_ = QSharedPointer<QuickSwitcher>(
new QuickSwitcher(this),
[](QuickSwitcher *switcher) { switcher->deleteLater(); });
auto dialog = new QuickSwitcher(this);
connect(quickSwitcher_.data(),
&QuickSwitcher::roomSelected,
room_list_,
&RoomList::highlightSelectedRoom);
connect(dialog, &QuickSwitcher::roomSelected, room_list_, &RoomList::highlightSelectedRoom);
connect(dialog, &QuickSwitcher::closing, this, [this]() {
MainWindow::instance()->hideOverlay();
text_input_->setFocus(Qt::FocusReason::PopupFocusReason);
});
connect(quickSwitcher_.data(), &QuickSwitcher::closing, this, [this]() {
if (!quickSwitcherModal_.isNull())
quickSwitcherModal_->hide();
text_input_->setFocus(Qt::FocusReason::PopupFocusReason);
});
}
if (quickSwitcherModal_.isNull()) {
quickSwitcherModal_ = QSharedPointer<OverlayModal>(
new OverlayModal(MainWindow::instance(), quickSwitcher_.data()),
[](OverlayModal *modal) { modal->deleteLater(); });
quickSwitcherModal_->setColor(QColor(30, 30, 30, 170));
}
quickSwitcherModal_->show();
MainWindow::instance()->showTransparentOverlayModal(dialog);
}
void
@ -917,26 +901,6 @@ ChatPage::removeLeftRooms(const std::map<std::string, mtx::responses::LeftRoom>
}
}
void
ChatPage::showReadReceipts(const QString &event_id)
{
if (receiptsDialog_.isNull()) {
receiptsDialog_ = QSharedPointer<dialogs::ReadReceipts>(
new dialogs::ReadReceipts(this),
[](dialogs::ReadReceipts *dialog) { dialog->deleteLater(); });
}
if (receiptsModal_.isNull()) {
receiptsModal_ = QSharedPointer<OverlayModal>(
new OverlayModal(MainWindow::instance(), receiptsDialog_.data()),
[](OverlayModal *modal) { modal->deleteLater(); });
receiptsModal_->setColor(QColor(30, 30, 30, 170));
}
receiptsDialog_->addUsers(cache::client()->readReceipts(event_id, current_room_));
receiptsModal_->show();
}
void
ChatPage::setGroupViewState(bool isEnabled)
{

View file

@ -44,10 +44,6 @@ class UserInfoWidget;
class UserSettings;
class NotificationsManager;
namespace dialogs {
class ReadReceipts;
}
constexpr int CONSENSUS_TIMEOUT = 1000;
constexpr int SHOW_CONTENT_TIMEOUT = 3000;
constexpr int TYPING_REFRESH_TIMEOUT = 10000;
@ -62,7 +58,6 @@ public:
// Initialize all the components of the UI.
void bootstrap(QString userid, QString homeserver, QString token);
void showQuickSwitcher();
void showReadReceipts(const QString &event_id);
QString currentRoom() const { return current_room_; }
static ChatPage *instance() { return instance_; }
@ -237,12 +232,6 @@ private:
std::map<QString, QList<QString>> typingUsers_;
QTimer *typingRefresher_;
QSharedPointer<QuickSwitcher> quickSwitcher_;
QSharedPointer<OverlayModal> quickSwitcherModal_;
QSharedPointer<dialogs::ReadReceipts> receiptsDialog_;
QSharedPointer<OverlayModal> receiptsModal_;
// Global user settings.
QSharedPointer<UserSettings> userSettings_;

View file

@ -19,7 +19,6 @@
#include <QLabel>
#include <QLayout>
#include <QSharedPointer>
#include <QWidget>
class FlatButton;

View file

@ -42,18 +42,19 @@
#include "dialogs/LeaveRoom.h"
#include "dialogs/Logout.h"
#include "dialogs/MemberList.h"
#include "dialogs/ReadReceipts.h"
#include "dialogs/RoomSettings.h"
MainWindow *MainWindow::instance_ = nullptr;
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, progressModal_{nullptr}
, spinner_{nullptr}
{
setWindowTitle("nheko");
setObjectName("MainWindow");
modal_ = new OverlayModal(this);
restoreWindowSize();
QFont font("Open Sans");
@ -223,26 +224,20 @@ MainWindow::removeOverlayProgressBar()
connect(timer, &QTimer::timeout, [this, timer]() {
timer->deleteLater();
if (!progressModal_.isNull())
progressModal_->hide();
if (modal_)
modal_->hide();
if (!spinner_.isNull())
if (spinner_)
spinner_->stop();
progressModal_.reset();
spinner_.reset();
});
// FIXME: Snackbar doesn't work if it's initialized in the constructor.
QTimer::singleShot(0, this, [this]() {
snackBar_ = QSharedPointer<SnackBar>(new SnackBar(this));
connect(chat_page_,
&ChatPage::showNotification,
snackBar_.data(),
&SnackBar::showMessage);
snackBar_ = new SnackBar(this);
connect(chat_page_, &ChatPage::showNotification, snackBar_, &SnackBar::showMessage);
});
timer->start(500);
timer->start(50);
}
void
@ -312,18 +307,10 @@ MainWindow::hasActiveUser()
void
MainWindow::openUserProfile(const QString &user_id, const QString &room_id)
{
if (!userProfileDialog_)
userProfileDialog_ =
QSharedPointer<dialogs::UserProfile>(new dialogs::UserProfile(this));
auto dialog = new dialogs::UserProfile(this);
dialog->init(user_id, room_id);
userProfileDialog_->init(user_id, room_id);
if (!userProfileModal_)
userProfileModal_ =
QSharedPointer<OverlayModal>(new OverlayModal(this, userProfileDialog_.data()));
userProfileModal_->setContentAlignment(Qt::AlignTop | Qt::AlignHCenter);
userProfileModal_->show();
showTransparentOverlayModal(dialog);
}
void
@ -331,18 +318,13 @@ MainWindow::openRoomSettings(const QString &room_id)
{
const auto roomToSearch = room_id.isEmpty() ? chat_page_->currentRoom() : "";
roomSettingsDialog_ =
QSharedPointer<dialogs::RoomSettings>(new dialogs::RoomSettings(roomToSearch, this));
connect(roomSettingsDialog_.data(), &dialogs::RoomSettings::closing, this, [this]() {
roomSettingsModal_->hide();
auto dialog = new dialogs::RoomSettings(roomToSearch, this);
connect(dialog, &dialogs::RoomSettings::closing, this, [this]() {
if (modal_)
modal_->hide();
});
roomSettingsModal_ =
QSharedPointer<OverlayModal>(new OverlayModal(this, roomSettingsDialog_.data()));
roomSettingsModal_->setContentAlignment(Qt::AlignTop | Qt::AlignHCenter);
roomSettingsModal_->show();
showTransparentOverlayModal(dialog);
}
void
@ -350,13 +332,8 @@ MainWindow::openMemberListDialog(const QString &room_id)
{
const auto roomToSearch = room_id.isEmpty() ? chat_page_->currentRoom() : "";
memberListDialog_ =
QSharedPointer<dialogs::MemberList>(new dialogs::MemberList(roomToSearch, this));
memberListModal_ =
QSharedPointer<OverlayModal>(new OverlayModal(this, memberListDialog_.data()));
memberListModal_->show();
modal_->setWidget(new dialogs::MemberList(roomToSearch, this));
modal_->show();
}
void
@ -364,161 +341,146 @@ MainWindow::openLeaveRoomDialog(const QString &room_id)
{
auto roomToLeave = room_id.isEmpty() ? chat_page_->currentRoom() : room_id;
leaveRoomDialog_ = QSharedPointer<dialogs::LeaveRoom>(new dialogs::LeaveRoom(this));
auto dialog = new dialogs::LeaveRoom(this);
connect(dialog, &dialogs::LeaveRoom::closing, this, [this, roomToLeave](bool leaving) {
if (modal_)
modal_->hide();
connect(leaveRoomDialog_.data(),
&dialogs::LeaveRoom::closing,
this,
[this, roomToLeave](bool leaving) {
leaveRoomModal_->hide();
if (leaving)
chat_page_->leaveRoom(roomToLeave);
});
if (leaving)
chat_page_->leaveRoom(roomToLeave);
});
leaveRoomModal_ =
QSharedPointer<OverlayModal>(new OverlayModal(this, leaveRoomDialog_.data()));
leaveRoomModal_->setColor(QColor(30, 30, 30, 170));
leaveRoomModal_->show();
showTransparentOverlayModal(dialog, Qt::AlignCenter);
}
void
MainWindow::showOverlayProgressBar()
{
if (spinner_.isNull()) {
spinner_ = QSharedPointer<LoadingIndicator>(
new LoadingIndicator(this),
[](LoadingIndicator *indicator) { indicator->deleteLater(); });
spinner_->setFixedHeight(100);
spinner_->setFixedWidth(100);
spinner_->setObjectName("ChatPageLoadSpinner");
spinner_->start();
}
spinner_ = new LoadingIndicator(this);
spinner_->setFixedHeight(100);
spinner_->setFixedWidth(100);
spinner_->setObjectName("ChatPageLoadSpinner");
spinner_->start();
if (progressModal_.isNull()) {
progressModal_ =
QSharedPointer<OverlayModal>(new OverlayModal(this, spinner_.data()),
[](OverlayModal *modal) { modal->deleteLater(); });
progressModal_->setColor(QColor(30, 30, 30));
progressModal_->setDismissible(false);
progressModal_->show();
}
showSolidOverlayModal(spinner_);
}
void
MainWindow::openInviteUsersDialog(std::function<void(const QStringList &invitees)> callback)
{
if (inviteUsersDialog_.isNull()) {
inviteUsersDialog_ =
QSharedPointer<dialogs::InviteUsers>(new dialogs::InviteUsers(this));
auto dialog = new dialogs::InviteUsers(this);
connect(dialog,
&dialogs::InviteUsers::closing,
this,
[this, callback](bool isSending, QStringList invitees) {
if (modal_)
modal_->hide();
if (isSending && !invitees.isEmpty())
callback(invitees);
});
connect(inviteUsersDialog_.data(),
&dialogs::InviteUsers::closing,
this,
[this, callback](bool isSending, QStringList invitees) {
inviteUsersModal_->hide();
if (isSending && !invitees.isEmpty())
callback(invitees);
});
}
if (inviteUsersModal_.isNull()) {
inviteUsersModal_ = QSharedPointer<OverlayModal>(
new OverlayModal(MainWindow::instance(), inviteUsersDialog_.data()));
inviteUsersModal_->setColor(QColor(30, 30, 30, 170));
}
inviteUsersModal_->show();
showTransparentOverlayModal(dialog);
}
void
MainWindow::openJoinRoomDialog(std::function<void(const QString &room_id)> callback)
{
if (joinRoomDialog_.isNull()) {
joinRoomDialog_ = QSharedPointer<dialogs::JoinRoom>(new dialogs::JoinRoom(this));
auto dialog = new dialogs::JoinRoom(this);
connect(dialog,
&dialogs::JoinRoom::closing,
this,
[this, callback](bool isJoining, const QString &room) {
if (modal_)
modal_->hide();
connect(joinRoomDialog_.data(),
&dialogs::JoinRoom::closing,
this,
[this, callback](bool isJoining, const QString &room) {
joinRoomModal_->hide();
if (isJoining && !room.isEmpty())
callback(room);
});
if (isJoining && !room.isEmpty())
callback(room);
});
}
if (joinRoomModal_.isNull()) {
joinRoomModal_ = QSharedPointer<OverlayModal>(
new OverlayModal(MainWindow::instance(), joinRoomDialog_.data()));
}
joinRoomModal_->show();
showTransparentOverlayModal(dialog, Qt::AlignCenter);
}
void
MainWindow::openCreateRoomDialog(
std::function<void(const mtx::requests::CreateRoom &request)> callback)
{
if (createRoomDialog_.isNull()) {
createRoomDialog_ =
QSharedPointer<dialogs::CreateRoom>(new dialogs::CreateRoom(this));
auto dialog = new dialogs::CreateRoom(this);
connect(dialog,
&dialogs::CreateRoom::closing,
this,
[this, callback](bool isCreating, const mtx::requests::CreateRoom &request) {
if (modal_)
modal_->hide();
connect(
createRoomDialog_.data(),
&dialogs::CreateRoom::closing,
this,
[this, callback](bool isCreating, const mtx::requests::CreateRoom &request) {
createRoomModal_->hide();
if (isCreating)
callback(request);
});
if (isCreating)
callback(request);
});
}
showTransparentOverlayModal(dialog);
}
if (createRoomModal_.isNull()) {
createRoomModal_ = QSharedPointer<OverlayModal>(
new OverlayModal(MainWindow::instance(), createRoomDialog_.data()));
}
void
MainWindow::showTransparentOverlayModal(QWidget *content, QFlags<Qt::AlignmentFlag> flags)
{
modal_->setWidget(content);
modal_->setColor(QColor(30, 30, 30, 150));
modal_->setDismissible(true);
modal_->setContentAlignment(flags);
modal_->raise();
modal_->show();
}
createRoomModal_->show();
void
MainWindow::showSolidOverlayModal(QWidget *content, QFlags<Qt::AlignmentFlag> flags)
{
modal_->setWidget(content);
modal_->setColor(QColor(30, 30, 30));
modal_->setDismissible(false);
modal_->setContentAlignment(flags);
modal_->raise();
modal_->show();
}
void
MainWindow::openLogoutDialog(std::function<void()> callback)
{
if (logoutDialog_.isNull()) {
logoutDialog_ = QSharedPointer<dialogs::Logout>(new dialogs::Logout(this));
connect(logoutDialog_.data(),
&dialogs::Logout::closing,
this,
[this, callback](bool logging_out) {
logoutModal_->hide();
auto dialog = new dialogs::Logout(this);
connect(dialog, &dialogs::Logout::closing, this, [this, callback](bool logging_out) {
if (modal_)
modal_->hide();
if (logging_out)
callback();
});
if (logging_out)
callback();
});
showTransparentOverlayModal(dialog, Qt::AlignCenter);
}
void
MainWindow::openReadReceiptsDialog(const QString &event_id)
{
auto dialog = new dialogs::ReadReceipts(this);
const auto room_id = chat_page_->currentRoom();
try {
dialog->addUsers(cache::client()->readReceipts(event_id, room_id));
} catch (const lmdb::error &e) {
nhlog::db()->warn("failed to retrieve read receipts for {} {}",
event_id.toStdString(),
chat_page_->currentRoom().toStdString());
dialog->deleteLater();
return;
}
if (logoutModal_.isNull()) {
logoutModal_ = QSharedPointer<OverlayModal>(
new OverlayModal(MainWindow::instance(), logoutDialog_.data()));
}
logoutModal_->show();
showTransparentOverlayModal(dialog);
}
bool
MainWindow::hasActiveDialogs() const
{
return (!leaveRoomModal_.isNull() && leaveRoomModal_->isVisible()) ||
(!progressModal_.isNull() && progressModal_->isVisible()) ||
(!inviteUsersModal_.isNull() && inviteUsersModal_->isVisible()) ||
(!joinRoomModal_.isNull() && joinRoomModal_->isVisible()) ||
(!createRoomModal_.isNull() && createRoomModal_->isVisible()) ||
(!logoutModal_.isNull() && logoutModal_->isVisible());
return !modal_ && modal_->isVisible();
}
bool
@ -527,3 +489,10 @@ MainWindow::pageSupportsTray() const
return !welcome_page_->isVisible() && !login_page_->isVisible() &&
!register_page_->isVisible();
}
void
MainWindow::hideOverlay()
{
if (modal_)
modal_->hide();
}

View file

@ -29,6 +29,7 @@
#include "UserSettingsPage.h"
#include "WelcomePage.h"
#include "dialogs/UserProfile.h"
#include "ui/OverlayModal.h"
class ChatPage;
class LoadingIndicator;
@ -73,6 +74,14 @@ public:
void openRoomSettings(const QString &room_id = "");
void openMemberListDialog(const QString &room_id = "");
void openUserProfile(const QString &user_id, const QString &room_id);
void openReadReceiptsDialog(const QString &event_id);
void hideOverlay();
void showSolidOverlayModal(QWidget *content,
QFlags<Qt::AlignmentFlag> flags = Qt::AlignCenter);
void showTransparentOverlayModal(QWidget *content,
QFlags<Qt::AlignmentFlag> flags = Qt::AlignTop |
Qt::AlignHCenter);
protected:
void closeEvent(QCloseEvent *event) override;
@ -138,42 +147,11 @@ private:
ChatPage *chat_page_;
UserSettingsPage *userSettingsPage_;
QSharedPointer<UserSettings> userSettings_;
//! Used to hide undefined states between page transitions.
QSharedPointer<OverlayModal> progressModal_;
QSharedPointer<LoadingIndicator> spinner_;
//! Tray icon that shows the unread message count.
TrayIcon *trayIcon_;
//! Notifications display.
QSharedPointer<SnackBar> snackBar_;
//! Leave room modal.
QSharedPointer<OverlayModal> leaveRoomModal_;
//! Leave room dialog.
QSharedPointer<dialogs::LeaveRoom> leaveRoomDialog_;
//! Invite users modal.
QSharedPointer<OverlayModal> inviteUsersModal_;
//! Invite users dialog.
QSharedPointer<dialogs::InviteUsers> inviteUsersDialog_;
//! Join room modal.
QSharedPointer<OverlayModal> joinRoomModal_;
//! Join room dialog.
QSharedPointer<dialogs::JoinRoom> joinRoomDialog_;
//! Create room modal.
QSharedPointer<OverlayModal> createRoomModal_;
//! Create room dialog.
QSharedPointer<dialogs::CreateRoom> createRoomDialog_;
//! Logout modal.
QSharedPointer<OverlayModal> logoutModal_;
//! Logout dialog.
QSharedPointer<dialogs::Logout> logoutDialog_;
//! Room settings modal.
QSharedPointer<OverlayModal> roomSettingsModal_;
//! Room settings dialog.
QSharedPointer<dialogs::RoomSettings> roomSettingsDialog_;
//! Member list modal.
QSharedPointer<OverlayModal> memberListModal_;
//! Member list dialog.
QSharedPointer<dialogs::MemberList> memberListDialog_;
QSharedPointer<OverlayModal> userProfileModal_;
QSharedPointer<dialogs::UserProfile> userProfileDialog_;
SnackBar *snackBar_ = nullptr;
//! Overlay modal used to project other widgets.
OverlayModal *modal_ = nullptr;
LoadingIndicator *spinner_ = nullptr;
};

View file

@ -19,7 +19,6 @@
#include <QLabel>
#include <QLayout>
#include <QSharedPointer>
#include <memory>
class FlatButton;

View file

@ -24,7 +24,6 @@
#include <QPaintEvent>
#include <QPainter>
#include <QPen>
#include <QSharedPointer>
#include <QStyle>
#include <QStyleOption>
#include <QVBoxLayout>

View file

@ -4,7 +4,6 @@
#include <QMessageBox>
#include <QPainter>
#include <QPixmap>
#include <QSharedPointer>
#include <QShowEvent>
#include <QStyleOption>
#include <QVBoxLayout>

View file

@ -19,9 +19,6 @@ class TextField;
class TextField;
class Toggle;
template<class T>
class QSharedPointer;
class EditModal : public QWidget
{
Q_OBJECT

View file

@ -170,8 +170,6 @@ UserProfile::UserProfile(QWidget *parent)
vlayout->setContentsMargins(WIDGET_MARGIN, TOP_WIDGET_MARGIN, WIDGET_MARGIN, WIDGET_MARGIN);
qRegisterMetaType<std::vector<DeviceInfo>>();
connect(this, &UserProfile::devicesRetrieved, this, &UserProfile::updateDeviceList);
}
void
@ -227,10 +225,15 @@ UserProfile::init(const QString &userId, const QString &roomId)
mtx::requests::QueryKeys req;
req.device_keys[userId.toStdString()] = {};
// A proxy object is used to emit the signal instead of the original object
// which might be destroyed by the time the http call finishes.
auto proxy = std::make_shared<Proxy>();
QObject::connect(proxy.get(), &Proxy::done, this, &UserProfile::updateDeviceList);
http::client()->query_keys(
req,
[user_id = userId.toStdString(), this](const mtx::responses::QueryKeys &res,
mtx::http::RequestErr err) {
[user_id = userId.toStdString(), proxy = std::move(proxy), this](
const mtx::responses::QueryKeys &res, mtx::http::RequestErr err) {
if (err) {
nhlog::net()->warn("failed to query device keys: {} {}",
err->matrix_error.error,
@ -264,7 +267,7 @@ UserProfile::init(const QString &userId, const QString &roomId)
});
if (!deviceInfo.empty())
emit devicesRetrieved(QString::fromStdString(user_id), deviceInfo);
emit proxy->done(QString::fromStdString(user_id), deviceInfo);
});
}

View file

@ -17,6 +17,14 @@ struct DeviceInfo
Q_DECLARE_METATYPE(std::vector<DeviceInfo>)
class Proxy : public QObject
{
Q_OBJECT
signals:
void done(const QString &user_id, const std::vector<DeviceInfo> &devices);
};
namespace dialogs {
class DeviceItem : public QWidget
@ -43,9 +51,6 @@ public:
protected:
void paintEvent(QPaintEvent *) override;
signals:
void devicesRetrieved(const QString &user_id, const std::vector<DeviceInfo> &devices);
private slots:
void updateDeviceList(const QString &user_id, const std::vector<DeviceInfo> &devices);

View file

@ -166,7 +166,7 @@ TimelineItem::init()
connect(showReadReceipts_, &QAction::triggered, this, [this]() {
if (!event_id_.isEmpty())
ChatPage::instance()->showReadReceipts(event_id_);
MainWindow::instance()->openReadReceiptsDialog(event_id_);
});
connect(this, &TimelineItem::eventRedacted, this, [this](const QString &event_id) {

View file

@ -12,17 +12,10 @@ LoadingIndicator::LoadingIndicator(QWidget *parent)
setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
setFocusPolicy(Qt::NoFocus);
timer_ = new QTimer();
timer_ = new QTimer(this);
connect(timer_, SIGNAL(timeout()), this, SLOT(onTimeout()));
}
LoadingIndicator::~LoadingIndicator()
{
stop();
delete timer_;
}
void
LoadingIndicator::paintEvent(QPaintEvent *e)
{

View file

@ -13,7 +13,6 @@ class LoadingIndicator : public QWidget
public:
LoadingIndicator(QWidget *parent = 0);
virtual ~LoadingIndicator();
void paintEvent(QPaintEvent *e);

View file

@ -20,18 +20,31 @@
#include "OverlayModal.h"
OverlayModal::OverlayModal(QWidget *parent, QWidget *content)
OverlayModal::OverlayModal(QWidget *parent)
: OverlayWidget(parent)
, content_{content}
, color_{QColor(30, 30, 30, 170)}
{
layout_ = new QVBoxLayout(this);
layout_->addWidget(content);
layout_->setSpacing(0);
layout_->setContentsMargins(10, 40, 10, 20);
setContentAlignment(Qt::AlignCenter);
}
content->setFocus();
void
OverlayModal::setWidget(QWidget *widget)
{
// Delete the previous widget
if (layout_->count() > 0) {
QLayoutItem *item;
while ((item = layout_->takeAt(0)) != nullptr) {
delete item->widget();
delete item;
}
}
layout_->addWidget(widget);
content_ = widget;
content_->setFocus();
}
void

View file

@ -27,12 +27,13 @@
class OverlayModal : public OverlayWidget
{
public:
OverlayModal(QWidget *parent, QWidget *content);
OverlayModal(QWidget *parent);
void setColor(QColor color) { color_ = color; }
void setDismissible(bool state) { isDismissible_ = state; }
void setContentAlignment(QFlags<Qt::AlignmentFlag> flag) { layout_->setAlignment(flag); }
void setWidget(QWidget *widget);
protected:
void paintEvent(QPaintEvent *event) override;