Skip to content

Commit

Permalink
Add a "create a new conda" dialog
Browse files Browse the repository at this point in the history
Signed-off-by: Geoff Hutchison <[email protected]>
  • Loading branch information
ghutchis committed Dec 31, 2023
1 parent 613efd7 commit cb302d4
Show file tree
Hide file tree
Showing 7 changed files with 251 additions and 11 deletions.
4 changes: 2 additions & 2 deletions avogadro/qtplugins/configurepython/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ avogadro_plugin(ConfigurePython
ExtensionPlugin
configurepython.h
ConfigurePython
"configurepython.cpp;configurepythondialog.cpp"
"configurepythondialog.ui"
"condadialog.cpp;configurepython.cpp;configurepythondialog.cpp"
"condadialog.ui;configurepythondialog.ui"
)
27 changes: 27 additions & 0 deletions avogadro/qtplugins/configurepython/condadialog.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/******************************************************************************
This source file is part of the Avogadro project.
This source code is released under the 3-Clause BSD License, (see "LICENSE").
******************************************************************************/

#include "condadialog.h"
#include "ui_condadialog.h"

namespace Avogadro::QtPlugins {

CondaDialog::CondaDialog(QWidget* aParent)
: QDialog(aParent), m_ui(new Ui::CondaDialog)
{
m_ui->setupUi(this);
}

CondaDialog::~CondaDialog()
{
delete m_ui;
}

QString CondaDialog::environmentName() const
{
return m_ui->environmentName->text();
}

} // namespace Avogadro::QtPlugins
38 changes: 38 additions & 0 deletions avogadro/qtplugins/configurepython/condadialog.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/******************************************************************************
This source file is part of the Avogadro project.
This source code is released under the 3-Clause BSD License, (see "LICENSE").
******************************************************************************/

#ifndef AVOGADRO_QTPLUGINS_CONDADIALOG_H
#define AVOGADRO_QTPLUGINS_CONDADIALOG_H

#include <QtWidgets/QDialog>

namespace Avogadro {
namespace QtPlugins {

namespace Ui {
class CondaDialog;
}

/**
* @brief Dialog to prompt a format and descriptor string.
*/
class CondaDialog : public QDialog
{
Q_OBJECT

public:
explicit CondaDialog(QWidget* parent = nullptr);
~CondaDialog() override;

QString environmentName() const;

private:
Ui::CondaDialog* m_ui;

Check notice on line 32 in avogadro/qtplugins/configurepython/condadialog.h

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

avogadro/qtplugins/configurepython/condadialog.h#L32

class member 'CondaDialog::m_ui' is never used.
};

} // namespace QtPlugins
} // namespace Avogadro

#endif // AVOGADRO_QTPLUGINS_CONDADIALOG_H
111 changes: 111 additions & 0 deletions avogadro/qtplugins/configurepython/condadialog.ui
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>Avogadro::QtPlugins::CondaDialog</class>
<widget class="QDialog" name="Avogadro::QtPlugins::CondaDialog">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>376</width>
<height>169</height>
</rect>
</property>
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Minimum">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="windowTitle">
<string>Python Settings…</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout_2">
<item>
<widget class="QLabel" name="textLabel">
<property name="text">
<string>Only the “base” conda environment exists.
Would you like to create a new environment for Avogadro?
This will make a copy of your base environment.
</string>
</property>
</widget>
</item>
<item>
<layout class="QHBoxLayout" name="horizontalLayout">
<item>
<widget class="QLabel" name="label">
<property name="text">
<string>Environment name:</string>
</property>
</widget>
</item>
<item>
<widget class="QLineEdit" name="environmentName">
<property name="placeholderText">
<string>avogadro</string>
</property>
</widget>
</item>
</layout>
</item>
<item>
<spacer name="verticalSpacer">
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>20</width>
<height>0</height>
</size>
</property>
</spacer>
</item>
<item>
<widget class="QDialogButtonBox" name="buttonBox">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="standardButtons">
<set>QDialogButtonBox::Cancel|QDialogButtonBox::Ok</set>
</property>
</widget>
</item>
</layout>
</widget>
<resources/>
<connections>
<connection>
<sender>buttonBox</sender>
<signal>accepted()</signal>
<receiver>Avogadro::QtPlugins::CondaDialog</receiver>
<slot>accept()</slot>
<hints>
<hint type="sourcelabel">
<x>187</x>
<y>148</y>
</hint>
<hint type="destinationlabel">
<x>187</x>
<y>84</y>
</hint>
</hints>
</connection>
<connection>
<sender>buttonBox</sender>
<signal>rejected()</signal>
<receiver>Avogadro::QtPlugins::CondaDialog</receiver>
<slot>reject()</slot>
<hints>
<hint type="sourcelabel">
<x>187</x>
<y>148</y>
</hint>
<hint type="destinationlabel">
<x>187</x>
<y>84</y>
</hint>
</hints>
</connection>
</connections>
</ui>
3 changes: 2 additions & 1 deletion avogadro/qtplugins/configurepython/configurepython.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include <QtCore/QSysInfo>
#include <QtCore/QUrl>
#include <QtGui/QDesktopServices>
#include <QtWidgets/QMessageBox>

namespace Avogadro::QtPlugins {

Expand Down Expand Up @@ -75,7 +76,7 @@ ConfigurePython::ConfigurePython(QObject* parent_)
if (miniforge.isValid()) {
QDesktopServices::openUrl(miniforge);
// open install instructions
QDesktopServices::openURL(QUrl("https://github.com/conda-forge/"
QDesktopServices::openUrl(QUrl("https://github.com/conda-forge/"
"miniforge?tab=readme-ov-file#install"));
}
}
Expand Down
76 changes: 68 additions & 8 deletions avogadro/qtplugins/configurepython/configurepythondialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,19 @@
******************************************************************************/

#include "configurepythondialog.h"
#include "condadialog.h"

#include "ui_configurepythondialog.h"

#include <QDebug>
#include <QFileInfo>
#include <QtCore/QProcess>
#include <QtCore/QSettings>

namespace Avogadro::QtPlugins {

ConfigurePythonDialog::ConfigurePythonDialog(QWidget* aParent)
: QDialog(aParent), m_ui(new Ui::ConfigurePythonDialog)
: QDialog(aParent), m_ui(new Ui::ConfigurePythonDialog), m_condaUi(nullptr)
{
m_ui->setupUi(this);
m_ui->browseWidget->hide();
Expand Down Expand Up @@ -63,8 +68,16 @@ ConfigurePythonDialog::~ConfigurePythonDialog()
void ConfigurePythonDialog::setupCondaEnvironment()
{
// suggest the user create a new environment through a dialog
if (m_condaUi == nullptr) {
m_condaUi = new CondaDialog(qobject_cast<QWidget*>(parent()));
}
int choice = m_condaUi->exec();
if (choice == QDialog::Rejected)
return;

QString newEnvironment;
QString newEnvironment = m_condaUi->environmentName();
if (newEnvironment.isEmpty())
return;

// create the environment
QProcess condaProcess;
Expand All @@ -80,23 +93,40 @@ void ConfigurePythonDialog::setupCondaEnvironment()
<< "-n" << newEnvironment << "--clone"
<< "base";
condaProcess.start(condaPath, arguments);
if (condaProcess.waitForFinished()) {
QString output = condaProcess.readAllStandardOutput();
if (output.contains("done")) {
// environment created
m_condaEnvironments << newEnvironment;
settings.setValue("interpreters/condaEnvironment", newEnvironment);
}
}
}

void ConfigurePythonDialog::condaPath() const
QString ConfigurePythonDialog::condaPath() const
{
QSettings settings;
return settings.value("interpreters/condaPath").toString();
QString path = settings.value("interpreters/condaPath").toString();
return path;
}

void ConfigurePythonDialog::condaEnvironment() const
QString ConfigurePythonDialog::condaEnvironment() const
{
return "";
QSettings settings;
QString environment =
settings.value("interpreters/condaEnvironment").toString();
return environment;
}

void ConfigurePythonDialog::setOptions(const QStringList& options)
{
m_ui->environmentCombo->clear();

// add all conda environments
foreach (const QString& environment, m_condaEnvironments) {
m_ui->environmentCombo->addItem(QString("%1 (conda)").arg(environment));
}

// get the Python version from each interpreter
QStringList versions, arguments;
QProcess process;
Expand All @@ -117,7 +147,7 @@ void ConfigurePythonDialog::setOptions(const QStringList& options)

for (int i = 0; i < options.size(); ++i) {
m_ui->environmentCombo->addItem(
QString("Python %1 (%2)").arg(versions.at(i)).arg(options.at(i)));
QString("%1 (%2)").arg(options.at(i)).arg(versions.at(i)));
}

m_ui->environmentCombo->addItem(tr("Other…"));
Expand All @@ -143,8 +173,38 @@ QString ConfigurePythonDialog::currentOption() const
return m_ui->browseWidget->fileName();

QString path = m_ui->environmentCombo->currentText();
// check if this is a conda choice
int index = path.indexOf(" (conda)");
if (index >= 0) {
// get the environment name
QString environment = path.left(index);
QSettings settings;
settings.setValue("interpreters/condaEnvironment", environment);

// activate the environment and get the path to the python interpreter
QProcess condaProcess;
QString condaPath =
settings.value("interpreters/condaPath", "conda").toString();
// check if conda is executable
if (!QFileInfo(condaPath).isExecutable())
return QString();
condaProcess.start(condaPath, QStringList()
<< "run"
<< "-n" << environment << "which"
<< "python");
if (condaProcess.waitForFinished()) {
QString output = condaProcess.readAllStandardOutput();
qDebug() << " output: " << output << "\n";
if (output.contains("python")) {
// remove the newline
output.remove("\n");
return output;
}
}
}

// remove the Python version to get the path
int index = path.indexOf(" (");
index = path.indexOf(" (");
if (index >= 0)
return path.left(index);

Expand Down
3 changes: 3 additions & 0 deletions avogadro/qtplugins/configurepython/configurepythondialog.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
namespace Avogadro {
namespace QtPlugins {

class CondaDialog;

namespace Ui {
class ConfigurePythonDialog;
}
Expand Down Expand Up @@ -46,6 +48,7 @@ protected slots:

private:
Ui::ConfigurePythonDialog* m_ui;
CondaDialog* m_condaUi;
QStringList m_condaEnvironments;
};

Expand Down

0 comments on commit cb302d4

Please sign in to comment.