Skip to content

Commit

Permalink
Cell: add setVerticalAlignment method
Browse files Browse the repository at this point in the history
  • Loading branch information
dfaure-kdab committed Jun 8, 2023
1 parent f86ede1 commit 3082185
Show file tree
Hide file tree
Showing 7 changed files with 66 additions and 32 deletions.
1 change: 1 addition & 0 deletions docs/CHANGES_2_3.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
17 changes: 1 addition & 16 deletions src/KDReports/KDReportsAutoTableElement.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,29 +90,14 @@ 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();
QTextCharFormat cellFormat = cell.format();
if (background.canConvert<QBrush>()) {
cellFormat.setBackground(qvariant_cast<QBrush>(background));
}
cellFormat.setVerticalAlignment(toVerticalAlignment(alignment));
cellFormat.setVerticalAlignment(KDReports::ReportBuilder::toVerticalAlignment(alignment));
cell.setFormat(cellFormat);

QTextBlockFormat blockFormat = cellCursor.blockFormat();
Expand Down
11 changes: 11 additions & 0 deletions src/KDReports/KDReportsCell.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ class KDReports::CellPrivate
QList<ElementData> m_elements;
int m_columnSpan = 1;
int m_rowSpan = 1;
Qt::AlignmentFlag m_verticalAlignment = Qt::AlignmentFlag(0);
};

KDReports::Cell::Cell()
Expand Down Expand Up @@ -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) {
Expand Down
11 changes: 11 additions & 0 deletions src/KDReports/KDReportsCell.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
15 changes: 15 additions & 0 deletions src/KDReports/KDReportsReportBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
2 changes: 2 additions & 0 deletions src/KDReports/KDReportsReportBuilder_p.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
41 changes: 25 additions & 16 deletions src/KDReports/KDReportsTableElement.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ class CellContentMap : public QMap<QPair<int /*row*/, int /*column*/>, 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;
Expand Down Expand Up @@ -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())
Expand All @@ -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);
Expand Down

0 comments on commit 3082185

Please sign in to comment.