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;
1718PluginsManagerDlg::
1819PluginsManagerDlg (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
106127PluginsManagerDlg::
@@ -113,14 +134,15 @@ void
113134PluginsManagerDlg::
114135openPluginsFolder ()
115136{
116- QDesktopServices::openUrl (QUrl::fromLocalFile (_pluginsFolderPath));
137+ // QDesktopServices::openUrl(QUrl::fromLocalFile(_pluginsFolderPath));
138+ QDesktopServices::openUrl (QUrl (_pluginsFolder.absolutePath ()));
117139}
118140
119141QString
120142PluginsManagerDlg::
121143pluginsFolderPath () const
122144{
123- return _pluginsFolderPath ;
145+ return _pluginsFolder. absolutePath () ;
124146}
125147
126148void
@@ -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 }
0 commit comments