Fix resize slowness on the TopBar

This commit is contained in:
Konstantinos Sideris 2018-05-25 16:13:38 +03:00
parent f533d9d7ea
commit bcba977f4c
4 changed files with 42 additions and 79 deletions

View file

@ -75,8 +75,8 @@ constexpr int communityBubble = bubble - 4;
namespace userInfoWidget { namespace userInfoWidget {
namespace fonts { namespace fonts {
constexpr int displayName = 16; constexpr int displayName = 15;
constexpr int userid = 14; constexpr int userid = 13;
} // namespace fonts } // namespace fonts
} // namespace userInfoWidget } // namespace userInfoWidget

View file

@ -22,12 +22,15 @@
#include <QImage> #include <QImage>
#include <QLabel> #include <QLabel>
#include <QPaintEvent> #include <QPaintEvent>
#include <QPainter>
#include <QPen>
#include <QSharedPointer> #include <QSharedPointer>
#include <QStyle>
#include <QStyleOption>
#include <QVBoxLayout> #include <QVBoxLayout>
class Avatar; class Avatar;
class FlatButton; class FlatButton;
class Label;
class Menu; class Menu;
class OverlayModal; class OverlayModal;
@ -55,21 +58,35 @@ signals:
void inviteUsers(QStringList users); void inviteUsers(QStringList users);
protected: protected:
void paintEvent(QPaintEvent *event) override; void mousePressEvent(QMouseEvent *) override
void mousePressEvent(QMouseEvent *event) override; {
if (roomSettings_ != nullptr)
roomSettings_->trigger();
}
void paintEvent(QPaintEvent *) override
{
QStyleOption opt;
opt.init(this);
QPainter p(this);
style()->drawPrimitive(QStyle::PE_Widget, &opt, &p, this);
p.setPen(QPen(borderColor()));
p.drawLine(QPointF(0, height()), QPointF(width(), height()));
}
private: private:
QHBoxLayout *topLayout_; QHBoxLayout *topLayout_ = nullptr;
QVBoxLayout *textLayout_; QVBoxLayout *textLayout_ = nullptr;
QLabel *nameLabel_; QLabel *nameLabel_ = nullptr;
Label *topicLabel_; QLabel *topicLabel_ = nullptr;
Menu *menu_; Menu *menu_;
QAction *leaveRoom_; QAction *leaveRoom_ = nullptr;
QAction *roomMembers_; QAction *roomMembers_ = nullptr;
QAction *roomSettings_; QAction *roomSettings_ = nullptr;
QAction *inviteUsers_; QAction *inviteUsers_ = nullptr;
FlatButton *settingsBtn_; FlatButton *settingsBtn_;
@ -77,8 +94,5 @@ private:
int buttonSize_; int buttonSize_;
QString roomName_;
QString roomTopic_;
QColor borderColor_; QColor borderColor_;
}; };

View file

@ -21,7 +21,6 @@
#include "Avatar.h" #include "Avatar.h"
#include "Config.h" #include "Config.h"
#include "FlatButton.h" #include "FlatButton.h"
#include "Label.h"
#include "MainWindow.h" #include "MainWindow.h"
#include "Menu.h" #include "Menu.h"
#include "OverlayModal.h" #include "OverlayModal.h"
@ -32,12 +31,11 @@ TopRoomBar::TopRoomBar(QWidget *parent)
: QWidget(parent) : QWidget(parent)
, buttonSize_{32} , buttonSize_{32}
{ {
setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed); setFixedHeight(60);
setFixedHeight(65);
topLayout_ = new QHBoxLayout(); topLayout_ = new QHBoxLayout(this);
topLayout_->setSpacing(10); topLayout_->setSpacing(8);
topLayout_->setMargin(10); topLayout_->setMargin(8);
avatar_ = new Avatar(this); avatar_ = new Avatar(this);
avatar_->setLetter(""); avatar_->setLetter("");
@ -52,19 +50,16 @@ TopRoomBar::TopRoomBar(QWidget *parent)
nameLabel_ = new QLabel(this); nameLabel_ = new QLabel(this);
nameLabel_->setFont(roomFont); nameLabel_->setFont(roomFont);
nameLabel_->setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Fixed);
QFont descriptionFont("Open Sans"); QFont descriptionFont("Open Sans");
descriptionFont.setPixelSize(conf::topRoomBar::fonts::roomDescription); descriptionFont.setPixelSize(conf::topRoomBar::fonts::roomDescription);
topicLabel_ = new Label(this); topicLabel_ = new QLabel(this);
topicLabel_->setFont(descriptionFont); topicLabel_->setFont(descriptionFont);
topicLabel_->setTextFormat(Qt::RichText);
topicLabel_->setTextInteractionFlags(Qt::TextBrowserInteraction); topicLabel_->setTextInteractionFlags(Qt::TextBrowserInteraction);
topicLabel_->setOpenExternalLinks(true); topicLabel_->setOpenExternalLinks(true);
connect(topicLabel_, &Label::clicked, [this](QMouseEvent *e) { topicLabel_->setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Fixed);
if (e->button() == Qt::LeftButton && !topicLabel_->hasSelectedText())
topicLabel_->setWordWrap(!topicLabel_->wordWrap());
});
textLayout_->addWidget(nameLabel_); textLayout_->addWidget(nameLabel_);
textLayout_->addWidget(topicLabel_); textLayout_->addWidget(topicLabel_);
@ -115,8 +110,6 @@ TopRoomBar::TopRoomBar(QWidget *parent)
menu_->popup( menu_->popup(
QPoint(pos.x() + buttonSize_ - menu_->sizeHint().width(), pos.y() + buttonSize_)); QPoint(pos.x() + buttonSize_ - menu_->sizeHint().width(), pos.y() + buttonSize_));
}); });
setLayout(topLayout_);
} }
void void
@ -132,51 +125,6 @@ TopRoomBar::reset()
nameLabel_->setText(""); nameLabel_->setText("");
topicLabel_->setText(""); topicLabel_->setText("");
avatar_->setLetter(""); avatar_->setLetter("");
roomName_.clear();
roomTopic_.clear();
}
void
TopRoomBar::paintEvent(QPaintEvent *event)
{
Q_UNUSED(event);
QStyleOption option;
option.initFrom(this);
QPainter painter(this);
style()->drawPrimitive(QStyle::PE_Widget, &option, &painter, this);
painter.setPen(QPen(borderColor()));
painter.drawLine(QPointF(0, height()), QPointF(width(), height()));
// Number of pixels that we can move sidebar splitter per frame. If label contains
// text which fills entire it's width then label starts blocking it's layout from
// shrinking. Making label little bit shorter leaves some space for it to shrink.
const auto perFrameResize = 20;
QString elidedText =
QFontMetrics(nameLabel_->font())
.elidedText(roomName_, Qt::ElideRight, nameLabel_->width() - perFrameResize);
nameLabel_->setText(elidedText);
if (topicLabel_->wordWrap())
elidedText = roomTopic_;
else
elidedText =
QFontMetrics(topicLabel_->font())
.elidedText(roomTopic_, Qt::ElideRight, topicLabel_->width() - perFrameResize);
elidedText.replace(conf::strings::url_regex, conf::strings::url_html);
topicLabel_->setText(elidedText);
}
void
TopRoomBar::mousePressEvent(QMouseEvent *event)
{
if (childAt(event->pos()) == topicLabel_) {
event->accept();
}
} }
void void
@ -196,13 +144,14 @@ TopRoomBar::updateRoomAvatar(const QIcon &icon)
void void
TopRoomBar::updateRoomName(const QString &name) TopRoomBar::updateRoomName(const QString &name)
{ {
roomName_ = name; nameLabel_->setText(name);
update(); update();
} }
void void
TopRoomBar::updateRoomTopic(QString topic) TopRoomBar::updateRoomTopic(QString topic)
{ {
roomTopic_ = topic; topic.replace(conf::strings::url_regex, conf::strings::url_html);
topicLabel_->setText(topic);
update(); update();
} }

View file

@ -30,7 +30,7 @@ UserInfoWidget::UserInfoWidget(QWidget *parent)
, user_id_("@user:homeserver.org") , user_id_("@user:homeserver.org")
, logoutButtonSize_{20} , logoutButtonSize_{20}
{ {
setFixedHeight(65); setFixedHeight(60);
topLayout_ = new QHBoxLayout(this); topLayout_ = new QHBoxLayout(this);
topLayout_->setSpacing(0); topLayout_->setSpacing(0);
@ -42,7 +42,7 @@ UserInfoWidget::UserInfoWidget(QWidget *parent)
userAvatar_ = new Avatar(this); userAvatar_ = new Avatar(this);
userAvatar_->setObjectName("userAvatar"); userAvatar_->setObjectName("userAvatar");
userAvatar_->setLetter(QChar('?')); userAvatar_->setLetter(QChar('?'));
userAvatar_->setSize(55); userAvatar_->setSize(45);
QFont nameFont("Open Sans SemiBold"); QFont nameFont("Open Sans SemiBold");
nameFont.setPixelSize(conf::userInfoWidget::fonts::displayName); nameFont.setPixelSize(conf::userInfoWidget::fonts::displayName);