Classes | GUI > Accessories

QPenPrinter : QObject : Object

QPen PDF export and printing of vector graphics

Description

QPenPrinter allows Pen to operate on a printer device. The graphics can be exported to PDF by using "print to file" as printer device.

Class Methods

QPenPrinter.new

Create a new QPenPrinter object.

Returns:

an instance of QPenPrinter

QPenPrinter.print(printFunc, cancelFunc)

Convenience function to show a print dialog and print.

Arguments:

printFunc

A Function to be evaluated when the user presses "Print", with the printer object as Pen painter target. See aPrintFunc in -print below.

cancelFunc

An optional Function to be evaluated if the user presses "Cancel".

Inherited class methods

Instance Methods

Printing

.showDialog(aOkFunc, aCancelFunc)

Shows a Print Dialog to allow the user to configure the printer object. This is asynchronous and the method will return immediately. When the user presses the "Print" button, aOkFunc is called with this QPenPrinter object as argument.

Arguments:

aOkFunc

A Function to be evaluated when the user presses "Print".

aCancelFunc

An optional Function to be evaluated if the user presses "Cancel".

.print(aPrintFunc)

This method does the actual printing or PDF export. It evaluates aPrintFunc with the printer object as Pen painter target. This QPenPrinter object is passed as the argument.

All the ordinary Pen commands can be used inside the function.

Arguments:

aPrintFunc

A Function to be evaluated to draw the graphics.

Discussion:

If this method is called without configuring the printer object first, it will print on the default printer with default settings.

This method is typically called from within the aOkFunc of -showDialog above. After showDialog has configured the printer once, this method can be called multiple times to reuse the last printer configuration.

The point at (0@0) will coincide with the origin of -pageRect, which is offset by the page margins. So you don't need to translate the Pen.

.newPage

Starts a new page. Typically called within the aPrintFunc of -print.

Properties

.paperRect

Get the paper bounds.

Returns:

a Rect

.pageRect

Get the page bounds, which is the printable area and usually smaller than -paperRect due to margins.

Returns:

a Rect

Discussion:

The origin of the Rect is relative to the paper, and will be non-zero due to margins.

.pageSize

Get the page size as a Size.

Returns:

a Size

Discussion:

This can be used to scale the graphics to fit the page if the bounds of the graphics is known:

x = penPrinter.pageSize.width / bounds.width;
Pen.scale(x,x);
// ... draw stuff here ...

Page range

The methods below returns the page range selected by the user. Page number starts at 1. When both methods returns 0 it means "print all pages".

.fromPage

Get the start page.

Returns:

.toPage

Get the end page.

Returns:

Inherited instance methods

Undocumented instance methods

.bounds

Examples

Simple usage:

QPenPrinter.print {
    // first page
    Pen.moveTo(100@100);
    Pen.lineTo(300@300);
    Pen.stroke;

    // second page
    p.newPage;
    Pen.addRect(p.pageSize.asRect);
    Pen.stroke;
}

Keep the QPenPrinter object to save configuration state:

p = QPenPrinter();

The code below can then be called multiple times:

p.showDialog {
    p.print {
        // first page
        Pen.moveTo(100@100);
        Pen.lineTo(300@300);
        Pen.stroke;

        // second page
        p.newPage;
        Pen.addRect(p.pageSize.asRect);
        Pen.stroke;
    }
} {
    "Printing cancelled!".postln;
};