-rw-r--r-- | noncore/apps/opie-sheet/sheet.cpp | 10 |
1 files changed, 5 insertions, 5 deletions
diff --git a/noncore/apps/opie-sheet/sheet.cpp b/noncore/apps/opie-sheet/sheet.cpp index d4419af..88847da 100644 --- a/noncore/apps/opie-sheet/sheet.cpp +++ b/noncore/apps/opie-sheet/sheet.cpp @@ -1324,1243 +1324,1243 @@ QString Sheet::calculateFunction(const QString &func, const QString ¶meters, if (function=="LEN") { // return the length of a string(param1) s1=calculateVariable(getParameter(parameters, 0, TRUE, function)); return QString::number(s1.length()); }; if (function=="MID") { // returns the mid word of string param1 with start param2 and len param3 s1=calculateVariable(getParameter(parameters, 0, TRUE, function)); w1=calculateVariable(getParameter(parameters, 1, TRUE, function)).toInt(&ok); w2=calculateVariable(getParameter(parameters, 2, TRUE, function)).toInt(&ok); s2=s1.mid(w1,w2); return QString(s2); }; if (function=="REPLACE") { //replace in param1 text in pos param2 and length param3 to newtext param4 s1=calculateVariable(getParameter(parameters, 0, TRUE, function)); w1=calculateVariable(getParameter(parameters, 1, TRUE, function)).toInt(&ok); w2=calculateVariable(getParameter(parameters, 2, TRUE, function)).toInt(&ok); s2=calculateVariable(getParameter(parameters, 3, TRUE, function)); if(w1<0 || w2<0) return QString(s1); s1=s1.left(w2-1)+s2+s1.right(s1.length()-w1-w2); return QString(s1); }; if (function=="REPT") { //repeats param1 string param2 times s1=calculateVariable(getParameter(parameters, 0, TRUE, function)); w1=calculateVariable(getParameter(parameters, 1, TRUE, function)).toInt(&ok); for(w2=1;w2<=w1;w2++) { s2=s2.append(s1); }; return QString(s2); }; if (function=="RIGHT") { // returns the param2 right chars from param1 string s1=calculateVariable(getParameter(parameters, 0, TRUE, function)); vali=calculateVariable(getParameter(parameters, 1, TRUE, function)).toInt(&ok); s2=s1.right(vali); return QString(s2); }; if (function=="UPPER") { // returns the upper param1 string s1=calculateVariable(getParameter(parameters, 0, TRUE, function)); s1=s1.upper(); return QString(s1); }; if (function=="LOWER") { // returns the lower param1 string s1=calculateVariable(getParameter(parameters, 0, TRUE, function)); s1=s1.lower(); return QString(s1); }; if (function=="IF") { //usage: IF(param1,param2,param3) //returns param4 if true(param1)/ param5 if false(param1) val1=getParameter(parameters, 0, TRUE, function).toDouble(&ok); if(val1==1.0) { s1=calculateVariable(getParameter(parameters, 1, TRUE, function)); return QString(s1); }else { s1=calculateVariable(getParameter(parameters, 2, TRUE, function)); return QString(s1); }; }; if (function=="SUM") { //NumOfParams val2=0.0; for(w1=1;w1<=(NumOfParams/2);w1++) { val1=functionSum(getParameter(parameters, (w1-1)*2, FALSE, function), getParameter(parameters, (w1-1)*2+1, TRUE, function)); val2=val2+val1; }; if(NumOfParams%2==1) { val2=val2+calculateVariable(getParameter(parameters,NumOfParams-1,FALSE, function)).toDouble(&ok); }; return QString::number(val2); }; if (function=="INDEX") { s1=functionIndex(getParameter(parameters,0,TRUE,function), getParameter(parameters, 1, TRUE, function), getParameter(parameters,2,TRUE,function).toInt(&ok)); return QString(s1); }; if (function=="SUMSQ") { //NumOfParams val2=0.0; for(w1=1;w1<=(NumOfParams/2);w1++) { val1=functionSumSQ(getParameter(parameters, (w1-1)*2, FALSE, function), getParameter(parameters, (w1-1)*2+1, TRUE, function)); val2=val2+val1; }; if(NumOfParams%2==1) { val1=calculateVariable(getParameter(parameters,NumOfParams-1,FALSE, function)).toDouble(&ok); val2=val2+val1*val1; }; return QString::number(val2); }; if (function=="COUNT") { //NumOfParams val2=0.0; for(w1=1;w1<=(NumOfParams/2);w1++) { val1=functionCount(getParameter(parameters, (w1-1)*2, FALSE, function), getParameter(parameters, (w1-1)*2+1, TRUE, function)); val2=val2+val1; }; if(NumOfParams%2==1) { val1=calculateVariable(getParameter(parameters,NumOfParams-1,FALSE, function)).toDouble(&ok); if(ok) val2=val2+1; }; return QString::number(val2); }; if (function=="COUNTIF") { //NumOfParams val1=functionCountIf(getParameter(parameters, 0, TRUE, function), getParameter(parameters, 1, TRUE, function), getParameter(parameters, 2, TRUE, function)); return QString::number(val1); }; if (function=="MIN") { //NumOfParams val2=0.0; for(w1=1;w1<=(NumOfParams/2);w1++) { val1=functionMin(getParameter(parameters, (w1-1)*2, FALSE, function), getParameter(parameters, (w1-1)*2+1, TRUE, function)); val2=val1; }; if(NumOfParams%2==1) { val1=calculateVariable(getParameter(parameters,NumOfParams-1,FALSE, function)).toDouble(&ok); if(val1<val2) val2=val1; }; return QString::number(val2); }; if (function=="MAX") { //NumOfParams val2=0.0; for(w1=1;w1<=(NumOfParams/2);w1++) { val1=functionMax(getParameter(parameters, (w1-1)*2, FALSE, function), getParameter(parameters, (w1-1)*2+1, TRUE, function)); val2=val1; }; if(NumOfParams%2==1) { val1=calculateVariable(getParameter(parameters,NumOfParams-1,FALSE, function)).toDouble(&ok); if(val1>val2) val2=val1; }; return QString::number(val2); }; if (function=="AVERAGE") { val1=functionAvg(getParameter(parameters, 0, TRUE, function), getParameter(parameters, 1, TRUE, function)); return QString::number(val1); }; if(function=="BESSELI") { // BesselI (x,n) val1=calculateVariable(getParameter(parameters, 0, TRUE, function)).toDouble(&ok); vali=calculateVariable(getParameter(parameters, 1, TRUE, function)).toInt(&ok); val2=BesselI(vali,val1); return QString::number(val2); }; if(function=="BESSELJ") { // BesselJ (x,n) val1=calculateVariable(getParameter(parameters, 0, TRUE, function)).toDouble(&ok); vali=calculateVariable(getParameter(parameters, 1, TRUE, function)).toInt(&ok); val2=BesselJ(vali,val1); return QString::number(val2); }; if(function=="BESSELK") { // BesselK (x,n) val1=calculateVariable(getParameter(parameters, 0, TRUE, function)).toDouble(&ok); vali=calculateVariable(getParameter(parameters, 1, TRUE, function)).toInt(&ok); val2=BesselK(vali,val1); return QString::number(val2); }; if(function=="BESSELY") { // BesselY (x,n) val1=calculateVariable(getParameter(parameters, 0, TRUE, function)).toDouble(&ok); vali=calculateVariable(getParameter(parameters, 1, TRUE, function)).toInt(&ok); val2=BesselY(vali,val1); return QString::number(val2); }; if(function=="GAMMALN") { val1=calculateVariable(getParameter(parameters, 0, TRUE, function)).toDouble(&ok); val2=GammaLn(val1); return QString::number(val2); }; if(function=="ERF") { // ERF (a,b) val1=calculateVariable(getParameter(parameters, 0, TRUE, function)).toDouble(&ok); val2=calculateVariable(getParameter(parameters, 1, TRUE, function)).toDouble(&ok); return QString::number(ErrorFunction(val2)-ErrorFunction(val1)); }; if(function=="ERFC") { // ERFC (a,b) val1=calculateVariable(getParameter(parameters, 0, TRUE, function)).toDouble(&ok); val2=calculateVariable(getParameter(parameters, 1, TRUE, function)).toDouble(&ok); return QString::number(ErrorFunctionComplementary(val2)-ErrorFunctionComplementary(val1)); }; if(function=="POISSON") { // POISSON DISTR(x,n,distr/desnt) val1=calculateVariable(getParameter(parameters, 0, TRUE, function)).toDouble(&ok); val2=calculateVariable(getParameter(parameters, 1, TRUE, function)).toDouble(&ok); vali=calculateVariable(getParameter(parameters, 2, TRUE, function)).toInt(&ok); if(vali==1) { return QString::number(GammaQ(floor(val1)+1, val2)); }else { return QString::number(exp(-val2)*pow(val2,val1)/exp(GammaLn(val1+1.0))); }; }; if(function=="CHIDIST") { // POISSON CHIDIST(x,n,distr/density) val1=calculateVariable(getParameter(parameters, 0, TRUE, function)).toDouble(&ok); val2=calculateVariable(getParameter(parameters, 1, TRUE, function)).toDouble(&ok); vali=calculateVariable(getParameter(parameters, 2, TRUE, function)).toInt(&ok); if(vali==1) { return QString::number(GammaP(val2/2.0,val1*val1/2.0)); } else { return QString::number( pow(val1,val2-1.0)*exp(-val1*val1/2)/ ( pow(2,val2/2.0-1.0)*exp(GammaLn(val2/2.0))) ); }; }; if(function=="CHI2DIST") { // POISSON CHISQUAREDIST(x,n,distr/density) val1=calculateVariable(getParameter(parameters, 0, TRUE, function)).toDouble(&ok); val2=calculateVariable(getParameter(parameters, 1, TRUE, function)).toDouble(&ok); vali=calculateVariable(getParameter(parameters, 2, TRUE, function)).toInt(&ok); if(vali==1) { return QString::number(GammaP(val2/2.0,val1/2.0)); } else { return QString::number( pow(val1,val2/2.0-1.0)/(exp(val1/2.0)*pow(sqrt(2.0),val2)*exp(GammaLn(val2/2.0))) ); }; }; if(function=="BETAI") { // BETA INCOMPLETE BETA(x,a,b) val1=calculateVariable(getParameter(parameters, 0, TRUE, function)).toDouble(&ok); val2=calculateVariable(getParameter(parameters, 1, TRUE, function)).toDouble(&ok); val3=calculateVariable(getParameter(parameters, 2, TRUE, function)).toDouble(&ok); return QString::number(BetaIncomplete(val2,val3,val1)); }; if(function=="GAMMAP") { // GammaP (x,a) val1=calculateVariable(getParameter(parameters, 0, TRUE, function)).toDouble(&ok); val2=calculateVariable(getParameter(parameters, 1, TRUE, function)).toDouble(&ok); return QString::number(GammaP(val2,val1)); }; if(function=="GAMMAQ") { // GammaQ (x,a) val1=calculateVariable(getParameter(parameters, 0, TRUE, function)).toDouble(&ok); val2=calculateVariable(getParameter(parameters, 1, TRUE, function)).toDouble(&ok); return QString::number(GammaQ(val2,val1)); }; if (function=="VAR") { val1=functionVariance(getParameter(parameters, 0, TRUE, function), getParameter(parameters, 1, TRUE, function)); return QString::number(val1); }; if (function=="VARP") { val1=functionVariancePopulation(getParameter(parameters, 0, TRUE, function), getParameter(parameters, 1, TRUE, function)); return QString::number(val1); }; if (function=="STDEV") { val1=functionVariance(getParameter(parameters, 0, TRUE, function), getParameter(parameters, 1, TRUE, function)); if(val1<=0.0) return QString::number(0.0); return QString::number(sqrt(val1)); }; if (function=="STDEVP") { val1=functionVariancePopulation(getParameter(parameters, 0, TRUE, function), getParameter(parameters, 1, TRUE, function)); if(val1<=0.0) return QString::number(0.0); return QString::number(sqrt(val1)); }; if (function=="SKEW") { val1=functionSkew(getParameter(parameters, 0, TRUE, function), getParameter(parameters, 1, TRUE, function)); return QString::number(val1); }; if (function=="KURT") { val1=functionKurt(getParameter(parameters, 0, TRUE, function), getParameter(parameters, 1, TRUE, function)); return QString::number(val1); }; if(function=="GAMMADIST") { // GAMMADIST (x,alpha,beta,distribution?density1:0) val1=calculateVariable(getParameter(parameters, 0, TRUE, function)).toDouble(&ok); val2=calculateVariable(getParameter(parameters, 1, TRUE, function)).toDouble(&ok); val3=calculateVariable(getParameter(parameters, 2, TRUE, function)).toDouble(&ok); vali=calculateVariable(getParameter(parameters, 3, TRUE, function)).toInt(&ok); if(vali==1)//distribution { if(val3==0.0) return QString::number(0.0); else return QString::number(GammaP(val2,val1/val3)); }else //density { return QString::number( pow(val1,val2-1.0)*exp(-val1/val3) / (pow(val3,val2)*exp(GammaLn(val2))) ); }; }; if(function=="BETADIST") { // BETADIST (z,alpha,beta,distribution?density1:0) val1=calculateVariable(getParameter(parameters, 0, TRUE, function)).toDouble(&ok); val2=calculateVariable(getParameter(parameters, 1, TRUE, function)).toDouble(&ok); val3=calculateVariable(getParameter(parameters, 2, TRUE, function)).toDouble(&ok); vali=calculateVariable(getParameter(parameters, 3, TRUE, function)).toInt(&ok); if(vali==1)//distribution { return QString::number(BetaIncomplete(val2,val3,val1)); }else //density { return QString::number( pow(val1,val2-1.0)*pow(1.0-val1,val3-1.0) / Beta(val2,val3) ); }; }; if(function=="FDIST") { // FDIST (z,d1,d2,distribution?density1:0) val1=calculateVariable(getParameter(parameters, 0, TRUE, function)).toDouble(&ok); val2=calculateVariable(getParameter(parameters, 1, TRUE, function)).toDouble(&ok); val3=calculateVariable(getParameter(parameters, 2, TRUE, function)).toDouble(&ok); vali=calculateVariable(getParameter(parameters, 3, TRUE, function)).toInt(&ok); if(vali==1)//distribution { return QString::number( -BetaIncomplete(val3/2,val2/2,val3/(val3+val2*val1)) +BetaIncomplete(val3/2,val2/2,1) ); }else //density { return QString::number( pow(val2,val2/2)*pow(val3,val3/2)*pow(val1,val2/2-1)/ (pow(val3+val2*val1,(val2+val3)/2)*Beta(val2/2,val3/2)) ); }; }; if(function=="NORMALDIST") { // NORMALDIST (x,m,s,distribution?density1:0) val1=calculateVariable(getParameter(parameters, 0, TRUE, function)).toDouble(&ok); val2=calculateVariable(getParameter(parameters, 1, TRUE, function)).toDouble(&ok); val3=calculateVariable(getParameter(parameters, 2, TRUE, function)).toDouble(&ok); vali=calculateVariable(getParameter(parameters, 3, TRUE, function)).toInt(&ok); if(vali==1)//distribution { return QString::number( (ErrorFunction((val1-val2)/(sqrt(2)*val3))+1)/2.0 ); }else //density { return QString::number( exp(-pow(((val1-val2)/val3),2)/2)/(val3*sqrt(2*M_PI)) ); }; }; if(function=="PHI") { // NORMALDIST (x,distribution?density1:0) with mean=0 s=1.0 val1=calculateVariable(getParameter(parameters, 0, TRUE, function)).toDouble(&ok); vali=calculateVariable(getParameter(parameters, 1, TRUE, function)).toInt(&ok); if(vali==1)//distribution { return QString::number( (ErrorFunction(val1/(sqrt(2)))+1)/2.0 ); }else //density { return QString::number( exp(-pow(val1,2)/2)/(sqrt(2*M_PI)) ); }; }; /* StudentTDistribution/: PDF[StudentTDistribution[n_], x_] := 1/(Sqrt[n] Beta[n/2, 1/2]) Sqrt[n/(n+x^2)]^(n+1) /; ParameterQ[StudentTDistribution[n]] StudentTDistribution/: CDF[StudentTDistribution[n_], x_] := (1 + Sign[x] BetaRegularized[n/(n+x^2), 1, n/2, 1/2])/2 /; ParameterQ[StudentTDistribution[n]] */ return 0; }; QString Sheet::dataParserHelper(const QString &data) { if(data.left(1)=="""" && data.right(1)=="""") return QString(data); Expression exp1(data); exp1.Parse(); QStack<QString> stack1; stack1.setAutoDelete(TRUE); int i=0; QString* s1; QString* s2=NULL; int* i1; int args,tokentype; QString tempval; s1=exp1.CompiledBody.first();i1=exp1.CompiledBodyType.first(); while(i<=(int)exp1.CompiledBody.count()-1) { args= ((*i1) & 0xFF00)>>8; tokentype=(*i1) & 0x00FF; if(tokentype==NUMBER_TOKEN) { stack1.push(new QString(*s1)); //printf("Parse:Number=%s\r\n",s1->latin1()); } else if(tokentype==VARIABLE_TOKEN) { stack1.push(new QString(QString(*s1).upper())); //printf("Parse:Var=%s\r\n",s1->latin1()); //here to put implementation of other types of variables except cell. //for example names } else if(tokentype==STRING_TOKEN) { stack1.push(new QString(*s1)); //printf("Parse:String=%s\r\n",s1->ascii()); } else if(tokentype==FUNCTION_TOKEN) { QString params=""; for(int w1=1;w1<=args;w1++) { if((int)stack1.count()!=0) s2=stack1.pop(); params=*s2+params;//args in reverse order params=","+params; }; params=params.mid(1); if(params==NULL) params="0"; //printf("Parse:Func=%s, params=%s, stackcount=%d,args=%d\r\n" // ,s1->latin1(),params.latin1(),stack1.count(),args); tempval=calculateFunction(*s1,params,args); tempval=tempval; stack1.push(new QString(tempval)); }; //loops to next token if(exp1.CompiledBody.next()!=NULL) s1=exp1.CompiledBody.current(); else break; if(exp1.CompiledBodyType.next()!=NULL) i1=exp1.CompiledBodyType.current(); else break; i++; }; if((int)stack1.count()!=0)s2=stack1.pop(); else s2=new QString("!ERROR"); tempval=*s2; return(tempval); }; QString Sheet::dataParser(const QString &cell, const QString &data) { QString strippedData(data); strippedData.replace(QRegExp("\\s"), ""); if (strippedData.isEmpty() || strippedData[0]!='=') return data; if (listDataParser.find(cell)!=listDataParser.end()) return "0"; listDataParser.append(cell); // printf("DATAPARSER: data=%s, cell=%s\r\n",data.ascii(),cell.ascii()); strippedData=dataParserHelper(strippedData.remove(0, 1).replace(QRegExp(":"), ",")); int i=0; QString tempParameter(getParameter(strippedData, i)), result=""; do { result+=","+calculateVariable(tempParameter); tempParameter=getParameter(strippedData, ++i); } while (!tempParameter.isNull()); listDataParser.remove(cell); return result.mid(1); } void Sheet::setData(const QString &data) { setText(currentRow(), currentColumn(), data); slotCellChanged(currentRow(), currentColumn()); activateNextCell(); } QString Sheet::getData() { typeCellData *cellData=findCellData(currentRow(), currentColumn()); if (cellData) return cellData->data; return ""; } void Sheet::lockClicks(bool lock) { clicksLocked=lock; } void Sheet::paintCell(QPainter *p, int row, int col, const QRect & cr, bool selected) { if (selected && row==currentRow() && col==currentColumn()) selected=FALSE; int sheetDataCurrent=sheetData.at(); typeCellData *cellData=findCellData(row, col); if (sheetDataCurrent>=0) sheetData.at(sheetDataCurrent); if (!cellData) cellData=&defaultCellData; if (selected) p->fillRect(0, 0, cr.width(), cr.height(), colorGroup().highlight()); else { p->fillRect(0, 0, cr.width(), cr.height(), colorGroup().base()); p->fillRect(0, 0, cr.width(), cr.height(), cellData->background); } QTableItem *cellItem=item(row, col); if (cellItem) { p->setPen(selected ? colorGroup().highlightedText() : cellData->fontColor); p->setFont(cellData->font); QString str=cellItem->text(); p->drawText(2, 2, cr.width()-4, cr.height()-4, cellData->alignment, cellItem->text()); } int rx=cr.width()-1, ry=cr.height()-1; QPen pen(p->pen()); p->setPen(cellData->borders.right); p->drawLine(rx, 0, rx, ry); p->setPen(cellData->borders.bottom); p->drawLine(0, ry, rx, ry); p->setPen(pen); } void Sheet::viewportMousePressEvent(QMouseEvent *e) { QMouseEvent ce(e->type(), viewportToContents(e->pos()), e->globalPos(), e->button(), e->state()); if (clicksLocked) { if (selectionNo<0) { clearSelection(); QTableSelection newSelection; newSelection.init(rowAt(ce.pos().y()), columnAt(ce.pos().x())); newSelection.expandTo(newSelection.anchorRow(), newSelection.anchorCol()); selectionNo=addSelection(newSelection); } } else QTable::contentsMousePressEvent(&ce); } void Sheet::viewportMouseMoveEvent(QMouseEvent *e) { QMouseEvent ce(e->type(), viewportToContents(e->pos()), e->globalPos(), e->button(), e->state()); if (clicksLocked) { if (selectionNo>=0) { QTableSelection oldSelection(selection(selectionNo)); oldSelection.expandTo(rowAt(ce.pos().y()), columnAt(ce.pos().x())); if (!(oldSelection==selection(selectionNo))) { removeSelection(selectionNo); selectionNo=addSelection(oldSelection); } } } else QTable::contentsMouseMoveEvent(&ce); } void Sheet::viewportMouseReleaseEvent(QMouseEvent *e) { QMouseEvent ce(e->type(), viewportToContents(e->pos()), e->globalPos(), e->button(), e->state()); if (clicksLocked && selectionNo>=0) { QTableSelection oldSelection(selection(selectionNo)); oldSelection.expandTo(rowAt(ce.pos().y()), columnAt(ce.pos().x())); removeSelection(selectionNo); selectionNo=-1; if (oldSelection.topRow()==oldSelection.bottomRow() && oldSelection.leftCol()==oldSelection.rightCol()) emit cellClicked(findCellName(oldSelection.topRow(), oldSelection.leftCol())); else emit cellClicked(findCellName(oldSelection.topRow(), oldSelection.leftCol())+','+findCellName(oldSelection.bottomRow(), oldSelection.rightCol())); } else QTable::contentsMouseReleaseEvent(&ce); } QString Sheet::findCellName(int row, int col) { return (getHeaderString(col+1)+QString::number(row+1)); } void Sheet::copySheetData(QList<typeCellData> *destSheetData) { typeCellData *tempCellData, *newCellData; destSheetData->clear(); for (tempCellData=sheetData.first(); tempCellData; tempCellData=sheetData.next()) { newCellData=new typeCellData; *newCellData=*tempCellData; destSheetData->append(newCellData); } } void Sheet::setSheetData(QList<typeCellData> *srcSheetData) { typeCellData *tempCellData, *newCellData; for (tempCellData=sheetData.first(); tempCellData; tempCellData=sheetData.next()) { clearCell(tempCellData->row, tempCellData->col); updateCell(tempCellData->row, tempCellData->col); } sheetData.clear(); for (tempCellData=srcSheetData->first(); tempCellData; tempCellData=srcSheetData->next()) { newCellData=new typeCellData; *newCellData=*tempCellData; sheetData.append(newCellData); setText(newCellData->row, newCellData->col, dataParser(findCellName(newCellData->row, newCellData->col), newCellData->data)); } emit sheetModified(); } void Sheet::setName(const QString &name) { sheetName=name; emit sheetModified(); } QString Sheet::getName() { return sheetName; } void Sheet::setBrush(int row, int col, const QBrush &brush) { typeCellData *cellData=findCellData(row, col); if (!cellData) cellData=createCellData(row, col); if (cellData) { cellData->background=brush; emit sheetModified(); } } QBrush Sheet::getBrush(int row, int col) { typeCellData *cellData=findCellData(row, col); if (!cellData) cellData=&defaultCellData; return cellData->background; } void Sheet::setTextAlign(int row, int col, Qt::AlignmentFlags flags) { typeCellData *cellData=findCellData(row, col); if (!cellData) cellData=createCellData(row, col); if (cellData) { cellData->alignment=flags; emit sheetModified(); } } Qt::AlignmentFlags Sheet::getAlignment(int row, int col) { typeCellData *cellData=findCellData(row, col); if (!cellData) cellData=&defaultCellData; return cellData->alignment; } void Sheet::setTextFont(int row, int col, const QFont &font, const QColor &color) { typeCellData *cellData=findCellData(row, col); if (!cellData) cellData=createCellData(row, col); if (cellData) { cellData->font=font; cellData->fontColor=color; emit sheetModified(); } } QFont Sheet::getFont(int row, int col) { typeCellData *cellData=findCellData(row, col); if (!cellData) cellData=&defaultCellData; return cellData->font; } QColor Sheet::getFontColor(int row, int col) { typeCellData *cellData=findCellData(row, col); if (!cellData) cellData=&defaultCellData; return cellData->fontColor; } void Sheet::setPen(int row, int col, int vertical, const QPen &pen) { typeCellData *cellData=findCellData(row, col); if (!cellData) cellData=createCellData(row, col); if (cellData) { if (vertical) cellData->borders.right=pen; else cellData->borders.bottom=pen; emit sheetModified(); } } QPen Sheet::getPen(int row, int col, int vertical) { typeCellData *cellData=findCellData(row, col); if (!cellData) cellData=&defaultCellData; return (vertical ? cellData->borders.right : cellData->borders.bottom); } void Sheet::getSelection(int *row1, int *col1, int *row2, int *col2) { int selectionNo=currentSelection(); if (selectionNo>=0) { - QTableSelection selection(selection(selectionNo)); - *row1=selection.topRow(); - *row2=selection.bottomRow(); - *col1=selection.leftCol(); - *col2=selection.rightCol(); + QTableSelection select(selection(selectionNo)); + *row1=select.topRow(); + *row2=select.bottomRow(); + *col1=select.leftCol(); + *col2=select.rightCol(); } else { *row1=*row2=currentRow(); *col1=*col2=currentColumn(); } } void Sheet::editClear() { int row1, row2, col1, col2; getSelection(&row1, &col1, &row2, &col2); int row, col; for (row=row1; row<=row2; ++row) for (col=col1; col<=col2; ++col) { setText(row, col, ""); slotCellChanged(row, col); } } void Sheet::editCopy() { clipboardData.clear(); int row1, row2, col1, col2; getSelection(&row1, &col1, &row2, &col2); typeCellData *cellData, *newCellData; int row, col; for (row=row1; row<=row2; ++row) for (col=col1; col<=col2; ++col) { cellData=findCellData(row, col); if (cellData) { newCellData=new typeCellData; *newCellData=*cellData; newCellData->row-=row1; newCellData->col-=col1; clipboardData.append(newCellData); } } } void Sheet::editCut() { editCopy(); editClear(); } void Sheet::editPaste(bool onlyContents) { int row1=currentRow(), col1=currentColumn(); typeCellData *cellData, *tempCellData; for (tempCellData=clipboardData.first(); tempCellData; tempCellData=clipboardData.next()) { cellData=findCellData(tempCellData->row+row1, tempCellData->col+col1); if (!cellData) cellData=createCellData(tempCellData->row+row1, tempCellData->col+col1); if (cellData) { if (onlyContents) cellData->data=tempCellData->data; else { *cellData=*tempCellData; cellData->row+=row1; cellData->col+=col1; } setText(cellData->row, cellData->col, dataParser(findCellName(cellData->row, cellData->col), cellData->data)); emit sheetModified(); } } } void Sheet::insertRows(int no, bool allColumns) { setNumRows(numRows()+no); typeCellData *tempCellData; int row=currentRow(), col=currentColumn(); for (tempCellData=sheetData.first(); tempCellData; tempCellData=sheetData.next()) if (tempCellData->row>=row && (allColumns || tempCellData->col==col)) { clearCell(tempCellData->row, tempCellData->col); tempCellData->row+=no; } for (tempCellData=sheetData.first(); tempCellData; tempCellData=sheetData.next()) if (tempCellData->row>=row && (allColumns || tempCellData->col==col)) { updateCell(tempCellData->row-no, tempCellData->col); setText(tempCellData->row, tempCellData->col, dataParser(findCellName(tempCellData->row, tempCellData->col), tempCellData->data)); } emit sheetModified(); } void Sheet::insertColumns(int no, bool allRows) { int noCols=numCols(); int newCols=noCols+no; setNumCols(newCols); for (int i=noCols; i<newCols; ++i) horizontalHeader()->setLabel(i, getHeaderString(i+1), DEFAULT_COL_WIDTH); typeCellData *tempCellData; int col=currentColumn(), row=currentRow(); for (tempCellData=sheetData.first(); tempCellData; tempCellData=sheetData.next()) if (tempCellData->col>=col && (allRows || tempCellData->row==row)) { clearCell(tempCellData->row, tempCellData->col); tempCellData->col+=no; } for (tempCellData=sheetData.first(); tempCellData; tempCellData=sheetData.next()) if (tempCellData->col>=col && (allRows || tempCellData->row==row)) { updateCell(tempCellData->row, tempCellData->col-no); setText(tempCellData->row, tempCellData->col, dataParser(findCellName(tempCellData->row, tempCellData->col), tempCellData->data)); } emit sheetModified(); } void Sheet::dataFindReplace(const QString &findStr, const QString &replaceStr, bool matchCase, bool allCells, bool entireCell, bool replace, bool replaceAll) { typeCellData *tempCellData; int row1, col1, row2, col2; getSelection(&row1, &col1, &row2, &col2); bool found=FALSE; for (tempCellData=sheetData.first(); tempCellData; tempCellData=sheetData.next()) if (allCells || (tempCellData->row>=row1 && tempCellData->row<=row2 && tempCellData->col>=col1 && tempCellData->col<=col2)) { QTableItem *cellItem=item(tempCellData->row, tempCellData->col); if (cellItem && (entireCell ? (matchCase ? cellItem->text()==findStr : cellItem->text().upper()==findStr.upper()) : cellItem->text().find(findStr, 0, matchCase)>=0)) { if (!found) { found=TRUE; clearSelection(); } setCurrentCell(tempCellData->row, tempCellData->col); if (replace) { tempCellData->data=cellItem->text().replace(QRegExp(findStr, matchCase), replaceStr); setText(tempCellData->row, tempCellData->col, dataParser(findCellName(tempCellData->row, tempCellData->col), tempCellData->data)); } if (!replace || !replaceAll) break; } } if (found) { if (replace) slotCellChanged(currentRow(), currentColumn()); } else QMessageBox::warning(this, tr("Error"), tr("Search key not found!")); } // // Static functions // QString Sheet::getHeaderString(int section) { if (section<1) return ""; return getHeaderString((section-1)/26)+QChar('A'+(section-1)%26); } int Sheet::getHeaderColumn(const QString §ion) { if (section.isEmpty()) return 0; return (section[section.length()-1]-'A'+1)+getHeaderColumn(section.left(section.length()-1))*26; } //Expression Parser Class Definition QChar Expression::chunk0(void) { if(chunk.length()>0) return(chunk[0]); else return('\0'); }; Expression::Expression(QString expr1)// constructor { Body=expr1; SYMBOL="+-*/%^=()<>&|!,"; MATHSYMBOL="+-*/%^=<>&|!,"; // lnlim=1.0e-36; // Smallest number allowed // loglim=1.0e-10 ; // Smallest number allowed in call to log10() * ErrorFound=TRUE; n=0;chunk="";SymbGroup=NONE_TOKEN;InExpr=Body; ArgsOfFunc=0; CompiledBody.setAutoDelete(TRUE); CompiledBodyType.setAutoDelete(TRUE); //CompiledBody=QStringList(0); }; bool Expression::isSymbol(QChar ch) { int j = 0; while (j<=((int)SYMBOL.length()-1) && ch!=SYMBOL[j]) j++; if(j<((int)SYMBOL.length())) return true; else return false; }; bool Expression::isMathSymbol(QChar ch) { int j = 0; while (j<=((int)MATHSYMBOL.length()-1) && ch!=MATHSYMBOL[j]) j++; if(j<((int)MATHSYMBOL.length())) return true; else return false; }; void Expression::GetNext() { chunk=""; if(n>=(int)InExpr.length()) return; while (InExpr[n]==' ') n++; if(InExpr[n]=='\"') { while ( (n<(int)InExpr.length()) && (InExpr[n+1]!='\"') ) { printf("chunk=%s\r\n",chunk.latin1()); chunk+=InExpr[n]; n++; }; chunk+=InExpr[n]; printf("2\r\n"); SymbGroup=STRING_TOKEN; } else if (isSymbol(InExpr[n])) { SymbGroup=SYMBOL_TOKEN; chunk+=InExpr[n]; n++; if( (n<(int)InExpr.length()) && isMathSymbol(InExpr[n-1]) && isMathSymbol(InExpr[n]) ) { SymbGroup=SYMBOL_TOKEN; chunk+=InExpr[n]; n++; }; } else if ((InExpr[n].isLetter())||(InExpr[n]=='#')) { while ( (n<(int)InExpr.length()) && !isSymbol(InExpr[n]) ) { if (!(InExpr[n]==' ')) chunk+=InExpr[n]; n++; }; if (InExpr[n]=='(') SymbGroup=FUNCTION_TOKEN; // function TOKEN else SymbGroup=VARIABLE_TOKEN; } else if((n<(int)InExpr.length()) && ((InExpr[n].isDigit()) || (InExpr[n]=='.'))) { while( n<(int)InExpr.length() ) { if((InExpr[n].isDigit()) || InExpr[n]=='.') { chunk+=InExpr[n]; SymbGroup=NUMBER_TOKEN; n++; } else if(InExpr[n]=='e') { if((n+1)<(int)InExpr.length()) { if(InExpr[n+1]=='-' || InExpr[n+1]=='+' || InExpr[n+1].isDigit()) { chunk+=InExpr[n]; chunk+=InExpr[n+1]; SymbGroup=NUMBER_TOKEN; n+=2; } } else { break; } } else { break; } }//while }//else if };//end function void Expression::First() { GetNext(); if (!(chunk=="") && !ErrorFound) Third(); else ErrorFound = true; }; void Expression::Third() { QChar sign, secS='\0'; Fourth(); sign = chunk0(); if((int)chunk.length()>1) secS=chunk[1]; while( sign == '+' || sign == '-'|| sign == '<' || sign == '>'|| sign == '%'|| sign == '&' || sign == '|' || sign == '!' || sign == '=' ) { GetNext(); Fourth(); QString name; if( sign == '+' ) name= "+" ; else if(sign=='-') name= "-" ; else if(sign=='>' && secS=='\0') name= ">" ; else if(sign=='<' && secS=='\0') name= "<" ; else if(sign=='=' && secS=='=') name= "==" ; else if(sign=='!' && secS=='=') name= "!=" ; else if(sign=='>' && secS=='=') name= ">=" ; else if(sign=='<' && secS=='=') name= "<=" ; else if(sign=='&' && secS=='&') name= "AND" ; else if(sign=='|' && secS=='|') name= "OR" ; else if(sign=='%') name= "MOD" ; CompiledBody.append(new QString(name)); // not sure if pushed in the back. CompiledBodyType.append(new int(FUNCTION_TOKEN | 2<<8)); //2 argument functions sign = chunk0(); } }; void Expression::Fourth() { QChar sign; Fifth(); sign = chunk0(); while( sign == '*' || sign == '/' ) { GetNext(); Fifth(); QString name; if( sign == '*' ) name= "*" ; else name= "/" ; CompiledBody.append(new QString(name)); CompiledBodyType.append(new int(FUNCTION_TOKEN | 2<<8)); //2 arguments functions sign = chunk0(); } }; void Expression::Fifth() { Sixth(); //if(chunk.Length==0) return; if( chunk0() == '^' ) { GetNext(); Fifth(); CompiledBody.append(new QString("POWER")); CompiledBodyType.append(new int(FUNCTION_TOKEN | 2<<8)); // 2 argument functions } }; void Expression::Sixth() { char sign; sign = ' '; if(SymbGroup== SYMBOL_TOKEN && chunk0() == '+' || chunk0() == '-' | chunk0() == '!') { sign = chunk0(); GetNext(); } Seventh(); if( sign == '-' ) { CompiledBody.append(new QString("CHGSGN")); // unary minus CompiledBodyType.append(new int(FUNCTION_TOKEN | 1<<8)); //1 argument } if( sign == '!' ) { CompiledBody.append(new QString("NOT")); // unary minus CompiledBodyType.append(new int(FUNCTION_TOKEN | 1<<8)); //1 argument } }; void Expression::Seventh() { if( chunk0() == '(' && SymbGroup==SYMBOL_TOKEN) { GetNext(); Third(); //parse the insides until we get a ')' if (chunk0() != ')') ErrorFound = true; GetNext(); } else Eighth(); }; void Expression::Eighth() { if ( SymbGroup== NUMBER_TOKEN ) { CompiledBody.append(new QString(chunk)); CompiledBodyType.append(new int(NUMBER_TOKEN)); GetNext(); } else if ( SymbGroup== VARIABLE_TOKEN ) { CompiledBody.append(new QString(chunk)); CompiledBodyType.append(new int(VARIABLE_TOKEN)); GetNext(); } else if (SymbGroup== STRING_TOKEN ) { CompiledBody.append(new QString(chunk+QString("\""))); CompiledBodyType.append(new int(STRING_TOKEN)); GetNext(); } else Ninth(); }; void Expression::Ninth() { if ( SymbGroup== FUNCTION_TOKEN ) { QString TempFunk = chunk ; GetNext(); if(chunk0() == '(' ) { FuncDepth++; ArgsOfFunc.resize(FuncDepth+1); ArgsOfFunc[FuncDepth]=1; //ArgsOfFunc=1; GetNext(); Third(); while(chunk0()==',') //function arguments separator { //ArgsOfFunc++; ArgsOfFunc[FuncDepth]++; GetNext(); Third(); }; if (chunk0() != ')') ErrorFound = true; CompiledBody.append(new QString(TempFunk)); if (TempFunk=="PI") ArgsOfFunc[FuncDepth]=0; // couldn't find a better way to parse function PI() with 0 args. :) CompiledBodyType.append(new int(FUNCTION_TOKEN | (ArgsOfFunc[FuncDepth]<<8) )); //the mask &FF00 gives the arguments of the functions passed. FuncDepth--; ArgsOfFunc.resize(FuncDepth+1); GetNext(); } } else ErrorFound = true; }; bool Expression::Parse() { CompiledBody.clear(); ErrorFound = false; n = 0;ArgsOfFunc.resize(0);FuncDepth=0; InExpr=Body; First(); return(!ErrorFound); }; |