int index=ui->m_tableWidgetLogger->currentRow();
if(index<0)
return;
int screenResolution = screenPrinter.resolution();
printer.
setOrientation(QPrinter::Portrait);
printer.
setOutputFormat(QPrinter::PdfFormat);
printer.setOutputFileName(fileName);
painter.begin(&printer);
//////INSERTS THIS BOARDER DESGIN MIGHT HAVE TO CHANGE
// QPen pen(Qt::black, 2, Qt::SolidLine);
// painter.setPen(pen);
// painter.drawRoundRect(0,0,9400,1050,15,15);
// QPen pen(Qt::black, 20);
// painter.setPen(pen);
// QLineF line(30,30,15000,30);
// painter.drawLine(line);
// painter.setFont(QFont("Helvetica",24,QFont::Bold));
head= "Escort Data Logger Report";
// QRectF source(0,0,70,40);
// painter.drawText(rect,"report",QTextOption(Qt::AlignCenter));
QImage image
(":/Pictures/escort.png");
font.setBold(true);
QString dateTimeString
= dateTime.
toString();
painter.drawImage(rect,image);
painter.
drawText(painter.
window(),Qt
::AlignCenter|Qt
::AlignBottom,
QString::number(1));
painter.
drawText(painter.
window(),Qt
::AlignRight|Qt
::AlignJustify,
QString("Date: "+dateTimeString
));
// Print Summary
m_mainApp->m_table->SetTableWidget(summaryTab);
// summaryTab->setGeometry(700,0,printer.pageRect().width(),printer.pageRect().height());
summaryTab->setAutoScroll(false);
m_mainApp->m_table->InitTableSummary(summaryTab);
m_mainApp->m_table->FillTableSummary(summaryTab,index);
summaryTab->setAlternatingRowColors(true);
summaryTab
->setFrameShape
(QFrame::Box);
painter.save();
editor->indentWidth();
painter.scale(printer.resolution()/(screenResolution*1),printer.resolution()/screenResolution*.55f);
editor->drawContents(&painter);
painter.restore();
if(m_mainApp->mLoggerList[index].recCurrentAddress)
{
// Print Graph
painter.
drawText(painter.
window(),Qt
::AlignRight|Qt
::AlignTop,
QString("Date: "+dateTimeString
));
QwtPlotRenderer renderer;
renderer.setDiscardFlag(QwtPlotRenderer::DiscardBackground,false);
renderer.setLayoutFlag(QwtPlotRenderer::KeepFrames,true);
renderer.
render(m_plot,
&painter,
QRect(0,printer.
height()/2,printer.
width(),printer.
height()/2.2));
printer.newPage();
// Print Table
m_mainApp->m_table->SetTableWidget(table);
table->setGeometry(0,0,printer.pageRect().width(),printer.pageRect().height());
table->setAutoScroll(false);
m_mainApp->m_table->InitTablePrint(index,printer.pageRect().width());
int nbPage=m_mainApp->m_table->GetNbPrintPage(index,printer.pageRect().height());
for(int page=0;page<nbPage;page++)
{
painter.drawImage(rect,image);
painter.
drawText(painter.
window(),Qt
::AlignRight|Qt
::AlignTop,
QString("Date: "+dateTimeString
));
painter.
drawText(painter.
window(),Qt
::AlignCenter|Qt
::AlignBottom,
QString::number(page
+2));
m_mainApp->m_table->currentIndex=-1;
m_mainApp->m_table->InitTablePrint(index,printer.pageRect().width());
m_mainApp->m_table->FillTablePrint(index,printer.pageRect().height(),page);
painter.save();
painter.scale(printer.resolution()/(screenResolution*1.2f),printer.resolution()/screenResolution*.55f);
editor->drawContents(&painter);
painter.restore();
printer.newPage();
}
// Print Histogram
painter.drawImage(rect,image);
painter.
drawText(painter.
window(),Qt
::AlignRight|Qt
::AlignTop,
QString("Date: "+dateTimeString
));
painter.
drawText(painter.
window(),Qt
::AlignCenter|Qt
::AlignBottom,
QString::number(nbPage
+2));
renderer.setDiscardFlag(QwtPlotRenderer::DiscardBackground,false);
renderer.setLayoutFlag(QwtPlotRenderer::KeepFrames,true);
// renderer.render(m_histogram,&painter,QRect(0,0,printer.width(),printer.height()/2.2));
renderer.
render(m_histogram,
&painter,
QRect(0,printer.
height()/6,printer.
width(),printer.
height()/2.5));
///was 2.2
painter.end();
}
#if(DEBUG_MODE==1)
qDebug() << "GeneratePdf...";
#endif
}
{
// editor->setDocumentMargin(25);
//creates writing format
NormalFormat.
setFont(QFont("Helvetica",
24,
QFont::Bold));
//create pointer to current cursor location
cursor.beginEditBlock();
// // cursor.insertImage(QImage(":/Pictures/escort.png"));
cursor.insertBlock();
cursor.insertBlock();
cursor.insertBlock();
cursor.insertBlock();
cursor.insertBlock();
cursor.insertBlock();
cursor.insertBlock();
cursor.insertBlock();
cursor.insertBlock();
cursor.insertBlock();
cursor.insertBlock();
cursor.insertText(" ",NormalFormat);
cursor.insertText("Escort Data Logger Report",NormalFormat);
cursor.insertBlock();
cursor.insertBlock();
cursor.insertBlock();
textEdit.setAlignment(Qt::AlignCenter);
QTextTable *tab
=cursor.
insertTable(tabW
->rowCount
()+1,tabW
->columnCount
(),tableFormat
);
tableFormat.
setBackground(QColor("#ffffff"));
tableFormat.setCellPadding(0);
tableFormat.setCellSpacing(4);
//tableFormat.setHeight(QTextLength(QTextLength::PercentageLength,10));
frameFormat.setBorder(0);
frameFormat.setLeftMargin(50);
frameFormat.setTopMargin(10);
// frameFormat.setBottomMargin(50);
cursor.insertFrame(frameFormat);
cursor.setPosition(frame->lastPosition());
frame->setFrameFormat(frameFormat);
format_entete_tab.setFontPointSize(15);
format_entete_tab.
setFontWeight(QFont::Bold);
format_cellule.setFontPointSize(11);
for(int i=0;i<tabW->columnCount();i++)
{
QTextTableCell titre
= tab
->cellAt
(0,i
);
// change 0 it moves the title cellCursor.insertText(tabW->horizontalHeaderItem(i)->text(),format_entete_tab);
}
for(int row=1;row<tab->rows();row++)
{
for(int col=0;col<tab->columns();col++)
{
cell=tab->cellAt(row,col);
cellCursor=cell.firstCursorPosition();
cellCursor.insertText(tr("%1").arg(tabW->item(row-1,col)->text()),format_cellule);
}
}
cursor.endEditBlock();
return editor;
int index=ui->m_tableWidgetLogger->currentRow();
if(index<0)
return;
QPrinter screenPrinter(QPrinter::ScreenResolution);
int screenResolution = screenPrinter.resolution();
QPrinter printer(QPrinter::HighResolution);
printer.setOrientation(QPrinter::Portrait);
printer.setPaperSize(QPrinter::A4);
printer.setOutputFormat(QPrinter::PdfFormat);
printer.setOutputFileName(fileName);
QPainter painter;
painter.begin(&printer);
//////INSERTS THIS BOARDER DESGIN MIGHT HAVE TO CHANGE
// QPen pen(Qt::black, 2, Qt::SolidLine);
// painter.setPen(pen);
// painter.drawRoundRect(0,0,9400,1050,15,15);
// QPen pen(Qt::black, 20);
// painter.setPen(pen);
// QLineF line(30,30,15000,30);
// painter.drawLine(line);
// painter.setFont(QFont("Helvetica",24,QFont::Bold));
QRectF rect(10,10,2000,1000);
QString head;
head= "Escort Data Logger Report";
// QRectF source(0,0,70,40);
// painter.drawText(rect,"report",QTextOption(Qt::AlignCenter));
QDateTime dateTime = QDateTime::currentDateTime();
QImage image(":/Pictures/escort.png");
QFont font;
font.setBold(true);
QString dateTimeString = dateTime.toString();
painter.drawImage(rect,image);
painter.drawText(painter.window(),Qt::AlignCenter|Qt::AlignBottom,QString::number(1));
painter.drawText(painter.window(),Qt::AlignRight|Qt::AlignJustify,QString("Date: "+dateTimeString));
// Print Summary
QTableWidget *summaryTab=new QTableWidget();
m_mainApp->m_table->SetTableWidget(summaryTab);
// summaryTab->setGeometry(700,0,printer.pageRect().width(),printer.pageRect().height());
summaryTab->setAutoScroll(false);
m_mainApp->m_table->InitTableSummary(summaryTab);
m_mainApp->m_table->FillTableSummary(summaryTab,index);
summaryTab->setAlternatingRowColors(true);
summaryTab->setFrameShape(QFrame::Box);
painter.save();
QTextDocument *editor = QTableWidget2QTextBrowser(summaryTab);
editor->indentWidth();
painter.scale(printer.resolution()/(screenResolution*1),printer.resolution()/screenResolution*.55f);
editor->drawContents(&painter);
painter.restore();
if(m_mainApp->mLoggerList[index].recCurrentAddress)
{
// Print Graph
painter.drawText(painter.window(),Qt::AlignRight|Qt::AlignTop,QString("Date: "+dateTimeString));
QwtPlotRenderer renderer;
renderer.setDiscardFlag(QwtPlotRenderer::DiscardBackground,false);
renderer.setLayoutFlag(QwtPlotRenderer::KeepFrames,true);
renderer.render(m_plot,&painter,QRect(0,printer.height()/2,printer.width(),printer.height()/2.2));
printer.newPage();
// Print Table
QTableWidget *table=new QTableWidget();
m_mainApp->m_table->SetTableWidget(table);
table->setGeometry(0,0,printer.pageRect().width(),printer.pageRect().height());
table->setAutoScroll(false);
m_mainApp->m_table->InitTablePrint(index,printer.pageRect().width());
int nbPage=m_mainApp->m_table->GetNbPrintPage(index,printer.pageRect().height());
for(int page=0;page<nbPage;page++)
{
painter.drawImage(rect,image);
painter.drawText(painter.window(),Qt::AlignRight|Qt::AlignTop,QString("Date: "+dateTimeString));
painter.drawText(painter.window(),Qt::AlignCenter|Qt::AlignBottom,QString::number(page+2));
m_mainApp->m_table->currentIndex=-1;
m_mainApp->m_table->InitTablePrint(index,printer.pageRect().width());
m_mainApp->m_table->FillTablePrint(index,printer.pageRect().height(),page);
painter.save();
QTextDocument *editor = QTableWidget2QTextBrowser(table);
painter.scale(printer.resolution()/(screenResolution*1.2f),printer.resolution()/screenResolution*.55f);
editor->drawContents(&painter);
painter.restore();
printer.newPage();
}
// Print Histogram
painter.drawImage(rect,image);
painter.drawText(painter.window(),Qt::AlignRight|Qt::AlignTop,QString("Date: "+dateTimeString));
painter.drawText(painter.window(),Qt::AlignCenter|Qt::AlignBottom,QString::number(nbPage+2));
renderer.setDiscardFlag(QwtPlotRenderer::DiscardBackground,false);
renderer.setLayoutFlag(QwtPlotRenderer::KeepFrames,true);
// renderer.render(m_histogram,&painter,QRect(0,0,printer.width(),printer.height()/2.2));
renderer.render(m_histogram,&painter,QRect(0,printer.height()/6,printer.width(),printer.height()/2.5));///was 2.2
painter.end();
}
#if(DEBUG_MODE==1)
qDebug() << "GeneratePdf...";
#endif
}
QTextDocument *MainWindow::QTableWidget2QTextBrowser(QTableWidget *tabW)
{
QTextDocument *editor = new QTextDocument;
// editor->setDocumentMargin(25);
//creates writing format
QTextCharFormat NormalFormat;
NormalFormat.setFont(QFont("Helvetica",24,QFont::Bold));
//create pointer to current cursor location
QTextCursor cursor (editor);
cursor.beginEditBlock();
cursor.movePosition(QTextCursor::Start);
// // cursor.insertImage(QImage(":/Pictures/escort.png"));
cursor.insertBlock();
cursor.insertBlock();
cursor.insertBlock();
cursor.insertBlock();
cursor.insertBlock();
cursor.insertBlock();
cursor.insertBlock();
cursor.insertBlock();
cursor.insertBlock();
cursor.insertBlock();
cursor.insertBlock();
cursor.insertText(" ",NormalFormat);
cursor.insertText("Escort Data Logger Report",NormalFormat);
cursor.insertBlock();
cursor.insertBlock();
cursor.insertBlock();
QTextTableFormat tableFormat;
QTextEdit textEdit;
textEdit.setAlignment(Qt::AlignCenter);
QTextTable *tab=cursor.insertTable(tabW->rowCount()+1,tabW->columnCount(),tableFormat);
tableFormat.setBackground(QColor("#ffffff"));
tableFormat.setCellPadding(0);
tableFormat.setCellSpacing(4);
//tableFormat.setHeight(QTextLength(QTextLength::PercentageLength,10));
QTextFrame *frame=cursor.currentFrame();
QTextFrameFormat frameFormat=frame->frameFormat();
frameFormat.setBorder(0);
frameFormat.setLeftMargin(50);
frameFormat.setTopMargin(10);
// frameFormat.setBottomMargin(50);
cursor.insertFrame(frameFormat);
cursor.setPosition(frame->lastPosition());
frameFormat.setBorderStyle(QTextFrameFormat::BorderStyle_Inset);
frame->setFrameFormat(frameFormat);
QTextCharFormat format_entete_tab;
format_entete_tab.setFontPointSize(15);
format_entete_tab.setFontWeight(QFont::Bold);
QTextCharFormat format_cellule;
format_cellule.setFontPointSize(11);
for(int i=0;i<tabW->columnCount();i++)
{
QTextTableCell titre = tab->cellAt(0,i);// change 0 it moves the title
QTextCursor cellCursor=titre.firstCursorPosition();
cellCursor.insertText(tabW->horizontalHeaderItem(i)->text(),format_entete_tab);
}
QTextTableCell cell;
QTextCursor cellCursor;
for(int row=1;row<tab->rows();row++)
{
for(int col=0;col<tab->columns();col++)
{
cell=tab->cellAt(row,col);
cellCursor=cell.firstCursorPosition();
cellCursor.insertText(tr("%1").arg(tabW->item(row-1,col)->text()),format_cellule);
}
}
cursor.endEditBlock();
return editor;
To copy to clipboard, switch view to plain text mode
Bookmarks