From 0973cb2991b3f8cbc42fd8f8c007729a668b36c7 Mon Sep 17 00:00:00 2001 From: blue Date: Fri, 30 Apr 2021 23:07:00 +0300 Subject: [PATCH] first lousy attempt to make load indicator in feedView --- ui/models/messagefeed.cpp | 7 +++++++ ui/models/messagefeed.h | 16 ++++++++------- ui/utils/feedview.cpp | 43 +++++++++++++++++++++++++++++++++++---- ui/utils/feedview.h | 5 +++++ 4 files changed, 60 insertions(+), 11 deletions(-) diff --git a/ui/models/messagefeed.cpp b/ui/models/messagefeed.cpp index d1024b8..09b11cd 100644 --- a/ui/models/messagefeed.cpp +++ b/ui/models/messagefeed.cpp @@ -346,6 +346,7 @@ void Models::MessageFeed::fetchMore(const QModelIndex& parent) { if (syncState == incomplete) { syncState = syncing; + emit syncStateChange(syncState); emit requestStateChange(true); if (storage.size() == 0) { @@ -373,6 +374,7 @@ void Models::MessageFeed::responseArchive(const std::list list, } else { syncState = incomplete; } + emit syncStateChange(syncState); emit requestStateChange(false); } } @@ -543,3 +545,8 @@ void Models::MessageFeed::reportLocalPathInvalid(const QString& messageId) emit dataChanged(index, index, {MessageRoles::Attach}); } + +Models::MessageFeed::SyncState Models::MessageFeed::getSyncState() const +{ + return syncState; +} diff --git a/ui/models/messagefeed.h b/ui/models/messagefeed.h index 5c5c019..cc833fa 100644 --- a/ui/models/messagefeed.h +++ b/ui/models/messagefeed.h @@ -41,7 +41,13 @@ namespace Models { class MessageFeed : public QAbstractListModel { Q_OBJECT -public: +public: + enum SyncState { + incomplete, + syncing, + complete + }; + MessageFeed(const Element* rosterItem, QObject *parent = nullptr); ~MessageFeed(); @@ -69,6 +75,7 @@ public: void incrementObservers(); void decrementObservers(); + SyncState getSyncState() const; signals: void requestArchive(const QString& before); @@ -78,6 +85,7 @@ signals: void newMessage(const Shared::Message& msg); void unnoticedMessage(const Shared::Message& msg); void localPathInvalid(const QString& path); + void syncStateChange(SyncState state); public: enum MessageRoles { @@ -102,12 +110,6 @@ protected: std::set detectChanges(const Shared::Message& msg, const QMap& data) const; private: - enum SyncState { - incomplete, - syncing, - complete - }; - //tags struct id {}; struct time {}; diff --git a/ui/utils/feedview.cpp b/ui/utils/feedview.cpp index 05302b0..f7b0f9d 100644 --- a/ui/utils/feedview.cpp +++ b/ui/utils/feedview.cpp @@ -28,6 +28,7 @@ constexpr int maxMessageHeight = 10000; constexpr int approximateSingleMessageHeight = 20; +constexpr int progressSize = 70; const std::set FeedView::geometryChangingRoles = { Models::MessageFeed::Attach, @@ -43,13 +44,18 @@ FeedView::FeedView(QWidget* parent): vo(0), specialDelegate(false), specialModel(false), - clearWidgetsMode(false) + clearWidgetsMode(false), + modelState(Models::MessageFeed::complete), + progress() { horizontalScrollBar()->setRange(0, 0); verticalScrollBar()->setSingleStep(approximateSingleMessageHeight); setMouseTracking(true); setSelectionBehavior(SelectItems); // viewport()->setAttribute(Qt::WA_Hover, true); + + progress.setParent(viewport()); + progress.resize(progressSize, progressSize); } FeedView::~FeedView() @@ -293,6 +299,13 @@ void FeedView::mouseMoveEvent(QMouseEvent* event) QAbstractItemView::mouseMoveEvent(event); } +void FeedView::resizeEvent(QResizeEvent* event) +{ + progress.move((width() - progressSize) / 2, 0); + + QAbstractItemView::resizeEvent(event); +} + QFont FeedView::getFont() const { @@ -319,14 +332,22 @@ void FeedView::setItemDelegate(QAbstractItemDelegate* delegate) } } -void FeedView::setModel(QAbstractItemModel* model) +void FeedView::setModel(QAbstractItemModel* p_model) { - QAbstractItemView::setModel(model); + if (specialModel) { + Models::MessageFeed* feed = static_cast(model()); + disconnect(feed, &Models::MessageFeed::syncStateChange, this, &FeedView::onModelSyncStateChange); + } - Models::MessageFeed* feed = dynamic_cast(model); + QAbstractItemView::setModel(p_model); + + Models::MessageFeed* feed = dynamic_cast(p_model); if (feed) { + onModelSyncStateChange(feed->getSyncState()); specialModel = true; + connect(feed, &Models::MessageFeed::syncStateChange, this, &FeedView::onModelSyncStateChange); } else { + onModelSyncStateChange(Models::MessageFeed::complete); specialModel = false; } } @@ -352,3 +373,17 @@ void FeedView::onMessageInvalidPath(const QString& messageId) } } +void FeedView::onModelSyncStateChange(Models::MessageFeed::SyncState state) +{ + if (modelState != state) { + modelState = state; + + if (state == Models::MessageFeed::syncing) { + progress.show(); + progress.start(); + } else { + progress.stop(); + progress.hide(); + } + } +} diff --git a/ui/utils/feedview.h b/ui/utils/feedview.h index 8361ec9..85ef31e 100644 --- a/ui/utils/feedview.h +++ b/ui/utils/feedview.h @@ -25,6 +25,7 @@ #include #include +#include "progress.h" /** * @todo write docs @@ -56,6 +57,7 @@ protected slots: void dataChanged(const QModelIndex & topLeft, const QModelIndex & bottomRight, const QVector & roles) override; void onMessageButtonPushed(const QString& messageId, bool download); void onMessageInvalidPath(const QString& messageId); + void onModelSyncStateChange(Models::MessageFeed::SyncState state); protected: int verticalOffset() const override; @@ -63,6 +65,7 @@ protected: void paintEvent(QPaintEvent * event) override; void updateGeometries() override; void mouseMoveEvent(QMouseEvent * event) override; + void resizeEvent(QResizeEvent * event) override; private: bool tryToCalculateGeometriesWithNoScrollbars(const QStyleOptionViewItem& option, const QAbstractItemModel* model, uint32_t totalHeight); @@ -78,6 +81,8 @@ private: bool specialDelegate; bool specialModel; bool clearWidgetsMode; + Models::MessageFeed::SyncState modelState; + Progress progress; static const std::set geometryChangingRoles;