Skip to content
Snippets Groups Projects
Commit 3205e5fd authored by Rokas Kupstys's avatar Rokas Kupstys Committed by mujx
Browse files

Make sidebar topic expand on click and fix html formatting of elided text. (#96)

Fixes #95
parent 8299a747
No related branches found
No related tags found
No related merge requests found
...@@ -170,6 +170,7 @@ set(SRC_FILES ...@@ -170,6 +170,7 @@ set(SRC_FILES
src/ui/Badge.cc src/ui/Badge.cc
src/ui/LoadingIndicator.cc src/ui/LoadingIndicator.cc
src/ui/FlatButton.cc src/ui/FlatButton.cc
src/ui/Label.cc
src/ui/OverlayModal.cc src/ui/OverlayModal.cc
src/ui/ScrollBar.cc src/ui/ScrollBar.cc
src/ui/SnackBar.cc src/ui/SnackBar.cc
...@@ -250,6 +251,7 @@ qt5_wrap_cpp(MOC_HEADERS ...@@ -250,6 +251,7 @@ qt5_wrap_cpp(MOC_HEADERS
include/ui/Badge.h include/ui/Badge.h
include/ui/LoadingIndicator.h include/ui/LoadingIndicator.h
include/ui/FlatButton.h include/ui/FlatButton.h
include/ui/Label.h
include/ui/OverlayWidget.h include/ui/OverlayWidget.h
include/ui/ScrollBar.h include/ui/ScrollBar.h
include/ui/SnackBar.h include/ui/SnackBar.h
......
...@@ -29,6 +29,7 @@ ...@@ -29,6 +29,7 @@
#include "Avatar.h" #include "Avatar.h"
#include "FlatButton.h" #include "FlatButton.h"
#include "Label.h"
#include "LeaveRoomDialog.h" #include "LeaveRoomDialog.h"
#include "Menu.h" #include "Menu.h"
#include "OverlayModal.h" #include "OverlayModal.h"
...@@ -58,6 +59,7 @@ signals: ...@@ -58,6 +59,7 @@ signals:
protected: protected:
void paintEvent(QPaintEvent *event) override; void paintEvent(QPaintEvent *event) override;
void mousePressEvent(QMouseEvent *event) override;
private slots: private slots:
void closeLeaveRoomDialog(bool leaving); void closeLeaveRoomDialog(bool leaving);
...@@ -67,7 +69,7 @@ private: ...@@ -67,7 +69,7 @@ private:
QVBoxLayout *textLayout_; QVBoxLayout *textLayout_;
QLabel *nameLabel_; QLabel *nameLabel_;
QLabel *topicLabel_; Label *topicLabel_;
QSharedPointer<RoomSettings> roomSettings_; QSharedPointer<RoomSettings> roomSettings_;
...@@ -112,7 +114,6 @@ TopRoomBar::updateRoomName(const QString &name) ...@@ -112,7 +114,6 @@ TopRoomBar::updateRoomName(const QString &name)
inline void inline void
TopRoomBar::updateRoomTopic(QString topic) TopRoomBar::updateRoomTopic(QString topic)
{ {
topic.replace(URL_REGEX, URL_HTML);
roomTopic_ = topic; roomTopic_ = topic;
update(); update();
} }
#pragma once
#include <QLabel>
class Label : public QLabel
{
Q_OBJECT
public:
explicit Label(QWidget *parent = Q_NULLPTR, Qt::WindowFlags f = Qt::WindowFlags());
explicit Label(const QString &text,
QWidget *parent = Q_NULLPTR,
Qt::WindowFlags f = Qt::WindowFlags());
~Label() override {}
signals:
void clicked(QMouseEvent *e);
void pressed(QMouseEvent *e);
void released(QMouseEvent *e);
protected:
void mousePressEvent(QMouseEvent *e) override;
void mouseReleaseEvent(QMouseEvent *e) override;
QPoint pressPosition_;
};
...@@ -52,11 +52,15 @@ TopRoomBar::TopRoomBar(QWidget *parent) ...@@ -52,11 +52,15 @@ TopRoomBar::TopRoomBar(QWidget *parent)
QFont descriptionFont("Open Sans"); QFont descriptionFont("Open Sans");
descriptionFont.setPixelSize(conf::topRoomBar::fonts::roomDescription); descriptionFont.setPixelSize(conf::topRoomBar::fonts::roomDescription);
topicLabel_ = new QLabel(this); topicLabel_ = new Label(this);
topicLabel_->setFont(descriptionFont); topicLabel_->setFont(descriptionFont);
topicLabel_->setTextFormat(Qt::RichText); topicLabel_->setTextFormat(Qt::RichText);
topicLabel_->setTextInteractionFlags(Qt::TextBrowserInteraction); topicLabel_->setTextInteractionFlags(Qt::TextBrowserInteraction);
topicLabel_->setOpenExternalLinks(true); topicLabel_->setOpenExternalLinks(true);
connect(topicLabel_, &Label::clicked, [=](QMouseEvent *e) {
if (e->button() == Qt::LeftButton && !topicLabel_->hasSelectedText())
topicLabel_->setWordWrap(!topicLabel_->wordWrap());
});
textLayout_->addWidget(nameLabel_); textLayout_->addWidget(nameLabel_);
textLayout_->addWidget(topicLabel_); textLayout_->addWidget(topicLabel_);
...@@ -164,15 +168,34 @@ TopRoomBar::paintEvent(QPaintEvent *event) ...@@ -164,15 +168,34 @@ TopRoomBar::paintEvent(QPaintEvent *event)
QPainter painter(this); QPainter painter(this);
style()->drawPrimitive(QStyle::PE_Widget, &option, &painter, this); style()->drawPrimitive(QStyle::PE_Widget, &option, &painter, this);
// 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 = QString elidedText =
QFontMetrics(nameLabel_->font()).elidedText(roomName_, Qt::ElideRight, width()); QFontMetrics(nameLabel_->font())
.elidedText(roomName_, Qt::ElideRight, nameLabel_->width() - perFrameResize);
nameLabel_->setText(elidedText); nameLabel_->setText(elidedText);
elidedText = if (topicLabel_->wordWrap())
QFontMetrics(topicLabel_->font()).elidedText(roomTopic_, Qt::ElideRight, width()); elidedText = roomTopic_;
else
elidedText =
QFontMetrics(topicLabel_->font())
.elidedText(roomTopic_, Qt::ElideRight, topicLabel_->width() - perFrameResize);
elidedText.replace(URL_REGEX, URL_HTML);
topicLabel_->setText(elidedText); topicLabel_->setText(elidedText);
} }
void
TopRoomBar::mousePressEvent(QMouseEvent *event)
{
if (childAt(event->pos()) == topicLabel_) {
event->accept();
}
}
void void
TopRoomBar::setRoomSettings(QSharedPointer<RoomSettings> settings) TopRoomBar::setRoomSettings(QSharedPointer<RoomSettings> settings)
{ {
......
/*
* nheko Copyright (C) 2017 Konstantinos Sideris <siderisk@auth.gr>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "Label.h"
#include <QMouseEvent>
Label::Label(QWidget *parent, Qt::WindowFlags f)
: QLabel(parent, f)
{}
Label::Label(const QString &text, QWidget *parent, Qt::WindowFlags f)
: QLabel(text, parent, f)
{}
void
Label::mousePressEvent(QMouseEvent *e)
{
pressPosition_ = e->pos();
emit pressed(e);
QLabel::mousePressEvent(e);
}
void
Label::mouseReleaseEvent(QMouseEvent *e)
{
emit released(e);
if (pressPosition_ == e->pos())
emit clicked(e);
QLabel::mouseReleaseEvent(e);
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment