comparison src/main.cpp @ 157:325e7590f93e

Move most of the printing related code to printing.cpp
author Matti Hamalainen <ccr@tnsp.org>
date Tue, 12 Sep 2017 15:56:40 +0300
parents afadc1380fb1
children d9b9be9c4788
comparison
equal deleted inserted replaced
156:afadc1380fb1 157:325e7590f93e
7 // included file "COPYING" for exact terms. 7 // included file "COPYING" for exact terms.
8 // 8 //
9 #include <QApplication> 9 #include <QApplication>
10 #include <QMessageBox> 10 #include <QMessageBox>
11 #include <QSettings> 11 #include <QSettings>
12 #include <QPrintDialog>
13 #include <QPrintPreviewDialog>
14 #include <QStandardPaths> 12 #include <QStandardPaths>
15 #include "main.h" 13 #include "main.h"
16 #include "ui_mainwindow.h" 14 #include "ui_mainwindow.h"
17 #include "ui_editperson.h" 15 #include "ui_editperson.h"
18 #include "ui_aboutwindow.h" 16 #include "ui_aboutwindow.h"
129 // 127 //
130 // Check if an SQL error has occured (for given QSqlError) and 128 // Check if an SQL error has occured (for given QSqlError) and
131 // report it to stdout if so. Return "false" if error has occured, 129 // report it to stdout if so. Return "false" if error has occured,
132 // true otherwise. 130 // true otherwise.
133 // 131 //
134 bool slCheckAndReportSQLError(const QString where, const QSqlError &err, bool report = false) 132 bool slCheckAndReportSQLError(const QString where, const QSqlError &err, bool report)
135 { 133 {
136 if (err.isValid()) 134 if (err.isValid())
137 { 135 {
138 // If an error has occured, log it 136 // If an error has occured, log it
139 slLog("ERROR", 137 slLog("ERROR",
779 777
780 778
781 void SyntilistaMainWindow::on_button_About_clicked() 779 void SyntilistaMainWindow::on_button_About_clicked()
782 { 780 {
783 new AboutWindow(this); 781 new AboutWindow(this);
784 }
785
786
787 void SyntilistaMainWindow::on_button_Print_clicked()
788 {
789 // Create a printer object and force some basic settings
790 QPrinter printer(QPrinter::HighResolution);
791 printer.setPageSize(QPageSize(QPageSize::A4));
792 printer.setColorMode(QPrinter::GrayScale);
793 printer.setResolution(300);
794
795 // We need to get the page count here, and also need it again in
796 // printDocument(), but there is no sane way to pass that there,
797 // so some code duplication is unfortunately necessary
798 SLPageInfo pinfo;
799 pinfo.npages = 0;
800 pinfo.nlinesPerPage = 0;
801
802 QPixmap tmpPixmap(1000, 1300);
803 QPainter tmpPainter;
804 tmpPainter.begin(&tmpPixmap);
805 bool ret = printDocumentPage(pinfo, true, -1, &tmpPainter, &printer);
806 tmpPainter.end();
807
808 if (!ret)
809 {
810 // Some kind of error occured
811 return;
812 }
813
814
815 // Set available pages
816 printer.setFromTo(1, pinfo.npages);
817
818 // Create print preview dialog and show it
819 QPrintPreviewDialog preview(&printer, this);
820 preview.setWindowModality(Qt::ApplicationModal);
821 preview.setSizeGripEnabled(true);
822
823 connect(
824 &preview,
825 SIGNAL(paintRequested(QPrinter *)),
826 this,
827 SLOT(printDocument(QPrinter *)));
828
829 preview.exec();
830 }
831
832
833 void SyntilistaMainWindow::printDocument(QPrinter *printer)
834 {
835 // Create progress dialog
836 QProgressDialog progress(
837 tr("Tulostetaan ..."),
838 tr("Peruuta"),
839 0,
840 1,
841 this);
842
843 progress.setWindowModality(Qt::ApplicationModal);
844
845 // Again, get the page info here .. we need the number of lines per page
846 SLPageInfo pinfo;
847 pinfo.npages = 0;
848 pinfo.nlinesPerPage = 0;
849
850 QPixmap tmpPixmap(1000, 1300);
851 QPainter tmpPainter;
852 tmpPainter.begin(&tmpPixmap);
853 bool ret = printDocumentPage(pinfo, true, -1, &tmpPainter, printer);
854 tmpPainter.end();
855
856 if (!ret)
857 return;
858
859 // If from and to are 0, we are supposed to print all pages
860 if (printer->fromPage() == 0 && printer->toPage() == 0)
861 printer->setFromTo(1, pinfo.npages);
862
863 // Setup rest of the progress dialog here
864 progress.setMinimum(printer->fromPage() - 1);
865 progress.setMaximum(printer->toPage());
866
867 // Begin painting to the printer (or preview)
868 QPainter painter;
869 painter.begin(printer);
870
871 bool firstPage = true;
872 for (int page = printer->fromPage(); page <= printer->toPage(); page++)
873 {
874 if (!firstPage)
875 printer->newPage();
876
877 qApp->processEvents();
878 if (progress.wasCanceled())
879 break;
880
881 printDocumentPage(pinfo, false, page, &painter, printer);
882 progress.setValue(page);
883 firstPage = false;
884 }
885
886 painter.end();
887 }
888
889
890 bool SyntilistaMainWindow::printDocumentPage(SLPageInfo &pinfo, const bool getPageInfo, const int npage, QPainter *pt, QPrinter *printer)
891 {
892 // Form the SQL query for list of users
893 QString querystr = QStringLiteral(
894 "SELECT id,first_name,last_name,extra_info,added,updated, "
895 "(SELECT TOTAL(value) FROM transactions WHERE transactions.person=people.id) AS balance "
896 "FROM people ORDER BY last_name ASC,first_name ASC");
897
898 // If we are fetching page info, we need to process all entries
899 if (!getPageInfo)
900 {
901 // Otherwise we can limit to given page number
902 querystr += QStringLiteral(" LIMIT %1 OFFSET %2").
903 arg(pinfo.nlinesPerPage).
904 arg((npage - 1) * pinfo.nlinesPerPage);
905 }
906
907 QSqlQuery query;
908 query.prepare(querystr);
909 query.setForwardOnly(true);
910 query.exec();
911
912 if (!slCheckAndReportSQLError("printDocumentPage()", query.lastError()))
913 {
914 slErrorMsg(
915 tr("SQL-tietokantavirhe"),
916 tr("Tietokantaa selattaessa tapahtui virhe."));
917
918 return false;
919 }
920
921 pt->save();
922 if (!getPageInfo)
923 {
924 pt->scale(
925 printer->pageRect().width() / 1000.0f,
926 printer->pageRect().height() / 1300.0f);
927 }
928
929 QFont font1("Arial", 5);
930 SLDrawContext ctx(pt);
931 ctx.setFont(font1);
932
933 int nline = 0;
934 while (query.next())
935 {
936 PersonInfo info;
937 slGetPersonInfoRec(query, info);
938
939 // Check for end of page
940 // KLUDGE for now
941 if (getPageInfo && ctx.lfq(10) >= 1300.0f)
942 {
943 if (nline > pinfo.nlinesPerPage)
944 pinfo.nlinesPerPage = nline;
945
946 pinfo.npages++;
947 nline = 0;
948 }
949
950 if (nline == 0)
951 {
952 // If we are at the start of the page, we shall draw a header
953 pt->setBrush(QBrush(Qt::black));
954 pt->setPen(QPen(Qt::black, 3, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin));
955
956 ctx.setPos(0, 0);
957 ctx.drawText( 5, 180, tr("Etunimi"));
958 ctx.drawText( 200, 230, tr("Sukunimi"));
959 ctx.drawText( 450, 190, tr("Lisätty"));
960 ctx.drawText( 650, 190, tr("Päivitetty"));
961 ctx.drawText( 870, 120, tr("Tase"));
962 ctx.lf();
963
964 pt->drawLine(0, ctx.m_pos.y(), 1000, ctx.m_pos.y());
965
966 ctx.move(0, 5);
967 }
968
969 // Draw a gray bar under every second line
970 if (nline % 2 == 0)
971 {
972 pt->fillRect(
973 0,
974 ctx.m_pos.y() - 1,
975 1000,
976 ctx.boundRect().height() + 4,
977 QColor(0, 0, 0, 40));
978 }
979
980 ctx.drawText( 5, 180, info.firstName);
981 ctx.drawText( 200, 230, info.lastName);
982 ctx.drawText( 450, 190, slDateTimeToStr(info.added));
983 ctx.drawText( 650, 190, slDateTimeToStr(info.updated));
984 ctx.drawText( 870, 120, slMoneyValueToStr(info.balance), Qt::AlignRight);
985
986 ctx.lf(10);
987 nline++;
988 }
989
990 query.finish();
991
992 if (getPageInfo)
993 {
994 if (nline > pinfo.nlinesPerPage)
995 pinfo.nlinesPerPage = nline;
996
997 pinfo.npages++;
998 }
999 else
1000 {
1001 ctx.setPos(0, 1240);
1002 ctx.drawText(0, 1000,
1003 tr("Sivu %1 / %2 (%3 / %4)").
1004 arg(npage - printer->fromPage() + 1).
1005 arg(printer->toPage() - printer->fromPage() + 1).
1006 arg(npage).
1007 arg(printer->toPage()),
1008 Qt::AlignHCenter);
1009 }
1010
1011 pt->restore();
1012 return true;
1013 } 782 }
1014 783
1015 784
1016 void SyntilistaMainWindow::on_button_DeletePerson_clicked() 785 void SyntilistaMainWindow::on_button_DeletePerson_clicked()
1017 { 786 {