From 3abb4b4b4cb4118f9e22307ce767dda8bac04365 Mon Sep 17 00:00:00 2001 From: Nikhil Tanwar <2002nikhiltanwar@gmail.com> Date: Mon, 3 Jul 2023 18:52:53 +0530 Subject: [PATCH] Loader widget is shown while catalog is being downloaded Added a new widget (KiwixLoader) which is displayed when the catalog is being downloaded. --- kiwix-desktop.pro | 2 + src/contentmanager.cpp | 1 + src/contentmanagerview.cpp | 13 +++++++ src/contentmanagerview.h | 5 +++ src/contentmanagerview.ui | 38 ++++++++++++++----- src/kiwixloader.cpp | 75 ++++++++++++++++++++++++++++++++++++++ src/kiwixloader.h | 28 ++++++++++++++ 7 files changed, 152 insertions(+), 10 deletions(-) create mode 100644 src/kiwixloader.cpp create mode 100644 src/kiwixloader.h diff --git a/kiwix-desktop.pro b/kiwix-desktop.pro index ec8c807..062512a 100644 --- a/kiwix-desktop.pro +++ b/kiwix-desktop.pro @@ -39,6 +39,7 @@ SOURCES += \ src/contenttypefilter.cpp \ src/findinpagebar.cpp \ src/kiwixconfirmbox.cpp \ + src/kiwixloader.cpp \ src/node.cpp \ src/suggestionlistworker.cpp \ src/thumbnaildownloader.cpp \ @@ -78,6 +79,7 @@ HEADERS += \ src/contenttypefilter.h \ src/findinpagebar.h \ src/kiwixconfirmbox.h \ + src/kiwixloader.h \ src/node.h \ src/suggestionlistworker.h \ src/thumbnaildownloader.h \ diff --git a/src/contentmanager.cpp b/src/contentmanager.cpp index 22424a3..27fb367 100644 --- a/src/contentmanager.cpp +++ b/src/contentmanager.cpp @@ -60,6 +60,7 @@ ContentManager::ContentManager(Library* library, kiwix::Downloader* downloader, }); connect(&m_remoteLibraryManager, &OpdsRequestManager::requestReceived, this, &ContentManager::updateRemoteLibrary); connect(mp_view->getView(), SIGNAL(customContextMenuRequested(const QPoint &)), this, SLOT(onCustomContextMenu(const QPoint &))); + connect(this, &ContentManager::pendingRequest, mp_view, &ContentManagerView::showLoader); } QList> ContentManager::getBooksList() diff --git a/src/contentmanagerview.cpp b/src/contentmanagerview.cpp index d5c29db..c6eaa68 100644 --- a/src/contentmanagerview.cpp +++ b/src/contentmanagerview.cpp @@ -23,6 +23,9 @@ ContentManagerView::ContentManagerView(QWidget *parent) searcher->setPlaceholderText(gt("search-files")); searcher->setStyleSheet(styleSheet); + loader = new KiwixLoader(mp_ui->loading); + mp_ui->stackedWidget->setCurrentIndex(0); + QIcon searchIcon = QIcon(":/icons/search.svg"); searcher->addAction(searchIcon, QLineEdit::LeadingPosition); @@ -46,3 +49,13 @@ ContentManagerView::~ContentManagerView() { } + +void ContentManagerView::showLoader(bool show) +{ + mp_ui->stackedWidget->setCurrentIndex(show); + if (show) { + loader->startAnimation(); + } else { + loader->stopAnimation(); + } +} diff --git a/src/contentmanagerview.h b/src/contentmanagerview.h index 2ecbd6f..defeb5a 100644 --- a/src/contentmanagerview.h +++ b/src/contentmanagerview.h @@ -3,6 +3,7 @@ #include #include "ui_contentmanagerview.h" +#include "kiwixloader.h" namespace Ui { class contentmanagerview; @@ -18,8 +19,12 @@ public: QTreeView* getView() { return mp_ui->m_view; } QLineEdit* &getSearcher() { return mp_ui->searcher; } +public slots: + void showLoader(bool show); + private: Ui::contentmanagerview *mp_ui; + KiwixLoader *loader; }; #endif // CONTENTMANAGERVIEW_H diff --git a/src/contentmanagerview.ui b/src/contentmanagerview.ui index 013c6e9..11f3ce7 100644 --- a/src/contentmanagerview.ui +++ b/src/contentmanagerview.ui @@ -13,16 +13,34 @@ Form - - - - - - - - - - + + + + + 1 + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/kiwixloader.cpp b/src/kiwixloader.cpp new file mode 100644 index 0000000..d6aaeb9 --- /dev/null +++ b/src/kiwixloader.cpp @@ -0,0 +1,75 @@ +#include "kiwixloader.h" +#include +#include +#include +#include + +KiwixLoader::KiwixLoader(QWidget *parent) + : QWidget(parent), m_timer(nullptr) +{ + setFixedSize(parent->width(), parent->height()); + m_timer = new QTimer(this); + connect(m_timer, &QTimer::timeout, this, &KiwixLoader::updateAnimation); +} + +KiwixLoader::~KiwixLoader() +{ +} + +void KiwixLoader::stopAnimation() +{ + m_timer->stop(); +} + +void KiwixLoader::startAnimation() +{ + m_timer->start(20); +} + +void createArc(QPainter &painter, int startAngle, int spanAngle, QRect rectangle, QPen pen) +{ + painter.setRenderHint(QPainter::Antialiasing); + int arcX = rectangle.x(); + int arcY = rectangle.y(); + int arcW = rectangle.width(); + int arcH = rectangle.height(); + QPainterPath path; + path.moveTo(arcX + arcW, arcY + arcH/2); + path.arcTo(rectangle, startAngle, spanAngle); + painter.strokePath(path, pen); +} + +void KiwixLoader::paintEvent(QPaintEvent *event) +{ + QPainter painter(this); + painter.setRenderHint(QPainter::Antialiasing); + + int width = 100; + int height = 100; + setFixedSize(this->parentWidget()->width(), this->parentWidget()->height()); + int centerX = this->parentWidget()->width()/2 - width; + int centerY = this->parentWidget()->height()/2 - height; + + QPen pen; + pen.setWidth(5); + painter.setPen(pen); + painter.setRenderHint(QPainter::Antialiasing); + + QRect rectangle(centerX, centerY, width, height); + + pen.setColor("#eaecf0"); + createArc(painter, 0, 360, rectangle, pen); + + int startAngle = 0; + int spanAngle = -progress; + pen.setColor("#3366cc"); + createArc(painter, startAngle, spanAngle, rectangle, pen); +} + +void KiwixLoader::updateAnimation() +{ + progress += 10; + if (progress == 360) + progress = 0; + update(); +} diff --git a/src/kiwixloader.h b/src/kiwixloader.h new file mode 100644 index 0000000..34a77ca --- /dev/null +++ b/src/kiwixloader.h @@ -0,0 +1,28 @@ +#ifndef KIWIXLOADER_H +#define KIWIXLOADER_H + +#include +#include + +class KiwixLoader : public QWidget +{ + Q_OBJECT + +public: + explicit KiwixLoader(QWidget *parent = nullptr); + ~KiwixLoader(); + void startAnimation(); + void stopAnimation(); + +protected: + void paintEvent(QPaintEvent *event) override; + +private slots: + void updateAnimation(); + +private: + QTimer *m_timer; + int progress = 0; +}; + +#endif // KIWIXLOADER_H