gSAFE  1.3.8
xmlolib.cpp
1 /* gSAFE - LIB
2  general Sql dAtabase FrontEnd
3  http://hyperprog.com/gsafe/
4 
5  (C) 2011 Peter Deak (hyper80@gmail.com)
6 
7  License: GPLv2 http://www.gnu.org/licenses/gpl-2.0.html
8 
9  xmlolib.cpp
10 */
11 
12 #include <QtCore>
13 #include <QtGui>
14 
15 #include "dconsole.h"
16 #include "xmlolib.h"
17 
18 
20 {
21 
22 }
23 
25 {
26 
27 }
28 
29 void HExcelXmlGenerator::generate(HBase *d, QTextStream *s, QString params)
30 {
31  int c;
32  int rowCount=0;
33  int columnCount=0;
34  bool datareaded=false;
35  HXmlWriter xml;
36  QString tname = "Generated table";
37  QString tnameshort;
38 
39  bool bordereddata = false;
40  bool putcolumnwidthsett = false;
41  bool putheader = true;
42  bool puttitle = true;
43 
44  if(params.contains("bordered_data"))
45  bordereddata = true;
46  if(params.contains("set_columnwidth"))
47  putcolumnwidthsett = true;
48  if(params.contains("without_header"))
49  putheader = false;
50  if(params.contains("notitle"))
51  puttitle = false;
52 
53  if(d->getWhoami() == "HTable")
54  {
55  if( !((HTableBase *)d)->tableTitle().isEmpty() )
56  tname = ((HTableBase *)d)->tableTitle();
57  rowCount = ((HTable *)d)->recordCount();
58  columnCount = ((HTable *)d)->fieldCount();
59 
60  }
61  if(d->getWhoami() == "HList")
62  {
63  if( !((HTableBase *)d)->tableTitle().isEmpty() )
64  tname = ((HTableBase *)d)->tableTitle();
65  rowCount = ((HList *)d)->recordCount();
66  columnCount = ((HList *)d)->fieldCount();
67  }
68  if(d->getWhoami() == "HPlainDataMatrix")
69  {
70  if( !((HPlainDataMatrix *)d)->getTitle().isEmpty() )
71  tname = ((HPlainDataMatrix *)d)->getTitle();
72  rowCount = ((HPlainDataMatrix *)d)->rowCount();
73  columnCount = ((HPlainDataMatrix *)d)->columnCount();
74  }
75 
76  if(tname.length() > 18)
77  tnameshort = tname.left(15).append("...");
78  else
79  tnameshort = tname;
80 
81  if(puttitle)
82  {
83  if(columnCount<3)
84  columnCount = 3;
85  rowCount += 3;
86  }
87 
88  if(putheader)
89  {
90  rowCount += 1;
91  }
92 
93 
94  //Begin building table
95 
96  s->setCodec("UTF-8");
97  xml.setStream(s);
98  xml.putRawData("<?xml version=\"1.0\"?>\n<?mso-application progid=\"Excel.Sheet\"?>\n");
99  xml.beginNode("Workbook","xmlns=\"urn:schemas-microsoft-com:office:spreadsheet\"\n"
100  " xmlns:o=\"urn:schemas-microsoft-com:office:office\"\n"
101  " xmlns:x=\"urn:schemas-microsoft-com:office:excel\"\n"
102  " xmlns:ss=\"urn:schemas-microsoft-com:office:spreadsheet\"\n"
103  " xmlns:html=\"http://www.w3.org/TR/REC-html40\"");
104  xml.beginNode("DocumentProperties","xmlns=\"urn:schemas-microsoft-com:office:office\"");
105  xml.putCData("Author","gSAFE");
106  xml.putCData("LastAuthor","gSAFE");
107  //xml.putCData("Created","2011-03-29T11:30:06Z");
108  xml.putCData("Company","gSAFE");
109  xml.putCData("Version","12.00");
110  xml.endNode();
111 
112  xml.beginNode("ExcelWorkbook","xmlns=\"urn:schemas-microsoft-com:office:excel\"");
113  xml.putCData("WindowHeight","8775");
114  xml.putCData("WindowWidth","14160");
115  xml.putCData("WindowTopX","360");
116  xml.putCData("WindowTopY","75");
117  xml.putCData("ProtectStructure","False");
118  xml.putCData("ProtectWindows","False");
119  xml.endNode();
120 
121  xml.beginNode("Styles");
122  xml.beginNode("Style","ss:ID=\"Default\" ss:Name=\"Normal\"");
123  xml.putEmptyNode("Alignment","ss:Vertical=\"Top\"");
124  xml.putEmptyNode("Borders");
125  xml.putEmptyNode("Font","x:CharSet=\"238\" ");
126  xml.putEmptyNode("Interior");
127  xml.putEmptyNode("NumberFormat");
128  xml.putEmptyNode("Protection");
129  xml.endNode();
130 
131  // --------- OPTIONAL STYLES -----------
132  //s21 Bold font style
133  xml.beginNode("Style","ss:ID=\"s21\"");
134  xml.putEmptyNode("Font","x:CharSet=\"238\" x:Family=\"Swiss\" ss:Bold=\"1\"");
135  xml.endNode();
136  //Bigger sized bold font style. Not wrapped.
137  xml.beginNode("Style","ss:ID=\"s22\"");
138  xml.putEmptyNode("Font","x:CharSet=\"238\" x:Family=\"Swiss\" ss:Size=\"14\" ss:Bold=\"1\"");
139  xml.putEmptyNode("Alignment","ss:Vertical=\"Top\" ss:WrapText=\"0\"");
140  xml.endNode();
141  // Bordered normal font style.
142  xml.beginNode("Style","ss:ID=\"s23\"");
143  xml.beginNode("Borders");
144  xml.putEmptyNode("Border","ss:Position=\"Bottom\" ss:LineStyle=\"Continuous\" ss:Weight=\"1\"");
145  xml.putEmptyNode("Border","ss:Position=\"Left\" ss:LineStyle=\"Continuous\" ss:Weight=\"1\"");
146  xml.putEmptyNode("Border","ss:Position=\"Right\" ss:LineStyle=\"Continuous\" ss:Weight=\"1\"");
147  xml.putEmptyNode("Border","ss:Position=\"Top\" ss:LineStyle=\"Continuous\" ss:Weight=\"1\"");
148  xml.endNode();
149  xml.endNode();
150  // Bordered and Bold font style.
151  xml.beginNode("Style","ss:ID=\"s24\"");
152  xml.putEmptyNode("Font","x:CharSet=\"238\" x:Family=\"Swiss\" ss:Bold=\"1\"");
153  xml.beginNode("Borders");
154  xml.putEmptyNode("Border","ss:Position=\"Bottom\" ss:LineStyle=\"Continuous\" ss:Weight=\"1\"");
155  xml.putEmptyNode("Border","ss:Position=\"Left\" ss:LineStyle=\"Continuous\" ss:Weight=\"1\"");
156  xml.putEmptyNode("Border","ss:Position=\"Right\" ss:LineStyle=\"Continuous\" ss:Weight=\"1\"");
157  xml.putEmptyNode("Border","ss:Position=\"Top\" ss:LineStyle=\"Continuous\" ss:Weight=\"1\"");
158  xml.endNode();
159  xml.endNode();
160  // --------- OPTIONAL STYLES -----------
161 
162  xml.endNode();//Styles
163 
164  xml.beginNode("Worksheet",QString("ss:Name=\"%1\"").arg(tnameshort));
165  xml.beginNode("Table",QString("ss:ExpandedColumnCount=\"%1\" ss:ExpandedRowCount=\"%2\""
166  " x:FullColumns=\"1\" x:FullRows=\"1\" ss:DefaultRowHeight=\"15\"")
167  .arg(columnCount)
168  .arg(rowCount)
169  );
170 
171  //Set column width if necessary
172  if(putcolumnwidthsett)
173  for(c=0;c<columnCount;++c)
174  xml.putEmptyNode("Column",QString("ss:Index=\"%1\" ss:AutoFitWidth=\"1\" ss:Width=\"%2\"")
175  .arg(c+1)
176  .arg(80));
177 
178 
179  if(puttitle)
180  {
181  xml.beginNode("Row"); xml.putEmptyNode("Cell"); xml.endNode();
182 
183  xml.beginNode("Row","ss:StyleID=\"s22\"");
184  xml.beginNode("Cell","ss:Index=\"1\"");
185  xml.putCData("Data",tname,"ss:Type=\"String\"");
186  xml.endNode();
187  xml.endNode();
188 
189  xml.beginNode("Row"); xml.putEmptyNode("Cell"); xml.endNode();
190  }
191 
192  //let's rock!
193  if(d->getWhoami() == "HTable")
194  {
195  datareaded = true;
196  HTable *data = (HTable *)d;
197 
198  HDataField *df=NULL;
199  data->firstField();
200  while((df=data->nextFieldAll()) != NULL)
201  {
202  if(!df->isShow())
203  continue;
204  xml.beginNode("Row","ss:StyleID=\"s24\"");
205  xml.beginNode("Cell");
206  xml.putCData("Data",df->getExplainText(),"ss:Type=\"String\"");
207  xml.endNode();
208  xml.beginNode("Cell",(bordereddata ? "ss:StyleID=\"s23\"" : ""));
209  xml.putCData("Data",df->dbValueToDispValue(df->getValue()).toString(),"ss:Type=\"String\"");
210  xml.endNode();
211  xml.endNode();
212  }
213  }
214 
215  if(d->getWhoami() == "HList")
216  {
217  datareaded = true;
218  HList *data = (HList *)d;
219 
220  int i;
221  QStringList sl;
222 
223  //Header!
224  if(putheader)
225  {
226  sl = data->getShortTitleHeads();
227  xml.beginNode("Row","ss:StyleID=\"s24\"");
228  for(i =0;i < (int)sl.size();++i)
229  {
230  xml.beginNode("Cell");
231  xml.putCData("Data",sl[i],"ss:Type=\"String\"");
232  xml.endNode();
233  }
234  xml.endNode();
235  }
236 
237  QList<QStringList *>::Iterator iv = data->getValues()->begin();
238  while(iv != data->getValues()->end())
239  {
240  sl = **iv;
241  xml.beginNode("Row",(bordereddata ? "ss:StyleID=\"s23\"" : ""));
242 
243  for(i =0;i < (int)sl.size();++i)
244  {
245  xml.beginNode("Cell");
246  xml.putCData("Data",sl[i],"ss:Type=\"String\"");
247  xml.endNode();
248  }
249  xml.endNode();
250  ++iv;
251  }
252  }
253 
254  if(d->getWhoami() == "HPlainDataMatrix")
255  {
256  datareaded = true;
257  HPlainDataMatrix *data = (HPlainDataMatrix *)d;
258 
259  int i;
260  QStringList sl;
261 
262  //Header!
263  if(putheader)
264  {
265  sl = data->getHeader();
266  xml.beginNode("Row","ss:StyleID=\"s24\"");
267  for(i=0;i < (int)sl.size();++i)
268  {
269  xml.beginNode("Cell");
270  xml.putCData("Data",sl[i],"ss:Type=\"String\"");
271  xml.endNode();
272  }
273  xml.endNode();
274  }
275 
276  if(data->rowCount() > 0)
277  {
278  data->firstRow();
279  do
280  {
281  sl = data->currentRowStr();
282  xml.beginNode("Row",(bordereddata ? "ss:StyleID=\"s23\"" : ""));
283  for(i =0;i < (int)sl.size();++i)
284  {
285  xml.beginNode("Cell");
286  xml.putCData("Data",sl[i],"ss:Type=\"String\"");
287  xml.endNode();
288  }
289  xml.endNode();
290  }
291  while(data->nextRow());
292  }
293  }
294 
295  if(!datareaded)
296  error(QString("HExcelXmlGenerator::generate: Cannot handle item: Not supported type (%1)!")
297  .arg(d->getWhoami()));
298 
299  xml.endNode();//Table
300 
301  xml.beginNode("WorksheetOptions","xmlns=\"urn:schemas-microsoft-com:office:excel\"");
302  xml.beginNode("PageSetup");
303  xml.putEmptyNode("Header","x:Margin=\"0.3\"");
304  xml.putEmptyNode("Footer","x:Margin=\"0.3\"");
305  xml.putEmptyNode("PageMargins","x:Bottom=\"0.75\" x:Left=\"0.7\" x:Right=\"0.7\" x:Top=\"0.75\"");
306  xml.endNode();
307 
308  xml.beginNode("Panes");
309  xml.beginNode("Pane");
310  xml.putCData("Number","3");
311  xml.putCData("ActiveRow",QString("%1").arg(rowCount));
312  xml.putCData("ActiveCol",QString("%2").arg(columnCount));
313  xml.endNode();
314  xml.endNode();
315 
316  xml.putCData("ProtectObjects","False");
317  xml.putCData("ProtectScenarios","False");
318  xml.endNode();
319  xml.endNode();
320  xml.endNode();
321 }
322 
323 QString HExcelXmlGenerator::generateToString(HBase *d, QString params)
324 {
325  QString xml;
326  QTextStream *s = new QTextStream(&xml);
327  generate(d,s,params);
328  delete s;
329  return xml;
330 }
331 
332 void HExcelXmlGenerator::generateToFile(HBase *d, QString filename, QString params)
333 {
334  QFile xmlfile(filename);
335  if (!xmlfile.open(QIODevice::WriteOnly))
336  {
337  error(QString(GSAFETEXT_CANNOT_CREATE_FILE).arg(filename));
338  return;
339  }
340 
341  QTextStream *s = new QTextStream(&xmlfile);
342  generate(d,s,params);
343  xmlfile.close();
344  delete s;
345 }
346 
347 // ////////////////////////////////////////////////////////////////////////////////////////////////////
348 
349 static const char* const image_browse_data[] = {
350 "20 20 13 1",
351 "c c #000000",
352 "e c #000080",
353 "k c #0000ff",
354 "j c #008080",
355 "i c #00ff00",
356 "h c #00ffff",
357 "g c #800000",
358 "a c #808080",
359 "d c #c0c0c0",
360 ". c #dbd8d1",
361 "f c #ff0000",
362 "# c #ffff00",
363 "b c #ffffff",
364 "....................",
365 "..#..a#.............",
366 "..a#.ab..a..........",
367 "...a#a#.a#..........",
368 "....a#ba#...........",
369 "..aaab##aaaaaaaaaa..",
370 "....#abbbbbbbbbbbc..",
371 "..abda#bbbefbbghbc..",
372 "..#.aac#bbijbbfjbc..",
373 "....abddbbddbbddbc..",
374 "....abbbbbbbbbbbbc..",
375 "....abfjbbikbbfibc..",
376 "....abghbbafbbaebc..",
377 "....abddbbddbbddbc..",
378 "....abbbbbbbbbbbbc..",
379 "....accccccccccccc..",
380 "....................",
381 "....................",
382 "....................",
383 "...................."};
384 
385 HXmloTable::HXmloTable(QWidget *parent,HBase *d)
386 :QDialog(parent)
387 
388 {
389  data = d;
390 
391  setWindowTitle(GSAFETEXT_XMLO_WINTITLE);
392 
393  QVBoxLayout *mainlayout = new QVBoxLayout(this);
394  QHBoxLayout *toplayout = new QHBoxLayout(0);
395  QHBoxLayout *midlayout = new QHBoxLayout(0);
396  QHBoxLayout *buttlayout = new QHBoxLayout(0);
397 
398  QLabel *label1 = new QLabel(GSAFETEXT_XMLO_GETLABEL,this);
399  QPushButton *savePushButton = new QPushButton(this);
400  savePushButton->setText(GSAFETEXT_SAVE);
401  QPushButton *closePushButton = new QPushButton(this);
402  closePushButton->setText(GSAFETEXT_CLOSE);
403  fnedit = new QLineEdit(this);
404  fnedit->setText("output.xml");
405  QToolButton *fcToolButton = new QToolButton(this);
406  fcToolButton->setIcon(QIcon( QPixmap( (const char **) image_browse_data ) ));
407 
408  toplayout->addStretch();
409  toplayout->addWidget(label1);
410  toplayout->addStretch();
411 
412  midlayout->addStretch();
413  midlayout->addWidget(fnedit);
414  midlayout->addSpacing(10);
415  midlayout->addWidget(fcToolButton);
416  midlayout->addStretch();
417 
418  buttlayout->addStretch();
419  buttlayout->addWidget(savePushButton);
420  buttlayout->addSpacing(10);
421  buttlayout->addWidget(closePushButton);
422  buttlayout->addStretch();
423 
424  mainlayout->addSpacing(15);
425  mainlayout->addLayout(toplayout);
426  mainlayout->addSpacing(10);
427  mainlayout->addLayout(midlayout);
428  mainlayout->addStretch();
429  mainlayout->addLayout(buttlayout);
430  mainlayout->addSpacing(20);
431 
432  setSizeGripEnabled(true);
433 
434  connect(fcToolButton,SIGNAL(clicked()),this,SLOT(cf()));
435  connect(savePushButton,SIGNAL(clicked()),this,SLOT(saveButton()));
436  connect(closePushButton,SIGNAL(clicked()),this,SLOT(closeButton()));
437  resize(300,160);
438 }
439 
441 {
442 }
443 
444 int HXmloTable::cf(void)
445 {
446  QString o;
447  o = QFileDialog::getSaveFileName(this,GSAFETEXT_XMLO_OUTFILETITLE,"","*.xml");
448  if(!o.isEmpty())
449  fnedit->setText(o);
450  return 0;
451 }
452 
453 int HXmloTable::saveButton(void)
454 {
455  HExcelXmlGenerator xmlgen;
456  sdebug("saveB");
457 
458  if(data == NULL)
459  return 0;
460  if(fnedit->text().isEmpty())
461  return 0;
462 
463  QFile f(fnedit->text());
464  if (f.open(QIODevice::WriteOnly))
465  {
466  QTextStream *s = new QTextStream(&f); // we will serialize the data into file f
467  s->setCodec(QTextCodec::codecForName("UTF-8"));
468  xmlgen.generate(data,s,"bordered_data set_columnwidth");
469  f.close();
470  }
471  else
472  {
473  error(QString(GSAFETEXT_CANNOT_CREATE_FILE).arg(fnedit->text()));
474  return 0;
475  }
476  close();
477  return 0;
478 }
479 
480 int HXmloTable::closeButton(void)
481 {
482  close();
483  return 0;
484 }
485 
486 
487 
488 //end code
HDataField * nextFieldAll(void)
Definition: datalib.cpp:2843
HExcelXmlGenerator(void)
Definition: xmlolib.cpp:19
bool nextRow(void)
Definition: datalib.cpp:1722
void generate(HBase *d, QTextStream *s, QString params="")
Definition: xmlolib.cpp:29
QStringList getShortTitleHeads(void)
Definition: datalib.h:1138
void beginNode(QString n, QString parameters="")
Definition: datalib.cpp:353
QString generateToString(HBase *d, QString params="")
Definition: xmlolib.cpp:323
HXmloTable(QWidget *parent, HBase *d)
Definition: xmlolib.cpp:385
void putRawData(QString data)
Definition: datalib.cpp:382
virtual QList< QStringList * > * getValues(void)
Definition: datalib.h:1798
void firstField(void)
Definition: datalib.cpp:2822
void putCData(QString n, QString data, QString parameters="")
Definition: datalib.cpp:366
Definition: datalib.h:312
void setStream(QTextStream *oo)
Definition: datalib.cpp:320
void putEmptyNode(QString n, QString parameters="")
Definition: datalib.cpp:374
QList< QString > currentRowStr(void)
Definition: datalib.cpp:1775
virtual QVariant dbValueToDispValue(QVariant v)
Definition: datalib.cpp:5309
void sdebug(QString s)
Definition: dconsole.cpp:48
void endNode(void)
Definition: datalib.cpp:361
~HExcelXmlGenerator(void)
Definition: xmlolib.cpp:24
QString getExplainText(void)
Definition: datalib.h:2184
QString getWhoami(void)
Definition: datalib.cpp:774
void firstRow(void)
Definition: datalib.cpp:1716
~HXmloTable(void)
Definition: xmlolib.cpp:440
virtual QVariant getValue(void)
Definition: datalib.cpp:5418
void error(QString s)
Definition: datalib.cpp:37
bool isShow(void)
Definition: datalib.h:2278
void generateToFile(HBase *d, QString filename, QString params="")
Definition: xmlolib.cpp:332
QList< QString > getHeader(void)
Definition: datalib.cpp:1500