pluginsDialog.cpp 2.82 KB
Newer Older
Pierre Kraemer's avatar
Pierre Kraemer committed
1
#include "dialogs/pluginsDialog.h"
2 3 4 5

#include <QFileDialog>
#include <QMessageBox>

6
#include "window.h"
7
#include "plugin.h"
8

Pierre Kraemer's avatar
Pierre Kraemer committed
9 10 11 12 13 14
namespace CGoGN
{

namespace SCHNApps
{

Pierre Kraemer's avatar
Pierre Kraemer committed
15
PluginsDialog::PluginsDialog(Window* window) :
16
	QDialog(window),
17
	m_window(window),
18
	b_refreshingUI(false)
19
{
20 21
	setupUi(this);
	setModal(false);
22

23 24
	connect(button_registerDirectory, SIGNAL(pressed()), this, SLOT(registerPluginsDirectory()));
	connect(list_plugins, SIGNAL(itemChanged(QListWidgetItem*)), this, SLOT(togglePlugin(QListWidgetItem*)));
25

26 27
	connect(m_window, SIGNAL(pluginLoaded(Plugin*)), this, SLOT(checkPlugin(Plugin*)));
	connect(m_window, SIGNAL(pluginUnloaded(Plugin*)), this, SLOT(uncheckPlugin(Plugin*)));
28 29
}

Pierre Kraemer's avatar
Pierre Kraemer committed
30
PluginsDialog::~PluginsDialog()
31 32
{}

33
void PluginsDialog::refreshPluginsList()
34
{
35 36 37 38 39
	b_refreshingUI = true;
	list_plugins->clear();
	const QMap<QString, QString>& availablePlugins = m_window->getAvailablePlugins();
	const PluginHash& loadedPlugins = m_window->getPluginsHash();
	for(QMap<QString, QString>::const_iterator it = availablePlugins.constBegin(); it != availablePlugins.constEnd(); ++it)
40
	{
41 42 43
		QListWidgetItem* item = new QListWidgetItem(it.key(), list_plugins);
		if(loadedPlugins.contains(it.key()))
			item->setCheckState(Qt::Checked);
44
		else
45
			item->setCheckState(Qt::Unchecked);
46
	}
47
	b_refreshingUI = false;
48 49
}

50
void PluginsDialog::registerPluginsDirectory()
51 52 53
{
	QString dir = QFileDialog::getExistingDirectory(
		this,
Pierre Kraemer's avatar
Pierre Kraemer committed
54
		tr("Select a directory"),
55
		m_window->getAppPath(),
56 57 58 59
		QFileDialog::ShowDirsOnly | QFileDialog::DontResolveSymlinks
	);

	if (!dir.isEmpty())
60
		m_window->registerPluginsDirectory(dir);
61 62
}

63
void PluginsDialog::togglePlugin(QListWidgetItem* item)
64
{
65
	if(!b_refreshingUI)
66
	{
67
		if(item->checkState() == Qt::Checked)
68
		{
69 70
			Plugin* p = m_window->loadPlugin(item->text());
			if (!p)
71
			{
72 73 74
				b_refreshingUI = true;
				item->setCheckState(Qt::Unchecked);
				b_refreshingUI = false;
75
			}
76 77 78 79 80
		}
		else // Unchecked
		{
			Plugin* p = m_window->getPlugin(item->text());
			if (p)
81
			{
82
				if(p->isUsed())
83
				{
84 85 86 87 88
					QMessageBox::warning(this, tr("Warning"), "Plugin is currently used");
					b_refreshingUI = true;
					item->setCheckState(Qt::Checked);
					b_refreshingUI = false;
					return;
89 90
				}
				else
91
					m_window->unloadPlugin(item->text());
92 93 94 95 96
			}
		}
	}
}

97
void PluginsDialog::checkPlugin(Plugin* plugin)
98
{
99 100 101 102 103 104
	b_refreshingUI = true;
	QString name = plugin->getName();
	QList<QListWidgetItem*> items = list_plugins->findItems(name, Qt::MatchExactly);
	if(!items.empty())
		items[0]->setCheckState(Qt::Checked);
	b_refreshingUI = false;
Pierre Kraemer's avatar
Pierre Kraemer committed
105
}
106

107 108 109 110 111 112 113 114 115
void PluginsDialog::uncheckPlugin(Plugin* plugin)
{
	b_refreshingUI = true;
	QString name = plugin->getName();
	QList<QListWidgetItem*> items = list_plugins->findItems(name, Qt::MatchExactly);
	if(!items.empty())
		items[0]->setCheckState(Qt::Unchecked);
	b_refreshingUI = false;
}
Pierre Kraemer's avatar
Pierre Kraemer committed
116 117 118 119

} // namespace SCHNApps

} // namespace CGoGN