PIVX Core  5.6.99
P2P Digital Currency
loadingdialog.cpp
Go to the documentation of this file.
1 // Copyright (c) 2019-2020 The PIVX Core developers
2 // Distributed under the MIT software license, see the accompanying
3 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
4 
5 #include "loadingdialog.h"
6 #include "ui_loadingdialog.h"
7 
8 #include <QMovie>
9 
11  if (runnable) {
12  try {
13  runnable->run(type);
14  } catch (std::exception &e) {
15  QString errorStr = QString::fromStdString(e.what());
16  runnable->onError(errorStr, type);
17  Q_EMIT error(errorStr, type);
18  } catch (...) {
19  QString errorStr = QString::fromStdString("Unknown error running background task");
20  runnable->onError(errorStr, type);
21  Q_EMIT error(errorStr, type);
22  }
23  } else {
24  Q_EMIT error("Null runnable", type);
25  }
26  Q_EMIT finished();
27 };
28 
29 LoadingDialog::LoadingDialog(QWidget *parent, QString loadingMsg) :
30  QDialog(parent),
31  ui(new Ui::LoadingDialog)
32 {
33  ui->setupUi(this);
34 
35  this->setStyleSheet(parent->styleSheet());
36 
37  ui->frame->setProperty("cssClass", "container-loading");
38 
39  QMovie *movie = new QMovie("://ani-loading-dark");
40  ui->labelMovie->setText("");
41  ui->labelMovie->setMovie(movie);
42  movie->start();
43 
44  ui->labelMessage->setProperty("cssClass", "text-loading");
45  ui->labelDots->setProperty("cssClass", "text-loading");
46 
47  if (!loadingMsg.isEmpty()) {
48  ui->labelMessage->setText(loadingMsg);
49  }
50 }
51 
52 void LoadingDialog::execute(Runnable *runnable, int type, std::unique_ptr<WalletModel::UnlockContext> pctx)
53 {
54  loadingTimer = new QTimer(this);
55  connect(loadingTimer, &QTimer::timeout, this, &LoadingDialog::loadingTextChange);
56  loadingTimer->start(250);
57 
58  QThread* thread = new QThread;
59  Worker* worker = (pctx == nullptr ?
60  new Worker(runnable, type) :
61  new WalletWorker(runnable, type, std::move(pctx)));
62  worker->moveToThread(thread);
63  connect(thread, &QThread::started, worker, &Worker::process);
64  connect(worker, &Worker::finished, thread, &QThread::quit);
65  connect(worker, &Worker::finished, worker, &Worker::deleteLater);
66  connect(thread, &QThread::finished, thread, &QThread::deleteLater);
67  connect(worker, &Worker::finished, this, &LoadingDialog::finished);
68  thread->start();
69 }
70 
72  if (loading == 4) {
73  loading = 0;
74  } else {
75  loading++;
76  }
77  switch (loading) {
78  case 1:
79  ui->labelDots->setText("");
80  break;
81  case 2:
82  ui->labelDots->setText(".");
83  break;
84  case 3:
85  ui->labelDots->setText("..");
86  break;
87  case 4:
88  ui->labelDots->setText("...");
89  break;
90  }
91 }
92 
94  loadingTimer->stop();
95  accept();
96  deleteLater();
97 }
98 
100 {
101  delete ui;
102 }
LoadingDialog(QWidget *parent=nullptr, QString loadingMsg="")
void loadingTextChange()
Ui::LoadingDialog * ui
Definition: loadingdialog.h:78
void execute(Runnable *runnable, int type, std::unique_ptr< WalletModel::UnlockContext > pctx=nullptr)
QTimer * loadingTimer
Definition: loadingdialog.h:79
virtual void run(int type)=0
virtual void onError(QString error, int type)=0
void error(QString err, int type)
Runnable * runnable
Definition: loadingdialog.h:36
void process()
int type
Definition: loadingdialog.h:37
void finished()