From d3a0bf018fdd6d70f6a232746ae4daf6aad85c03 Mon Sep 17 00:00:00 2001 From: wmayer Date: Sat, 4 Dec 2021 23:36:11 +0100 Subject: [PATCH] Spreadsheet: issue 0002957: spreadsheet direct printing --- src/Mod/Spreadsheet/Gui/SheetTableView.cpp | 72 +++++++++++++++++++- src/Mod/Spreadsheet/Gui/SheetTableView.h | 1 + src/Mod/Spreadsheet/Gui/SpreadsheetView.cpp | 74 ++++++++++++++++++--- src/Mod/Spreadsheet/Gui/SpreadsheetView.h | 8 +++ 4 files changed, 144 insertions(+), 11 deletions(-) diff --git a/src/Mod/Spreadsheet/Gui/SheetTableView.cpp b/src/Mod/Spreadsheet/Gui/SheetTableView.cpp index e65365c3eb..7a99207cb8 100644 --- a/src/Mod/Spreadsheet/Gui/SheetTableView.cpp +++ b/src/Mod/Spreadsheet/Gui/SheetTableView.cpp @@ -30,7 +30,9 @@ # include # include # include +# include #endif +# include #include #include @@ -887,16 +889,18 @@ void SheetTableView::edit ( const QModelIndex & index ) QTableView::edit(index); } -void SheetTableView::contextMenuEvent(QContextMenuEvent *) { +void SheetTableView::contextMenuEvent(QContextMenuEvent *) +{ const QMimeData* mimeData = QApplication::clipboard()->mimeData(); - if(!selectionModel()->hasSelection()) { + if (!selectionModel()->hasSelection()) { actionCut->setEnabled(false); actionCopy->setEnabled(false); actionDel->setEnabled(false); actionPaste->setEnabled(false); actionSplit->setEnabled(false); actionMerge->setEnabled(false); - }else{ + } + else { actionPaste->setEnabled(mimeData && (mimeData->hasText() || mimeData->hasText())); actionCut->setEnabled(true); actionCopy->setEnabled(true); @@ -908,4 +912,66 @@ void SheetTableView::contextMenuEvent(QContextMenuEvent *) { contextMenu->exec(QCursor::pos()); } +QString SheetTableView::toHtml() const +{ + std::set cells = sheet->getCells()->getUsedCells(); + int rowCount = 1; + int colCount = 1; + for (const auto& it : cells) { + rowCount = std::max(rowCount, it.row()); + colCount = std::max(colCount, it.col()); + } + + std::unique_ptr doc(new QTextDocument); + doc->setDocumentMargin(10); + QTextCursor cursor(doc.get()); + + cursor.movePosition(QTextCursor::Start); + + QTextTableFormat tableFormat; + tableFormat.setCellSpacing(0.0); + tableFormat.setCellPadding(2.0); + + QTextCharFormat boldFormat; + QFont boldFont = boldFormat.font(); + boldFont.setBold(true); + boldFormat.setFont(boldFont); + + QColor bgColor; + bgColor.setNamedColor(QLatin1String("#f0f0f0")); + QTextCharFormat bgFormat; + bgFormat.setBackground(QBrush(bgColor)); + + QTextTable *table = cursor.insertTable(rowCount + 2, colCount + 2, tableFormat); + for (int row = 0; row < rowCount + 1; row++) { + QTextTableCell headerCell = table->cellAt(row+1, 0); + headerCell.setFormat(bgFormat); + QTextCursor headerCellCursor = headerCell.firstCursorPosition(); + QString data = model()->headerData(row, Qt::Vertical).toString(); + headerCellCursor.insertText(data, boldFormat); + } + for (int col = 0; col < colCount + 1; col++) { + QTextTableCell headerCell = table->cellAt(0, col+1); + headerCell.setFormat(bgFormat); + QTextCursor headerCellCursor = headerCell.firstCursorPosition(); + QTextBlockFormat blockFormat = headerCellCursor.blockFormat(); + blockFormat.setAlignment(Qt::AlignHCenter); + headerCellCursor.setBlockFormat(blockFormat); + QString data = model()->headerData(col, Qt::Horizontal).toString(); + headerCellCursor.insertText(data, boldFormat); + } + + for (const auto& it : cells) { + QTextCharFormat cellFormat; + QTextTableCell cell = table->cellAt(it.row() + 1, it.col() + 1); + QTextCursor cellCursor = cell.firstCursorPosition(); + QString data = model()->data(model()->index(it.row(), it.col())).toString().simplified(); + cellCursor.insertText(data, cellFormat); + } + + cursor.movePosition(QTextCursor::End); + cursor.insertBlock(); + return doc->toHtml(); +} + #include "moc_SheetTableView.cpp" diff --git a/src/Mod/Spreadsheet/Gui/SheetTableView.h b/src/Mod/Spreadsheet/Gui/SheetTableView.h index 840a4961e2..b8e6c62522 100644 --- a/src/Mod/Spreadsheet/Gui/SheetTableView.h +++ b/src/Mod/Spreadsheet/Gui/SheetTableView.h @@ -58,6 +58,7 @@ public: void edit(const QModelIndex &index); void setSheet(Spreadsheet::Sheet *_sheet); std::vector selectedRanges() const; + QString toHtml() const; public Q_SLOTS: void mergeCells(); diff --git a/src/Mod/Spreadsheet/Gui/SpreadsheetView.cpp b/src/Mod/Spreadsheet/Gui/SpreadsheetView.cpp index 3595268f2d..2a9a7a2886 100644 --- a/src/Mod/Spreadsheet/Gui/SpreadsheetView.cpp +++ b/src/Mod/Spreadsheet/Gui/SpreadsheetView.cpp @@ -26,10 +26,14 @@ # include # include # include +# include +# include +# include # include # include # include # include +# include # include # include # include @@ -46,6 +50,7 @@ #include #include #include +#include #include #include #include @@ -191,22 +196,75 @@ bool SheetView::onHasMsg(const char *pMsg) const App::Document* doc = getAppDocument(); return doc && doc->getAvailableUndos() > 0; } - else if (strcmp("Redo",pMsg) == 0) { + if (strcmp("Redo",pMsg) == 0) { App::Document* doc = getAppDocument(); return doc && doc->getAvailableRedos() > 0; } - else if (strcmp("Save",pMsg) == 0) + if (strcmp("Save",pMsg) == 0) return true; - else if (strcmp("SaveAs",pMsg) == 0) + if (strcmp("SaveAs",pMsg) == 0) return true; - else if (strcmp("Cut",pMsg) == 0) + if (strcmp("Cut",pMsg) == 0) return true; - else if (strcmp("Copy",pMsg) == 0) + if (strcmp("Copy",pMsg) == 0) return true; - else if (strcmp("Paste",pMsg) == 0) + if (strcmp("Paste",pMsg) == 0) return true; - else - return false; + if (strcmp(pMsg, "Print") == 0) + return true; + if (strcmp(pMsg, "PrintPreview") == 0) + return true; + if (strcmp(pMsg, "PrintPdf") == 0) + return true; + + return false; +} + +/** + * Shows the printer dialog. + */ +void SheetView::print() +{ + QPrinter printer(QPrinter::ScreenResolution); + printer.setFullPage(true); + QPrintDialog dlg(&printer, this); + if (dlg.exec() == QDialog::Accepted) { + print(&printer); + } +} + +void SheetView::printPreview() +{ + QPrinter printer(QPrinter::ScreenResolution); + QPrintPreviewDialog dlg(&printer, this); + connect(&dlg, SIGNAL(paintRequested (QPrinter *)), + this, SLOT(print(QPrinter *))); + dlg.exec(); +} + +void SheetView::print(QPrinter* printer) +{ +#if 0 + ui->cells->render(printer); +#endif + std::unique_ptr document = std::make_unique(); + document->setHtml(ui->cells->toHtml()); + document->print(printer); +} + +/** + * Prints the document into a Pdf file. + */ +void SheetView::printPdf() +{ + QString filename = FileDialog::getSaveFileName(this, tr("Export PDF"), QString(), + QString::fromLatin1("%1 (*.pdf)").arg(tr("PDF file"))); + if (!filename.isEmpty()) { + QPrinter printer(QPrinter::ScreenResolution); + printer.setOutputFormat(QPrinter::PdfFormat); + printer.setOutputFileName(filename); + print(&printer); + } } void SheetView::setCurrentCell(QString str) diff --git a/src/Mod/Spreadsheet/Gui/SpreadsheetView.h b/src/Mod/Spreadsheet/Gui/SpreadsheetView.h index ec96b81d06..46d6665a6c 100644 --- a/src/Mod/Spreadsheet/Gui/SpreadsheetView.h +++ b/src/Mod/Spreadsheet/Gui/SpreadsheetView.h @@ -66,6 +66,14 @@ public: bool onMsg(const char* pMsg,const char** ppReturn); bool onHasMsg(const char* pMsg) const; + /** @name Printing */ + //@{ + void print(); + void printPdf(); + void printPreview(); + void print(QPrinter*); + //@} + void updateCell(const App::Property * prop); Spreadsheet::Sheet * getSheet() { return sheet; }