Skip to content

Commit

Permalink
Merge pull request #1562 from ghutchis/conda-environments
Browse files Browse the repository at this point in the history
Add support for first launch dialog and conda environments
  • Loading branch information
ghutchis committed Jan 4, 2024
2 parents 28b8800 + cb302d4 commit 5289da4
Show file tree
Hide file tree
Showing 8 changed files with 398 additions and 18 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;
};

} // 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>
96 changes: 82 additions & 14 deletions avogadro/qtplugins/configurepython/configurepython.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@
#include <QAction>
#include <QtCore/QDebug>
#include <QtCore/QSettings>
#include <QtCore/QSysInfo>
#include <QtCore/QUrl>
#include <QtGui/QDesktopServices>
#include <QtWidgets/QMessageBox>

namespace Avogadro::QtPlugins {

Expand All @@ -28,6 +32,56 @@ ConfigurePython::ConfigurePython(QObject* parent_)
m_action->setText(tr("Python Settings…"));
m_action->setProperty("menu priority", 510);
connect(m_action, SIGNAL(triggered()), SLOT(showDialog()));

// check for Python on first launch
QStringList paths = pythonPaths();
QSettings settings;

if (paths.isEmpty()) { // show a warning
if (settings.contains("interpreters/firstlaunch"))
return; // the user ignored the warning

// suggest the user install Python
auto option = QMessageBox::information(
qobject_cast<QWidget*>(parent()), tr("Install Python"),
tr("Python is used for many Avogadro "
"features. Do you want to download Python?"));
if (option == QMessageBox::Yes) {
//
QUrl miniforge;
#ifdef Q_OS_WIN
// TODO: ARM or Intel? .. but conda-forge doesn't have ARM builds yet
miniforge = QUrl("https://github.com/conda-forge/miniforge/releases/"
"latest/download/Miniforge3-Windows-x86_64.exe");
#elif defined(Q_OS_MACOS)
// ARM or Intel?
if (QSysInfo::currentCpuArchitecture().contains("arm"))
miniforge = QUrl("https://github.com/conda-forge/miniforge/releases/"
"latest/download/Miniforge3-MacOSX-arm64.sh");
else
miniforge = QUrl("https://github.com/conda-forge/miniforge/releases/"
"latest/download/Miniforge3-MacOSX-x86_64.sh");
#else
QString arch = QSysInfo::currentCpuArchitecture();
if (arch.contains("arm"))
miniforge = QUrl("https://github.com/conda-forge/miniforge/releases/"
"latest/download/Miniforge3-Linux-aarch64.sh");
else if (arch.contains("ppc"))
miniforge = QUrl("https://github.com/conda-forge/miniforge/releases/"
"latest/download/Miniforge3-Linux-ppc64le.sh");
else
miniforge = QUrl("https://github.com/conda-forge/miniforge/releases/"
"latest/download/Miniforge3-Linux-x86_64.sh");
#endif
if (miniforge.isValid()) {
QDesktopServices::openUrl(miniforge);
// open install instructions
QDesktopServices::openUrl(QUrl("https://github.com/conda-forge/"
"miniforge?tab=readme-ov-file#install"));
}
}
settings.setValue("interpreters/firstlaunch", true);
}
}

ConfigurePython::~ConfigurePython()
Expand All @@ -54,21 +108,22 @@ void ConfigurePython::accept()
QSettings settings;
settings.setValue("interpreters/python", m_dialog->currentOption());

// check if the dialog has a conda environment selected
if (!m_dialog->condaEnvironment().isEmpty()) {
settings.setValue("interpreters/condaEnvironment",
m_dialog->condaEnvironment());
// get the path to conda
QString condaPath = m_dialog->condaPath();
if (!condaPath.isEmpty()) {
settings.setValue("interpreters/condaPath", condaPath);
}
}

// TODO: reload the python interpreters
}

void ConfigurePython::showDialog()
QStringList ConfigurePython::pythonPaths() const
{
if (m_dialog == nullptr) {
m_dialog = new ConfigurePythonDialog(qobject_cast<QWidget*>(parent()));
connect(m_dialog, SIGNAL(accepted()), SLOT(accept()));
}

// Populate the dialog with the current settings
// TODO:
// - check for conda environments
// - get versions for each interpreter

// Check for python interpreter in env var
QString pythonInterp =
QString::fromLocal8Bit(qgetenv("AVO_PYTHON_INTERPRETER"));
Expand All @@ -91,11 +146,24 @@ void ConfigurePython::showDialog()
<< "python";
#endif

QStringList pythonInterps = findExecutablePaths(names);
QStringList paths = findExecutablePaths(names);

// Add the current interpreter to the list if it's not already there.
if (!pythonInterps.contains(pythonInterp))
pythonInterps.prepend(pythonInterp);
if (!paths.contains(pythonInterp))
paths.prepend(pythonInterp);

return paths;
}

void ConfigurePython::showDialog()
{
if (m_dialog == nullptr) {
m_dialog = new ConfigurePythonDialog(qobject_cast<QWidget*>(parent()));
connect(m_dialog, SIGNAL(accepted()), SLOT(accept()));
}

// Populate the dialog with the current settings
QStringList pythonInterps = pythonPaths();

m_dialog->setOptions(pythonInterps);
m_dialog->show();
Expand Down
2 changes: 2 additions & 0 deletions avogadro/qtplugins/configurepython/configurepython.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ class ConfigurePython : public QtGui::ExtensionPlugin
QList<QAction*> actions() const override;
QStringList menuPath(QAction*) const override;

QStringList pythonPaths() const;

void setMolecule(QtGui::Molecule*) override {}

private slots:
Expand Down

0 comments on commit 5289da4

Please sign in to comment.