Spreadsheet: issue 0002957: spreadsheet direct printing

This commit is contained in:
wmayer
2021-12-04 23:36:11 +01:00
parent 632af47a0d
commit d3a0bf018f
4 changed files with 144 additions and 11 deletions

View File

@@ -30,7 +30,9 @@
# include <QMenu>
# include <QMessageBox>
# include <QMimeData>
# include <QTextStream>
#endif
# include <QTextTableCell>
#include <App/Application.h>
#include <App/AutoTransaction.h>
@@ -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<App::CellAddress> 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<QTextDocument> 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"

View File

@@ -58,6 +58,7 @@ public:
void edit(const QModelIndex &index);
void setSheet(Spreadsheet::Sheet *_sheet);
std::vector<App::Range> selectedRanges() const;
QString toHtml() const;
public Q_SLOTS:
void mergeCells();

View File

@@ -26,10 +26,14 @@
# include <QApplication>
# include <QMenu>
# include <QMouseEvent>
# include <QPrinter>
# include <QPrintDialog>
# include <QPrintPreviewDialog>
# include <QSlider>
# include <QStatusBar>
# include <QToolBar>
# include <QTableWidgetItem>
# include <QTextDocument>
# include <QMessageBox>
# include <QPalette>
# include <cmath>
@@ -46,6 +50,7 @@
#include <Gui/CommandT.h>
#include <Gui/Document.h>
#include <Gui/ExpressionCompleter.h>
#include <Gui/FileDialog.h>
#include <LineEdit.h>
#include <Mod/Spreadsheet/App/Sheet.h>
#include <Mod/Spreadsheet/App/SheetPy.h>
@@ -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<QTextDocument> document = std::make_unique<QTextDocument>();
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)

View File

@@ -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; }