summaryrefslogtreecommitdiff
path: root/noncore/apps/opie-sheet/sheet.cpp
Unidiff
Diffstat (limited to 'noncore/apps/opie-sheet/sheet.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r--noncore/apps/opie-sheet/sheet.cpp4
1 files changed, 2 insertions, 2 deletions
diff --git a/noncore/apps/opie-sheet/sheet.cpp b/noncore/apps/opie-sheet/sheet.cpp
index f303d33..d4419af 100644
--- a/noncore/apps/opie-sheet/sheet.cpp
+++ b/noncore/apps/opie-sheet/sheet.cpp
@@ -1,239 +1,239 @@
1/*************************************************************************** 1/***************************************************************************
2 * * 2 * *
3 * This program is free software; you can redistribute it and/or modify * 3 * This program is free software; you can redistribute it and/or modify *
4 * it under the terms of the GNU General Public License as published by * 4 * it under the terms of the GNU General Public License as published by *
5 * the Free Software Foundation; either version 2 of the License, or * 5 * the Free Software Foundation; either version 2 of the License, or *
6 * (at your option) any later version. * 6 * (at your option) any later version. *
7 * * 7 * *
8 ***************************************************************************/ 8 ***************************************************************************/
9 9
10/* 10/*
11 * Opie Sheet (formerly Sheet/Qt) 11 * Opie Sheet (formerly Sheet/Qt)
12 * by Serdar Ozler <sozler@sitebest.com> 12 * by Serdar Ozler <sozler@sitebest.com>
13 */ 13 */
14 14
15#include "sheet.h" 15#include "sheet.h"
16 16
17#include <qmessagebox.h> 17#include <qmessagebox.h>
18#include <math.h> 18#include <math.h>
19#include <stdlib.h> 19#include <stdlib.h>
20#include <stdio.h> 20#include <stdio.h>
21#include <time.h> 21#include <time.h>
22 22
23#define DEFAULT_COL_WIDTH 50 23#define DEFAULT_COL_WIDTH 50
24 24
25Sheet::Sheet(int numRows, int numCols, QWidget *parent) 25Sheet::Sheet(int numRows, int numCols, QWidget *parent)
26 :QTable(numRows, numCols, parent) 26 :QTable(numRows, numCols, parent)
27{ 27{
28 defaultBorders.right=defaultBorders.bottom=QPen(Qt::gray, 1, Qt::SolidLine); 28 defaultBorders.right=defaultBorders.bottom=QPen(Qt::gray, 1, Qt::SolidLine);
29 defaultCellData.data=""; 29 defaultCellData.data="";
30 defaultCellData.background=QBrush(Qt::white, Qt::SolidPattern); 30 defaultCellData.background=QBrush(Qt::white, Qt::SolidPattern);
31 defaultCellData.alignment=(Qt::AlignmentFlags)(Qt::AlignLeft | Qt::AlignTop); 31 defaultCellData.alignment=(Qt::AlignmentFlags)(Qt::AlignLeft | Qt::AlignTop);
32 defaultCellData.fontColor=Qt::black; 32 defaultCellData.fontColor=Qt::black;
33 defaultCellData.font=font(); 33 defaultCellData.font=font();
34 defaultCellData.borders=defaultBorders; 34 defaultCellData.borders=defaultBorders;
35 35
36 clicksLocked=FALSE; 36 clicksLocked=FALSE;
37 selectionNo=-1; 37 selectionNo=-1;
38 setSelectionMode(QTable::Single); 38 setSelectionMode(QTable::Single);
39 39
40 sheetData.setAutoDelete(TRUE); 40 sheetData.setAutoDelete(TRUE);
41 clipboardData.setAutoDelete(TRUE); 41 clipboardData.setAutoDelete(TRUE);
42 for (int i=0; i<numCols; ++i) 42 for (int i=0; i<numCols; ++i)
43 horizontalHeader()->setLabel(i, getHeaderString(i+1), DEFAULT_COL_WIDTH); 43 horizontalHeader()->setLabel(i, getHeaderString(i+1), DEFAULT_COL_WIDTH);
44 44
45 45
46 connect(this, SIGNAL(currentChanged(int, int)), this, SLOT(slotCellSelected(int, int))); 46 connect(this, SIGNAL(currentChanged(int,int)), this, SLOT(slotCellSelected(int,int)));
47 connect(this, SIGNAL(valueChanged(int, int)), this, SLOT(slotCellChanged(int, int))); 47 connect(this, SIGNAL(valueChanged(int,int)), this, SLOT(slotCellChanged(int,int)));
48} 48}
49 49
50Sheet::~Sheet() 50Sheet::~Sheet()
51{ 51{
52} 52}
53 53
54typeCellData *Sheet::findCellData(int row, int col) 54typeCellData *Sheet::findCellData(int row, int col)
55{ 55{
56 typeCellData *tempCellData; 56 typeCellData *tempCellData;
57 for (tempCellData=sheetData.first(); tempCellData; tempCellData=sheetData.next()) 57 for (tempCellData=sheetData.first(); tempCellData; tempCellData=sheetData.next())
58 { 58 {
59 if (tempCellData->row==row && tempCellData->col==col) return tempCellData; 59 if (tempCellData->row==row && tempCellData->col==col) return tempCellData;
60 } 60 }
61 return NULL; 61 return NULL;
62} 62}
63 63
64void Sheet::slotCellSelected(int row, int col) 64void Sheet::slotCellSelected(int row, int col)
65{ 65{
66 typeCellData *cellData=findCellData(row, col); 66 typeCellData *cellData=findCellData(row, col);
67 if (cellData) 67 if (cellData)
68 { 68 {
69 emit currentDataChanged(cellData->data); 69 emit currentDataChanged(cellData->data);
70 }else 70 }else
71 emit currentDataChanged(""); 71 emit currentDataChanged("");
72} 72}
73 73
74typeCellData *Sheet::createCellData(int row, int col) 74typeCellData *Sheet::createCellData(int row, int col)
75{ 75{
76 if (row<0 || col<0) return NULL; 76 if (row<0 || col<0) return NULL;
77 typeCellData *cellData=new typeCellData; 77 typeCellData *cellData=new typeCellData;
78 cellData->row=row; 78 cellData->row=row;
79 cellData->col=col; 79 cellData->col=col;
80 cellData->data=defaultCellData.data; 80 cellData->data=defaultCellData.data;
81 cellData->borders=defaultCellData.borders; 81 cellData->borders=defaultCellData.borders;
82 cellData->alignment=defaultCellData.alignment; 82 cellData->alignment=defaultCellData.alignment;
83 cellData->font=defaultCellData.font; 83 cellData->font=defaultCellData.font;
84 cellData->fontColor=defaultCellData.fontColor; 84 cellData->fontColor=defaultCellData.fontColor;
85 cellData->background=defaultCellData.background; 85 cellData->background=defaultCellData.background;
86 sheetData.append(cellData); 86 sheetData.append(cellData);
87 return cellData; 87 return cellData;
88} 88}
89 89
90void Sheet::slotCellChanged(int row, int col) 90void Sheet::slotCellChanged(int row, int col)
91{ 91{
92 typeCellData *cellData=findCellData(row, col); 92 typeCellData *cellData=findCellData(row, col);
93 if (!cellData) cellData=createCellData(row, col); 93 if (!cellData) cellData=createCellData(row, col);
94 if (cellData) cellData->data=text(row, col); 94 if (cellData) cellData->data=text(row, col);
95 for (cellData=sheetData.first(); cellData; cellData=sheetData.next()) 95 for (cellData=sheetData.first(); cellData; cellData=sheetData.next())
96 { 96 {
97 // modified by Toussis Manolis koppermind@panafonet.gr 97 // modified by Toussis Manolis koppermind@panafonet.gr
98 // the parser was crashing if there were no closed parenthesis. 98 // the parser was crashing if there were no closed parenthesis.
99 int w1,ii=0; 99 int w1,ii=0;
100 for(w1=0;w1<=(int)text(row, col).length();w1++) 100 for(w1=0;w1<=(int)text(row, col).length();w1++)
101 { 101 {
102 if(text(row,col)[w1]=='(') ii++; 102 if(text(row,col)[w1]=='(') ii++;
103 if(text(row,col)[w1]==')') ii--; 103 if(text(row,col)[w1]==')') ii--;
104 }; 104 };
105 if(ii==0) setText(cellData->row, cellData->col, dataParser(findCellName(cellData->row, cellData->col), cellData->data)); 105 if(ii==0) setText(cellData->row, cellData->col, dataParser(findCellName(cellData->row, cellData->col), cellData->data));
106 //end of modification 106 //end of modification
107 // old was plain: 107 // old was plain:
108 //setText(cellData->row, cellData->col, dataParser(findCellName(cellData->row, cellData->col), cellData->data)); 108 //setText(cellData->row, cellData->col, dataParser(findCellName(cellData->row, cellData->col), cellData->data));
109 }; 109 };
110 emit sheetModified(); 110 emit sheetModified();
111} 111}
112 112
113 113
114void Sheet::ReCalc(void) 114void Sheet::ReCalc(void)
115{ 115{
116 typeCellData* cellData; 116 typeCellData* cellData;
117 for (cellData=sheetData.first(); cellData; cellData=sheetData.next()) 117 for (cellData=sheetData.first(); cellData; cellData=sheetData.next())
118 { 118 {
119 //printf("cellchanged:%d, %d\r\n",cellData->row,cellData->col); 119 //printf("cellchanged:%d, %d\r\n",cellData->row,cellData->col);
120 120
121 slotCellChanged(cellData->row,cellData->col); 121 slotCellChanged(cellData->row,cellData->col);
122 }; 122 };
123}; 123};
124 124
125 125
126 126
127 127
128void Sheet::swapCells(int row1, int col1, int row2, int col2) 128void Sheet::swapCells(int row1, int col1, int row2, int col2)
129{ 129{
130 typeCellData *cellData1=findCellData(row1, col1), *cellData2=findCellData(row2, col2); 130 typeCellData *cellData1=findCellData(row1, col1), *cellData2=findCellData(row2, col2);
131 if (!cellData1) cellData1=createCellData(row1, col1); 131 if (!cellData1) cellData1=createCellData(row1, col1);
132 if (!cellData2) cellData2=createCellData(row2, col2); 132 if (!cellData2) cellData2=createCellData(row2, col2);
133 if (cellData1 && cellData2) 133 if (cellData1 && cellData2)
134 { 134 {
135 QString tempData(cellData1->data); 135 QString tempData(cellData1->data);
136 cellData1->data=cellData2->data; 136 cellData1->data=cellData2->data;
137 cellData2->data=tempData; 137 cellData2->data=tempData;
138 setText(cellData1->row, cellData1->col, dataParser(findCellName(cellData1->row, cellData1->col), cellData1->data)); 138 setText(cellData1->row, cellData1->col, dataParser(findCellName(cellData1->row, cellData1->col), cellData1->data));
139 setText(cellData2->row, cellData2->col, dataParser(findCellName(cellData2->row, cellData2->col), cellData2->data)); 139 setText(cellData2->row, cellData2->col, dataParser(findCellName(cellData2->row, cellData2->col), cellData2->data));
140 emit sheetModified(); 140 emit sheetModified();
141 } 141 }
142} 142}
143 143
144QString Sheet::getParameter(const QString &parameters, int paramNo, bool giveError, const QString funcName) 144QString Sheet::getParameter(const QString &parameters, int paramNo, bool giveError, const QString funcName)
145{ 145{
146 QString params(parameters); 146 QString params(parameters);
147 int position; 147 int position;
148 for (int i=0; i<paramNo; ++i) 148 for (int i=0; i<paramNo; ++i)
149 { 149 {
150 position=params.find(','); 150 position=params.find(',');
151 if (position<0) 151 if (position<0)
152 { 152 {
153 if (giveError) QMessageBox::critical(this, tr("Error"), tr("Too few arguments to function '"+funcName+'\'')); 153 if (giveError) QMessageBox::critical(this, tr("Error"), tr("Too few arguments to function '"+funcName+'\''));
154 //printf("params:%s\r\n",parameters.ascii()); 154 //printf("params:%s\r\n",parameters.ascii());
155 return QString(NULL); 155 return QString(NULL);
156 } 156 }
157 params=params.mid(position+1); 157 params=params.mid(position+1);
158 } 158 }
159 position=params.find(','); 159 position=params.find(',');
160 if (position<0) return params; 160 if (position<0) return params;
161 return params.left(position); 161 return params.left(position);
162} 162}
163 163
164bool Sheet::findRange(const QString &variable1, const QString &variable2, int *row1, int *col1, int *row2, int *col2) 164bool Sheet::findRange(const QString &variable1, const QString &variable2, int *row1, int *col1, int *row2, int *col2)
165{ 165{
166 int row, col; 166 int row, col;
167 if (!findRowColumn(variable1, row1, col1, FALSE) || !findRowColumn(variable2, row2, col2, FALSE)) return FALSE; 167 if (!findRowColumn(variable1, row1, col1, FALSE) || !findRowColumn(variable2, row2, col2, FALSE)) return FALSE;
168 if (*row1>*row2) 168 if (*row1>*row2)
169 { 169 {
170 row=*row1; 170 row=*row1;
171 *row1=*row2; 171 *row1=*row2;
172 *row2=row; 172 *row2=row;
173 } 173 }
174 if (*col1>*col2) 174 if (*col1>*col2)
175 { 175 {
176 col=*col1; 176 col=*col1;
177 *col1=*col2; 177 *col1=*col2;
178 *col2=col; 178 *col2=col;
179 } 179 }
180 return TRUE; 180 return TRUE;
181} 181}
182 182
183bool Sheet::findRowColumn(const QString &variable, int *row, int *col, bool giveError) 183bool Sheet::findRowColumn(const QString &variable, int *row, int *col, bool giveError)
184{ 184{
185 int position=variable.find(QRegExp("\\d")); 185 int position=variable.find(QRegExp("\\d"));
186 if (position<1) 186 if (position<1)
187 { 187 {
188 if (giveError) QMessageBox::critical(this, tr("Error"), tr("Invalid variable: '"+variable+'\'')); 188 if (giveError) QMessageBox::critical(this, tr("Error"), tr("Invalid variable: '"+variable+'\''));
189 return FALSE; 189 return FALSE;
190 } 190 }
191 *row=variable.mid(position).toInt()-1; 191 *row=variable.mid(position).toInt()-1;
192 *col=getHeaderColumn(variable.left(position))-1; 192 *col=getHeaderColumn(variable.left(position))-1;
193 return TRUE; 193 return TRUE;
194} 194}
195 195
196QString Sheet::calculateVariable(const QString &variable) 196QString Sheet::calculateVariable(const QString &variable)
197{ 197{
198 bool ok; 198 bool ok;
199 printf("calculateVariable=%s,len=%d\r\n",variable.ascii(),variable.length()); 199 printf("calculateVariable=%s,len=%d\r\n",variable.ascii(),variable.length());
200 if(variable.left(1)=="\"") return QString(variable.mid(1,variable.length()-2)); 200 if(variable.left(1)=="\"") return QString(variable.mid(1,variable.length()-2));
201 double tempResult=variable.toDouble(&ok); 201 double tempResult=variable.toDouble(&ok);
202 if (ok) 202 if (ok)
203 { 203 {
204 if(tempResult!=0.0) 204 if(tempResult!=0.0)
205 { 205 {
206 return QString::number(tempResult); 206 return QString::number(tempResult);
207 } 207 }
208 else 208 else
209 { 209 {
210 if(variable!="0" || variable!="0.0") return QString(variable); // hereis a string variable 210 if(variable!="0" || variable!="0.0") return QString(variable); // hereis a string variable
211 return QString::number(tempResult); 211 return QString::number(tempResult);
212 }; 212 };
213 }; 213 };
214 214
215 int row, col; 215 int row, col;
216 if(findRowColumn(variable, &row, &col, FALSE)) return dataParser(variable, text(row,col)); 216 if(findRowColumn(variable, &row, &col, FALSE)) return dataParser(variable, text(row,col));
217 //return (findRowColumn(variable, &row, &col, TRUE) ? dataParser(variable, text(row, col)) : 0); 217 //return (findRowColumn(variable, &row, &col, TRUE) ? dataParser(variable, text(row, col)) : 0);
218 return QString(variable); 218 return QString(variable);
219} 219}
220 220
221double Sheet::BesselI0(double x) 221double Sheet::BesselI0(double x)
222{ 222{
223 //Returns the modi ed Bessel function I0(x) for any real x. 223 //Returns the modi ed Bessel function I0(x) for any real x.
224 double ax,ans; 224 double ax,ans;
225 double y; 225 double y;
226 if ((ax=fabs(x)) < 3.75) 226 if ((ax=fabs(x)) < 3.75)
227 { 227 {
228 y=x/3.75; 228 y=x/3.75;
229 y*=y; 229 y*=y;
230 ans=1.0+y*(3.5156229+y*(3.0899424+y*(1.2067492 +y*(0.2659732+y*(0.360768e-1+y*0.45813e-2))))); 230 ans=1.0+y*(3.5156229+y*(3.0899424+y*(1.2067492 +y*(0.2659732+y*(0.360768e-1+y*0.45813e-2)))));
231 }else 231 }else
232 { 232 {
233 y=3.75/ax; 233 y=3.75/ax;
234 ans=(exp(ax)/sqrt(ax))*(0.39894228+y*(0.1328592e-1 +y*(0.225319e-2+y*(-0.157565e-2+y*(0.916281e-2 +y*(-0.2057706e-1+y*(0.2635537e-1+y*(-0.1647633e-1 +y*0.392377e-2)))))))); 234 ans=(exp(ax)/sqrt(ax))*(0.39894228+y*(0.1328592e-1 +y*(0.225319e-2+y*(-0.157565e-2+y*(0.916281e-2 +y*(-0.2057706e-1+y*(0.2635537e-1+y*(-0.1647633e-1 +y*0.392377e-2))))))));
235 } 235 }
236 return ans; 236 return ans;
237}; 237};
238 238
239double Sheet::BesselI1(double x) 239double Sheet::BesselI1(double x)