Skip to content

Commit 8792708

Browse files
committed
refactor:
The plugin directory is relative to the executable file. Uninstall the plug-in using the selection method.
1 parent c60e4d4 commit 8792708

File tree

3 files changed

+88
-59
lines changed

3 files changed

+88
-59
lines changed

examples/plugins_load/PluginsManagerDlg.cpp

Lines changed: 63 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include <QDir>
88
#include <QFileDialog>
99
#include <QDesktopServices>
10+
#include <QCoreApplication>
1011

1112
#include <QtNodes/NodeDelegateModelRegistry>
1213
#include <QtNodes/PluginInterface>
@@ -17,8 +18,11 @@ using QtNodes::PluginInterface;
1718
PluginsManagerDlg::
1819
PluginsManagerDlg(QWidget* parent)
1920
: QDialog(parent)
20-
, _pluginsFolderPath(R"(./nodes)")
2121
{
22+
setMinimumSize(300, 250);
23+
24+
_pluginsFolder.setPath(QDir::cleanPath(QCoreApplication::applicationDirPath() + QDir::separator() + R"(./nodes)"));
25+
2226
QGridLayout *layout = new QGridLayout();
2327
setLayout(layout);
2428

@@ -31,12 +35,14 @@ PluginsManagerDlg(QWidget* parent)
3135
_model = new QStandardItemModel(pluginTable);
3236

3337
_model->setColumnCount(2);
34-
_model->setHeaderData(0, Qt::Horizontal, "name");
35-
_model->setHeaderData(1, Qt::Horizontal, "version");
38+
_model->setHeaderData(0, Qt::Horizontal, "Name");
39+
_model->setHeaderData(1, Qt::Horizontal, "Version");
3640
pluginTable->setModel(_model);
3741

3842
loadPluginsFromFolder();
3943

44+
pluginTable->selectRow(0);
45+
4046
// add button
4147
QPushButton *addButton = new QPushButton("+");
4248
layout->addWidget(addButton, 1, 0);
@@ -47,60 +53,75 @@ PluginsManagerDlg(QWidget* parent)
4753
QString fileName =
4854
QFileDialog::getOpenFileName(this,
4955
tr("Load Plugin"),
50-
QDir::currentPath(),
56+
QCoreApplication::applicationDirPath(),
5157
tr("*.dll;*.so;*.dylib"));
5258

5359
if (!QFileInfo::exists(fileName))
5460
return;
5561

56-
// Copy to the plug-in directory
5762
QFileInfo f(fileName);
58-
QString newPath = _pluginsFolderPath + "/" + f.fileName();
59-
QFile::copy(fileName, newPath);
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;
6074

6175
PluginsManager* pluginsManager = PluginsManager::instance();
6276
auto plugin = pluginsManager->loadPluginFromPath(newPath);
63-
if (plugin == nullptr)
77+
if (!plugin)
6478
{
6579
QFile::remove(newPath);
6680
return;
6781
}
6882

6983
QStandardItem *item = new QStandardItem(plugin->name());
70-
item->setCheckable(true);
71-
item->setCheckState(Qt::Unchecked);
72-
_model->appendRow(item);
73-
7484
item->setData(newPath);
85+
_model->appendRow(item);
7586

7687
std::shared_ptr<NodeDelegateModelRegistry> reg = pluginsManager->registry();
7788
plugin->registerDataModels(reg);
7889
});
7990

80-
// // delete button
81-
// QPushButton *deleteButton = new QPushButton("-", this);
82-
// layout->addWidget(deleteButton, 1, 1);
83-
// connect(deleteButton, &QPushButton::clicked, this,
84-
// [this]()
85-
// {
86-
// for (int i = 0; i < _model->rowCount(); ++i)
87-
// {
88-
// QStandardItem* item = _model->item(i);
89-
// if(item->checkState() == Qt::Checked)
90-
// {
91-
// PluginsManager* pluginsManager = PluginsManager::instance();
92-
93-
// if (pluginsManager->unloadPluginFromName(item->text()))
94-
// {
95-
// qDebug() << item->data().toString();
96-
// if (QFile::remove(item->data().toString()))
97-
// {
98-
// _model->removeRow(i);
99-
// }
100-
// }
101-
// }
102-
// }
103-
// });
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+
});
104125
}
105126

106127
PluginsManagerDlg::
@@ -113,14 +134,15 @@ void
113134
PluginsManagerDlg::
114135
openPluginsFolder()
115136
{
116-
QDesktopServices::openUrl(QUrl::fromLocalFile(_pluginsFolderPath));
137+
// QDesktopServices::openUrl(QUrl::fromLocalFile(_pluginsFolderPath));
138+
QDesktopServices::openUrl(QUrl(_pluginsFolder.absolutePath()));
117139
}
118140

119141
QString
120142
PluginsManagerDlg::
121143
pluginsFolderPath() const
122144
{
123-
return _pluginsFolderPath;
145+
return _pluginsFolder.absolutePath();
124146
}
125147

126148
void
@@ -129,20 +151,17 @@ loadPluginsFromFolder()
129151
{
130152
PluginsManager* pluginsManager = PluginsManager::instance();
131153
std::shared_ptr<NodeDelegateModelRegistry> registry = pluginsManager->registry();
132-
pluginsManager->loadPlugins(_pluginsFolderPath);
154+
pluginsManager->loadPlugins(_pluginsFolder.absolutePath());
133155

134156
for(auto l : pluginsManager->loaders())
135157
{
136158
PluginInterface* plugin = qobject_cast<PluginInterface*>(l.second->instance());
137-
if (plugin == nullptr)
159+
if (!plugin)
138160
continue;
139161

140162
QStandardItem *item = new QStandardItem(plugin->name());
141-
item->setCheckable(true);
142-
item->setCheckState(Qt::Unchecked);
143-
_model->appendRow(item);
144-
145163
item->setData(l.second->fileName());
164+
_model->appendRow(item);
146165

147166
plugin->registerDataModels(registry);
148167
}

examples/plugins_load/PluginsManagerDlg.hpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#pragma once
22

3+
#include <QDir>
34
#include <QDialog>
45
#include <QStandardItemModel>
56

@@ -27,7 +28,7 @@ class PluginsManagerDlg : public QDialog
2728
loadPluginsFromFolder();
2829

2930
private:
30-
QString _pluginsFolderPath;
31+
QDir _pluginsFolder;
3132

3233
QStandardItemModel* _model = nullptr;
3334
};

src/PluginsManager.cpp

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -100,28 +100,36 @@ loadPluginFromPath(const QString & filePath)
100100
return nullptr;
101101

102102
QPluginLoader* loader = new QPluginLoader(filePath);
103-
if (loader->load())
103+
104+
qDebug()<< loader->metaData();
105+
106+
if (loader->isLoaded())
104107
{
105108
PluginInterface* plugin = qobject_cast<PluginInterface*>(loader->instance());
106-
if (plugin)
107-
{
108-
const QString name = plugin->name();
109-
qDebug() << "add plugin: " << name << loader->fileName();
110109

111-
_loaders[name] = loader;
110+
QPluginLoader* l = _loaders.find(plugin->name())->second;
111+
plugin = qobject_cast<PluginInterface*>(l->instance());
112112

113-
return plugin;
114-
}
115-
else
116-
{
117-
delete loader;
118-
loader = nullptr;
119-
}
113+
loader->unload();
114+
delete loader;
115+
116+
return plugin;
117+
}
118+
119+
PluginInterface* plugin = qobject_cast<PluginInterface*>(loader->instance());
120+
if (plugin)
121+
{
122+
_loaders[plugin->name()] = loader;
123+
124+
return plugin;
120125
}
121126
else
122127
{
123-
qCritical() << "loadPlugin:" << filePath << loader->errorString();
128+
qWarning() << loader->errorString();
129+
130+
delete loader;
124131
}
132+
125133
return nullptr;
126134
}
127135

@@ -177,4 +185,5 @@ unloadPluginFromName(const QString &pluginName)
177185
return false;
178186
}
179187

188+
180189
} // namespace QtNodes

0 commit comments

Comments
 (0)