ProjectExplorer: Streamline the canRun(Project) functions

There's no real need to go through the chain of decisions
a second time to retrieve a message.

Change-Id: Id32ee486a7555f8eaf38668f23ec8fb2e179db89
Reviewed-by: Daniel Teske <daniel.teske@digia.com>
This commit is contained in:
hjk
2014-09-04 13:48:44 +02:00
parent 3df7f9498a
commit 50e951dcb4
4 changed files with 64 additions and 68 deletions

View File

@@ -419,8 +419,7 @@ bool AnalyzerManagerPrivate::isActionRunnable(AnalyzerAction *action) const
if (action->startMode() == StartRemote) if (action->startMode() == StartRemote)
return true; return true;
ProjectExplorerPlugin *pe = ProjectExplorerPlugin::instance(); return ProjectExplorerPlugin::canRun(SessionManager::startupProject(), action->tool()->runMode(), 0);
return pe->canRun(SessionManager::startupProject(), action->tool()->runMode());
} }
void AnalyzerManagerPrivate::startTool() void AnalyzerManagerPrivate::startTool()
@@ -572,16 +571,14 @@ void AnalyzerManagerPrivate::saveToolSettings(AnalyzerAction *action)
void AnalyzerManagerPrivate::updateRunActions() void AnalyzerManagerPrivate::updateRunActions()
{ {
ProjectExplorerPlugin *pe = ProjectExplorerPlugin::instance();
Project *project = SessionManager::startupProject();
QString disabledReason; QString disabledReason;
if (m_isRunning) if (m_isRunning)
disabledReason = tr("An analysis is still in progress."); disabledReason = tr("An analysis is still in progress.");
else if (!m_currentAction) else if (!m_currentAction)
disabledReason = tr("No analyzer tool selected."); disabledReason = tr("No analyzer tool selected.");
else else
disabledReason = pe->cannotRunReason(project, m_currentAction->tool()->runMode()); ProjectExplorerPlugin::canRun(SessionManager::startupProject(),
m_currentAction->tool()->runMode(), &disabledReason);
m_startAction->setEnabled(isActionRunnable(m_currentAction)); m_startAction->setEnabled(isActionRunnable(m_currentAction));
m_startAction->setToolTip(disabledReason); m_startAction->setToolTip(disabledReason);

View File

@@ -1526,10 +1526,10 @@ void DebuggerPluginPrivate::onCurrentProjectChanged(Project *project)
m_interruptAction->setEnabled(false); m_interruptAction->setEnabled(false);
m_continueAction->setEnabled(false); m_continueAction->setEnabled(false);
m_exitAction->setEnabled(false); m_exitAction->setEnabled(false);
ProjectExplorerPlugin *pe = ProjectExplorerPlugin::instance(); QString whyNot;
const bool canRun = pe->canRun(project, DebugRunMode); const bool canRun = ProjectExplorerPlugin::canRun(project, DebugRunMode, &whyNot);
m_startAction->setEnabled(canRun); m_startAction->setEnabled(canRun);
m_startAction->setToolTip(canRun ? QString() : pe->cannotRunReason(project, DebugRunMode)); m_startAction->setToolTip(whyNot);
m_debugWithoutDeployAction->setEnabled(canRun); m_debugWithoutDeployAction->setEnabled(canRun);
setProxyAction(m_visibleStartAction, Core::Id(Constants::DEBUG)); setProxyAction(m_visibleStartAction, Core::Id(Constants::DEBUG));
} }
@@ -2278,9 +2278,8 @@ void DebuggerPluginPrivate::updateState(DebuggerEngine *engine)
m_hiddenStopAction->setAction(m_interruptAction); m_hiddenStopAction->setAction(m_interruptAction);
m_localsAndExpressionsWindow->setShowLocals(false); m_localsAndExpressionsWindow->setShowLocals(false);
} else if (state == DebuggerFinished) { } else if (state == DebuggerFinished) {
ProjectExplorerPlugin *pe = ProjectExplorerPlugin::instance();
Project *project = SessionManager::startupProject(); Project *project = SessionManager::startupProject();
const bool canRun = pe->canRun(project, DebugRunMode); const bool canRun = ProjectExplorerPlugin::canRun(project, DebugRunMode);
// We don't want to do anything anymore. // We don't want to do anything anymore.
m_interruptAction->setEnabled(false); m_interruptAction->setEnabled(false);
m_continueAction->setEnabled(false); m_continueAction->setEnabled(false);
@@ -2380,28 +2379,24 @@ void DebuggerPluginPrivate::updateDebugActions()
if (m_currentEngine->state() != DebuggerNotReady) if (m_currentEngine->state() != DebuggerNotReady)
return; return;
ProjectExplorerPlugin *pe = ProjectExplorerPlugin::instance();
Project *project = SessionManager::startupProject(); Project *project = SessionManager::startupProject();
const bool canRun = pe->canRun(project, DebugRunMode); QString whyNot;
const bool canRun = ProjectExplorerPlugin::canRun(project, DebugRunMode, &whyNot);
m_startAction->setEnabled(canRun); m_startAction->setEnabled(canRun);
m_startAction->setToolTip(canRun ? QString() : pe->cannotRunReason(project, DebugRunMode)); m_startAction->setToolTip(whyNot);
m_debugWithoutDeployAction->setEnabled(canRun); m_debugWithoutDeployAction->setEnabled(canRun);
// Step into/next: Start and break at 'main' unless a debugger is running. // Step into/next: Start and break at 'main' unless a debugger is running.
if (m_snapshotHandler->currentIndex() < 0) { if (m_snapshotHandler->currentIndex() < 0) {
const bool canRunAndBreakMain = pe->canRun(project, DebugRunModeWithBreakOnMain); QString toolTip;
const bool canRunAndBreakMain
= ProjectExplorerPlugin::canRun(project, DebugRunModeWithBreakOnMain, &toolTip);
m_stepAction->setEnabled(canRunAndBreakMain); m_stepAction->setEnabled(canRunAndBreakMain);
m_nextAction->setEnabled(canRunAndBreakMain); m_nextAction->setEnabled(canRunAndBreakMain);
QString toolTip;
if (canRunAndBreakMain) { if (canRunAndBreakMain) {
QTC_ASSERT(project, return ; ); QTC_ASSERT(project, return ; );
toolTip = tr("Start \"%1\" and break at function \"main()\"") toolTip = tr("Start \"%1\" and break at function \"main()\"")
.arg(project->displayName()); .arg(project->displayName());
} else {
// Do not display long tooltip saying run mode is not supported
// for project for projects to which 'break at main' is not applicable.
if (!canRun)
toolTip = pe->cannotRunReason(project, DebugRunModeWithBreakOnMain);
} }
m_stepAction->setToolTip(toolTip); m_stepAction->setToolTip(toolTip);
m_nextAction->setToolTip(toolTip); m_nextAction->setToolTip(toolTip);

View File

@@ -2641,70 +2641,75 @@ void ProjectExplorerPlugin::updateDeployActions()
emit updateRunActions(); emit updateRunActions();
} }
bool ProjectExplorerPlugin::canRun(Project *project, RunMode runMode) bool ProjectExplorerPlugin::canRun(Project *project, RunMode runMode, QString *whyNot)
{ {
if (!project || if (!project) {
!project->activeTarget() || if (whyNot)
!project->activeTarget()->activeRunConfiguration()) { *whyNot = tr("No active project.");
return false; return false;
} }
if (d->m_projectExplorerSettings.buildBeforeDeploy if (project->needsConfiguration()) {
&& d->m_projectExplorerSettings.deployBeforeRun if (whyNot)
&& hasBuildSettings(project) *whyNot = tr("The project \"%1\" is not configured.").arg(project->displayName());
&& !buildSettingsEnabled(project).first)
return false; return false;
}
Target *target = project->activeTarget();
if (!target) {
if (whyNot)
*whyNot = tr("The project \"%1\" has no active kit.").arg(project->displayName());
return false;
}
RunConfiguration *activeRC = project->activeTarget()->activeRunConfiguration(); RunConfiguration *activeRC = target->activeRunConfiguration();
if (!activeRC) {
if (whyNot)
*whyNot = tr("The kit \"%1\" for the project \"%2\" has no active run configuration.")
.arg(target->displayName(), project->displayName());
return false;
}
bool canRun = findRunControlFactory(activeRC, runMode) if (!activeRC->isEnabled()) {
&& activeRC->isEnabled(); if (whyNot)
return canRun && !BuildManager::isBuilding(); *whyNot = activeRC->disabledReason();
} return false;
QString ProjectExplorerPlugin::cannotRunReason(Project *project, RunMode runMode)
{
if (!project)
return tr("No active project.");
if (project->needsConfiguration())
return tr("The project %1 is not configured.").arg(project->displayName());
if (!project->activeTarget())
return tr("The project \"%1\" has no active kit.").arg(project->displayName());
if (!project->activeTarget()->activeRunConfiguration())
return tr("The kit \"%1\" for the project \"%2\" has no active run configuration.")
.arg(project->activeTarget()->displayName(), project->displayName());
if (d->m_projectExplorerSettings.buildBeforeDeploy
&& d->m_projectExplorerSettings.deployBeforeRun
&& hasBuildSettings(project)) {
QPair<bool, QString> buildState = buildSettingsEnabled(project);
if (!buildState.first)
return buildState.second;
} }
RunConfiguration *activeRC = project->activeTarget()->activeRunConfiguration(); if (m_instance->d->m_projectExplorerSettings.buildBeforeDeploy
if (!activeRC->isEnabled()) && m_instance->d->m_projectExplorerSettings.deployBeforeRun
return activeRC->disabledReason(); && m_instance->hasBuildSettings(project)) {
QPair<bool, QString> buildState = m_instance->buildSettingsEnabled(project);
if (!buildState.first) {
if (whyNot)
*whyNot = buildState.second;
return false;
}
}
// shouldn't actually be shown to the user... // shouldn't actually be shown to the user...
if (!findRunControlFactory(activeRC, runMode)) if (!m_instance->findRunControlFactory(activeRC, runMode)) {
return tr("Cannot run \"%1\".").arg(activeRC->displayName()); if (whyNot)
*whyNot = tr("Cannot run \"%1\".").arg(activeRC->displayName());
return false;
}
if (BuildManager::isBuilding()) if (BuildManager::isBuilding()) {
return tr("A build is still in progress."); if (whyNot)
return QString(); *whyNot = tr("A build is still in progress.");
return false;
}
return true;
} }
void ProjectExplorerPlugin::slotUpdateRunActions() void ProjectExplorerPlugin::slotUpdateRunActions()
{ {
Project *project = SessionManager::startupProject(); Project *project = SessionManager::startupProject();
const bool state = canRun(project, NormalRunMode); QString whyNot;
const bool state = canRun(project, NormalRunMode, &whyNot);
d->m_runAction->setEnabled(state); d->m_runAction->setEnabled(state);
d->m_runAction->setToolTip(cannotRunReason(project, NormalRunMode)); d->m_runAction->setToolTip(cannotRunReason(project, NormalRunMode));
d->m_runWithoutDeployAction->setEnabled(state); d->m_runWithoutDeployAction->setEnabled(state);

View File

@@ -106,8 +106,7 @@ public:
bool coreAboutToClose(); bool coreAboutToClose();
QList<QPair<QString, QString> > recentProjects(); QList<QPair<QString, QString> > recentProjects();
bool canRun(Project *pro, RunMode runMode); static bool canRun(Project *pro, RunMode runMode, QString *whyNot = 0);
QString cannotRunReason(Project *project, RunMode runMode);
void runProject(Project *pro, RunMode, const bool forceSkipDeploy = false); void runProject(Project *pro, RunMode, const bool forceSkipDeploy = false);
void runRunConfiguration(ProjectExplorer::RunConfiguration *rc, RunMode runMode, void runRunConfiguration(ProjectExplorer::RunConfiguration *rc, RunMode runMode,
const bool forceSkipDeploy = false); const bool forceSkipDeploy = false);