File indexing completed on 2024-05-12 16:01:26
0001 /* 0002 * SPDX-FileCopyrightText: 2016 Dmitry Kazakov <dimula73@gmail.com> 0003 * 0004 * SPDX-License-Identifier: GPL-2.0-or-later 0005 */ 0006 0007 #include "kis_async_action_feedback.h" 0008 0009 #include <QtConcurrent> 0010 #include <QProgressDialog> 0011 0012 0013 struct KisAsyncActionFeedback::Private 0014 { 0015 QScopedPointer<QProgressDialog> progress; 0016 }; 0017 0018 KisAsyncActionFeedback::KisAsyncActionFeedback(const QString &message, QWidget *parent) 0019 : m_d(new Private) 0020 { 0021 m_d->progress.reset(new QProgressDialog(message, "", 0, 0, parent)); 0022 m_d->progress->setWindowModality(Qt::ApplicationModal); 0023 m_d->progress->setCancelButton(0); 0024 m_d->progress->setMinimumDuration(1000); 0025 m_d->progress->setValue(0); 0026 0027 // disable close button 0028 m_d->progress->setWindowFlags(Qt::CustomizeWindowHint | Qt::WindowTitleHint); 0029 } 0030 0031 KisAsyncActionFeedback::~KisAsyncActionFeedback() 0032 { 0033 } 0034 0035 template <typename T> 0036 T runActionImpl(std::function<T()> func) 0037 { 0038 QFuture<T> result = QtConcurrent::run(func); 0039 QFutureWatcher<T> watcher; 0040 watcher.setFuture(result); 0041 0042 while (watcher.isRunning()) { 0043 qApp->processEvents(QEventLoop::ExcludeUserInputEvents); 0044 } 0045 0046 watcher.waitForFinished(); 0047 return watcher.result(); 0048 } 0049 0050 KisImportExportErrorCode KisAsyncActionFeedback::runAction(std::function<KisImportExportErrorCode()> func) 0051 { 0052 return runActionImpl(func); 0053 } 0054 0055 void KisAsyncActionFeedback::runVoidAction(std::function<void()> func) 0056 { 0057 QFuture<void> result = QtConcurrent::run(func); 0058 QFutureWatcher<void> watcher; 0059 watcher.setFuture(result); 0060 0061 while (watcher.isRunning()) { 0062 qApp->processEvents(QEventLoop::ExcludeUserInputEvents); 0063 } 0064 0065 watcher.waitForFinished(); 0066 } 0067 0068 void KisAsyncActionFeedback::waitForMutex(QMutex *mutex) 0069 { 0070 while (!mutex->tryLock()) { 0071 qApp->processEvents(QEventLoop::ExcludeUserInputEvents); 0072 } 0073 0074 mutex->unlock(); 0075 }