ctkPluginGeneratorAbstractTemplate.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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 "ctkPluginGeneratorAbstractTemplate.h"
  16. #include "ctkPluginGeneratorCodeModel.h"
  17. #include "ctkPluginGeneratorConstants.h"
  18. #include <QHash>
  19. #include <QFile>
  20. class ctkPluginGeneratorAbstractTemplatePrivate
  21. {
  22. public:
  23. ctkPluginGeneratorAbstractTemplatePrivate()
  24. : codeModel(0)
  25. {}
  26. ctkPluginGeneratorCodeModel* codeModel;
  27. QString filename;
  28. QHash<QString, QStringList> contentMap;
  29. };
  30. ctkPluginGeneratorAbstractTemplate::ctkPluginGeneratorAbstractTemplate(
  31. const QString& name, ctkPluginGeneratorAbstractTemplate* parent)
  32. : QObject(parent), d_ptr(new ctkPluginGeneratorAbstractTemplatePrivate)
  33. {
  34. this->setObjectName(name);
  35. }
  36. ctkPluginGeneratorAbstractTemplate::~ctkPluginGeneratorAbstractTemplate()
  37. {
  38. }
  39. void ctkPluginGeneratorAbstractTemplate::setCodeModel(ctkPluginGeneratorCodeModel *codeModel)
  40. {
  41. Q_D(ctkPluginGeneratorAbstractTemplate);
  42. d->codeModel = codeModel;
  43. }
  44. void ctkPluginGeneratorAbstractTemplate::setFilename(const QString& filename)
  45. {
  46. Q_D(ctkPluginGeneratorAbstractTemplate);
  47. d->filename = filename;
  48. }
  49. QString ctkPluginGeneratorAbstractTemplate::getFilename() const
  50. {
  51. Q_D(const ctkPluginGeneratorAbstractTemplate);
  52. QString filename = this->objectName();
  53. if(!d->filename.isEmpty())
  54. {
  55. filename = d->filename;
  56. }
  57. return filename;
  58. }
  59. void ctkPluginGeneratorAbstractTemplate::reset()
  60. {
  61. Q_D(ctkPluginGeneratorAbstractTemplate);
  62. d->contentMap.clear();
  63. }
  64. void ctkPluginGeneratorAbstractTemplate::addContent(const QString &marker, const QString &content, Position pos)
  65. {
  66. Q_D(ctkPluginGeneratorAbstractTemplate);
  67. switch (pos)
  68. {
  69. case PREPEND:
  70. {
  71. d->contentMap[marker].prepend(content);
  72. break;
  73. }
  74. case APPEND:
  75. {
  76. d->contentMap[marker].append(content);
  77. break;
  78. }
  79. case REPLACE:
  80. {
  81. QStringList& v = d->contentMap[marker];
  82. v.clear();
  83. v.append(content);
  84. break;
  85. }
  86. }
  87. }
  88. QStringList ctkPluginGeneratorAbstractTemplate::getContent(const QString &marker) const
  89. {
  90. Q_D(const ctkPluginGeneratorAbstractTemplate);
  91. if (d->contentMap.contains(marker))
  92. {
  93. return d->contentMap[marker];
  94. }
  95. QString globalDefault = d->codeModel->getContent(marker);
  96. if (!globalDefault.isEmpty())
  97. {
  98. return QStringList(globalDefault);
  99. }
  100. return QStringList();
  101. }
  102. void ctkPluginGeneratorAbstractTemplate::create(const QString& location)
  103. {
  104. QString filename = getFilename();
  105. const QString path = location + "/" + filename;
  106. QFile file(path);
  107. file.open(QIODevice::WriteOnly | QIODevice::Text);
  108. file.write(this->generateContent().toAscii());
  109. file.close();
  110. }
  111. QStringList ctkPluginGeneratorAbstractTemplate::getMarkers() const
  112. {
  113. return ctkPluginGeneratorConstants::getGlobalMarkers();
  114. }
  115. QString ctkPluginGeneratorAbstractTemplate::getSymbolicName(bool withPeriods) const
  116. {
  117. Q_D(const ctkPluginGeneratorAbstractTemplate);
  118. return d->codeModel->getSymbolicName(withPeriods);
  119. }