1
/***************************************************************************
4
--------------------------------------------------------------------
5
Copyright : (C) 2006-2007 by Ion Vasilief, Alex Kargovsky, Tilman Hoener zu Siederdissen
6
Email (use @ for *) : ion_vasilief*yahoo.fr, kargovsky*yumr.phys.msu.su, thzs*gmx.net
7
Description : Origin project import class
9
***************************************************************************/
11
/***************************************************************************
13
* This program is free software; you can redistribute it and/or modify *
14
* it under the terms of the GNU General Public License as published by *
15
* the Free Software Foundation; either version 2 of the License, or *
16
* (at your option) any later version. *
18
* This program is distributed in the hope that it will be useful, *
19
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
20
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
21
* GNU General Public License for more details. *
23
* You should have received a copy of the GNU General Public License *
24
* along with this program; if not, write to the Free Software *
25
* Foundation, Inc., 51 Franklin Street, Fifth Floor, *
26
* Boston, MA 02110-1301 USA *
28
***************************************************************************/
29
#include "importOPJ.h"
33
#include <QMessageBox>
34
#include <QDockWidget>
37
#include "MultiLayer.h"
39
#include "QwtHistogram.h"
41
#define OBJECTXOFFSET 200
43
QString strreverse(const QString &str) //QString reversing
46
for(int i=str.length()-1; i>=0; --i)
53
ImportOPJ::ImportOPJ(ApplicationWindow *app, const QString& filename) :
57
OPJFile opj((const char *)filename.latin1());
58
parse_error = opj.Parse();
62
mw->showResults(opj.resultsLogString(),mw->logWindow->isVisible());
65
int ImportOPJ::translateOrigin2QtiplotLineStyle(int linestyle) {
73
case OPJFile::ShortDash:
77
case OPJFile::ShortDot:
80
case OPJFile::DashDot:
81
case OPJFile::ShortDashDot:
84
case OPJFile::DashDotDot:
91
bool ImportOPJ::importTables(OPJFile opj)
94
int QtiPlot_scaling_factor=10; //in Origin width is measured in characters while in QtiPlot - pixels --- need to be accurate
95
for (int s=0; s<opj.numSpreads(); s++)
97
int nr_cols = opj.numCols(s);
98
int maxrows = opj.maxRows(s);
100
Table *table = (opj.spreadHidden(s)||opj.spreadLoose(s))&&opj.Version()==7.5 ? mw->newHiddenTable(opj.spreadName(s), opj.spreadLabel(s), maxrows, nr_cols)
101
: mw->newTable(opj.spreadName(s), maxrows, nr_cols);
105
table->setWindowLabel(opj.spreadLabel(s));
106
for (int j=0; j<nr_cols; j++)
108
QString name(opj.colName(s,j));
109
table->setColName(j, name.replace(QRegExp(".*_"),""));
110
table->setCommand(j, QString(opj.colCommand(s,j)));
111
table->setColComment(j, QString(opj.colComment(s,j)));
112
table->changeColWidth(opj.colWidth(s,j)*QtiPlot_scaling_factor, j);
114
if (QString(opj.colType(s,j)) == "X")
115
table->setColPlotDesignation(j, Table::X);
116
else if (QString(opj.colType(s,j)) == "Y")
117
table->setColPlotDesignation(j, Table::Y);
118
else if (QString(opj.colType(s,j)) == "Z")
119
table->setColPlotDesignation(j, Table::Z);
120
else if (QString(opj.colType(s,j)) == "DX")
121
table->setColPlotDesignation(j, Table::xErr);
122
else if (QString(opj.colType(s,j)) == "DY")
123
table->setColPlotDesignation(j, Table::yErr);
125
table->setColPlotDesignation(j, Table::None);
127
table->setHeaderColType();//update header
130
switch(opj.colValueType(s,j))
133
case 6: //Text&Numeric
135
if(opj.colNumDisplayType(s,j)==0)
138
switch(opj.colValueTypeSpec(s,j))
140
case 0: //Decimal 1000
146
case 2: //Engeneering
147
case 3: //Decimal 1,000
151
table->setColNumericFormat(f, opj.colDecPlaces(s,j), j);
154
table->setTextFormat(j);
157
switch(opj.colValueTypeSpec(s,j))
200
table->setDateFormat(format, j);
203
switch(opj.colValueTypeSpec(s,j))
215
format="hh:mm:ss.zzz";
236
format="hh:mm:ss.zzz";
239
table->setTimeFormat(format, j);
242
switch(opj.colValueTypeSpec(s,j))
246
format="shortMonthName";
249
format="longMonthName";
252
table->setMonthFormat(format, j);
255
switch(opj.colValueTypeSpec(s,j))
259
format="shortDayName";
262
format="longDayName";
265
table->setDayFormat(format, j);
269
for (int i=0; i<opj.numRows(s,j); i++)
271
if(strcmp(opj.colType(s,j),"LABEL")&&opj.colValueType(s,j)!=1)
273
double* val = (double*)opj.oData(s,j,i,true);
274
if(fabs(*val)>0 && fabs(*val)<2.0e-300)// empty entry
277
table->setText(i, j, QString::number(*val));
279
else// label? doesn't seem to work
280
table->setText(i, j, QString((char*)opj.oData(s,j,i)));
285
if(!(opj.spreadHidden(s)||opj.spreadLoose(s))||opj.Version()!=7.5)
290
int dx=table->verticalHeaderWidth();
291
int dy=table->parentWidget()->frameGeometry().height() - table->height();
292
table->parentWidget()->move(QPoint(visible_count*dx+xoffset*OBJECTXOFFSET,visible_count*dy));
298
for (int s=0; s<opj.numMatrices(); s++)
300
int nr_cols = opj.numMartixCols(s);
301
int nr_rows = opj.numMartixRows(s);
303
Matrix* matrix = mw->newMatrix(opj.matrixName(s), nr_rows, nr_cols);
307
matrix->setWindowLabel(opj.matrixLabel(s));
308
matrix->setFormula(opj.matrixFormula(s));
309
matrix->setColumnsWidth(opj.matrixWidth(s)*QtiPlot_scaling_factor);
310
for (int j=0; j<nr_cols; j++)
312
for (int i=0; i<nr_rows; i++)
314
double val = opj.matrixData(s,j,i);
315
if(fabs(val)>0 && fabs(val)<2.0e-300)// empty entry
318
matrix->setCell(i, j, val);
322
matrix->saveCellsToMemory();
325
switch(opj.matrixValueTypeSpec(s))
327
case 0: //Decimal 1000
333
case 2: //Engeneering
334
case 3: //Decimal 1,000
338
matrix->setNumericFormat(f, opj.matrixSignificantDigits(s));
340
matrix->showNormal();
342
//cascade the matrices
343
int dx=matrix->verticalHeaderWidth();
344
int dy=matrix->parentWidget()->frameGeometry().height() - matrix->height();
345
matrix->parentWidget()->move(QPoint(visible_count*dx+xoffset*OBJECTXOFFSET,visible_count*dy));
355
bool ImportOPJ::importNotes(OPJFile opj)
358
for (int n=0; n<opj.numNotes(); n++)
360
QString name=opj.noteName(n);
361
QRegExp rx("^@(\\S+)$");
362
if(rx.indexIn(name)==0)
364
name=name.mid(2,name.length()-3);
366
Note *note = mw->newNote(name);
369
note->setWindowLabel(opj.noteLabel(n));
370
note->setText(opj.noteText(n));
374
int dy=note->parentWidget()->frameGeometry().height() - note->height();
375
note->parentWidget()->move(QPoint(visible_count*dx+xoffset*OBJECTXOFFSET,visible_count*dy));
383
bool ImportOPJ::importGraphs(OPJFile opj)
385
double pi=3.141592653589793;
387
int tickTypeMap[]={0,3,1,2};
388
for (int g=0; g<opj.numGraphs(); g++)
390
MultiLayer *ml = mw->multilayerPlot(opj.graphName(g));
394
ml->hide();//!hack used in order to avoid resize and repaint events
395
ml->setWindowLabel(opj.graphLabel(g));
396
for(int l=0; l<opj.numLayers(g); l++)
398
Graph *graph=ml->addLayer();
402
graph->setXAxisTitle(parseOriginText(QString::fromLocal8Bit(opj.layerXAxisTitle(g,l))));
403
graph->setYAxisTitle(parseOriginText(QString::fromLocal8Bit(opj.layerYAxisTitle(g,l))));
404
if(strlen(opj.layerLegend(g,l))>0)
405
graph->newLegend(parseOriginText(QString::fromLocal8Bit(opj.layerLegend(g,l))));
409
for(int c=0; c<opj.numCurves(g,l); c++)
411
QString data(opj.curveDataName(g,l,c));
413
switch(opj.curveType(g,l,c))
418
case OPJFile::Scatter:
419
style=Graph::Scatter;
421
case OPJFile::LineSymbol:
422
style=Graph::LineSymbols;
424
case OPJFile::ErrorBar:
425
case OPJFile::XErrorBar:
426
style=Graph::ErrorBars;
428
case OPJFile::Column:
429
style=Graph::VerticalBars;
432
style=Graph::HorizontalBars;
434
case OPJFile::Histogram:
435
style=Graph::Histogram;
441
switch(data[0].toAscii())
444
tableName = data.right(data.length()-2);
445
if(style==Graph::ErrorBars)
447
int flags=opj.curveSymbolType(g,l,c);
448
graph->addErrorBars(tableName + "_" + opj.curveXColName(g,l,c), mw->table(tableName), tableName + "_" + opj.curveYColName(g,l,c),
449
((flags&0x10)==0x10?0:1), ceil(opj.curveLineWidth(g,l,c)), ceil(opj.curveSymbolSize(g,l,c)), QColor(Qt::black),
450
(flags&0x40)==0x40, (flags&2)==2, (flags&1)==1);
452
else if(style==Graph::Histogram)
453
graph->insertCurve(mw->table(tableName), tableName + "_" + opj.curveYColName(g,l,c), style);
455
graph->insertCurve(mw->table(tableName), tableName + "_" + opj.curveXColName(g,l,c), tableName + "_" + opj.curveYColName(g,l,c), style);
458
QStringList formulas;
459
QList<double> ranges;
460
int s=opj.functionIndex(data.right(data.length()-2).toStdString().c_str());
462
if(opj.functionType(s)==1)//Polar
465
formulas << opj.functionFormula(s) << "x";
466
ranges << pi/180*opj.functionBegin(s) << pi/180*opj.functionEnd(s);
471
formulas << opj.functionFormula(s);
472
ranges << opj.functionBegin(s) << opj.functionEnd(s);
474
graph->addFunctionCurve(type, formulas, "x", ranges, opj.functionPoints(s), opj.functionName(s));
476
mw->updateFunctionLists(type, formulas);
480
CurveLayout cl = graph->initCurveLayout(style, opj.numCurves(g,l));
481
cl.sSize = ceil(opj.curveSymbolSize(g,l,c));
482
cl.penWidth=opj.curveSymbolThickness(g,l,c);
483
color=opj.curveSymbolColor(g,l,c);
484
if((style==Graph::Scatter||style==Graph::LineSymbols)&&color==0xF7)//0xF7 -Automatic color
487
switch(opj.curveSymbolType(g,l,c)&0xFF)
517
case 9: //Horizontal -
520
case 10: //Vertical |
540
switch(opj.curveSymbolType(g,l,c)>>8)
551
color=opj.curveSymbolFillColor(g,l,c);
552
if((style==Graph::Scatter||style==Graph::LineSymbols)&&color==0xF7)//0xF7 -Automatic color
553
color=17;// depend on Origin settings - not stored in file
560
cl.lWidth = ceil(opj.curveLineWidth(g,l,c));
561
color=opj.curveLineColor(g,l,c);
562
cl.lCol=(color==0xF7?0:color); //0xF7 -Automatic color
563
int linestyle=opj.curveLineStyle(g,l,c);
564
cl.filledArea=(opj.curveIsFilledArea(g,l,c)||style==Graph::VerticalBars||style==Graph::HorizontalBars||style==Graph::Histogram)?1:0;
567
switch(opj.curveFillPattern(g,l,c))
603
color=(cl.aStyle==0 ? opj.curveFillAreaColor(g,l,c) : opj.curveFillPatternColor(g,l,c));
604
cl.aCol=(color==0xF7?0:color); //0xF7 -Automatic color
605
if (style == Graph::VerticalBars || style == Graph::HorizontalBars || style == Graph::Histogram)
607
color=opj.curveFillPatternBorderColor(g,l,c);
608
cl.lCol = (color==0xF7?0:color); //0xF7 -Automatic color
609
color=(cl.aStyle==0 ? opj.curveFillAreaColor(g,l,c) : opj.curveFillPatternColor(g,l,c));
610
cl.aCol=(color==0xF7?cl.lCol:color); //0xF7 -Automatic color
611
cl.lWidth = ceil(opj.curveFillPatternBorderWidth(g,l,c));
612
linestyle=opj.curveFillPatternBorderStyle(g,l,c);
621
case OPJFile::ShortDash:
625
case OPJFile::ShortDot:
628
case OPJFile::DashDot:
629
case OPJFile::ShortDashDot:
632
case OPJFile::DashDotDot:
637
graph->updateCurveLayout(c, &cl);
638
if (style == Graph::VerticalBars || style == Graph::HorizontalBars)
640
QwtBarCurve *b = (QwtBarCurve*)graph->curve(c);
642
b->setGap(qRound(100-opj.curveSymbolSize(g,l,c)*10));
644
else if(style == Graph::Histogram)
646
QwtHistogram *h = (QwtHistogram*)graph->curve(c);
649
vector<double> bin=opj.layerHistogram(g,l);
651
h->setBinning(false, bin[0], bin[1], bin[2]);
655
switch(opj.curveLineConnect(g,l,c))
657
case OPJFile::NoLine:
658
graph->setCurveStyle(c, QwtPlotCurve::NoCurve);
660
case OPJFile::Straight:
661
graph->setCurveStyle(c, QwtPlotCurve::Lines);
663
case OPJFile::BSpline:
664
case OPJFile::Bezier:
665
case OPJFile::Spline:
666
graph->setCurveStyle(c, 5);
668
case OPJFile::StepHorizontal:
669
case OPJFile::StepHCenter:
670
graph->setCurveStyle(c, QwtPlotCurve::Steps);
672
case OPJFile::StepVertical:
673
case OPJFile::StepVCenter:
674
graph->setCurveStyle(c, 6);
679
vector<double> rangeX=opj.layerXRange(g,l);
680
vector<int> ticksX=opj.layerXTicks(g,l);
681
vector<double> rangeY=opj.layerYRange(g,l);
682
vector<int> ticksY=opj.layerYTicks(g,l);
683
if(style==Graph::HorizontalBars)
685
graph->setScale(0,rangeX[0],rangeX[1],rangeX[2],ticksX[0],ticksX[1],opj.layerXScale(g,l));
686
graph->setScale(2,rangeY[0],rangeY[1],rangeY[2],ticksY[0],ticksY[1],opj.layerYScale(g,l));
690
graph->setScale(2,rangeX[0],rangeX[1],rangeX[2],ticksX[0],ticksX[1],opj.layerXScale(g,l));
691
graph->setScale(0,rangeY[0],rangeY[1],rangeY[2],ticksY[0],ticksY[1],opj.layerYScale(g,l));
695
vector<graphGrid> grids=opj.layerGrid(g,l);
697
grid.majorOnX=(grids[0].hidden?0:1);
698
grid.minorOnX=(grids[1].hidden?0:1);
699
grid.majorOnY=(grids[2].hidden?0:1);
700
grid.minorOnY=(grids[3].hidden?0:1);
701
grid.majorStyle=translateOrigin2QtiplotLineStyle(grids[0].style);
702
grid.majorCol=grids[0].color;
703
grid.majorWidth=ceil(grids[0].width);
704
grid.minorStyle=translateOrigin2QtiplotLineStyle(grids[1].style);
705
grid.minorCol=grids[1].color;
706
grid.minorWidth=ceil(grids[1].width);
711
graph->setGridOptions(grid);
713
vector<graphAxisFormat> formats=opj.layerAxisFormat(g,l);
714
vector<graphAxisTick> ticks=opj.layerAxisTickLabels(g,l);
715
for(int i=0; i<4; ++i)
717
QString data(ticks[i].dataName.c_str());
718
QString tableName=data.right(data.length()-2) + "_" + ticks[i].colName.c_str();
723
int prec=ticks[i].decimal_places;
724
switch(ticks[i].value_type)
726
case OPJFile::Numeric:
728
switch(ticks[i].value_type_specification)
730
case 0: //Decimal 1000
736
case 2: //Engeneering
737
case 3: //Decimal 1,000
744
case OPJFile::Text: //Text
753
case OPJFile::Month: // Month
755
format=ticks[i].value_type_specification;
757
case OPJFile::Day: // Day
759
format=ticks[i].value_type_specification;
761
case OPJFile::ColumnHeading:
762
type=Graph::ColHeader;
763
switch(ticks[i].value_type_specification)
765
case 0: //Decimal 1000
771
case 2: //Engeneering
772
case 3: //Decimal 1,000
784
graph->showAxis(i, type, tableName, mw->table(tableName), !(formats[i].hidden),
785
tickTypeMap[formats[i].majorTicksType], tickTypeMap[formats[i].minorTicksType],
786
!(ticks[i].hidden), ColorBox::color(formats[i].color), format, prec,
787
ticks[i].rotation, 0, "", (ticks[i].color==0xF7 ? ColorBox::color(formats[i].color) : ColorBox::color(ticks[i].color)));
791
graph->setAutoscaleFonts(mw->autoScaleFonts);//restore user defined fonts behaviour
792
graph->setIgnoreResizeEvents(!mw->autoResizeLayers);
795
if(!opj.graphHidden(g))
798
int dy=ml->parentWidget()->frameGeometry().height() - ml->height();
799
ml->parentWidget()->move(QPoint(visible_count*dx+xoffset*OBJECTXOFFSET,visible_count*dy));
802
ml->arrangeLayers(true,true);
807
ml->arrangeLayers(true,true);
816
QString ImportOPJ::parseOriginText(const QString &str)
818
QStringList lines=str.split("\n");
820
for(int i=0; i<lines.size(); ++i)
824
text.append(parseOriginTags(lines[i]));
829
QString ImportOPJ::parseOriginTags(const QString &str)
832
//replace \l(...) and %(...) tags
833
QRegExp rxline("\\\\\\s*l\\s*\\(\\s*\\d+\\s*\\)");
834
QRegExp rxcol("\\%\\(\\d+\\)");
835
int pos = rxline.indexIn(line);
837
QString value = rxline.cap(0);
838
int len=value.length();
839
value.replace(QRegExp(" "),"");
840
value="\\c{"+value.mid(3,value.length()-4)+"}";
841
line.replace(pos, len, value);
842
pos = rxline.indexIn(line);
844
//Lookbehind conditions are not supported - so need to reverse string
845
QRegExp rx("\\)[^\\)\\(]*\\((?!\\s*[buig\\+\\-]\\s*\\\\)");
846
QRegExp rxfont("\\)[^\\)\\(]*\\((?![^\\:]*\\:f\\s*\\\\)");
847
QString linerev = strreverse(line);
848
QString lBracket=strreverse("&lbracket;");
849
QString rBracket=strreverse("&rbracket;");
850
QString ltagBracket=strreverse("<agbracket;");
851
QString rtagBracket=strreverse("&rtagbracket;");
852
int pos1=rx.indexIn(linerev);
853
int pos2=rxfont.indexIn(linerev);
855
while (pos1>-1 || pos2>-1) {
858
QString value = rx.cap(0);
859
int len=value.length();
860
value=rBracket+value.mid(1,len-2)+lBracket;
861
linerev.replace(pos1, len, value);
863
else if ((pos1>pos2&&pos2!=-1)||pos1==-1)
865
QString value = rxfont.cap(0);
866
int len=value.length();
867
value=rtagBracket+value.mid(1,len-2)+ltagBracket;
868
linerev.replace(pos2, len, value);
870
else if ((pos2>pos1&&pos1!=-1)||pos2==-1)
872
QString value = rx.cap(0);
873
int len=value.length();
874
value=rtagBracket+value.mid(1,len-2)+ltagBracket;
875
linerev.replace(pos1, len, value);
878
pos1=rx.indexIn(linerev);
879
pos2=rxfont.indexIn(linerev);
881
linerev.replace(ltagBracket, "(");
882
linerev.replace(rtagBracket, ")");
884
line = strreverse(linerev);
886
//replace \b(...), \i(...), \u(...), \g(...), \+(...), \-(...), \f:font(...) tags
892
"\\\\\\s*\\+\\s*\\(",
893
"\\\\\\s*\\-\\s*\\(",
894
"\\\\\\s*f\\:[^\\(]*\\("};
895
int postag[]={0,0,0,0,0,0,0};
896
QString ltag[]={"<b>","<i>","<u>","<font face=Symbol>","<sup>","<sub>","<font face=%1>"};
897
QString rtag[]={"</b>","</i>","</u>","</font>","</sup>","</sub>","</font>"};
899
for(int i=0; i<7; ++i)
900
rxtags[i].setPattern(rxstr[i]+"[^\\(\\)]*\\)");
904
for(int i=0; i<7; ++i)
906
postag[i] = rxtags[i].indexIn(line);
907
while (postag[i] > -1) {
908
QString value = rxtags[i].cap(0);
909
int len=value.length();
910
int pos2=value.indexOf("(");
912
value=ltag[i]+value.mid(pos2+1,len-pos2-2)+rtag[i];
915
int posfont=value.indexOf("f:");
916
value=ltag[i].arg(value.mid(posfont+2,pos2-posfont-2))+value.mid(pos2+1,len-pos2-2)+rtag[i];
918
line.replace(postag[i], len, value);
919
postag[i] = rxtags[i].indexIn(line);
923
for(int i=0; i<7; ++i)
925
if(rxtags[i].indexIn(line)>-1)
933
//replace unclosed tags
934
for(int i=0; i<6; ++i)
935
line.replace(QRegExp(rxstr[i]), ltag[i]);
936
rxfont.setPattern(rxstr[6]);
937
pos = rxfont.indexIn(line);
939
QString value = rxfont.cap(0);
940
int len=value.length();
941
int posfont=value.indexOf("f:");
942
value=ltag[6].arg(value.mid(posfont+2,len-posfont-3));
943
line.replace(pos, len, value);
944
pos = rxfont.indexIn(line);
947
line.replace("&lbracket;", "(");
948
line.replace("&rbracket;", ")");
953
//TODO: bug in grid dialog
954
// scale/minor ticks checkbox
955
// histogram: autobin export
956
// if prec not setted - automac+4digits