From c1f6d7044589f7842d19fc7b44574152ce72a20f Mon Sep 17 00:00:00 2001 From: Jarek Kobus Date: Mon, 3 Jul 2023 14:27:45 +0200 Subject: [PATCH] RunControl: Remove initiateFinish() / finished() The only difference between initiateFinish() and initiateStop() is that the initiateFinish() deletes the RunControl automatically after the final state was reached. Add setAutoDeleteOnStop() property and reuse initiateStop() and stopped() instead. Change-Id: I8e842978831b19587a8658b4443c96a04eb7a6df Reviewed-by: hjk --- src/plugins/autotest/testrunner.cpp | 2 - src/plugins/autotest/testrunner.h | 1 - src/plugins/debugger/debuggerruncontrol.cpp | 19 +++-- .../perfprofiler/perfprofilerruncontrol.cpp | 2 - src/plugins/projectexplorer/appoutputpane.cpp | 12 ++- src/plugins/projectexplorer/runcontrol.cpp | 74 ++++++------------- src/plugins/projectexplorer/runcontrol.h | 4 +- src/plugins/qmlprofiler/qmlprofilertool.cpp | 10 +-- .../tests/localqmlprofilerrunner_test.cpp | 16 ++-- 9 files changed, 56 insertions(+), 84 deletions(-) diff --git a/src/plugins/autotest/testrunner.cpp b/src/plugins/autotest/testrunner.cpp index d355317b5eb..d3b62afcbfb 100644 --- a/src/plugins/autotest/testrunner.cpp +++ b/src/plugins/autotest/testrunner.cpp @@ -589,7 +589,6 @@ void TestRunner::debugTests() runControl, &RunControl::initiateStop); connect(runControl, &RunControl::stopped, this, &TestRunner::onFinished); - m_finishDebugConnect = connect(runControl, &RunControl::finished, this, &TestRunner::onFinished); ProjectExplorerPlugin::startRunControl(runControl); if (useOutputProcessor && TestSettings::instance()->popupOnStart()) AutotestPlugin::popupResultsPane(); @@ -705,7 +704,6 @@ void TestRunner::onFinished() if (m_taskTree) m_taskTree.release()->deleteLater(); disconnect(m_stopDebugConnect); - disconnect(m_finishDebugConnect); disconnect(m_targetConnect); qDeleteAll(m_selectedTests); m_selectedTests.clear(); diff --git a/src/plugins/autotest/testrunner.h b/src/plugins/autotest/testrunner.h index 25c65f89851..8d0aac6d811 100644 --- a/src/plugins/autotest/testrunner.h +++ b/src/plugins/autotest/testrunner.h @@ -79,7 +79,6 @@ private: QMetaObject::Connection m_buildConnect; // temporarily used when debugging QMetaObject::Connection m_stopDebugConnect; - QMetaObject::Connection m_finishDebugConnect; // temporarily used for handling of switching the current target QMetaObject::Connection m_targetConnect; QTimer m_cancelTimer; diff --git a/src/plugins/debugger/debuggerruncontrol.cpp b/src/plugins/debugger/debuggerruncontrol.cpp index dc97966aa90..498f54d37dc 100644 --- a/src/plugins/debugger/debuggerruncontrol.cpp +++ b/src/plugins/debugger/debuggerruncontrol.cpp @@ -545,10 +545,12 @@ void DebuggerRunTool::start() m_engine->setRunId(d->runId); m_engine->setRunTool(this); m_engine->setCompanionEngine(m_engine2); - connect(m_engine, &DebuggerEngine::requestRunControlFinish, - runControl(), &RunControl::initiateFinish); - connect(m_engine, &DebuggerEngine::requestRunControlStop, - runControl(), &RunControl::initiateStop); + auto rc = runControl(); + connect(m_engine, &DebuggerEngine::requestRunControlFinish, rc, [rc] { + rc->setAutoDeleteOnStop(true); + rc->initiateStop(); + }, Qt::QueuedConnection); + connect(m_engine, &DebuggerEngine::requestRunControlStop, rc, &RunControl::initiateStop); connect(m_engine, &DebuggerEngine::engineStarted, this, [this] { handleEngineStarted(m_engine); }); connect(m_engine, &DebuggerEngine::engineFinished, @@ -575,10 +577,11 @@ void DebuggerRunTool::start() m_engine2->setRunTool(this); m_engine2->setCompanionEngine(m_engine); m_engine2->setSecondaryEngine(); - connect(m_engine2, &DebuggerEngine::requestRunControlFinish, - runControl(), &RunControl::initiateFinish); - connect(m_engine2, &DebuggerEngine::requestRunControlStop, - runControl(), &RunControl::initiateStop); + connect(m_engine2, &DebuggerEngine::requestRunControlFinish, rc, [rc] { + rc->setAutoDeleteOnStop(true); + rc->initiateStop(); + }, Qt::QueuedConnection); + connect(m_engine2, &DebuggerEngine::requestRunControlStop, rc, &RunControl::initiateStop); connect(m_engine2, &DebuggerEngine::engineStarted, this, [this] { handleEngineStarted(m_engine2); }); connect(m_engine2, &DebuggerEngine::engineFinished, diff --git a/src/plugins/perfprofiler/perfprofilerruncontrol.cpp b/src/plugins/perfprofiler/perfprofilerruncontrol.cpp index 3d633160133..4368c713933 100644 --- a/src/plugins/perfprofiler/perfprofilerruncontrol.cpp +++ b/src/plugins/perfprofiler/perfprofilerruncontrol.cpp @@ -185,8 +185,6 @@ void PerfProfilerRunner::start() &PerfProfilerTool::onRunControlStarted); connect(runControl(), &RunControl::stopped, PerfProfilerTool::instance(), &PerfProfilerTool::onRunControlFinished); - connect(runControl(), &RunControl::finished, PerfProfilerTool::instance(), - &PerfProfilerTool::onRunControlFinished); PerfDataReader *reader = m_perfParserWorker->reader(); if (auto prw = qobject_cast(m_perfRecordWorker)) { diff --git a/src/plugins/projectexplorer/appoutputpane.cpp b/src/plugins/projectexplorer/appoutputpane.cpp index b8ce9417e70..099430b94a6 100644 --- a/src/plugins/projectexplorer/appoutputpane.cpp +++ b/src/plugins/projectexplorer/appoutputpane.cpp @@ -406,8 +406,10 @@ void AppOutputPane::createNewOutputWindow(RunControl *rc) }); if (tab != m_runControlTabs.end()) { // Reuse this tab - if (tab->runControl) - tab->runControl->initiateFinish(); + if (tab->runControl) { + tab->runControl->setAutoDeleteOnStop(true); + tab->runControl->initiateStop(); + } tab->runControl = rc; tab->window->reset(); rc->setupFormatter(tab->window->outputFormatter()); @@ -642,8 +644,10 @@ void AppOutputPane::closeTab(int tabIndex, CloseTabMode closeTabMode) m_tabWidget->removeTab(tabIndex); delete window; - if (runControl) - runControl->initiateFinish(); // Will self-destruct. + if (runControl) { + runControl->setAutoDeleteOnStop(true); + runControl->initiateStop(); + } Utils::erase(m_runControlTabs, [tab](const RunControlTab &t) { return t.runControl == tab->runControl; }); updateCloseActions(); diff --git a/src/plugins/projectexplorer/runcontrol.cpp b/src/plugins/projectexplorer/runcontrol.cpp index 31294f47f1c..e89587a660d 100644 --- a/src/plugins/projectexplorer/runcontrol.cpp +++ b/src/plugins/projectexplorer/runcontrol.cpp @@ -211,9 +211,7 @@ enum class RunControlState Starting, // Actual process/tool starts. Running, // All good and running. Stopping, // initiateStop() was called, stop application/tool - Stopped, // all good, but stopped. Can possibly be re-started - Finishing, // Application tab manually closed - Finished // Final state, will self-destruct with deleteLater() + Stopped // all good, but stopped. Can possibly be re-started }; static QString stateName(RunControlState s) @@ -225,8 +223,6 @@ static QString stateName(RunControlState s) SN(RunControlState::Running) SN(RunControlState::Stopping) SN(RunControlState::Stopped) - SN(RunControlState::Finishing) - SN(RunControlState::Finished) } return QString("").arg(int(s)); # undef SN @@ -259,6 +255,7 @@ public: QList> m_workers; RunControlState state = RunControlState::Initialized; bool printEnvironment = false; + bool autoDelete = false; }; class RunControlPrivate : public QObject, public RunControlPrivateData @@ -274,7 +271,7 @@ public: ~RunControlPrivate() override { - QTC_CHECK(state == RunControlState::Finished || state == RunControlState::Initialized); + QTC_CHECK(state == RunControlState::Stopped || state == RunControlState::Initialized); disconnect(); q = nullptr; qDeleteAll(m_workers); @@ -407,6 +404,11 @@ RunControl::~RunControl() #endif } +void RunControl::setAutoDeleteOnStop(bool autoDelete) +{ + d->autoDelete = autoDelete; +} + void RunControl::initiateStart() { emit aboutToStart(); @@ -429,11 +431,6 @@ void RunControl::forceStop() d->forceStop(); } -void RunControl::initiateFinish() -{ - QTimer::singleShot(0, d.get(), &RunControlPrivate::initiateFinish); -} - RunWorker *RunControl::createWorker(Id workerId) { const Id deviceType = DeviceTypeKitAspect::deviceTypeId(d->kit); @@ -542,7 +539,7 @@ void RunControlPrivate::continueStart() void RunControlPrivate::initiateStop() { - if (state != RunControlState::Starting && state != RunControlState::Running) + if (state == RunControlState::Initialized) qDebug() << "Unexpected initiateStop() in state" << stateName(state); setState(RunControlState::Stopping); @@ -596,12 +593,8 @@ void RunControlPrivate::continueStopOrFinish() } RunControlState targetState; - if (state == RunControlState::Finishing) { - targetState = RunControlState::Finished; - } else { - checkState(RunControlState::Stopping); + if (state == RunControlState::Stopping) targetState = RunControlState::Stopped; - } if (allDone) { debugMessage("All Stopped"); @@ -613,7 +606,7 @@ void RunControlPrivate::continueStopOrFinish() void RunControlPrivate::forceStop() { - if (state == RunControlState::Finished) { + if (state == RunControlState::Stopped) { debugMessage("Was finished, too late to force Stop"); return; } @@ -648,14 +641,6 @@ void RunControlPrivate::forceStop() debugMessage("All Stopped"); } -void RunControlPrivate::initiateFinish() -{ - setState(RunControlState::Finishing); - debugMessage("Ramping down"); - - continueStopOrFinish(); -} - void RunControlPrivate::onWorkerStarted(RunWorker *worker) { worker->d->state = RunWorkerState::Running; @@ -688,11 +673,9 @@ void RunControlPrivate::onWorkerFailed(RunWorker *worker, const QString &msg) initiateStop(); break; case RunControlState::Stopping: - case RunControlState::Finishing: continueStopOrFinish(); break; case RunControlState::Stopped: - case RunControlState::Finished: QTC_CHECK(false); // Should not happen. continueStopOrFinish(); break; @@ -723,7 +706,7 @@ void RunControlPrivate::onWorkerStopped(RunWorker *worker) break; } - if (state == RunControlState::Finishing || state == RunControlState::Stopping) { + if (state == RunControlState::Stopping) { continueStopOrFinish(); return; } else if (worker->isEssential()) { @@ -1078,26 +1061,15 @@ bool RunControlPrivate::isAllowedTransition(RunControlState from, RunControlStat { switch (from) { case RunControlState::Initialized: - return to == RunControlState::Starting - || to == RunControlState::Finishing; + return to == RunControlState::Starting; case RunControlState::Starting: - return to == RunControlState::Running - || to == RunControlState::Stopping - || to == RunControlState::Finishing; + return to == RunControlState::Running || to == RunControlState::Stopping; case RunControlState::Running: - return to == RunControlState::Stopping - || to == RunControlState::Stopped - || to == RunControlState::Finishing; + return to == RunControlState::Stopping || to == RunControlState::Stopped; case RunControlState::Stopping: - return to == RunControlState::Stopped - || to == RunControlState::Finishing; + return to == RunControlState::Stopped; case RunControlState::Stopped: - return to == RunControlState::Starting - || to == RunControlState::Finishing; - case RunControlState::Finishing: - return to == RunControlState::Finished; - case RunControlState::Finished: - return false; + return to != RunControlState::Initialized; } return false; } @@ -1125,14 +1097,14 @@ void RunControlPrivate::setState(RunControlState newState) emit q->started(); break; case RunControlState::Stopped: - q->setApplicationProcessHandle(Utils::ProcessHandle()); + if (autoDelete) { + debugMessage("All finished. Deleting myself"); + q->deleteLater(); + } else { + q->setApplicationProcessHandle(Utils::ProcessHandle()); + } emit q->stopped(); break; - case RunControlState::Finished: - emit q->finished(); - debugMessage("All finished. Deleting myself"); - q->deleteLater(); - break; default: break; } diff --git a/src/plugins/projectexplorer/runcontrol.h b/src/plugins/projectexplorer/runcontrol.h index 799ec538dd3..5955e778710 100644 --- a/src/plugins/projectexplorer/runcontrol.h +++ b/src/plugins/projectexplorer/runcontrol.h @@ -156,11 +156,12 @@ public: void copyDataFromRunConfiguration(RunConfiguration *runConfig); void copyDataFromRunControl(RunControl *runControl); + void setAutoDeleteOnStop(bool autoDelete); + void initiateStart(); void initiateReStart(); void initiateStop(); void forceStop(); - void initiateFinish(); bool promptToStop(bool *optionalPrompt = nullptr) const; void setPromptToStop(const std::function &promptToStop); @@ -237,7 +238,6 @@ signals: void aboutToStart(); void started(); void stopped(); - void finished(); void applicationProcessHandleChanged(QPrivateSignal); // Use setApplicationProcessHandle private: diff --git a/src/plugins/qmlprofiler/qmlprofilertool.cpp b/src/plugins/qmlprofiler/qmlprofilertool.cpp index 42826cd13fd..4d67d7a1fd6 100644 --- a/src/plugins/qmlprofiler/qmlprofilertool.cpp +++ b/src/plugins/qmlprofiler/qmlprofilertool.cpp @@ -297,7 +297,10 @@ void QmlProfilerTool::finalizeRunControl(QmlProfilerRunner *runWorker) } } - auto handleStop = [this, runControl]() { + auto handleStop = [this, runControl] { + if (!d->m_toolBusy) + return; + d->m_toolBusy = false; updateRunActions(); disconnect(d->m_stopAction, &QAction::triggered, runControl, &RunControl::initiateStop); @@ -311,11 +314,6 @@ void QmlProfilerTool::finalizeRunControl(QmlProfilerRunner *runWorker) }; connect(runControl, &RunControl::stopped, this, handleStop); - connect(runControl, &RunControl::finished, this, [this, handleStop] { - if (d->m_toolBusy) - handleStop(); - }); - connect(d->m_stopAction, &QAction::triggered, runControl, &RunControl::initiateStop); updateRunActions(); diff --git a/src/plugins/qmlprofiler/tests/localqmlprofilerrunner_test.cpp b/src/plugins/qmlprofiler/tests/localqmlprofilerrunner_test.cpp index 406b8dd4191..33c0eb7388d 100644 --- a/src/plugins/qmlprofiler/tests/localqmlprofilerrunner_test.cpp +++ b/src/plugins/qmlprofiler/tests/localqmlprofilerrunner_test.cpp @@ -66,10 +66,6 @@ void LocalQmlProfilerRunnerTest::testRunner() running = false; started = false; }); - connect(runControl, &RunControl::finished, this, [&]{ - running = false; - started = false; - }); }; connectRunner(); @@ -84,7 +80,8 @@ void LocalQmlProfilerRunnerTest::testRunner() QCOMPARE(stopCount, 1); QCOMPARE(runCount, 0); - runControl->initiateFinish(); + runControl->setAutoDeleteOnStop(true); + runControl->initiateStop(); QTRY_VERIFY(runControl.isNull()); QVERIFY(profiler.isNull()); @@ -104,7 +101,8 @@ void LocalQmlProfilerRunnerTest::testRunner() QCOMPARE(stopCount, 2); QCOMPARE(runCount, 1); - runControl->initiateFinish(); + runControl->setAutoDeleteOnStop(true); + runControl->initiateStop(); QTRY_VERIFY(runControl.isNull()); QVERIFY(profiler.isNull()); @@ -123,7 +121,8 @@ void LocalQmlProfilerRunnerTest::testRunner() QCOMPARE(stopCount, 3); QCOMPARE(runCount, 2); - runControl->initiateFinish(); + runControl->setAutoDeleteOnStop(true); + runControl->initiateStop(); QTRY_VERIFY(runControl.isNull()); QVERIFY(profiler.isNull()); @@ -146,7 +145,8 @@ void LocalQmlProfilerRunnerTest::testRunner() QCOMPARE(stopCount, 4); QCOMPARE(runCount, 3); - runControl->initiateFinish(); + runControl->setAutoDeleteOnStop(true); + runControl->initiateStop(); QTRY_VERIFY(runControl.isNull()); QVERIFY(profiler.isNull()); }