pluginsDialog.cpp 2.8 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
{
	this->setupUi(this);

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

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

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

32
void PluginsDialog::refreshPluginsList()
33
{
34 35 36 37 38
	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)
39
	{
40 41 42
		QListWidgetItem* item = new QListWidgetItem(it.key(), list_plugins);
		if(loadedPlugins.contains(it.key()))
			item->setCheckState(Qt::Checked);
43
		else
44
			item->setCheckState(Qt::Unchecked);
45
	}
46
	b_refreshingUI = false;
47 48
}

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

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

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

96
void PluginsDialog::checkPlugin(Plugin* plugin)
97
{
98 99 100 101 102 103
	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
104
}
105

106 107 108 109 110 111 112 113 114
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
115 116 117 118

} // namespace SCHNApps

} // namespace CGoGN