/
collapsedtablepainterex.cpp
485 lines (416 loc) · 19.6 KB
/
collapsedtablepainterex.cpp
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
/*
Copyright (C) 2011 Elvis Stansvik <elvstone@gmail.com>
For general Scribus (>=1.3.2) copyright and licensing information please refer
to the COPYING file provided with the program. Following this notice may exist
a copyright and/or license notice that predates the release of Scribus 1.3.2
for which a new license (GPL+exception) is in place.
*/
#include <QColor>
#include <QLineF>
#include <QRectF>
#include "pageitem_table.h"
#include "prefsmanager.h"
#include "scpageoutput.h"
#include "scpainterexbase.h"
#include "scribusdoc.h"
#include "tablecell.h"
#include "tableborder.h"
#include "tableutils.h"
#include "collapsedtablepainterex.h"
using namespace TableUtils;
CollapsedTablePainterEx::CollapsedTablePainterEx(ScPageOutput* pageOutput, PageItem_Table* table)
{
m_pageOutput = pageOutput;
m_table = table;
}
void CollapsedTablePainterEx::paintTable(ScPainterExBase* p)
{
p->save();
p->translate(m_table->gridOffset());
// Paint table fill.
paintTableFill(p);
/*
* We paint the table in five passes:
*
* 1) Cell fills.
* 2) Vertical borders.
* 3) Horizontal borders
* 4) Decorative grid lines.
* 5) Cell content.
*/
// Pass 1: Paint cell fills.
for (int row = 0; row < m_table->rows(); ++row)
{
int colSpan = 0;
for (int col = 0; col < m_table->columns(); col += colSpan)
{
TableCell cell = m_table->cellAt(row, col);
if (row == cell.row())
paintCellFill(cell, p);
colSpan = cell.columnSpan();
}
}
// Pass 2: Paint vertical borders.
for (int row = 0; row < m_table->rows(); ++row)
{
int colSpan = 0;
for (int col = 0; col < m_table->columns(); col += colSpan)
{
TableCell cell = m_table->cellAt(row, col);
if (row == cell.row())
{
paintCellRightBorders(cell, p);
if (col == 0)
paintCellLeftBorders(cell, p);
}
colSpan = cell.columnSpan();
}
}
// Pass 3: Paint horizontal borders.
for (int row = 0; row < m_table->rows(); ++row)
{
int colSpan = 0;
for (int col = 0; col < m_table->columns(); col += colSpan)
{
TableCell cell = m_table->cellAt(row, col);
if (row == cell.row())
{
paintCellBottomBorders(cell, p);
if (row == 0)
paintCellTopBorders(cell, p);
}
colSpan = cell.columnSpan();
}
}
// Pass 5: Paint cell content.
for (int row = 0; row < m_table->rows(); ++row)
{
for (int col = 0; col < m_table->columns(); col ++)
{
TableCell cell = m_table->cellAt(row, col);
if (cell.row() == row && cell.column() == col)
{
PageItem_TextFrame* textFrame = cell.textFrame();
m_pageOutput->drawItem(textFrame, p, QRect());
}
}
}
p->restore();
}
void CollapsedTablePainterEx::paintTableFill(ScPainterExBase* p) const
{
QString colorName = m_table->fillColor();
if (colorName == CommonStrings::None)
return;
p->save();
int lastCol = m_table->columns() - 1;
int lastRow = m_table->rows() - 1;
double x = m_table->columnPosition(0);
double y = m_table->rowPosition(0);
double width = m_table->columnPosition(lastCol) + m_table->columnWidth(lastCol) - x;
double height = m_table->rowPosition(lastRow) + m_table->rowHeight(lastRow) - y;
ScribusDoc* doc = m_table->doc();
ScColorShade colorShade(doc->PageColors[colorName], (int) m_table->fillShade());
p->setBrush(colorShade);
p->setFillMode(ScPainterExBase::Solid);
p->setStrokeMode(ScPainterExBase::None);
p->drawRect(x, y, width, height);
p->restore();
}
void CollapsedTablePainterEx::paintCellLeftBorders(const TableCell& cell, ScPainterExBase* p) const
{
/*
* We are going to paint the border marked # in the following setup.
*
* +----------------------+----------------------+
* | | |
* | | |
* | topLeftCell top topRightCell |
* | | |
* | | |
* +--------topLeft-------+-------topRight-------+
* | # |
* | # |
* | leftCell border cell |
* | # |
* | # |
* +-------bottomLeft-----+------bottomRight-----+
* | | |
* | | |
* | bottomLeftCell bottom bottomRightCell |
* | | |
* | | |
* +----------------------+----------------------+
*/
// The cell ends in this row.
const int lastRow = cell.row() + cell.rowSpan() - 1;
// The cell starts in this column.
const int firstCol = cell.column();
// The X position of the border segments to paint.
const double borderX = m_table->columnPosition(firstCol);
// The start point of the border segment currently being painted.
QPointF start(borderX, 0.0);
// The end point of the border segment currently being painted.
QPointF end(borderX, 0.0);
// The start and end offset factors for the border segment currently being painted.
QPointF startOffsetFactors, endOffsetFactors;
// Start and end row of border segment currently being painted.
int startRow, endRow;
for (int row = cell.row(); row <= lastRow; row += endRow - startRow + 1)
{
// Get the neighboring cell to the left and determine border segment row interval.
TableCell leftCell = m_table->cellAt(row, firstCol - 1);
startRow = qMax(cell.row(), leftCell.row());
endRow = qMin(lastRow, leftCell.isValid() ? leftCell.row() + leftCell.rowSpan() - 1 : lastRow);
// Get the remaining neighboring cells.
TableCell topLeftCell = m_table->cellAt(startRow - 1, firstCol - 1);
TableCell topRightCell = m_table->cellAt(startRow - 1, firstCol);
TableCell bottomRightCell = m_table->cellAt(lastRow + 1, firstCol);
TableCell bottomLeftCell = m_table->cellAt(lastRow + 1, firstCol - 1);
// Resolve borders between neighboring cells.
TableBorder topLeft, top, topRight, border, bottomLeft, bottom, bottomRight;
resolveBordersVertical(topLeftCell, topRightCell, leftCell, cell, bottomLeftCell, bottomRightCell, &topLeft, &top, &topRight, &border, &bottomLeft, &bottom, &bottomRight, m_table);
if (border.isNull())
continue; // Quit early if the border to paint is null.
// Set initial coordinates.
start.setY(m_table->rowPosition(startRow));
end.setY(m_table->rowPosition(endRow) + m_table->rowHeight(endRow));
// Adjust coordinates for joining.
joinVertical(border, topLeft, top, topRight, bottomLeft, bottom, bottomRight,
&start, &end, &startOffsetFactors, &endOffsetFactors);
// Paint the border segment.
paintBorder(border, start, end, startOffsetFactors, endOffsetFactors, p);
}
}
void CollapsedTablePainterEx::paintCellRightBorders(const TableCell& cell, ScPainterExBase* p) const
{
/*
* We are going to paint the border marked # in the following setup.
*
* +----------------------+----------------------+
* | | |
* | | |
* | topLeftCell top topRightCell |
* | | |
* | | |
* +--------topLeft-------+-------topRight-------+
* | # |
* | # |
* | cell border rightCell |
* | # |
* | # |
* +-------bottomLeft-----+------bottomRight-----+
* | | |
* | | |
* | bottomLeftCell bottom bottomRightCell |
* | | |
* | | |
* +----------------------+----------------------+
*/
// The cell ends in this row.
const int lastRow = cell.row() + cell.rowSpan() - 1;
// The cell ends in this column.
const int lastCol = cell.column() + cell.columnSpan() - 1;
// The X position of the border segments to paint.
const double borderX = m_table->columnPosition(lastCol) + m_table->columnWidth(lastCol);
// The start point of the border segment currently being painted.
QPointF start(borderX, 0.0);
// The end point of the border segment currently being painted.
QPointF end(borderX, 0.0);
// The start and end offset factors for the border segment currently being painted.
QPointF startOffsetFactors, endOffsetFactors;
// The start and end row of border segment currently being painted.
int startRow, endRow;
for (int row = cell.row(); row <= lastRow; row += endRow - startRow + 1)
{
// Get the neighboring cell to the right and determine border segment row interval.
TableCell rightCell = m_table->cellAt(row, lastCol + 1);
startRow = qMax(cell.row(), rightCell.row());
endRow = qMin(lastRow, rightCell.isValid() ? rightCell.row() + rightCell.rowSpan() - 1 : lastRow);
// Get the remaining neighboring cells surrounding the segment.
TableCell topLeftCell = m_table->cellAt(startRow - 1, lastCol);
TableCell topRightCell = m_table->cellAt(startRow - 1, lastCol + 1);
TableCell bottomRightCell = m_table->cellAt(endRow + 1, lastCol + 1);
TableCell bottomLeftCell = m_table->cellAt(endRow + 1, lastCol);
// Resolve borders between neighboring cells.
TableBorder topLeft, top, topRight, border, bottomLeft, bottom, bottomRight;
resolveBordersVertical(topLeftCell, topRightCell, cell, rightCell, bottomLeftCell, bottomRightCell, &topLeft, &top, &topRight, &border, &bottomLeft, &bottom, &bottomRight, m_table);
if (border.isNull())
continue; // Quit early if the border to paint is null.
// Set initial coordinates.
start.setY(m_table->rowPosition(startRow));
end.setY(m_table->rowPosition(endRow) + m_table->rowHeight(endRow));
// Adjust coordinates for joining.
joinVertical(border, topLeft, top, topRight, bottomLeft, bottom, bottomRight,
&start, &end, &startOffsetFactors, &endOffsetFactors);
// Paint the border.
paintBorder(border, start, end, startOffsetFactors, endOffsetFactors, p);
}
}
void CollapsedTablePainterEx::paintCellTopBorders(const TableCell& cell, ScPainterExBase* p) const
{
/*
* We are going to paint the border marked # in the following setup.
*
* +--------------------------+--------------------------+--------------------------+
* | | | |
* | | | |
* | topLeftCell topLeft topCell topRight topRightCell |
* | | | |
* | | | |
* +----------left------------+######### border-#########+----------right-----------+
* | | | |
* | | | |
* | bottomLeftCell bottomLeft cell bottomRight bottomRightCell |
* | | | |
* | | | |
* +--------------------------+--------------------------+--------------------------+
*/
// The cell starts in this row.
const int firstRow = cell.row();
// The cell ends in this column.
const int lastCol = cell.column() + cell.columnSpan() - 1;
// The Y position of the border segments to paint.
const double borderY = m_table->rowPosition(firstRow);
// The start point of the border segment currently being painted.
QPointF start(0.0, borderY);
// The end point of the border segment currently being painted.
QPointF end(0.0, borderY);
// The start and end offset factors for the border segment currently being painted.
QPointF startOffsetFactors, endOffsetFactors;
// Start and end column of border segment currently being painted.
int startCol, endCol;
for (int col = cell.column(); col <= lastCol; col += endCol - startCol + 1)
{
// Get the neighboring cell above and determine border segment column interval.
TableCell topCell = m_table->cellAt(firstRow - 1, col);
startCol = qMax(cell.column(), topCell.column());
endCol = qMin(lastCol, topCell.isValid() ? topCell.column() + topCell.columnSpan() - 1 : lastCol);
// Get the remaining neighboring cells.
TableCell topLeftCell = m_table->cellAt(firstRow - 1, startCol - 1);
TableCell topRightCell = m_table->cellAt(firstRow - 1, endCol + 1);
TableCell bottomRightCell = m_table->cellAt(firstRow, endCol + 1);
TableCell bottomLeftCell = m_table->cellAt(firstRow, startCol - 1);
// Resolve borders between neighboring cells.
TableBorder topLeft, left, bottomLeft, border, topRight, right, bottomRight;
resolveBordersHorizontal(topLeftCell, topCell, topRightCell, bottomLeftCell, cell, bottomRightCell, &topLeft, &left, &bottomLeft, &border, &topRight, &right, &bottomRight, m_table);
if (border.isNull())
continue; // Quit early if the border is null.
// Set initial coordinates.
start.setX(m_table->columnPosition(startCol));
end.setX(m_table->columnPosition(endCol) + m_table->columnWidth(endCol));
// Adjust coordinates for joining.
joinHorizontal(border, topLeft, left, bottomLeft, topRight, right, bottomRight,
&start, &end, &startOffsetFactors, &endOffsetFactors);
// Paint the border segment.
paintBorder(border, start, end, startOffsetFactors, endOffsetFactors, p);
}
}
void CollapsedTablePainterEx::paintCellBottomBorders(const TableCell& cell, ScPainterExBase* p) const
{
/*
* We are going to paint the border marked # in the following setup.
*
* +--------------------------+--------------------------+--------------------------+
* | | | |
* | | | |
* | topLeftCell topLeft cell topRight topRightCell |
* | | | |
* | | | |
* +----------left------------+######### border-#########+----------right-----------+
* | | | |
* | | | |
* | bottomLeftCell bottomLeft bottomCell bottomRight bottomRightCell |
* | | | |
* | | | |
* +--------------------------+--------------------------+--------------------------+
*/
// The cell ends in this row.
const int lastRow = cell.row() + cell.rowSpan() - 1;
// The cell ends in this column.
const int lastCol = cell.column() + cell.columnSpan() - 1;
// The Y position of the border segments to paint.
const double borderY = m_table->rowPosition(lastRow) + m_table->rowHeight(lastRow);
// The start point of the border segment currently being painted.
QPointF start(0.0, borderY);
// The end point of the border segment currently being painted.
QPointF end(0.0, borderY);
// The start and end offset factors for the border segment currently being painted.
QPointF startOffsetFactors, endOffsetFactors;
// Start and end column of border segment currently being painted.
int startCol, endCol;
for (int col = cell.column(); col <= lastCol; col += endCol - startCol + 1)
{
// Get the neighboring cell below and determine border segment column interval.
TableCell bottomCell = m_table->cellAt(lastRow + 1, col);
startCol = qMax(cell.column(), bottomCell.column());
endCol = qMin(lastCol, bottomCell.isValid() ? bottomCell.column() + bottomCell.columnSpan() - 1 : lastCol);
// Get the remaining neighboring cells.
TableCell topLeftCell = m_table->cellAt(lastRow, startCol - 1);
TableCell topRightCell = m_table->cellAt(lastRow, endCol + 1);
TableCell bottomRightCell = m_table->cellAt(lastRow + 1, endCol + 1);
TableCell bottomLeftCell = m_table->cellAt(lastRow + 1, startCol - 1);
// Resolve borders between neighboring cells.
TableBorder topLeft, left, bottomLeft, border, topRight, right, bottomRight;
resolveBordersHorizontal(topLeftCell, cell, topRightCell, bottomLeftCell, bottomCell, bottomRightCell, &topLeft, &left, &bottomLeft, &border, &topRight, &right, &bottomRight, m_table);
if (border.isNull())
continue; // Quit early if the border is null.
// Set initial coordinates.
start.setX(m_table->columnPosition(startCol));
end.setX(m_table->columnPosition(endCol) + m_table->columnWidth(endCol));
// Adjust coordinates for joining.
joinHorizontal(border, topLeft, left, bottomLeft, topRight, right, bottomRight,
&start, &end, &startOffsetFactors, &endOffsetFactors);
// Paint the border segment.
paintBorder(border, start, end, startOffsetFactors, endOffsetFactors, p);
}
}
void CollapsedTablePainterEx::paintCellFill(const TableCell& cell, ScPainterExBase* p) const
{
QString colorName = cell.fillColor();
if (colorName == CommonStrings::None)
return;
p->save();
ScribusDoc* doc = m_table->doc();
ScColorShade colorShade(doc->PageColors[colorName], (int) cell.fillShade());
p->setBrush(colorShade);
p->setFillMode(ScPainterExBase::Solid);
p->setStrokeMode(ScPainterExBase::None);
int row = cell.row();
int col = cell.column();
int lastRow = row + cell.rowSpan() - 1;
int lastCol = col + cell.columnSpan() - 1;
double x = m_table->columnPosition(col);
double y = m_table->rowPosition(row);
double width = m_table->columnPosition(lastCol) + m_table->columnWidth(lastCol) - x;
double height = m_table->rowPosition(lastRow) + m_table->rowHeight(lastRow) - y;
p->drawRect(x, y, width, height);
p->restore();
}
void CollapsedTablePainterEx::paintBorder(const TableBorder& border, const QPointF& start, const QPointF& end,
const QPointF& startOffsetFactors, const QPointF& endOffsetFactors,
ScPainterExBase *p) const
{
ScribusDoc* doc = m_table->doc();
p->save();
p->setStrokeMode(ScPainterExBase::Solid);
p->setFillMode(ScPainterExBase::None);
QColor lineColor;
QPointF lineStart, lineEnd;
foreach (const TableBorderLine& line, border.borderLines())
{
// Adjust line start and ends by line width multiplied by offset factors.
lineStart.setX(start.x() + line.width() * startOffsetFactors.x());
lineStart.setY(start.y() + line.width() * startOffsetFactors.y());
lineEnd.setX(end.x() + line.width() * endOffsetFactors.x());
lineEnd.setY(end.y() + line.width() * endOffsetFactors.y());
// Set line color.
if (line.color() == CommonStrings::None)
continue;
// Draw line.
ScColorShade penColor(doc->PageColors[line.color()], (int) line.shade());
p->setPen(penColor, line.width(), line.style(), Qt::FlatCap, Qt::MiterJoin);
p->drawLine(lineStart, lineEnd);
}
p->restore();
}