1/* This file is part of the KDE project
2 Copyright 2008 Stefan Nikolaus <stefan.nikolaus@kdemail.net>
3 Copyright 2005 Raphael Langerhorst <raphael.langerhorst@kdemail.net>
4 Copyright 2003 Philipp Müller <philipp.mueller@gmx.de>
5 Copyright 1998, 1999 Torben Weis <weis@kde.org>
6
7 This library is free software; you can redistribute it and/or
8 modify it under the terms of the GNU Library General Public
9 License as published by the Free Software Foundation; either
10 version 2 of the License, or (at your option) any later version.
11
12 This library is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Library General Public License for more details.
16
17 You should have received a copy of the GNU Library General Public License
18 along with this library; see the file COPYING.LIB. If not, write to
19 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
20 Boston, MA 02110-1301, USA.
21*/
22
23// Local
24#include "PrintSettings.h"
25
26// KSpread
27#include "calligra_sheets_limits.h"
28#include "Region.h"
29
30// Calligra
31#include <KoGenStyles.h>
32#include <KoPageLayout.h>
33#include <KoUnit.h>
34
35// Qt
36#include <QSize>
37
38using namespace Calligra::Sheets;
39
40class PrintSettings::Private
41{
42public:
43 KoPageLayout pageLayout;
44 bool printGrid : 1;
45 bool printCharts : 1;
46 bool printObjects : 1;
47 bool printGraphics : 1;
48 bool printCommentIndicator : 1;
49 bool printFormulaIndicator : 1;
50 bool printHeaders : 1;
51 bool printZeroValues : 1;
52 bool centerHorizontally : 1;
53 bool centerVertically : 1;
54 PageOrder pageOrder;
55 Region printRegion;
56 double zoom;
57 QSize pageLimits;
58 QPair<int, int> repeatedColumns;
59 QPair<int, int> repeatedRows;
60
61public:
62 void calculatePageDimensions();
63};
64
65void PrintSettings::Private::calculatePageDimensions()
66{
67 if (pageLayout.format != KoPageFormat::CustomSize) {
68 pageLayout.width = MM_TO_POINT(KoPageFormat::width(pageLayout.format, pageLayout.orientation));
69 pageLayout.height = MM_TO_POINT(KoPageFormat::height(pageLayout.format, pageLayout.orientation));
70 }
71}
72
73PrintSettings::PrintSettings()
74 : d(new Private)
75{
76 d->printGrid = false;
77 d->printCharts = true;
78 d->printObjects = true;
79 d->printGraphics = true;
80 d->printCommentIndicator = false;
81 d->printFormulaIndicator = false;
82 d->printHeaders = true;
83 d->printZeroValues = false;
84 d->centerHorizontally = false;
85 d->centerVertically = false;
86 d->pageOrder = LeftToRight;
87 d->printRegion = Region(1, 1, KS_colMax, KS_rowMax);
88 d->zoom = 1.0;
89}
90
91PrintSettings::PrintSettings(const PrintSettings& other)
92 : d(new Private)
93{
94 d->pageLayout = other.d->pageLayout;
95 d->printGrid = other.d->printGrid;
96 d->printCharts = other.d->printCharts;
97 d->printObjects = other.d->printObjects;
98 d->printGraphics = other.d->printGraphics;
99 d->printCommentIndicator = other.d->printCommentIndicator;
100 d->printFormulaIndicator = other.d->printFormulaIndicator;
101 d->printHeaders = other.d->printHeaders;
102 d->printZeroValues = other.d->printZeroValues;
103 d->centerHorizontally = other.d->centerHorizontally;
104 d->centerVertically = other.d->centerVertically;
105 d->pageOrder = other.d->pageOrder;
106 d->printRegion = other.d->printRegion;
107 d->zoom = other.d->zoom;
108 d->pageLimits = other.d->pageLimits;
109 d->repeatedColumns = other.d->repeatedColumns;
110 d->repeatedRows = other.d->repeatedRows;
111}
112
113PrintSettings::~PrintSettings()
114{
115 delete d;
116}
117
118const KoPageLayout& PrintSettings::pageLayout() const
119{
120 return d->pageLayout;
121}
122
123void PrintSettings::setPageLayout(const KoPageLayout& pageLayout)
124{
125 d->pageLayout = pageLayout;
126}
127
128void PrintSettings::setPageFormat(KoPageFormat::Format format)
129{
130 d->pageLayout.format = format;
131 d->calculatePageDimensions();
132}
133
134void PrintSettings::setPageOrientation(KoPageFormat::Orientation orientation)
135{
136 d->pageLayout.orientation = orientation;
137 d->calculatePageDimensions();
138}
139
140QString PrintSettings::paperFormatString() const
141{
142 if (d->pageLayout.format == KoPageFormat::CustomSize) {
143 QString tmp;
144 tmp.sprintf("%fx%f", d->pageLayout.width, d->pageLayout.height);
145 return tmp;
146 }
147 return KoPageFormat::formatString(d->pageLayout.format);
148}
149
150QString PrintSettings::orientationString() const
151{
152 switch (d->pageLayout.orientation) {
153 case QPrinter::Portrait:
154 default:
155 return "Portrait";
156 case QPrinter::Landscape:
157 return "Landscape";
158 }
159}
160
161double PrintSettings::printWidth() const
162{
163 return d->pageLayout.width - d->pageLayout.leftMargin - d->pageLayout.rightMargin;
164}
165
166double PrintSettings::printHeight() const
167{
168 return d->pageLayout.height - d->pageLayout.topMargin - d->pageLayout.bottomMargin;
169}
170
171PrintSettings::PageOrder PrintSettings::pageOrder() const
172{
173 return d->pageOrder;
174}
175
176void PrintSettings::setPageOrder(PageOrder order)
177{
178 d->pageOrder = order;
179}
180
181bool PrintSettings::printGrid() const
182{
183 return d->printGrid;
184}
185
186void PrintSettings::setPrintGrid(bool printGrid)
187{
188 d->printGrid = printGrid;
189}
190
191bool PrintSettings::printCharts() const
192{
193 return d->printCharts;
194}
195
196void PrintSettings::setPrintCharts(bool printCharts)
197{
198 d->printCharts = printCharts;
199}
200
201bool PrintSettings::printObjects() const
202{
203 return d->printObjects;
204}
205
206void PrintSettings::setPrintObjects(bool printObjects)
207{
208 d->printObjects = printObjects;
209}
210
211bool PrintSettings::printGraphics() const
212{
213 return d->printGraphics;
214}
215
216void PrintSettings::setPrintGraphics(bool printGraphics)
217{
218 d->printGraphics = printGraphics;
219}
220
221bool PrintSettings::printCommentIndicator() const
222{
223 return d->printCommentIndicator;
224}
225
226void PrintSettings::setPrintCommentIndicator(bool printCommentIndicator)
227{
228 d->printCommentIndicator = printCommentIndicator;
229}
230
231bool PrintSettings::printFormulaIndicator() const
232{
233 return d->printFormulaIndicator;
234}
235
236void PrintSettings::setPrintFormulaIndicator(bool printFormulaIndicator)
237{
238 d->printFormulaIndicator = printFormulaIndicator;
239}
240
241bool PrintSettings::printHeaders() const
242{
243 return d->printHeaders;
244}
245
246void PrintSettings::setPrintHeaders(bool printHeaders)
247{
248 d->printHeaders = printHeaders;
249}
250
251bool PrintSettings::printZeroValues() const
252{
253 return d->printZeroValues;
254}
255
256void PrintSettings::setPrintZeroValues(bool printZeroValues)
257{
258 d->printZeroValues = printZeroValues;
259}
260
261bool PrintSettings::centerHorizontally() const
262{
263 return d->centerHorizontally;
264}
265
266void PrintSettings::setCenterHorizontally(bool center)
267{
268 d->centerHorizontally = center;
269}
270
271bool PrintSettings::centerVertically() const
272{
273 return d->centerVertically;
274}
275
276void PrintSettings::setCenterVertically(bool center)
277{
278 d->centerVertically = center;
279}
280
281const Calligra::Sheets::Region& PrintSettings::printRegion() const
282{
283 return d->printRegion;
284}
285
286void PrintSettings::setPrintRegion(const Region& region)
287{
288 d->printRegion = region;
289}
290
291void PrintSettings::addPrintRange(const QRect& range)
292{
293 d->printRegion.add(range);
294}
295
296void PrintSettings::removePrintRange(const QRect& range)
297{
298 d->printRegion.sub(range, 0);
299}
300
301double PrintSettings::zoom() const
302{
303 return d->zoom;
304}
305
306void PrintSettings::setZoom(double zoom)
307{
308 d->zoom = zoom;
309}
310
311const QSize& PrintSettings::pageLimits() const
312{
313 return d->pageLimits;
314}
315
316void PrintSettings::setPageLimits(const QSize& pageLimits)
317{
318 d->pageLimits = pageLimits;
319}
320
321const QPair<int, int>& PrintSettings::repeatedColumns() const
322{
323 return d->repeatedColumns;
324}
325
326void PrintSettings::setRepeatedColumns(const QPair<int, int>& repeatedColumns)
327{
328 d->repeatedColumns = repeatedColumns;
329 kDebug() << repeatedColumns;
330}
331
332const QPair<int, int>& PrintSettings::repeatedRows() const
333{
334 return d->repeatedRows;
335}
336
337void PrintSettings::setRepeatedRows(const QPair<int, int>& repeatedRows)
338{
339 d->repeatedRows = repeatedRows;
340}
341
342QString PrintSettings::saveOdfPageLayout(KoGenStyles &mainStyles,
343 bool formulas, bool zeros)
344{
345 // Create a page layout style.
346 // 15.2.1 Page Size
347 // 15.2.4 Print Orientation
348 // 15.2.5 Margins
349 KoGenStyle pageLayout = d->pageLayout.saveOdf();
350
351 // 15.2.13 Print
352 QString printParameter;
353 if (d->printHeaders) {
354 printParameter = "headers ";
355 }
356 if (d->printGrid) {
357 printParameter += "grid ";
358 }
359 /* if (d->printComments) {
360 printParameter += "annotations ";
361 }*/
362 if (d->printObjects) {
363 printParameter += "objects ";
364 }
365 if (d->printCharts) {
366 printParameter += "charts ";
367 }
368 /* if (d->printDrawings) {
369 printParameter += "drawings ";
370 }*/
371 if (formulas) {
372 printParameter += "formulas ";
373 }
374 if (zeros) {
375 printParameter += "zero-values ";
376 }
377 if (!printParameter.isEmpty()) {
378 printParameter += "drawings"; //default print style attributes in OO
379 pageLayout.addProperty("style:print", printParameter);
380 }
381
382 // 15.2.14 Print Page Order
383 const QString pageOrder = (d->pageOrder == LeftToRight) ? "ltr" : "ttb";
384 pageLayout.addProperty("style:print-page-order", pageOrder);
385
386 // 15.2.16 Scale
387 // FIXME handle cases where only one direction is limited
388 if (d->pageLimits.width() > 0 && d->pageLimits.height() > 0) {
389 const int pages = d->pageLimits.width() * d->pageLimits.height();
390 pageLayout.addProperty("style:scale-to-pages", pages);
391 } else if (d->zoom != 1.0) {
392 pageLayout.addProperty("style:scale-to", qRound(d->zoom * 100)); // in %
393 }
394
395 // 15.2.17 Table Centering
396 if (d->centerHorizontally && d->centerVertically) {
397 pageLayout.addProperty("style:table-centering", "both");
398 } else if (d->centerHorizontally) {
399 pageLayout.addProperty("style:table-centering", "horizontal");
400 } else if (d->centerVertically) {
401 pageLayout.addProperty("style:table-centering", "vertical");
402 } else {
403 pageLayout.addProperty("style:table-centering", "none");
404 }
405
406 // this is called from Sheet::saveOdfSheetStyleName for writing the SytleMaster so
407 // the style has to be in the styles.xml file and only there
408 pageLayout.setAutoStyleInStylesDotXml(true);
409
410 return mainStyles.insert(pageLayout, "pm");
411}
412
413void PrintSettings::operator=(const PrintSettings & other)
414{
415 d->pageLayout = other.d->pageLayout;
416 d->printGrid = other.d->printGrid;
417 d->printCharts = other.d->printCharts;
418 d->printObjects = other.d->printObjects;
419 d->printGraphics = other.d->printGraphics;
420 d->printCommentIndicator = other.d->printCommentIndicator;
421 d->printFormulaIndicator = other.d->printFormulaIndicator;
422 d->printHeaders = other.d->printHeaders;
423 d->printZeroValues = other.d->printZeroValues;
424 d->centerHorizontally = other.d->centerHorizontally;
425 d->centerVertically = other.d->centerVertically;
426 d->pageOrder = other.d->pageOrder;
427 d->printRegion = other.d->printRegion;
428 d->zoom = other.d->zoom;
429 d->pageLimits = other.d->pageLimits;
430 d->repeatedColumns = other.d->repeatedColumns;
431 d->repeatedRows = other.d->repeatedRows;
432}
433
434bool PrintSettings::operator==(const PrintSettings& other) const
435{
436 if (d->pageLayout != other.d->pageLayout)
437 return false;
438 if (d->printGrid != other.d->printGrid)
439 return false;
440 if (d->printCharts != other.d->printCharts)
441 return false;
442 if (d->printObjects != other.d->printObjects)
443 return false;
444 if (d->printGraphics != other.d->printGraphics)
445 return false;
446 if (d->printCommentIndicator != other.d->printCommentIndicator)
447 return false;
448 if (d->printFormulaIndicator != other.d->printFormulaIndicator)
449 return false;
450 if (d->printHeaders != other.d->printHeaders)
451 return false;
452 if (d->printZeroValues != other.d->printZeroValues)
453 return false;
454 if (d->centerHorizontally != other.d->centerHorizontally)
455 return false;
456 if (d->centerVertically != other.d->centerVertically)
457 return false;
458 if (d->pageOrder != other.d->pageOrder)
459 return false;
460 if (d->printRegion != other.d->printRegion)
461 return false;
462 if (d->zoom != other.d->zoom)
463 return false;
464 if (d->pageLimits != other.d->pageLimits)
465 return false;
466 if (d->repeatedColumns != other.d->repeatedColumns)
467 return false;
468 if (d->repeatedRows != other.d->repeatedRows)
469 return false;
470 return true;
471}
472