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 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584
|
/* This file is part of the KDE project
* Copyright (C) 2009 Elvis Stansvik <elvstone@gmail.org>
* Copyright (C) 2009 KO GmbH <cbo@kogmbh.com>
*
* 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.
*/
#include "TableLayout.h"
#include "TableLayoutData.h"
#include <KoTableStyle.h>
#include <KoTableCellStyle.h>
#include <KoTableColumnStyle.h>
#include <KoTableRowStyle.h>
#include <KoTableColumnAndRowStyleManager.h>
#include <KoTextDocumentLayout.h>
#include <KoShape.h>
#include <QTextDocument>
#include <QTextTable>
#include <QTextLine>
#include <QPainter>
#include <QRectF>
#include <QPointF>
#include <kdebug.h>
TableLayout::TableLayout(QTextTable *table) : m_dirty(true)
{
setTable(table);
}
TableLayout::TableLayout() :
m_table(0),
m_tableLayoutData(0),
m_dirty(true)
{
}
void TableLayout::setTable(QTextTable *table)
{
Q_ASSERT(table);
TableLayoutData *tableLayoutData;
if (table == m_table)
return; // we are already set
if (!m_tableLayoutDataMap.contains(table)) {
// Set up new table layout data.
tableLayoutData = new TableLayoutData();
m_tableLayoutDataMap.insert(table, tableLayoutData);
connect(table, SIGNAL(destroyed(QObject *)), this, SLOT(tableDestroyed(QObject *)));
} else {
// Table layout data already in map.
tableLayoutData = m_tableLayoutDataMap.value(table);
}
m_table = table;
m_tableLayoutData = tableLayoutData;
// Resize geometry vectors for the table.
m_tableLayoutData->m_rowPositions.resize(m_table->rows());
m_tableLayoutData->m_rowHeights.resize(m_table->rows());
m_tableLayoutData->m_contentHeights.resize(m_table->rows());
for (int row = 0; row < m_table->rows(); ++row) {
m_tableLayoutData->m_contentHeights[row].resize(m_table->columns());
}
m_dirty = true;
}
QTextTable *TableLayout::table() const
{
return m_table;
}
void TableLayout::startNewTableRect(QPointF position, qreal parentWidth, int fromRow)
{
Q_ASSERT(isValid());
if (!isValid()) {
return;
}
// First remove the following (by now invalid) table rects
while (!m_tableLayoutData->m_tableRects.isEmpty() && m_tableLayoutData->m_tableRects.back().fromRow > fromRow) {
m_tableLayoutData->m_tableRects.removeLast();
}
// Also remove the back tableRect if we are about to add one similar.
// TODO when breaking inside rows this needs to be made smarter
if (!m_tableLayoutData->m_tableRects.isEmpty() && m_tableLayoutData->m_tableRects.back().fromRow == fromRow) {
m_tableLayoutData->m_tableRects.removeLast();
}
QTextTableFormat tableFormat = m_table->format();
// Table width.
qreal tableWidth = 0;
if (tableFormat.width().rawValue() == 0 || tableFormat.alignment() == Qt::AlignJustify) {
// We got a zero width value or alignment is justify, so use 100% of parent.
tableWidth = parentWidth;
} else {
if (tableFormat.width().type() == QTextLength::FixedLength) {
// Fixed length value, so use the raw value directly.
tableWidth = tableFormat.width().rawValue();
} else if (tableFormat.width().type() == QTextLength::PercentageLength) {
// Percentage length value, so use a percentage of parent width.
tableWidth = tableFormat.width().rawValue() * (parentWidth / 100)
- tableFormat.leftMargin() - tableFormat.rightMargin();
} else {
// Unknown length type, so use 100% of parent.
kWarning(32600) << "Unknown table width type";
tableWidth = parentWidth;
}
}
TableRect tableRect;
tableRect.fromRow = fromRow;
tableRect.rect = QRectF(position.x() + tableFormat.leftMargin(),
position.y() + (fromRow==0 ? tableFormat.topMargin() : 0.0),
tableWidth, 1); // the 1 is to make sure it's valid
tableRect.columnPositions.resize(m_table->columns());
tableRect.columnWidths.resize(m_table->columns());
// Get the column and row style manager.
KoTableColumnAndRowStyleManager *carsManager =
reinterpret_cast<KoTableColumnAndRowStyleManager *>(
tableFormat.property(KoTableStyle::ColumnAndRowStyleManager).value<void *>());
// Column widths.
qreal availableWidth = tableWidth; // Width available for columns.
QList<int> relativeWidthColumns; // List of relative width columns.
qreal relativeWidthSum = 0; // Sum of relative column width values.
int numNonStyleColumns = 0;
for (int col = 0; col < tableRect.columnPositions.size(); ++col) {
KoTableColumnStyle *columnStyle = carsManager ? carsManager->columnStyle(col) : 0;
if (!columnStyle) {
// No style so neither width nor relative width specified. Can this happen?
tableRect.columnWidths[col] = 0.0;
relativeWidthColumns.append(col); // handle it as a relative width column without asking for anything
++numNonStyleColumns;
} else if (columnStyle->hasProperty(KoTableColumnStyle::RelativeColumnWidth)) {
// Relative width specified. Will be handled in the next loop.
relativeWidthColumns.append(col);
relativeWidthSum += columnStyle->relativeColumnWidth();
} else if (columnStyle->hasProperty(KoTableColumnStyle::ColumnWidth)) {
// Only width specified, so use it.
tableRect.columnWidths[col] = columnStyle->columnWidth();
availableWidth -= columnStyle->columnWidth();
} else {
// Neither width nor relative width specified. Can this happen?
kWarning(32600) << "Neither column-width nor rel-column-width specified";
tableRect.columnWidths[col] = 0.0;
}
}
// Calculate width to those columns that don't actually request it
qreal widthForNonStyleColumn = ((1.0 - qMin<qreal>(relativeWidthSum, 1.0)) * availableWidth);
availableWidth -= widthForNonStyleColumn; // might as well do this calc before dividing by numNonStyleColumns
if (numNonStyleColumns)
widthForNonStyleColumn /= numNonStyleColumns;
// Relative column widths have now been summed up and can be distributed.
foreach (int col, relativeWidthColumns) {
KoTableColumnStyle *columnStyle = carsManager ? carsManager->columnStyle(col) : 0;
if (columnStyle) {
tableRect.columnWidths[col] =
qMax<qreal>(columnStyle->relativeColumnWidth() * availableWidth / relativeWidthSum, 0.0);
} else {
tableRect.columnWidths[col] = widthForNonStyleColumn;
}
}
// Column positions.
qreal columnPosition = position.x();
qreal columnOffset = tableFormat.leftMargin();
if (tableFormat.alignment() == Qt::AlignRight) {
// Table is right-aligned, so add all of the remaining space.
columnOffset += parentWidth - tableWidth;
}
if (tableFormat.alignment() == Qt::AlignHCenter) {
// Table is centered, so add half of the remaining space.
columnOffset += (parentWidth - tableWidth) / 2;
}
for (int col = 0; col < tableRect.columnPositions.size(); ++col) {
tableRect.columnPositions[col] = columnPosition + columnOffset;
// Increment by this column's width.
columnPosition += tableRect.columnWidths[col];
}
m_tableLayoutData->m_tableRects.append(tableRect);
m_tableLayoutData->m_rowPositions[fromRow] = tableRect.rect.top(); //Initialize the position of first row of tableRect
m_dirty = false;
}
void TableLayout::layoutRow(int row)
{
Q_ASSERT(isValid());
Q_ASSERT(row >= 0);
Q_ASSERT(row < m_table->rows());
if (!isValid()) {
return;
}
if (row < 0 || row >= m_table->rows()) {
return;
}
// First remove the following (by now invalid) table rects
while (m_tableLayoutData->m_tableRects.back().fromRow > row) {
m_tableLayoutData->m_tableRects.removeLast();
}
// Now the following calls to m_tableRects.back() are hitting the right table rect
QTextTableFormat tableFormat = m_table->format();
// Get the column and row style manager.
KoTableColumnAndRowStyleManager *carsManager =
reinterpret_cast<KoTableColumnAndRowStyleManager *>(
tableFormat.property(KoTableStyle::ColumnAndRowStyleManager).value<void *>());
/*
* Implementation Note:
*
* An undocumented behavior of QTextTable::cellAt is that requesting a
* cell that is covered by a spanning cell will return the cell that
* spans over the requested cell. Example:
*
* +------------+------------+
* | | |
* | | |
* | +------------+
* | | |
* | | |
* +------------+------------+
*
* table.cellAt(1, 0).row() // Will return 0.
*
* In the code below, we rely on this behavior to determine wheather
* a cell "vertically" ends in the current row, as those are the only
* cells that should contribute to the row height.
*/
KoTableRowStyle *rowStyle = carsManager ? carsManager->rowStyle(row) : 0;
// Adjust row height.
qreal rowHeight = rowStyle ? rowStyle->minimumRowHeight() : 0.0;
int col = 0;
while (col < m_table->columns()) {
// Get the cell format.
QTextTableCell cell = m_table->cellAt(row, col);
QTextTableCellFormat cellFormat = cell.format().toTableCellFormat();
if (row == cell.row() + cell.rowSpan() - 1) {
/*
* This cell ends vertically in this row, and hence should
* contribute to the row height. So we get the height of the
* entire cell, including borders and padding. This is done
* by calling KoTableCellStyle::boundingRect() with a
* rectangle as high as the cell content.
*/
KoTableCellStyle cellStyle(cellFormat);
QRectF heightRect(1, 1, 1, m_tableLayoutData->m_contentHeights.at(cell.row()).at(cell.column()));
qreal cellHeight = cellStyle.boundingRect(heightRect).height();
// Subtract the height of the rows above.
for (int rowAbove = row - 1; rowAbove >= cell.row(); --rowAbove) {
cellHeight -= m_tableLayoutData->m_rowHeights[rowAbove];
}
/*
* Now we know how much height this cell contributes to the row,
* and can determine wheather the row height will grow.
*/
rowHeight = qMax(rowHeight, cellHeight);
}
col += cell.columnSpan(); // Skip across column spans.
}
m_tableLayoutData->m_rowHeights[row] = rowHeight;
// Adjust Y position of NEXT row.
// This is nice since the outside layout routine relies on the next row having a correct y position
// the first row y position is set in createFirstLayoutRect()
if (row+1 < m_table->rows()) {
m_tableLayoutData->m_rowPositions[row+1] =
m_tableLayoutData->m_rowPositions[row ] + // Position of this row.
m_tableLayoutData->m_rowHeights[row]; // Height of this row.
}
// Adjust table rect height for new height.
m_tableLayoutData->m_tableRects.last().rect.setHeight(m_tableLayoutData->m_rowPositions[row]
+ m_tableLayoutData->m_rowHeights[row]
- m_tableLayoutData->m_rowPositions[m_tableLayoutData->m_tableRects.last().fromRow]);//FIXME review when breaking inside a row
}
void TableLayout::drawBackground(QPainter *painter) const
{
if (m_tableLayoutData->m_tableRects.isEmpty()) {
return;
}
painter->save();
// Draw table background.
foreach (TableRect tRect, m_tableLayoutData->m_tableRects) {
painter->fillRect(tRect.rect, m_table->format().background());
}
// Draw cell backgrounds using their styles.
for (int row = 0; row < m_table->rows(); ++row) {
for (int column = 0; column < m_table->columns(); ++column) {
QTextTableCell tableCell = m_table->cellAt(row, column);
/*
* The following check relies on the fact that QTextTable::cellAt()
* will return the cell that has the span when a covered cell is
* requested.
*/
if (row == tableCell.row() && column == tableCell.column()) {
// This is an actual cell we want to draw, and not a covered one.
KoTableCellStyle cellStyle(tableCell.format().toTableCellFormat());
cellStyle.paintBackground(*painter, cellBoundingRect(tableCell));
}
}
}
painter->restore();
}
void TableLayout::drawBorders(QPainter *painter, QPainterPath *accuBlankBorders) const
{
if (m_tableLayoutData->m_tableRects.isEmpty()) {
return;
}
painter->save();
KoTableStyle tableStyle(m_table->format());
bool collapsing = tableStyle.collapsingBorderModel();
// Draw cell borders using their styles.
for (int row = 0; row < m_table->rows(); ++row) {
for (int column = 0; column < m_table->columns(); ++column) {
QTextTableCell tableCell = m_table->cellAt(row, column);
/*
* The following check relies on the fact that QTextTable::cellAt()
* will return the cell that has the span when a covered cell is
* requested.
*/
if (row == tableCell.row() && column == tableCell.column()) {
// This is an actual cell we want to draw, and not a covered one.
KoTableCellStyle cellStyle(tableCell.format().toTableCellFormat());
if (collapsing) {
QRectF bRect = cellBoundingRect(tableCell);
// First the horizontal borders
if (row == 0)
cellStyle.drawTopHorizontalBorder(*painter, bRect.x(), bRect.y(), bRect.width(), accuBlankBorders);
if (row + tableCell.rowSpan() == m_table->rows()) {
// we hit the bottom of the table so just draw the bottom border
cellStyle.drawBottomHorizontalBorder(*painter, bRect.x(), bRect.bottom(), bRect.width(), accuBlankBorders);
} else {
// we have cells below so draw sharedborders
QTextTableCell tableCellBelow;
for (int c = column; c < column + tableCell.columnSpan();
c = tableCellBelow.column()+tableCellBelow.columnSpan()) {
tableCellBelow = m_table->cellAt(row + tableCell.rowSpan()-1+1, c);
QRectF belowBRect = cellBoundingRect(tableCellBelow);
qreal x = qMax(bRect.x(), belowBRect.x());
qreal x2 = qMin(bRect.right(), belowBRect.right());
KoTableCellStyle cellBelowStyle(tableCellBelow.format().toTableCellFormat());
cellStyle.drawSharedHorizontalBorder(*painter, cellBelowStyle, x, bRect.bottom(), x2 - x, accuBlankBorders);
}
}
// And then the same treatment for vertical borders
if (column == 0)
cellStyle.drawLeftmostVerticalBorder(*painter, bRect.x(), bRect.y(), bRect.height(), accuBlankBorders);
if (column + tableCell.columnSpan() == m_table->columns()) {
// we hit the rightmost edge of the table so draw the rightmost border
cellStyle.drawRightmostVerticalBorder(*painter, bRect.right(), bRect.y(), bRect.height(), accuBlankBorders);
} else {
// we have cells to the right so draw sharedborders
QTextTableCell tableCellRight;
for (int r = row; r < row + tableCell.rowSpan();
r = tableCellRight.row() + tableCellRight.rowSpan()) {
tableCellRight = m_table->cellAt(r, column + tableCell.columnSpan()-1+1);
QRectF rightBRect = cellBoundingRect(tableCellRight);
qreal y = qMax(bRect.y(), rightBRect.y());
qreal y2 = qMin(bRect.bottom(), rightBRect.bottom());
KoTableCellStyle cellBelowRight(tableCellRight.format().toTableCellFormat());
cellStyle.drawSharedVerticalBorder(*painter, cellBelowRight, bRect.right(), y, y2-y, accuBlankBorders);
}
}
} else { // separating border model
cellStyle.paintBorders(*painter, cellBoundingRect(tableCell));
}
}
}
}
painter->restore();
}
QTextTableCell TableLayout::hitTestTable(const QPointF &point) const
{
foreach (TableRect tableRect, m_tableLayoutData->m_tableRects) {
if (tableRect.rect.contains(point)) {
int col;
for (col = 1; col < m_table->columns(); ++col) {
if (point.x() < tableRect.columnPositions[col])
break;
}
--col;
int row;
for (row = tableRect.fromRow; row < m_table->rows(); ++row) {
if (point.y() < m_tableLayoutData->m_rowPositions[row])
break;
}
--row;
return m_table->cellAt(row, col);
}
}
return QTextTableCell();
}
QRectF TableLayout::cellContentRect(const QTextTableCell &cell) const
{
Q_ASSERT(isValid());
Q_ASSERT(cell.isValid());
/*
* Get the cell style and return the bounding rect adjusted for
* borders and paddings by calling KoTableCellStyle::contentRect().
*/
KoTableCellStyle cellStyle(cell.format().toTableCellFormat());
return cellStyle.contentRect(cellBoundingRect(cell));
}
QRectF TableLayout::cellBoundingRect(const QTextTableCell &cell) const
{
Q_ASSERT(isValid());
Q_ASSERT(cell.row() < m_tableLayoutData->m_rowPositions.size());
TableRect tableRect = m_tableLayoutData->m_tableRects.last();
while (tableRect.fromRow > cell.row()) {
tableRect = m_tableLayoutData->m_tableRects.first();
}
Q_ASSERT(cell.column() + cell.columnSpan() <= tableRect.columnPositions.size());
// Cell width.
qreal width = 0;
for (int col = 0; col < cell.columnSpan(); ++col) {
width += tableRect.columnWidths[cell.column() + col];
}
// Cell height.
qreal height = 0;
for (int r = 0; r < cell.rowSpan(); ++r) {
height += m_tableLayoutData->m_rowHeights[cell.row() + r];
// TODO when breaking within a row the tableRect needs to be switched at some point
}
return QRectF(tableRect.columnPositions[cell.column()], m_tableLayoutData->m_rowPositions[cell.row()],
width, height);
}
QRectF TableLayout::rowBoundingRect(int row) const
{
Q_ASSERT(row < m_tableLayoutData->m_rowPositions.size());
TableRect tableRect = m_tableLayoutData->m_tableRects.last();
while (tableRect.fromRow > row) {
tableRect = m_tableLayoutData->m_tableRects.first();
}
return QRectF(tableRect.rect.left(), m_tableLayoutData->m_rowPositions[row],
tableRect.rect.width(), m_tableLayoutData->m_rowHeights[row]);
}
void TableLayout::calculateCellContentHeight(const QTextTableCell &cell)
{
Q_ASSERT(isValid());
Q_ASSERT(cell.isValid());
if (!isValid() || !cell.isValid()) {
return;
}
// Get the first line in the first block in the cell.
QTextFrame::iterator cellIterator = cell.begin();
Q_ASSERT(cellIterator.currentFrame() == 0); // TODO: Nested tables?
QTextBlock firstBlock = cellIterator.currentBlock();
Q_ASSERT(firstBlock.isValid());
QTextLine topLine = firstBlock.layout()->lineAt(0);
Q_ASSERT(topLine.isValid());
// Get the last line in the last block in the cell.
cellIterator = cell.end();
cellIterator--;
Q_ASSERT(cellIterator.currentFrame() == 0); // TODO: Nested tables?
QTextBlock lastBlock = cellIterator.currentBlock();
Q_ASSERT(lastBlock.isValid());
QTextLine bottomLine = lastBlock.layout()->lineAt(lastBlock.layout()->lineCount() - 1);
Q_ASSERT(bottomLine.isValid());
// Content height is the difference between bottomLine and topLine.
qreal contentHeight = (bottomLine.y() + bottomLine.height()) - topLine.y();
// FIXME; using 'height()' above is wrong since it uses the font specs unconditionally
// see Layout::addLine() for the gory details on how to calculate the line height
Q_ASSERT(contentHeight >= 0); // Sanity check.
contentHeight = qMax(contentHeight, (qreal)0);
// Update content height value of the cell.
m_tableLayoutData->m_contentHeights[cell.row()][cell.column()] = contentHeight;
}
QTextTableCell TableLayout::cellAt(int position) const
{
Q_ASSERT(isValid());
if (!isValid())
return QTextTableCell(); // Return invalid cell.
return m_table->cellAt(position);
}
qreal TableLayout::yAfterTable() const
{
Q_ASSERT(isValid());
QTextTableFormat tableFormat = m_table->format();
return m_tableLayoutData->m_tableRects.last().rect.bottom() + tableFormat.bottomMargin();
}
bool TableLayout::isDirty() const
{
return m_dirty;
}
bool TableLayout::isValid() const
{
return (m_table && m_tableLayoutData);
}
void TableLayout::tableDestroyed(QObject *object)
{
QTextTable *table = static_cast<QTextTable *>(object);
Q_ASSERT(table);
if (m_tableLayoutDataMap.contains(table)) {
// Clean up table layout data for the table.
delete m_tableLayoutDataMap.value(table);
m_tableLayoutDataMap.remove(table);
}
if (m_table == table) {
// It was the currently set table that was deleted.
m_table = 0;
m_tableLayoutData = 0;
}
}
#include <TableLayout.moc>
|