From aa8e3bd9e5818e62fb0dc14892bc09554c3866e9 Mon Sep 17 00:00:00 2001 From: Seeker4 Date: Fri, 14 Jun 2019 21:17:43 -0700 Subject: [PATCH 1/3] Partial code for printing in blocks of html. --- oscar/statistics.cpp | 120 +++++++++++++++++++++++++++---------------- oscar/statistics.h | 20 +++++--- 2 files changed, 90 insertions(+), 50 deletions(-) diff --git a/oscar/statistics.cpp b/oscar/statistics.cpp index afa30ed4..a0a47acc 100644 --- a/oscar/statistics.cpp +++ b/oscar/statistics.cpp @@ -620,7 +620,7 @@ QString Statistics::getUserInfo () { const QString table_width = "width=99%"; // Create the page header in HTML. Includes everything from through -QString Statistics::htmlHeader(bool showheader) +QString Statistics::generateHeader(bool showheader) { QString html = QString("")+ "" @@ -647,7 +647,7 @@ QString Statistics::htmlHeader(bool showheader) } // HTML for page footer -QString Statistics::htmlFooter(bool showinfo) +QString Statistics::generateFooter(bool showinfo) { QString html; @@ -1177,8 +1177,8 @@ QString Statistics::GenerateCPAPUsage() // Create the HTML that will be the Statistics page. QString Statistics::GenerateHTML() { - htmlReportHeader = htmlHeader(true); - htmlReportFooter = htmlFooter(true); + htmlReportHeader = generateHeader(true); + htmlReportFooter = generateFooter(true); htmlUsage = GenerateCPAPUsage(); @@ -1189,69 +1189,103 @@ QString Statistics::GenerateHTML() htmlMachineSettings = GenerateRXChanges(); htmlMachines = GenerateMachineList(); - UpdateRecordsBox(); - QString htmlScript = ""; return htmlReportHeader + htmlUsage + htmlMachineSettings + htmlMachines + htmlScript + htmlReportFooter; } +int Statistics::printBlock (QString text, QPrinter *printer, QPainter *painter, int yPos) { + QTextEdit block; + block.setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff); + + QRect rect = printer->pageRect(); + + block.setHtml(text); + qDebug() << "initial text dimensions, width" << block.size().width() << "height" << block.size().height(); + block.resize(rect.width()/4, rect.height()/4); + block.setFrameShape(QFrame::NoFrame); + QSize dims = block.size(); + qDebug() << "resized text dimensions, width" << dims.width() << "height" << dims.height(); + + double xscale = printer->pageRect().width()/double(block.width()); + double yscale = printer->pageRect().height()/double(block.height()); + double scale = qMin(xscale, yscale); + painter->translate(printer->paperRect().x() + printer->pageRect().width()/2, + printer->paperRect().y() + printer->pageRect().height()/2); + painter->scale(scale, scale); + painter->translate(-block.width()/2, -block.height()/2); + + block.render(painter, QPoint(0,yPos)); + + return yPos; +} + +// Print the Statistics page on printer void Statistics::printReport(QWidget * parent) { - QPrinter printer(QPrinter::HighResolution); + QPrinter printer(QPrinter::HighResolution); //The QPrinter class is a paint device that paints on a printer + #ifdef Q_OS_LINUX printer.setPrinterName("Print to File (PDF)"); printer.setOutputFormat(QPrinter::PdfFormat); - QString name = "Statistics"; - QString datestr = QDate::currentDate().toString(Qt::ISODate); + QString name; + QString datestr; -// if (ui->tabWidget->currentWidget() == ui->statisticsTab) { -// name = "Statistics"; -// datestr = QDate::currentDate().toString(Qt::ISODate); -// } else if (ui->tabWidget->currentWidget() == ui->helpTab) { -// name = "Help"; -// datestr = QDateTime::currentDateTime().toString(Qt::ISODate); -// } else { name = "Unknown"; } + if (ui->tabWidget->currentWidget() == ui->statisticsTab) { + name = "Statistics"; + datestr = QDate::currentDate().toString(Qt::ISODate); + } else { name = "Unknown"; } - QString filename = p_pref->Get("{home}/") + name + "_" + p_profile->user->userName() + "_" + datestr + ".pdf"; + QString filename = p_pref->Get("{home}/" + name + "_" + p_profile->user->userName() + "_" + datestr + ".pdf"); printer.setOutputFileName(filename); #endif + printer.setPrintRange(QPrinter::AllPages); + printer.setOrientation(QPrinter::Portrait); + +// Setting default page orientation to landscape for statistics view? // if (ui->tabWidget->currentWidget() == ui->statisticsTab) { // printer.setOrientation(QPrinter::Landscape); -// } else { - printer.setOrientation(QPrinter::Portrait); - //} - printer.setFullPage(false); // This has nothing to do with scaling +// } + +// printer.setPageSize(QPrinter::A4); // Could be QPrinter::Letter +// printer.setOutputFormat(QPrinter::PdfFormat); + + printer.setFullPage(false); // Print only on printable area of page and not in non-printable margins +// printer.setFullPage(true); // Print only on printable area of page and not in non-printable margins printer.setNumCopies(1); printer.setResolution(1200); - //printer.setPaperSize(QPrinter::A4); - //printer.setOutputFormat(QPrinter::PdfFormat); - printer.setPageMargins(5, 5, 5, 5, QPrinter::Millimeter); + printer.setPageMargins(5, 5, 5, 5, QPrinter::Millimeter); // Set physical margins to 5 mm, which must be within printable area + // 5 mm is pretty small and less than most laser printers allow, so + // this will amount to default printer margins + + // Show print dialog to user and allow them to change settings as desired QPrintDialog pdlg(&printer, parent); if (pdlg.exec() == QPrintDialog::Accepted) { - QTextBrowser b; - QPainter painter; - painter.begin(&printer); - - QRect rect = printer.pageRect(); - b.setHtml(htmlReportHeader + htmlUsage + htmlMachineSettings + htmlMachines + htmlReportFooter); - b.resize(rect.width()/4, rect.height()/4); - b.setFrameShape(QFrame::NoFrame); - - double xscale = printer.pageRect().width()/double(b.width()); - double yscale = printer.pageRect().height()/double(b.height()); - double scale = qMin(xscale, yscale); - painter.translate(printer.paperRect().x() + printer.pageRect().width()/2, printer.paperRect().y() + printer.pageRect().height()/2); - painter.scale(scale, scale); - painter.translate(-b.width()/2, -b.height()/2); - - b.render(&painter, QPoint(0,0)); - painter.end(); - +// QString size = ""; + QPainter painter; + painter.begin(&printer); + int yPos = 0; +/* QTextDocument doc; + doc.setPageSize(QSizeF(printer.pageRect().size())); + doc.setDocumentMargin((qreal) 0.5); + QFont font("Times New Roman", 12); + doc.setDefaultFont(font); + doc.setHtml(htmlReportHeader + htmlUsage + htmlMachineSettings + htmlMachines + htmlReportFooter); + doc.setDefaultFont(font); + doc.print(&printer); +*/ + yPos = Statistics::printBlock(htmlReportHeader + htmlUsage + htmlMachineSettings + htmlMachines + htmlReportFooter, &printer, &painter, yPos); +/* yPos = Statistics::printBlock(size+htmlReportHeader, &printer, &painter, yPos); + yPos = Statistics::printBlock(size+htmlUsage, &printer, &painter, yPos); + yPos = Statistics::printBlock(size+htmlMachineSettings, &printer, &painter, yPos); + yPos = Statistics::printBlock(size+htmlMachines, &printer, &painter, yPos); + yPos = Statistics::printBlock(size+htmlReportFooter, &printer, &painter, yPos); +*/ + painter.end(); } } diff --git a/oscar/statistics.h b/oscar/statistics.h index 4c9b0e4d..bc9b4343 100644 --- a/oscar/statistics.h +++ b/oscar/statistics.h @@ -11,6 +11,8 @@ #include #include +#include +#include #include #include #include "SleepLib/schema.h" @@ -164,12 +166,7 @@ class Statistics : public QObject void saveRXChanges(); void updateRXChanges(); - QString getUserInfo(); - QString getRDIorAHIText(); QString GenerateHTML(); - QString GenerateMachineList(); - QString GenerateRXChanges(); - QString GenerateCPAPUsage(); QString UpdateRecordsBox(); @@ -177,9 +174,18 @@ class Statistics : public QObject protected: + QString getUserInfo(); + QString getRDIorAHIText(); + QString htmlNoData(); - QString htmlHeader(bool showheader); - QString htmlFooter(bool showinfo=true); + QString generateHeader(bool showheader); + QString generateFooter(bool showinfo=true); + + QString GenerateMachineList(); + QString GenerateRXChanges(); + QString GenerateCPAPUsage(); + + static int printBlock (QString text, QPrinter *printer, QPainter *painter, int yPos); // Using a map to maintain order QList rows; From 41c7d43f7bf1015e832093fa06d6a63902ffef84 Mon Sep 17 00:00:00 2001 From: Seeker4 Date: Sat, 15 Jun 2019 11:58:02 -0700 Subject: [PATCH 2/3] Print Statistics report with pagination, larger font on landscape orientation. --- oscar/statistics.cpp | 83 ++++++++++---------------------------------- oscar/statistics.h | 2 -- 2 files changed, 18 insertions(+), 67 deletions(-) diff --git a/oscar/statistics.cpp b/oscar/statistics.cpp index a0a47acc..8111d677 100644 --- a/oscar/statistics.cpp +++ b/oscar/statistics.cpp @@ -596,7 +596,7 @@ QString Statistics::getUserInfo () { QString address = p_profile->user->address(); address.replace("\n", "
"); - QString userinfo; + QString userinfo = ""; if (!p_profile->user->firstName().isEmpty()) { userinfo = tr("Name: %1, %2").arg(p_profile->user->lastName()).arg(p_profile->user->firstName()) + "
"; @@ -614,6 +614,9 @@ QString Statistics::getUserInfo () { } } + while (userinfo.length() > 0 && userinfo.endsWith("
")) // Strip trailing newlines + userinfo = userinfo.mid(0, userinfo.length()-5); + return userinfo; } @@ -633,15 +636,15 @@ QString Statistics::generateHeader(bool showheader) html += "
" "" "" - "" - "" "" "
" + getUserInfo() + "" + "" "" + STR_TR_OSCAR + "   
" "" + QObject::tr("Usage Statistics") + "   " "
" + resizeHTMLPixmap(logoPixmap,120,120)+"   
" + "
" + resizeHTMLPixmap(logoPixmap,80,80)+"   
" "
" - "

"; + ""; } return html; } @@ -1194,36 +1197,10 @@ QString Statistics::GenerateHTML() return htmlReportHeader + htmlUsage + htmlMachineSettings + htmlMachines + htmlScript + htmlReportFooter; } -int Statistics::printBlock (QString text, QPrinter *printer, QPainter *painter, int yPos) { - QTextEdit block; - block.setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff); - - QRect rect = printer->pageRect(); - - block.setHtml(text); - qDebug() << "initial text dimensions, width" << block.size().width() << "height" << block.size().height(); - block.resize(rect.width()/4, rect.height()/4); - block.setFrameShape(QFrame::NoFrame); - QSize dims = block.size(); - qDebug() << "resized text dimensions, width" << dims.width() << "height" << dims.height(); - - double xscale = printer->pageRect().width()/double(block.width()); - double yscale = printer->pageRect().height()/double(block.height()); - double scale = qMin(xscale, yscale); - painter->translate(printer->paperRect().x() + printer->pageRect().width()/2, - printer->paperRect().y() + printer->pageRect().height()/2); - painter->scale(scale, scale); - painter->translate(-block.width()/2, -block.height()/2); - - block.render(painter, QPoint(0,yPos)); - - return yPos; -} - // Print the Statistics page on printer void Statistics::printReport(QWidget * parent) { - QPrinter printer(QPrinter::HighResolution); //The QPrinter class is a paint device that paints on a printer + QPrinter printer(QPrinter::ScreenResolution); // ScreenResolution required for graphics sizing #ifdef Q_OS_LINUX printer.setPrinterName("Print to File (PDF)"); @@ -1243,49 +1220,25 @@ void Statistics::printReport(QWidget * parent) { printer.setPrintRange(QPrinter::AllPages); printer.setOrientation(QPrinter::Portrait); - -// Setting default page orientation to landscape for statistics view? -// if (ui->tabWidget->currentWidget() == ui->statisticsTab) { -// printer.setOrientation(QPrinter::Landscape); -// } - -// printer.setPageSize(QPrinter::A4); // Could be QPrinter::Letter -// printer.setOutputFormat(QPrinter::PdfFormat); - - printer.setFullPage(false); // Print only on printable area of page and not in non-printable margins -// printer.setFullPage(true); // Print only on printable area of page and not in non-printable margins + printer.setFullPage(false); // Print only on printable area of page and not in non-printable margins printer.setNumCopies(1); - printer.setResolution(1200); - printer.setPageMargins(5, 5, 5, 5, QPrinter::Millimeter); // Set physical margins to 5 mm, which must be within printable area - // 5 mm is pretty small and less than most laser printers allow, so - // this will amount to default printer margins + printer.setPageMargins(10, 10, 10, 10, QPrinter::Millimeter); // Show print dialog to user and allow them to change settings as desired QPrintDialog pdlg(&printer, parent); if (pdlg.exec() == QPrintDialog::Accepted) { -// QString size = ""; - QPainter painter; - painter.begin(&printer); - int yPos = 0; -/* QTextDocument doc; - doc.setPageSize(QSizeF(printer.pageRect().size())); - doc.setDocumentMargin((qreal) 0.5); - QFont font("Times New Roman", 12); - doc.setDefaultFont(font); + QTextDocument doc; + QSizeF printArea = printer.pageRect().size(); + qDebug() << "print area" << printArea; + doc.setPageSize(printArea); // Set document to print area, removing default 2cm margins + QFont sansFont; + sansFont.setPointSize(10 * (printArea.width()/1200.0)); // Scale the font + doc.setDefaultFont(sansFont); + qDebug() << "Default print font is" << doc.defaultFont(); doc.setHtml(htmlReportHeader + htmlUsage + htmlMachineSettings + htmlMachines + htmlReportFooter); - doc.setDefaultFont(font); doc.print(&printer); -*/ - yPos = Statistics::printBlock(htmlReportHeader + htmlUsage + htmlMachineSettings + htmlMachines + htmlReportFooter, &printer, &painter, yPos); -/* yPos = Statistics::printBlock(size+htmlReportHeader, &printer, &painter, yPos); - yPos = Statistics::printBlock(size+htmlUsage, &printer, &painter, yPos); - yPos = Statistics::printBlock(size+htmlMachineSettings, &printer, &painter, yPos); - yPos = Statistics::printBlock(size+htmlMachines, &printer, &painter, yPos); - yPos = Statistics::printBlock(size+htmlReportFooter, &printer, &painter, yPos); -*/ - painter.end(); } } diff --git a/oscar/statistics.h b/oscar/statistics.h index bc9b4343..95dab267 100644 --- a/oscar/statistics.h +++ b/oscar/statistics.h @@ -185,8 +185,6 @@ class Statistics : public QObject QString GenerateRXChanges(); QString GenerateCPAPUsage(); - static int printBlock (QString text, QPrinter *printer, QPainter *painter, int yPos); - // Using a map to maintain order QList rows; QMap calcnames; From aabd97d5371d403b64254d073e5959aeec486524 Mon Sep 17 00:00:00 2001 From: Seeker4 Date: Sat, 15 Jun 2019 15:09:04 -0700 Subject: [PATCH 3/3] Correct loss of previous changes. --- oscar/statistics.cpp | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/oscar/statistics.cpp b/oscar/statistics.cpp index 8111d677..dc958d20 100644 --- a/oscar/statistics.cpp +++ b/oscar/statistics.cpp @@ -1205,15 +1205,18 @@ void Statistics::printReport(QWidget * parent) { #ifdef Q_OS_LINUX printer.setPrinterName("Print to File (PDF)"); printer.setOutputFormat(QPrinter::PdfFormat); - QString name; - QString datestr; + QString name = "Statistics"; + QString datestr = QDate::currentDate().toString(Qt::ISODate); - if (ui->tabWidget->currentWidget() == ui->statisticsTab) { - name = "Statistics"; - datestr = QDate::currentDate().toString(Qt::ISODate); - } else { name = "Unknown"; } +// if (ui->tabWidget->currentWidget() == ui->statisticsTab) { +// name = "Statistics"; +// datestr = QDate::currentDate().toString(Qt::ISODate); +// } else if (ui->tabWidget->currentWidget() == ui->helpTab) { +// name = "Help"; +// datestr = QDateTime::currentDateTime().toString(Qt::ISODate); +// } else { name = "Unknown"; } - QString filename = p_pref->Get("{home}/" + name + "_" + p_profile->user->userName() + "_" + datestr + ".pdf"); + QString filename = p_pref->Get("{home}/") + name + "_" + p_profile->user->userName() + "_" + datestr + ".pdf"; printer.setOutputFileName(filename); #endif