From 812c0b84d79d5cb6299c22ca1ed1a9d2cf2ff3b9 Mon Sep 17 00:00:00 2001 From: Roman Telezhynskyi Date: Sun, 3 Sep 2023 13:52:03 +0300 Subject: [PATCH] Fix GUI freezing while nesting. --- ChangeLog.txt | 1 + src/app/valentina/mainwindowsnogui.cpp | 28 +++++++++++++++++--------- src/libs/vlayout/vlayoutgenerator.cpp | 2 -- src/libs/vlayout/vposition.cpp | 21 +++---------------- 4 files changed, 23 insertions(+), 29 deletions(-) diff --git a/ChangeLog.txt b/ChangeLog.txt index cf10ad3ae..fe2b9c3cc 100644 --- a/ChangeLog.txt +++ b/ChangeLog.txt @@ -37,6 +37,7 @@ - Support for Dark mode. - Fix issue with passing incorrect number of dimension to Tape. - New option: Pointer mode. +- Fix GUI freezing while nesting. # Valentina 0.7.52 September 12, 2022 - Fix crash when default locale is ru. diff --git a/src/app/valentina/mainwindowsnogui.cpp b/src/app/valentina/mainwindowsnogui.cpp index 6c992d5e8..d11bd9515 100644 --- a/src/app/valentina/mainwindowsnogui.cpp +++ b/src/app/valentina/mainwindowsnogui.cpp @@ -193,7 +193,7 @@ auto MainWindowsNoGUI::GenerateLayout(VLayoutGenerator &lGenerator) -> bool #if defined(Q_OS_WIN32) && QT_VERSION < QT_VERSION_CHECK(6, 0, 0) && QT_VERSION >= QT_VERSION_CHECK(5, 7, 0) QTimer *progressTimer = nullptr; #endif - + QSharedPointer progress; if (VApplication::IsGUIMode()) { @@ -206,10 +206,10 @@ auto MainWindowsNoGUI::GenerateLayout(VLayoutGenerator &lGenerator) -> bool [this, timer]() { m_taskbarProgress->setValue(static_cast(timer.elapsed() / 1000)); }); progressTimer->start(1000); #endif - + progress = QSharedPointer( new DialogLayoutProgress(timer, lGenerator.GetNestingTimeMSecs(), this)); - + connect(progress.data(), &DialogLayoutProgress::Abort, &lGenerator, &VLayoutGenerator::Abort); connect(progress.data(), &DialogLayoutProgress::Timeout, &lGenerator, &VLayoutGenerator::Timeout); @@ -278,7 +278,15 @@ auto MainWindowsNoGUI::GenerateLayout(VLayoutGenerator &lGenerator) -> bool break; } - lGenerator.Generate(timer, lGenerator.GetNestingTimeMSecs(), nestingState); + { + QEventLoop wait; + QFutureWatcher fw; + fw.setFuture( + QtConcurrent::run([&lGenerator, timer, nestingState]() + { lGenerator.Generate(timer, lGenerator.GetNestingTimeMSecs(), nestingState); })); + QObject::connect(&fw, &QFutureWatcher::finished, &wait, &QEventLoop::quit); + wait.exec(); + } if (IsTimeout()) { @@ -380,12 +388,14 @@ auto MainWindowsNoGUI::GenerateLayout(VLayoutGenerator &lGenerator) -> bool break; } - nestingState = lGenerator.State(); - if (nestingState == LayoutErrors::PrepareLayoutError || nestingState == LayoutErrors::ProcessStoped || - nestingState == LayoutErrors::TerminatedByException || - (nestingState == LayoutErrors::NoError && not qFuzzyIsNull(lGenerator.GetEfficiencyCoefficient()) && - efficiency >= lGenerator.GetEfficiencyCoefficient())) + nestingState == LayoutErrors::TerminatedByException) + { + break; + } + + if (nestingState == LayoutErrors::NoError && not qFuzzyIsNull(lGenerator.GetEfficiencyCoefficient()) && + efficiency >= lGenerator.GetEfficiencyCoefficient()) { if (not lGenerator.IsPreferOneSheetSolution() || lGenerator.PapersCount() == 1) { diff --git a/src/libs/vlayout/vlayoutgenerator.cpp b/src/libs/vlayout/vlayoutgenerator.cpp index a0cb1fa3b..0b3d0b467 100644 --- a/src/libs/vlayout/vlayoutgenerator.cpp +++ b/src/libs/vlayout/vlayoutgenerator.cpp @@ -214,8 +214,6 @@ void VLayoutGenerator::Generate(const QElapsedTimer &timer, qint64 timeout, Layo return; } - QCoreApplication::processEvents(); - if (stopGeneration.load()) { break; diff --git a/src/libs/vlayout/vposition.cpp b/src/libs/vlayout/vposition.cpp index 36594258d..7186041ea 100644 --- a/src/libs/vlayout/vposition.cpp +++ b/src/libs/vlayout/vposition.cpp @@ -238,27 +238,12 @@ auto VPosition::ArrangeDetail(const VPositionData &data, std::atomic_bool *stop, watcher.setFuture(QtConcurrent::mapped(jobs, Nest)); - while (not watcher.isStarted()) - { - QCoreApplication::processEvents(); - QThread::msleep(250); - } - - // Wait for done - do - { - QCoreApplication::processEvents(); - QThread::msleep(250); - } while (watcher.isRunning() && not stop->load()); + QEventLoop wait; + QObject::connect(&watcher, &QFutureWatcher::finished, &wait, &QEventLoop::quit); + wait.exec(); if (stop->load()) { - do - { - QCoreApplication::processEvents(); - QThread::msleep(250); - } while (watcher.isRunning()); - return bestResult; }