Reuse the profile modal & adjust its spacing relative to the font size

This commit is contained in:
Konstantinos Sideris 2018-07-21 14:33:35 +03:00
parent 54c7eb374a
commit 6c65213c83
3 changed files with 59 additions and 22 deletions

View file

@ -312,13 +312,17 @@ MainWindow::hasActiveUser()
void void
MainWindow::openUserProfile(const QString &user_id, const QString &room_id) MainWindow::openUserProfile(const QString &user_id, const QString &room_id)
{ {
userProfileDialog_ = QSharedPointer<dialogs::UserProfile>(new dialogs::UserProfile(this)); if (!userProfileDialog_)
userProfileDialog_ =
QSharedPointer<dialogs::UserProfile>(new dialogs::UserProfile(this));
userProfileDialog_->init(user_id, room_id); userProfileDialog_->init(user_id, room_id);
userProfileModal_ = if (!userProfileModal_)
QSharedPointer<OverlayModal>(new OverlayModal(this, userProfileDialog_.data())); userProfileModal_ =
userProfileModal_->setContentAlignment(Qt::AlignTop | Qt::AlignHCenter); QSharedPointer<OverlayModal>(new OverlayModal(this, userProfileDialog_.data()));
userProfileModal_->setContentAlignment(Qt::AlignTop | Qt::AlignHCenter);
userProfileModal_->show(); userProfileModal_->show();
} }

View file

@ -17,7 +17,13 @@
using namespace dialogs; using namespace dialogs;
constexpr int BUTTON_SIZE = 36; constexpr int BUTTON_SIZE = 36;
constexpr int BUTTON_RADIUS = BUTTON_SIZE / 2;
constexpr int WIDGET_MARGIN = 20;
constexpr int TOP_WIDGET_MARGIN = 2 * WIDGET_MARGIN;
constexpr int WIDGET_SPACING = 15;
constexpr int TEXT_SPACING = 4;
constexpr int DEVICE_SPACING = 5;
DeviceItem::DeviceItem(DeviceInfo device, QWidget *parent) DeviceItem::DeviceItem(DeviceInfo device, QWidget *parent)
: QWidget(parent) : QWidget(parent)
@ -47,36 +53,36 @@ UserProfile::UserProfile(QWidget *parent)
banIcon.addFile(":/icons/icons/ui/do-not-disturb-rounded-sign.png"); banIcon.addFile(":/icons/icons/ui/do-not-disturb-rounded-sign.png");
banBtn_ = new FlatButton(this); banBtn_ = new FlatButton(this);
banBtn_->setFixedSize(BUTTON_SIZE, BUTTON_SIZE); banBtn_->setFixedSize(BUTTON_SIZE, BUTTON_SIZE);
banBtn_->setCornerRadius(BUTTON_SIZE / 2); banBtn_->setCornerRadius(BUTTON_RADIUS);
banBtn_->setIcon(banIcon); banBtn_->setIcon(banIcon);
banBtn_->setIconSize(QSize(BUTTON_SIZE / 2, BUTTON_SIZE / 2)); banBtn_->setIconSize(QSize(BUTTON_RADIUS, BUTTON_RADIUS));
banBtn_->setToolTip(tr("Ban the user from the room")); banBtn_->setToolTip(tr("Ban the user from the room"));
banBtn_->setDisabled(true); // Not used yet. banBtn_->setDisabled(true); // Not used yet.
ignoreIcon.addFile(":/icons/icons/ui/volume-off-indicator.png"); ignoreIcon.addFile(":/icons/icons/ui/volume-off-indicator.png");
ignoreBtn_ = new FlatButton(this); ignoreBtn_ = new FlatButton(this);
ignoreBtn_->setFixedSize(BUTTON_SIZE, BUTTON_SIZE); ignoreBtn_->setFixedSize(BUTTON_SIZE, BUTTON_SIZE);
ignoreBtn_->setCornerRadius(BUTTON_SIZE / 2); ignoreBtn_->setCornerRadius(BUTTON_RADIUS);
ignoreBtn_->setIcon(ignoreIcon); ignoreBtn_->setIcon(ignoreIcon);
ignoreBtn_->setIconSize(QSize(BUTTON_SIZE / 2, BUTTON_SIZE / 2)); ignoreBtn_->setIconSize(QSize(BUTTON_RADIUS, BUTTON_RADIUS));
ignoreBtn_->setToolTip(tr("Ignore messages from this user")); ignoreBtn_->setToolTip(tr("Ignore messages from this user"));
ignoreBtn_->setDisabled(true); // Not used yet. ignoreBtn_->setDisabled(true); // Not used yet.
kickIcon.addFile(":/icons/icons/ui/round-remove-button.png"); kickIcon.addFile(":/icons/icons/ui/round-remove-button.png");
kickBtn_ = new FlatButton(this); kickBtn_ = new FlatButton(this);
kickBtn_->setFixedSize(BUTTON_SIZE, BUTTON_SIZE); kickBtn_->setFixedSize(BUTTON_SIZE, BUTTON_SIZE);
kickBtn_->setCornerRadius(BUTTON_SIZE / 2); kickBtn_->setCornerRadius(BUTTON_RADIUS);
kickBtn_->setIcon(kickIcon); kickBtn_->setIcon(kickIcon);
kickBtn_->setIconSize(QSize(BUTTON_SIZE / 2, BUTTON_SIZE / 2)); kickBtn_->setIconSize(QSize(BUTTON_RADIUS, BUTTON_RADIUS));
kickBtn_->setToolTip(tr("Kick the user from the room")); kickBtn_->setToolTip(tr("Kick the user from the room"));
kickBtn_->setDisabled(true); // Not used yet. kickBtn_->setDisabled(true); // Not used yet.
startChatIcon.addFile(":/icons/icons/ui/black-bubble-speech.png"); startChatIcon.addFile(":/icons/icons/ui/black-bubble-speech.png");
startChat_ = new FlatButton(this); startChat_ = new FlatButton(this);
startChat_->setFixedSize(BUTTON_SIZE, BUTTON_SIZE); startChat_->setFixedSize(BUTTON_SIZE, BUTTON_SIZE);
startChat_->setCornerRadius(BUTTON_SIZE / 2); startChat_->setCornerRadius(BUTTON_RADIUS);
startChat_->setIcon(startChatIcon); startChat_->setIcon(startChatIcon);
startChat_->setIconSize(QSize(BUTTON_SIZE / 2, BUTTON_SIZE / 2)); startChat_->setIconSize(QSize(BUTTON_RADIUS, BUTTON_RADIUS));
startChat_->setToolTip(tr("Start a conversation")); startChat_->setToolTip(tr("Start a conversation"));
connect(startChat_, &QPushButton::clicked, this, [this]() { connect(startChat_, &QPushButton::clicked, this, [this]() {
@ -120,14 +126,14 @@ UserProfile::UserProfile(QWidget *parent)
textLayout->addWidget(userIdLabel_); textLayout->addWidget(userIdLabel_);
textLayout->setAlignment(displayNameLabel_, Qt::AlignCenter | Qt::AlignTop); textLayout->setAlignment(displayNameLabel_, Qt::AlignCenter | Qt::AlignTop);
textLayout->setAlignment(userIdLabel_, Qt::AlignCenter | Qt::AlignTop); textLayout->setAlignment(userIdLabel_, Qt::AlignCenter | Qt::AlignTop);
textLayout->setSpacing(4); textLayout->setSpacing(TEXT_SPACING);
textLayout->setMargin(0); textLayout->setMargin(0);
devices_ = new QListWidget{this}; devices_ = new QListWidget{this};
devices_->setFrameStyle(QFrame::NoFrame); devices_->setFrameStyle(QFrame::NoFrame);
devices_->setSelectionMode(QAbstractItemView::NoSelection); devices_->setSelectionMode(QAbstractItemView::NoSelection);
devices_->setAttribute(Qt::WA_MacShowFocusRect, 0); devices_->setAttribute(Qt::WA_MacShowFocusRect, 0);
devices_->setSpacing(5); devices_->setSpacing(DEVICE_SPACING);
devices_->hide(); devices_->hide();
QFont descriptionLabelFont; QFont descriptionLabelFont;
@ -143,6 +149,7 @@ UserProfile::UserProfile(QWidget *parent)
vlayout->addLayout(btnLayout); vlayout->addLayout(btnLayout);
vlayout->addWidget(devicesLabel_, Qt::AlignLeft); vlayout->addWidget(devicesLabel_, Qt::AlignLeft);
vlayout->addWidget(devices_); vlayout->addWidget(devices_);
vlayout->addStretch(1);
vlayout->setAlignment(avatar_, Qt::AlignCenter | Qt::AlignTop); vlayout->setAlignment(avatar_, Qt::AlignCenter | Qt::AlignTop);
vlayout->setAlignment(userIdLabel_, Qt::AlignCenter | Qt::AlignTop); vlayout->setAlignment(userIdLabel_, Qt::AlignCenter | Qt::AlignTop);
@ -151,20 +158,38 @@ UserProfile::UserProfile(QWidget *parent)
setWindowFlags(Qt::Tool | Qt::WindowStaysOnTopHint); setWindowFlags(Qt::Tool | Qt::WindowStaysOnTopHint);
setWindowModality(Qt::WindowModal); setWindowModality(Qt::WindowModal);
setMinimumWidth(340); QFont doubleFont;
doubleFont.setPointSizeF(doubleFont.pointSizeF() * 2);
setMinimumWidth(
std::max(devices_->sizeHint().width() + 4 * WIDGET_MARGIN,
QFontMetrics(doubleFont).averageCharWidth() * 30 - 2 * WIDGET_MARGIN));
setSizePolicy(QSizePolicy::Maximum, QSizePolicy::Maximum); setSizePolicy(QSizePolicy::Maximum, QSizePolicy::Maximum);
vlayout->setSpacing(15); vlayout->setSpacing(WIDGET_SPACING);
vlayout->setContentsMargins(20, 40, 20, 20); vlayout->setContentsMargins(WIDGET_MARGIN, TOP_WIDGET_MARGIN, WIDGET_MARGIN, WIDGET_MARGIN);
qRegisterMetaType<std::vector<DeviceInfo>>(); qRegisterMetaType<std::vector<DeviceInfo>>();
connect(this, &UserProfile::devicesRetrieved, this, &UserProfile::updateDeviceList); connect(this, &UserProfile::devicesRetrieved, this, &UserProfile::updateDeviceList);
} }
void
UserProfile::resetToDefaults()
{
avatar_->setLetter("X");
devices_->clear();
ignoreBtn_->show();
devices_->hide();
devicesLabel_->hide();
}
void void
UserProfile::init(const QString &userId, const QString &roomId) UserProfile::init(const QString &userId, const QString &roomId)
{ {
resetToDefaults();
auto displayName = Cache::displayName(roomId, userId); auto displayName = Cache::displayName(roomId, userId);
userIdLabel_->setText(userId); userIdLabel_->setText(userId);
@ -184,6 +209,9 @@ UserProfile::init(const QString &userId, const QString &roomId)
if (!hasMemberRights) { if (!hasMemberRights) {
kickBtn_->hide(); kickBtn_->hide();
banBtn_->hide(); banBtn_->hide();
} else {
kickBtn_->show();
banBtn_->show();
} }
} catch (const lmdb::error &e) { } catch (const lmdb::error &e) {
nhlog::db()->warn("lmdb error: {}", e.what()); nhlog::db()->warn("lmdb error: {}", e.what());
@ -236,13 +264,16 @@ UserProfile::init(const QString &userId, const QString &roomId)
}); });
if (!deviceInfo.empty()) if (!deviceInfo.empty())
emit devicesRetrieved(deviceInfo); emit devicesRetrieved(QString::fromStdString(user_id), deviceInfo);
}); });
} }
void void
UserProfile::updateDeviceList(const std::vector<DeviceInfo> &devices) UserProfile::updateDeviceList(const QString &user_id, const std::vector<DeviceInfo> &devices)
{ {
if (user_id != userIdLabel_->text())
return;
for (const auto &dev : devices) { for (const auto &dev : devices) {
auto deviceItem = new DeviceItem(dev, this); auto deviceItem = new DeviceItem(dev, this);
auto item = new QListWidgetItem; auto item = new QListWidgetItem;

View file

@ -44,12 +44,14 @@ protected:
void paintEvent(QPaintEvent *) override; void paintEvent(QPaintEvent *) override;
signals: signals:
void devicesRetrieved(const std::vector<DeviceInfo> &devices); void devicesRetrieved(const QString &user_id, const std::vector<DeviceInfo> &devices);
private slots: private slots:
void updateDeviceList(const std::vector<DeviceInfo> &devices); void updateDeviceList(const QString &user_id, const std::vector<DeviceInfo> &devices);
private: private:
void resetToDefaults();
Avatar *avatar_; Avatar *avatar_;
QLabel *userIdLabel_; QLabel *userIdLabel_;