diff options
| author | David Schulz <david.schulz@qt.io> | 2023-01-23 13:43:58 +0100 |
|---|---|---|
| committer | David Schulz <david.schulz@qt.io> | 2023-03-21 05:39:48 +0000 |
| commit | 7d4f123842ea937de4390b2ba783c7c13f9ea55a (patch) | |
| tree | 2a430480e14f39435c80d1b203bcdc61e6f297fc /src/plugins/python/pythoneditor.cpp | |
| parent | 5256f08b6dcd6fe5abdf80427626b56194ca209a (diff) | |
Python: add create venv action
The action can be triggered from the interpreter chooser of the editor
toolbar.
Change-Id: Ie23b68a3790525ea02883ef359b357a0d317b2f5
Reviewed-by: Christian Stenger <christian.stenger@qt.io>
Reviewed-by: <github-actions-qt-creator@cristianadam.eu>
Diffstat (limited to 'src/plugins/python/pythoneditor.cpp')
| -rw-r--r-- | src/plugins/python/pythoneditor.cpp | 37 |
1 files changed, 28 insertions, 9 deletions
diff --git a/src/plugins/python/pythoneditor.cpp b/src/plugins/python/pythoneditor.cpp index f758ae3d9b6..42771fc91a2 100644 --- a/src/plugins/python/pythoneditor.cpp +++ b/src/plugins/python/pythoneditor.cpp @@ -163,6 +163,7 @@ void PythonEditorWidget::setUserDefinedPython(const Interpreter &interpreter) } } definePythonForDocument(textDocument()->filePath(), interpreter.command); + updateInterpretersSelector(); pythonDocument->checkForPyls(); } @@ -212,33 +213,51 @@ void PythonEditorWidget::updateInterpretersSelector() m_interpreters->setText(text); }; - const FilePath currentInterpreter = detectPython(textDocument()->filePath()); + const FilePath currentInterpreterPath = detectPython(textDocument()->filePath()); const QList<Interpreter> configuredInterpreters = PythonSettings::interpreters(); - bool foundCurrentInterpreter = false; auto interpretersGroup = new QActionGroup(menu); interpretersGroup->setExclusive(true); + std::optional<Interpreter> currentInterpreter; for (const Interpreter &interpreter : configuredInterpreters) { QAction *action = interpretersGroup->addAction(interpreter.name); connect(action, &QAction::triggered, this, [this, interpreter]() { setUserDefinedPython(interpreter); }); action->setCheckable(true); - if (!foundCurrentInterpreter && interpreter.command == currentInterpreter) { - foundCurrentInterpreter = true; + if (!currentInterpreter && interpreter.command == currentInterpreterPath) { + currentInterpreter = interpreter; action->setChecked(true); setButtonText(interpreter.name); m_interpreters->setToolTip(interpreter.command.toUserOutput()); } } menu->addActions(interpretersGroup->actions()); - if (!foundCurrentInterpreter) { - if (currentInterpreter.exists()) - setButtonText(currentInterpreter.toUserOutput()); + if (!currentInterpreter) { + if (currentInterpreterPath.exists()) + setButtonText(currentInterpreterPath.toUserOutput()); else setButtonText(Tr::tr("No Python Selected")); } - if (!interpretersGroup->actions().isEmpty()) - menu->addSeparator(); + if (!interpretersGroup->actions().isEmpty()) { + menu->addSeparator(); + auto venvAction = menu->addAction(Tr::tr("Create Virtual Environment")); + connect(venvAction, + &QAction::triggered, + this, + [self = QPointer<PythonEditorWidget>(this), currentInterpreter]() { + if (!currentInterpreter) + return; + auto callback = [self](const std::optional<Interpreter> &venvInterpreter) { + if (self && venvInterpreter) + self->setUserDefinedPython(*venvInterpreter); + }; + PythonSettings::createVirtualEnvironment(self->textDocument() + ->filePath() + .parentDir(), + *currentInterpreter, + callback); + }); + } auto settingsAction = menu->addAction(Tr::tr("Manage Python Interpreters")); connect(settingsAction, &QAction::triggered, this, []() { Core::ICore::showOptionsDialog(Constants::C_PYTHONOPTIONS_PAGE_ID); |
