ParameterList.cpp 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. /**
  2. * @file ParameterList.cpp
  3. * @brief 参数列表类的头文件
  4. * @author FastCAE研发小组(fastcae@diso.cn)
  5. * @version 2.5.0
  6. * @date 2022-03-09 16:13
  7. * @copyright Copyright (c) Since 2020 青岛数智船海科技有限公司 All rights reserved.
  8. *
  9. * ============================================================================
  10. * Program: FastCAE
  11. *
  12. * Copyright (c) Since 2020 青岛数智船海科技有限公司 All rights reserved.
  13. * See License or http://www.fastcae.com/ for details.
  14. *
  15. * BSD 3-Clause License
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  18. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  19. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  20. * DISCLAIMED.
  21. * ==================================================================================
  22. */
  23. #include "ParameterList.h"
  24. #include <QDomElement>
  25. #include <QDomNodeList>
  26. #include <QDomDocument>
  27. #include "ParameterInt.h"
  28. #include "ParameterDouble.h"
  29. #include "ParameterBool.h"
  30. #include "ParameterSelectable.h"
  31. #include "ParameterTable.h"
  32. #include "ParameterString.h"
  33. #include "ParameterPath.h"
  34. #include "ParameterColor.h"
  35. #include "DataBase.h"
  36. #include <QDebug>
  37. #include <QDataStream>
  38. namespace DataProperty
  39. {
  40. ParameterList::~ParameterList()
  41. {
  42. const int n = _paraList.size();
  43. for (int i = 0; i < n; ++i)
  44. {
  45. ParameterBase* p = _paraList.at(i);
  46. delete p;
  47. }
  48. _paraList.clear();
  49. }
  50. void ParameterList::appendParameter(ParameterBase* para)
  51. {
  52. _paraList.append(para);
  53. }
  54. ParameterBase* ParameterList::getParameterAt(const int i)
  55. {
  56. if(i >= 0 && i < _paraList.size()){
  57. return _paraList.at(i);
  58. }else{
  59. return nullptr;
  60. }
  61. }
  62. int ParameterList::getParameterCount()
  63. {
  64. return _paraList.size();
  65. }
  66. ParameterBase* ParameterList::appendParameter(ParaType t)
  67. {
  68. ParameterBase* pbase = this->createParameterByType(t);
  69. if (pbase != nullptr) _paraList.append(pbase);
  70. return pbase;
  71. }
  72. void ParameterList::writeParameters(QDomDocument* doc, QDomElement* parent)
  73. {
  74. const int n = this->getParameterCount();
  75. for (int i = 0; i < n; ++i)
  76. {
  77. ParameterBase* p = _paraList.at(i);
  78. if (p == nullptr) continue;
  79. QDomElement ele = doc->createElement("Parameter");
  80. p->writeParameter(doc, &ele);
  81. parent->appendChild(ele);
  82. }
  83. }
  84. void ParameterList::readParameters(QDomElement* ele)
  85. {
  86. QDomNodeList paraList = ele->elementsByTagName("Parameter");
  87. const int n = paraList.size();
  88. for (int i = 0; i < n; ++i)
  89. {
  90. QDomElement ele = paraList.at(i).toElement();
  91. QString stype = ele.attribute("Type");
  92. DataProperty::ParaType type = ParameterBase::StringToParaType(stype);
  93. DataProperty::ParameterBase* para = this->createParameterByType(type);
  94. if (para == nullptr) continue;
  95. para->readParameter(&ele);
  96. this->appendParameter(para);
  97. }
  98. }
  99. ParameterBase* ParameterList::createParameterByType(QString stype)
  100. {
  101. DataProperty::ParaType type = ParameterBase::StringToParaType(stype);
  102. return createParameterByType(type);
  103. }
  104. ParameterBase* ParameterList::createParameterByType(ParaType t)
  105. {
  106. ParameterBase* p = nullptr;
  107. switch (t)
  108. {
  109. case DataProperty::Para_Int:
  110. p = new DataProperty::ParameterInt;
  111. break;
  112. case DataProperty::Para_Double:
  113. p = new DataProperty::ParameterDouble;
  114. break;
  115. case DataProperty::Para_String:
  116. p = new DataProperty::ParameterString;
  117. break;
  118. case DataProperty::Para_Bool:
  119. p = new DataProperty::ParameterBool;
  120. break;
  121. case DataProperty::Para_Selectable:
  122. p = new DataProperty::ParameterSelectable;
  123. break;
  124. case DataProperty::Para_Path:
  125. p = new DataProperty::ParameterPath;
  126. break;
  127. case DataProperty::Para_Table:
  128. p = new DataProperty::ParameterTable;
  129. break;
  130. case DataProperty::Para_Color:
  131. p = new DataProperty::ParameterColor;
  132. break;
  133. default:
  134. break;
  135. }
  136. return p;
  137. }
  138. void ParameterList::copy(ParameterList* data)
  139. {
  140. if (data == nullptr) return;
  141. const int n = data->getParameterCount();
  142. for (int i = 0; i < n; ++i)
  143. {
  144. ParameterBase* pb = data->getParameterAt(i);
  145. ParameterBase* para = this->createParameterByType(pb->getParaType());
  146. if (para != nullptr)
  147. {
  148. para->copy(pb);
  149. this->appendParameter(para);
  150. }
  151. }
  152. }
  153. int ParameterList::getVisibleParaCount()
  154. {
  155. int count = 0;
  156. for (int i = 0; i < _paraList.size(); ++i)
  157. {
  158. ParameterBase* b = _paraList.at(i);
  159. if (b->isVisible())
  160. count++;
  161. }
  162. return count;
  163. }
  164. ParameterBase* ParameterList::getVisibleParameterAt(const int i)
  165. {
  166. QList<ParameterBase*> list;
  167. for (int i = 0; i < _paraList.size(); ++i)
  168. {
  169. ParameterBase* b = _paraList.at(i);
  170. if (b->isVisible())
  171. list.append(b);
  172. }
  173. ParameterBase* b = nullptr;
  174. if (i >= 0 && i < list.size()) b = list.at(i);
  175. return b;
  176. }
  177. ParameterBase* ParameterList::getParameterByName(QString name)
  178. {
  179. ParameterBase* p = nullptr;
  180. for (int i = 0; i < _paraList.size(); ++i)
  181. {
  182. auto pa = _paraList.at(i);
  183. if (name == pa->getDescribe())
  184. {
  185. p = pa;
  186. break;;
  187. }
  188. }
  189. return p;
  190. }
  191. ParameterBase* ParameterList::copyParameter(ParameterBase* p)
  192. {
  193. ParameterBase* cp = nullptr;
  194. if (p != nullptr)
  195. {
  196. ParaType t = p->getParaType();
  197. cp = createParameterByType(t);
  198. cp->copy(p);
  199. }
  200. return cp;
  201. }
  202. QList<ParameterBase*> ParameterList::getParaList(){
  203. return _paraList;
  204. }
  205. void ParameterList::dataToStream(QDataStream* datas)
  206. {
  207. int nparaList = getParameterCount();
  208. for (int i = 0; i < nparaList; i++){
  209. DataProperty::ParaType paratype = _paraList[i]->getParaType();
  210. switch (paratype)
  211. {
  212. case DataProperty::Para_Int:
  213. {
  214. DataProperty::ParameterInt *para_int = dynamic_cast<DataProperty::ParameterInt*>(getParameterAt(i));
  215. *datas << QString::number(para_int->getValue());
  216. // qDebug() << para_int->getDescribe() << para_int->getValue();
  217. break;
  218. }
  219. case DataProperty::Para_Double:
  220. {
  221. DataProperty::ParameterDouble *para_double = dynamic_cast<DataProperty::ParameterDouble*>(getParameterAt(i));
  222. *datas << QString::number(para_double->getValue());
  223. // qDebug() << para_double->getDescribe() << para_double->getValue();
  224. break;
  225. }
  226. case DataProperty::Para_Bool:
  227. {
  228. DataProperty::ParameterBool *para_bool = dynamic_cast<DataProperty::ParameterBool*>(_paraList[i]);
  229. //*datas << QString::number(para_bool->getValue());
  230. *datas << para_bool->getValue();
  231. // qDebug() << para_bool->getDescribe() << QString::number(para_bool->getValue());
  232. break;
  233. }
  234. case DataProperty::Para_Selectable:
  235. {
  236. DataProperty::ParameterSelectable *para_selectable = dynamic_cast<DataProperty::ParameterSelectable*>(_paraList[i]);
  237. //*datas << QString::number(para_selectable->getCurrentIndex());
  238. *datas << para_selectable->getCurrentIndex();
  239. // qDebug() << para_selectable->getDescribe() << para_selectable->getCurrentIndex();
  240. break;
  241. }
  242. case DataProperty::Para_String:
  243. {
  244. DataProperty::ParameterString *para_string = dynamic_cast<DataProperty::ParameterString*>(_paraList[i]);
  245. *datas << para_string->getValue();
  246. // qDebug() << para_string->getDescribe() << para_string->getValue();
  247. break;
  248. }
  249. case DataProperty::Para_Path:
  250. {
  251. DataProperty::ParameterPath *para_path = dynamic_cast<DataProperty::ParameterPath*>(_paraList[i]);
  252. *datas << para_path->getFile();
  253. // qDebug() << para_path->getDescribe() << para_path->getFile();
  254. break;
  255. }
  256. case DataProperty::Para_Table:
  257. {
  258. DataProperty::ParameterTable *para_table = dynamic_cast<DataProperty::ParameterTable*>(_paraList[i]);
  259. for (int k = 0; k < para_table->getRowCount(); k++){
  260. for (int j = 0; j < para_table->getColumnCount(); j++){
  261. //*datas << QString::number(para_table->getValue(k, j));
  262. *datas << para_table->getValue(k, j);
  263. }
  264. }
  265. break;
  266. }
  267. default:
  268. break;
  269. }
  270. }
  271. }
  272. void ParameterList::removeParameter(ParameterBase* p)
  273. {
  274. if (_paraList.contains(p))
  275. {
  276. _paraList.removeOne(p);
  277. delete p;
  278. }
  279. }
  280. void ParameterList::removeParameterAt(int i)
  281. {
  282. if (i < 0 || i >= _paraList.size()) return;
  283. ParameterBase* p = _paraList.at(i);
  284. this->removeParameter(p);
  285. }
  286. }