Skip to content

Commit

Permalink
Add python version to menu
Browse files Browse the repository at this point in the history
Signed-off-by: Geoff Hutchison <geoff.hutchison@gmail.com>
  • Loading branch information
ghutchis committed Dec 29, 2023
1 parent eb16456 commit c64ee1d
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 17 deletions.
13 changes: 12 additions & 1 deletion avogadro/qtplugins/configurepython/configurepython.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,23 @@ QStringList ConfigurePython::menuPath(QAction*) const
return QStringList() << tr("&Extensions");
}

void ConfigurePython::accept()
{
if (m_dialog == nullptr)
return;

// Save the settings
QSettings settings;
settings.setValue("interpreters/python", m_dialog->currentOption());

// TODO: reload the python interpreters
}

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

// Populate the dialog with the current settings
Expand Down
1 change: 1 addition & 0 deletions avogadro/qtplugins/configurepython/configurepython.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ class ConfigurePython : public QtGui::ExtensionPlugin

private slots:
void showDialog();
void accept();

private:
QAction* m_action;
Expand Down
64 changes: 49 additions & 15 deletions avogadro/qtplugins/configurepython/configurepythondialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
#include "configurepythondialog.h"
#include "ui_configurepythondialog.h"

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

namespace Avogadro::QtPlugins {

Expand All @@ -18,6 +18,9 @@ ConfigurePythonDialog::ConfigurePythonDialog(QWidget* aParent)

connect(m_ui->environmentCombo, SIGNAL(currentIndexChanged(int)),
SLOT(optionChanged(int)));

connect(m_ui->buttonBox, SIGNAL(accepted()), SLOT(accept()));
connect(m_ui->buttonBox, SIGNAL(rejected()), SLOT(reject()));
}

ConfigurePythonDialog::~ConfigurePythonDialog()
Expand All @@ -28,14 +31,32 @@ ConfigurePythonDialog::~ConfigurePythonDialog()
void ConfigurePythonDialog::setOptions(const QStringList& options)
{
m_ui->environmentCombo->clear();
m_ui->environmentCombo->addItems(options);
m_ui->environmentCombo->addItem("Other…");

QSettings settings;
QString lastUsed = settings.value("ConfigurePython/lastUsed").toString();
int index = m_ui->environmentCombo->findText(lastUsed);
if (index >= 0)
m_ui->environmentCombo->setCurrentIndex(index);
// get the Python version from each interpreter
QStringList versions, arguments;
QProcess process;
arguments << "-V";
foreach (const QString& option, options) {
process.start(option, arguments);
if (process.waitForFinished()) {
QString output = process.readAllStandardOutput();
if (output.startsWith("Python")) {
versions << output.split(" ").at(1);
} else {
versions << tr("Unknown");
}
} else {
versions << tr("Unknown");
}
}

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

m_ui->environmentCombo->addItem("Other…");
m_ui->browseWidget->hide();
}

void ConfigurePythonDialog::optionChanged(int index)
Expand All @@ -56,21 +77,34 @@ QString ConfigurePythonDialog::currentOption() const
m_ui->environmentCombo->count() - 1)
return m_ui->browseWidget->fileName();

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

return path;
}

void ConfigurePythonDialog::setCurrentOption(const QString& option)
void ConfigurePythonDialog::reject()
{
int index = m_ui->environmentCombo->findText(option);
if (index >= 0)
m_ui->environmentCombo->setCurrentIndex(index);
QDialog::reject();

emit rejected();
}

void ConfigurePythonDialog::accept()
{
QSettings settings;
settings.setValue("ConfigurePython/lastUsed", currentOption());
QDialog::accept();

emit accepted();
}

void ConfigurePythonDialog::setCurrentOption(const QString& option)
{
int index = m_ui->environmentCombo->findText(option);
if (index >= 0)
m_ui->environmentCombo->setCurrentIndex(index);
}

} // namespace Avogadro::QtPlugins
5 changes: 5 additions & 0 deletions avogadro/qtplugins/configurepython/configurepythondialog.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,14 @@ class ConfigurePythonDialog : public QDialog
void setCurrentOption(const QString& option);
QString currentOption() const;

signals:
void accepted();
void rejected();

protected slots:
void optionChanged(int index);
void accept() override;
void reject() override;

private:
Ui::ConfigurePythonDialog* m_ui;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
</sizepolicy>
</property>
<property name="windowTitle">
<string>Insert Molecule…</string>
<string>Python Settings…</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout_2">
<item>
Expand Down

0 comments on commit c64ee1d

Please sign in to comment.