Python: More RunConfiguration aspect use

Include interpreter and main script settings.

Change-Id: I91c19ef124ff8b7da34bc67fb1a5ef054c3499b8
Reviewed-by: Christian Stenger <christian.stenger@qt.io>
This commit is contained in:
hjk
2018-04-26 17:04:52 +02:00
parent e8d1adc57f
commit c6cc997be4

View File

@@ -55,7 +55,6 @@
#include <utils/algorithm.h> #include <utils/algorithm.h>
#include <utils/outputformatter.h> #include <utils/outputformatter.h>
#include <utils/pathchooser.h>
#include <utils/qtcprocess.h> #include <utils/qtcprocess.h>
#include <utils/utilsicons.h> #include <utils/utilsicons.h>
@@ -75,8 +74,6 @@ using namespace Utils;
namespace PythonEditor { namespace PythonEditor {
namespace Internal { namespace Internal {
const char InterpreterKey[] = "PythonEditor.RunConfiguation.Interpreter";
const char MainScriptKey[] = "PythonEditor.RunConfiguation.MainScript";
const char PythonMimeType[] = "text/x-python-project"; // ### FIXME const char PythonMimeType[] = "text/x-python-project"; // ### FIXME
const char PythonProjectId[] = "PythonProject"; const char PythonProjectId[] = "PythonProject";
const char PythonErrorTaskCategory[] = "Task.Category.Python"; const char PythonErrorTaskCategory[] = "Task.Category.Python";
@@ -214,14 +211,22 @@ private:
const QRegularExpression filePattern; const QRegularExpression filePattern;
}; };
class PythonRunConfigurationWidget : public QWidget ////////////////////////////////////////////////////////////////
{
public:
explicit PythonRunConfigurationWidget(PythonRunConfiguration *runConfiguration);
void setInterpreter(const QString &interpreter);
private: class InterpreterAspect : public BaseStringAspect
PythonRunConfiguration *m_runConfiguration; {
Q_OBJECT
public:
explicit InterpreterAspect(RunConfiguration *rc) : BaseStringAspect(rc) {}
};
class MainScriptAspect : public BaseStringAspect
{
Q_OBJECT
public:
explicit MainScriptAspect(RunConfiguration *rc) : BaseStringAspect(rc) {}
}; };
class PythonRunConfiguration : public RunConfiguration class PythonRunConfiguration : public RunConfiguration
@@ -236,113 +241,77 @@ class PythonRunConfiguration : public RunConfiguration
public: public:
PythonRunConfiguration(Target *target, Core::Id id); PythonRunConfiguration(Target *target, Core::Id id);
QWidget *createConfigurationWidget() override; private:
QVariantMap toMap() const override; void doAdditionalSetup(const RunConfigurationCreationInfo &) final { updateTargetInformation(); }
bool fromMap(const QVariantMap &map) override; void fillConfigurationLayout(QFormLayout *layout) const final;
Runnable runnable() const override; Runnable runnable() const final;
void doAdditionalSetup(const RunConfigurationCreationInfo &info) override;
bool supportsDebugger() const { return true; } bool supportsDebugger() const { return true; }
QString mainScript() const { return m_mainScript; } QString mainScript() const { return extraAspect<MainScriptAspect>()->value(); }
QString arguments() const; QString arguments() const { return extraAspect<ArgumentsAspect>()->arguments(); }
QString interpreter() const { return m_interpreter; } QString interpreter() const { return extraAspect<InterpreterAspect>()->value(); }
void setInterpreter(const QString &interpreter) { m_interpreter = interpreter; }
private: void updateTargetInformation();
QString defaultDisplayName() const;
QString m_interpreter;
QString m_mainScript;
}; };
////////////////////////////////////////////////////////////////
PythonRunConfiguration::PythonRunConfiguration(Target *target, Core::Id id) PythonRunConfiguration::PythonRunConfiguration(Target *target, Core::Id id)
: RunConfiguration(target, id) : RunConfiguration(target, id)
{ {
const Environment sysEnv = Environment::systemEnvironment();
const QString exec = sysEnv.searchInPath("python").toString();
auto interpreterAspect = new InterpreterAspect(this);
interpreterAspect->setSettingsKey("PythonEditor.RunConfiguation.Interpreter");
interpreterAspect->setLabelText(tr("Interpreter:"));
interpreterAspect->setDisplayStyle(BaseStringAspect::PathChooserDisplay);
interpreterAspect->setValue(exec.isEmpty() ? "python" : exec);
addExtraAspect(interpreterAspect);
auto scriptAspect = new MainScriptAspect(this);
scriptAspect->setSettingsKey("PythonEditor.RunConfiguation.Script");
scriptAspect->setLabelText(tr("Script:"));
scriptAspect->setDisplayStyle(BaseStringAspect::LabelDisplay);
addExtraAspect(scriptAspect);
addExtraAspect(new LocalEnvironmentAspect(this, LocalEnvironmentAspect::BaseEnvironmentModifier())); addExtraAspect(new LocalEnvironmentAspect(this, LocalEnvironmentAspect::BaseEnvironmentModifier()));
addExtraAspect(new ArgumentsAspect(this, "PythonEditor.RunConfiguration.Arguments")); addExtraAspect(new ArgumentsAspect(this, "PythonEditor.RunConfiguration.Arguments"));
addExtraAspect(new TerminalAspect(this, "PythonEditor.RunConfiguration.UseTerminal")); addExtraAspect(new TerminalAspect(this, "PythonEditor.RunConfiguration.UseTerminal"));
setOutputFormatter<PythonOutputFormatter>(); setOutputFormatter<PythonOutputFormatter>();
connect(target, &Target::applicationTargetsChanged,
this, &PythonRunConfiguration::updateTargetInformation);
connect(target->project(), &Project::parsingFinished,
this, &PythonRunConfiguration::updateTargetInformation);
} }
QVariantMap PythonRunConfiguration::toMap() const void PythonRunConfiguration::updateTargetInformation()
{ {
QVariantMap map(RunConfiguration::toMap()); BuildTargetInfo bti = target()->applicationTargets().buildTargetInfo(buildKey());
map.insert(MainScriptKey, m_mainScript); const QString script = bti.targetFilePath.toString();
map.insert(InterpreterKey, m_interpreter); setDefaultDisplayName(tr("Run %1").arg(script));
return map; extraAspect<MainScriptAspect>()->setValue(script);
} }
bool PythonRunConfiguration::fromMap(const QVariantMap &map) void PythonRunConfiguration::fillConfigurationLayout(QFormLayout *layout) const
{ {
if (!RunConfiguration::fromMap(map)) extraAspect<InterpreterAspect>()->addToConfigurationLayout(layout);
return false; extraAspect<MainScriptAspect>()->addToConfigurationLayout(layout);
m_mainScript = map.value(MainScriptKey).toString(); extraAspect<ArgumentsAspect>()->addToConfigurationLayout(layout);
m_interpreter = map.value(InterpreterKey).toString(); extraAspect<TerminalAspect>()->addToConfigurationLayout(layout);
return true;
}
void PythonRunConfiguration::doAdditionalSetup(const RunConfigurationCreationInfo &info)
{
Environment sysEnv = Environment::systemEnvironment();
const QString exec = sysEnv.searchInPath("python").toString();
m_interpreter = exec.isEmpty() ? "python" : exec;
m_mainScript = info.buildKey;
setDefaultDisplayName(defaultDisplayName());
}
QString PythonRunConfiguration::defaultDisplayName() const
{
return tr("Run %1").arg(m_mainScript);
}
QWidget *PythonRunConfiguration::createConfigurationWidget()
{
return wrapWidget(new PythonRunConfigurationWidget(this));
} }
Runnable PythonRunConfiguration::runnable() const Runnable PythonRunConfiguration::runnable() const
{ {
StandardRunnable r; StandardRunnable r;
QtcProcess::addArg(&r.commandLineArguments, m_mainScript); QtcProcess::addArg(&r.commandLineArguments, mainScript());
QtcProcess::addArgs(&r.commandLineArguments, extraAspect<ArgumentsAspect>()->arguments()); QtcProcess::addArgs(&r.commandLineArguments, extraAspect<ArgumentsAspect>()->arguments());
r.executable = m_interpreter; r.executable = extraAspect<InterpreterAspect>()->value();
r.runMode = extraAspect<TerminalAspect>()->runMode(); r.runMode = extraAspect<TerminalAspect>()->runMode();
r.environment = extraAspect<EnvironmentAspect>()->environment(); r.environment = extraAspect<EnvironmentAspect>()->environment();
return r; return r;
} }
QString PythonRunConfiguration::arguments() const
{
auto aspect = extraAspect<ArgumentsAspect>();
QTC_ASSERT(aspect, return QString());
return aspect->arguments();
}
PythonRunConfigurationWidget::PythonRunConfigurationWidget(PythonRunConfiguration *runConfiguration)
: m_runConfiguration(runConfiguration)
{
auto fl = new QFormLayout(this);
auto interpreterChooser = new FancyLineEdit(this);
interpreterChooser->setText(runConfiguration->interpreter());
connect(interpreterChooser, &QLineEdit::textChanged,
this, &PythonRunConfigurationWidget::setInterpreter);
auto scriptLabel = new QLabel(this);
scriptLabel->setText(runConfiguration->mainScript());
fl->addRow(PythonRunConfiguration::tr("Interpreter: "), interpreterChooser);
fl->addRow(PythonRunConfiguration::tr("Script: "), scriptLabel);
runConfiguration->extraAspect<ArgumentsAspect>()->addToConfigurationLayout(fl);
runConfiguration->extraAspect<TerminalAspect>()->addToConfigurationLayout(fl);
connect(runConfiguration->target(), &Target::applicationTargetsChanged, this, [this, scriptLabel] {
scriptLabel->setText(QDir::toNativeSeparators(m_runConfiguration->mainScript()));
});
}
class PythonRunConfigurationFactory : public RunConfigurationFactory class PythonRunConfigurationFactory : public RunConfigurationFactory
{ {
public: public:
@@ -634,13 +603,6 @@ bool PythonProjectNode::renameFile(const QString &filePath, const QString &newFi
return m_project->renameFile(filePath, newFilePath); return m_project->renameFile(filePath, newFilePath);
} }
// PythonRunConfigurationWidget
void PythonRunConfigurationWidget::setInterpreter(const QString &interpreter)
{
m_runConfiguration->setInterpreter(interpreter);
}
//////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////
// //
// PythonEditorPlugin // PythonEditorPlugin
@@ -669,8 +631,8 @@ bool PythonEditorPlugin::initialize(const QStringList &arguments, QString *error
ProjectManager::registerProjectType<PythonProject>(PythonMimeType); ProjectManager::registerProjectType<PythonProject>(PythonMimeType);
auto constraint = [](RunConfiguration *runConfiguration) { auto constraint = [](RunConfiguration *runConfiguration) {
auto rc = dynamic_cast<PythonRunConfiguration *>(runConfiguration); auto aspect = runConfiguration->extraAspect<InterpreterAspect>();
return rc && !rc->interpreter().isEmpty(); return aspect && !aspect->value().isEmpty();
}; };
RunControl::registerWorker<SimpleTargetRunner>(ProjectExplorer::Constants::NORMAL_RUN_MODE, constraint); RunControl::registerWorker<SimpleTargetRunner>(ProjectExplorer::Constants::NORMAL_RUN_MODE, constraint);