Krita Source Code Documentation
Loading...
Searching...
No Matches
kis_async_action_feedback.cpp
Go to the documentation of this file.
1/*
2 * SPDX-FileCopyrightText: 2016 Dmitry Kazakov <dimula73@gmail.com>
3 *
4 * SPDX-License-Identifier: GPL-2.0-or-later
5 */
6
8
9#include <QApplication>
10#include <QEventLoop>
11#include <QFutureWatcher>
12#include <QtConcurrentRun>
13#include <QProgressDialog>
14
15
17{
18 QScopedPointer<QProgressDialog> progress;
19};
20
21KisAsyncActionFeedback::KisAsyncActionFeedback(const QString &message, QWidget *parent)
22 : m_d(new Private)
23{
24 m_d->progress.reset(new QProgressDialog(message, "", 0, 0, parent));
25 m_d->progress->setWindowModality(Qt::ApplicationModal);
26 m_d->progress->setCancelButton(0);
27 m_d->progress->setMinimumDuration(1000);
28 m_d->progress->setValue(0);
29
30 // disable close button
31 m_d->progress->setWindowFlags(Qt::CustomizeWindowHint | Qt::WindowTitleHint);
32}
33
37
38template <typename T>
39T runActionImpl(std::function<T()> func)
40{
41 QFuture<T> result = QtConcurrent::run(func);
42 QFutureWatcher<T> watcher;
43 watcher.setFuture(result);
44
45 while (watcher.isRunning()) {
46 qApp->processEvents(QEventLoop::ExcludeUserInputEvents);
47 }
48
49 watcher.waitForFinished();
50 return watcher.result();
51}
52
57
58bool KisAsyncActionFeedback::runAction(std::function<bool()> func)
59{
60 return runActionImpl(func);
61}
62
63void KisAsyncActionFeedback::runVoidAction(std::function<void()> func)
64{
65 QFuture<void> result = QtConcurrent::run(func);
66 QFutureWatcher<void> watcher;
67 watcher.setFuture(result);
68
69 while (watcher.isRunning()) {
70 qApp->processEvents(QEventLoop::ExcludeUserInputEvents);
71 }
72
73 watcher.waitForFinished();
74}
75
76void KisAsyncActionFeedback::waitForMutexLikeImpl(std::unique_ptr<MutexLikeBase> &&mutex)
77{
78 while (!mutex->try_lock()) {
79 qApp->processEvents(QEventLoop::ExcludeUserInputEvents);
80 }
81
82 mutex->unlock();
83}
84
86{
87 return i18nc("progress dialog message when the user has to wait for the image to become unlocked", "Waiting for the action to complete...");
88}
const QScopedPointer< Private > m_d
KisImportExportErrorCode runAction(std::function< KisImportExportErrorCode()> func)
KisAsyncActionFeedback(const QString &message, QWidget *parent)
void runVoidAction(std::function< void()> func)
void waitForMutexLikeImpl(std::unique_ptr< MutexLikeBase > &&mutex)
T runActionImpl(std::function< T()> func)
static QMutex mutex
QScopedPointer< QProgressDialog > progress