new_plugin 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. #!/bin/bash
  2. #
  3. # Copyright (C) 2013-2020 Mattia Basaglia
  4. #
  5. # This program is free software: you can redistribute it and/or modify
  6. # it under the terms of the GNU Lesser General Public License as published by
  7. # the Free Software Foundation, either version 3 of the License, or
  8. # (at your option) any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU Lesser General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU Lesser General Public License
  16. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. SELFDIR=$(dirname $(readlink -se "${BASH_SOURCE[0]}"))
  18. function class_to_underscore()
  19. {
  20. echo "$1" | sed -r -e 's/([a-z])([A-Z])/\1_\L\2/g' -e 's/[A-Z]/\L\0/g'
  21. }
  22. function class_to_header()
  23. {
  24. echo "$(class_to_underscore "$1").hpp"
  25. }
  26. function header_to_source()
  27. {
  28. echo "$1" | sed -r -e 's/\.h(pp)?$/.cpp/i'
  29. }
  30. function read_arg()
  31. {
  32. varname=$1
  33. prompt="$2"
  34. default="$3"
  35. [ "$default" ] && prompt="${prompt} [$default]"
  36. read -p "$prompt: " -i "$default" $varname
  37. [ -z "${!varname}" ] && eval "$varname=\"$default\""
  38. }
  39. function header_to_guard()
  40. {
  41. echo "$1" | tr [:lower:] [:upper:] | tr /. _
  42. }
  43. read_arg class "Class name"
  44. [ -z "$class" ] && exit 1
  45. read_arg description "Description"
  46. read_arg header "Header" "$(class_to_header "$class")"
  47. read_arg plugin "Plugin Class" "${class}_Plugin"
  48. read_arg plugin_header "Plugin Header" "$(class_to_header "$plugin")"
  49. read_arg plugin_source "Plugin Source" "$(header_to_source "$plugin_header")"
  50. read_arg plugin_path "Plugin Path" "$SELFDIR"
  51. read_arg plugin_author "Author" "$(git config user.name)"
  52. read_arg plugin_copyright "Copyright" "2013-$(date +%Y) $plugin_author"
  53. echo "Summary:"
  54. echo " Class: $class"
  55. echo " Description: $description"
  56. echo " Header: $header"
  57. echo " Plugin Class: $plugin"
  58. echo " Plugin Header: $plugin_header"
  59. echo " Plugin Source: $plugin_source"
  60. echo " Plugin Path: $plugin_path"
  61. echo " Author: $plugin_author"
  62. echo " Copyright: $plugin_copyright"
  63. object_name="$(class_to_underscore $class)"
  64. cat >"$plugin_path/$plugin_source" <<PLUGIN
  65. /**
  66. * \file
  67. *
  68. * \author $plugin_author
  69. *
  70. * \copyright Copyright (C) $plugin_copyright
  71. *
  72. * This program is free software: you can redistribute it and/or modify
  73. * it under the terms of the GNU Lesser General Public License as published by
  74. * the Free Software Foundation, either version 3 of the License, or
  75. * (at your option) any later version.
  76. *
  77. * This program is distributed in the hope that it will be useful,
  78. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  79. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  80. * GNU Lesser General Public License for more details.
  81. *
  82. * You should have received a copy of the GNU Lesser General Public License
  83. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  84. *
  85. */
  86. #include "$plugin_header"
  87. #include "QtColorWidgets/$header"
  88. QWidget* $plugin::createWidget(QWidget *parent)
  89. {
  90. color_widgets::$class *widget = new color_widgets::$class(parent);
  91. return widget;
  92. }
  93. QIcon $plugin::icon() const
  94. {
  95. return QIcon();
  96. }
  97. QString $plugin::domXml() const
  98. {
  99. return "<ui language=\"c++\">\n"
  100. " <widget class=\"color_widgets::$class\" name=\"$object_name\">\n"
  101. " </widget>\n"
  102. "</ui>\n";
  103. }
  104. bool $plugin::isContainer() const
  105. {
  106. return false;
  107. }
  108. $plugin::$plugin(QObject *parent) :
  109. QObject(parent), initialized(false)
  110. {
  111. }
  112. void $plugin::initialize(QDesignerFormEditorInterface *)
  113. {
  114. initialized = true;
  115. }
  116. bool $plugin::isInitialized() const
  117. {
  118. return initialized;
  119. }
  120. QString $plugin::name() const
  121. {
  122. return "color_widgets::$class";
  123. }
  124. QString $plugin::group() const
  125. {
  126. return "Color Widgets";
  127. }
  128. QString $plugin::toolTip() const
  129. {
  130. return "$description";
  131. }
  132. QString $plugin::whatsThis() const
  133. {
  134. return toolTip();
  135. }
  136. QString $plugin::includeFile() const
  137. {
  138. return "QtColorWidgets/$header";
  139. }
  140. PLUGIN
  141. header_guard="COLOR_WIDGETS_$(header_to_guard "$plugin_header")"
  142. cat >"$plugin_path/$plugin_header" <<PLUGIN
  143. /**
  144. * \file
  145. *
  146. * \author $plugin_author
  147. *
  148. * \copyright Copyright (C) $plugin_copyright
  149. *
  150. * This program is free software: you can redistribute it and/or modify
  151. * it under the terms of the GNU Lesser General Public License as published by
  152. * the Free Software Foundation, either version 3 of the License, or
  153. * (at your option) any later version.
  154. *
  155. * This program is distributed in the hope that it will be useful,
  156. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  157. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  158. * GNU Lesser General Public License for more details.
  159. *
  160. * You should have received a copy of the GNU Lesser General Public License
  161. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  162. *
  163. */
  164. #ifndef $header_guard
  165. #define $header_guard
  166. #include <QObject>
  167. #include <QtUiPlugin/QDesignerCustomWidgetInterface>
  168. class $plugin : public QObject, public QDesignerCustomWidgetInterface
  169. {
  170. Q_OBJECT
  171. Q_INTERFACES(QDesignerCustomWidgetInterface)
  172. public:
  173. explicit $plugin(QObject *parent = nullptr);
  174. void initialize(QDesignerFormEditorInterface *core) Q_DECL_OVERRIDE;
  175. bool isInitialized() const Q_DECL_OVERRIDE;
  176. QWidget *createWidget(QWidget *parent) Q_DECL_OVERRIDE;
  177. QString name() const Q_DECL_OVERRIDE;
  178. QString group() const Q_DECL_OVERRIDE;
  179. QIcon icon() const Q_DECL_OVERRIDE;
  180. QString toolTip() const Q_DECL_OVERRIDE;
  181. QString whatsThis() const Q_DECL_OVERRIDE;
  182. bool isContainer() const Q_DECL_OVERRIDE;
  183. QString domXml() const Q_DECL_OVERRIDE;
  184. QString includeFile() const Q_DECL_OVERRIDE;
  185. private:
  186. bool initialized;
  187. };
  188. #endif // $header_guard
  189. PLUGIN
  190. sed -i -r \
  191. -e "\\~# add new sources above this line~i\\$plugin_source" \
  192. -e "\\~# add new headers above this line~i\\$plugin_header" \
  193. "$plugin_path/CMakeLists.txt"
  194. sed -i -r \
  195. -e "\\~// add new plugin headers above this line~i#include \"$plugin_header\"" \
  196. -e "\\~// add new plugins above this line~i\ widgets.push_back(new $plugin(this));" \
  197. "$plugin_path/color_widget_plugin_collection.cpp"