Add scroll-down button
This commit is contained in:
parent
8e15a5080d
commit
845228ac6a
8 changed files with 152 additions and 0 deletions
|
|
@ -27,6 +27,7 @@
|
|||
#include "MessageEvent.h"
|
||||
#include "MessageEventContent.h"
|
||||
|
||||
#include "FloatingButton.h"
|
||||
#include "ImageItem.h"
|
||||
#include "TimelineItem.h"
|
||||
#include "TimelineView.h"
|
||||
|
|
@ -140,6 +141,16 @@ TimelineView::sliderMoved(int position)
|
|||
if (!scroll_area_->verticalScrollBar()->isVisible())
|
||||
return;
|
||||
|
||||
const int maxScroll = scroll_area_->verticalScrollBar()->maximum();
|
||||
const int currentScroll = scroll_area_->verticalScrollBar()->value();
|
||||
|
||||
if (maxScroll - currentScroll > SCROLL_BAR_GAP) {
|
||||
scrollDownBtn_->show();
|
||||
scrollDownBtn_->raise();
|
||||
} else {
|
||||
scrollDownBtn_->hide();
|
||||
}
|
||||
|
||||
// The scrollbar is high enough so we can start retrieving old events.
|
||||
if (position < SCROLL_BAR_GAP) {
|
||||
if (isTimelineFinished)
|
||||
|
|
@ -376,6 +387,18 @@ TimelineView::init()
|
|||
QSettings settings;
|
||||
local_user_ = settings.value("auth/user_id").toString();
|
||||
|
||||
QIcon icon;
|
||||
icon.addFile(":/icons/icons/ui/angle-arrow-down.png");
|
||||
scrollDownBtn_ = new FloatingButton(icon, this);
|
||||
scrollDownBtn_->setBackgroundColor(QColor("#F5F5F5"));
|
||||
scrollDownBtn_->setForegroundColor(QColor("black"));
|
||||
scrollDownBtn_->hide();
|
||||
|
||||
connect(scrollDownBtn_, &QPushButton::clicked, this, [=]() {
|
||||
const int max = scroll_area_->verticalScrollBar()->maximum();
|
||||
scroll_area_->verticalScrollBar()->setValue(max);
|
||||
});
|
||||
|
||||
top_layout_ = new QVBoxLayout(this);
|
||||
top_layout_->setSpacing(0);
|
||||
top_layout_->setMargin(0);
|
||||
|
|
|
|||
95
src/ui/FloatingButton.cc
Normal file
95
src/ui/FloatingButton.cc
Normal file
|
|
@ -0,0 +1,95 @@
|
|||
#include <QPainterPath>
|
||||
|
||||
#include "FloatingButton.h"
|
||||
|
||||
FloatingButton::FloatingButton(const QIcon &icon, QWidget *parent)
|
||||
: RaisedButton(parent)
|
||||
{
|
||||
setFixedSize(DIAMETER, DIAMETER);
|
||||
setGeometry(buttonGeometry());
|
||||
|
||||
if (parentWidget())
|
||||
parentWidget()->installEventFilter(this);
|
||||
|
||||
setFixedRippleRadius(50);
|
||||
setIcon(icon);
|
||||
raise();
|
||||
}
|
||||
|
||||
QRect
|
||||
FloatingButton::buttonGeometry() const
|
||||
{
|
||||
QWidget *parent = parentWidget();
|
||||
|
||||
if (!parent)
|
||||
return QRect();
|
||||
|
||||
return QRect(parent->width() - (OFFSET_X + DIAMETER),
|
||||
parent->height() - (OFFSET_Y + DIAMETER),
|
||||
DIAMETER,
|
||||
DIAMETER);
|
||||
}
|
||||
|
||||
bool
|
||||
FloatingButton::event(QEvent *event)
|
||||
{
|
||||
if (!parent())
|
||||
return RaisedButton::event(event);
|
||||
|
||||
switch (event->type()) {
|
||||
case QEvent::ParentChange: {
|
||||
parent()->installEventFilter(this);
|
||||
setGeometry(buttonGeometry());
|
||||
break;
|
||||
}
|
||||
case QEvent::ParentAboutToChange: {
|
||||
parent()->installEventFilter(this);
|
||||
break;
|
||||
}
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
return RaisedButton::event(event);
|
||||
}
|
||||
|
||||
bool
|
||||
FloatingButton::eventFilter(QObject *obj, QEvent *event)
|
||||
{
|
||||
const QEvent::Type type = event->type();
|
||||
|
||||
if (QEvent::Move == type || QEvent::Resize == type)
|
||||
setGeometry(buttonGeometry());
|
||||
|
||||
return RaisedButton::eventFilter(obj, event);
|
||||
}
|
||||
|
||||
void
|
||||
FloatingButton::paintEvent(QPaintEvent *event)
|
||||
{
|
||||
Q_UNUSED(event);
|
||||
|
||||
QRect square = QRect(0, 0, DIAMETER, DIAMETER);
|
||||
square.moveCenter(rect().center());
|
||||
|
||||
QPainter p(this);
|
||||
p.setRenderHints(QPainter::Antialiasing);
|
||||
|
||||
QBrush brush;
|
||||
brush.setStyle(Qt::SolidPattern);
|
||||
brush.setColor(backgroundColor());
|
||||
|
||||
p.setBrush(brush);
|
||||
p.setPen(Qt::NoPen);
|
||||
p.drawEllipse(square);
|
||||
|
||||
QRect iconGeometry(0, 0, ICON_SIZE, ICON_SIZE);
|
||||
iconGeometry.moveCenter(square.center());
|
||||
|
||||
QPixmap pixmap = icon().pixmap(QSize(ICON_SIZE, ICON_SIZE));
|
||||
QPainter icon(&pixmap);
|
||||
icon.setCompositionMode(QPainter::CompositionMode_SourceIn);
|
||||
icon.fillRect(pixmap.rect(), foregroundColor());
|
||||
|
||||
p.drawPixmap(iconGeometry, pixmap);
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue