diff --git a/docs/CHANGES_2_3.txt b/docs/CHANGES_2_3.txt index a78cfaf4..10425473 100644 --- a/docs/CHANGES_2_3.txt +++ b/docs/CHANGES_2_3.txt @@ -12,3 +12,4 @@ Bugfixes: New features: ------------- * AutoTableElement: add customization functions for header styling, see setHorizontalHeaderFormatFunction and setVerticalHeaderFormatFunction, and their use in the PriceList example. +* Cell: add setVerticalAlignment, for vertical centering (or top alignment) of text in table cells, e.g. when using different font sizes in the same row diff --git a/src/KDReports/KDReportsAutoTableElement.cpp b/src/KDReports/KDReportsAutoTableElement.cpp index 00b80c18..ca8afb76 100644 --- a/src/KDReports/KDReportsAutoTableElement.cpp +++ b/src/KDReports/KDReportsAutoTableElement.cpp @@ -90,21 +90,6 @@ class FillCellHelper QTextCursor cellCursor; }; -static QTextCharFormat::VerticalAlignment toVerticalAlignment(Qt::Alignment alignment) -{ - switch (alignment & Qt::AlignVertical_Mask) { - case Qt::AlignTop: - return QTextCharFormat::AlignTop; - case Qt::AlignBottom: - return QTextCharFormat::AlignBottom; - case Qt::AlignVCenter: - return QTextCharFormat::AlignMiddle; - case Qt::AlignBaseline: - return QTextCharFormat::AlignBaseline; - } - return QTextCharFormat::AlignNormal; -} - void FillCellHelper::fill(QTextTable *textTable, KDReports::ReportBuilder &builder, QTextDocument &textDoc, QTextTableCell &cell) { cellCursor = cell.firstCursorPosition(); @@ -112,7 +97,7 @@ void FillCellHelper::fill(QTextTable *textTable, KDReports::ReportBuilder &build if (background.canConvert()) { cellFormat.setBackground(qvariant_cast(background)); } - cellFormat.setVerticalAlignment(toVerticalAlignment(alignment)); + cellFormat.setVerticalAlignment(KDReports::ReportBuilder::toVerticalAlignment(alignment)); cell.setFormat(cellFormat); QTextBlockFormat blockFormat = cellCursor.blockFormat(); diff --git a/src/KDReports/KDReportsCell.cpp b/src/KDReports/KDReportsCell.cpp index f7afcdec..3e558f45 100644 --- a/src/KDReports/KDReportsCell.cpp +++ b/src/KDReports/KDReportsCell.cpp @@ -19,6 +19,7 @@ class KDReports::CellPrivate QList m_elements; int m_columnSpan = 1; int m_rowSpan = 1; + Qt::AlignmentFlag m_verticalAlignment = Qt::AlignmentFlag(0); }; KDReports::Cell::Cell() @@ -85,6 +86,16 @@ void KDReports::Cell::addVerticalSpacing(qreal space) d->m_elements.append(KDReports::ElementData(KDReports::ElementData::VerticalSpacing, space)); } +void KDReports::Cell::setVerticalAlignment(Qt::AlignmentFlag verticalAlignment) +{ + d->m_verticalAlignment = verticalAlignment; +} + +Qt::AlignmentFlag KDReports::Cell::verticalAlignment() const +{ + return d->m_verticalAlignment; +} + void KDReports::Cell::build(ReportBuilder &builder) const { foreach (const KDReports::ElementData &ed, d->m_elements) { diff --git a/src/KDReports/KDReportsCell.h b/src/KDReports/KDReportsCell.h index aca5cea5..73da6d45 100644 --- a/src/KDReports/KDReportsCell.h +++ b/src/KDReports/KDReportsCell.h @@ -78,6 +78,17 @@ class KDREPORTS_EXPORT Cell final : public Element */ void addVerticalSpacing(qreal space); + /*! + * Set the vertical alignment of the cell contents + * \since 2.3 + */ + void setVerticalAlignment(Qt::AlignmentFlag verticalAlignment); + /*! + * Returns the vertical alignment of the cell contents + * \since 2.3 + */ + Qt::AlignmentFlag verticalAlignment() const; + /** * @internal * @reimp diff --git a/src/KDReports/KDReportsReportBuilder.cpp b/src/KDReports/KDReportsReportBuilder.cpp index 4363718a..02c08d4a 100644 --- a/src/KDReports/KDReportsReportBuilder.cpp +++ b/src/KDReports/KDReportsReportBuilder.cpp @@ -210,3 +210,18 @@ int KDReports::ReportBuilder::currentPosition() { return m_cursor.position(); } + +QTextCharFormat::VerticalAlignment KDReports::ReportBuilder::toVerticalAlignment(Qt::Alignment alignment) +{ + switch (alignment & Qt::AlignVertical_Mask) { + case Qt::AlignTop: + return QTextCharFormat::AlignTop; + case Qt::AlignBottom: + return QTextCharFormat::AlignBottom; + case Qt::AlignVCenter: + return QTextCharFormat::AlignMiddle; + case Qt::AlignBaseline: + return QTextCharFormat::AlignBaseline; + } + return QTextCharFormat::AlignNormal; +} diff --git a/src/KDReports/KDReportsReportBuilder_p.h b/src/KDReports/KDReportsReportBuilder_p.h index 1cd831e7..91f9f0db 100644 --- a/src/KDReports/KDReportsReportBuilder_p.h +++ b/src/KDReports/KDReportsReportBuilder_p.h @@ -105,6 +105,8 @@ class KDREPORTS_EXPORT ReportBuilder void copyStateFrom(const ReportBuilder &parentBuilder); int currentPosition(); + static QTextCharFormat::VerticalAlignment toVerticalAlignment(Qt::Alignment alignment); + private: ReportBuilder(const ReportBuilder &other) = delete; ReportBuilder &operator=(const ReportBuilder &other) = delete; diff --git a/src/KDReports/KDReportsTableElement.cpp b/src/KDReports/KDReportsTableElement.cpp index 5d2492b9..63226563 100644 --- a/src/KDReports/KDReportsTableElement.cpp +++ b/src/KDReports/KDReportsTableElement.cpp @@ -42,6 +42,8 @@ class CellContentMap : public QMap, Cell> class KDReports::TableElementPrivate { public: + void createCell(QTextTable *textTable, ReportBuilder &builder, int row, int column, const Cell &cell, QTextCharFormat charFormat) const; + KDReports::CellContentMap m_cellContentMap; int m_headerRowCount = 0; int m_headerColumnCount = 0; @@ -105,6 +107,28 @@ KDReports::Cell &KDReports::TableElement::cell(int row, int column) return d->m_cellContentMap[coord]; // find or create } +void KDReports::TableElementPrivate::createCell(QTextTable *textTable, ReportBuilder &builder, int row, int column, const Cell &cell, QTextCharFormat charFormat) const +{ + if (cell.columnSpan() > 1 || cell.rowSpan() > 1) + textTable->mergeCells(row, column, cell.rowSpan(), cell.columnSpan()); + QTextTableCell tableCell = textTable->cellAt(row, column); + Q_ASSERT(tableCell.isValid()); + QTextCursor cellCursor = tableCell.firstCursorPosition(); + QTextCharFormat tableCellFormat = charFormat; + if (cell.background().style() != Qt::NoBrush) + tableCellFormat.setBackground(cell.background()); + tableCellFormat.setTableCellColumnSpan(cell.columnSpan()); + tableCellFormat.setTableCellRowSpan(cell.rowSpan()); + if (cell.verticalAlignment() != 0) + tableCellFormat.setVerticalAlignment(ReportBuilder::toVerticalAlignment(cell.verticalAlignment())); + tableCell.setFormat(tableCellFormat); + cellCursor.setCharFormat(tableCellFormat); + ReportBuilder cellBuilder(builder.currentDocumentData(), cellCursor, builder.report()); + cellBuilder.copyStateFrom(builder); + cellBuilder.setDefaultFont(charFormat.font()); + cell.build(cellBuilder); +} + void KDReports::TableElement::build(ReportBuilder &builder) const { if (d->m_cellContentMap.isEmpty()) @@ -131,22 +155,7 @@ void KDReports::TableElement::build(ReportBuilder &builder) const const int row = it.key().first; const int column = it.key().second; const Cell &cell = it.value(); - if (cell.columnSpan() > 1 || cell.rowSpan() > 1) - textTable->mergeCells(row, column, cell.rowSpan(), cell.columnSpan()); - QTextTableCell tableCell = textTable->cellAt(row, column); - Q_ASSERT(tableCell.isValid()); - QTextCursor cellCursor = tableCell.firstCursorPosition(); - QTextCharFormat tableCellFormat = charFormat; - if (cell.background().style() != Qt::NoBrush) - tableCellFormat.setBackground(cell.background()); - tableCellFormat.setTableCellColumnSpan(cell.columnSpan()); - tableCellFormat.setTableCellRowSpan(cell.rowSpan()); - tableCell.setFormat(tableCellFormat); - cellCursor.setCharFormat(tableCellFormat); - ReportBuilder cellBuilder(builder.currentDocumentData(), cellCursor, builder.report()); - cellBuilder.copyStateFrom(builder); - cellBuilder.setDefaultFont(charFormat.font()); - cell.build(cellBuilder); + d->createCell(textTable, builder, row, column, cell, charFormat); } textDocCursor.movePosition(QTextCursor::End);