CuteLogger
Fast and simple logging solution for Qt based applications
longuitask.h
1 /*
2  * Copyright (c) 2020 Meltytech, LLC
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program. If not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 #ifndef LONGUITASK_H
19 #define LONGUITASK_H
20 
21 #include <QFuture>
22 #include <QProgressDialog>
23 #include <QtConcurrent/QtConcurrent>
24 
25 class LongUiTask : public QProgressDialog
26 {
27 public:
28  explicit LongUiTask(QString title);
29  ~LongUiTask();
30 
31  template<class Ret>
32  Ret wait(QString text, const QFuture<Ret> &future)
33  {
34  setLabelText(text);
35  setRange(0, 0);
36  while (!future.isFinished()) {
37  setValue(0);
38  QCoreApplication::processEvents();
39  QThread::msleep(100);
40  }
41  return future.result();
42  }
43 
44  template<class Ret, class Func, class... Args>
45  Ret runAsync(QString text, Func &&f, Args &&...args)
46  {
47  QFuture<Ret> future = QtConcurrent::run(f, std::forward<Args>(args)...);
48  return wait<Ret>(text, future);
49  }
50 
51  void reportProgress(QString text, int value, int max);
52  static void cancel();
53 };
54 
55 #endif // LONGUITASK_H