summaryrefslogtreecommitdiff
Unidiff
Diffstat (more/less context) (ignore whitespace changes)
-rw-r--r--noncore/apps/opie-sheet/ChangeLog4
-rw-r--r--noncore/apps/opie-sheet/sheet.cpp1
2 files changed, 5 insertions, 0 deletions
diff --git a/noncore/apps/opie-sheet/ChangeLog b/noncore/apps/opie-sheet/ChangeLog
index d3c5c84..c12a6db 100644
--- a/noncore/apps/opie-sheet/ChangeLog
+++ b/noncore/apps/opie-sheet/ChangeLog
@@ -1,21 +1,25 @@
1October 05, 2002
2
3 - Fixed bug #252. (by thufir)
4
1August 31, 2002 5August 31, 2002
2 6
3 - Fixed and updated opie-sheet.pro file. (by cniehaus) 7 - Fixed and updated opie-sheet.pro file. (by cniehaus)
4 8
5August 01, 2002 9August 01, 2002
6 10
7 - Fixed gcc3 compile problems. (by harlekin) 11 - Fixed gcc3 compile problems. (by harlekin)
8 12
9July 06, 2002 13July 06, 2002
10 14
11 - Fixed DocLnk management to save documents correctly. (by leseb) 15 - Fixed DocLnk management to save documents correctly. (by leseb)
12 16
13July 04, 2002 17July 04, 2002
14 18
15 * Release 1.0.1 (by thufir) 19 * Release 1.0.1 (by thufir)
16 - Sheet/Qt is now a part of Opie, so its name is changed to Opie Sheet. (by thufir) 20 - Sheet/Qt is now a part of Opie, so its name is changed to Opie Sheet. (by thufir)
17 - Fixed sorting bug. (by thufir) 21 - Fixed sorting bug. (by thufir)
18 22
19April 14, 2002 23April 14, 2002
20 24
21 * Release 1.0.0 (by thufir) 25 * Release 1.0.0 (by thufir)
diff --git a/noncore/apps/opie-sheet/sheet.cpp b/noncore/apps/opie-sheet/sheet.cpp
index 2279191..be4046b 100644
--- a/noncore/apps/opie-sheet/sheet.cpp
+++ b/noncore/apps/opie-sheet/sheet.cpp
@@ -1,545 +1,546 @@
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 <qmainwindow.h> 17#include <qmainwindow.h>
18#include <qmessagebox.h> 18#include <qmessagebox.h>
19#include <math.h> 19#include <math.h>
20 20
21#define DEFAULT_COL_WIDTH 50 21#define DEFAULT_COL_WIDTH 50
22 22
23Sheet::Sheet(int numRows, int numCols, QWidget *parent) 23Sheet::Sheet(int numRows, int numCols, QWidget *parent)
24 :QTable(numRows, numCols, parent) 24 :QTable(numRows, numCols, parent)
25{ 25{
26 defaultBorders.right=defaultBorders.bottom=QPen(Qt::gray, 1, Qt::SolidLine); 26 defaultBorders.right=defaultBorders.bottom=QPen(Qt::gray, 1, Qt::SolidLine);
27 defaultCellData.data=""; 27 defaultCellData.data="";
28 defaultCellData.background=QBrush(Qt::white, Qt::SolidPattern); 28 defaultCellData.background=QBrush(Qt::white, Qt::SolidPattern);
29 defaultCellData.alignment=(Qt::AlignmentFlags)(Qt::AlignLeft | Qt::AlignTop); 29 defaultCellData.alignment=(Qt::AlignmentFlags)(Qt::AlignLeft | Qt::AlignTop);
30 defaultCellData.fontColor=Qt::black; 30 defaultCellData.fontColor=Qt::black;
31 defaultCellData.font=font(); 31 defaultCellData.font=font();
32 defaultCellData.borders=defaultBorders; 32 defaultCellData.borders=defaultBorders;
33 33
34 clicksLocked=FALSE;
34 selectionNo=-1; 35 selectionNo=-1;
35 setSelectionMode(QTable::Single); 36 setSelectionMode(QTable::Single);
36 37
37 sheetData.setAutoDelete(TRUE); 38 sheetData.setAutoDelete(TRUE);
38 clipboardData.setAutoDelete(TRUE); 39 clipboardData.setAutoDelete(TRUE);
39 for (int i=0; i<numCols; ++i) 40 for (int i=0; i<numCols; ++i)
40 horizontalHeader()->setLabel(i, getHeaderString(i+1), DEFAULT_COL_WIDTH); 41 horizontalHeader()->setLabel(i, getHeaderString(i+1), DEFAULT_COL_WIDTH);
41 42
42 connect(this, SIGNAL(currentChanged(int, int)), this, SLOT(slotCellSelected(int, int))); 43 connect(this, SIGNAL(currentChanged(int, int)), this, SLOT(slotCellSelected(int, int)));
43 connect(this, SIGNAL(valueChanged(int, int)), this, SLOT(slotCellChanged(int, int))); 44 connect(this, SIGNAL(valueChanged(int, int)), this, SLOT(slotCellChanged(int, int)));
44} 45}
45 46
46Sheet::~Sheet() 47Sheet::~Sheet()
47{ 48{
48} 49}
49 50
50typeCellData *Sheet::findCellData(int row, int col) 51typeCellData *Sheet::findCellData(int row, int col)
51{ 52{
52 typeCellData *tempCellData; 53 typeCellData *tempCellData;
53 for (tempCellData=sheetData.first(); tempCellData; tempCellData=sheetData.next()) 54 for (tempCellData=sheetData.first(); tempCellData; tempCellData=sheetData.next())
54 if (tempCellData->row==row && tempCellData->col==col) 55 if (tempCellData->row==row && tempCellData->col==col)
55 return tempCellData; 56 return tempCellData;
56 return NULL; 57 return NULL;
57} 58}
58 59
59void Sheet::slotCellSelected(int row, int col) 60void Sheet::slotCellSelected(int row, int col)
60{ 61{
61 typeCellData *cellData=findCellData(row, col); 62 typeCellData *cellData=findCellData(row, col);
62 if (cellData) 63 if (cellData)
63 emit currentDataChanged(cellData->data); 64 emit currentDataChanged(cellData->data);
64 else 65 else
65 emit currentDataChanged(""); 66 emit currentDataChanged("");
66} 67}
67 68
68typeCellData *Sheet::createCellData(int row, int col) 69typeCellData *Sheet::createCellData(int row, int col)
69{ 70{
70 if (row<0 || col<0) return NULL; 71 if (row<0 || col<0) return NULL;
71 typeCellData *cellData=new typeCellData; 72 typeCellData *cellData=new typeCellData;
72 cellData->row=row; 73 cellData->row=row;
73 cellData->col=col; 74 cellData->col=col;
74 cellData->data=defaultCellData.data; 75 cellData->data=defaultCellData.data;
75 cellData->borders=defaultCellData.borders; 76 cellData->borders=defaultCellData.borders;
76 cellData->alignment=defaultCellData.alignment; 77 cellData->alignment=defaultCellData.alignment;
77 cellData->font=defaultCellData.font; 78 cellData->font=defaultCellData.font;
78 cellData->fontColor=defaultCellData.fontColor; 79 cellData->fontColor=defaultCellData.fontColor;
79 cellData->background=defaultCellData.background; 80 cellData->background=defaultCellData.background;
80 sheetData.append(cellData); 81 sheetData.append(cellData);
81 return cellData; 82 return cellData;
82} 83}
83 84
84void Sheet::slotCellChanged(int row, int col) 85void Sheet::slotCellChanged(int row, int col)
85{ 86{
86 typeCellData *cellData=findCellData(row, col); 87 typeCellData *cellData=findCellData(row, col);
87 if (!cellData) cellData=createCellData(row, col); 88 if (!cellData) cellData=createCellData(row, col);
88 if (cellData) cellData->data=text(row, col); 89 if (cellData) cellData->data=text(row, col);
89 for (cellData=sheetData.first(); cellData; cellData=sheetData.next()) 90 for (cellData=sheetData.first(); cellData; cellData=sheetData.next())
90 setText(cellData->row, cellData->col, dataParser(cellData->data)); 91 setText(cellData->row, cellData->col, dataParser(cellData->data));
91 emit sheetModified(); 92 emit sheetModified();
92} 93}
93 94
94void Sheet::swapCells(int row1, int col1, int row2, int col2) 95void Sheet::swapCells(int row1, int col1, int row2, int col2)
95{ 96{
96 typeCellData *cellData1=findCellData(row1, col1), *cellData2=findCellData(row2, col2); 97 typeCellData *cellData1=findCellData(row1, col1), *cellData2=findCellData(row2, col2);
97 if (!cellData1) cellData1=createCellData(row1, col1); 98 if (!cellData1) cellData1=createCellData(row1, col1);
98 if (!cellData2) cellData2=createCellData(row2, col2); 99 if (!cellData2) cellData2=createCellData(row2, col2);
99 if (cellData1 && cellData2) 100 if (cellData1 && cellData2)
100 { 101 {
101 QString tempData(cellData1->data); 102 QString tempData(cellData1->data);
102 cellData1->data=cellData2->data; 103 cellData1->data=cellData2->data;
103 cellData2->data=tempData; 104 cellData2->data=tempData;
104 setText(cellData1->row, cellData1->col, dataParser(cellData1->data)); 105 setText(cellData1->row, cellData1->col, dataParser(cellData1->data));
105 setText(cellData2->row, cellData2->col, dataParser(cellData2->data)); 106 setText(cellData2->row, cellData2->col, dataParser(cellData2->data));
106 emit sheetModified(); 107 emit sheetModified();
107 } 108 }
108} 109}
109 110
110QString Sheet::getParameter(const QString &parameters, int paramNo, bool giveError, const QString funcName) 111QString Sheet::getParameter(const QString &parameters, int paramNo, bool giveError, const QString funcName)
111{ 112{
112 QString params(parameters); 113 QString params(parameters);
113 int position; 114 int position;
114 for (int i=0; i<paramNo; ++i) 115 for (int i=0; i<paramNo; ++i)
115 { 116 {
116 position=params.find(','); 117 position=params.find(',');
117 if (position<0) 118 if (position<0)
118 { 119 {
119 if (giveError) QMessageBox::critical(this, tr("Error"), tr("Too few arguments to function '"+funcName+'\'')); 120 if (giveError) QMessageBox::critical(this, tr("Error"), tr("Too few arguments to function '"+funcName+'\''));
120 return QString(); 121 return QString();
121 } 122 }
122 params=params.mid(position+1); 123 params=params.mid(position+1);
123 } 124 }
124 position=params.find(','); 125 position=params.find(',');
125 if (position<0) return params; 126 if (position<0) return params;
126 return params.left(position); 127 return params.left(position);
127} 128}
128 129
129bool Sheet::findRange(const QString &variable1, const QString &variable2, int *row1, int *col1, int *row2, int *col2) 130bool Sheet::findRange(const QString &variable1, const QString &variable2, int *row1, int *col1, int *row2, int *col2)
130{ 131{
131 int row, col; 132 int row, col;
132 if (!findRowColumn(variable1, row1, col1, TRUE) || !findRowColumn(variable2, row2, col2, TRUE)) return FALSE; 133 if (!findRowColumn(variable1, row1, col1, TRUE) || !findRowColumn(variable2, row2, col2, TRUE)) return FALSE;
133 if (*row1>*row2) 134 if (*row1>*row2)
134 { 135 {
135 row=*row1; 136 row=*row1;
136 *row1=*row2; 137 *row1=*row2;
137 *row2=row; 138 *row2=row;
138 } 139 }
139 if (*col1>*col2) 140 if (*col1>*col2)
140 { 141 {
141 col=*col1; 142 col=*col1;
142 *col1=*col2; 143 *col1=*col2;
143 *col2=col; 144 *col2=col;
144 } 145 }
145 return TRUE; 146 return TRUE;
146} 147}
147 148
148bool Sheet::findRowColumn(const QString &variable, int *row, int *col, bool giveError) 149bool Sheet::findRowColumn(const QString &variable, int *row, int *col, bool giveError)
149{ 150{
150 int position=variable.find(QRegExp("\\d")); 151 int position=variable.find(QRegExp("\\d"));
151 if (position<1) 152 if (position<1)
152 { 153 {
153 if (giveError) QMessageBox::critical(this, tr("Error"), tr("Invalid variable: '"+variable+'\'')); 154 if (giveError) QMessageBox::critical(this, tr("Error"), tr("Invalid variable: '"+variable+'\''));
154 return FALSE; 155 return FALSE;
155 } 156 }
156 *row=variable.mid(position).toInt()-1; 157 *row=variable.mid(position).toInt()-1;
157 *col=getHeaderColumn(variable.left(position))-1; 158 *col=getHeaderColumn(variable.left(position))-1;
158 return TRUE; 159 return TRUE;
159} 160}
160 161
161double Sheet::calculateVariable(const QString &variable) 162double Sheet::calculateVariable(const QString &variable)
162{ 163{
163 bool ok; 164 bool ok;
164 double tempResult=variable.toDouble(&ok); 165 double tempResult=variable.toDouble(&ok);
165 if (ok) return tempResult; 166 if (ok) return tempResult;
166 167
167 int row, col; 168 int row, col;
168 return (findRowColumn(variable, &row, &col, TRUE) ? text(row, col).toDouble() : 0); 169 return (findRowColumn(variable, &row, &col, TRUE) ? text(row, col).toDouble() : 0);
169} 170}
170 171
171double Sheet::functionSum(const QString &param1, const QString &param2) 172double Sheet::functionSum(const QString &param1, const QString &param2)
172{ 173{
173 int row1, col1, row2, col2, row, col; 174 int row1, col1, row2, col2, row, col;
174 if (!findRange(param1, param2, &row1, &col1, &row2, &col2)) return 0; 175 if (!findRange(param1, param2, &row1, &col1, &row2, &col2)) return 0;
175 176
176 double result=0, tempResult; 177 double result=0, tempResult;
177 bool ok; 178 bool ok;
178 for (row=row1; row<=row2; ++row) 179 for (row=row1; row<=row2; ++row)
179 for (col=col1; col<=col2; ++col) 180 for (col=col1; col<=col2; ++col)
180 { 181 {
181 tempResult=text(row, col).toDouble(&ok); 182 tempResult=text(row, col).toDouble(&ok);
182 if (ok) result+=tempResult; 183 if (ok) result+=tempResult;
183 } 184 }
184 185
185 return result; 186 return result;
186} 187}
187 188
188double Sheet::functionMin(const QString &param1, const QString &param2) 189double Sheet::functionMin(const QString &param1, const QString &param2)
189{ 190{
190 int row1, col1, row2, col2, row, col; 191 int row1, col1, row2, col2, row, col;
191 if (!findRange(param1, param2, &row1, &col1, &row2, &col2)) return 0; 192 if (!findRange(param1, param2, &row1, &col1, &row2, &col2)) return 0;
192 193
193 double min=0, tempMin; 194 double min=0, tempMin;
194 bool ok, init=FALSE; 195 bool ok, init=FALSE;
195 for (row=row1; row<=row2; ++row) 196 for (row=row1; row<=row2; ++row)
196 for (col=col1; col<=col2; ++col) 197 for (col=col1; col<=col2; ++col)
197 { 198 {
198 tempMin=text(row, col).toDouble(&ok); 199 tempMin=text(row, col).toDouble(&ok);
199 if (ok && (!init || tempMin<min)) 200 if (ok && (!init || tempMin<min))
200 { 201 {
201 min=tempMin; 202 min=tempMin;
202 init=TRUE; 203 init=TRUE;
203 } 204 }
204 } 205 }
205 206
206 return min; 207 return min;
207} 208}
208 209
209double Sheet::functionMax(const QString &param1, const QString &param2) 210double Sheet::functionMax(const QString &param1, const QString &param2)
210{ 211{
211 int row1, col1, row2, col2, row, col; 212 int row1, col1, row2, col2, row, col;
212 if (!findRange(param1, param2, &row1, &col1, &row2, &col2)) return 0; 213 if (!findRange(param1, param2, &row1, &col1, &row2, &col2)) return 0;
213 214
214 double max=0, tempMax; 215 double max=0, tempMax;
215 bool ok, init=FALSE; 216 bool ok, init=FALSE;
216 for (row=row1; row<=row2; ++row) 217 for (row=row1; row<=row2; ++row)
217 for (col=col1; col<=col2; ++col) 218 for (col=col1; col<=col2; ++col)
218 { 219 {
219 tempMax=text(row, col).toDouble(&ok); 220 tempMax=text(row, col).toDouble(&ok);
220 if (ok && (!init || tempMax>max)) 221 if (ok && (!init || tempMax>max))
221 { 222 {
222 max=tempMax; 223 max=tempMax;
223 init=TRUE; 224 init=TRUE;
224 } 225 }
225 } 226 }
226 227
227 return max; 228 return max;
228} 229}
229 230
230double Sheet::functionAvg(const QString &param1, const QString &param2) 231double Sheet::functionAvg(const QString &param1, const QString &param2)
231{ 232{
232 double resultSum=functionSum(param1, param2), resultCount=functionCount(param1, param2); 233 double resultSum=functionSum(param1, param2), resultCount=functionCount(param1, param2);
233 return (resultCount>0 ? resultSum/resultCount : 0); 234 return (resultCount>0 ? resultSum/resultCount : 0);
234} 235}
235 236
236double Sheet::functionCount(const QString &param1, const QString &param2) 237double Sheet::functionCount(const QString &param1, const QString &param2)
237{ 238{
238 int row1, col1, row2, col2, row, col; 239 int row1, col1, row2, col2, row, col;
239 if (!findRange(param1, param2, &row1, &col1, &row2, &col2)) return 0; 240 if (!findRange(param1, param2, &row1, &col1, &row2, &col2)) return 0;
240 241
241 int divider=0; 242 int divider=0;
242 bool ok; 243 bool ok;
243 for (row=row1; row<=row2; ++row) 244 for (row=row1; row<=row2; ++row)
244 for (col=col1; col<=col2; ++col) 245 for (col=col1; col<=col2; ++col)
245 { 246 {
246 text(row, col).toDouble(&ok); 247 text(row, col).toDouble(&ok);
247 if (ok) ++divider; 248 if (ok) ++divider;
248 } 249 }
249 250
250 return divider; 251 return divider;
251} 252}
252 253
253double Sheet::calculateFunction(const QString &function, const QString &parameters) 254double Sheet::calculateFunction(const QString &function, const QString &parameters)
254{ 255{
255 if (function=="+") 256 if (function=="+")
256 return calculateVariable(getParameter(parameters, 0))+calculateVariable(getParameter(parameters, 1)); 257 return calculateVariable(getParameter(parameters, 0))+calculateVariable(getParameter(parameters, 1));
257 if (function=="-") 258 if (function=="-")
258 return calculateVariable(getParameter(parameters, 0))-calculateVariable(getParameter(parameters, 1)); 259 return calculateVariable(getParameter(parameters, 0))-calculateVariable(getParameter(parameters, 1));
259 if (function=="*") 260 if (function=="*")
260 return calculateVariable(getParameter(parameters, 0))*calculateVariable(getParameter(parameters, 1)); 261 return calculateVariable(getParameter(parameters, 0))*calculateVariable(getParameter(parameters, 1));
261 if (function=="/") 262 if (function=="/")
262 return calculateVariable(getParameter(parameters, 0))/calculateVariable(getParameter(parameters, 1)); 263 return calculateVariable(getParameter(parameters, 0))/calculateVariable(getParameter(parameters, 1));
263 if (function=="SUM") 264 if (function=="SUM")
264 return functionSum(getParameter(parameters, 0, TRUE, function), getParameter(parameters, 1, TRUE, function)); 265 return functionSum(getParameter(parameters, 0, TRUE, function), getParameter(parameters, 1, TRUE, function));
265 if (function=="COUNT") 266 if (function=="COUNT")
266 return functionCount(getParameter(parameters, 0, TRUE, function), getParameter(parameters, 1, TRUE, function)); 267 return functionCount(getParameter(parameters, 0, TRUE, function), getParameter(parameters, 1, TRUE, function));
267 if (function=="MIN") 268 if (function=="MIN")
268 return functionMin(getParameter(parameters, 0, TRUE, function), getParameter(parameters, 1, TRUE, function)); 269 return functionMin(getParameter(parameters, 0, TRUE, function), getParameter(parameters, 1, TRUE, function));
269 if (function=="MAX") 270 if (function=="MAX")
270 return functionMax(getParameter(parameters, 0, TRUE, function), getParameter(parameters, 1, TRUE, function)); 271 return functionMax(getParameter(parameters, 0, TRUE, function), getParameter(parameters, 1, TRUE, function));
271 if (function=="AVG") 272 if (function=="AVG")
272 return functionAvg(getParameter(parameters, 0, TRUE, function), getParameter(parameters, 1, TRUE, function)); 273 return functionAvg(getParameter(parameters, 0, TRUE, function), getParameter(parameters, 1, TRUE, function));
273 if (function=="ABS") 274 if (function=="ABS")
274 return fabs(calculateVariable(getParameter(parameters, 0, TRUE, function))); 275 return fabs(calculateVariable(getParameter(parameters, 0, TRUE, function)));
275 if (function=="SIN") 276 if (function=="SIN")
276 return sin(calculateVariable(getParameter(parameters, 0, TRUE, function))); 277 return sin(calculateVariable(getParameter(parameters, 0, TRUE, function)));
277 if (function=="COS") 278 if (function=="COS")
278 return cos(calculateVariable(getParameter(parameters, 0, TRUE, function))); 279 return cos(calculateVariable(getParameter(parameters, 0, TRUE, function)));
279 if (function=="TAN") 280 if (function=="TAN")
280 return tan(calculateVariable(getParameter(parameters, 0, TRUE, function))); 281 return tan(calculateVariable(getParameter(parameters, 0, TRUE, function)));
281 if (function=="ATAN") 282 if (function=="ATAN")
282 return atan(calculateVariable(getParameter(parameters, 0, TRUE, function))); 283 return atan(calculateVariable(getParameter(parameters, 0, TRUE, function)));
283 if (function=="ATAN2") 284 if (function=="ATAN2")
284 return atan2(calculateVariable(getParameter(parameters, 0, TRUE, function)), calculateVariable(getParameter(parameters, 1, TRUE, function))); 285 return atan2(calculateVariable(getParameter(parameters, 0, TRUE, function)), calculateVariable(getParameter(parameters, 1, TRUE, function)));
285 if (function=="ASIN") 286 if (function=="ASIN")
286 return asin(calculateVariable(getParameter(parameters, 0, TRUE, function))); 287 return asin(calculateVariable(getParameter(parameters, 0, TRUE, function)));
287 if (function=="ACOS") 288 if (function=="ACOS")
288 return acos(calculateVariable(getParameter(parameters, 0, TRUE, function))); 289 return acos(calculateVariable(getParameter(parameters, 0, TRUE, function)));
289 if (function=="EXP") 290 if (function=="EXP")
290 return exp(calculateVariable(getParameter(parameters, 0, TRUE, function))); 291 return exp(calculateVariable(getParameter(parameters, 0, TRUE, function)));
291 if (function=="LOG") 292 if (function=="LOG")
292 return log(calculateVariable(getParameter(parameters, 0, TRUE, function))); 293 return log(calculateVariable(getParameter(parameters, 0, TRUE, function)));
293 if (function=="POW") 294 if (function=="POW")
294 return pow(calculateVariable(getParameter(parameters, 0, TRUE, function)), calculateVariable(getParameter(parameters, 1, TRUE, function))); 295 return pow(calculateVariable(getParameter(parameters, 0, TRUE, function)), calculateVariable(getParameter(parameters, 1, TRUE, function)));
295 return 0; 296 return 0;
296} 297}
297 298
298int Sheet::getOperatorPriority(char oper) 299int Sheet::getOperatorPriority(char oper)
299{ 300{
300 switch (oper) 301 switch (oper)
301 { 302 {
302 case '+': 303 case '+':
303 case '-': 304 case '-':
304 return 1; 305 return 1;
305 306
306 case '*': 307 case '*':
307 case '/': 308 case '/':
308 return 2; 309 return 2;
309 } 310 }
310 return 0; 311 return 0;
311} 312}
312 313
313void Sheet::pushCharStack(QStack<QChar> *stackChars, const QChar &character) 314void Sheet::pushCharStack(QStack<QChar> *stackChars, const QChar &character)
314{ 315{
315 QChar *temp=new QChar(character); 316 QChar *temp=new QChar(character);
316 stackChars->push(temp); 317 stackChars->push(temp);
317} 318}
318 319
319void Sheet::pushStringStack(QStack<QString> *stackStrings, const QString &string) 320void Sheet::pushStringStack(QStack<QString> *stackStrings, const QString &string)
320{ 321{
321 QString *temp=new QString(string); 322 QString *temp=new QString(string);
322 stackStrings->push(temp); 323 stackStrings->push(temp);
323} 324}
324 325
325QChar Sheet::popCharStack(QStack<QChar> *stackChars) 326QChar Sheet::popCharStack(QStack<QChar> *stackChars)
326{ 327{
327 if (stackChars->isEmpty()) 328 if (stackChars->isEmpty())
328 { 329 {
329 QMessageBox::critical(this, tr("Error"), tr("Syntax error!")); 330 QMessageBox::critical(this, tr("Error"), tr("Syntax error!"));
330 return '0'; 331 return '0';
331 } 332 }
332 333
333 QChar *temp=stackChars->pop(); 334 QChar *temp=stackChars->pop();
334 QChar temp2(*temp); 335 QChar temp2(*temp);
335 delete temp; 336 delete temp;
336 return temp2; 337 return temp2;
337} 338}
338 339
339QString Sheet::popStringStack(QStack<QString> *stackStrings) 340QString Sheet::popStringStack(QStack<QString> *stackStrings)
340{ 341{
341 if (stackStrings->isEmpty()) 342 if (stackStrings->isEmpty())
342 { 343 {
343 QMessageBox::critical(this, tr("Error"), tr("Syntax error!")); 344 QMessageBox::critical(this, tr("Error"), tr("Syntax error!"));
344 return "0"; 345 return "0";
345 } 346 }
346 347
347 QString *temp=stackStrings->pop(); 348 QString *temp=stackStrings->pop();
348 QString temp2(*temp); 349 QString temp2(*temp);
349 delete temp; 350 delete temp;
350 return temp2; 351 return temp2;
351} 352}
352 353
353QString Sheet::dataParserHelper(const QString &data) 354QString Sheet::dataParserHelper(const QString &data)
354{ 355{
355 QStack<QString> stackElements; 356 QStack<QString> stackElements;
356 QStack<QChar> stackOperators; 357 QStack<QChar> stackOperators;
357 QString tempElement(""), temp2Element, firstElement, secondElement; 358 QString tempElement(""), temp2Element, firstElement, secondElement;
358 int paranCount; 359 int paranCount;
359 360
360 for (unsigned int i=0; i<data.length(); ++i) 361 for (unsigned int i=0; i<data.length(); ++i)
361 { 362 {
362 if (data[i]=='+' || data[i]=='-' || data[i]=='*' || data[i]=='/') 363 if (data[i]=='+' || data[i]=='-' || data[i]=='*' || data[i]=='/')
363 { 364 {
364 pushStringStack(&stackElements, tempElement); 365 pushStringStack(&stackElements, tempElement);
365 tempElement=""; 366 tempElement="";
366 if (!stackOperators.isEmpty() && getOperatorPriority(*stackOperators.top())>getOperatorPriority(data[i])) 367 if (!stackOperators.isEmpty() && getOperatorPriority(*stackOperators.top())>getOperatorPriority(data[i]))
367 { 368 {
368 secondElement=popStringStack(&stackElements); 369 secondElement=popStringStack(&stackElements);
369 firstElement=popStringStack(&stackElements); 370 firstElement=popStringStack(&stackElements);
370 pushStringStack(&stackElements, QString::number(calculateFunction(popCharStack(&stackOperators), firstElement+","+secondElement))); 371 pushStringStack(&stackElements, QString::number(calculateFunction(popCharStack(&stackOperators), firstElement+","+secondElement)));
371 } 372 }
372 pushCharStack(&stackOperators, data[i]); 373 pushCharStack(&stackOperators, data[i]);
373 } 374 }
374 else 375 else
375 if (data[i]==',') 376 if (data[i]==',')
376 { 377 {
377 if (!tempElement.isEmpty()) pushStringStack(&stackElements, tempElement); 378 if (!tempElement.isEmpty()) pushStringStack(&stackElements, tempElement);
378 while (!stackOperators.isEmpty()) 379 while (!stackOperators.isEmpty())
379 { 380 {
380 secondElement=popStringStack(&stackElements); 381 secondElement=popStringStack(&stackElements);
381 firstElement=popStringStack(&stackElements); 382 firstElement=popStringStack(&stackElements);
382 pushStringStack(&stackElements, QString::number(calculateFunction(popCharStack(&stackOperators), firstElement+","+secondElement))); 383 pushStringStack(&stackElements, QString::number(calculateFunction(popCharStack(&stackOperators), firstElement+","+secondElement)));
383 } 384 }
384 tempElement=""; 385 tempElement="";
385 } 386 }
386 else 387 else
387 if (data[i]=='(') 388 if (data[i]=='(')
388 { 389 {
389 paranCount=1; 390 paranCount=1;
390 temp2Element=""; 391 temp2Element="";
391 for (++i; paranCount>0; ++i) 392 for (++i; paranCount>0; ++i)
392 { 393 {
393 temp2Element+=data[i]; 394 temp2Element+=data[i];
394 if (data[i]=='(') ++paranCount; 395 if (data[i]=='(') ++paranCount;
395 if (data[i]==')') --paranCount; 396 if (data[i]==')') --paranCount;
396 } 397 }
397 temp2Element=dataParserHelper(temp2Element.left(temp2Element.length()-1)); 398 temp2Element=dataParserHelper(temp2Element.left(temp2Element.length()-1));
398 if (tempElement.isEmpty()) 399 if (tempElement.isEmpty())
399 tempElement=temp2Element; 400 tempElement=temp2Element;
400 else 401 else
401 tempElement.setNum(calculateFunction(tempElement, temp2Element)); 402 tempElement.setNum(calculateFunction(tempElement, temp2Element));
402 --i; 403 --i;
403 } 404 }
404 else 405 else
405 tempElement+=data[i]; 406 tempElement+=data[i];
406 } 407 }
407 if (!tempElement.isEmpty()) pushStringStack(&stackElements, tempElement); 408 if (!tempElement.isEmpty()) pushStringStack(&stackElements, tempElement);
408 while (!stackOperators.isEmpty()) 409 while (!stackOperators.isEmpty())
409 { 410 {
410 secondElement=popStringStack(&stackElements); 411 secondElement=popStringStack(&stackElements);
411 firstElement=popStringStack(&stackElements); 412 firstElement=popStringStack(&stackElements);
412 pushStringStack(&stackElements, QString::number(calculateFunction(popCharStack(&stackOperators), firstElement+","+secondElement))); 413 pushStringStack(&stackElements, QString::number(calculateFunction(popCharStack(&stackOperators), firstElement+","+secondElement)));
413 } 414 }
414 415
415 if (!stackElements.isEmpty()) 416 if (!stackElements.isEmpty())
416 tempElement=popStringStack(&stackElements); 417 tempElement=popStringStack(&stackElements);
417 while (!stackElements.isEmpty()) 418 while (!stackElements.isEmpty())
418 tempElement.prepend(popStringStack(&stackElements)+","); 419 tempElement.prepend(popStringStack(&stackElements)+",");
419 return tempElement; 420 return tempElement;
420} 421}
421 422
422QString Sheet::dataParser(const QString &data) 423QString Sheet::dataParser(const QString &data)
423{ 424{
424 QString strippedData(data); 425 QString strippedData(data);
425 strippedData.replace(QRegExp("\\s"), ""); 426 strippedData.replace(QRegExp("\\s"), "");
426 if (strippedData.isEmpty() || strippedData[0]!='=') return data; 427 if (strippedData.isEmpty() || strippedData[0]!='=') return data;
427 strippedData=dataParserHelper(strippedData.remove(0, 1).upper().replace(QRegExp(":"), ",")); 428 strippedData=dataParserHelper(strippedData.remove(0, 1).upper().replace(QRegExp(":"), ","));
428 429
429 int i=0; 430 int i=0;
430 QString tempParameter(getParameter(strippedData, i)), result=""; 431 QString tempParameter(getParameter(strippedData, i)), result="";
431 do 432 do
432 { 433 {
433 result+=","+QString::number(calculateVariable(tempParameter)); 434 result+=","+QString::number(calculateVariable(tempParameter));
434 tempParameter=getParameter(strippedData, ++i); 435 tempParameter=getParameter(strippedData, ++i);
435 } 436 }
436 while (!tempParameter.isNull()); 437 while (!tempParameter.isNull());
437 return result.mid(1); 438 return result.mid(1);
438} 439}
439 440
440void Sheet::setData(const QString &data) 441void Sheet::setData(const QString &data)
441{ 442{
442 setText(currentRow(), currentColumn(), data); 443 setText(currentRow(), currentColumn(), data);
443 slotCellChanged(currentRow(), currentColumn()); 444 slotCellChanged(currentRow(), currentColumn());
444 activateNextCell(); 445 activateNextCell();
445} 446}
446 447
447QString Sheet::getData() 448QString Sheet::getData()
448{ 449{
449 typeCellData *cellData=findCellData(currentRow(), currentColumn()); 450 typeCellData *cellData=findCellData(currentRow(), currentColumn());
450 if (cellData) 451 if (cellData)
451 return cellData->data; 452 return cellData->data;
452 return ""; 453 return "";
453} 454}
454 455
455void Sheet::lockClicks(bool lock) 456void Sheet::lockClicks(bool lock)
456{ 457{
457 clicksLocked=lock; 458 clicksLocked=lock;
458} 459}
459 460
460void Sheet::paintCell(QPainter *p, int row, int col, const QRect & cr, bool selected) 461void Sheet::paintCell(QPainter *p, int row, int col, const QRect & cr, bool selected)
461{ 462{
462 if (selected && row==currentRow() && col==currentColumn()) selected=FALSE; 463 if (selected && row==currentRow() && col==currentColumn()) selected=FALSE;
463 464
464 int sheetDataCurrent=sheetData.at(); 465 int sheetDataCurrent=sheetData.at();
465 typeCellData *cellData=findCellData(row, col); 466 typeCellData *cellData=findCellData(row, col);
466 if (sheetDataCurrent>=0) sheetData.at(sheetDataCurrent); 467 if (sheetDataCurrent>=0) sheetData.at(sheetDataCurrent);
467 if (!cellData) cellData=&defaultCellData; 468 if (!cellData) cellData=&defaultCellData;
468 if (selected) 469 if (selected)
469 p->fillRect(0, 0, cr.width(), cr.height(), colorGroup().highlight()); 470 p->fillRect(0, 0, cr.width(), cr.height(), colorGroup().highlight());
470 else 471 else
471 { 472 {
472 p->fillRect(0, 0, cr.width(), cr.height(), colorGroup().base()); 473 p->fillRect(0, 0, cr.width(), cr.height(), colorGroup().base());
473 p->fillRect(0, 0, cr.width(), cr.height(), cellData->background); 474 p->fillRect(0, 0, cr.width(), cr.height(), cellData->background);
474 } 475 }
475 476
476 QTableItem *cellItem=item(row, col); 477 QTableItem *cellItem=item(row, col);
477 if (cellItem) 478 if (cellItem)
478 { 479 {
479 p->setPen(selected ? colorGroup().highlightedText() : cellData->fontColor); 480 p->setPen(selected ? colorGroup().highlightedText() : cellData->fontColor);
480 p->setFont(cellData->font); 481 p->setFont(cellData->font);
481 p->drawText(2, 2, cr.width()-4, cr.height()-4, cellData->alignment, cellItem->text()); 482 p->drawText(2, 2, cr.width()-4, cr.height()-4, cellData->alignment, cellItem->text());
482 } 483 }
483 484
484 int rx=cr.width()-1, ry=cr.height()-1; 485 int rx=cr.width()-1, ry=cr.height()-1;
485 QPen pen(p->pen()); 486 QPen pen(p->pen());
486 p->setPen(cellData->borders.right); 487 p->setPen(cellData->borders.right);
487 p->drawLine(rx, 0, rx, ry); 488 p->drawLine(rx, 0, rx, ry);
488 p->setPen(cellData->borders.bottom); 489 p->setPen(cellData->borders.bottom);
489 p->drawLine(0, ry, rx, ry); 490 p->drawLine(0, ry, rx, ry);
490 p->setPen(pen); 491 p->setPen(pen);
491} 492}
492 493
493void Sheet::viewportMousePressEvent(QMouseEvent *e) 494void Sheet::viewportMousePressEvent(QMouseEvent *e)
494{ 495{
495 QMouseEvent ce(e->type(), viewportToContents(e->pos()), e->globalPos(), e->button(), e->state()); 496 QMouseEvent ce(e->type(), viewportToContents(e->pos()), e->globalPos(), e->button(), e->state());
496 if (clicksLocked) 497 if (clicksLocked)
497 { 498 {
498 if (selectionNo<0) 499 if (selectionNo<0)
499 { 500 {
500 clearSelection(); 501 clearSelection();
501 QTableSelection newSelection; 502 QTableSelection newSelection;
502 newSelection.init(rowAt(ce.pos().y()), columnAt(ce.pos().x())); 503 newSelection.init(rowAt(ce.pos().y()), columnAt(ce.pos().x()));
503 newSelection.expandTo(newSelection.anchorRow(), newSelection.anchorCol()); 504 newSelection.expandTo(newSelection.anchorRow(), newSelection.anchorCol());
504 selectionNo=addSelection(newSelection); 505 selectionNo=addSelection(newSelection);
505 } 506 }
506 } 507 }
507 else 508 else
508 QTable::contentsMousePressEvent(&ce); 509 QTable::contentsMousePressEvent(&ce);
509} 510}
510 511
511void Sheet::viewportMouseMoveEvent(QMouseEvent *e) 512void Sheet::viewportMouseMoveEvent(QMouseEvent *e)
512{ 513{
513 QMouseEvent ce(e->type(), viewportToContents(e->pos()), e->globalPos(), e->button(), e->state()); 514 QMouseEvent ce(e->type(), viewportToContents(e->pos()), e->globalPos(), e->button(), e->state());
514 if (clicksLocked) 515 if (clicksLocked)
515 { 516 {
516 if (selectionNo>=0) 517 if (selectionNo>=0)
517 { 518 {
518 QTableSelection oldSelection(selection(selectionNo)); 519 QTableSelection oldSelection(selection(selectionNo));
519 oldSelection.expandTo(rowAt(ce.pos().y()), columnAt(ce.pos().x())); 520 oldSelection.expandTo(rowAt(ce.pos().y()), columnAt(ce.pos().x()));
520 if (!(oldSelection==selection(selectionNo))) 521 if (!(oldSelection==selection(selectionNo)))
521 { 522 {
522 removeSelection(selectionNo); 523 removeSelection(selectionNo);
523 selectionNo=addSelection(oldSelection); 524 selectionNo=addSelection(oldSelection);
524 } 525 }
525 } 526 }
526 } 527 }
527 else 528 else
528 QTable::contentsMouseMoveEvent(&ce); 529 QTable::contentsMouseMoveEvent(&ce);
529} 530}
530 531
531void Sheet::viewportMouseReleaseEvent(QMouseEvent *e) 532void Sheet::viewportMouseReleaseEvent(QMouseEvent *e)
532{ 533{
533 QMouseEvent ce(e->type(), viewportToContents(e->pos()), e->globalPos(), e->button(), e->state()); 534 QMouseEvent ce(e->type(), viewportToContents(e->pos()), e->globalPos(), e->button(), e->state());
534 if (clicksLocked && selectionNo>=0) 535 if (clicksLocked && selectionNo>=0)
535 { 536 {
536 QTableSelection oldSelection(selection(selectionNo)); 537 QTableSelection oldSelection(selection(selectionNo));
537 oldSelection.expandTo(rowAt(ce.pos().y()), columnAt(ce.pos().x())); 538 oldSelection.expandTo(rowAt(ce.pos().y()), columnAt(ce.pos().x()));
538 removeSelection(selectionNo); 539 removeSelection(selectionNo);
539 selectionNo=-1; 540 selectionNo=-1;
540 if (oldSelection.topRow()==oldSelection.bottomRow() && oldSelection.leftCol()==oldSelection.rightCol()) 541 if (oldSelection.topRow()==oldSelection.bottomRow() && oldSelection.leftCol()==oldSelection.rightCol())
541 emit cellClicked(getHeaderString(oldSelection.leftCol()+1)+QString::number(oldSelection.topRow()+1)); 542 emit cellClicked(getHeaderString(oldSelection.leftCol()+1)+QString::number(oldSelection.topRow()+1));
542 else 543 else
543 emit cellClicked(getHeaderString(oldSelection.leftCol()+1)+QString::number(oldSelection.topRow()+1)+','+getHeaderString(oldSelection.rightCol()+1)+QString::number(oldSelection.bottomRow()+1)); 544 emit cellClicked(getHeaderString(oldSelection.leftCol()+1)+QString::number(oldSelection.topRow()+1)+','+getHeaderString(oldSelection.rightCol()+1)+QString::number(oldSelection.bottomRow()+1));
544 } 545 }
545 else 546 else