ctkPluginGeneratorCodeModel.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /*=============================================================================
  2. Library: CTK
  3. Copyright (c) 2010 German Cancer Research Center,
  4. Division of Medical and Biological Informatics
  5. Licensed under the Apache License, Version 2.0 (the "License");
  6. you may not use this file except in compliance with the License.
  7. You may obtain a copy of the License at
  8. http://www.apache.org/licenses/LICENSE-2.0
  9. Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. =============================================================================*/
  15. #include "ctkPluginGeneratorCodeModel.h"
  16. #include <QDir>
  17. #include <QHash>
  18. #include <stdexcept>
  19. class ctkPluginGeneratorCodeModelPrivate
  20. {
  21. public:
  22. QHash<QString, ctkPluginGeneratorAbstractTemplate*> rootTemplates;
  23. };
  24. class ctkPluginGeneratorFolderTemplate : public ctkPluginGeneratorAbstractTemplate
  25. {
  26. public:
  27. ctkPluginGeneratorFolderTemplate(const QString& name, ctkPluginGeneratorAbstractTemplate* parent = 0)
  28. : ctkPluginGeneratorAbstractTemplate(name, parent)
  29. {}
  30. QString generateContent()
  31. {
  32. return "";
  33. }
  34. void create(const QString& location)
  35. {
  36. QDir dir(location);
  37. if (dir.mkdir(this->objectName()))
  38. {
  39. QString currLocation = location + QDir::separator() + this->objectName();
  40. QListIterator<QObject*> it(this->children());
  41. while (it.hasNext())
  42. {
  43. qobject_cast<ctkPluginGeneratorAbstractTemplate*>(it.next())->create(currLocation);
  44. }
  45. }
  46. else
  47. {
  48. throw std::runtime_error(std::string("The directory ") + (location + this->objectName()).toStdString() + " could not be created");
  49. }
  50. }
  51. };
  52. ctkPluginGeneratorCodeModel::ctkPluginGeneratorCodeModel()
  53. : d_ptr(new ctkPluginGeneratorCodeModelPrivate)
  54. {
  55. }
  56. ctkPluginGeneratorCodeModel::~ctkPluginGeneratorCodeModel()
  57. {
  58. }
  59. void ctkPluginGeneratorCodeModel::addTemplate(ctkPluginGeneratorAbstractTemplate *templ,
  60. const QString& path)
  61. {
  62. Q_D(ctkPluginGeneratorCodeModel);
  63. if (path.isEmpty())
  64. {
  65. d->rootTemplates.insert(templ->objectName(), templ);
  66. }
  67. else
  68. {
  69. ctkPluginGeneratorAbstractTemplate* parentTemplate = 0;
  70. QStringList paths = path.split("/", QString::SkipEmptyParts);
  71. QStringListIterator it(paths);
  72. if (it.hasNext())
  73. {
  74. QString rootEntry = it.next();
  75. // search the root templates
  76. if (d->rootTemplates.contains(rootEntry))
  77. {
  78. if (!dynamic_cast<ctkPluginGeneratorFolderTemplate*>(d->rootTemplates[rootEntry]))
  79. {
  80. throw std::runtime_error(std::string("The segment \"") + rootEntry.toStdString() + "\" in \"" + path.toStdString() + "\" is not a folder");
  81. }
  82. parentTemplate = d->rootTemplates[rootEntry];
  83. }
  84. else
  85. {
  86. parentTemplate = new ctkPluginGeneratorFolderTemplate(rootEntry);
  87. d->rootTemplates.insert(rootEntry, parentTemplate);
  88. }
  89. while (it.hasNext())
  90. {
  91. QString currEntry = it.next();
  92. QListIterator<QObject*> children(parentTemplate->children());
  93. bool childFound = false;
  94. while (children.hasNext())
  95. {
  96. QObject* child = children.next();
  97. if (child->objectName() == currEntry)
  98. {
  99. childFound = true;
  100. parentTemplate = qobject_cast<ctkPluginGeneratorAbstractTemplate*>(child);
  101. if (parentTemplate == 0)
  102. {
  103. throw std::runtime_error(std::string("The segment \"") + currEntry.toStdString() + "\" in \"" + path.toStdString() + "\" is not a folder");
  104. }
  105. break;
  106. }
  107. }
  108. if (!childFound)
  109. {
  110. parentTemplate = new ctkPluginGeneratorFolderTemplate(currEntry, parentTemplate);
  111. }
  112. }
  113. }
  114. templ->setParent(parentTemplate);
  115. }
  116. }
  117. void ctkPluginGeneratorCodeModel::create(const QString& location)
  118. {
  119. Q_D(ctkPluginGeneratorCodeModel);
  120. QListIterator<ctkPluginGeneratorAbstractTemplate*> it(d->rootTemplates.values());
  121. while (it.hasNext())
  122. {
  123. ctkPluginGeneratorAbstractTemplate* templ = it.next();
  124. templ->create(location);
  125. }
  126. }