123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151 |
- /*=============================================================================
- Library: CTK
- Copyright (c) 2010 German Cancer Research Center,
- Division of Medical and Biological Informatics
- Licensed under the Apache License, Version 2.0 (the "License");
- you may not use this file except in compliance with the License.
- You may obtain a copy of the License at
- http://www.apache.org/licenses/LICENSE-2.0
- Unless required by applicable law or agreed to in writing, software
- distributed under the License is distributed on an "AS IS" BASIS,
- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- See the License for the specific language governing permissions and
- limitations under the License.
- =============================================================================*/
- #include "ctkPluginGeneratorCodeModel.h"
- #include <QDir>
- #include <QHash>
- #include <stdexcept>
- class ctkPluginGeneratorCodeModelPrivate
- {
- public:
- QHash<QString, ctkPluginGeneratorAbstractTemplate*> rootTemplates;
- };
- class ctkPluginGeneratorFolderTemplate : public ctkPluginGeneratorAbstractTemplate
- {
- public:
- ctkPluginGeneratorFolderTemplate(const QString& name, ctkPluginGeneratorAbstractTemplate* parent = 0)
- : 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::runtime_error(std::string("The directory ") + (location + this->objectName()).toStdString() + " could not be created");
- }
- }
- };
- 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::runtime_error(std::string("The segment \"") + rootEntry.toStdString() + "\" in \"" + path.toStdString() + "\" 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 = children.next();
- if (child->objectName() == currEntry)
- {
- childFound = true;
- parentTemplate = qobject_cast<ctkPluginGeneratorAbstractTemplate*>(child);
- if (parentTemplate == 0)
- {
- throw std::runtime_error(std::string("The segment \"") + currEntry.toStdString() + "\" in \"" + path.toStdString() + "\" 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.values());
- while (it.hasNext())
- {
- ctkPluginGeneratorAbstractTemplate* templ = it.next();
- templ->create(location);
- }
- }
|