dialogivctocsv.cpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. #include "dialogivctocsv.h"
  2. #include "ui_dialogivctocsv.h"
  3. #include <QFileDialog>
  4. #include <QMessageBox>
  5. #include <QDateTime>
  6. #include <functional>
  7. Dialogivctocsv::Dialogivctocsv(QWidget *parent) :
  8. QDialog(parent),
  9. ui(new Ui::Dialogivctocsv)
  10. {
  11. ui->setupUi(this);
  12. ui->checkBox->setChecked(false);
  13. ui->comboBox->addItem("ms");
  14. ui->comboBox->addItem("s");
  15. setWindowTitle("ivc to csv");
  16. }
  17. Dialogivctocsv::~Dialogivctocsv()
  18. {
  19. delete ui;
  20. }
  21. class ivcdataunit
  22. {
  23. public:
  24. qint64 ntime;
  25. double fvalue;
  26. };
  27. class ivcdata
  28. {
  29. public:
  30. std::string strvarname;
  31. size_t hashname;
  32. std::vector<ivcdataunit> mvectordata;
  33. int nnowpos = 0;
  34. };
  35. void Dialogivctocsv::on_pushButton_Convert_clicked()
  36. {
  37. QString strpath = QFileDialog::getOpenFileName(this,"Load ivc File",".","*.ivc");
  38. if(strpath.isEmpty())return;
  39. QString strpathSave = QFileDialog::getSaveFileName(this,"Save CSV File",".","*.csv");
  40. if(strpathSave.isEmpty())return;
  41. QFile xFileivc;
  42. QFile xFilecsv;
  43. if(strpathSave.right(4) != ".csv")
  44. {
  45. strpathSave.append(".csv");
  46. }
  47. xFileivc.setFileName(strpath);
  48. xFilecsv.setFileName(strpathSave);
  49. if(!xFileivc.open(QIODevice::ReadOnly))
  50. {
  51. QMessageBox::warning(this,"Waring","Can't Open IVC File.",QMessageBox::YesAll);
  52. return;
  53. }
  54. if(!xFilecsv.open(QIODevice::ReadWrite))
  55. {
  56. xFileivc.close();
  57. QMessageBox::warning(this,"Warning","Can't Open CSV File.",QMessageBox::YesAll);
  58. return;
  59. }
  60. int ntimemode = ui->comboBox->currentIndex();
  61. bool bFillData = ui->checkBox->isChecked();
  62. int ndatasize;
  63. iv::ivchart::ivchartarray xivarray;
  64. std::vector<ivcdata> xvectorivcdata;
  65. int nRead = xFileivc.read((char *)&ndatasize,sizeof(int));
  66. while(nRead == sizeof(int))
  67. {
  68. if(ndatasize<=0)break;
  69. char * str = new char[ndatasize];
  70. nRead = xFileivc.read(str,ndatasize);
  71. if(nRead == ndatasize)
  72. {
  73. if(xivarray.ParseFromArray(str,ndatasize))
  74. {
  75. unsigned int i;
  76. for(i=0;i<xivarray.xivchartunit_size();i++)
  77. {
  78. iv::ivchart::ivchartunit * punit = xivarray.mutable_xivchartunit(i);
  79. std::hash<std::string > h;
  80. size_t hash_unit = h(punit->strvarname());
  81. unsigned int j;
  82. bool bhave = false;
  83. for(j=0;j<xvectorivcdata.size();j++)
  84. {
  85. if(xvectorivcdata[j].hashname == hash_unit)
  86. {
  87. ivcdataunit iu;
  88. iu.ntime = punit->timex();
  89. iu.fvalue = punit->fvalue();
  90. xvectorivcdata[j].mvectordata.push_back(iu);
  91. bhave = true;
  92. break;
  93. }
  94. }
  95. if(bhave == false)
  96. {
  97. ivcdata xdata;
  98. xdata.hashname = hash_unit;
  99. xdata.strvarname = punit->strvarname();
  100. ivcdataunit iu;
  101. iu.ntime = punit->timex();
  102. iu.fvalue = punit->fvalue();
  103. xdata.mvectordata.push_back(iu);
  104. xvectorivcdata.push_back(xdata);
  105. }
  106. }
  107. }
  108. }
  109. else
  110. {
  111. break;
  112. }
  113. nRead = xFileivc.read((char *)&ndatasize,sizeof(int));
  114. }
  115. char strout[10000];
  116. char strtem[1000];
  117. double fDiv = 1;
  118. if(ntimemode == 1)fDiv = 1000.0;
  119. snprintf(strout,10000,"Time ; AbsTime ; RelTime");
  120. unsigned int i;
  121. double * xvectorlastvalue = new double[xvectorivcdata.size()];
  122. for(i=0;i<xvectorivcdata.size();i++)
  123. {
  124. snprintf(strtem,1000," ; %s",xvectorivcdata[i].strvarname.data());
  125. strncat(strout,strtem,10000);
  126. xvectorivcdata[i].nnowpos = 0;
  127. xvectorlastvalue[i] = -123456.6789;
  128. }
  129. snprintf(strtem,1000,"\n");
  130. strncat(strout,strtem,10000);
  131. xFilecsv.write(strout,strnlen(strout,10000));
  132. qint64 nstarttime = xvectorivcdata[0].mvectordata[0].ntime;
  133. qint64 nnowtime = nstarttime;
  134. bool bComplete = false;
  135. while(bComplete == false)
  136. {
  137. snprintf(strout,10000,"%s ; %lld ; %f ",QDateTime::fromMSecsSinceEpoch(nnowtime).toString("yyyy-MM-dd hh:mm:ss:zzz").toLatin1().data(),
  138. nnowtime,((double)(nnowtime - nstarttime))/fDiv);
  139. for(i=0;i<xvectorivcdata.size();i++)
  140. {
  141. if(xvectorivcdata[i].nnowpos <xvectorivcdata[i].mvectordata.size())
  142. {
  143. ivcdataunit * pdata = &xvectorivcdata[i].mvectordata[xvectorivcdata[i].nnowpos];
  144. if(nnowtime == pdata->ntime)
  145. {
  146. snprintf(strtem,1000," ; %f",pdata->fvalue);
  147. strncat(strout,strtem,10000);
  148. xvectorlastvalue[i] = pdata->fvalue;
  149. xvectorivcdata[i].nnowpos++;
  150. }
  151. else
  152. {
  153. if((bFillData)&&(xvectorlastvalue[i] != -123456.6789))
  154. {
  155. snprintf(strtem,1000," ; %f",xvectorlastvalue[i]);
  156. strncat(strout,strtem,10000);
  157. }
  158. else
  159. {
  160. snprintf(strtem,1000," ; ");
  161. strncat(strout,strtem,10000);
  162. }
  163. }
  164. }
  165. else
  166. {
  167. snprintf(strtem,1000," ; ");
  168. strncat(strout,strtem,10000);
  169. }
  170. }
  171. snprintf(strtem,1000,"\n");
  172. strncat(strout,strtem,10000);
  173. xFilecsv.write(strout,strnlen(strout,10000));
  174. bool bFindNewNow = false;
  175. for(i=0;i<xvectorivcdata.size();i++)
  176. {
  177. if(xvectorivcdata[i].nnowpos <xvectorivcdata[i].mvectordata.size())
  178. {
  179. ivcdataunit * pdata = &xvectorivcdata[i].mvectordata[xvectorivcdata[i].nnowpos];
  180. if(bFindNewNow == false)
  181. {
  182. bFindNewNow = true;
  183. nnowtime = pdata->ntime;
  184. }
  185. else
  186. {
  187. if(nnowtime > pdata->ntime)
  188. {
  189. nnowtime = pdata->ntime;
  190. }
  191. }
  192. }
  193. }
  194. if(bFindNewNow == false)
  195. {
  196. bComplete = true;
  197. break;
  198. }
  199. }
  200. xFilecsv.close();
  201. QMessageBox::information(this,"Info","Covert Complete.",QMessageBox::YesAll);
  202. }