canvas.cpp 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. #include "canvas.h"
  2. #include <QPainter>
  3. #include <QVariant>
  4. #include "logger.h"
  5. #include "appcontext.h"
  6. #include "abstractwidget.h"
  7. Canvas::Canvas(const xmlNodePtr canvasNodePtr, QWidget *parent)
  8. : QWidget{parent}
  9. , m_backgroundColor(Qt::black)
  10. , m_isDefault(false)
  11. {
  12. load(canvasNodePtr);
  13. }
  14. Canvas::~Canvas()
  15. {
  16. }
  17. const QColor &Canvas::backgroundColor() const
  18. {
  19. return m_backgroundColor;
  20. }
  21. void Canvas::setBackgroundColor(const QColor &newBackgroundColor)
  22. {
  23. m_backgroundColor = newBackgroundColor;
  24. }
  25. bool Canvas::load(const xmlNodePtr canvasNodePtr)
  26. {
  27. if(!canvasNodePtr) {
  28. LOG_INFO("null canvas node");
  29. return false;
  30. }
  31. if(xmlStrcmp(canvasNodePtr->name, BAD_CAST "canvas") == 0) {
  32. // 导入前清空画布
  33. // m_pCanvas->clear();
  34. PluginManager &pluginMgr = AppContext::instance().pluginManager();
  35. auto customWidgets = pluginMgr.customWidgets();
  36. QString propertyName;
  37. QString propertyValue;
  38. xmlChar *value;
  39. int canvasWidth = 1280;
  40. int canvasHeight = 720;
  41. int isDefault;
  42. value = xmlGetProp(canvasNodePtr, BAD_CAST "backgroundColor");
  43. if(value) {
  44. QString canvasColor = QString("%1").arg((char *)value);
  45. m_backgroundColor = QColor(canvasColor);
  46. xmlFree(value);
  47. }
  48. value = xmlGetProp(canvasNodePtr, BAD_CAST "width");
  49. if (value) {
  50. canvasWidth = QString((char *)value).toInt();
  51. xmlFree(value);
  52. }
  53. value = xmlGetProp(canvasNodePtr, BAD_CAST "height");
  54. if (value) {
  55. canvasHeight = QString((char *)value).toInt();
  56. xmlFree(value);
  57. }
  58. value = xmlGetProp(canvasNodePtr, BAD_CAST "default");
  59. if (value) {
  60. isDefault = QString((char *)value).toInt();
  61. if(isDefault)
  62. m_isDefault = true;
  63. xmlFree(value);
  64. }
  65. resize(canvasWidth, canvasHeight);
  66. xmlNodePtr child = canvasNodePtr->children;
  67. while(child) // 遍历控件节点
  68. {
  69. QList<QPair<QString, QVariant> > propertys;
  70. QStringList parameters;
  71. if (child->type == XML_ELEMENT_NODE)
  72. {
  73. int x = 0;
  74. int y = 0;
  75. int w = 0;
  76. int h = 0;
  77. QString widgetName = (char *)child->name;
  78. xmlNodePtr node = child->children;
  79. LOG_DEBUG("widget: {}", (char *)child->name);
  80. while(node)
  81. {
  82. if(xmlStrcmp(node->name, BAD_CAST "property") == 0)
  83. {
  84. // 遍历 property 下的每个子节点
  85. xmlNodePtr propChild = node->children;
  86. LOG_DEBUG("\tproperty:");
  87. while(propChild)
  88. {
  89. if(propChild->type == XML_ELEMENT_NODE)
  90. {
  91. xmlChar* content = xmlNodeGetContent(propChild);
  92. if(content) {
  93. propertyName = QString("%1").arg((char*)propChild->name);
  94. propertyValue = QString("%1").arg((char*)content);
  95. LOG_DEBUG("\t\t{}:{}", propertyName.toUtf8().data(), propertyValue.toUtf8().data());
  96. if(propertyName == "x")
  97. {
  98. x = propertyValue.toInt();
  99. }
  100. else if(propertyName == "y")
  101. {
  102. y = propertyValue.toInt();
  103. }
  104. else if(propertyName == "width")
  105. {
  106. w = propertyValue.toInt();
  107. }
  108. else if(propertyName == "height")
  109. {
  110. h = propertyValue.toInt();
  111. }
  112. else
  113. {
  114. QVariant propertyVariantValue = QVariant(propertyValue);
  115. if (propertyValue.startsWith("#") && propertyValue.length() == 9)
  116. {
  117. propertyVariantValue = QColor(propertyValue);
  118. }
  119. propertys.append(qMakePair(propertyName, propertyVariantValue));
  120. }
  121. xmlFree(content);
  122. }
  123. }
  124. propChild = propChild->next;
  125. }
  126. }
  127. else if(xmlStrcmp(node->name, BAD_CAST "parameter") == 0)
  128. {
  129. xmlNodePtr paramChild = node->children;
  130. LOG_DEBUG("\tparameter:");
  131. while(paramChild)
  132. {
  133. if(paramChild->type == XML_ELEMENT_NODE) {
  134. xmlChar* content = xmlNodeGetContent(paramChild);
  135. if(content) {
  136. parameters << (char *)content;
  137. LOG_DEBUG("\t\t{}", (char *)content);
  138. xmlFree(content);
  139. }
  140. }
  141. paramChild = paramChild->next;
  142. }
  143. }
  144. node = node->next;
  145. }
  146. //根据不同的控件类型实例化控件
  147. int countWidget = customWidgets.count();
  148. int countProperty = propertys.count();
  149. for(int i = 0; i < countWidget; ++i)
  150. {
  151. QString className = customWidgets.at(i)->name();
  152. if(widgetName == className)
  153. {
  154. //生成对应的控件
  155. QWidget *widget = customWidgets.at(i)->createWidget(this);
  156. // 设置绑定参数
  157. AbstractWidget *pAbstractWidget = static_cast<AbstractWidget *>(widget);
  158. if(pAbstractWidget) {
  159. QMetaObject::invokeMethod(pAbstractWidget,
  160. "setParameters",
  161. Qt::AutoConnection,
  162. Q_ARG(QStringList, parameters));
  163. m_widgetList << widget;
  164. m_parameterList << parameters;
  165. }
  166. //逐个设置自定义控件的属性
  167. for(int j = 0; j < countProperty; j++)
  168. {
  169. QPair<QString, QVariant> property = propertys.at(j);
  170. QString name = property.first;
  171. QVariant value = property.second;
  172. widget->setProperty(name.toUtf8().data(), value);
  173. }
  174. //设置控件坐标及宽高
  175. widget->setGeometry(x, y, w, h);
  176. break;
  177. }
  178. }
  179. }
  180. child = child->next;
  181. }
  182. return true;
  183. }
  184. else {
  185. LOG_ERROR("err node name: {}", (char *)canvasNodePtr->name);
  186. return false;
  187. }
  188. }
  189. QList<QWidget *> Canvas::widgetList() const
  190. {
  191. return m_widgetList;
  192. }
  193. QList<QStringList> Canvas::parameterList() const
  194. {
  195. return m_parameterList;
  196. }
  197. bool Canvas::isDefault() const
  198. {
  199. return m_isDefault;
  200. }
  201. void Canvas::clear()
  202. {
  203. qDeleteAll(m_widgetList);
  204. m_widgetList.clear();
  205. m_parameterList.clear();
  206. }
  207. int Canvas::parameterCount() const
  208. {
  209. int count = 0;
  210. for(const auto &parameters : m_parameterList) {
  211. count += parameters.count();
  212. }
  213. return count;
  214. }
  215. void Canvas::paintEvent(QPaintEvent *event)
  216. {
  217. // 绘制背景颜色
  218. QPainter painter(this);
  219. painter.fillRect(rect(), m_backgroundColor);
  220. }