KD Reports

           | Home |            | Namespaces |            | Classes |            | Files |            | Directories |
Public Member Functions

KDReports::Report Class Reference

#include <KDReportsReport.h>

List of all members.

Public Member Functions

 Report (QObject *parent=0)
 ~Report ()
void setDefaultFont (const QFont &font)
void addInlineElement (const Element &element)
void addElement (const Element &element, Qt::AlignmentFlag horizontalAlignment=Qt::AlignLeft, const QColor &backgroundColor=QColor())
void addVerticalSpacing (qreal space)
void addFragment (const QTextDocumentFragment &fragment)
void addPageBreak ()
void setPageSize (const QPrinter::PageSize &size)
QPrinter::PageSize pageSize () const
void setOrientation (QPrinter::Orientation orientation)
QPrinter::Orientation orientation () const
void setWidthForEndlessPrinter (qreal widthMM)
void setMargins (qreal top, qreal left, qreal bottom, qreal right)
void getMargins (qreal *top, qreal *left, qreal *bottom, qreal *right) const
void setHeaderBodySpacing (qreal spacing)
void setFooterBodySpacing (qreal spacing)
Headerheader (HeaderLocations hl=AllPages)
Footerfooter (HeaderLocations hl=AllPages)
void associateModel (const QString &modelKey, QAbstractItemModel *model)
void associateTextValue (const QString &id, const QString &value)
void associateImageValue (const QString &id, const QPixmap &value)
bool loadFromXML (QIODevice *iodevice, ErrorDetails *details=0)
bool loadFromXML (const QDomDocument &doc, ErrorDetails *details=0)
void setXmlElementHandler (KDReports::XmlElementHandler *handler)
bool printWithDialog (QWidget *parent)
bool print (QPrinter *printer, QWidget *parent=0)
bool exportToFile (const QString &fileName, QWidget *parent=0)
bool exportToImage (const QSize &size, const QString &fileName, const char *format)
bool exportToHtml (const QString &fileName)
void paintPage (int pageNumber, QPainter &painter)
int numberOfPages () const
void setTableBreakingEnabled (bool tableBreakingEnabled)
 Enable breaking of tables across multiple pages.
bool isTableBreakingEnabled () const
void setTableBreakingPageOrder (TableBreakingPageOrder pageOrder)
TableBreakingPageOrder tableBreakingPageOrder () const
void setFontScalingFactor (qreal factor)
 Scale the fonts in the document by a given factor.
qreal fontScalingFactor () const
void scaleTo (int numPagesHorizontally, int numPagesVertically)
 Ensure that the report fits into a number of pages.
int maximumNumberOfPagesForHorizontalScaling () const
int maximumNumberOfPagesForVerticalScaling () const
QList
< KDReports::AutoTableElement * > 
autoTableElements () const
void regenerateAutoTables ()
void regenerateAutoTableForModel (QAbstractItemModel *model)
void setWatermarkText (const QString &text, int rotation=0, const QColor &color=QColor(204, 204, 204), const QFont &font=QFont(QLatin1String("Helvetica"), 48))
QString watermarkText () const
int watermarkRotation () const
QColor watermarkColor () const
QFont watermarkFont () const
void setWatermarkPixmap (const QPixmap &pixmap, bool autoGrayOut=true)
QPixmap watermarkPixmap () const
void setWatermarkImage (const QImage &image)
QImage watermarkImage () const

Detailed Description

The Report class represents a report.

Initially the report is empty. Content can be added to the report using the addElement API, or using loadFromXML to load an equivalent definition from an XML file.

The report can then be previewed with PreviewDialog, printed, exported to PDF, or exported to an image.


Constructor & Destructor Documentation

KDReports::Report::Report ( QObject *  parent = 0  ) 

Constructs a report.

Parameters:
parent optional parent object, for memory management
KDReports::Report::~Report (  ) 

Destroys the report.


Member Function Documentation

void KDReports::Report::addElement ( const Element element,
Qt::AlignmentFlag  horizontalAlignment = Qt::AlignLeft,
const QColor &  backgroundColor = QColor() 
)

Adds an element to the report, creating a new paragraph for it. You can specify the alignment of that paragraph.

An element defines what is added to the report. The report does not store the element, though. For this reason, any changes to the element after insertion will have no effect on the report. Set all settings in the element before calling addElement.

Parameters:
element the definition of what to add to the report
horizontalAlignment the alignment of the paragraph created for this element
backgroundColor the background color of the paragraph created for this element The difference with Element::setBackground is that it affects the whole paragraph, including any inline elements as well as the unused area up to the right margin of the page.
void KDReports::Report::addFragment ( const QTextDocumentFragment &  fragment  ) 

Adds a piece of formatted text from a QTextDocument. This method is an optimization for the case where the initial text is in a QTextDocument (e.g. in a QTextEdit). In general you probably want to use addElement(HtmlElement()) instead.

void KDReports::Report::addInlineElement ( const Element element  ) 

Adds an element to the report, next to the previous element, in the same paragraph.

An element defines what is added to the report. The report does not store the element, though. For this reason, any changes to the element after insertion will have no effect on the report. Set all settings in the element before calling addInlineElement.

void KDReports::Report::addPageBreak (  ) 

Adds a page break to the report. The next element added to the report will be at the top of a page.

Note that you cannot call addPageBreak twice in a row, it will only insert one page break. To get a completely empty page you need to insert an empty text element between both calls to addPageBreak.

void KDReports::Report::addVerticalSpacing ( qreal  space  ) 

Adds vertical spacing between paragraphs. Make sure to call addElement after that, not addInlineElement.

Parameters:
space the space in millimeters
void KDReports::Report::associateImageValue ( const QString &  id,
const QPixmap &  value 
)

Associate a pixmap with the id of an image element. This is only used if the report description is going to be loaded from an XML file, using loadFromXML. This method needs to be called before loadFromXML is called.

Parameters:
id the id of the image element, specified using id="foo" in the XML.
value the pixmap to use in the report. If associateImageValue was already called with the id id, the new value replaces the old one.
void KDReports::Report::associateModel ( const QString &  modelKey,
QAbstractItemModel *  model 
)

Associates a model with a model key. This is only used if the report description is going to be loaded from an XML file, using loadFromXML. This method needs to be called before loadFromXML is called.

void KDReports::Report::associateTextValue ( const QString &  id,
const QString &  value 
)

Associate a text string with the id of a text or html element. This is only used if the report description is going to be loaded from an XML file, using loadFromXML. This method needs to be called before loadFromXML is called.

Parameters:
id the id of the text or html element, specified using id="foo" in the XML.
value the text for this element, which will appear in the report. The value can include newlines. If associateTextValue was already called with the id id, the new value replaces the old one.
QList<KDReports::AutoTableElement *> KDReports::Report::autoTableElements (  )  const

Returns a list of the auto-table elements that were used to create this report. You wouldn't normally need this method; this is only to be able to change some settings like showing table headers or table borders at runtime.

Since:
1.1
bool KDReports::Report::exportToFile ( const QString &  fileName,
QWidget *  parent = 0 
)

Export the whole report to a PS or PDF file. If the file name has the suffix ".ps" then PostScript is automatically selected as output format. If the file name has the ".pdf" suffix PDF is generated.

bool KDReports::Report::exportToHtml ( const QString &  fileName  ) 

Export the whole report to HTML. Note that HTML export does not include headers and footers, nor watermark.

Images are saved into separate files.

Since:
1.2
bool KDReports::Report::exportToImage ( const QSize &  size,
const QString &  fileName,
const char *  format 
)

Export the whole report to an image file.

Parameters:
size size of the image in pixels
fileName the name of the image file
format format of the image, for instance: BMP, JPG, PNG.
qreal KDReports::Report::fontScalingFactor (  )  const
Returns:
the font scaling factor given to setFontScalingFactor
Since:
1.1
Footer& KDReports::Report::footer ( HeaderLocations  hl = AllPages  ) 
Returns:
a reference to the footer object. Calling this method makes the report have a footer on the specified pages. See header() for caveats.

Specify the contents of the footer by calling Footer::addElement.

void KDReports::Report::getMargins ( qreal *  top,
qreal *  left,
qreal *  bottom,
qreal *  right 
) const
Returns:
the page margins in mm.
Since:
1.1
Header& KDReports::Report::header ( HeaderLocations  hl = AllPages  ) 

Returns a reference to a header object. Calling this method makes the report have a header on the specified pages.

Calling the method with different sets of intersecting flags leads to undefined behavior. For instance header(EvenPages|FirstPage) and header(OddPages) is fine, but header(EvenPages|FirstPage) and header(FirstPage) leads to two headers being defined for the first page, anyone of the two could be picked. Calling the method with the same set multiple times, for instance header(EvenPages|FirstPage), is ok though: the same header instance will be returned every time.

Note that all headers will occupy the same height: the height of the tallest header.

Specify the contents of the header by calling Header::addElement.

bool KDReports::Report::isTableBreakingEnabled (  )  const
Returns:
the value passed to setTableBreakingEnabled, also true when scaleTo was called with numPagesHorizontally > 0
bool KDReports::Report::loadFromXML ( QIODevice *  iodevice,
ErrorDetails details = 0 
)

Loads a report definition from an XML document. Notice that if the report definition contains Chart or Table elements, you need to call associate for each such element. iodevice needs to be open and readable. If you have any chart or table elements in your report, you must associate the models using associateModel() first, otherwise these elements will stay empty.

Returns:
true if the XML document was succesfully loaded, false otherwise
bool KDReports::Report::loadFromXML ( const QDomDocument &  doc,
ErrorDetails details = 0 
)

This is an overloaded member function, provided for convenience. With this method you can modify the QDomDocument before giving it to KDReports.

Note: if the QDomDocument is simply created with a file or QIODevice as argument, it will ignore whitespace-only nodes like <text> </text>. The other loadFromXML takes care of this, while this overload requires that the QDomDocument has been properly set up.

Returns:
true if the XML document was succesfully loaded, false otherwise
Since:
1.1
int KDReports::Report::maximumNumberOfPagesForHorizontalScaling (  )  const
Returns:
the number of horizontal pages set with scaleTo, otherwise 1.
Since:
1.1
int KDReports::Report::maximumNumberOfPagesForVerticalScaling (  )  const
Returns:
the number of vertical pages set with scaleTo, otherwise 0.
Since:
1.1
int KDReports::Report::numberOfPages (  )  const
Returns:
the report's number of pages (with the current page size).
QPrinter::Orientation KDReports::Report::orientation (  )  const
Returns:
the orientation set by setOrientation. The default orientation is QPrinter::Portrait.
QPrinter::PageSize KDReports::Report::pageSize (  )  const
Returns:
the page size set by setPageSize. The default page size is A4.
void KDReports::Report::paintPage ( int  pageNumber,
QPainter &  painter 
)

Paints a given page of the report into the painter. Note that this method can be used for printing and for on-screen rendering so it doesn't fill the background with white, the caller has do to that.

bool KDReports::Report::print ( QPrinter *  printer,
QWidget *  parent = 0 
)

Print directly using the setup values from a preexisting QPrinter object. Notice that this will only change settings like page sizes or orientation temporarily, during this particular print operation. After the operation, these values will be reset to their previous values.

Returns false if cancelled (not possible yet).

Parameters:
parent the parent widget for the progress dialog that appears when printing
bool KDReports::Report::printWithDialog ( QWidget *  parent  ) 

Show the print dialog to let the user choose a printer, and print.

Parameters:
parent the parent widget for the progress dialog that appears when printing
Returns:
false if the print dialog was cancelled
void KDReports::Report::regenerateAutoTableForModel ( QAbstractItemModel *  model  ) 

Regenerate the contents of a single auto table. This can be useful if the data in this specific model changes frequently, to save time compared to recreating the full report.

Since:
1.2
void KDReports::Report::regenerateAutoTables (  ) 

Regenerate the contents of auto tables. This can be useful if the data in the item model changes frequently, to save time compared to recreating the full report. It's also used by the Table Breaking dialog to show/hide headers dynamically.

Since:
1.1
void KDReports::Report::scaleTo ( int  numPagesHorizontally,
int  numPagesVertically 
)

Ensure that the report fits into a number of pages.

By default tables are layouted so that all columns fit into the page width, but for large tables this can wrap the text too much. This method allows to scale the report so that it fits into a number of pages horizontally and vertically. For instance, 3 pages horizontally means that tables columns can span over 3 pages, arranged side by side. This is a maximum number of pages; if the table is really small it will still occupy only a single page.

Scaling also means that the font sizes can be reduced (similar to what setFontScalingFactor does) so that the report fits into the number of pages specified by this method.

NOTE: this is mutually exclusive with setTableBreakingEnabled.

Parameters:
numPagesHorizontally number of pages in the horizontal direction, 1 for no table breaking in the horizontal direction.
numPagesVertically number of pages in the vertical direction, 0 for no limit
Since:
1.1
void KDReports::Report::setDefaultFont ( const QFont &  font  ) 

Sets the default font used for text in this report.

Since:
1.1
void KDReports::Report::setFontScalingFactor ( qreal  factor  ) 

Scale the fonts in the document by a given factor.

Scale all the fonts used in the main part of the report (not headers and footers) by the given amount. Therefore setFontScalingFactor(1.0) is a no-op.

Since:
1.1
void KDReports::Report::setFooterBodySpacing ( qreal  spacing  ) 

Sets the margin between the footer and the body of the report, in mm. This margin is 0 by default, i.e. the footer starts immediately under the body.

Since:
1.1
void KDReports::Report::setHeaderBodySpacing ( qreal  spacing  ) 

Sets the margin between the header and the body of the report, in mm. This margin is 0 by default, i.e. the body starts immediately under the header.

Since:
1.1
void KDReports::Report::setMargins ( qreal  top,
qreal  left,
qreal  bottom,
qreal  right 
)

Sets the page margins in mm. The default margins are 20 mm.

void KDReports::Report::setOrientation ( QPrinter::Orientation  orientation  ) 

Sets the orientation, QPrinter::Portrait or QPrinter::Landscape.

void KDReports::Report::setPageSize ( const QPrinter::PageSize &  size  ) 

Set the list of tabs (tabulations) to use when adding block elements. Those tabs will be set for any block element that is added from now on, until calling this method again.

This method allows to align numbers based on their decimal separator, for instance. Example:

     QList<QTextOption::Tab> tabs;
     QTextOption::Tab tab;
     tab.position = 50; // in mm
     tab.type = QTextOption::DelimiterTab;
     tab.delimiter = QLatin1Char( '.' );
     tabs.append( tab );
     report.setTabPositions( tabs );

     Then insert text using a tab, like TextElement("\t123.456").

This feature requires Qt 4.4 or newer.

Since:
1.2 Sets the page size of the report. Example: setPageSize(QPrinter::A4) or setPageSize(QPrinter::Letter); Note that for performance reasons, it is recommended to do this after adding all the elements to the report rather than before.
void KDReports::Report::setTableBreakingEnabled ( bool  tableBreakingEnabled  ) 

Enable breaking of tables across multiple pages.

By default tables are layouted so that all columns fit into the page width, but for large tables this can wrap the text too much. Table breaking ensures that no wrapping occurs in table cells, the tables span over as many pages as needed horizontally.

Table breaking works best for the case where the document consists of a single table. Otherwise, text after the table might still be after the end of the table, i.e. not where expected.

Calling setTableBreakingEnabled(true) is logically equivalent to calling scaleTo(infinity, 0), i.e. enabling table breaking without a limit on the number of pages.

NOTE: this is mutually exclusive with scaleTo, since scaleTo enables table breaking as well, but with different parameters.

Since:
1.1
void KDReports::Report::setTableBreakingPageOrder ( TableBreakingPageOrder  pageOrder  ) 

Sets the page order to use when breaking tables. When a table is bigger than the page in both directions, horizontally and vertically, then the table breaking can be done in two ways: either the table goes "down then right", i.e. all rows appear first, for the first few columns, and then other columns are printed, or it goes "right then down", i.e. all columns appear first, for the number of rows that fit into one page, and then the bottom part of the table is printed.

The default page order is DownThenRight.

Since:
1.1
void KDReports::Report::setWatermarkImage ( const QImage &  image  ) 

Same as setWatermarkPixmap but with a QImage. Especially useful in threads.

Since:
1.2
void KDReports::Report::setWatermarkPixmap ( const QPixmap &  pixmap,
bool  autoGrayOut = true 
)

Sets the pixmap to be used as a watermark. By default, the image will be "grayed out" much like QIcon does for disabled pixmaps. If you do not want this behavior, set autoGrayOut to false. However, you need to ensure then that the image does prevent the legibility of the report. The pixmap will not be scaled, it will be centered on the page. You can set both a watermark text and a watermark image, but the two will overwrite each other so this is not recommended.

void KDReports::Report::setWatermarkText ( const QString &  text,
int  rotation = 0,
const QColor &  color = QColor(204, 204, 204),
const QFont &  font = QFont(QLatin1String("Helvetica"), 48) 
)

Sets the text to be used as a watermark. By default, the text will be displayed with 0 degrees rotation, in a light gray, in a 48pt Helvetica font, centered on the page. You can set both a watermark text and a watermark image, but the two will overwrite each other so this is not recommended. Also notice that if you choose a watermark color that is too dark, this will make the reports less legible.

void KDReports::Report::setWidthForEndlessPrinter ( qreal  widthMM  ) 

Set a custom page size for an endless printer. The page width is known, the document is laid out without pagination within that width. The page height is set automatically so that the entire document fits within one page.

When calling setWidthForEndlessPrinter you don't have to call setPageSize or setOrientation.

Call setWidthForEndlessPrinter(0) to reset to normal (pagesize-dependent) behavior.

NOTE: with Qt-4.3, this feature only works reliably on Unix, using Qt compiled with CUPS support. With Qt-4.4, there is no such limitation and this feature should work everywhere.

void KDReports::Report::setXmlElementHandler ( KDReports::XmlElementHandler handler  ) 

Sets an xml element handler. The report does not take ownership of the xml element handler.

If you want it to be deleted with the report, derive from both QObject and XmlElementHandler and give the report as parent QObject.

Since:
1.2
TableBreakingPageOrder KDReports::Report::tableBreakingPageOrder (  )  const
Returns:
the table breaking page order given to setTableBreakingPageOrder
Since:
1.1
QColor KDReports::Report::watermarkColor (  )  const
Returns:
the watermark text color.
QFont KDReports::Report::watermarkFont (  )  const
Returns:
the watermark text font.
QImage KDReports::Report::watermarkImage (  )  const
Returns:
the image to be used as watermark.
Since:
1.2
QPixmap KDReports::Report::watermarkPixmap (  )  const
Returns:
the image to be used as watermark. If autoGrayOut was used when calling setWatermarkPixmap, this is not the same pixmap as was passed to setWatermarkPixmap.
int KDReports::Report::watermarkRotation (  )  const
Returns:
the watermark text rotation.
QString KDReports::Report::watermarkText (  )  const
Returns:
the watermark text.

The documentation for this class was generated from the following file:

Copyright © 2008, Klarälvdalens Datakonsult AB KD Reports