comparison src/main.cpp @ 114:a5c8741b8662

Initial prototype support for printing list of users + print preview dialog. Has some issues currently.
author Matti Hamalainen <ccr@tnsp.org>
date Wed, 05 Jul 2017 12:48:55 +0300
parents 907f2ddf6801
children 71cfb7d96cfc
comparison
equal deleted inserted replaced
113:907f2ddf6801 114:a5c8741b8662
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 <QProgressDialog>
12 #include "main.h" 15 #include "main.h"
13 #include "ui_mainwindow.h" 16 #include "ui_mainwindow.h"
14 #include "ui_editperson.h" 17 #include "ui_editperson.h"
15 #include "ui_aboutwindow.h" 18 #include "ui_aboutwindow.h"
16 19
335 338
336 new QShortcut(QKeySequence(Qt::Key_PageUp), this, SLOT(selectRowPrev())); 339 new QShortcut(QKeySequence(Qt::Key_PageUp), this, SLOT(selectRowPrev()));
337 new QShortcut(QKeySequence(Qt::Key_PageDown), this, SLOT(selectRowNext())); 340 new QShortcut(QKeySequence(Qt::Key_PageDown), this, SLOT(selectRowNext()));
338 341
339 new QShortcut(QKeySequence(Qt::CTRL + Qt::Key_Return), this, SLOT(focusDebtEdit())); 342 new QShortcut(QKeySequence(Qt::CTRL + Qt::Key_Return), this, SLOT(focusDebtEdit()));
343
344 new QShortcut(QKeySequence(Qt::CTRL + Qt::Key_P), this, SLOT(on_button_Print_clicked()));
340 } 345 }
341 346
342 347
343 // 348 //
344 // Application main window destructor 349 // Application main window destructor
501 506
502 507
503 void SyntilistaMainWindow::on_button_About_clicked() 508 void SyntilistaMainWindow::on_button_About_clicked()
504 { 509 {
505 new AboutWindow(this); 510 new AboutWindow(this);
511 }
512
513
514 void SyntilistaMainWindow::on_button_Print_clicked()
515 {
516 // Create a printer object and force some basic settings
517 QPrinter printer(QPrinter::HighResolution);
518 printer.setPageSize(QPageSize(QPageSize::A4));
519 printer.setColorMode(QPrinter::GrayScale);
520 printer.setResolution(300);
521
522 // We need to get the page count here, and also need it again in
523 // printDocument(), but there is no sane way to pass that there,
524 // so some code duplication is unfortunately necessary
525 SLPageInfo pinfo;
526 pinfo.npages = 0;
527 pinfo.nlinesPerPage = 0;
528
529 QPixmap tmpPixmap(1000, 1300);
530 QPainter tmpPainter;
531 tmpPainter.begin(&tmpPixmap);
532 bool ret = printDocumentPage(pinfo, true, -1, &tmpPainter, &printer);
533 tmpPainter.end();
534
535 if (!ret)
536 {
537 // Some kind of error occured
538 return;
539 }
540
541
542 // Set available pages
543 printer.setFromTo(1, pinfo.npages);
544
545 // Create print preview dialog and show it
546 QPrintPreviewDialog preview(&printer, this);
547 preview.setSizeGripEnabled(true);
548
549 connect(
550 &preview,
551 SIGNAL(paintRequested(QPrinter *)),
552 this,
553 SLOT(printDocument(QPrinter *)));
554
555 preview.exec();
556 }
557
558
559 void SyntilistaMainWindow::printDocument(QPrinter *printer)
560 {
561 // Create progress dialog
562 QProgressDialog progress(
563 tr("Tulostetaan ..."),
564 tr("Peruuta"),
565 0,
566 1,
567 this);
568
569 // Again, get the page info here .. we need the number of lines per page
570 SLPageInfo pinfo;
571 pinfo.npages = 0;
572 pinfo.nlinesPerPage = 0;
573
574 QPixmap tmpPixmap(1000, 1300);
575 QPainter tmpPainter;
576 tmpPainter.begin(&tmpPixmap);
577 bool ret = printDocumentPage(pinfo, true, -1, &tmpPainter, printer);
578 tmpPainter.end();
579
580 if (!ret)
581 return;
582
583 // Setup rest of the progress dialog here
584 progress.setWindowModality(Qt::ApplicationModal);
585 progress.setMinimum(printer->fromPage() - 1);
586 progress.setMaximum(printer->toPage());
587
588
589 // Begin painting to the printer (or preview)
590 QPainter painter;
591 painter.begin(printer);
592
593 bool firstPage = true;
594 for (int page = printer->fromPage(); page <= printer->toPage(); page++)
595 {
596 if (!firstPage)
597 printer->newPage();
598
599 qApp->processEvents();
600 if (progress.wasCanceled())
601 break;
602
603 printDocumentPage(pinfo, false, page, &painter, printer);
604 progress.setValue(page);
605 firstPage = false;
606 }
607
608 painter.end();
609 }
610
611
612 bool SyntilistaMainWindow::printDocumentPage(SLPageInfo &pinfo, const bool getPageInfo, const int npage, QPainter *pt, QPrinter *printer)
613 {
614 // Form the SQL query for list of users
615 QString querystr = QStringLiteral(
616 "SELECT id,first_name,last_name,extra_info,added,updated, "
617 "(SELECT TOTAL(value) FROM transactions WHERE transactions.person=people.id) AS balance "
618 "FROM people ORDER BY last_name ASC,first_name ASC");
619
620 // If we are fetching page info, we need to process all entries
621 if (!getPageInfo)
622 {
623 // Otherwise we can limit to given page number
624 querystr += QStringLiteral(" LIMIT %1 OFFSET %2").
625 arg(pinfo.nlinesPerPage).
626 arg((npage - 1) * pinfo.nlinesPerPage);
627 }
628
629 QSqlQuery query;
630 query.prepare(querystr);
631 query.setForwardOnly(true);
632 query.exec();
633
634 if (!slCheckAndReportSQLError("printDocumentPage()", query.lastError()))
635 {
636 slErrorMsg(
637 tr("SQL-tietokantavirhe"),
638 tr("Tietokantaa selattaessa tapahtui virhe."));
639
640 return false;
641 }
642
643 pt->save();
644 if (!getPageInfo)
645 {
646 pt->scale(
647 printer->pageRect().width() / 1000.0f,
648 printer->pageRect().height() / 1300.0f);
649 }
650
651 QFont font1("Arial", 5);
652 SLDrawContext ctx(pt);
653 ctx.setFont(font1);
654
655 int nline = 0;
656 while (query.next())
657 {
658 PersonInfo info;
659 slGetPersonInfoRec(query, info);
660
661 // Check for end of page
662 // KLUDGE for now
663 if (getPageInfo &&
664 ctx.m_pos.y() + (ctx.boundRect().height() * 4) >= 1300.0f / 2.0f)
665 {
666 if (nline > pinfo.nlinesPerPage)
667 pinfo.nlinesPerPage = nline;
668
669 pinfo.npages++;
670 nline = 0;
671 }
672
673 if (nline == 0)
674 {
675 // If we are at the start of the page, we shall draw a header
676 pt->setBrush(QBrush(Qt::black));
677 pt->setPen(QPen(Qt::black, 3, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin));
678
679 ctx.setPos(0, 0);
680 ctx.drawText( 5, 180, "Etunimi");
681 ctx.drawText( 200, 230, "Sukunimi");
682 ctx.drawText( 450, 190, "Lisätty");
683 ctx.drawText( 650, 190, "Päivitetty");
684 ctx.drawText( 870, 120, "Tase");
685 ctx.lf();
686
687 pt->drawLine(0, ctx.m_pos.y(), 1000, ctx.m_pos.y());
688
689 ctx.move(0, 5);
690 }
691
692 // Draw a gray bar under every second line
693 if (nline % 2 == 0)
694 {
695 pt->fillRect(
696 0,
697 ctx.m_pos.y() - 1,
698 1000,
699 ctx.boundRect().height() + 4,
700 QColor(0, 0, 0, 40));
701 }
702
703 ctx.drawText( 5, 180, info.firstName);
704 ctx.drawText( 200, 230, info.lastName);
705 ctx.drawText( 450, 190, slDateTimeToStr(info.added));
706 ctx.drawText( 650, 190, slDateTimeToStr(info.updated));
707 ctx.drawText( 870, 120, slMoneyValueToStr(info.balance), Qt::AlignRight);
708
709 ctx.lf(10);
710 nline++;
711 }
712
713 query.finish();
714
715 if (getPageInfo)
716 {
717 if (nline > pinfo.nlinesPerPage)
718 pinfo.nlinesPerPage = nline;
719
720 pinfo.npages++;
721 }
722 else
723 {
724 ctx.setPos(0, 1240);
725 ctx.drawText(0, 1000,
726 QStringLiteral("Sivu %1 / %2").
727 arg(npage).arg(printer->toPage()),
728 Qt::AlignHCenter);
729 }
730
731 pt->restore();
732 return true;
506 } 733 }
507 734
508 735
509 void SyntilistaMainWindow::on_button_DeletePerson_clicked() 736 void SyntilistaMainWindow::on_button_DeletePerson_clicked()
510 { 737 {