/src/DataContainers/cdfxfile.cpp

http://hexplorer.googlecode.com/ · C++ · 1284 lines · 937 code · 154 blank · 193 comment · 336 complexity · 842b46ea78701799cd0a94e85377c63e MD5 · raw file

  1. // HEXplorer is an Asap and HEX file editor
  2. // Copyright (C) 2011 <Christophe Hoel>
  3. //
  4. // This file is part of HEXplorer.
  5. //
  6. // This program is free software: you can redistribute it and/or modify
  7. // it under the terms of the GNU General Public License as published by
  8. // the Free Software Foundation, either version 3 of the License, or
  9. // (at your option) any later version.
  10. //
  11. // This program is distributed in the hope that it will be useful,
  12. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. // GNU General Public License for more details.
  15. // You should have received a copy of the GNU General Public License
  16. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. //
  18. // please contact the author at : christophe.hoel@gmail.com
  19. #include "cdfxfile.h"
  20. #include <QFile>
  21. #include <QFileInfo>
  22. #include <QDomNode>
  23. #include <QFileDialog>
  24. #include <QMessageBox>
  25. #include <QDir>
  26. #include <QtXmlPatterns>
  27. #include <QHeaderView>
  28. #include <typeinfo>
  29. #include "data.h"
  30. #include "lexer.h"
  31. #include "lexerCsv.h"
  32. #include "formcompare.h"
  33. #include "spreadsheetview.h"
  34. #include "sptablemodel.h"
  35. #include "qdebug.h"
  36. bool compar(Data *a, Data *b)
  37. {
  38. if (a->getName() < b->getName())
  39. return true;
  40. else return false;
  41. }
  42. bool swInstanceCompar(SwInstance *a, SwInstance *b)
  43. {
  44. if (a->name < b->name)
  45. return true;
  46. else return false;
  47. }
  48. class MessageHandler : public QAbstractMessageHandler
  49. {
  50. public:
  51. MessageHandler()
  52. : QAbstractMessageHandler(0)
  53. {
  54. }
  55. QString statusMessage() const
  56. {
  57. return m_description;
  58. }
  59. int line() const
  60. {
  61. return m_sourceLocation.line();
  62. }
  63. int column() const
  64. {
  65. return m_sourceLocation.column();
  66. }
  67. protected:
  68. virtual void handleMessage(QtMsgType type, const QString &description,
  69. const QUrl &identifier, const QSourceLocation &sourceLocation)
  70. {
  71. Q_UNUSED(type);
  72. Q_UNUSED(identifier);
  73. m_messageType = type;
  74. m_description = description;
  75. m_sourceLocation = sourceLocation;
  76. }
  77. private:
  78. QtMsgType m_messageType;
  79. QString m_description;
  80. QSourceLocation m_sourceLocation;
  81. };
  82. // --------- SwInstance clas -----------//
  83. bool SwInstance::isConsistentWith(Data *data)
  84. {
  85. // ASCII
  86. // if (data->getType() == "ASCII")
  87. // {
  88. // QString text = Zvalues.at(0).at(0);
  89. // if (text.length() <= data->xCount())
  90. // {
  91. // //transform ascii text into array of char
  92. // int asciiVal;
  93. // QStringList list;
  94. // for(int i = 0; i < data->xCount(); i++)
  95. // {
  96. // if (i < text.length() - 2)
  97. // {
  98. // asciiVal = text.at(i + 1).toAscii();
  99. // list.append(QString::number(asciiVal));
  100. // }
  101. // else
  102. // {
  103. // list.append("0");
  104. // }
  105. // }
  106. // Zvalues.clear();
  107. // Zvalues.append(list);
  108. // isValid = true;
  109. // return true;
  110. // }
  111. // else
  112. // {
  113. // isValid = false;
  114. // return false;
  115. // }
  116. // }
  117. data;
  118. isValid = true;
  119. return true;
  120. }
  121. // --------- Cdfx Constructor -----------//
  122. CdfxFile::CdfxFile(QString fullCdfxFileFileName, WorkProject *parentWP, QString modName, QObject *parent)
  123. : QObject(parent) , DataContainer(parentWP, modName)
  124. {
  125. a2lProject = (PROJECT*)getParentWp()->a2lFile->getProject();
  126. fullPath = fullCdfxFileFileName;
  127. name = new char[(QFileInfo(fullPath).fileName()).toLocal8Bit().count() + 1];
  128. strcpy(name, (QFileInfo(fullPath).fileName()).toLocal8Bit().data());
  129. if (readFile())
  130. {
  131. isRead = true;
  132. }
  133. else
  134. {
  135. isRead = false;
  136. }
  137. }
  138. CdfxFile::~CdfxFile()
  139. {
  140. delete[] name;
  141. qDeleteAll(listSwInstance);
  142. }
  143. // ------------ Parser -------------//
  144. bool CdfxFile::readFile()
  145. {
  146. // create a QDomDocument
  147. QFile file(fullPath);
  148. if (file.open(QIODevice::ReadOnly))
  149. {
  150. //check syntax conformity with .dtd file
  151. if (validate(&file))
  152. // if (1)
  153. {
  154. file.seek(0);
  155. if (!document.setContent(&file))
  156. {
  157. errorList.append("DomDocument could not be created");
  158. return false;
  159. }
  160. file.close();
  161. }
  162. else
  163. {
  164. return false;
  165. }
  166. }
  167. //parse MSRSW
  168. QDomNodeList listMSRSW = document.elementsByTagName("MSRSW");
  169. QDomNode node = listMSRSW.at(0);
  170. if (node.isNull())
  171. {
  172. errorList.append("parser CDFX error : no MSRSW node in file "
  173. + QString(this->getParentWp()->a2lFile->name));
  174. QMessageBox::warning(0,"parser CDFX error","no MSRSW node in Cdf file");
  175. return false;
  176. }
  177. else
  178. {
  179. parseMSRSW(node);
  180. if (errorList.isEmpty())
  181. {
  182. swInstance2Data();
  183. return true;
  184. }
  185. else
  186. return false;
  187. }
  188. }
  189. bool CdfxFile::validate(QFile *file)
  190. {
  191. // create a schema
  192. MessageHandler messageHandler;
  193. QXmlSchema schema;
  194. schema.setMessageHandler(&messageHandler);
  195. QFile schemaFile(":/files/cdf_v2.0.0.cl.xsd");
  196. schemaFile.open(QIODevice::ReadOnly);
  197. schema.load(&schemaFile);
  198. bool errorOccurred = false;
  199. if (schema.isValid())
  200. {
  201. QXmlSchemaValidator validator(schema);
  202. if (!validator.validate(file))
  203. {
  204. errorOccurred = true;
  205. }
  206. }
  207. else
  208. {
  209. errorList.append("CDFx schema file not valid");
  210. QMessageBox::warning(0,"check Cdf file schema","CDFx schema file not valid");
  211. return false;
  212. }
  213. schemaFile.close();
  214. if (errorOccurred)
  215. {
  216. errorList.append("CDFX file is not valid at line " + QString::number(messageHandler.line()));
  217. errorList.append(messageHandler.statusMessage());
  218. QMessageBox::warning(0,"check Cdf file syntax","Cdf file not consistent with the schema");
  219. return false;
  220. }
  221. return true;
  222. }
  223. void CdfxFile::parseMSRSW(QDomNode &node)
  224. {
  225. //parse the childNodes
  226. QDomNodeList childMSRSW = node.childNodes();
  227. for (int i = 0; i < childMSRSW.count(); i++)
  228. {
  229. QDomNode node = childMSRSW.at(i);
  230. //SHORT-NAME (1)
  231. if (node.nodeName() == "SHORT-NAME")
  232. {
  233. }
  234. //CATEGORY (?)
  235. else if (node.nodeName() == "CATEGORY")
  236. {
  237. QString category = node.firstChild().nodeValue();
  238. if (category != "CDF20")
  239. {
  240. errorList.append("parser CDFX error : MSRSW category unknown in "
  241. + QString(this->getParentWp()->a2lFile->name)
  242. + " at line " + node.lineNumber());
  243. return;
  244. }
  245. }
  246. //SW-SYSTEMS (?)
  247. else if (node.nodeName() == "SW-SYSTEMS")
  248. {
  249. QDomElement swSystems = node.toElement();
  250. //SW-SYSTEM (*)
  251. QDomNodeList listSwSystem = swSystems.elementsByTagName("SW-SYSTEM");
  252. for (uint i = 0; i < listSwSystem.length(); i++)
  253. {
  254. //parse the SW-SYSTEM
  255. QDomNode qnode= listSwSystem.at(i);
  256. parseSwSystem(qnode);
  257. }
  258. }
  259. //SDGS (?)
  260. else if (node.nodeName() == "SDGS")
  261. {
  262. }
  263. //LOCS (?)
  264. else if (node.nodeName() == "LOCS")
  265. {
  266. }
  267. }
  268. }
  269. void CdfxFile::parseSwSystem(QDomNode &node)
  270. {
  271. //parse the childNodes
  272. QDomNodeList childSwSystem = node.childNodes();
  273. for (int i = 0; i < childSwSystem.count(); i++)
  274. {
  275. QDomNode node = childSwSystem.at(i);
  276. //SHORT-NAME (1)
  277. if (node.nodeName() == "SHORT-NAME")
  278. {
  279. }
  280. //LONG-NAME (?)
  281. else if (node.nodeName() == "LONG-NAME")
  282. {
  283. }
  284. //CATEGORY(?)
  285. else if (node.nodeName() == "CATEGORY")
  286. {
  287. }
  288. //SW-INSTANCE-SPEC(?)
  289. else if (node.nodeName() == "SW-INSTANCE-SPEC")
  290. {
  291. //get childNodes
  292. QDomNodeList childSwInstanceSpec = node.childNodes();
  293. for (int i = 0; i < childSwInstanceSpec.count(); i++)
  294. {
  295. QDomNode node = childSwInstanceSpec.at(i);
  296. //SW-INSTANCE-TREE (?)
  297. if (node.nodeName() == "SW-INSTANCE-TREE")
  298. {
  299. parseSwInstanceTree(node);
  300. }
  301. }
  302. }
  303. }
  304. }
  305. void CdfxFile::parseSwInstanceTree(QDomNode &node)
  306. {
  307. //parse the childNodes
  308. QDomNodeList childSwInstanceTree = node.childNodes();
  309. for (int i = 0; i < childSwInstanceTree.count(); i++)
  310. {
  311. QDomNode node = childSwInstanceTree.at(i);
  312. //SHORT-NAME (1)
  313. if (node.nodeName() == "SHORT-NAME")
  314. {
  315. }
  316. //LONG-NAME (?)
  317. else if (node.nodeName() == "LONG-NAME")
  318. {
  319. }
  320. //CATEGORY(?)
  321. else if (node.nodeName() == "CATEGORY")
  322. {
  323. QString category = node.firstChild().nodeValue();
  324. if (category != "VCD" && category != "NO_VCD")
  325. {
  326. errorList.append("parser CDFX error : SW-INSTANCE-TREE category unknown in "
  327. + QString(this->getParentWp()->a2lFile->name)
  328. + " at line " + node.lineNumber());
  329. return;
  330. }
  331. }
  332. //SW-INSTANCE-TREE-ORIGIN(?)
  333. else if (node.nodeName() == "SW-INSTANCE-TREE-ORIGIN")
  334. {
  335. }
  336. //SW-CS-COLLECTIONS(?)
  337. else if (node.nodeName() == "SW-CS-COLLECTIONS")
  338. {
  339. }
  340. //SW-INSTANCE (*)
  341. else if (node.nodeName() == "SW-INSTANCE")
  342. {
  343. //parse the SW-INSTANCE
  344. parseSwInstance(node);
  345. }
  346. }
  347. }
  348. void CdfxFile::parseSwInstance(QDomNode &node)
  349. {
  350. // check if name exists
  351. QDomElement swInstance = node.toElement();
  352. QDomNodeList list = swInstance.elementsByTagName("SHORT-NAME");
  353. if (list.count() != 1)
  354. {
  355. return;
  356. }
  357. // create a new SwInstance
  358. SwInstance *instance = new SwInstance();
  359. //parse the childNodes
  360. QDomNodeList childSwInstance = node.childNodes();
  361. for (int i = 0; i < childSwInstance.count(); i++)
  362. {
  363. QDomNode node = childSwInstance.at(i);
  364. //SHORT-NAME (1)
  365. if (node.nodeName() == "SHORT-NAME")
  366. {
  367. instance->name = node.firstChild().nodeValue();
  368. }
  369. //SW-ARRAY-INDEX (1)
  370. else if (node.nodeName() == "SW-ARRAY-INDEX")
  371. {
  372. }
  373. //LONG-NAME (?)
  374. else if (node.nodeName() == "LONG-NAME")
  375. {
  376. instance->description = node.firstChild().nodeValue();
  377. }
  378. //DISPLAY-NAME (?)
  379. else if (node.nodeName() == "DISPLAY-NAME")
  380. {
  381. }
  382. //CATEGORY (?)
  383. else if (node.nodeName() == "CATEGORY")
  384. {
  385. QString category = node.firstChild().nodeValue();
  386. if (category == "VALUE" || category == "DEPENDENT_VALUE" ||
  387. category == "BOOLEAN" || category == "ASCII" ||
  388. category == "VAL_BLK" || category == "CURVE" ||
  389. category == "MAP" || category == "COM_AXIS" ||
  390. category == "CURVE_AXIS" || category == "RES_AXIS" ||
  391. category == "STRUCTURE" || category == "UNION" ||
  392. category == "VALUE_ARRAY" || category == "CURVE_ARRAY" ||
  393. category == "MAP_ARRAY" || category == "STRUCTURE_ARRAY")
  394. {
  395. instance->category = category;
  396. }
  397. else
  398. {
  399. errorList.append("parser CDFX error : SW-INSTANCE category unknown in "
  400. + QString(this->getParentWp()->a2lFile->name)
  401. + " at line " + node.lineNumber());
  402. return;
  403. }
  404. }
  405. //SW-FEATURE-REF (?)
  406. else if (node.nodeName() == "SW-FEATURE-REF")
  407. {
  408. instance->subset = node.firstChild().nodeValue();
  409. }
  410. // SW-VALUE-CONT (?)
  411. else if (node.nodeName() == "SW-VALUE-CONT")
  412. {
  413. parseSwValueCont(node, instance);
  414. }
  415. // SW-AXIS-CONTS (?)
  416. else if (node.nodeName() == "SW-AXIS-CONTS")
  417. {
  418. QDomNodeList childSwAxisConts = node.childNodes();
  419. for (int i = 0; i < childSwAxisConts.count(); i++)
  420. {
  421. QDomNode node = childSwAxisConts.at(i);
  422. // SW-AXIS-CONT (*)
  423. if (node.nodeName() == "SW-AXIS-CONT")
  424. {
  425. parseSwAxisCont(node, instance, i);
  426. }
  427. }
  428. }
  429. //SW-CS-HISTORY (?)
  430. else if (node.nodeName() == "SW-CS-HISTORY")
  431. {
  432. }
  433. //SW-CS-FLAGS (?)
  434. else if (node.nodeName() == "FLAGS")
  435. {
  436. }
  437. //SW-INSTANCE-PROPS-VARIANTS (?)
  438. else if (node.nodeName() == "SW-INSTANCE-PROPS-VARIANTS")
  439. {
  440. }
  441. // SW-INSTANCE (*)
  442. else if (node.nodeName() == "SW-INSTANCE")
  443. {
  444. //parseSwInstance(node);
  445. }
  446. }
  447. // add the swInstance to the list
  448. QList<SwInstance*>::iterator i = qLowerBound(listSwInstance.begin(), listSwInstance.end(), instance, swInstanceCompar);
  449. listSwInstance.insert(i, instance);
  450. }
  451. void CdfxFile::parseSwValueCont(QDomNode &node, SwInstance *instance)
  452. {
  453. QDomNodeList childSwValueCont = node.childNodes();
  454. for (int i = 0; i < childSwValueCont.count(); i++)
  455. {
  456. QDomNode node = childSwValueCont.at(i);
  457. //UNIT-DISPLAY-NAME (?)
  458. if (node.nodeName() == "UNIT-DISPLAY-NAME")
  459. {
  460. instance->unit = node.firstChild().nodeValue();
  461. }
  462. //SW-ARRAYSIZE (?)
  463. else if (node.nodeName() == "SW-ARRAYSIZE")
  464. {
  465. instance->arraySize = node.firstChild().nodeValue();
  466. }
  467. //SW-VALUES-PHYS (?)
  468. else if (node.nodeName() == "SW-VALUES-PHYS")
  469. {
  470. QStringList _zValues;
  471. QDomNodeList childSwValuePhys = node.childNodes();
  472. QString valueType = "";
  473. for (int i = 0; i < childSwValuePhys.count(); i++)
  474. {
  475. QDomNode node = childSwValuePhys.at(i);
  476. valueType = childSwValuePhys.at(0).nodeName();
  477. //VT (*)
  478. if (node.nodeName() == "VT" && valueType == "VT")
  479. {
  480. //_zValues.append("\"" + node.firstChild().nodeValue() + "\"");
  481. _zValues.append(node.firstChild().nodeValue());
  482. }
  483. //V (*)
  484. else if (node.nodeName() == "V" && valueType == "V")
  485. {
  486. _zValues.append(node.firstChild().nodeValue());
  487. }
  488. //VG (*)
  489. else if (node.nodeName() == "VG" && valueType == "VG")
  490. {
  491. QStringList rowZvalues;
  492. parseVG(node, rowZvalues);
  493. //instance->Zvalues.append(rowZvalues);
  494. _zValues.append(rowZvalues);
  495. }
  496. else
  497. {
  498. errorList.append("mixed <VT>, <V> and <VG> in Cdf file.");
  499. QMessageBox::warning(0,"import Cdf file","check Cdf format");
  500. return;
  501. }
  502. }
  503. // if zValues : append to instance->Zvalues
  504. if (_zValues.count() != 0)
  505. instance->Zvalues.append(_zValues);
  506. }
  507. }
  508. }
  509. void CdfxFile::parseSwAxisCont(QDomNode &node, SwInstance *instance, int index)
  510. {
  511. //parse the childNodes
  512. QDomNodeList childSwAxisCont = node.childNodes();
  513. for (int i = 0; i < childSwAxisCont.count(); i++)
  514. {
  515. QDomNode node = childSwAxisCont.at(i);
  516. //CATEGORY (?)
  517. if (node.nodeName() == "CATEGORY")
  518. {
  519. QString category = node.firstChild().nodeValue();
  520. if (category == "FIX_AXIS" || category == "STD_AXIS" ||
  521. category == "COM_AXIS" || category == "CURVE_AXIS" ||
  522. category == "RES_AXIS")
  523. {
  524. if (index == 0)
  525. instance->axisXtype = category;
  526. else if (index == 1)
  527. instance->axisYtype = category;
  528. else if (index == 2)
  529. instance->axisZtype = category;
  530. }
  531. else
  532. {
  533. errorList.append("parser CDFX error : SW-AXIS-CONT category unknown in "
  534. + QString(this->getParentWp()->a2lFile->name)
  535. + " at line " + node.lineNumber());
  536. return;
  537. }
  538. }
  539. //UNIT-DISPLAY-NAME (?)
  540. else if (node.nodeName() == "UNIT-DISPLAY-NAME")
  541. {
  542. QString unit = "\"" + node.firstChild().nodeValue() + "\"";
  543. if (index == 0)
  544. instance->axisXunit = unit;
  545. else if (index == 1)
  546. instance->axisYunit = unit;
  547. else if (index == 2)
  548. instance->axisZunit= unit;
  549. }
  550. //SW-INSTANCE-REF (?)
  551. else if (node.nodeName() == "SW-INSTANCE-REF")
  552. {
  553. QString reference = node.firstChild().nodeValue();
  554. if (index == 0)
  555. instance->axisXreference = reference;
  556. else if (index == 1)
  557. instance->axisYreference = reference;
  558. else if (index == 2)
  559. instance->axisZreference = reference;
  560. }
  561. //SW-ARRAYSIZE (?)
  562. else if (node.nodeName() == "SW-ARRAYSIZE")
  563. {
  564. }
  565. //SW-VALUES-PHYS (?)
  566. else if (node.nodeName() == "SW-VALUES-PHYS")
  567. {
  568. QStringList listAxisValues;
  569. QDomNodeList childSwValuePhys = node.childNodes();
  570. QString valueType = "";
  571. for (int i = 0; i < childSwValuePhys.count(); i++)
  572. {
  573. QDomNode node = childSwValuePhys.at(i);
  574. valueType = childSwValuePhys.at(0).nodeName();
  575. //VT (*)
  576. if (node.nodeName() == "VT" && valueType == "VT")
  577. {
  578. //listAxisValues.append("\"" + node.firstChild().nodeValue() + "\"");
  579. listAxisValues.append(node.firstChild().nodeValue());
  580. }
  581. //V (*)
  582. else if (node.nodeName() == "V" && valueType == "V")
  583. {
  584. listAxisValues.append(node.firstChild().nodeValue());
  585. }
  586. //VG (*)
  587. else if (node.nodeName() == "VG" && valueType == "VG")
  588. {
  589. parseVG(node, listAxisValues);
  590. }
  591. else
  592. {
  593. errorList.append("mixed <VT>, <V> and <VG> in Cdf file.");
  594. QMessageBox::warning(0,"import Cdf file","check Cdf format");
  595. return;
  596. }
  597. }
  598. // copy the listValues into data.x or data.y
  599. if (index == 0)
  600. {
  601. instance->axisXvalues.append(listAxisValues);
  602. }
  603. else if (index == 1)
  604. {
  605. instance->axisYvalues.append(listAxisValues);
  606. }
  607. else if (index == 2)
  608. {
  609. instance->axisZvalues.append(listAxisValues);
  610. }
  611. }
  612. }
  613. }
  614. void CdfxFile::parseVG(QDomNode &node, QStringList &list)
  615. {
  616. QDomNodeList childVG = node.childNodes();
  617. QString valueType = "";
  618. for (int i = 0; i < childVG.count(); i++)
  619. {
  620. QDomNode child = childVG.at(i);
  621. //VT (*)
  622. if (child.nodeName() == "VT" && (valueType == "VT" || valueType == ""))
  623. {
  624. valueType = "VT";
  625. //list.append("\"" + child.firstChild().nodeValue() + "\"");
  626. list.append(child.firstChild().nodeValue());
  627. }
  628. //V (*)
  629. else if (child.nodeName() == "V" && (valueType == "V" || valueType == ""))
  630. {
  631. valueType = "V";
  632. list.append(child.firstChild().nodeValue());
  633. }
  634. //VG (*)
  635. else if (child.nodeName() == "VG" && (valueType == "VG" || valueType == ""))
  636. {
  637. valueType = "VG";
  638. parseVG(child, list);
  639. }
  640. //LABEL (?)
  641. else if (child.nodeName() == "LABEL")
  642. {
  643. }
  644. else
  645. {
  646. errorList.append("mixed <VT>, <V> and <VG> in Cdf file.");
  647. QMessageBox::warning(0,"import Cdf file","check Cdf format");
  648. return;
  649. }
  650. }
  651. }
  652. void CdfxFile::swInstance2Data()
  653. {
  654. //convert SwInstance into Data
  655. foreach (SwInstance *instance, listSwInstance)
  656. {
  657. Data *data = NULL;
  658. QString instanceName = instance->name;
  659. if (!getData(instanceName))
  660. {
  661. //get the characteristic node from a2l
  662. Node *nodeChar = a2lProject->getNode("MODULE/" + getModuleName() + "/CHARACTERISTIC");
  663. Node *nodeAxis = a2lProject->getNode("MODULE/" + getModuleName() + "/AXIS_PTS");
  664. Node *label = nodeChar->getNode(instanceName);
  665. if (label)
  666. {
  667. //create a data
  668. data = new Data((CHARACTERISTIC*)label, a2lProject, this, false);
  669. data->isSortedByRow = 1;
  670. }
  671. else
  672. {
  673. Node *label2 = nodeAxis->getNode(instanceName);
  674. if (label2)
  675. {
  676. //create a data
  677. data = new Data((AXIS_PTS*)label2, a2lProject, this, false);
  678. }
  679. else
  680. {
  681. infoList.append("parser CDFX info : " + instanceName + " not found into "
  682. + QString(this->getParentWp()->a2lFile->name));
  683. }
  684. }
  685. // perform checks and convert to Data
  686. if (data && instance->isConsistentWith(data))
  687. {
  688. // status to define if can be added or not to dataList
  689. bool swInstanceStatus = true;
  690. // case of ASCII
  691. QString type = data->getType();
  692. if (type == "ASCII")
  693. {
  694. QString text = instance->Zvalues.at(0);
  695. if (text.length() <= data->xCount())
  696. {
  697. //transform ascii text into array of char
  698. int asciiVal;
  699. QStringList list;
  700. for(int i = 0; i < data->xCount(); i++)
  701. {
  702. if (i < text.length() - 2)
  703. {
  704. asciiVal = text.at(i + 1).toAscii();
  705. list.append(QString::number(asciiVal));
  706. }
  707. else
  708. {
  709. list.append("0");
  710. }
  711. }
  712. instance->Zvalues.clear();
  713. instance->Zvalues.append(list);
  714. }
  715. else
  716. {
  717. swInstanceStatus = false;
  718. infoList.append("label " + instanceName + " : text too long.");
  719. }
  720. }
  721. // copy the listValues into data.z
  722. foreach (QString str, instance->Zvalues)
  723. {
  724. // convert PHYS value from Csv into HEX value
  725. QString hex = data->phys2hex(str, "z");
  726. // convert HEX value into PHYS value
  727. QString phys = data->hex2phys(hex, "z");
  728. // copy PHYS value into Zaxis
  729. data->appendZ(phys);
  730. }
  731. // fill in axis for : AXIS_PTS / CURVE / MAP
  732. if (type == "AXIS_PTS")
  733. {
  734. // axisX
  735. for (int i = 0; i < data->zCount(); i++)
  736. {
  737. data->appendX(QString::number(i));
  738. }
  739. }
  740. else if (type == "CURVE")
  741. {
  742. if (QString(data->getAxisDescrX()->getPar("Attribute")) == "COM_AXIS")
  743. {
  744. //get dataName corrisponding to the AXIS_PTS
  745. AXIS_PTS_REF *axisPtsRef = (AXIS_PTS_REF*)data->getAxisDescrX()->getItem("AXIS_PTS_REF");
  746. QString nameAxisX = axisPtsRef->getPar("AxisPoints");
  747. //check if ref_axis is present
  748. SwInstance *axisXInstance = getSwInstance(nameAxisX);
  749. if (axisXInstance)
  750. {
  751. //check if axisX and Zvalues have the same length
  752. if ( instance->Zvalues.count() == axisXInstance->Zvalues.count() )
  753. {
  754. for (int i = 0; i < axisXInstance->Zvalues.count(); i++)
  755. {
  756. data->appendX(QString::number(i));
  757. }
  758. }
  759. else
  760. {
  761. // page 40 "Calibration data format V2.0.0"
  762. swInstanceStatus = false;
  763. infoList.append("label " + instanceName + " : different axisX and axisZ length.");
  764. }
  765. }
  766. else
  767. {
  768. // page 41 "Calibration data format V2.0.0"
  769. //if the object type and the including axis type description defines referenced
  770. //axis and the values are not available, this parameter is not valid
  771. swInstanceStatus = false;
  772. infoList.append("label " + instanceName + " : axisX reference is missing.");
  773. }
  774. }
  775. else if (QString(data->getAxisDescrX()->getPar("Attribute")) == "FIX_AXIS")
  776. {
  777. //OFFSET, SHIFT and NUMBERAPO
  778. FIX_AXIS_PAR *fixAxisPar = (FIX_AXIS_PAR*)data->getAxisDescrX()->getItem("FIX_AXIS_PAR");
  779. QString off = fixAxisPar->getPar("Offset");
  780. QString sft = fixAxisPar->getPar("Shift");
  781. QString napo = fixAxisPar->getPar("Numberapo");
  782. bool bl;
  783. int offset = off.toInt(&bl, 10);
  784. int shift = sft.toInt(&bl, 10);
  785. int nPtsX = napo.toUInt(&bl, 10);
  786. //check if nPts < nPtsmax
  787. QString maxAxisPts = data->getAxisDescrX()->getPar("MaxAxisPoints");
  788. double nmaxPts = maxAxisPts.toDouble();
  789. if (nPtsX > nmaxPts)
  790. nPtsX = nmaxPts;
  791. QString str;
  792. for (int i = 0; i < nPtsX; i++)
  793. {
  794. str.setNum((int)(offset + i * qPow(2, shift)), 16);
  795. data->appendX(data->hex2phys(str, "x"));
  796. }
  797. }
  798. else if (QString(data->getAxisDescrX()->getPar("Attribute")) == "STD_AXIS")
  799. {
  800. //check if axisX and Zvalues have the same length
  801. if ( instance->Zvalues.count() == instance->axisXvalues.count() )
  802. {
  803. foreach (QString str, instance->axisXvalues)
  804. {
  805. // convert PHYS value from Csv into HEX value
  806. QString hex = data->phys2hex(str, "x");
  807. // convert HEX value into PHYS value
  808. QString phys = data->hex2phys(hex, "x");
  809. // copy PHYS value into Zaxis
  810. data->appendX(phys);
  811. }
  812. }
  813. else
  814. {
  815. // page 40 "Calibration data format V2.0.0"
  816. swInstanceStatus = false;
  817. infoList.append("label " + instanceName + " : different axisX and axisZ length.");
  818. }
  819. }
  820. }
  821. else if (type == "MAP")
  822. {
  823. //axisX
  824. if (QString(data->getAxisDescrX()->getPar("Attribute")) == "COM_AXIS")
  825. {
  826. //get dataName corrisponding to the AXIS_PTS
  827. AXIS_PTS_REF *axisPtsRef = (AXIS_PTS_REF*)data->getAxisDescrX()->getItem("AXIS_PTS_REF");
  828. QString nameAxisX = axisPtsRef->getPar("AxisPoints");
  829. SwInstance *axisXInstance = getSwInstance(nameAxisX);
  830. //check if ref_axis is present
  831. if (axisXInstance)
  832. {
  833. for (int i = 0; i < axisXInstance->Zvalues.count(); i++)
  834. {
  835. data->appendX(QString::number(i));
  836. }
  837. }
  838. else
  839. {
  840. // page 41 "Calibration data format V2.0.0"
  841. //if the object type and the including axis type description defines referenced
  842. //axis and the values are not available, this parameter is not valid
  843. swInstanceStatus = false;
  844. infoList.append("label " + instanceName + " : axisX reference is missing.");
  845. }
  846. }
  847. else if (QString(data->getAxisDescrX()->getPar("Attribute")) == "FIX_AXIS")
  848. {
  849. //OFFSET, SHIFT and NUMBERAPO
  850. FIX_AXIS_PAR *fixAxisPar = (FIX_AXIS_PAR*)data->getAxisDescrX()->getItem("FIX_AXIS_PAR");
  851. QString off = fixAxisPar->getPar("Offset");
  852. QString sft = fixAxisPar->getPar("Shift");
  853. QString napo = fixAxisPar->getPar("Numberapo");
  854. bool bl;
  855. int offset = off.toInt(&bl, 10);
  856. int shift = sft.toInt(&bl, 10);
  857. int nPtsX = napo.toUInt(&bl, 10);
  858. //check if nPts < nPtsmax
  859. QString maxAxisPts = data->getAxisDescrX()->getPar("MaxAxisPoints");
  860. double nmaxPts = maxAxisPts.toDouble();
  861. if (nPtsX > nmaxPts)
  862. nPtsX = nmaxPts;
  863. QString str;
  864. for (int i = 0; i < nPtsX; i++)
  865. {
  866. str.setNum((int)(offset + i * qPow(2, shift)), 16);
  867. data->appendX(data->hex2phys(str, "x"));
  868. }
  869. }
  870. else if (QString(data->getAxisDescrX()->getPar("Attribute")) == "STD_AXIS")
  871. {
  872. foreach (QString str, instance->axisXvalues)
  873. {
  874. // convert PHYS value from Csv into HEX value
  875. QString hex = data->phys2hex(str, "x");
  876. // convert HEX value into PHYS value
  877. QString phys = data->hex2phys(hex, "x");
  878. // copy PHYS value into Zaxis
  879. data->appendX(phys);
  880. }
  881. }
  882. // axisY
  883. if (QString(data->getAxisDescrY()->getPar("Attribute")) == "COM_AXIS")
  884. {
  885. //get dataName corrisponding to the AXIS_PTS
  886. AXIS_PTS_REF *axisPtsRef = (AXIS_PTS_REF*)data->getAxisDescrY()->getItem("AXIS_PTS_REF");
  887. QString nameAxisY = axisPtsRef->getPar("AxisPoints");
  888. SwInstance *axisYInstance = getSwInstance(nameAxisY);
  889. //check if ref_axis is present
  890. if (axisYInstance)
  891. {
  892. for (int i = 0; i < axisYInstance->Zvalues.count(); i++)
  893. {
  894. data->appendY(QString::number(i));
  895. }
  896. }
  897. else
  898. {
  899. // page 41 "Calibration data format V2.0.0"
  900. //if the object type and the including axis type description defines referenced
  901. //axis and the values are not available, this parameter is not valid
  902. swInstanceStatus = false;
  903. infoList.append("label " + instanceName + " : axisY reference is missing.");
  904. }
  905. }
  906. else if (QString(data->getAxisDescrY()->getPar("Attribute")) == "FIX_AXIS")
  907. {
  908. //OFFSET, SHIFT and NUMBERAPO
  909. FIX_AXIS_PAR *fixAxisPar = (FIX_AXIS_PAR*)data->getAxisDescrY()->getItem("FIX_AXIS_PAR");
  910. QString off = fixAxisPar->getPar("Offset");
  911. QString sft = fixAxisPar->getPar("Shift");
  912. QString napo = fixAxisPar->getPar("Numberapo");
  913. bool bl;
  914. int offset = off.toInt(&bl, 10);
  915. int shift = sft.toInt(&bl, 10);
  916. int nPtsX = napo.toUInt(&bl, 10);
  917. //check if nPts < nPtsmax
  918. QString maxAxisPts = data->getAxisDescrY()->getPar("MaxAxisPoints");
  919. double nmaxPts = maxAxisPts.toDouble();
  920. if (nPtsX > nmaxPts)
  921. nPtsX = nmaxPts;
  922. QString str;
  923. for (int i = 0; i < nPtsX; i++)
  924. {
  925. str.setNum((int)(offset + i * qPow(2, shift)), 16);
  926. data->appendY(data->hex2phys(str, "x"));
  927. }
  928. }
  929. else if (QString(data->getAxisDescrY()->getPar("Attribute")) == "STD_AXIS")
  930. {
  931. foreach (QString str, instance->axisYvalues)
  932. {
  933. // convert PHYS value from Csv into HEX value
  934. QString hex = data->phys2hex(str, "y");
  935. // convert HEX value into PHYS value
  936. QString phys = data->hex2phys(hex, "y");
  937. // copy PHYS value into Zaxis
  938. data->appendY(phys);
  939. }
  940. }
  941. // check dimension
  942. if (data->zCount() != data->xCount() * data->yCount())
  943. {
  944. // page 40 "Calibration data format V2.0.0"
  945. swInstanceStatus = false;
  946. infoList.append("label " + instanceName + " : different axisX, axisY and axisZ length.");
  947. }
  948. }
  949. //add data to the listData only if every check is passed
  950. if (swInstanceStatus)
  951. {
  952. data->updateSize();
  953. QList<Data*>::iterator i = qLowerBound(listData.begin(), listData.end(), data, compar);
  954. listData.insert(i, data);
  955. }
  956. else
  957. {
  958. delete data;
  959. }
  960. }
  961. else
  962. {
  963. infoList.append("label " + instanceName + " from Cdf file is not consistent with this project");
  964. }
  965. }
  966. else
  967. {
  968. // ignore this SW-INSTANCE
  969. // the first SW-INSTANCE of the CDF v2.0 file is used
  970. // see Calibration Data Format v2.0.0 at page 40
  971. infoList.append("label " + instanceName + " from Cdf file is ignored because declared more times.");
  972. }
  973. }
  974. }
  975. // -------------------------------//
  976. std::string CdfxFile::pixmap()
  977. {
  978. return ":/icones/excel.png";
  979. }
  980. QString CdfxFile::fullName()
  981. {
  982. return fullPath;
  983. }
  984. QStringList CdfxFile::getListNameData()
  985. {
  986. QStringList list;
  987. foreach (Data *data, listData)
  988. {
  989. list.append(data->name);
  990. }
  991. return list;
  992. }
  993. QStringList CdfxFile::getErrorList()
  994. {
  995. return errorList;
  996. }
  997. QStringList CdfxFile::getInfoList()
  998. {
  999. return infoList;
  1000. }
  1001. void CdfxFile::checkDisplay()
  1002. {
  1003. //check if parentWp still exists
  1004. if (!parentNode->getParentNode()->isChild(this->parentNode->name))
  1005. {
  1006. int ret = QMessageBox::warning(0, "HEXplorer :: add project",
  1007. "This project was deleted !\nDo you want to reload it ?",
  1008. QMessageBox::Ok, QMessageBox::Cancel);
  1009. if (ret == QMessageBox::Ok)
  1010. {
  1011. MDImain *mdi = getParentWp()->parentWidget;
  1012. mdi->reAppendProject(getParentWp());
  1013. }
  1014. }
  1015. //check if this is alaways a child of its parentWp
  1016. if (!parentNode->isChild(this->name))
  1017. {
  1018. int ret = QMessageBox::warning(0, "HEXplorer :: add CdfxFileFile",
  1019. "This CdfxFile file was deleted !\nDo you want to reload it ?",
  1020. QMessageBox::Ok, QMessageBox::Cancel);
  1021. if (ret == QMessageBox::Ok)
  1022. {
  1023. WorkProject *wp = this->getParentWp();
  1024. wp->addCdfx(this);
  1025. this->attach(wp);
  1026. }
  1027. }
  1028. }
  1029. void CdfxFile::attach(QObject *o)
  1030. {
  1031. //check owner for validity
  1032. if(o==0)return;
  1033. //check for duplicates
  1034. //if(owners.contains(o))return;
  1035. //register
  1036. owners.append(o);
  1037. connect(o,SIGNAL(destroyed(QObject*)),this,SLOT(detach(QObject*)));
  1038. }
  1039. void CdfxFile::detach(QObject *o)
  1040. {
  1041. //remove it
  1042. //owners.removeAll(o);
  1043. owners.removeOne(o);
  1044. //remove self after last one
  1045. if(owners.size()==0)
  1046. delete this;
  1047. }
  1048. void CdfxFile::addLabelsAsChild()
  1049. {
  1050. foreach (Data *data, listData)
  1051. {
  1052. data->setParentNode(this);
  1053. this->addChildNode(data);
  1054. getParentWp()->treeModel->dataInserted(this, childNodes.indexOf(data));
  1055. }
  1056. }
  1057. bool CdfxFile::save(QString fileName)
  1058. {
  1059. if (fileName == "")
  1060. {
  1061. if (exportDataList2Cdf(getListNameData(), this->fullName()))
  1062. {
  1063. //remove childrens nodes
  1064. foreach (Data* data, modifiedData)
  1065. {
  1066. data->clearOldValues();
  1067. removeModifiedData(data);
  1068. }
  1069. return true;
  1070. }
  1071. else
  1072. return false;
  1073. }
  1074. else
  1075. {
  1076. if (exportDataList2Cdf(getListNameData(), fileName))
  1077. {
  1078. //remove childrens nodes
  1079. foreach (Data* data, modifiedData)
  1080. {
  1081. data->clearOldValues();
  1082. removeModifiedData(data);
  1083. }
  1084. return true;
  1085. }
  1086. else
  1087. return false;
  1088. }
  1089. }
  1090. void CdfxFile::setFullName(QString fullName)
  1091. {
  1092. fullPath = fullName;
  1093. WorkProject *wp = getParentWp();
  1094. wp->rename(this);
  1095. // change the name displayed into owners
  1096. foreach(QObject *obj, owners)
  1097. {
  1098. QString type = typeid(*obj).name();
  1099. if (type.toLower().endsWith("formcompare"))
  1100. {
  1101. FormCompare *fcomp = (FormCompare*)obj;
  1102. if (fcomp->getCdf1() == this)
  1103. {
  1104. QString str = QFileInfo(getParentWp()->getFullA2lFileName().c_str()).fileName()
  1105. + "/"
  1106. + QFileInfo(fullPath).fileName();
  1107. fcomp->setDataset1(str);
  1108. }
  1109. else if (fcomp->getCdf2() == this)
  1110. {
  1111. QString str = QFileInfo(getParentWp()->getFullA2lFileName().c_str()).fileName()
  1112. + "/"
  1113. + QFileInfo(fullPath).fileName();
  1114. fcomp->setDataset2(str);
  1115. }
  1116. }
  1117. }
  1118. }
  1119. SwInstance *CdfxFile::getSwInstance(QString str)
  1120. {
  1121. SwInstance instance;
  1122. instance.name = str;
  1123. QList<SwInstance*>::iterator i = qBinaryFind(listSwInstance.begin(), listSwInstance.end(), &instance, swInstanceCompar);
  1124. if (i == listSwInstance.end())
  1125. {
  1126. return NULL;
  1127. }
  1128. else
  1129. return *i;
  1130. }