filters

KSpreadLeader.cpp
1/*
2This file is part of the KDE project
3Copyright (C) 2002 Fred Malabre <fmalabre@yahoo.com>
4
5This library is free software; you can redistribute it and/or
6modify it under the terms of the GNU Library General Public
7License as published by the Free Software Foundation; either
8version 2 of the License, or (at your option) any later version.
9
10This library is distributed in the hope that it will be useful,
11but WITHOUT ANY WARRANTY; without even the implied warranty of
12MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13Library General Public License for more details.
14
15You should have received a copy of the GNU Library General Public License
16along with this library; see the file COPYING.LIB. If not, write to
17the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 * Boston, MA 02110-1301, USA.
19*/
20
21#include "KSpreadLeader.h"
22
23using namespace KSpread;
24
25Leader::Leader(KoFilterChain *filterChain) {
26 m_worker = NULL;
27 m_filterChain = filterChain;
28}
29
30
31Leader::Leader(KoFilterChain *filterChain, KSpreadBaseWorker *newWorker) {
32 m_worker = newWorker;
33 m_filterChain = filterChain;
34}
35
36
39
40
41KSpreadBaseWorker *Leader::getWorker() const {
42 return m_worker;
43}
44
45
46void Leader::setWorker(KSpreadBaseWorker *newWorker) {
47 m_worker = newWorker;
48}
49
50
51KoFilter::ConversionStatus Leader::convert() {
52 KoFilter::ConversionStatus status;
53
54 // Validate the filter chain and the worker
55 if (!m_filterChain) {
56 kdWarning(30508) << "koFilterChain is NULL!" << endl;
57 return KoFilter::StupidError;
58 }
59 if (!m_worker) {
60 kdWarning(30508) << "the KSpreadWorker is NULL!" << endl;
61 return KoFilter::StupidError;
62 }
63
64 // Gather data about the filter itself
65 KSpreadFilterProperty docProperty;
66 docProperty["outputfile"] = m_filterChain->outputFile();
67 status = m_worker->startDocument(docProperty);
68 if (status != KoFilter::OK)
69 return status;
70
71 // Get the document in memory
72 KSpreadDoc *document = (KSpreadDoc *) m_filterChain->inputDocument();
73 if (!document) {
74 kdWarning(30508) << "the KSpreadDoc is NULL!" << endl;
75 return KoFilter::StupidError;
76 }
77 if ( !::tqt_cast<const KSpread::Doc *>( document ) ) {
78 kdWarning(30508) << "the document is not a KSpreadDoc!" << endl;
79 return KoFilter::StupidError;
80 }
81 if (document->mimeType() != "application/x-kspread") {
82 kdWarning(30508) << "the mime type document is not application/x-kspread!" << endl;
83 return KoFilter::StupidError;
84 }
85 KoDocumentInfo *info = document->documentInfo();
86 if (!document) {
87 kdWarning(30508) << "the KoDocumentInfo is NULL!" << endl;
88 return KoFilter::StupidError;
89 }
90
91 // Gather data about the document info
92 status = doInfo(info);
93 if (status != KoFilter::OK)
94 return status;
95
96 // Gather data about the spread book
97 status = doSpreadBook(document);
98 if (status != KoFilter::OK)
99 return status;
100
101 // Gather data about the spread sheet
102 KSpreadSheet *spreadSheet = document->map()->firstTable();
103 while (spreadSheet != 0) {
104 status = doSpreadSheet(spreadSheet);
105 if (status != KoFilter::OK)
106 return status;
107
108 // Gather data about the cell
109 for (int row = 1; row <= m_maxCellRow; ++row) {
110 for (int column = 1; column <= m_maxCellColumn; ++column) {
111 Cell*spreadCell = spreadSheet->cellAt(column, row);
112 status = doSpreadCell(spreadCell, column, row);
113 if (status != KoFilter::OK)
114 return status;
115 }
116 }
117
118 spreadSheet = document->map()->nextTable();
119 }
120
121 return status;
122}
123
124
125KoFilter::ConversionStatus Leader::doInfo(KoDocumentInfo *info) {
126 KoFilter::ConversionStatus status;
127
128#if 0 // this was never used, it's been removed now
129 // Gather data about the document log
130 KSpreadFilterProperty docInfoLogProperty;
131 KoDocumentInfoLog *infoLog = (KoDocumentInfoLog *) info->page("log");
132 docInfoLogProperty["oldlog"] = infoLog->oldLog();
133 docInfoLogProperty["newlog"] = infoLog->newLog();
134 status = m_worker->startInfoLog(docInfoLogProperty);
135 if (status != KoFilter::OK)
136 return status;
137#endif
138
139 // Gather data about the document author
140 KSpreadFilterProperty docInfoAuthorProperty;
141 KoDocumentInfoAuthor *infoAuthor = (KoDocumentInfoAuthor *) info->page("author");
142 docInfoAuthorProperty["fullname"] = infoAuthor->fullName();
143 docInfoAuthorProperty["initial"] = infoAuthor->initial();
144 docInfoAuthorProperty["title"] = infoAuthor->title();
145 docInfoAuthorProperty["company"] = infoAuthor->company();
146 docInfoAuthorProperty["email"] = infoAuthor->email();
147 docInfoAuthorProperty["telephone"] = infoAuthor->telephone();
148 docInfoAuthorProperty["fax"] = infoAuthor->fax();
149 docInfoAuthorProperty["country"] = infoAuthor->country();
150 docInfoAuthorProperty["postalcode"] = infoAuthor->postalCode();
151 docInfoAuthorProperty["city"] = infoAuthor->city();
152 docInfoAuthorProperty["street"] = infoAuthor->street();
153 status = m_worker->startInfoAuthor(docInfoAuthorProperty);
154 if (status != KoFilter::OK)
155 return status;
156
157 // Gather data about the document about
158 KSpreadFilterProperty docInfoAboutProperty;
159 KoDocumentInfoAbout *infoAbout = (KoDocumentInfoAbout *) info->page("about");
160 docInfoAboutProperty["title"] = infoAbout->title();
161 docInfoAboutProperty["author"] = infoAbout->abstract();
162 status = m_worker->startInfoAbout(docInfoAboutProperty);
163 return status;
164}
165
166
167KoFilter::ConversionStatus Leader::doSpreadBook(KSpreadDoc *document) {
168 KSpreadFilterProperty docSpreadBookProperty;
169 docSpreadBookProperty["spreadsheetcount"] = TQString::number(document->map()->count());
170 docSpreadBookProperty["decimalsymbol"] = document->locale()->decimalSymbol();
171 docSpreadBookProperty["thousandsseparator"] = document->locale()->thousandsSeparator();
172 docSpreadBookProperty["currencysymbol"] = document->locale()->currencySymbol();
173 docSpreadBookProperty["monetarydecimalsymbol"] = document->locale()->monetaryDecimalSymbol();
174 docSpreadBookProperty["monetarythousandsseparator"] = document->locale()->monetaryThousandsSeparator();
175 docSpreadBookProperty["positivesign"] = document->locale()->positiveSign();
176 docSpreadBookProperty["negativesign"] = document->locale()->negativeSign();
177 docSpreadBookProperty["fracdigits"] = TQString::number(document->locale()->fracDigits());
178 docSpreadBookProperty["positiveprefixcurrencysymbol"] = (document->locale()->positivePrefixCurrencySymbol()==0?"false":"true");
179 docSpreadBookProperty["negativeprefixcurrencysymbol"] = (document->locale()->negativePrefixCurrencySymbol()==0?"false":"true");
180 docSpreadBookProperty["use12clock"] = (document->locale()->use12Clock()==0?"false":"true");
181 docSpreadBookProperty["weekstartsmonday"] = (document->locale()->weekStartsMonday()==0?"false":"true");
182 docSpreadBookProperty["weekstartday"] = TQString::number(document->locale()->weekStartDay());
183 docSpreadBookProperty["language"] = document->locale()->language();
184 docSpreadBookProperty["country"] = document->locale()->country();
185 docSpreadBookProperty["encoding"] = document->locale()->encoding();
186 docSpreadBookProperty["dateformat"] = document->locale()->dateFormat();
187 docSpreadBookProperty["dateformatshort"] = document->locale()->dateFormatShort();
188 docSpreadBookProperty["timeformat"] = document->locale()->timeFormat();
189 docSpreadBookProperty["defaultlanguage"] = TDELocale::defaultLanguage();
190 docSpreadBookProperty["defaultcountry"] = TDELocale::defaultCountry();
191 docSpreadBookProperty["defaultgridpencolorname"] = document->defaultGridPen().color().name();
192 docSpreadBookProperty["defaultgridpencolorred"] = TQString::number(document->defaultGridPen().color().red());
193 docSpreadBookProperty["defaultgridpencolorgreen"] = TQString::number(document->defaultGridPen().color().green());
194 docSpreadBookProperty["defaultgridpencolorblue"] = TQString::number(document->defaultGridPen().color().blue());
195 docSpreadBookProperty["defaultgridpenwidth"] = TQString::number(document->defaultGridPen().width());
196 docSpreadBookProperty["showverticalscrollbar"] = (document->getShowVerticalScrollBar()==0?"false":"true");
197 docSpreadBookProperty["showhorizontalscrollbar"] = (document->getShowHorizontalScrollBar()==0?"false":"true");
198 docSpreadBookProperty["showcolheader"] = (document->getShowColHeader()==0?"false":"true");
199 docSpreadBookProperty["showrowheader"] = (document->getShowRowHeader()==0?"false":"true");
200 docSpreadBookProperty["indentvalue"] = TQString::number(document->getIndentValue());
201 docSpreadBookProperty["movetovalue"] = TQString::number(document->getMoveToValue());
202 docSpreadBookProperty["showmessageerror"] = (document->getShowMessageError()==0?"false":"true");
203 docSpreadBookProperty["showtabbar"] = (document->getShowTabBar()==0?"false":"true");
204 docSpreadBookProperty["pagebordercolorname"] = document->pageBorderColor().name();
205 docSpreadBookProperty["pagebordercolorred"] = TQString::number(document->pageBorderColor().red());
206 docSpreadBookProperty["pagebordercolorgreen"] = TQString::number(document->pageBorderColor().green());
207 docSpreadBookProperty["pagebordercolorblue"] = TQString::number(document->pageBorderColor().blue());
208 docSpreadBookProperty["showcommentindicator"] = (document->getShowCommentIndicator()==0?"false":"true");
209 docSpreadBookProperty["showformulabar"] = (document->getShowFormulaBar()==0?"false":"true");
210 docSpreadBookProperty["dontcheckupperword"] = (document->dontCheckUpperWord()==0?"false":"true");
211 docSpreadBookProperty["dontchecktitlecase"] = (document->dontCheckTitleCase()==0?"false":"true");
212 docSpreadBookProperty["showstatusbar"] = (document->getShowStatusBar()==0?"false":"true");
213 docSpreadBookProperty["unitname"] = document->getUnitName();
214 docSpreadBookProperty["syntaxversion"] = TQString::number(document->syntaxVersion());
215 return m_worker->startSpreadBook(docSpreadBookProperty);
216}
217
218
219KoFilter::ConversionStatus Leader::doSpreadSheet(KSpreadSheet *spreadSheet) {
220 KSpreadFilterProperty docSpreadSheetProperty;
221 docSpreadSheetProperty["name"] = spreadSheet->tableName();
222 docSpreadSheetProperty["sizemaxx"] = TQString::number(spreadSheet->sizeMaxX());
223 docSpreadSheetProperty["sizemaxy"] = TQString::number(spreadSheet->sizeMaxY());
224 docSpreadSheetProperty["showgrid"] = (spreadSheet->getShowGrid()==0?"false":"true");
225 docSpreadSheetProperty["showformula"] = (spreadSheet->getShowFormula()==0?"false":"true");
226 docSpreadSheetProperty["showformulaindicator"] = (spreadSheet->getShowFormulaIndicator()==0?"false":"true");
227 docSpreadSheetProperty["lcmode"] = (spreadSheet->getLcMode()==0?"false":"true");
228 docSpreadSheetProperty["autocalc"] = (spreadSheet->getAutoCalc()==0?"false":"true");
229 docSpreadSheetProperty["showcolumnnumber"] = (spreadSheet->getShowColumnNumber()==0?"false":"true");
230 docSpreadSheetProperty["hidezero"] = (spreadSheet->getHideZero()==0?"false":"true");
231 docSpreadSheetProperty["firstletterupper"] = (spreadSheet->getFirstLetterUpper()==0?"false":"true");
232 docSpreadSheetProperty["ishidden"] = (spreadSheet->isHidden()==0?"false":"true");
233 docSpreadSheetProperty["showpageborders"] = (spreadSheet->isShowPageBorders()==0?"false":"true");
234 docSpreadSheetProperty["printablewidth"] = TQString::number(spreadSheet->printableWidth());
235 docSpreadSheetProperty["printableheight"] = TQString::number(spreadSheet->printableHeight());
236 docSpreadSheetProperty["paperwidth"] = TQString::number(spreadSheet->paperWidth());
237 docSpreadSheetProperty["paperheight"] = TQString::number(spreadSheet->paperHeight());
238 docSpreadSheetProperty["leftborder"] = TQString::number(spreadSheet->leftBorder());
239 docSpreadSheetProperty["rightborder"] = TQString::number(spreadSheet->rightBorder());
240 docSpreadSheetProperty["topborder"] = TQString::number(spreadSheet->topBorder());
241 docSpreadSheetProperty["bottomborder"] = TQString::number(spreadSheet->bottomBorder());
242 docSpreadSheetProperty["headleft"] = spreadSheet->headLeft();
243 docSpreadSheetProperty["headmid"] = spreadSheet->headMid();
244 docSpreadSheetProperty["headright"] = spreadSheet->headRight();
245 docSpreadSheetProperty["footleft"] = spreadSheet->footLeft();
246 docSpreadSheetProperty["footmid"] = spreadSheet->footMid();
247 docSpreadSheetProperty["footright"] = spreadSheet->footRight();
248 docSpreadSheetProperty["orientation"] = spreadSheet->orientationString();
249 docSpreadSheetProperty["paperformat"] = spreadSheet->paperFormatString();
250 docSpreadSheetProperty["printgrid"] = (spreadSheet->getPrintGrid()==0?"false":"true");
251 docSpreadSheetProperty["printcomment"] = (spreadSheet->getPrintCommentIndicator()==0?"false":"true");
252 docSpreadSheetProperty["printformula"] = (spreadSheet->getPrintFormulaIndicator()==0?"false":"true");
253 updateMaxCells(spreadSheet);
254 docSpreadSheetProperty["maxcellrow"] = TQString::number(m_maxCellRow);
255 docSpreadSheetProperty["maxcellcolumn"] = TQString::number(m_maxCellColumn);
256 return m_worker->startSpreadSheet(docSpreadSheetProperty);
257}
258
259
260KoFilter::ConversionStatus Leader::doSpreadCell(Cell*spreadCell, int column, int row) {
261 KSpreadFilterProperty docSpreadCellProperty;
262 docSpreadCellProperty["column"] = TQString::number(column);
263 docSpreadCellProperty["row"] = TQString::number(row);
264 docSpreadCellProperty["width"] = TQString::number(spreadCell->dblWidth());
265 docSpreadCellProperty["height"] = TQString::number(spreadCell->dblHeight());
266 docSpreadCellProperty["empty"] = (spreadCell->isEmpty()==0?"false":"true");
267 if (!spreadCell->isEmpty()) {
268 docSpreadCellProperty["text"] = spreadCell->text();
269 docSpreadCellProperty["strouttext"] = spreadCell->strOutText();
270 docSpreadCellProperty["action"] = spreadCell->action();
271 docSpreadCellProperty["date"] = (spreadCell->isDate()==0?"false":"true");
272 docSpreadCellProperty["time"] = (spreadCell->isTime()==0?"false":"true");
273 docSpreadCellProperty["textwidth"] = TQString::number(spreadCell->textWidth());
274 docSpreadCellProperty["textheight"] = TQString::number(spreadCell->textHeight());
275 docSpreadCellProperty["forceextracells"] = (spreadCell->isForceExtraCells()==0?"false":"true");
276 docSpreadCellProperty["mergedxcells"] = TQString::number(spreadCell->mergedXCells());
277 docSpreadCellProperty["mergedycells"] = TQString::number(spreadCell->mergedYCells());
278 docSpreadCellProperty["extraxcells"] = TQString::number(spreadCell->extraXCells());
279 docSpreadCellProperty["extraycells"] = TQString::number(spreadCell->extraYCells());
280 docSpreadCellProperty["extrawidth"] = TQString::number(spreadCell->extraWidth());
281 docSpreadCellProperty["extraheight"] = TQString::number(spreadCell->extraHeight());
282 docSpreadCellProperty["formula"] = (spreadCell->isFormula()==0?"false":"true");
283 docSpreadCellProperty["haserror"] = (spreadCell->hasError()==0?"false":"true");
284 docSpreadCellProperty["alignx"] = TQString::number(spreadCell->defineAlignX());
285 docSpreadCellProperty["name"] = spreadCell->name();
286 docSpreadCellProperty["fullname"] = spreadCell->fullName();
287 docSpreadCellProperty["content"] = TQString::number(spreadCell->content());
288 docSpreadCellProperty["style"] = TQString::number(spreadCell->style());
289 docSpreadCellProperty["valuedate"] = spreadCell->valueDate().toString();
290 docSpreadCellProperty["valuetime"] = spreadCell->valueTime().toString();
291 docSpreadCellProperty["leftborderwidth"] = TQString::number(spreadCell->leftBorderPen(column, row).width());
292 docSpreadCellProperty["leftbordercolorname"] = spreadCell->leftBorderPen(column, row).color().name();
293 docSpreadCellProperty["leftbordercolorred"] = TQString::number(spreadCell->leftBorderPen(column, row).color().red());
294 docSpreadCellProperty["leftbordercolorgreen"] = TQString::number(spreadCell->leftBorderPen(column, row).color().green());
295 docSpreadCellProperty["leftbordercolorblue"] = TQString::number(spreadCell->leftBorderPen(column, row).color().blue());
296 docSpreadCellProperty["topborderwidth"] = TQString::number(spreadCell->topBorderPen(column, row).width());
297 docSpreadCellProperty["topbordercolorname"] = spreadCell->topBorderPen(column, row).color().name();
298 docSpreadCellProperty["topbordercolorred"] = TQString::number(spreadCell->topBorderPen(column, row).color().red());
299 docSpreadCellProperty["topbordercolorgreen"] = TQString::number(spreadCell->topBorderPen(column, row).color().green());
300 docSpreadCellProperty["topbordercolorblue"] = TQString::number(spreadCell->topBorderPen(column, row).color().blue());
301 docSpreadCellProperty["rightborderwidth"] = TQString::number(spreadCell->rightBorderPen(column, row).width());
302 docSpreadCellProperty["rightbordercolorname"] = spreadCell->rightBorderPen(column, row).color().name();
303 docSpreadCellProperty["rightbordercolorred"] = TQString::number(spreadCell->rightBorderPen(column, row).color().red());
304 docSpreadCellProperty["rightbordercolorgreen"] = TQString::number(spreadCell->rightBorderPen(column, row).color().green());
305 docSpreadCellProperty["rightbordercolorblue"] = TQString::number(spreadCell->rightBorderPen(column, row).color().blue());
306 docSpreadCellProperty["bottomborderwidth"] = TQString::number(spreadCell->bottomBorderPen(column, row).width());
307 docSpreadCellProperty["bottombordercolorname"] = spreadCell->bottomBorderPen(column, row).color().name();
308 docSpreadCellProperty["bottombordercolorred"] = TQString::number(spreadCell->bottomBorderPen(column, row).color().red());
309 docSpreadCellProperty["bottombordercolorgreen"] = TQString::number(spreadCell->bottomBorderPen(column, row).color().green());
310 docSpreadCellProperty["bottombordercolorblue"] = TQString::number(spreadCell->bottomBorderPen(column, row).color().blue());
311 docSpreadCellProperty["bgcolorname"] = spreadCell->bgColor(column, row).name();
312 docSpreadCellProperty["bgcolorred"] = TQString::number(spreadCell->bgColor(column, row).red());
313 docSpreadCellProperty["bgcolorgreen"] = TQString::number(spreadCell->bgColor(column, row).green());
314 docSpreadCellProperty["bgcolorblue"] = TQString::number(spreadCell->bgColor(column, row).blue());
315 docSpreadCellProperty["bgbrushstyle"] = TQString::number(spreadCell->backGroundBrush(column, row).style());
316 docSpreadCellProperty["valueempty"] = (spreadCell->value().isEmpty()==0?"false":"true");
317 docSpreadCellProperty["valueboolean"] = (spreadCell->value().isBoolean()==0?"false":"true");
318 docSpreadCellProperty["valueinteger"] = (spreadCell->value().isInteger()==0?"false":"true");
319 docSpreadCellProperty["valuefloat"] = (spreadCell->value().isFloat()==0?"false":"true");
320 docSpreadCellProperty["valuenumber"] = (spreadCell->value().isNumber()==0?"false":"true");
321 docSpreadCellProperty["valuestring"] = (spreadCell->value().isString()==0?"false":"true");
322 docSpreadCellProperty["valueerror"] = (spreadCell->value().isError()==0?"false":"true");
323 docSpreadCellProperty["valueasboolean"] = (spreadCell->value().asBoolean()==0?"false":"true");
324 docSpreadCellProperty["valueasinteger"] = TQString::number(spreadCell->value().asInteger());
325 docSpreadCellProperty["valueasfloat"] = TQString::number(spreadCell->value().asFloat());
326 docSpreadCellProperty["valueasstring"] = spreadCell->value().asString();
327 docSpreadCellProperty["valueasdatetime"] = spreadCell->value().asDateTime().toString();
328 docSpreadCellProperty["valueaserror"] = spreadCell->value().errorMessage();
329 }
330 return m_worker->startSpreadCell(docSpreadCellProperty);
331}
332
333
334void Leader::updateMaxCells(KSpreadSheet *spreadSheet) {
335 m_maxCellColumn = 0;
336 m_maxCellRow = 0;
337
338 int maxColumn = spreadSheet->maxColumn();
339 int maxRow = spreadSheet->maxRow();
340
341 // Go through all the SpreadSheet to find out the minimum rectangle of cells
342 // Maybe we should have something which does that in the KSpreadSheet class,
343 // it would be easy to keep track of this each time a new Cellis instanciated.
344 for (int row = 1; row < maxRow; ++row) {
345 bool usedColumn = false;
346 for (int column = 1; column < maxColumn; ++column) {
347 Cell*cell = spreadSheet->cellAt(column, row);
348 if (!cell->isDefault() && !cell->isEmpty()) {
349 if (column > m_maxCellColumn) {
350 m_maxCellColumn = column;
351 }
352 usedColumn = true;
353 }
354 }
355 if (usedColumn) {
356 m_maxCellRow = row;
357 }
358 }
359}
void setWorker(KSpreadBaseWorker *newWorker)
Set your own implementation of the Worker.
Leader(KoFilterChain *filterChain)
Default constructor.
KoFilter::ConversionStatus convert()
Start the conversion process.
virtual ~Leader()
Default destructor.