diff --git a/src/widgets/room/messagelistview.cpp b/src/widgets/room/messagelistview.cpp index ac1a9ab9..68b1cf32 100644 --- a/src/widgets/room/messagelistview.cpp +++ b/src/widgets/room/messagelistview.cpp @@ -1,78 +1,90 @@ /* Copyright (c) 2020 Laurent Montel This library is free software; you can redistribute it and/or modify it under the terms of the GNU Library General Public License as published by the Free Software Foundation; either version 2 of the License or ( at your option ) version 3 or, at the discretion of KDE e.V. ( which shall act as a proxy as in section 14 of the GPLv3 ), any later version. This library 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 Library General Public License for more details. You should have received a copy of the GNU Library General Public License along with this library; see the file COPYING.LIB. If not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ #include "messagelistview.h" #include "ruqola.h" #include "rocketchataccount.h" #include "messagelistdelegate.h" #include #include MessageListView::MessageListView(QWidget *parent) : QListView(parent) { auto *delegate = new MessageListDelegate(this); delegate->setRocketChatAccount(Ruqola::self()->rocketChatAccount()); setItemDelegate(delegate); setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff); setVerticalScrollMode(QAbstractItemView::ScrollPerPixel); // nicer in case of huge messages setWordWrap(true); // so that the delegate sizeHint is called again when the width changes } MessageListView::~MessageListView() { } void MessageListView::setChannelSelected(const QString &roomId) { Ruqola::self()->rocketChatAccount()->switchingToRoom(roomId); setModel(Ruqola::self()->rocketChatAccount()->messageModelForRoom(roomId)); } void MessageListView::setModel(QAbstractItemModel *newModel) { QAbstractItemModel *oldModel = model(); if (oldModel) { disconnect(oldModel, nullptr, this, nullptr); } QListView::setModel(newModel); connect(newModel, &QAbstractItemModel::rowsAboutToBeInserted, this, &MessageListView::checkIfAtBottom); connect(newModel, &QAbstractItemModel::rowsAboutToBeRemoved, this, &MessageListView::checkIfAtBottom); connect(newModel, &QAbstractItemModel::modelAboutToBeReset, this, &MessageListView::checkIfAtBottom); connect(newModel, &QAbstractItemModel::rowsInserted, this, &MessageListView::maybeScrollToBottom); connect(newModel, &QAbstractItemModel::rowsRemoved, this, &MessageListView::maybeScrollToBottom); connect(newModel, &QAbstractItemModel::modelReset, this, &MessageListView::maybeScrollToBottom); + + connect(newModel, &QAbstractItemModel::rowsInserted, this, &MessageListView::modelChanged); + connect(newModel, &QAbstractItemModel::rowsRemoved, this, &MessageListView::modelChanged); + connect(newModel, &QAbstractItemModel::modelReset, this, &MessageListView::modelChanged); + scrollToBottom(); } +void MessageListView::resizeEvent(QResizeEvent *ev) +{ + QListView::resizeEvent(ev); + + // Fix not being really at bottom when the view gets reduced by the header widget becoming taller + checkIfAtBottom(); + maybeScrollToBottom(); // this forces a layout in QAIV, which then changes the vbar max value +} + void MessageListView::checkIfAtBottom() { auto *vbar = verticalScrollBar(); mAtBottom = vbar->value() == vbar->maximum(); - Q_EMIT modelChanged(); } void MessageListView::maybeScrollToBottom() { if (mAtBottom) { scrollToBottom(); } - Q_EMIT modelChanged(); } diff --git a/src/widgets/room/messagelistview.h b/src/widgets/room/messagelistview.h index 22ef3d9c..7a5b4225 100644 --- a/src/widgets/room/messagelistview.h +++ b/src/widgets/room/messagelistview.h @@ -1,49 +1,52 @@ /* Copyright (c) 2020 Laurent Montel This library is free software; you can redistribute it and/or modify it under the terms of the GNU Library General Public License as published by the Free Software Foundation; either version 2 of the License or ( at your option ) version 3 or, at the discretion of KDE e.V. ( which shall act as a proxy as in section 14 of the GPLv3 ), any later version. This library 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 Library General Public License for more details. You should have received a copy of the GNU Library General Public License along with this library; see the file COPYING.LIB. If not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ #ifndef MESSAGELISTVIEW_H #define MESSAGELISTVIEW_H #include #include "libruqolawidgets_private_export.h" class LIBRUQOLAWIDGETS_TESTS_EXPORT MessageListView : public QListView { Q_OBJECT public: explicit MessageListView(QWidget *parent = nullptr); ~MessageListView(); void setChannelSelected(const QString &roomId); void setModel(QAbstractItemModel *newModel) override; +protected: + void resizeEvent(QResizeEvent *ev) override; + Q_SIGNALS: void modelChanged(); private Q_SLOTS: void checkIfAtBottom(); void maybeScrollToBottom(); private: bool mAtBottom = true; }; #endif // MESSAGELISTVIEW_H