123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282 |
- /*=============================================================================
- Library: CTK
- Copyright (c) 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 <ctkException.h>
- class ctkPluginGeneratorCodeModelPrivate
- {
- public:
- QHash<QString, ctkPluginGeneratorAbstractTemplate*> rootTemplates;
- QHash<QString, QString> contentMap;
- QString symbolicNameWithPeriods;
- QString symbolicNameWithUnderscores;
- QString exportMacro;
- QString exportMacroInclude;
- QString license;
- };
- 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 ctkRuntimeException("The directory " + location + this->objectName() + " could not be created");
- }
- }
- };
- ctkPluginGeneratorCodeModel::ctkPluginGeneratorCodeModel()
- : d_ptr(new ctkPluginGeneratorCodeModelPrivate)
- {
- }
- ctkPluginGeneratorCodeModel::~ctkPluginGeneratorCodeModel()
- {
- }
- void ctkPluginGeneratorCodeModel::addContent(const QString& marker, const QString& content)
- {
- Q_D(ctkPluginGeneratorCodeModel);
- d->contentMap[marker] = content;
- }
- QString ctkPluginGeneratorCodeModel::getContent(const QString& marker) const
- {
- Q_D(const ctkPluginGeneratorCodeModel);
- if (d->contentMap.contains(marker))
- {
- return d->contentMap[marker];
- }
- return QString();
- }
- void ctkPluginGeneratorCodeModel::setExportMacro(const QString& exportMacro)
- {
- Q_D(ctkPluginGeneratorCodeModel);
- d->exportMacro = exportMacro;
- }
- QString ctkPluginGeneratorCodeModel::getExportMacro() const
- {
- Q_D(const ctkPluginGeneratorCodeModel);
- return d->exportMacro;
- }
- void ctkPluginGeneratorCodeModel::setExportMacroInclude(const QString& exportMacroInclude)
- {
- Q_D(ctkPluginGeneratorCodeModel);
- d->exportMacroInclude = exportMacroInclude;
- }
- QString ctkPluginGeneratorCodeModel::getExportMacroInclude() const
- {
- Q_D(const ctkPluginGeneratorCodeModel);
- return d->exportMacroInclude;
- }
- void ctkPluginGeneratorCodeModel::setSymbolicName(const QString& symbolicName)
- {
- Q_D(ctkPluginGeneratorCodeModel);
- d->symbolicNameWithPeriods = QString(symbolicName).replace("_", ".");
- d->symbolicNameWithUnderscores = QString(symbolicName).replace(".", "_");
- }
- QString ctkPluginGeneratorCodeModel::getSymbolicName(bool withPeriods) const
- {
- Q_D(const ctkPluginGeneratorCodeModel);
- if (withPeriods)
- {
- return d->symbolicNameWithPeriods;
- }
- return d->symbolicNameWithUnderscores;
- }
- void ctkPluginGeneratorCodeModel::setLicense(const QString& license)
- {
- Q_D(ctkPluginGeneratorCodeModel);
- d->license = license;
- }
- QString ctkPluginGeneratorCodeModel::getLicense() const
- {
- Q_D(const ctkPluginGeneratorCodeModel);
- return d->license;
- }
- void ctkPluginGeneratorCodeModel::addTemplate(ctkPluginGeneratorAbstractTemplate *templ,
- const QString& path)
- {
- Q_D(ctkPluginGeneratorCodeModel);
- templ->setCodeModel(this);
- if (path.isEmpty())
- {
- d->rootTemplates.insert(templ->objectName(), templ);
- templ->setParent(this);
- }
- 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 ctkRuntimeException("The segment \"" + rootEntry + "\" in \"" + path + "\" 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 ctkRuntimeException("The segment \"" + currEntry + "\" in \"" + path + "\" is not a folder");
- }
- break;
- }
- }
- if (!childFound)
- {
- parentTemplate = new ctkPluginGeneratorFolderTemplate(currEntry, parentTemplate);
- }
- }
- }
- templ->setParent(parentTemplate);
- }
- }
- ctkPluginGeneratorAbstractTemplate* ctkPluginGeneratorCodeModel::getTemplate(const QString& path) const
- {
- Q_D(const ctkPluginGeneratorCodeModel);
- if (!path.contains("/"))
- {
- foreach(ctkPluginGeneratorAbstractTemplate* t, d->rootTemplates)
- {
- if (t->objectName() == path) return t;
- ctkPluginGeneratorAbstractTemplate* child =
- t->findChild<ctkPluginGeneratorAbstractTemplate*>(path);
- if (child) return child;
- }
- return 0;
- }
- QStringList paths = path.split("/", QString::SkipEmptyParts);
- if (paths.empty()) return 0;
- QObject* currChild = d->rootTemplates[paths.front()];
- paths.pop_front();
- int depth = 0;
- foreach (QString curr, paths)
- {
- foreach (QObject* child, currChild->children())
- {
- if (child->objectName() == curr)
- {
- currChild = child;
- ++depth;
- break;
- }
- }
- }
- if (paths.size() == depth)
- {
- return qobject_cast<ctkPluginGeneratorAbstractTemplate*>(currChild);
- }
- return 0;
- }
- 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);
- }
- }
- void ctkPluginGeneratorCodeModel::reset()
- {
- Q_D(ctkPluginGeneratorCodeModel);
- qDeleteAll(d->rootTemplates.values());
- d->rootTemplates.clear();
- }
|