From 67f8a4c61c19e7a69a43c4e11668eea1de5886b9 Mon Sep 17 00:00:00 2001 From: wmayer Date: Sun, 5 Dec 2021 12:16:54 +0100 Subject: [PATCH] Spreadsheet: fix format of table cells when used for printing, by default use landscape orientation --- src/Mod/Spreadsheet/Gui/SheetTableView.cpp | 65 ++++++++++++++++++++- src/Mod/Spreadsheet/Gui/SpreadsheetView.cpp | 3 + 2 files changed, 67 insertions(+), 1 deletion(-) diff --git a/src/Mod/Spreadsheet/Gui/SheetTableView.cpp b/src/Mod/Spreadsheet/Gui/SheetTableView.cpp index 7a99207cb8..d5f079138e 100644 --- a/src/Mod/Spreadsheet/Gui/SheetTableView.cpp +++ b/src/Mod/Spreadsheet/Gui/SheetTableView.cpp @@ -931,6 +931,12 @@ QString SheetTableView::toHtml() const QTextTableFormat tableFormat; tableFormat.setCellSpacing(0.0); tableFormat.setCellPadding(2.0); + QVector constraints; + for (int col = 0; col < colCount + 1; col++) { + constraints.append(QTextLength(QTextLength::FixedLength, sheet->getColumnWidth(col))); + } + constraints.prepend(QTextLength(QTextLength::FixedLength, 30.0)); + tableFormat.setColumnWidthConstraints(constraints); QTextCharFormat boldFormat; QFont boldFont = boldFormat.font(); @@ -943,6 +949,8 @@ QString SheetTableView::toHtml() const bgFormat.setBackground(QBrush(bgColor)); QTextTable *table = cursor.insertTable(rowCount + 2, colCount + 2, tableFormat); + + // The header cells of the rows for (int row = 0; row < rowCount + 1; row++) { QTextTableCell headerCell = table->cellAt(row+1, 0); headerCell.setFormat(bgFormat); @@ -950,6 +958,8 @@ QString SheetTableView::toHtml() const QString data = model()->headerData(row, Qt::Vertical).toString(); headerCellCursor.insertText(data, boldFormat); } + + // The header cells of the columns for (int col = 0; col < colCount + 1; col++) { QTextTableCell headerCell = table->cellAt(0, col+1); headerCell.setFormat(bgFormat); @@ -961,11 +971,64 @@ QString SheetTableView::toHtml() const headerCellCursor.insertText(data, boldFormat); } + // The cells for (const auto& it : cells) { + if (sheet->isMergedCell(it)) { + int rows, cols; + sheet->getSpans(it, rows, cols); + table->mergeCells(it.row() + 1, it.col() + 1, rows, cols); + } + QModelIndex index = model()->index(it.row(), it.col()); + QTextCharFormat cellFormat; QTextTableCell cell = table->cellAt(it.row() + 1, it.col() + 1); + + // font + QVariant font = model()->data(index, Qt::FontRole); + if (font.isValid()) { + cellFormat.setFont(font.value()); + } + + // foreground + QVariant fgColor = model()->data(index, Qt::ForegroundRole); + if (fgColor.isValid()) { + cellFormat.setForeground(QBrush(fgColor.value())); + } + + // background + QVariant cbgClor = model()->data(index, Qt::BackgroundRole); + if (cbgClor.isValid()) { + QTextCharFormat bgFormat; + bgFormat.setBackground(QBrush(cbgClor.value())); + cell.setFormat(bgFormat); + } + QTextCursor cellCursor = cell.firstCursorPosition(); - QString data = model()->data(model()->index(it.row(), it.col())).toString().simplified(); + + // alignment + QVariant align = model()->data(index, Qt::TextAlignmentRole); + if (align.isValid()) { + Qt::Alignment alignment = static_cast(align.toInt()); + QTextBlockFormat blockFormat = cellCursor.blockFormat(); + blockFormat.setAlignment(alignment); + cellCursor.setBlockFormat(blockFormat); + + // This doesn't seem to have any effect on single cells but works if several + // cells are merged + QTextCharFormat::VerticalAlignment valign = QTextCharFormat::AlignMiddle; + QTextCharFormat format = cell.format(); + if (alignment & Qt::AlignTop) { + valign = QTextCharFormat::AlignTop; + } + else if (alignment & Qt::AlignBottom) { + valign = QTextCharFormat::AlignBottom; + } + format.setVerticalAlignment(valign); + cell.setFormat(format); + } + + // text + QString data = model()->data(index).toString().simplified(); cellCursor.insertText(data, cellFormat); } diff --git a/src/Mod/Spreadsheet/Gui/SpreadsheetView.cpp b/src/Mod/Spreadsheet/Gui/SpreadsheetView.cpp index 2a9a7a2886..36b444b4c5 100644 --- a/src/Mod/Spreadsheet/Gui/SpreadsheetView.cpp +++ b/src/Mod/Spreadsheet/Gui/SpreadsheetView.cpp @@ -226,6 +226,7 @@ bool SheetView::onHasMsg(const char *pMsg) const void SheetView::print() { QPrinter printer(QPrinter::ScreenResolution); + printer.setOrientation(QPrinter::Landscape); printer.setFullPage(true); QPrintDialog dlg(&printer, this); if (dlg.exec() == QDialog::Accepted) { @@ -236,6 +237,7 @@ void SheetView::print() void SheetView::printPreview() { QPrinter printer(QPrinter::ScreenResolution); + printer.setOrientation(QPrinter::Landscape); QPrintPreviewDialog dlg(&printer, this); connect(&dlg, SIGNAL(paintRequested (QPrinter *)), this, SLOT(print(QPrinter *))); @@ -261,6 +263,7 @@ void SheetView::printPdf() QString::fromLatin1("%1 (*.pdf)").arg(tr("PDF file"))); if (!filename.isEmpty()) { QPrinter printer(QPrinter::ScreenResolution); + printer.setOrientation(QPrinter::Landscape); printer.setOutputFormat(QPrinter::PdfFormat); printer.setOutputFileName(filename); print(&printer);