ctkPluginGeneratorMainExtension.cpp 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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 "ctkPluginGeneratorMainExtension.h"
  16. #include <ctkPluginGeneratorCodeModel.h>
  17. #include <ctkPluginGeneratorConstants.h>
  18. #include <ctkPluginGeneratorCMakeLists.h>
  19. #include <ctkPluginGeneratorHeaderTemplate.h>
  20. #include <ctkPluginGeneratorCppPluginActivator.h>
  21. #include <ctkPluginGeneratorTargetLibraries.h>
  22. ctkPluginGeneratorMainExtension::ctkPluginGeneratorMainExtension()
  23. : ui(0)
  24. {
  25. this->setTitle(tr("Main"));
  26. this->setDescription("The main parameters for a new plugin");
  27. }
  28. QWidget* ctkPluginGeneratorMainExtension::createWidget()
  29. {
  30. ui = new Ui::ctkPluginGeneratorMainExtension();
  31. QWidget* container = new QWidget();
  32. ui->setupUi(container);
  33. connectSignals();
  34. setTitle(tr("Main"));
  35. return container;
  36. }
  37. void ctkPluginGeneratorMainExtension::connectSignals()
  38. {
  39. connect(ui->symbolicNameEdit, SIGNAL(textChanged(QString)), this, SLOT(symbolicNameChanged()));
  40. connect(ui->activatorClassEdit, SIGNAL(textChanged(QString)), this, SLOT(activatorClassChanged()));
  41. connect(ui->symbolicNameEdit, SIGNAL(textChanged(QString)), this, SLOT(updateParameters()));
  42. connect(ui->exportDirectiveEdit, SIGNAL(textChanged(QString)), this, SLOT(updateParameters()));
  43. connect(ui->activatorClassEdit, SIGNAL(textChanged(QString)), this, SLOT(updateParameters()));
  44. connect(ui->activatorHeaderEdit, SIGNAL(textChanged(QString)), this, SLOT(updateParameters()));
  45. connect(ui->activatorSourceEdit, SIGNAL(textChanged(QString)), this, SLOT(updateParameters()));
  46. }
  47. void ctkPluginGeneratorMainExtension::symbolicNameChanged()
  48. {
  49. QString symbolicName = ui->symbolicNameEdit->text().replace(".", "_");
  50. ui->exportDirectiveEdit->setText(symbolicName + "_EXPORT");
  51. QString activatorClassName;
  52. QStringList tokens = symbolicName.split('_');
  53. if (tokens.size() > 1)
  54. {
  55. tokens.pop_front();
  56. activatorClassName += tokens.takeFirst();
  57. foreach(QString token, tokens)
  58. {
  59. activatorClassName += token.left(1).toUpper() + token.mid(1);
  60. }
  61. }
  62. activatorClassName += "Plugin";
  63. ui->activatorClassEdit->setText(activatorClassName);
  64. ui->activatorHeaderEdit->setText(activatorClassName + "_p.h");
  65. ui->activatorSourceEdit->setText(activatorClassName + ".cpp");
  66. }
  67. void ctkPluginGeneratorMainExtension::activatorClassChanged()
  68. {
  69. QString activatorClassName = ui->activatorClassEdit->text();
  70. ui->activatorHeaderEdit->setText(activatorClassName + "_p.h");
  71. ui->activatorSourceEdit->setText(activatorClassName + ".cpp");
  72. ui->advancedButton->setText(tr("Advanced (activator class: %1)").arg(activatorClassName));
  73. }
  74. bool ctkPluginGeneratorMainExtension::verifyParameters(
  75. const QHash<QString, QVariant>& params)
  76. {
  77. if (params["symbolic-name"].toString().isEmpty())
  78. {
  79. this->setErrorMessage(tr("The symbolic name cannot be empty"));
  80. return false;
  81. }
  82. if (params["export-directive"].toString().isEmpty())
  83. {
  84. this->setErrorMessage(tr("The export directive cannot be empty"));
  85. return false;
  86. }
  87. if (params["activator-classname"].toString().isEmpty())
  88. {
  89. this->setErrorMessage(tr("The activator class name cannot be empty"));
  90. return false;
  91. }
  92. if (params["activator-headerfile"].toString().isEmpty())
  93. {
  94. this->setErrorMessage(tr("The activator header filename cannot be empty"));
  95. return false;
  96. }
  97. if (params["activator-sourcefile"].toString().isEmpty())
  98. {
  99. this->setErrorMessage(tr("The activator source filename cannot be empty"));
  100. return false;
  101. }
  102. this->setErrorMessage("");
  103. return true;
  104. }
  105. void ctkPluginGeneratorMainExtension::updateCodeModel(const QHash<QString, QVariant>& params)
  106. {
  107. ctkPluginGeneratorCodeModel* codeModel = this->getCodeModel();
  108. codeModel->setSymbolicName(params["symbolic-name"].toString());
  109. codeModel->setExportMacroInclude(QString("#include <") + codeModel->getSymbolicName() + "_Export.h>");
  110. codeModel->setExportMacro(params["export-directive"].toString());
  111. // Add CMakeLists.txt template
  112. codeModel->addTemplate(new ctkPluginGeneratorCMakeLists());
  113. ctkPluginGeneratorAbstractTemplate* cmakelistsTemplate = codeModel->getTemplate(ctkPluginGeneratorConstants::TEMPLATE_CMAKELISTS_TXT);
  114. cmakelistsTemplate->addContent(ctkPluginGeneratorCMakeLists::PLUGIN_PROJECT_NAME_MARKER,
  115. codeModel->getSymbolicName(),
  116. ctkPluginGeneratorAbstractTemplate::REPLACE);
  117. cmakelistsTemplate->addContent(ctkPluginGeneratorConstants::PLUGIN_EXPORTMACRO_MARKER,
  118. codeModel->getExportMacro(),
  119. ctkPluginGeneratorAbstractTemplate::REPLACE);
  120. // Add <plugin-activator>.h template
  121. QString activatorClassName = params["activator-classname"].toString();
  122. ctkPluginGeneratorAbstractTemplate* activatorHeaderTemplate =
  123. new ctkPluginGeneratorHeaderTemplate(ctkPluginGeneratorConstants::TEMPLATE_PLUGINACTIVATOR_H);
  124. activatorHeaderTemplate->addContent(ctkPluginGeneratorConstants::PLUGIN_LICENSE_MARKER, codeModel->getLicense());
  125. activatorHeaderTemplate->addContent(ctkPluginGeneratorHeaderTemplate::H_INCLUDES_MARKER, "#include <ctkPluginActivator.h>");
  126. activatorHeaderTemplate->addContent(ctkPluginGeneratorHeaderTemplate::H_CLASSNAME_MARKER, activatorClassName);
  127. activatorHeaderTemplate->addContent(ctkPluginGeneratorHeaderTemplate::H_SUPERCLASSES_MARKER, "public QObject, public ctkPluginActivator");
  128. activatorHeaderTemplate->addContent(ctkPluginGeneratorHeaderTemplate::H_DEFAULT_ACCESS_MARKER, "Q_OBJECT\nQ_INTERFACES(ctkPluginActivator)");
  129. activatorHeaderTemplate->addContent(ctkPluginGeneratorHeaderTemplate::H_PUBLIC_MARKER, activatorClassName + "();\n~" + activatorClassName + "();");
  130. activatorHeaderTemplate->addContent(ctkPluginGeneratorHeaderTemplate::H_PUBLIC_MARKER, "void start(ctkPluginContext* context);\nvoid stop(ctkPluginContext* context);");
  131. activatorHeaderTemplate->addContent(ctkPluginGeneratorHeaderTemplate::H_PUBLIC_MARKER, QString("static ") + activatorClassName + "* getInstance();");
  132. activatorHeaderTemplate->addContent(ctkPluginGeneratorHeaderTemplate::H_PUBLIC_MARKER, "ctkPluginContext* getPluginContext() const;");
  133. activatorHeaderTemplate->addContent(ctkPluginGeneratorHeaderTemplate::H_PRIVATE_MARKER, QString("static ") + activatorClassName + "* instance;\nctkPluginContext* context;");
  134. activatorHeaderTemplate->setFilename(params["activator-headerfile"].toString());
  135. codeModel->addTemplate(activatorHeaderTemplate);
  136. // Add <plugin-activator>.cpp template
  137. ctkPluginGeneratorAbstractTemplate* activatorCppTemplate =
  138. new ctkPluginGeneratorCppPluginActivator();
  139. activatorCppTemplate->addContent(ctkPluginGeneratorConstants::PLUGIN_LICENSE_MARKER, codeModel->getLicense());
  140. activatorCppTemplate->addContent(ctkPluginGeneratorCppTemplate::CPP_CLASSNAME_MARKER, activatorClassName);
  141. activatorCppTemplate->addContent(ctkPluginGeneratorCppTemplate::CPP_INCLUDES_MARKER, QString("#include \"") + activatorHeaderTemplate->getFilename() + "\"");
  142. activatorCppTemplate->addContent(ctkPluginGeneratorCppTemplate::CPP_GLOBAL_MARKER, activatorClassName + "* " + activatorClassName + "::instance = 0;");
  143. activatorCppTemplate->addContent(ctkPluginGeneratorCppTemplate::CPP_CONSTRUCTOR_INITLIST_MARKER, "context(0)");
  144. activatorCppTemplate->addContent(ctkPluginGeneratorCppTemplate::CPP_DESTRUCTOR_BODY_MARKER, "");
  145. activatorCppTemplate->addContent(ctkPluginGeneratorCppPluginActivator::PLUGINACTIVATOR_START_MARKER, "instance = this;\nthis->context = context;");
  146. activatorCppTemplate->addContent(ctkPluginGeneratorCppTemplate::CPP_METHODS_MARKER, activatorClassName + "* " + activatorClassName + "::getInstance()\n{\n return instance;\n}");
  147. activatorCppTemplate->addContent(ctkPluginGeneratorCppTemplate::CPP_METHODS_MARKER, QString("ctkPluginContext* ") + activatorClassName + "::getPluginContext() const\n{\n return context;\n}");
  148. activatorCppTemplate->setFilename(params["activator-sourcefile"].toString());
  149. codeModel->addTemplate(activatorCppTemplate);
  150. // Add target_libraries.cmake template
  151. // TODO only add the CTKPluginFramework library if there are no plugin dependencies
  152. ctkPluginGeneratorAbstractTemplate* targetLibrariesTemplate =
  153. new ctkPluginGeneratorTargetLibraries();
  154. targetLibrariesTemplate->addContent(ctkPluginGeneratorTargetLibraries::TARGETLIBRARIES_MARKER, "CTKPluginFramework");
  155. codeModel->addTemplate(targetLibrariesTemplate);
  156. // add project files to CMakeLists.txt
  157. cmakelistsTemplate->addContent(ctkPluginGeneratorCMakeLists::PLUGIN_SRCS_MARKER, activatorCppTemplate->getFilename());
  158. cmakelistsTemplate->addContent(ctkPluginGeneratorCMakeLists::PLUGIN_MOC_SRCS_MARKER, activatorHeaderTemplate->getFilename());
  159. }
  160. void ctkPluginGeneratorMainExtension::updateParameters()
  161. {
  162. this->setParameter("symbolic-name", ui->symbolicNameEdit->text());
  163. this->setParameter("plugin-name", ui->nameEdit->text());
  164. this->setParameter("plugin-version", ui->versionEdit->text());
  165. this->setParameter("export-directive", ui->exportDirectiveEdit->text());
  166. this->setParameter("activator-classname", ui->activatorClassEdit->text());
  167. this->setParameter("activator-headerfile", ui->activatorHeaderEdit->text());
  168. this->setParameter("activator-sourcefile", ui->activatorSourceEdit->text());
  169. this->validate();
  170. }