17 #include <QAbstractTextDocumentLayout>
20 #include <QTextCursor>
21 #include <QTextTableCell>
27 class KDReports::TableElementPrivate
30 void createCell(QTextTable *textTable, ReportBuilder &builder,
int row,
int column,
const Cell &cell, QTextCharFormat charFormat)
const;
34 int m_columnCount = 0;
35 int m_headerRowCount = 0;
36 int m_headerColumnCount = 0;
42 : d(new TableElementPrivate)
48 , d(new TableElementPrivate(*other.d))
73 d->m_headerRowCount = count;
78 return d->m_headerRowCount;
83 d->m_headerColumnCount = count;
88 return d->m_headerColumnCount;
98 return d->m_columnCount;
103 d->m_rowCount = std::max(d->m_rowCount, row + 1);
104 d->m_columnCount = std::max(d->m_columnCount, column + 1);
106 const QPair<int, int> coord = qMakePair(row, column);
107 return d->m_cellContentMap[coord];
110 void KDReports::TableElementPrivate::createCell(QTextTable *textTable,
ReportBuilder &builder,
int row,
int column,
const Cell &cell, QTextCharFormat charFormat)
const
114 QTextTableCell tableCell = textTable->cellAt(row, column);
115 Q_ASSERT(tableCell.isValid());
116 QTextCursor cellCursor = tableCell.firstCursorPosition();
117 QTextTableCellFormat tableCellFormat(charFormat.toTableCellFormat());
119 tableCellFormat.setBackground(cell.
background());
120 tableCellFormat.setTableCellColumnSpan(cell.
columnSpan());
121 tableCellFormat.setTableCellRowSpan(cell.
rowSpan());
125 func(row, column, tableCellFormat);
126 tableCell.setFormat(tableCellFormat);
127 cellCursor.setCharFormat(tableCellFormat);
129 cellBuilder.copyStateFrom(builder);
130 cellBuilder.setDefaultFont(charFormat.font());
131 cell.
build(cellBuilder);
136 if (d->m_cellContentMap.isEmpty())
139 QTextCursor &textDocCursor = builder.
cursor();
141 QTextTableFormat tableFormat;
142 tableFormat.setHeaderRowCount(d->m_headerRowCount);
144 tableFormat.setAlignment(textDocCursor.blockFormat().alignment());
145 tableFormat.setBackground(background());
146 fillTableFormat(tableFormat, textDocCursor);
147 QTextCharFormat charFormat = textDocCursor.charFormat();
149 QTextTable *textTable = textDocCursor.insertTable(d->m_rowCount, d->m_columnCount, tableFormat);
151 CellContentMap::const_iterator it = d->m_cellContentMap.constBegin();
152 for (; it != d->m_cellContentMap.constEnd(); ++it) {
153 const int row = it.key().first;
154 const int column = it.key().second;
155 const Cell &cell = it.value();
156 d->createCell(textTable, builder, row, column, cell, charFormat);
159 textDocCursor.movePosition(QTextCursor::End);
AbstractTableElement & operator=(const AbstractTableElement &other)
void build(ReportBuilder &builder) const override
Qt::AlignmentFlag verticalAlignment() const
Returns the vertical alignment of the cell contents.
CellFormatFunc cellFormatFunction() const
Returns the function passed to setCellFormatFunction()
QBrush background() const
TextDocumentData & currentDocumentData()
static QTextCharFormat::VerticalAlignment toVerticalAlignment(Qt::Alignment alignment)
void build(ReportBuilder &) const override
void setHeaderRowCount(int count)
int headerRowCount() const
int headerColumnCount() const
void setHeaderColumnCount(int count)
Cell & cell(int row, int column)
TableElement & operator=(const TableElement &other)
Element * clone() const override
void registerTable(QTextTable *table)
QMap< QPair< int, int >, Cell > CellContentMap