| 1 |
/****************************************************************************** |
| 2 |
This source file is part of the Avogadro project. |
| 3 |
This source code is released under the 3-Clause BSD License, (see "LICENSE"). |
| 4 |
******************************************************************************/ |
| 5 |
|
| 6 |
#include "scriptcharges.h" |
| 7 |
|
| 8 |
#include "scriptchargemodel.h" |
| 9 |
|
| 10 |
#include <avogadro/calc/chargemanager.h> |
| 11 |
#include <avogadro/calc/chargemodel.h> |
| 12 |
|
| 13 |
#include <avogadro/qtgui/packagemanager.h> |
| 14 |
#include <avogadro/qtgui/utilities.h> |
| 15 |
|
| 16 |
#include <QtCore/QDebug> |
| 17 |
|
| 18 |
namespace Avogadro::QtPlugins { |
| 19 |
|
| 20 |
ScriptCharges::ScriptCharges(QObject* p) : ExtensionPlugin(p) |
| 21 |
{ |
| 22 |
refreshModels(); |
| 23 |
|
| 24 |
// Connect to PackageManager for pyproject.toml-based packages |
| 25 |
auto* pm = QtGui::PackageManager::instance(); |
| 26 |
connect(pm, &QtGui::PackageManager::featureRegistered, this, |
| 27 |
&ScriptCharges::registerFeature); |
| 28 |
connect(pm, &QtGui::PackageManager::featureRemoved, this, |
| 29 |
&ScriptCharges::unregisterFeature); |
| 30 |
|
| 31 |
// If PackageManager already replayed cached features before this plugin was |
| 32 |
// constructed, we would miss those registrations. Replay only |
| 33 |
// electrostatic-models here to catch up. |
| 34 |
pm->loadRegisteredPackages(QStringLiteral("electrostatic-models")); |
| 35 |
} |
| 36 |
|
| 37 |
ScriptCharges::~ScriptCharges() {} |
| 38 |
|
| 39 |
QList<QAction*> ScriptCharges::actions() const |
| 40 |
{ |
| 41 |
return QList<QAction*>(); |
| 42 |
} |
| 43 |
|
| 44 |
QStringList ScriptCharges::menuPath(QAction*) const |
| 45 |
{ |
| 46 |
return QStringList(); |
| 47 |
} |
| 48 |
|
| 49 |
void ScriptCharges::setMolecule(QtGui::Molecule*) {} |
| 50 |
|
| 51 |
void ScriptCharges::refreshModels() |
| 52 |
{ |
| 53 |
unregisterModels(); |
| 54 |
qDeleteAll(m_models); |
| 55 |
m_models.clear(); |
| 56 |
m_packageModels.clear(); |
| 57 |
registerModels(); |
| 58 |
} |
| 59 |
|
| 60 |
void ScriptCharges::unregisterModels() |
| 61 |
{ |
| 62 |
for (auto* model : m_models) |
| 63 |
Calc::ChargeManager::unregisterModel(model->identifier()); |
| 64 |
} |
| 65 |
|
| 66 |
void ScriptCharges::registerModels() |
| 67 |
{ |
| 68 |
for (auto* model : m_models) { |
| 69 |
if (!Calc::ChargeManager::registerModel(model->newInstance())) { |
| 70 |
qDebug() << "Could not register model" << model->identifier().c_str() |
| 71 |
<< "due to name conflict."; |
| 72 |
} |
| 73 |
} |
| 74 |
} |
| 75 |
|
| 76 |
void ScriptCharges::registerFeature(const QString& type, |
| 77 |
const QString& packageDir, |
| 78 |
const QString& command, |
| 79 |
const QString& identifier, |
| 80 |
const QVariantMap& metadata) |
| 81 |
{ |
| 82 |
if (type != QLatin1String("electrostatic-models")) |
| 83 |
return; |
| 84 |
|
| 85 |
const QString featureKey = |
| 86 |
QtGui::PackageManager::packageFeatureKey(packageDir, command, identifier); |
| 87 |
// Ignore duplicate feature emissions for the same package feature key. |
| 88 |
// This can happen when replaying cached registrations in addition to live |
| 89 |
// registration events. |
| 90 |
if (m_packageModels.contains(featureKey)) |
| 91 |
return; |
| 92 |
|
| 93 |
auto* model = new ScriptChargeModel(); |
| 94 |
model->setPackageInfo(packageDir, command, identifier); |
| 95 |
model->readMetaData(metadata); |
| 96 |
if (model->isValid()) { |
| 97 |
QString managerId = QString::fromStdString(model->identifier()); |
| 98 |
if (!Calc::ChargeManager::registerModel(model->newInstance())) { |
| 99 |
qDebug() << "Could not register charge model" << identifier |
| 100 |
<< "due to name conflict."; |
| 101 |
delete model; |
| 102 |
} else { |
| 103 |
m_models.push_back(model); |
| 104 |
m_packageModels.insert(featureKey, managerId); |
| 105 |
} |
| 106 |
} else { |
| 107 |
delete model; |
| 108 |
} |
| 109 |
} |
| 110 |
|
| 111 |
void ScriptCharges::unregisterFeature(const QString& type, |
| 112 |
const QString& packageDir, |
| 113 |
const QString& command, |
| 114 |
const QString& identifier) |
| 115 |
{ |
| 116 |
if (type != QLatin1String("electrostatic-models")) |
| 117 |
return; |
| 118 |
|
| 119 |
const QString featureKey = |
| 120 |
QtGui::PackageManager::packageFeatureKey(packageDir, command, identifier); |
| 121 |
const QList<QString> managerIds = m_packageModels.values(featureKey); |
| 122 |
if (managerIds.isEmpty()) |
| 123 |
return; |
| 124 |
|
| 125 |
m_packageModels.remove(featureKey); |
| 126 |
for (const QString& managerId : managerIds) { |
| 127 |
Calc::ChargeManager::unregisterModel(managerId.toStdString()); |
| 128 |
for (int i = m_models.size() - 1; i >= 0; --i) { |
| 129 |
if (QString::fromStdString(m_models[i]->identifier()) == managerId) |
| 130 |
delete m_models.takeAt(i); |
| 131 |
} |
| 132 |
} |
| 133 |
} |
| 134 |
|
| 135 |
} // namespace Avogadro::QtPlugins |