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 
30  template <class Ret>
31  Ret wait(QString text, const QFuture<Ret>& future)
32  {
33  setLabelText(text);
34  setRange(0, 0);
35  while (!future.isFinished()) {
36  setValue(0);
37  QCoreApplication::processEvents();
38  QThread::msleep(100);
39  }
40  return future.result();
41  }
42 
43  template <class Ret, class Func, class Arg>
44  Ret runAsync(QString text, Func&& f, Arg&& arg)
45  {
46  QFuture<Ret> future = QtConcurrent::run(f, arg);
47  return wait<Ret>(text, future);
48  }
49 
50  void reportProgress(QString text, int value, int max);
51 private:
52 
53 };
54 
55 #endif // LONGUITASK_H