|
@@ -22,14 +22,127 @@
|
|
|
|
|
|
#include "ctkPluginGeneratorCodeModel.h"
|
|
#include "ctkPluginGeneratorCodeModel.h"
|
|
|
|
|
|
|
|
+#include <QDir>
|
|
|
|
+#include <QHash>
|
|
|
|
+
|
|
class ctkPluginGeneratorCodeModelPrivate
|
|
class ctkPluginGeneratorCodeModelPrivate
|
|
{
|
|
{
|
|
public:
|
|
public:
|
|
|
|
|
|
- QList<QObject*> rootTemplates;
|
|
|
|
|
|
+ QHash<QString, ctkPluginGeneratorAbstractTemplate*> rootTemplates;
|
|
|
|
+
|
|
|
|
+};
|
|
|
|
+
|
|
|
|
+class ctkPluginGeneratorFolderTemplate : public ctkPluginGeneratorAbstractTemplate
|
|
|
|
+{
|
|
|
|
+public:
|
|
|
|
+
|
|
|
|
+ ctkPluginGeneratorFolderTemplate(const QString& name, ctkPluginGeneratorAbstractTemplate* parent)
|
|
|
|
+ : ctkPluginGeneratorAbstractTemplate(name, parent)
|
|
|
|
+ {}
|
|
|
|
+
|
|
|
|
+ QString generateContent()
|
|
|
|
+ {
|
|
|
|
+ return "";
|
|
|
|
+ }
|
|
|
|
|
|
|
|
+ void create(const QString& location)
|
|
|
|
+ {
|
|
|
|
+ QDir dir(location);
|
|
|
|
+ if (dir.mkdir(this->objectName()))
|
|
|
|
+ {
|
|
|
|
+ QString currLocation = location + QDir::separator() + this->objectName();
|
|
|
|
+ QListIterator<QObject*> it(this->children());
|
|
|
|
+ while (it.hasNext())
|
|
|
|
+ {
|
|
|
|
+ qobject_cast<ctkPluginGeneratorAbstractTemplate*>(it.next())->create(currLocation);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ else
|
|
|
|
+ {
|
|
|
|
+ throw std::exception("The directory " + location.append(this->objectName()).toAscii() + " could not be created");
|
|
|
|
+ }
|
|
|
|
+ }
|
|
};
|
|
};
|
|
|
|
|
|
ctkPluginGeneratorCodeModel::ctkPluginGeneratorCodeModel()
|
|
ctkPluginGeneratorCodeModel::ctkPluginGeneratorCodeModel()
|
|
|
|
+ : d_ptr(new ctkPluginGeneratorCodeModelPrivate)
|
|
|
|
+{
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+ctkPluginGeneratorCodeModel::~ctkPluginGeneratorCodeModel()
|
|
|
|
+{
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+void ctkPluginGeneratorCodeModel::addTemplate(ctkPluginGeneratorAbstractTemplate *templ,
|
|
|
|
+ const QString& path)
|
|
|
|
+{
|
|
|
|
+ Q_D(ctkPluginGeneratorCodeModel);
|
|
|
|
+ if (path.isEmpty())
|
|
|
|
+ {
|
|
|
|
+ d->rootTemplates.insert(templ->objectName(), templ);
|
|
|
|
+ }
|
|
|
|
+ else
|
|
|
|
+ {
|
|
|
|
+ ctkPluginGeneratorAbstractTemplate* parentTemplate = 0;
|
|
|
|
+ QStringList paths = path.split("/", QString::SkipEmptyParts);
|
|
|
|
+ QStringListIterator it(paths);
|
|
|
|
+ if (it.hasNext())
|
|
|
|
+ {
|
|
|
|
+ QString rootEntry = it.next();
|
|
|
|
+ // search the root templates
|
|
|
|
+ if (d->rootTemplates.contains(rootEntry))
|
|
|
|
+ {
|
|
|
|
+ if (!dynamic_cast<ctkPluginGeneratorFolderTemplate*>(d->rootTemplates[rootEntry]))
|
|
|
|
+ {
|
|
|
|
+ throw std::exception("The segment \"" + rootEntry.toAscii() + "\" in \"" + path.toAscii() + "\" is not a folder");
|
|
|
|
+ }
|
|
|
|
+ parentTemplate = d->rootTemplates[rootEntry];
|
|
|
|
+ }
|
|
|
|
+ else
|
|
|
|
+ {
|
|
|
|
+ parentTemplate = new ctkPluginGeneratorFolderTemplate(rootEntry);
|
|
|
|
+ d->rootTemplates.insert(rootEntry, parentTemplate);
|
|
|
|
+ }
|
|
|
|
+ while (it.hasNext())
|
|
|
|
+ {
|
|
|
|
+ QString currEntry = it.next();
|
|
|
|
+ QListIterator<QObject*> children(parentTemplate->children());
|
|
|
|
+ bool childFound = false;
|
|
|
|
+ while (children.hasNext())
|
|
|
|
+ {
|
|
|
|
+ QObject* child = it.next();
|
|
|
|
+ if (child->objectName() == currEntry)
|
|
|
|
+ {
|
|
|
|
+ childFound = true;
|
|
|
|
+ parentTemplate = qobject_cast<ctkPluginGeneratorAbstractTemplate*>(child);
|
|
|
|
+ if (parentTemplate == 0)
|
|
|
|
+ {
|
|
|
|
+ throw std::exception("The segment \"" + currEntry.toAscii() + "\" in \"" + path.toAscii() + "\" is not a folder");
|
|
|
|
+ }
|
|
|
|
+ break;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ if (!childFound)
|
|
|
|
+ {
|
|
|
|
+ parentTemplate = new ctkPluginGeneratorFolderTemplate(currEntry, parentTemplate);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ templ->setParent(parentTemplate);
|
|
|
|
+ }
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+void ctkPluginGeneratorCodeModel::create(const QString& location)
|
|
{
|
|
{
|
|
|
|
+ Q_D(ctkPluginGeneratorCodeModel);
|
|
|
|
+
|
|
|
|
+ QListIterator<ctkPluginGeneratorAbstractTemplate*> it(d->rootTemplates);
|
|
|
|
+ while (it.hasNext())
|
|
|
|
+ {
|
|
|
|
+ ctkPluginGeneratorAbstractTemplate* templ = it.next();
|
|
|
|
+ templ->create(location);
|
|
|
|
+ }
|
|
}
|
|
}
|