File: SheetPrint.cpp

package info (click to toggle)
calligra 1%3A3.2.1%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 260,432 kB
  • sloc: cpp: 650,911; xml: 27,662; python: 6,044; perl: 2,724; yacc: 1,817; ansic: 1,325; sh: 1,277; lex: 1,107; ruby: 1,010; javascript: 495; makefile: 24
file content (512 lines) | stat: -rw-r--r-- 17,728 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
/* This file is part of the KDE project
   Copyright 2007, 2009 Stefan Nikolaus <stefan.nikolaus@kdemail.net>
   Copyright 2005 Raphael Langerhorst <raphael.langerhorst@kdemail.net>
   Copyright 2003 Philipp Müller <philipp.mueller@gmx.de>
   Copyright 1998, 1999 Torben Weis <weis@kde.org>

   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Library General Public
   License as published by the Free Software Foundation; either
   version 2 of the License, or (at your option) any later version.

   This library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Library General Public License for more details.

   You should have received a copy of the GNU Library General Public License
   along with this library; see the file COPYING.LIB.  If not, write to
   the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
   Boston, MA 02110-1301, USA.
*/

// Local
#include "SheetPrint.h"
#include "SheetPrint_p.h"

#include "HeaderFooter.h"
#include "PrintSettings.h"
#include "calligra_sheets_limits.h"
#include "Region.h"
#include "Sheet.h"
#include "SheetsDebug.h"

using namespace Calligra::Sheets;

SheetPrint::SheetPrint(Sheet *sheet)
        : d(new Private(this))
{
    d->m_pSheet = sheet;

    d->m_settings = new PrintSettings();
    d->m_headerFooter = new HeaderFooter(sheet);

    d->m_maxCheckedNewPageX = 0;
    d->m_maxCheckedNewPageY = 0;
    d->m_dPrintRepeatColumnsWidth = 0.0;
    d->m_dPrintRepeatRowsHeight = 0.0;
}

SheetPrint::SheetPrint(const SheetPrint &other)
        : d(new Private(this))
{
    d->m_pSheet = other.d->m_pSheet;

    d->m_settings = new PrintSettings(*other.d->m_settings);
    d->m_headerFooter = new HeaderFooter(*other.d->m_headerFooter);

    d->m_maxCheckedNewPageX = other.d->m_maxCheckedNewPageX;
    d->m_maxCheckedNewPageY = other.d->m_maxCheckedNewPageY;
    d->m_dPrintRepeatColumnsWidth = other.d->m_dPrintRepeatColumnsWidth;
    d->m_dPrintRepeatRowsHeight = other.d->m_dPrintRepeatRowsHeight;
    d->m_lnewPageListX = other.d->m_lnewPageListX;
    d->m_lnewPageListY = other.d->m_lnewPageListY;
}

SheetPrint::~SheetPrint()
{
    delete d->m_headerFooter;
    delete d->m_settings;
    delete d;
}

PrintSettings *SheetPrint::settings() const
{
    return d->m_settings;
}

void SheetPrint::setSettings(const PrintSettings &settings, bool force)
{
    // Relayout forced?
    if (force) {
        *d->m_settings = settings;
        d->updateRepeatedColumnsWidth();
        d->updateRepeatedRowsHeight();
        const QSize pageLimits = settings.pageLimits();
        const QSize usedArea = d->m_pSheet->usedArea(true).size();
        if (pageLimits.width() > 0) {
            d->calculateZoomForPageLimitX();
        } else {
            updateHorizontalPageParameters(0);
            d->calculateHorizontalPageParameters(usedArea.width());
        }
        if (pageLimits.height() > 0) {
            d->calculateZoomForPageLimitY();
        } else {
            updateVerticalPageParameters(0);
            d->calculateVerticalPageParameters(usedArea.height());
        }
        return;
    }

    const KoPageLayout oldPageLayout = d->m_settings->pageLayout();
    const KoPageLayout pageLayout = settings.pageLayout();
    const QRect oldPrintRange = d->m_settings->printRegion().lastRange();
    const QRect printRange = settings.printRegion().lastRange();
    const QSize oldPageLimits = d->m_settings->pageLimits();
    const QSize pageLimits = settings.pageLimits();
    const QPair<int, int> oldRepeatedColumns = d->m_settings->repeatedColumns();
    const QPair<int, int> repeatedColumns = settings.repeatedColumns();
    const QPair<int, int> oldRepeatedRows = d->m_settings->repeatedRows();
    const QPair<int, int> repeatedRows = settings.repeatedRows();

    const bool pageWidthChanged = oldPageLayout.width != pageLayout.width;
    const bool pageHeightChanged = oldPageLayout.height != pageLayout.height;
    const bool horizontalLimitChanged = oldPageLimits.width() != pageLimits.width();
    const bool verticalLimitChanged = oldPageLimits.height() != pageLimits.height();
    const bool repeatedColumnsChanged = oldRepeatedColumns != repeatedColumns;
    const bool repeatedRowsChanged = oldRepeatedRows != repeatedRows;
    const bool zoomChanged = d->m_settings->zoom() != settings.zoom();

    *d->m_settings = settings;

    // The starting column/row for the page parameter updates.
    int column = KS_colMax + 1;
    int row = KS_rowMax + 1;

    // The print range.
    if (oldPrintRange.left() != printRange.left()) {
        column = qMin(oldPrintRange.left(), printRange.left());
    }
    if (oldPrintRange.top() != printRange.top()) {
        row = qMin(oldPrintRange.top(), printRange.top());
    }

    // The zoom.
    if (zoomChanged) {
        column = 0;
        row = 0;
    }

    // The page limits.
    if (horizontalLimitChanged && pageLimits.width() <= 0) {
        column = 0;
    }
    if (verticalLimitChanged && pageLimits.height() <= 0) {
        row = 0;
    }

    // The page dimensions.
    if (pageWidthChanged) {
        column = 0;
    }
    if (pageHeightChanged) {
        row = 0;
    }

    // The column/row repetitions.
    if (repeatedColumnsChanged) {
        d->updateRepeatedColumnsWidth();
        column = qMin(column, oldRepeatedColumns.first);
        column = qMin(column, repeatedColumns.first);
    }
    if (repeatedRowsChanged) {
        d->updateRepeatedRowsHeight();
        row = qMin(row, oldRepeatedRows.first);
        row = qMin(row, repeatedRows.first);
    }

    // Update the page parameters.
    // If page limits are set to non-zero, call the special methods.
    if (horizontalLimitChanged && pageLimits.width() > 0) {
        d->calculateZoomForPageLimitX();
    } else if (column <= KS_colMax) {
        updateHorizontalPageParameters(column);
    }
    if (verticalLimitChanged && pageLimits.height() > 0) {
        d->calculateZoomForPageLimitY();
    } else if (row <= KS_rowMax) {
        updateVerticalPageParameters(row);
    }
}

HeaderFooter *SheetPrint::headerFooter() const
{
    return d->m_headerFooter;
}

bool SheetPrint::isColumnOnNewPage(int _column)
{
    if (_column > d->m_maxCheckedNewPageX)
        d->calculateHorizontalPageParameters(_column);

    //Are these the edges of the print range?
    const QRect printRange = d->m_settings->printRegion().lastRange();
    if (_column == printRange.left() || _column == printRange.right()) {
        return true;
    }

    //beyond the print range it's always false
    if (_column < printRange.left() || _column > printRange.right()) {
        return false;
    }

    //Now check if we find the column already in the list
    if (d->m_lnewPageListX.indexOf(PrintNewPageEntry(_column)) != -1) {
        if (_column > d->m_maxCheckedNewPageX)
            d->m_maxCheckedNewPageX = _column;
        return true;
    }
    return false;
}



bool SheetPrint::isRowOnNewPage(int _row)
{
    if (_row > d->m_maxCheckedNewPageY)
        d->calculateVerticalPageParameters(_row);

    //Are these the edges of the print range?
    const QRect printRange = d->m_settings->printRegion().lastRange();
    if (_row == printRange.top() || _row == printRange.bottom()) {
        return true;
    }

    //beyond the print range it's always false
    if (_row < printRange.top() || _row > printRange.bottom()) {
        return false;
    }

    //Now check if we find the row already in the list
    if (d->m_lnewPageListY.indexOf(PrintNewPageEntry(_row)) != -1) {
        if (_row > d->m_maxCheckedNewPageY)
            d->m_maxCheckedNewPageY = _row;
        return true;
    }

    return false;
}

void SheetPrint::updateHorizontalPageParameters(int _col)
{
    //If the new range is after the first entry, we need to delete the whole list
    const QRect printRange = d->m_settings->printRegion().lastRange();
    if (d->m_lnewPageListX.isEmpty() || d->m_lnewPageListX.first().startItem() != printRange.left() || _col == 0) {
        d->m_lnewPageListX.clear();
        d->m_maxCheckedNewPageX = 0;
        d->updateRepeatedColumnsWidth();
        return;
    }

    if (_col <= d->m_lnewPageListX.last().endItem()) {
        // Find the page entry for this column
        int index = d->m_lnewPageListX.count() - 1;
        while (_col < d->m_lnewPageListX[index].startItem()) {
            --index;
        }

        //Remove later pages
        while (index != d->m_lnewPageListX.count())
            d->m_lnewPageListX.removeAt(index);

        d->m_maxCheckedNewPageX = d->m_lnewPageListX.isEmpty() ? 0 : d->m_lnewPageListX.last().endItem();
    }

    // The column is not beyond the repeated columns?
    if (_col <= d->m_settings->repeatedColumns().second) {
        d->updateRepeatedColumnsWidth();
    }
}

void SheetPrint::updateVerticalPageParameters(int _row)
{
    //If the new range is after the first entry, we need to delete the whole list
    const QRect printRange = d->m_settings->printRegion().lastRange();
    if (d->m_lnewPageListY.isEmpty() || d->m_lnewPageListY.first().startItem() != printRange.top() || _row == 0) {
        d->m_lnewPageListY.clear();
        d->m_maxCheckedNewPageY = 0;
        d->updateRepeatedRowsHeight();
        return;
    }

    if (_row <= d->m_lnewPageListY.last().endItem()) {
        // Find the page entry for this row
        int index = d->m_lnewPageListY.count() - 1;
        while (_row < d->m_lnewPageListY[index].startItem()) {
            --index;
        }

        //Remove later pages
        while (index != d->m_lnewPageListY.count())
            d->m_lnewPageListY.removeAt(index);

        d->m_maxCheckedNewPageY = d->m_lnewPageListY.isEmpty() ? 0 : d->m_lnewPageListY.last().endItem();
    }

    // The row is not beyond the repeated rows?
    if (_row <= d->m_settings->repeatedRows().second) {
        d->updateRepeatedRowsHeight();
    }
}

void SheetPrint::insertColumn(int col, int nbCol)
{
    //update print range, when it has been defined
    const QRect printRange = d->m_settings->printRegion().lastRange();
    if (printRange != QRect(QPoint(1, 1), QPoint(KS_colMax, KS_rowMax))) {
        int left = printRange.left();
        int right = printRange.right();

        for (int i = 0; i < nbCol; i++) {
            if (left >= col) left++;
            if (right >= col) right++;
        }
        //Validity checks
        if (left > KS_colMax) left = KS_colMax;
        if (right > KS_colMax) right = KS_colMax;
        const Region region(QRect(QPoint(left, printRange.top()),
                                  QPoint(right, printRange.bottom())), d->m_pSheet);
        // Trigger an update by setting it indirectly.
        PrintSettings settings = *d->m_settings;
        settings.setPrintRegion(region);
        setSettings(settings);
    }
}

void SheetPrint::insertRow(int row, int nbRow)
{
    //update print range, when it has been defined
    const QRect printRange = d->m_settings->printRegion().lastRange();
    if (printRange != QRect(QPoint(1, 1), QPoint(KS_colMax, KS_rowMax))) {
        int top = printRange.top();
        int bottom = printRange.bottom();

        for (int i = 0; i < nbRow; i++) {
            if (top >= row) top++;
            if (bottom >= row) bottom++;
        }
        //Validity checks
        if (top > KS_rowMax) top = KS_rowMax;
        if (bottom > KS_rowMax) bottom = KS_rowMax;
        const Region region(QRect(QPoint(printRange.left(), top),
                                  QPoint(printRange.right(), bottom)), d->m_pSheet);
        // Trigger an update by setting it indirectly.
        PrintSettings settings = *d->m_settings;
        settings.setPrintRegion(region);
        setSettings(settings);
    }
}

void SheetPrint::removeColumn(int col, int nbCol)
{
    PrintSettings settings = *d->m_settings;
    //update print range, when it has been defined
    const QRect printRange = d->m_settings->printRegion().lastRange();
    if (printRange != QRect(QPoint(1, 1), QPoint(KS_colMax, KS_rowMax))) {
        int left = printRange.left();
        int right = printRange.right();

        for (int i = 0; i < nbCol; i++) {
            if (left > col) left--;
            if (right >= col) right--;
        }
        //Validity checks
        if (left < 1) left = 1;
        if (right < 1) right = 1;
        const Region region(QRect(QPoint(left, printRange.top()),
                                  QPoint(right, printRange.bottom())), d->m_pSheet);
        settings.setPrintRegion(region);
    }

    //update repeat columns, when it has been defined
    const QPair<int, int> repeatedColumns = d->m_settings->repeatedColumns();
    if (repeatedColumns.first != 0) {
        int left = repeatedColumns.first;
        int right = repeatedColumns.second;

        for (int i = 0; i < nbCol; i++) {
            if (left > col) left--;
            if (right >= col) right--;
        }
        //Validity checks
        if (left < 1) left = 1;
        if (right < 1) right = 1;
        settings.setRepeatedColumns(qMakePair(left, right));
    }
    // Trigger an update by setting them indirectly.
    setSettings(settings);
}

void SheetPrint::removeRow(int row, int nbRow)
{
    PrintSettings settings = *d->m_settings;
    //update print range, when it has been defined
    const QRect printRange = d->m_settings->printRegion().lastRange();
    if (printRange != QRect(QPoint(1, 1), QPoint(KS_colMax, KS_rowMax))) {
        int top = printRange.top();
        int bottom = printRange.bottom();

        for (int i = 0; i < nbRow; i++) {
            if (top > row) top--;
            if (bottom >= row) bottom--;
        }
        //Validity checks
        if (top < 1) top = 1;
        if (bottom < 1) bottom = 1;
        const Region region(QRect(QPoint(printRange.left(), top),
                                  QPoint(printRange.right(), bottom)), d->m_pSheet);
        settings.setPrintRegion(region);
    }

    //update repeat rows, when it has been defined
    const QPair<int, int> repeatedRows = d->m_settings->repeatedRows();
    if (repeatedRows.first != 0) {
        int top = repeatedRows.first;
        int bottom = repeatedRows.second;

        for (int i = 0; i < nbRow; i++) {
            if (top > row) top--;
            if (bottom >= row) bottom--;
        }
        //Validity checks
        if (top < 1) top = 1;
        if (bottom < 1) bottom = 1;
        settings.setRepeatedRows(qMakePair(top, bottom));
    }
    // Trigger an update by setting them indirectly.
    setSettings(settings);
}

int SheetPrint::pageCount() const
{
    return d->m_lnewPageListX.count() * d->m_lnewPageListY.count();
}

QRect SheetPrint::cellRange(int page) const
{
    if (d->m_lnewPageListX.isEmpty() || d->m_lnewPageListY.isEmpty()) {
        return QRect();
    }
    if (page - 1 > pageCount()) {
        return QRect();
    }
    debugSheets << "page:" << page << "of" << pageCount();

    int horizontalIndex = 0;
    int verticalIndex = 0;
    if (d->m_settings->pageOrder() == PrintSettings::LeftToRight) {
        horizontalIndex = (page - 1) % d->m_lnewPageListX.count();
        verticalIndex = (page - 1) / d->m_lnewPageListX.count();
    } else {
        horizontalIndex = (page - 1) / d->m_lnewPageListY.count();
        verticalIndex = (page - 1) % d->m_lnewPageListY.count();
    }
    debugSheets << "horizontal:" << horizontalIndex + 1 << "of" << d->m_lnewPageListX.count();
    debugSheets << "vertical:" << verticalIndex + 1 << "of" << d->m_lnewPageListY.count();

    const PrintNewPageEntry horizontalParameters = d->m_lnewPageListX[horizontalIndex];
    const PrintNewPageEntry verticalParameters = d->m_lnewPageListY[verticalIndex];

    QRect cellRange;
    cellRange.setLeft(horizontalParameters.startItem());
    cellRange.setRight(horizontalParameters.endItem());
    cellRange.setTop(verticalParameters.startItem());
    cellRange.setBottom(verticalParameters.endItem());
    return cellRange;
}

QRectF SheetPrint::documentArea(int page) const
{
    if (d->m_lnewPageListX.isEmpty() || d->m_lnewPageListY.isEmpty()) {
        return QRectF();
    }
    if (page - 1 > pageCount()) {
        return QRectF();
    }

    int horizontalIndex = 0;
    int verticalIndex = 0;
    if (d->m_settings->pageOrder() == PrintSettings::LeftToRight) {
        horizontalIndex = (page - 1) % d->m_lnewPageListX.count();
        verticalIndex = (page - 1) / d->m_lnewPageListX.count();
    } else {
        horizontalIndex = (page - 1) / d->m_lnewPageListY.count();
        verticalIndex = (page - 1) % d->m_lnewPageListY.count();
    }

    const PrintNewPageEntry horizontalParameters = d->m_lnewPageListX[horizontalIndex];
    const PrintNewPageEntry verticalParameters = d->m_lnewPageListY[verticalIndex];

    QRectF documentArea;
    documentArea.setLeft(horizontalParameters.offset());
    documentArea.setWidth(horizontalParameters.size());
    documentArea.setTop(verticalParameters.offset());
    documentArea.setHeight(verticalParameters.size());
    return documentArea;
}

void SheetPrint::operator=(const SheetPrint & other)
{
    d->m_pSheet = other.d->m_pSheet;

    *d->m_settings = *other.d->m_settings;
    *d->m_headerFooter = *other.d->m_headerFooter;

    d->m_maxCheckedNewPageX = other.d->m_maxCheckedNewPageX;
    d->m_maxCheckedNewPageY = other.d->m_maxCheckedNewPageY;
    d->m_dPrintRepeatColumnsWidth = other.d->m_dPrintRepeatColumnsWidth;
    d->m_dPrintRepeatRowsHeight = other.d->m_dPrintRepeatRowsHeight;
    d->m_lnewPageListX = other.d->m_lnewPageListX;
    d->m_lnewPageListY = other.d->m_lnewPageListY;
}