-rw-r--r-- | noncore/apps/opie-sheet/ChangeLog | 4 | ||||
-rw-r--r-- | noncore/apps/opie-sheet/sheet.cpp | 1 |
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 @@ | |||
1 | October 05, 2002 | ||
2 | |||
3 | - Fixed bug #252. (by thufir) | ||
4 | |||
1 | August 31, 2002 | 5 | August 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 | ||
5 | August 01, 2002 | 9 | August 01, 2002 |
6 | 10 | ||
7 | - Fixed gcc3 compile problems. (by harlekin) | 11 | - Fixed gcc3 compile problems. (by harlekin) |
8 | 12 | ||
9 | July 06, 2002 | 13 | July 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 | ||
13 | July 04, 2002 | 17 | July 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 | ||
19 | April 14, 2002 | 23 | April 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,863 +1,864 @@ | |||
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 | ||
23 | Sheet::Sheet(int numRows, int numCols, QWidget *parent) | 23 | Sheet::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 | ||
46 | Sheet::~Sheet() | 47 | Sheet::~Sheet() |
47 | { | 48 | { |
48 | } | 49 | } |
49 | 50 | ||
50 | typeCellData *Sheet::findCellData(int row, int col) | 51 | typeCellData *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 | ||
59 | void Sheet::slotCellSelected(int row, int col) | 60 | void 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 | ||
68 | typeCellData *Sheet::createCellData(int row, int col) | 69 | typeCellData *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 | ||
84 | void Sheet::slotCellChanged(int row, int col) | 85 | void 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 | ||
94 | void Sheet::swapCells(int row1, int col1, int row2, int col2) | 95 | void 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 | ||
110 | QString Sheet::getParameter(const QString ¶meters, int paramNo, bool giveError, const QString funcName) | 111 | QString Sheet::getParameter(const QString ¶meters, 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 | ||
129 | bool Sheet::findRange(const QString &variable1, const QString &variable2, int *row1, int *col1, int *row2, int *col2) | 130 | bool 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 | ||
148 | bool Sheet::findRowColumn(const QString &variable, int *row, int *col, bool giveError) | 149 | bool 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 | ||
161 | double Sheet::calculateVariable(const QString &variable) | 162 | double 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 | ||
171 | double Sheet::functionSum(const QString ¶m1, const QString ¶m2) | 172 | double Sheet::functionSum(const QString ¶m1, const QString ¶m2) |
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 | ||
188 | double Sheet::functionMin(const QString ¶m1, const QString ¶m2) | 189 | double Sheet::functionMin(const QString ¶m1, const QString ¶m2) |
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 | ||
209 | double Sheet::functionMax(const QString ¶m1, const QString ¶m2) | 210 | double Sheet::functionMax(const QString ¶m1, const QString ¶m2) |
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 | ||
230 | double Sheet::functionAvg(const QString ¶m1, const QString ¶m2) | 231 | double Sheet::functionAvg(const QString ¶m1, const QString ¶m2) |
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 | ||
236 | double Sheet::functionCount(const QString ¶m1, const QString ¶m2) | 237 | double Sheet::functionCount(const QString ¶m1, const QString ¶m2) |
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 | ||
253 | double Sheet::calculateFunction(const QString &function, const QString ¶meters) | 254 | double Sheet::calculateFunction(const QString &function, const QString ¶meters) |
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 | ||
298 | int Sheet::getOperatorPriority(char oper) | 299 | int 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 | ||
313 | void Sheet::pushCharStack(QStack<QChar> *stackChars, const QChar &character) | 314 | void 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 | ||
319 | void Sheet::pushStringStack(QStack<QString> *stackStrings, const QString &string) | 320 | void 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 | ||
325 | QChar Sheet::popCharStack(QStack<QChar> *stackChars) | 326 | QChar 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 | ||
339 | QString Sheet::popStringStack(QStack<QString> *stackStrings) | 340 | QString 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 | ||
353 | QString Sheet::dataParserHelper(const QString &data) | 354 | QString 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 | ||
422 | QString Sheet::dataParser(const QString &data) | 423 | QString 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 | ||
440 | void Sheet::setData(const QString &data) | 441 | void 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 | ||
447 | QString Sheet::getData() | 448 | QString 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 | ||
455 | void Sheet::lockClicks(bool lock) | 456 | void Sheet::lockClicks(bool lock) |
456 | { | 457 | { |
457 | clicksLocked=lock; | 458 | clicksLocked=lock; |
458 | } | 459 | } |
459 | 460 | ||
460 | void Sheet::paintCell(QPainter *p, int row, int col, const QRect & cr, bool selected) | 461 | void 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 | ||
493 | void Sheet::viewportMousePressEvent(QMouseEvent *e) | 494 | void 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 | ||
511 | void Sheet::viewportMouseMoveEvent(QMouseEvent *e) | 512 | void 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 | ||
531 | void Sheet::viewportMouseReleaseEvent(QMouseEvent *e) | 532 | void 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 |
546 | QTable::contentsMouseReleaseEvent(&ce); | 547 | QTable::contentsMouseReleaseEvent(&ce); |
547 | } | 548 | } |
548 | 549 | ||
549 | void Sheet::copySheetData(QList<typeCellData> *destSheetData) | 550 | void Sheet::copySheetData(QList<typeCellData> *destSheetData) |
550 | { | 551 | { |
551 | typeCellData *tempCellData, *newCellData; | 552 | typeCellData *tempCellData, *newCellData; |
552 | destSheetData->clear(); | 553 | destSheetData->clear(); |
553 | 554 | ||
554 | for (tempCellData=sheetData.first(); tempCellData; tempCellData=sheetData.next()) | 555 | for (tempCellData=sheetData.first(); tempCellData; tempCellData=sheetData.next()) |
555 | { | 556 | { |
556 | newCellData=new typeCellData; | 557 | newCellData=new typeCellData; |
557 | *newCellData=*tempCellData; | 558 | *newCellData=*tempCellData; |
558 | destSheetData->append(newCellData); | 559 | destSheetData->append(newCellData); |
559 | } | 560 | } |
560 | } | 561 | } |
561 | 562 | ||
562 | void Sheet::setSheetData(QList<typeCellData> *srcSheetData) | 563 | void Sheet::setSheetData(QList<typeCellData> *srcSheetData) |
563 | { | 564 | { |
564 | typeCellData *tempCellData, *newCellData; | 565 | typeCellData *tempCellData, *newCellData; |
565 | 566 | ||
566 | for (tempCellData=sheetData.first(); tempCellData; tempCellData=sheetData.next()) | 567 | for (tempCellData=sheetData.first(); tempCellData; tempCellData=sheetData.next()) |
567 | { | 568 | { |
568 | clearCell(tempCellData->row, tempCellData->col); | 569 | clearCell(tempCellData->row, tempCellData->col); |
569 | updateCell(tempCellData->row, tempCellData->col); | 570 | updateCell(tempCellData->row, tempCellData->col); |
570 | } | 571 | } |
571 | sheetData.clear(); | 572 | sheetData.clear(); |
572 | 573 | ||
573 | for (tempCellData=srcSheetData->first(); tempCellData; tempCellData=srcSheetData->next()) | 574 | for (tempCellData=srcSheetData->first(); tempCellData; tempCellData=srcSheetData->next()) |
574 | { | 575 | { |
575 | newCellData=new typeCellData; | 576 | newCellData=new typeCellData; |
576 | *newCellData=*tempCellData; | 577 | *newCellData=*tempCellData; |
577 | sheetData.append(newCellData); | 578 | sheetData.append(newCellData); |
578 | setText(newCellData->row, newCellData->col, dataParser(newCellData->data)); | 579 | setText(newCellData->row, newCellData->col, dataParser(newCellData->data)); |
579 | } | 580 | } |
580 | emit sheetModified(); | 581 | emit sheetModified(); |
581 | } | 582 | } |
582 | 583 | ||
583 | void Sheet::setName(const QString &name) | 584 | void Sheet::setName(const QString &name) |
584 | { | 585 | { |
585 | sheetName=name; | 586 | sheetName=name; |
586 | emit sheetModified(); | 587 | emit sheetModified(); |
587 | } | 588 | } |
588 | 589 | ||
589 | QString Sheet::getName() | 590 | QString Sheet::getName() |
590 | { | 591 | { |
591 | return sheetName; | 592 | return sheetName; |
592 | } | 593 | } |
593 | 594 | ||
594 | void Sheet::setBrush(int row, int col, const QBrush &brush) | 595 | void Sheet::setBrush(int row, int col, const QBrush &brush) |
595 | { | 596 | { |
596 | typeCellData *cellData=findCellData(row, col); | 597 | typeCellData *cellData=findCellData(row, col); |
597 | if (!cellData) cellData=createCellData(row, col); | 598 | if (!cellData) cellData=createCellData(row, col); |
598 | if (cellData) | 599 | if (cellData) |
599 | { | 600 | { |
600 | cellData->background=brush; | 601 | cellData->background=brush; |
601 | emit sheetModified(); | 602 | emit sheetModified(); |
602 | } | 603 | } |
603 | } | 604 | } |
604 | 605 | ||
605 | QBrush Sheet::getBrush(int row, int col) | 606 | QBrush Sheet::getBrush(int row, int col) |
606 | { | 607 | { |
607 | typeCellData *cellData=findCellData(row, col); | 608 | typeCellData *cellData=findCellData(row, col); |
608 | if (!cellData) cellData=&defaultCellData; | 609 | if (!cellData) cellData=&defaultCellData; |
609 | return cellData->background; | 610 | return cellData->background; |
610 | } | 611 | } |
611 | 612 | ||
612 | void Sheet::setTextAlign(int row, int col, Qt::AlignmentFlags flags) | 613 | void Sheet::setTextAlign(int row, int col, Qt::AlignmentFlags flags) |
613 | { | 614 | { |
614 | typeCellData *cellData=findCellData(row, col); | 615 | typeCellData *cellData=findCellData(row, col); |
615 | if (!cellData) cellData=createCellData(row, col); | 616 | if (!cellData) cellData=createCellData(row, col); |
616 | if (cellData) | 617 | if (cellData) |
617 | { | 618 | { |
618 | cellData->alignment=flags; | 619 | cellData->alignment=flags; |
619 | emit sheetModified(); | 620 | emit sheetModified(); |
620 | } | 621 | } |
621 | } | 622 | } |
622 | 623 | ||
623 | Qt::AlignmentFlags Sheet::getAlignment(int row, int col) | 624 | Qt::AlignmentFlags Sheet::getAlignment(int row, int col) |
624 | { | 625 | { |
625 | typeCellData *cellData=findCellData(row, col); | 626 | typeCellData *cellData=findCellData(row, col); |
626 | if (!cellData) cellData=&defaultCellData; | 627 | if (!cellData) cellData=&defaultCellData; |
627 | return cellData->alignment; | 628 | return cellData->alignment; |
628 | } | 629 | } |
629 | 630 | ||
630 | void Sheet::setTextFont(int row, int col, const QFont &font, const QColor &color) | 631 | void Sheet::setTextFont(int row, int col, const QFont &font, const QColor &color) |
631 | { | 632 | { |
632 | typeCellData *cellData=findCellData(row, col); | 633 | typeCellData *cellData=findCellData(row, col); |
633 | if (!cellData) cellData=createCellData(row, col); | 634 | if (!cellData) cellData=createCellData(row, col); |
634 | if (cellData) | 635 | if (cellData) |
635 | { | 636 | { |
636 | cellData->font=font; | 637 | cellData->font=font; |
637 | cellData->fontColor=color; | 638 | cellData->fontColor=color; |
638 | emit sheetModified(); | 639 | emit sheetModified(); |
639 | } | 640 | } |
640 | } | 641 | } |
641 | 642 | ||
642 | QFont Sheet::getFont(int row, int col) | 643 | QFont Sheet::getFont(int row, int col) |
643 | { | 644 | { |
644 | typeCellData *cellData=findCellData(row, col); | 645 | typeCellData *cellData=findCellData(row, col); |
645 | if (!cellData) cellData=&defaultCellData; | 646 | if (!cellData) cellData=&defaultCellData; |
646 | return cellData->font; | 647 | return cellData->font; |
647 | } | 648 | } |
648 | 649 | ||
649 | QColor Sheet::getFontColor(int row, int col) | 650 | QColor Sheet::getFontColor(int row, int col) |
650 | { | 651 | { |
651 | typeCellData *cellData=findCellData(row, col); | 652 | typeCellData *cellData=findCellData(row, col); |
652 | if (!cellData) cellData=&defaultCellData; | 653 | if (!cellData) cellData=&defaultCellData; |
653 | return cellData->fontColor; | 654 | return cellData->fontColor; |
654 | } | 655 | } |
655 | 656 | ||
656 | void Sheet::setPen(int row, int col, int vertical, const QPen &pen) | 657 | void Sheet::setPen(int row, int col, int vertical, const QPen &pen) |
657 | { | 658 | { |
658 | typeCellData *cellData=findCellData(row, col); | 659 | typeCellData *cellData=findCellData(row, col); |
659 | if (!cellData) cellData=createCellData(row, col); | 660 | if (!cellData) cellData=createCellData(row, col); |
660 | if (cellData) | 661 | if (cellData) |
661 | { | 662 | { |
662 | if (vertical) | 663 | if (vertical) |
663 | cellData->borders.right=pen; | 664 | cellData->borders.right=pen; |
664 | else | 665 | else |
665 | cellData->borders.bottom=pen; | 666 | cellData->borders.bottom=pen; |
666 | emit sheetModified(); | 667 | emit sheetModified(); |
667 | } | 668 | } |
668 | } | 669 | } |
669 | 670 | ||
670 | QPen Sheet::getPen(int row, int col, int vertical) | 671 | QPen Sheet::getPen(int row, int col, int vertical) |
671 | { | 672 | { |
672 | typeCellData *cellData=findCellData(row, col); | 673 | typeCellData *cellData=findCellData(row, col); |
673 | if (!cellData) cellData=&defaultCellData; | 674 | if (!cellData) cellData=&defaultCellData; |
674 | return (vertical ? cellData->borders.right : cellData->borders.bottom); | 675 | return (vertical ? cellData->borders.right : cellData->borders.bottom); |
675 | } | 676 | } |
676 | 677 | ||
677 | void Sheet::getSelection(int *row1, int *col1, int *row2, int *col2) | 678 | void Sheet::getSelection(int *row1, int *col1, int *row2, int *col2) |
678 | { | 679 | { |
679 | int selectionNo=currentSelection(); | 680 | int selectionNo=currentSelection(); |
680 | if (selectionNo>=0) | 681 | if (selectionNo>=0) |
681 | { | 682 | { |
682 | QTableSelection selection(selection(selectionNo)); | 683 | QTableSelection selection(selection(selectionNo)); |
683 | *row1=selection.topRow(); | 684 | *row1=selection.topRow(); |
684 | *row2=selection.bottomRow(); | 685 | *row2=selection.bottomRow(); |
685 | *col1=selection.leftCol(); | 686 | *col1=selection.leftCol(); |
686 | *col2=selection.rightCol(); | 687 | *col2=selection.rightCol(); |
687 | } | 688 | } |
688 | else | 689 | else |
689 | { | 690 | { |
690 | *row1=*row2=currentRow(); | 691 | *row1=*row2=currentRow(); |
691 | *col1=*col2=currentColumn(); | 692 | *col1=*col2=currentColumn(); |
692 | } | 693 | } |
693 | } | 694 | } |
694 | 695 | ||
695 | void Sheet::editClear() | 696 | void Sheet::editClear() |
696 | { | 697 | { |
697 | int row1, row2, col1, col2; | 698 | int row1, row2, col1, col2; |
698 | getSelection(&row1, &col1, &row2, &col2); | 699 | getSelection(&row1, &col1, &row2, &col2); |
699 | 700 | ||
700 | int row, col; | 701 | int row, col; |
701 | for (row=row1; row<=row2; ++row) | 702 | for (row=row1; row<=row2; ++row) |
702 | for (col=col1; col<=col2; ++col) | 703 | for (col=col1; col<=col2; ++col) |
703 | { | 704 | { |
704 | setText(row, col, ""); | 705 | setText(row, col, ""); |
705 | slotCellChanged(row, col); | 706 | slotCellChanged(row, col); |
706 | } | 707 | } |
707 | } | 708 | } |
708 | 709 | ||
709 | void Sheet::editCopy() | 710 | void Sheet::editCopy() |
710 | { | 711 | { |
711 | clipboardData.clear(); | 712 | clipboardData.clear(); |
712 | 713 | ||
713 | int row1, row2, col1, col2; | 714 | int row1, row2, col1, col2; |
714 | getSelection(&row1, &col1, &row2, &col2); | 715 | getSelection(&row1, &col1, &row2, &col2); |
715 | 716 | ||
716 | typeCellData *cellData, *newCellData; | 717 | typeCellData *cellData, *newCellData; |
717 | int row, col; | 718 | int row, col; |
718 | for (row=row1; row<=row2; ++row) | 719 | for (row=row1; row<=row2; ++row) |
719 | for (col=col1; col<=col2; ++col) | 720 | for (col=col1; col<=col2; ++col) |
720 | { | 721 | { |
721 | cellData=findCellData(row, col); | 722 | cellData=findCellData(row, col); |
722 | if (cellData) | 723 | if (cellData) |
723 | { | 724 | { |
724 | newCellData=new typeCellData; | 725 | newCellData=new typeCellData; |
725 | *newCellData=*cellData; | 726 | *newCellData=*cellData; |
726 | newCellData->row-=row1; | 727 | newCellData->row-=row1; |
727 | newCellData->col-=col1; | 728 | newCellData->col-=col1; |
728 | clipboardData.append(newCellData); | 729 | clipboardData.append(newCellData); |
729 | } | 730 | } |
730 | } | 731 | } |
731 | } | 732 | } |
732 | 733 | ||
733 | void Sheet::editCut() | 734 | void Sheet::editCut() |
734 | { | 735 | { |
735 | editCopy(); | 736 | editCopy(); |
736 | editClear(); | 737 | editClear(); |
737 | } | 738 | } |
738 | 739 | ||
739 | void Sheet::editPaste(bool onlyContents) | 740 | void Sheet::editPaste(bool onlyContents) |
740 | { | 741 | { |
741 | int row1=currentRow(), col1=currentColumn(); | 742 | int row1=currentRow(), col1=currentColumn(); |
742 | typeCellData *cellData, *tempCellData; | 743 | typeCellData *cellData, *tempCellData; |
743 | 744 | ||
744 | for (tempCellData=clipboardData.first(); tempCellData; tempCellData=clipboardData.next()) | 745 | for (tempCellData=clipboardData.first(); tempCellData; tempCellData=clipboardData.next()) |
745 | { | 746 | { |
746 | cellData=findCellData(tempCellData->row+row1, tempCellData->col+col1); | 747 | cellData=findCellData(tempCellData->row+row1, tempCellData->col+col1); |
747 | if (!cellData) cellData=createCellData(tempCellData->row+row1, tempCellData->col+col1); | 748 | if (!cellData) cellData=createCellData(tempCellData->row+row1, tempCellData->col+col1); |
748 | if (cellData) | 749 | if (cellData) |
749 | { | 750 | { |
750 | if (onlyContents) | 751 | if (onlyContents) |
751 | cellData->data=tempCellData->data; | 752 | cellData->data=tempCellData->data; |
752 | else | 753 | else |
753 | { | 754 | { |
754 | *cellData=*tempCellData; | 755 | *cellData=*tempCellData; |
755 | cellData->row+=row1; | 756 | cellData->row+=row1; |
756 | cellData->col+=col1; | 757 | cellData->col+=col1; |
757 | } | 758 | } |
758 | setText(cellData->row, cellData->col, dataParser(cellData->data)); | 759 | setText(cellData->row, cellData->col, dataParser(cellData->data)); |
759 | emit sheetModified(); | 760 | emit sheetModified(); |
760 | } | 761 | } |
761 | } | 762 | } |
762 | } | 763 | } |
763 | 764 | ||
764 | void Sheet::insertRows(int no, bool allColumns) | 765 | void Sheet::insertRows(int no, bool allColumns) |
765 | { | 766 | { |
766 | setNumRows(numRows()+no); | 767 | setNumRows(numRows()+no); |
767 | 768 | ||
768 | typeCellData *tempCellData; | 769 | typeCellData *tempCellData; |
769 | int row=currentRow(), col=currentColumn(); | 770 | int row=currentRow(), col=currentColumn(); |
770 | 771 | ||
771 | for (tempCellData=sheetData.first(); tempCellData; tempCellData=sheetData.next()) | 772 | for (tempCellData=sheetData.first(); tempCellData; tempCellData=sheetData.next()) |
772 | if (tempCellData->row>=row && (allColumns || tempCellData->col==col)) | 773 | if (tempCellData->row>=row && (allColumns || tempCellData->col==col)) |
773 | { | 774 | { |
774 | clearCell(tempCellData->row, tempCellData->col); | 775 | clearCell(tempCellData->row, tempCellData->col); |
775 | tempCellData->row+=no; | 776 | tempCellData->row+=no; |
776 | } | 777 | } |
777 | for (tempCellData=sheetData.first(); tempCellData; tempCellData=sheetData.next()) | 778 | for (tempCellData=sheetData.first(); tempCellData; tempCellData=sheetData.next()) |
778 | if (tempCellData->row>=row && (allColumns || tempCellData->col==col)) | 779 | if (tempCellData->row>=row && (allColumns || tempCellData->col==col)) |
779 | { | 780 | { |
780 | updateCell(tempCellData->row-no, tempCellData->col); | 781 | updateCell(tempCellData->row-no, tempCellData->col); |
781 | setText(tempCellData->row, tempCellData->col, dataParser(tempCellData->data)); | 782 | setText(tempCellData->row, tempCellData->col, dataParser(tempCellData->data)); |
782 | } | 783 | } |
783 | emit sheetModified(); | 784 | emit sheetModified(); |
784 | } | 785 | } |
785 | 786 | ||
786 | void Sheet::insertColumns(int no, bool allRows) | 787 | void Sheet::insertColumns(int no, bool allRows) |
787 | { | 788 | { |
788 | int noCols=numCols(); | 789 | int noCols=numCols(); |
789 | int newCols=noCols+no; | 790 | int newCols=noCols+no; |
790 | setNumCols(newCols); | 791 | setNumCols(newCols); |
791 | for (int i=noCols; i<newCols; ++i) | 792 | for (int i=noCols; i<newCols; ++i) |
792 | horizontalHeader()->setLabel(i, getHeaderString(i+1), DEFAULT_COL_WIDTH); | 793 | horizontalHeader()->setLabel(i, getHeaderString(i+1), DEFAULT_COL_WIDTH); |
793 | 794 | ||
794 | typeCellData *tempCellData; | 795 | typeCellData *tempCellData; |
795 | int col=currentColumn(), row=currentRow(); | 796 | int col=currentColumn(), row=currentRow(); |
796 | 797 | ||
797 | for (tempCellData=sheetData.first(); tempCellData; tempCellData=sheetData.next()) | 798 | for (tempCellData=sheetData.first(); tempCellData; tempCellData=sheetData.next()) |
798 | if (tempCellData->col>=col && (allRows || tempCellData->row==row)) | 799 | if (tempCellData->col>=col && (allRows || tempCellData->row==row)) |
799 | { | 800 | { |
800 | clearCell(tempCellData->row, tempCellData->col); | 801 | clearCell(tempCellData->row, tempCellData->col); |
801 | tempCellData->col+=no; | 802 | tempCellData->col+=no; |
802 | } | 803 | } |
803 | for (tempCellData=sheetData.first(); tempCellData; tempCellData=sheetData.next()) | 804 | for (tempCellData=sheetData.first(); tempCellData; tempCellData=sheetData.next()) |
804 | if (tempCellData->col>=col && (allRows || tempCellData->row==row)) | 805 | if (tempCellData->col>=col && (allRows || tempCellData->row==row)) |
805 | { | 806 | { |
806 | updateCell(tempCellData->row, tempCellData->col-no); | 807 | updateCell(tempCellData->row, tempCellData->col-no); |
807 | setText(tempCellData->row, tempCellData->col, dataParser(tempCellData->data)); | 808 | setText(tempCellData->row, tempCellData->col, dataParser(tempCellData->data)); |
808 | } | 809 | } |
809 | emit sheetModified(); | 810 | emit sheetModified(); |
810 | } | 811 | } |
811 | 812 | ||
812 | void Sheet::dataFindReplace(const QString &findStr, const QString &replaceStr, bool matchCase, bool allCells, bool entireCell, bool replace, bool replaceAll) | 813 | void Sheet::dataFindReplace(const QString &findStr, const QString &replaceStr, bool matchCase, bool allCells, bool entireCell, bool replace, bool replaceAll) |
813 | { | 814 | { |
814 | typeCellData *tempCellData; | 815 | typeCellData *tempCellData; |
815 | int row1, col1, row2, col2; | 816 | int row1, col1, row2, col2; |
816 | getSelection(&row1, &col1, &row2, &col2); | 817 | getSelection(&row1, &col1, &row2, &col2); |
817 | bool found=FALSE; | 818 | bool found=FALSE; |
818 | 819 | ||
819 | for (tempCellData=sheetData.first(); tempCellData; tempCellData=sheetData.next()) | 820 | for (tempCellData=sheetData.first(); tempCellData; tempCellData=sheetData.next()) |
820 | if (allCells || (tempCellData->row>=row1 && tempCellData->row<=row2 && tempCellData->col>=col1 && tempCellData->col<=col2)) | 821 | if (allCells || (tempCellData->row>=row1 && tempCellData->row<=row2 && tempCellData->col>=col1 && tempCellData->col<=col2)) |
821 | { | 822 | { |
822 | QTableItem *cellItem=item(tempCellData->row, tempCellData->col); | 823 | QTableItem *cellItem=item(tempCellData->row, tempCellData->col); |
823 | if (cellItem && (entireCell ? (matchCase ? cellItem->text()==findStr : cellItem->text().upper()==findStr.upper()) : cellItem->text().find(findStr, 0, matchCase)>=0)) | 824 | if (cellItem && (entireCell ? (matchCase ? cellItem->text()==findStr : cellItem->text().upper()==findStr.upper()) : cellItem->text().find(findStr, 0, matchCase)>=0)) |
824 | { | 825 | { |
825 | if (!found) | 826 | if (!found) |
826 | { | 827 | { |
827 | found=TRUE; | 828 | found=TRUE; |
828 | clearSelection(); | 829 | clearSelection(); |
829 | } | 830 | } |
830 | setCurrentCell(tempCellData->row, tempCellData->col); | 831 | setCurrentCell(tempCellData->row, tempCellData->col); |
831 | if (replace) | 832 | if (replace) |
832 | { | 833 | { |
833 | tempCellData->data=cellItem->text().replace(QRegExp(findStr, matchCase), replaceStr); | 834 | tempCellData->data=cellItem->text().replace(QRegExp(findStr, matchCase), replaceStr); |
834 | setText(tempCellData->row, tempCellData->col, dataParser(tempCellData->data)); | 835 | setText(tempCellData->row, tempCellData->col, dataParser(tempCellData->data)); |
835 | } | 836 | } |
836 | if (!replace || !replaceAll) break; | 837 | if (!replace || !replaceAll) break; |
837 | } | 838 | } |
838 | } | 839 | } |
839 | 840 | ||
840 | if (found) | 841 | if (found) |
841 | { | 842 | { |
842 | if (replace) | 843 | if (replace) |
843 | slotCellChanged(currentRow(), currentColumn()); | 844 | slotCellChanged(currentRow(), currentColumn()); |
844 | } | 845 | } |
845 | else | 846 | else |
846 | QMessageBox::warning(this, tr("Error"), tr("Search key not found!")); | 847 | QMessageBox::warning(this, tr("Error"), tr("Search key not found!")); |
847 | } | 848 | } |
848 | 849 | ||
849 | // | 850 | // |
850 | // Static functions | 851 | // Static functions |
851 | // | 852 | // |
852 | 853 | ||
853 | QString Sheet::getHeaderString(int section) | 854 | QString Sheet::getHeaderString(int section) |
854 | { | 855 | { |
855 | if (section<1) return ""; | 856 | if (section<1) return ""; |
856 | return getHeaderString((section-1)/26)+QChar('A'+(section-1)%26); | 857 | return getHeaderString((section-1)/26)+QChar('A'+(section-1)%26); |
857 | } | 858 | } |
858 | 859 | ||
859 | int Sheet::getHeaderColumn(const QString §ion) | 860 | int Sheet::getHeaderColumn(const QString §ion) |
860 | { | 861 | { |
861 | if (section.isEmpty()) return 0; | 862 | if (section.isEmpty()) return 0; |
862 | return (section[section.length()-1]-'A'+1)+getHeaderColumn(section.left(section.length()-1))*26; | 863 | return (section[section.length()-1]-'A'+1)+getHeaderColumn(section.left(section.length()-1))*26; |
863 | } | 864 | } |