|
| 1 | +#include "PluginsManagerDlg.hpp" |
| 2 | + |
| 3 | +#include <QTableView> |
| 4 | +#include <QPushButton> |
| 5 | +#include <QGridLayout> |
| 6 | +#include <QHeaderView> |
| 7 | +#include <QDir> |
| 8 | +#include <QFileDialog> |
| 9 | +#include <QDesktopServices> |
| 10 | +#include <QCoreApplication> |
| 11 | + |
| 12 | +#include <QtNodes/NodeDelegateModelRegistry> |
| 13 | +#include <QtNodes/PluginInterface> |
| 14 | + |
| 15 | +using QtNodes::NodeDelegateModelRegistry; |
| 16 | +using QtNodes::PluginInterface; |
| 17 | + |
| 18 | +PluginsManagerDlg:: |
| 19 | +PluginsManagerDlg(QWidget* parent) |
| 20 | + : QDialog(parent) |
| 21 | +{ |
| 22 | + setMinimumSize(300, 250); |
| 23 | + |
| 24 | + _pluginsFolder.setPath(QDir::cleanPath(QCoreApplication::applicationDirPath() + QDir::separator() + R"(./nodes)")); |
| 25 | + |
| 26 | + QGridLayout *layout = new QGridLayout(); |
| 27 | + setLayout(layout); |
| 28 | + |
| 29 | + QTableView *pluginTable = new QTableView(); |
| 30 | + pluginTable->setSelectionBehavior(QAbstractItemView::SelectRows); |
| 31 | + pluginTable->setEditTriggers(QAbstractItemView::NoEditTriggers); |
| 32 | + pluginTable->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch); |
| 33 | + layout->addWidget(pluginTable, 0, 0, 1, 2); |
| 34 | + |
| 35 | + _model = new QStandardItemModel(pluginTable); |
| 36 | + |
| 37 | + _model->setColumnCount(2); |
| 38 | + _model->setHeaderData(0, Qt::Horizontal, "Name"); |
| 39 | + _model->setHeaderData(1, Qt::Horizontal, "Version"); |
| 40 | + pluginTable->setModel(_model); |
| 41 | + |
| 42 | + loadPluginsFromFolder(); |
| 43 | + |
| 44 | + pluginTable->selectRow(0); |
| 45 | + |
| 46 | + // add button |
| 47 | + QPushButton *addButton = new QPushButton("+"); |
| 48 | + layout->addWidget(addButton, 1, 0); |
| 49 | + connect(addButton, &QPushButton::clicked, this, |
| 50 | + [this]() |
| 51 | + { |
| 52 | + // TODO: How to switch different suffixes according to different os |
| 53 | + QString fileName = |
| 54 | + QFileDialog::getOpenFileName(this, |
| 55 | + tr("Load Plugin"), |
| 56 | + QCoreApplication::applicationDirPath(), |
| 57 | + tr("*.dll;*.so;*.dylib")); |
| 58 | + |
| 59 | + if (!QFileInfo::exists(fileName)) |
| 60 | + return; |
| 61 | + |
| 62 | + QFileInfo f(fileName); |
| 63 | + |
| 64 | + QFileInfo newFile( |
| 65 | + QDir::cleanPath(_pluginsFolder.absolutePath() + QDir::separator() + f.fileName())); |
| 66 | + QString const newPath = newFile.absoluteFilePath(); |
| 67 | + |
| 68 | + if (f.absoluteFilePath() == newPath) |
| 69 | + return; |
| 70 | + |
| 71 | + // Copy to the plug-in directory |
| 72 | + if (!QFile::copy(f.absoluteFilePath(), newPath)) |
| 73 | + return; |
| 74 | + |
| 75 | + PluginsManager* pluginsManager = PluginsManager::instance(); |
| 76 | + auto plugin = pluginsManager->loadPluginFromPath(newPath); |
| 77 | + if (!plugin) |
| 78 | + { |
| 79 | + QFile::remove(newPath); |
| 80 | + return; |
| 81 | + } |
| 82 | + |
| 83 | + QStandardItem *item = new QStandardItem(plugin->name()); |
| 84 | + item->setData(newPath); |
| 85 | + _model->appendRow(item); |
| 86 | + |
| 87 | + std::shared_ptr<NodeDelegateModelRegistry> reg = pluginsManager->registry(); |
| 88 | + plugin->registerDataModels(reg); |
| 89 | + }); |
| 90 | + |
| 91 | + // delete button |
| 92 | + QPushButton *deleteButton = new QPushButton("-", this); |
| 93 | + layout->addWidget(deleteButton, 1, 1); |
| 94 | + connect(deleteButton, |
| 95 | + &QPushButton::clicked, |
| 96 | + this, |
| 97 | + [this, pluginTable]() |
| 98 | + { |
| 99 | + QItemSelectionModel *selectionModel = pluginTable->selectionModel(); |
| 100 | + |
| 101 | + int row = selectionModel->currentIndex().row(); |
| 102 | + |
| 103 | + while (selectionModel->selectedRows().count() > 0) |
| 104 | + { |
| 105 | + auto rowIdx = selectionModel->selectedRows().first(); |
| 106 | + row = std::min(row, rowIdx.row()); |
| 107 | + |
| 108 | + QStandardItem *item = _model->itemFromIndex(rowIdx); |
| 109 | + |
| 110 | + PluginsManager *pluginsManager = PluginsManager::instance(); |
| 111 | + |
| 112 | + // FIXME: Unload plugin successfully, but cannot delete the plugin file |
| 113 | + if (!pluginsManager->unloadPluginFromName(item->text()) || |
| 114 | + !QFile::remove(item->data().toString())) |
| 115 | + { |
| 116 | + selectionModel->select(rowIdx, QItemSelectionModel::Deselect); |
| 117 | + continue; |
| 118 | + } |
| 119 | + |
| 120 | + _model->removeRow(rowIdx.row()); |
| 121 | + } |
| 122 | + |
| 123 | + pluginTable->selectRow(row); |
| 124 | + }); |
| 125 | +} |
| 126 | + |
| 127 | +PluginsManagerDlg:: |
| 128 | +~PluginsManagerDlg() |
| 129 | +{ |
| 130 | + // |
| 131 | +} |
| 132 | + |
| 133 | +void |
| 134 | +PluginsManagerDlg:: |
| 135 | +openPluginsFolder() |
| 136 | +{ |
| 137 | + // QDesktopServices::openUrl(QUrl::fromLocalFile(_pluginsFolderPath)); |
| 138 | + QDesktopServices::openUrl(QUrl(_pluginsFolder.absolutePath())); |
| 139 | +} |
| 140 | + |
| 141 | +QString |
| 142 | +PluginsManagerDlg:: |
| 143 | +pluginsFolderPath() const |
| 144 | +{ |
| 145 | + return _pluginsFolder.absolutePath(); |
| 146 | +} |
| 147 | + |
| 148 | +void |
| 149 | +PluginsManagerDlg:: |
| 150 | +loadPluginsFromFolder() |
| 151 | +{ |
| 152 | + PluginsManager* pluginsManager = PluginsManager::instance(); |
| 153 | + std::shared_ptr<NodeDelegateModelRegistry> registry = pluginsManager->registry(); |
| 154 | + pluginsManager->loadPlugins(_pluginsFolder.absolutePath()); |
| 155 | + |
| 156 | + for(auto l : pluginsManager->loaders()) |
| 157 | + { |
| 158 | + PluginInterface* plugin = qobject_cast<PluginInterface*>(l.second->instance()); |
| 159 | + if (!plugin) |
| 160 | + continue; |
| 161 | + |
| 162 | + QStandardItem *item = new QStandardItem(plugin->name()); |
| 163 | + item->setData(l.second->fileName()); |
| 164 | + _model->appendRow(item); |
| 165 | + |
| 166 | + plugin->registerDataModels(registry); |
| 167 | + } |
| 168 | +} |
0 commit comments