int main( int argc, char** argv ) {
    QApplication app( argc, argv );

    // Create a report
    KDReports::Report report;

    // open a DB connection to an in-memory database
    QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
    db.setDatabaseName(":memory:");
    if( !db.open() ) {
        QMessageBox::critical(0, QObject::tr("Cannot open database"),
                              QObject::tr("Cannot create connection to the requested database. Your Qt is probably lacking the QSQLITE driver. Please check your Qt installation." ), QMessageBox::Cancel );
        return false;
    }

    // fill the DB with some test data
    QSqlQuery query;
    query.exec("create table airlines (id int primary key, "
               "name varchar(20), homecountry varchar(2))");
    query.exec("insert into airlines values(1, 'Lufthansa', 'DE')");
    query.exec("insert into airlines values(2, 'SAS', 'SE')");
    query.exec("insert into airlines values(3, 'United', 'US')");
    query.exec("insert into airlines values(4, 'KLM', 'NL')");
    query.exec("insert into airlines values(5, 'Aeroflot', 'RU')");

    // Create a QSqlTableModel, connect to the previously created database, fill
    // the db with some data.
    QSqlTableModel tableModel( 0, db );
    tableModel.setTable( "airlines" );
    tableModel.select();
    tableModel.removeColumn( 0 );
    tableModel.setHeaderData( 0, Qt::Horizontal, QObject::tr("Name") );
    tableModel.setHeaderData( 1, Qt::Horizontal, QObject::tr("Home country") );
    QFont font = app.font();
    font.setBold( true );
    tableModel.setHeaderData( 0, Qt::Horizontal, font, Qt::FontRole );
    tableModel.setHeaderData( 1, Qt::Horizontal, font, Qt::FontRole );

    // associate the model and load the XML file
    report.associateModel( "airlines", &tableModel );
    QFile reportFile( ":/Database.xml" );
    if( !reportFile.open( QIODevice::ReadOnly ) ) {
        QMessageBox::warning( 0, QObject::tr( "Warning" ), QObject::tr( "Could not open report description file 'Database.xml'. Please start this program from the DatabaseXML directory." ) );
        return -1;
    }

    KDReports::ErrorDetails details;
    if( !report.loadFromXML( &reportFile, &details ) ) {
        QMessageBox::warning( 0, QObject::tr( "Warning" ), QObject::tr( "Could not parse report description file:\n%1" ).arg(details.message()) );
        reportFile.close();
        return -2;
    }

    // To show a print preview:
    KDReports::PreviewDialog preview( &report );
    return preview.exec();

    //return app.exec();
}
示例#2
0
int main( int argc, char** argv ) {
    QApplication app( argc, argv );

    KDReports::Report report;

    // open a DB connection to an in-memory database
    QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
    db.setDatabaseName(":memory:");
    if( !db.open() ) {
        QMessageBox::critical(0, QObject::tr("Cannot open database"),
                              QObject::tr("Cannot create connection to the requested database. Your Qt is probably lacking the QSQLITE driver. Please check your Qt installation." ), QMessageBox::Cancel );
        return false;
    }

    // fill the DB with some test data
    QSqlQuery query;
    query.exec("create table airlines (id int primary key, "
               "name varchar(20), homecountry varchar(2))");
    query.exec("insert into airlines values(1, 'Lufthansa', 'DE')");
    query.exec("insert into airlines values(2, 'SAS', 'SE')");
    query.exec("insert into airlines values(3, 'United', 'US')");
    query.exec("insert into airlines values(4, 'KLM', 'NL')");
    query.exec("insert into airlines values(5, 'Aeroflot', 'RU')");

    // Create a QSqlTableModel, connect to the previously created database, fill
    // the db with some data.
    QSqlTableModel tableModel( 0, db );
    tableModel.setTable( "airlines" );
    tableModel.select();
    tableModel.removeColumn( 0 );
    tableModel.setHeaderData(0, Qt::Horizontal, QObject::tr("Name"));
    tableModel.setHeaderData(1, Qt::Horizontal, QObject::tr("Home country"));
    QFont font = app.font();
    font.setBold( true );
    tableModel.setHeaderData( 0, Qt::Horizontal, font, Qt::FontRole );
    tableModel.setHeaderData( 1, Qt::Horizontal, font, Qt::FontRole );

    KDReports::AutoTableElement tableElement( &tableModel );
    tableElement.setVerticalHeaderVisible( false );
    report.addElement( tableElement );

    // To export to an image file:
    //qDebug() << "Exporting to output.png";
    //report.exportToImage( QSize(300, 400), "output.png", "PNG" );


    KDReports::PreviewDialog preview( &report );
    return preview.exec();
}
int main( int argc, char** argv ) {
    QApplication app( argc, argv );

    // Create a report
    KDReports::Report report;

    // Set the content of a text field - this shows how xml files can be used as templates for reports,
    // not only as complete (generated) reports.
    report.associateTextValue( "title_element", "Price list example" );
    report.associateTextValue( "company_address", "Klarälvdalens Datakonsult AB\n"
"Rysktorp\n"
"SE-68392 Hagfors\n"
"Sweden" );
    // Note how id="table1_title" is used twice in the xml, both places get the right value
    report.associateTextValue( "table1_title", "Network Peripherals" );
    report.associateTextValue( "table2_title", "Printer Cartridges" );

    report.associateImageValue( "image_system", QPixmap( ":/system.png" ) );

    // Create two table models which will be used by one table element each.
    TableModel table1;
    table1.setDataHasVerticalHeaders( false );
    table1.loadFromCSV( ":/table1" );
    report.associateModel( QLatin1String( "table1" ), &table1 );
    TableModel table2;
    table2.setDataHasVerticalHeaders( false );
    table2.loadFromCSV( ":/table2" );
    report.associateModel( QLatin1String( "table2" ), &table2 );

    QFile reportFile( ":/PriceList.xml" );
    if( !reportFile.open( QIODevice::ReadOnly ) ) {
        QMessageBox::warning( 0, QObject::tr( "Warning" ), QObject::tr( "Could not open report description file 'PriceList.xml'. Please start this program from the PriceListXML directory." ) );
        return -1;
    }

    KDReports::ErrorDetails details;
    if( !report.loadFromXML( &reportFile, &details ) ) {
        QMessageBox::warning( 0, QObject::tr( "Warning" ), QObject::tr( "Could not parse report description file:\n%1" ).arg(details.message()) );
        reportFile.close();
        return -2;
    }

    // To show a print preview:
    KDReports::PreviewDialog preview( &report );
    return preview.exec();

    //return app.exec();
}
示例#4
0
int main( int argc, char** argv ) {
    QApplication app( argc, argv );

    KDReports::Report report;
    KDReports::Header& header = report.header( KDReports::FirstPage );
    header.addElement( KDReports::TextElement( "A tall image made to fit into the page" ) );

    KDReports::Header& header2 = report.header( KDReports::EvenPages );
    header2.addElement( KDReports::TextElement( "A wide image made to fit into the page" ) );

    KDReports::Footer& footer = report.footer();
    footer.addElement( KDReports::TextElement( "Page" ), Qt::AlignRight );
    footer.addInlineElement( KDReports::TextElement( " " ) );
    footer.addVariable( KDReports::PageNumber );
    footer.addInlineElement( KDReports::TextElement( "/" ) );
    footer.addVariable( KDReports::PageCount );

    KDReports::ImageElement imageElement( QPixmap( ":/imageTall.jpg" ) );
    imageElement.setFitToPage();
    report.addElement( imageElement );

    KDReports::ImageElement imageElement2( QPixmap( ":/image.jpg" ) );
    imageElement2.setFitToPage();
    report.addElement( imageElement2 );

    KDReports::PreviewDialog preview( &report );
    return preview.exec();
}
示例#5
0
int main( int argc, char** argv ) {
    QApplication app( argc, argv );

    // Create a report
    KDReports::Report report;

    QFile reportFile( ":/MailMerge.xml" );
    if( !reportFile.open( QIODevice::ReadOnly ) ) {
        QMessageBox::warning(
            0, QObject::tr( "Warning" ),
            QObject::tr( "Could not open report description file 'MailMerge.xml'. "
                         "Please start this program from the HelloWorldXML directory." ) );
        return -1;
    }

    TableModel members;
    members.setDataHasVerticalHeaders( false );
    members.loadFromCSV( ":/members.csv" );
    report.associateModel( QLatin1String( "members" ), &members);
  
    for (int i = 0; i < members.rowCount(); i++ ) {
        report.setCurrentRow(&members, i);
        KDReports::ErrorDetails details;
        if( !report.loadFromXML( &reportFile, &details ) ) {
            QMessageBox::warning( 0, QObject::tr( "Warning" ), QObject::tr( "Could not parse report description file:\n%1" ).arg(details.message()) );
            reportFile.close();
            return -2;
        }
        
        if ( i < members.rowCount() - 1 )
            report.addPageBreak();

        // show a print preview:
    }
    KDReports::PreviewDialog preview( &report );
    return preview.exec();
}
示例#6
0
 virtual void run() {
     KDReports::Report report;
     report.associateTextValue( "title_element", "Price list example" );
     report.associateImageValue( "image_system", QImage( ":/system.png" ) );
     QFile reportFile( ":/PriceList.xml" );
     if( !reportFile.open( QIODevice::ReadOnly ) ) {
         Q_ASSERT( false );
     }
     KDReports::ErrorDetails details;
     if( !report.loadFromXML( &reportFile, &details ) ) {
         qWarning( "ERROR: %s", qPrintable( details.message() ) );
     }
     report.exportToImage( QSize(800, 600), "xml.png", "PNG" ); // deleted by main() below
     report.exportToFile( "xml.pdf" ); // deleted by main() below
 }
示例#7
0
int main( int argc, char** argv ) {
    QApplication app( argc, argv );

    KDReports::Report report;
    report.setDefaultFont( QFont( "Arial", 8 ) );

    report.setFirstPageNumber( 100 );

    KDReports::Header& header = report.header( KDReports::FirstPage );
    header.addElement( KDReports::TextElement( "This is a test for generating one section of a larger report." ) );

    KDReports::Footer& footer = report.footer();
    footer.addElement( KDReports::TextElement( "We are generating pages 100-109 out of 500." ) );
    footer.addElement( KDReports::TextElement( "Page" ), Qt::AlignRight );
    footer.addInlineElement( KDReports::TextElement( " " ) );
    footer.addVariable( KDReports::PageNumber );
    footer.addInlineElement( KDReports::TextElement( " / " ) );
    // As an example for combining reports together, we'll set the value of
    // the total page count later, when it's known.
    KDReports::TextElement pageCountElement;
    pageCountElement.setId( QLatin1String("id_page_count") );
    footer.addInlineElement( pageCountElement );

    QList<QTextOption::Tab> tabs;
    tabs.append( report.rightAlignedTab() );
    report.setTabPositions( tabs );

    KDReports::TextElement tocElement("<Table of Contents will go here>");
    tocElement.setId( QLatin1String("id_toc") );
    report.addElement( tocElement );

    report.addVerticalSpacing( 10 );

    report.setTabPositions( QList<QTextOption::Tab>() ); // for performance

    KDReports::TableElement largeTableElement = largeTable();
    report.addElement( largeTableElement );

    // Let's assume we know the total page count is 1000 now, let's set it.
    report.associateTextValue( QLatin1String("id_page_count"), QString::number(500) );

    // Fill in the table of contents
    QStringList tocList;
    tocList << QString::fromLatin1("Chapter one\tpage 1");
    tocList << QString::fromLatin1("Chapter two\tpage 3");
    tocList << QString::fromLatin1("Last chapter\tpage 6");
    QString tocText = tocList.join( QString::fromLatin1("\n") );
    report.associateTextValue( QLatin1String("id_toc"), tocText );

    KDReports::PreviewDialog preview( &report );
    return preview.exec();

    // For performance testing...
    //report.exportToFile( "testout.pdf" );
    //return 0;
}
示例#8
0
    virtual void run() {

        // Create a report
        KDReports::Report report;

        report.setWatermarkImage( QImage( ":/background.jpg" ) );
        report.setWatermarkText( QString() );

        KDReports::ImageElement imageElement(QImage(":/logo.png"));
        report.header().addElement(imageElement);

        // Add a text element for the title
        KDReports::TextElement titleElement( QObject::tr( "KD Reports Hello world example" ) );
        titleElement.setPointSize( 18 );
        report.addElement( titleElement, Qt::AlignHCenter );

        // A bit of spacing (10 mm)
        report.addVerticalSpacing( 10 );

        // Add another text element, demonstrating "<<" operator
        KDReports::TextElement textElement;
        textElement << QObject::tr( "This is a sample report produced with KD Reports." );
        textElement << "\n";
        textElement << QObject::tr( "Klarälvdalens Datakonsult AB, Platform-independent software solutions" );
        textElement << "\n";
        report.addElement( textElement );

        // Create a table model, which will be used by the table element
        HelloWorldTableModel tableModel;

        // A bit of spacing (10 mm)
        report.addVerticalSpacing( 10 );

        // Add an "auto table" (a table that shows the contents of a model)
        KDReports::AutoTableElement tableElement( &tableModel );
        tableElement.setBorder( 1 );
        report.addElement( tableElement );

        // Add many paragraphs, to demonstrate page breaking
        for ( int i = 1; i < 100 ; ++i ) {
            report.addElement( KDReports::HtmlElement( QString::fromLatin1( "<b>Customer</b> <em>%1</em>" ).arg(i) ), Qt::AlignLeft );
            report.addInlineElement( KDReports::TextElement( "  - 2006" ) );
        }

        // Add a page break
        report.addPageBreak();

        report.addElement( KDReports::TextElement( "This is the last page" ) );

        // To export to an image file:
        //qDebug() << "Exporting to output.png";
        report.exportToImage( QSize(200, 1200), "output.png", "PNG" ); // deleted by main() below

        // To export to a PDF file:
        //qDebug() << "Exporting to output.pdf";
        report.exportToFile( "output.pdf" ); // deleted by main() below
    }
示例#9
0
int main( int argc, char** argv ) {
    QApplication app( argc, argv );

    KDReports::Report report;

    report.setHeaderBodySpacing( 10 ); // mm
    report.setFooterBodySpacing( 10 ); // mm

//     report.setWatermarkPixmap( QPixmap( ":/kdab.jpg" ) );
//     report.setWatermarkText( QString::null );

    KDReports::Header& header = report.header( KDReports::OddPages );
    QPixmap kdab( ":/kdab_small.jpg" );
    // add a border around the pixmap, mostly for debugging
    //QPainter painter( &kdab );
    //painter.drawRect( 0, 0, kdab.width() - 1, kdab.height() - 1 );
    KDReports::ImageElement imageElement( kdab );
    // The image size can be set in mm or in percent of the page width (without margins)
    // imageElement.setWidth( 50 ); // mm
    imageElement.setWidth( 40, KDReports::Percent );

    header.addElement( imageElement );
    header.addVariable( KDReports::PageNumber );
    header.addInlineElement( KDReports::TextElement( " / " ) );
    header.addVariable( KDReports::PageCount );
    header.addInlineElement( KDReports::TextElement( ", Date: " ) );
    header.addVariable( KDReports::TextDate );
    header.addInlineElement( KDReports::TextElement( ", Time: " ) );
    header.addVariable( KDReports::TextTime );

    KDReports::Header& evenPagesHeader = report.header( KDReports::EvenPages );
    evenPagesHeader.addElement( imageElement );
    evenPagesHeader.addInlineElement( KDReports::TextElement( "Even pages header: " ) );
    evenPagesHeader.addVariable( KDReports::PageNumber );
    evenPagesHeader.addInlineElement( KDReports::TextElement( " / " ) );
    evenPagesHeader.addVariable( KDReports::PageCount );

    KDReports::Footer& footer = report.footer();
    KDReports::TextElement companyAddressElement( QString::fromUtf8( "Klarälvdalens Datakonsult AB\nRysktorp\nSE-68392 Hagfors\nSweden" ) );
    footer.addElement( companyAddressElement, Qt::AlignRight );

    KDReports::TextElement titleElement;
    titleElement << "Price list example";
    titleElement.setPointSize( 18 );
    report.addElement( titleElement, Qt::AlignHCenter );

    report.addVerticalSpacing( 10 ); // 1 cm

    const QColor titleElementColor( 204, 204, 255 );

    // A text element with the title above a table
    // Note that the background color is not just behind the text (as setBackground() would do),
    // but behind the whole paragraph, up until the right margin of the page.
    KDReports::TextElement tableTitleElement( "Network Peripherals" );
    tableTitleElement.setBold( true );
    report.addElement( tableTitleElement, Qt::AlignLeft, titleElementColor );

    TableModel table1;
    table1.setDataHasVerticalHeaders( false );
    table1.loadFromCSV( ":/table1" );
    // Q_ASSERT( table1.headerData( 0, Qt::Vertical ).toString() == "10/100 Mbps switch" );
    //Q_ASSERT( table1.data( table1.index( 0, 0 ) ).toString() == "Product" );
    KDReports::AutoTableElement autoTableElement1( &table1 );
    autoTableElement1.setWidth( 100, KDReports::Percent );
    report.addElement( autoTableElement1 );

    report.addVerticalSpacing( 5 );

    // Notice how elements can be copied and modified
    // This way, we use the same font attributes for all title elements
    KDReports::TextElement tableTitleElement2 = tableTitleElement;
    tableTitleElement2.setText( "Printer Cartridges" );
    report.addElement( tableTitleElement2, Qt::AlignLeft, titleElementColor );

    TableModel table2;
    table2.setDataHasVerticalHeaders( false );
    table2.loadFromCSV( ":/table2" );
    KDReports::AutoTableElement autoTableElement2( &table2 );
    autoTableElement2.setWidth( 100, KDReports::Percent );
    report.addElement( autoTableElement2 );

    // and again, on the second page
    report.addPageBreak();
    report.addElement( tableTitleElement, Qt::AlignLeft, titleElementColor );
    report.addElement( autoTableElement1 );
    report.addVerticalSpacing( 5 );
    report.addElement( tableTitleElement2, Qt::AlignLeft, titleElementColor );
    report.addElement( autoTableElement2 );

    report.addVerticalSpacing( 5 );

    // ===========================================================================
    // Another kind of table, where the data comes from code and not from a model:
    // ===========================================================================
    KDReports::TableElement tableElement;
    tableElement.setHeaderRowCount( 2 );
    tableElement.setPadding( 3 );
    QColor headerColor( "#DADADA" );
    // Merged header in row 0
    KDReports::Cell& topHeader = tableElement.cell( 0, 0 );
    topHeader.setColumnSpan( 2 );
    topHeader.setBackground( headerColor );
    topHeader.addElement( KDReports::TextElement( "TableElement example" ), Qt::AlignHCenter );

    // Normal header in row 1
    KDReports::Cell& headerCell1 = tableElement.cell( 1, 0 );
    headerCell1.setBackground( headerColor );
    // This would look better if centered vertically. This feature is only available since
    // Qt-4.3 though (QTextCharFormat::AlignMiddle)
    QPixmap systemPixmap( ":/system.png" );
    headerCell1.addElement( KDReports::ImageElement( systemPixmap ) );
    headerCell1.addInlineElement( KDReports::TextElement( " Item" ) );
    KDReports::Cell& headerCell2 = tableElement.cell( 1, 1 );
    headerCell2.setBackground( headerColor );
    KDReports::TextElement expected( "Expected" );
    expected.setItalic( true );
    expected.setBackground( QColor("#999999") ); // note that this background only applies to this element
    headerCell2.addElement( expected );
    headerCell2.addInlineElement( KDReports::TextElement( " shipping time" ) );

    // Data in rows 2 and 3
    tableElement.cell( 2, 0 ).addElement( KDReports::TextElement( "Network Peripherals" ) );
    tableElement.cell( 2, 1 ).addElement( KDReports::TextElement( "4 days" ) );
    tableElement.cell( 3, 0 ).addElement( KDReports::TextElement( "Printer Cartridges" ) );
    tableElement.cell( 3, 1 ).addElement( KDReports::TextElement( "3 days" ) );

    report.addElement( tableElement );

    KDReports::PreviewDialog preview( &report );
    return preview.exec();
}
示例#10
0
void ReportView::generate()
{
    // Check if there are rows in model. If so, generate report
    if(m_tableModel->rowCount() > 0)
    {
        // Create report container
        KDReports::Report report;

        // Generate table element using the table model
        KDReports::AutoTableElement tableElement(m_tableModel);
        // Show vertical header
        tableElement.setVerticalHeaderVisible(true);

        // Title text
        KDReports::TextElement title;
        title.setText("Transaction Report");
        // Show in bold format
        title.setBold(true);
        // Underline text
        title.setUnderline(true);
        // Set font size to 20
        title.setPointSize(20);
        // Add title to report
        report.addElement(title);

        // Insert vertical spacing for easier reading
        report.addVerticalSpacing(2);

        // Text to display report date
        KDReports::TextElement date;
        // Insert to date
        date << "Date: " << m_genData.fromDate.toString();

        // Check if report is not daily
        if(!m_genData.daily)
            // Add to date
            date << " - " << m_genData.toDate.toString();

        // Insert date element in to report
        report.addElement(date);

        // Insert 10 mm vertical spacing
        report.addVerticalSpacing(10);

        // Insert table element
        report.addElement(tableElement);

        // Insert a further 10 mm vertical spacing
        report.addVerticalSpacing(10);

        // Get report sums
        ReportTable::Sums sums = static_cast<ReportTable*>(m_tableModel)
                ->GetSums(m_genData);

        // Check validity of data
        if(sums.cost == -1)
        {
            // Data invalid, notify user
            QMessageBox::critical(this, "Failed to generate report.",
                            "Report data is invalid.",
                             QMessageBox::Ok);
            // Exit function
            return;
        }


        // Total revenue text
        KDReports::TextElement revenueTotal;
        // insert total revenue values
        revenueTotal << "Total revenue: "
                // convert to localized currency format
                << QLocale::system().toCurrencyString(sums.revenue);


        // total cost text
        KDReports::TextElement costTotal;
        costTotal << "Total cost: "
                // convert to localized currency format
                << QLocale::system().toCurrencyString(sums.cost);

        // total profit text
        KDReports::TextElement profitTotal;
        profitTotal << "Total profit: "
                // convert to localized currency format
                << QLocale::system().toCurrencyString(sums.profit);


        // Add revenue total to report
        report.addElement(revenueTotal);
        // Add cost total to report
        report.addElement(costTotal);
        // Add profit total to report
        report.addElement(profitTotal);

        // Create preview dialog using report.
        KDReports::PreviewDialog preview(&report);

        // Show preview dialog
        preview.exec();
    }
    else    // No data in table model
    {
        // Notify user of error
        QMessageBox::warning(this, "No report generated.",
               "Please generate a report before attempting to print it.",
                QMessageBox::Ok);
    }
}