-
Notifications
You must be signed in to change notification settings - Fork 844
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add plug-in management dialog, delete plug-in map
- Loading branch information
Showing
8 changed files
with
297 additions
and
88 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,168 @@ | ||
#include "PluginsManagerDlg.hpp" | ||
|
||
#include <QTableView> | ||
#include <QPushButton> | ||
#include <QGridLayout> | ||
#include <QHeaderView> | ||
#include <QDir> | ||
#include <QFileDialog> | ||
#include <QDesktopServices> | ||
#include <QCoreApplication> | ||
|
||
#include <QtNodes/NodeDelegateModelRegistry> | ||
#include <QtNodes/PluginInterface> | ||
|
||
using QtNodes::NodeDelegateModelRegistry; | ||
using QtNodes::PluginInterface; | ||
|
||
PluginsManagerDlg:: | ||
PluginsManagerDlg(QWidget* parent) | ||
: QDialog(parent) | ||
{ | ||
setMinimumSize(300, 250); | ||
|
||
_pluginsFolder.setPath(QDir::cleanPath(QCoreApplication::applicationDirPath() + QDir::separator() + R"(./nodes)")); | ||
|
||
QGridLayout *layout = new QGridLayout(); | ||
setLayout(layout); | ||
|
||
QTableView *pluginTable = new QTableView(); | ||
pluginTable->setSelectionBehavior(QAbstractItemView::SelectRows); | ||
pluginTable->setEditTriggers(QAbstractItemView::NoEditTriggers); | ||
pluginTable->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch); | ||
layout->addWidget(pluginTable, 0, 0, 1, 2); | ||
|
||
_model = new QStandardItemModel(pluginTable); | ||
|
||
_model->setColumnCount(2); | ||
_model->setHeaderData(0, Qt::Horizontal, "Name"); | ||
_model->setHeaderData(1, Qt::Horizontal, "Version"); | ||
pluginTable->setModel(_model); | ||
|
||
loadPluginsFromFolder(); | ||
|
||
pluginTable->selectRow(0); | ||
|
||
// add button | ||
QPushButton *addButton = new QPushButton("+"); | ||
layout->addWidget(addButton, 1, 0); | ||
connect(addButton, &QPushButton::clicked, this, | ||
[this]() | ||
{ | ||
// TODO: How to switch different suffixes according to different os | ||
QString fileName = | ||
QFileDialog::getOpenFileName(this, | ||
tr("Load Plugin"), | ||
QCoreApplication::applicationDirPath(), | ||
tr("*.dll;*.so;*.dylib")); | ||
|
||
if (!QFileInfo::exists(fileName)) | ||
return; | ||
|
||
QFileInfo f(fileName); | ||
|
||
QFileInfo newFile( | ||
QDir::cleanPath(_pluginsFolder.absolutePath() + QDir::separator() + f.fileName())); | ||
QString const newPath = newFile.absoluteFilePath(); | ||
|
||
if (f.absoluteFilePath() == newPath) | ||
return; | ||
|
||
// Copy to the plug-in directory | ||
if (!QFile::copy(f.absoluteFilePath(), newPath)) | ||
return; | ||
|
||
PluginsManager* pluginsManager = PluginsManager::instance(); | ||
auto plugin = pluginsManager->loadPluginFromPath(newPath); | ||
if (!plugin) | ||
{ | ||
QFile::remove(newPath); | ||
return; | ||
} | ||
|
||
QStandardItem *item = new QStandardItem(plugin->name()); | ||
item->setData(newPath); | ||
_model->appendRow(item); | ||
|
||
std::shared_ptr<NodeDelegateModelRegistry> reg = pluginsManager->registry(); | ||
plugin->registerDataModels(reg); | ||
}); | ||
|
||
// delete button | ||
QPushButton *deleteButton = new QPushButton("-", this); | ||
layout->addWidget(deleteButton, 1, 1); | ||
connect(deleteButton, | ||
&QPushButton::clicked, | ||
this, | ||
[this, pluginTable]() | ||
{ | ||
QItemSelectionModel *selectionModel = pluginTable->selectionModel(); | ||
|
||
int row = selectionModel->currentIndex().row(); | ||
|
||
while (selectionModel->selectedRows().count() > 0) | ||
{ | ||
auto rowIdx = selectionModel->selectedRows().first(); | ||
row = std::min(row, rowIdx.row()); | ||
|
||
QStandardItem *item = _model->itemFromIndex(rowIdx); | ||
|
||
PluginsManager *pluginsManager = PluginsManager::instance(); | ||
|
||
// FIXME: Unload plugin successfully, but cannot delete the plugin file | ||
if (!pluginsManager->unloadPluginFromName(item->text()) || | ||
!QFile::remove(item->data().toString())) | ||
{ | ||
selectionModel->select(rowIdx, QItemSelectionModel::Deselect); | ||
continue; | ||
} | ||
|
||
_model->removeRow(rowIdx.row()); | ||
} | ||
|
||
pluginTable->selectRow(row); | ||
}); | ||
} | ||
|
||
PluginsManagerDlg:: | ||
~PluginsManagerDlg() | ||
{ | ||
// | ||
} | ||
|
||
void | ||
PluginsManagerDlg:: | ||
openPluginsFolder() | ||
{ | ||
// QDesktopServices::openUrl(QUrl::fromLocalFile(_pluginsFolderPath)); | ||
QDesktopServices::openUrl(QUrl(_pluginsFolder.absolutePath())); | ||
} | ||
|
||
QString | ||
PluginsManagerDlg:: | ||
pluginsFolderPath() const | ||
{ | ||
return _pluginsFolder.absolutePath(); | ||
} | ||
|
||
void | ||
PluginsManagerDlg:: | ||
loadPluginsFromFolder() | ||
{ | ||
PluginsManager* pluginsManager = PluginsManager::instance(); | ||
std::shared_ptr<NodeDelegateModelRegistry> registry = pluginsManager->registry(); | ||
pluginsManager->loadPlugins(_pluginsFolder.absolutePath()); | ||
|
||
for(auto l : pluginsManager->loaders()) | ||
{ | ||
PluginInterface* plugin = qobject_cast<PluginInterface*>(l.second->instance()); | ||
if (!plugin) | ||
continue; | ||
|
||
QStandardItem *item = new QStandardItem(plugin->name()); | ||
item->setData(l.second->fileName()); | ||
_model->appendRow(item); | ||
|
||
plugin->registerDataModels(registry); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
#pragma once | ||
|
||
#include <QDir> | ||
#include <QDialog> | ||
#include <QStandardItemModel> | ||
|
||
#include <QtNodes/PluginsManager> | ||
|
||
using QtNodes::PluginsManager; | ||
|
||
class PluginsManagerDlg : public QDialog | ||
{ | ||
public: | ||
PluginsManagerDlg(QWidget* parent = nullptr); | ||
|
||
virtual | ||
~PluginsManagerDlg(); | ||
|
||
public: | ||
void | ||
openPluginsFolder(); | ||
|
||
QString | ||
pluginsFolderPath() const; | ||
|
||
private: | ||
void | ||
loadPluginsFromFolder(); | ||
|
||
private: | ||
QDir _pluginsFolder; | ||
|
||
QStandardItemModel* _model = nullptr; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.