forked from mirror/nheko
parent
21c68c5824
commit
3097037c3d
@ -0,0 +1,88 @@ |
|||||||
|
#pragma once |
||||||
|
|
||||||
|
#include <QFrame> |
||||||
|
#include <QImage> |
||||||
|
|
||||||
|
#include "Cache.h" |
||||||
|
|
||||||
|
class FlatButton; |
||||||
|
class TextField; |
||||||
|
class Avatar; |
||||||
|
class QPixmap; |
||||||
|
class QLayout; |
||||||
|
class QLabel; |
||||||
|
|
||||||
|
template<class T> |
||||||
|
class QSharedPointer; |
||||||
|
|
||||||
|
class TopSection : public QWidget |
||||||
|
{ |
||||||
|
Q_OBJECT |
||||||
|
|
||||||
|
Q_PROPERTY(QColor textColor WRITE setTextColor READ textColor) |
||||||
|
|
||||||
|
public: |
||||||
|
TopSection(const RoomInfo &info, const QImage &img, QWidget *parent = nullptr) |
||||||
|
: QWidget{parent} |
||||||
|
, info_{std::move(info)} |
||||||
|
{ |
||||||
|
textColor_ = palette().color(QPalette::Text); |
||||||
|
avatar_ = QPixmap::fromImage(img.scaled( |
||||||
|
AvatarSize, AvatarSize, Qt::IgnoreAspectRatio, Qt::SmoothTransformation)); |
||||||
|
} |
||||||
|
|
||||||
|
QSize sizeHint() const override |
||||||
|
{ |
||||||
|
QFont font; |
||||||
|
font.setPixelSize(18); |
||||||
|
return QSize(200, AvatarSize + QFontMetrics(font).ascent() + 6 * Padding); |
||||||
|
} |
||||||
|
|
||||||
|
QColor textColor() const { return textColor_; } |
||||||
|
void setTextColor(QColor &color) { textColor_ = color; } |
||||||
|
|
||||||
|
protected: |
||||||
|
void paintEvent(QPaintEvent *event) override; |
||||||
|
|
||||||
|
private: |
||||||
|
static constexpr int AvatarSize = 72; |
||||||
|
static constexpr int Padding = 5; |
||||||
|
|
||||||
|
RoomInfo info_; |
||||||
|
QPixmap avatar_; |
||||||
|
QColor textColor_; |
||||||
|
}; |
||||||
|
|
||||||
|
namespace dialogs { |
||||||
|
|
||||||
|
class RoomSettings : public QFrame |
||||||
|
{ |
||||||
|
Q_OBJECT |
||||||
|
public: |
||||||
|
RoomSettings(const QString &room_id, |
||||||
|
QSharedPointer<Cache> cache, |
||||||
|
QWidget *parent = nullptr); |
||||||
|
|
||||||
|
signals: |
||||||
|
void closing(); |
||||||
|
|
||||||
|
protected: |
||||||
|
void paintEvent(QPaintEvent *event) override; |
||||||
|
|
||||||
|
private: |
||||||
|
static constexpr int AvatarSize = 64; |
||||||
|
|
||||||
|
void setAvatar(const QImage &img) { avatarImg_ = img; } |
||||||
|
|
||||||
|
QSharedPointer<Cache> cache_; |
||||||
|
|
||||||
|
// Button section
|
||||||
|
FlatButton *saveBtn_; |
||||||
|
FlatButton *cancelBtn_; |
||||||
|
|
||||||
|
RoomInfo info_; |
||||||
|
QString room_id_; |
||||||
|
QImage avatarImg_; |
||||||
|
}; |
||||||
|
|
||||||
|
} // dialogs
|
@ -0,0 +1,147 @@ |
|||||||
|
#include "Avatar.h" |
||||||
|
#include "Config.h" |
||||||
|
#include "FlatButton.h" |
||||||
|
#include "Painter.h" |
||||||
|
#include "Utils.h" |
||||||
|
#include "dialogs/RoomSettings.hpp" |
||||||
|
|
||||||
|
#include <QComboBox> |
||||||
|
#include <QLabel> |
||||||
|
#include <QPainter> |
||||||
|
#include <QPixmap> |
||||||
|
#include <QSharedPointer> |
||||||
|
#include <QStyleOption> |
||||||
|
#include <QVBoxLayout> |
||||||
|
|
||||||
|
using namespace dialogs; |
||||||
|
|
||||||
|
RoomSettings::RoomSettings(const QString &room_id, QSharedPointer<Cache> cache, QWidget *parent) |
||||||
|
: QFrame(parent) |
||||||
|
, cache_{cache} |
||||||
|
, room_id_{std::move(room_id)} |
||||||
|
{ |
||||||
|
setMaximumWidth(385); |
||||||
|
|
||||||
|
try { |
||||||
|
auto res = cache_->getRoomInfo({room_id_.toStdString()}); |
||||||
|
info_ = res[room_id_]; |
||||||
|
|
||||||
|
setAvatar(QImage::fromData(cache_->image(info_.avatar_url))); |
||||||
|
} catch (const lmdb::error &e) { |
||||||
|
qWarning() << "failed to retrieve room info from cache" << room_id; |
||||||
|
} |
||||||
|
|
||||||
|
auto layout = new QVBoxLayout(this); |
||||||
|
layout->setSpacing(30); |
||||||
|
layout->setMargin(20); |
||||||
|
|
||||||
|
saveBtn_ = new FlatButton("SAVE", this); |
||||||
|
saveBtn_->setFontSize(conf::btn::fontSize); |
||||||
|
cancelBtn_ = new FlatButton(tr("CANCEL"), this); |
||||||
|
cancelBtn_->setFontSize(conf::btn::fontSize); |
||||||
|
|
||||||
|
auto btnLayout = new QHBoxLayout(); |
||||||
|
btnLayout->setSpacing(0); |
||||||
|
btnLayout->setMargin(0); |
||||||
|
btnLayout->addStretch(1); |
||||||
|
btnLayout->addWidget(saveBtn_); |
||||||
|
btnLayout->addWidget(cancelBtn_); |
||||||
|
|
||||||
|
auto notifOptionLayout_ = new QHBoxLayout; |
||||||
|
notifOptionLayout_->setMargin(5); |
||||||
|
auto themeLabel_ = new QLabel(tr("Notifications"), this); |
||||||
|
auto notifCombo = new QComboBox(this); |
||||||
|
notifCombo->addItem("Nothing"); |
||||||
|
notifCombo->addItem("Mentions only"); |
||||||
|
notifCombo->addItem("All messages"); |
||||||
|
themeLabel_->setStyleSheet("font-size: 15px;"); |
||||||
|
|
||||||
|
notifOptionLayout_->addWidget(themeLabel_); |
||||||
|
notifOptionLayout_->addWidget(notifCombo, 0, Qt::AlignBottom | Qt::AlignRight); |
||||||
|
|
||||||
|
layout->addWidget(new TopSection(info_, avatarImg_, this)); |
||||||
|
layout->addLayout(notifOptionLayout_); |
||||||
|
layout->addLayout(btnLayout); |
||||||
|
|
||||||
|
connect(cancelBtn_, &FlatButton::clicked, this, &RoomSettings::closing); |
||||||
|
connect(saveBtn_, &FlatButton::clicked, this, [this]() { emit closing(); }); |
||||||
|
} |
||||||
|
|
||||||
|
void |
||||||
|
RoomSettings::paintEvent(QPaintEvent *) |
||||||
|
{ |
||||||
|
QStyleOption opt; |
||||||
|
opt.init(this); |
||||||
|
QPainter p(this); |
||||||
|
style()->drawPrimitive(QStyle::PE_Widget, &opt, &p, this); |
||||||
|
} |
||||||
|
|
||||||
|
void |
||||||
|
TopSection::paintEvent(QPaintEvent *) |
||||||
|
{ |
||||||
|
Painter p(this); |
||||||
|
PainterHighQualityEnabler hq(p); |
||||||
|
|
||||||
|
constexpr int textPadding = 23; |
||||||
|
constexpr int textStartX = AvatarSize + 5 * Padding; |
||||||
|
const int availableTextWidth = width() - textStartX; |
||||||
|
|
||||||
|
constexpr int nameFont = 15; |
||||||
|
constexpr int membersFont = 14; |
||||||
|
constexpr int labelFont = 18; |
||||||
|
|
||||||
|
QFont font; |
||||||
|
font.setPixelSize(labelFont); |
||||||
|
font.setWeight(70); |
||||||
|
|
||||||
|
p.setFont(font); |
||||||
|
p.setPen(textColor()); |
||||||
|
p.drawTextLeft(Padding, Padding, "Room settings"); |
||||||
|
p.translate(0, textPadding + QFontMetrics(p.font()).ascent()); |
||||||
|
|
||||||
|
p.save(); |
||||||
|
p.translate(textStartX, 2 * Padding); |
||||||
|
|
||||||
|
// Draw the name.
|
||||||
|
font.setPixelSize(membersFont); |
||||||
|
const auto members = QString("%1 members").arg(info_.member_count); |
||||||
|
|
||||||
|
font.setPixelSize(nameFont); |
||||||
|
const auto name = QFontMetrics(font).elidedText( |
||||||
|
QString::fromStdString(info_.name), Qt::ElideRight, availableTextWidth - 4 * Padding); |
||||||
|
|
||||||
|
font.setWeight(60); |
||||||
|
p.setFont(font); |
||||||
|
p.drawTextLeft(0, 0, name); |
||||||
|
|
||||||
|
// Draw the number of members
|
||||||
|
p.translate(0, QFontMetrics(p.font()).ascent() + 2 * Padding); |
||||||
|
|
||||||
|
font.setPixelSize(membersFont); |
||||||
|
font.setWeight(50); |
||||||
|
p.setFont(font); |
||||||
|
p.drawTextLeft(0, 0, members); |
||||||
|
p.restore(); |
||||||
|
|
||||||
|
if (avatar_.isNull()) { |
||||||
|
font.setPixelSize(AvatarSize / 2); |
||||||
|
font.setWeight(60); |
||||||
|
p.setFont(font); |
||||||
|
|
||||||
|
p.translate(Padding, Padding); |
||||||
|
p.drawLetterAvatar(utils::firstChar(name), |
||||||
|
QColor("white"), |
||||||
|
QColor("black"), |
||||||
|
AvatarSize + Padding, |
||||||
|
AvatarSize + Padding, |
||||||
|
AvatarSize); |
||||||
|
} else { |
||||||
|
QRect avatarRegion(Padding, Padding, AvatarSize, AvatarSize); |
||||||
|
|
||||||
|
QPainterPath pp; |
||||||
|
pp.addEllipse(avatarRegion.center(), AvatarSize, AvatarSize); |
||||||
|
|
||||||
|
p.setClipPath(pp); |
||||||
|
p.drawPixmap(avatarRegion, avatar_); |
||||||
|
} |
||||||
|
} |
Loading…
Reference in new issue