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
58void KisAsyncActionFeedback::runVoidAction(std::function<void()> func)
59{
60 QFuture<void> result = QtConcurrent::run(func);
61 QFutureWatcher<void> watcher;
62 watcher.setFuture(result);
63
64 while (watcher.isRunning()) {
65 qApp->processEvents(QEventLoop::ExcludeUserInputEvents);
66 }
67
68 watcher.waitForFinished();
69}
70
71void KisAsyncActionFeedback::waitForMutexLikeImpl(std::unique_ptr<MutexLikeBase> &&mutex)
72{
73 while (!mutex->try_lock()) {
74 qApp->processEvents(QEventLoop::ExcludeUserInputEvents);
75 }
76
77 mutex->unlock();
78}
79
81{
82 return i18nc("progress dialog message when the user has to wait for the image to become unlocked", "Waiting for the action to complete...");
83}
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