|
| 1 | +#include "loadingindicator.hpp" |
| 2 | + |
| 3 | +#include <QtWidgets> |
| 4 | + |
| 5 | +class LoadingIndicator::LoadingIndicatorPrivate |
| 6 | +{ |
| 7 | +public: |
| 8 | + explicit LoadingIndicatorPrivate(LoadingIndicator *q) |
| 9 | + : q_ptr(q) |
| 10 | + , movie(new QMovie(":/gif/resource/loading.gif", {}, q_ptr)) |
| 11 | + { |
| 12 | + movie->setCacheMode(QMovie::CacheAll); |
| 13 | + } |
| 14 | + |
| 15 | + void setupUI() |
| 16 | + { |
| 17 | + auto *label = new QLabel(q_ptr); |
| 18 | + label->setFixedSize(50, 50); |
| 19 | + label->setScaledContents(true); |
| 20 | + label->setMovie(movie); |
| 21 | + movie->setScaledSize(label->size()); |
| 22 | + |
| 23 | + auto *layout = new QHBoxLayout(q_ptr); |
| 24 | + layout->addWidget(label, 0, Qt::AlignCenter); |
| 25 | + } |
| 26 | + |
| 27 | + LoadingIndicator *q_ptr; |
| 28 | + |
| 29 | + QMovie *movie; |
| 30 | +}; |
| 31 | + |
| 32 | +LoadingIndicator::LoadingIndicator(QWidget *parent) |
| 33 | + : QWidget{parent} |
| 34 | + , d_ptr(new LoadingIndicatorPrivate(this)) |
| 35 | +{ |
| 36 | + d_ptr->setupUI(); |
| 37 | + QMetaObject::invokeMethod(this, [this] { showLoading(); }, Qt::QueuedConnection); |
| 38 | +} |
| 39 | + |
| 40 | +LoadingIndicator::~LoadingIndicator() {} |
| 41 | + |
| 42 | +void LoadingIndicator::showLoading(QWidget *parent) |
| 43 | +{ |
| 44 | + if (parent == nullptr) { |
| 45 | + parent = parentWidget(); |
| 46 | + } |
| 47 | + if (parent == nullptr) { |
| 48 | + parent = qApp->activeWindow(); |
| 49 | + } |
| 50 | + setParent(parent); |
| 51 | + resize(parent->size()); |
| 52 | + |
| 53 | + show(); |
| 54 | + raise(); |
| 55 | + d_ptr->movie->start(); |
| 56 | +} |
| 57 | + |
| 58 | +void LoadingIndicator::hideLoading() |
| 59 | +{ |
| 60 | + d_ptr->movie->stop(); |
| 61 | + hide(); |
| 62 | +} |
0 commit comments