ctkPluginGeneratorCodeModel.cpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. /*=============================================================================
  2. Library: CTK
  3. Copyright (c) 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 <ctkException.h>
  19. class ctkPluginGeneratorCodeModelPrivate
  20. {
  21. public:
  22. QHash<QString, ctkPluginGeneratorAbstractTemplate*> rootTemplates;
  23. QHash<QString, QString> contentMap;
  24. QString symbolicNameWithPeriods;
  25. QString symbolicNameWithUnderscores;
  26. QString exportMacro;
  27. QString exportMacroInclude;
  28. QString license;
  29. };
  30. class ctkPluginGeneratorFolderTemplate : public ctkPluginGeneratorAbstractTemplate
  31. {
  32. public:
  33. ctkPluginGeneratorFolderTemplate(const QString& name, ctkPluginGeneratorAbstractTemplate* parent = 0)
  34. : ctkPluginGeneratorAbstractTemplate(name, parent)
  35. {}
  36. QString generateContent()
  37. {
  38. return "";
  39. }
  40. void create(const QString& location)
  41. {
  42. QDir dir(location);
  43. if (dir.mkdir(this->objectName()))
  44. {
  45. QString currLocation = location + QDir::separator() + this->objectName();
  46. QListIterator<QObject*> it(this->children());
  47. while (it.hasNext())
  48. {
  49. qobject_cast<ctkPluginGeneratorAbstractTemplate*>(it.next())->create(currLocation);
  50. }
  51. }
  52. else
  53. {
  54. throw ctkRuntimeException("The directory " + location + this->objectName() + " could not be created");
  55. }
  56. }
  57. };
  58. ctkPluginGeneratorCodeModel::ctkPluginGeneratorCodeModel()
  59. : d_ptr(new ctkPluginGeneratorCodeModelPrivate)
  60. {
  61. }
  62. ctkPluginGeneratorCodeModel::~ctkPluginGeneratorCodeModel()
  63. {
  64. }
  65. void ctkPluginGeneratorCodeModel::addContent(const QString& marker, const QString& content)
  66. {
  67. Q_D(ctkPluginGeneratorCodeModel);
  68. d->contentMap[marker] = content;
  69. }
  70. QString ctkPluginGeneratorCodeModel::getContent(const QString& marker) const
  71. {
  72. Q_D(const ctkPluginGeneratorCodeModel);
  73. if (d->contentMap.contains(marker))
  74. {
  75. return d->contentMap[marker];
  76. }
  77. return QString();
  78. }
  79. void ctkPluginGeneratorCodeModel::setExportMacro(const QString& exportMacro)
  80. {
  81. Q_D(ctkPluginGeneratorCodeModel);
  82. d->exportMacro = exportMacro;
  83. }
  84. QString ctkPluginGeneratorCodeModel::getExportMacro() const
  85. {
  86. Q_D(const ctkPluginGeneratorCodeModel);
  87. return d->exportMacro;
  88. }
  89. void ctkPluginGeneratorCodeModel::setExportMacroInclude(const QString& exportMacroInclude)
  90. {
  91. Q_D(ctkPluginGeneratorCodeModel);
  92. d->exportMacroInclude = exportMacroInclude;
  93. }
  94. QString ctkPluginGeneratorCodeModel::getExportMacroInclude() const
  95. {
  96. Q_D(const ctkPluginGeneratorCodeModel);
  97. return d->exportMacroInclude;
  98. }
  99. void ctkPluginGeneratorCodeModel::setSymbolicName(const QString& symbolicName)
  100. {
  101. Q_D(ctkPluginGeneratorCodeModel);
  102. d->symbolicNameWithPeriods = QString(symbolicName).replace("_", ".");
  103. d->symbolicNameWithUnderscores = QString(symbolicName).replace(".", "_");
  104. }
  105. QString ctkPluginGeneratorCodeModel::getSymbolicName(bool withPeriods) const
  106. {
  107. Q_D(const ctkPluginGeneratorCodeModel);
  108. if (withPeriods)
  109. {
  110. return d->symbolicNameWithPeriods;
  111. }
  112. return d->symbolicNameWithUnderscores;
  113. }
  114. void ctkPluginGeneratorCodeModel::setLicense(const QString& license)
  115. {
  116. Q_D(ctkPluginGeneratorCodeModel);
  117. d->license = license;
  118. }
  119. QString ctkPluginGeneratorCodeModel::getLicense() const
  120. {
  121. Q_D(const ctkPluginGeneratorCodeModel);
  122. return d->license;
  123. }
  124. void ctkPluginGeneratorCodeModel::addTemplate(ctkPluginGeneratorAbstractTemplate *templ,
  125. const QString& path)
  126. {
  127. Q_D(ctkPluginGeneratorCodeModel);
  128. templ->setCodeModel(this);
  129. if (path.isEmpty())
  130. {
  131. d->rootTemplates.insert(templ->objectName(), templ);
  132. templ->setParent(this);
  133. }
  134. else
  135. {
  136. ctkPluginGeneratorAbstractTemplate* parentTemplate = 0;
  137. QStringList paths = path.split("/", QString::SkipEmptyParts);
  138. QStringListIterator it(paths);
  139. if (it.hasNext())
  140. {
  141. QString rootEntry = it.next();
  142. // search the root templates
  143. if (d->rootTemplates.contains(rootEntry))
  144. {
  145. if (!dynamic_cast<ctkPluginGeneratorFolderTemplate*>(d->rootTemplates[rootEntry]))
  146. {
  147. throw ctkRuntimeException("The segment \"" + rootEntry + "\" in \"" + path + "\" is not a folder");
  148. }
  149. parentTemplate = d->rootTemplates[rootEntry];
  150. }
  151. else
  152. {
  153. parentTemplate = new ctkPluginGeneratorFolderTemplate(rootEntry);
  154. d->rootTemplates.insert(rootEntry, parentTemplate);
  155. }
  156. while (it.hasNext())
  157. {
  158. QString currEntry = it.next();
  159. QListIterator<QObject*> children(parentTemplate->children());
  160. bool childFound = false;
  161. while (children.hasNext())
  162. {
  163. QObject* child = children.next();
  164. if (child->objectName() == currEntry)
  165. {
  166. childFound = true;
  167. parentTemplate = qobject_cast<ctkPluginGeneratorAbstractTemplate*>(child);
  168. if (parentTemplate == 0)
  169. {
  170. throw ctkRuntimeException("The segment \"" + currEntry + "\" in \"" + path + "\" is not a folder");
  171. }
  172. break;
  173. }
  174. }
  175. if (!childFound)
  176. {
  177. parentTemplate = new ctkPluginGeneratorFolderTemplate(currEntry, parentTemplate);
  178. }
  179. }
  180. }
  181. templ->setParent(parentTemplate);
  182. }
  183. }
  184. ctkPluginGeneratorAbstractTemplate* ctkPluginGeneratorCodeModel::getTemplate(const QString& path) const
  185. {
  186. Q_D(const ctkPluginGeneratorCodeModel);
  187. if (!path.contains("/"))
  188. {
  189. foreach(ctkPluginGeneratorAbstractTemplate* t, d->rootTemplates)
  190. {
  191. if (t->objectName() == path) return t;
  192. ctkPluginGeneratorAbstractTemplate* child =
  193. t->findChild<ctkPluginGeneratorAbstractTemplate*>(path);
  194. if (child) return child;
  195. }
  196. return 0;
  197. }
  198. QStringList paths = path.split("/", QString::SkipEmptyParts);
  199. if (paths.empty()) return 0;
  200. QObject* currChild = d->rootTemplates[paths.front()];
  201. paths.pop_front();
  202. int depth = 0;
  203. foreach (QString curr, paths)
  204. {
  205. foreach (QObject* child, currChild->children())
  206. {
  207. if (child->objectName() == curr)
  208. {
  209. currChild = child;
  210. ++depth;
  211. break;
  212. }
  213. }
  214. }
  215. if (paths.size() == depth)
  216. {
  217. return qobject_cast<ctkPluginGeneratorAbstractTemplate*>(currChild);
  218. }
  219. return 0;
  220. }
  221. void ctkPluginGeneratorCodeModel::create(const QString& location)
  222. {
  223. Q_D(ctkPluginGeneratorCodeModel);
  224. QListIterator<ctkPluginGeneratorAbstractTemplate*> it(d->rootTemplates.values());
  225. while (it.hasNext())
  226. {
  227. ctkPluginGeneratorAbstractTemplate* templ = it.next();
  228. templ->create(location);
  229. }
  230. }
  231. void ctkPluginGeneratorCodeModel::reset()
  232. {
  233. Q_D(ctkPluginGeneratorCodeModel);
  234. qDeleteAll(d->rootTemplates.values());
  235. d->rootTemplates.clear();
  236. }