| 1 | /////////////////////////////////////////////////////////////////////////////// |
| 2 | // textprinter.h |
| 3 | // ------------------- |
| 4 | // Copyright (c) 2007 David Johnson <[email protected]> |
| 5 | // All rights reserved. |
| 6 | // |
| 7 | // Redistribution and use in source and binary forms, with or without |
| 8 | // modification, are permitted provided that the following conditions |
| 9 | // are met: |
| 10 | // |
| 11 | // 1. Redistributions of source code must retain the above copyright |
| 12 | // notice, this list of conditions and the following disclaimer. |
| 13 | // |
| 14 | // 2. Redistributions in binary form must reproduce the above copyright |
| 15 | // notice, this list of conditions and the following disclaimer in the |
| 16 | // documentation and/or other materials provided with the distribution. |
| 17 | // |
| 18 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 19 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 20 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 21 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 22 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 23 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 24 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 25 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 26 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 27 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 28 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 29 | /////////////////////////////////////////////////////////////////////////////// |
| 30 | |
| 31 | #ifndef TEXTPRINTER_H |
| 32 | #define TEXTPRINTER_H |
| 33 | |
| 34 | #include <QObject> |
| 35 | #include <QPrinter> |
| 36 | |
| 37 | class QTextDocument; |
| 38 | class QWidget; |
| 39 | |
| 40 | /////////////////////////////////////////////////////////////////////////////// |
| 41 | /// \class TextPrinter |
| 42 | /// \version 0.3 |
| 43 | /// |
| 44 | /// TextPrinter is a printing utility class. It provides methods to print, |
| 45 | /// preview, and export to PDF. The page format, including header and footers, |
| 46 | /// can be defined. The content to be printed is provided as a QTextDocument |
| 47 | /// object. |
| 48 | /////////////////////////////////////////////////////////////////////////////// |
| 49 | |
| 50 | class TextPrinter : public QObject |
| 51 | { |
| 52 | Q_OBJECT |
| 53 | public: |
| 54 | /// Default constructor |
| 55 | explicit TextPrinter(QObject *parent=0); |
| 56 | /// Destructor |
| 57 | ~TextPrinter(); |
| 58 | |
| 59 | /// Print the document |
| 60 | void print(const QTextDocument *document, |
| 61 | const QString &caption=QString()); |
| 62 | /// Export the document to PDF |
| 63 | void exportPdf(const QTextDocument *document, |
| 64 | const QString &caption=QString(), |
| 65 | const QString &filename=QString()); |
| 66 | /// Display the document in a preview dialog |
| 67 | void preview(const QTextDocument *document, |
| 68 | const QString &caption=QString()); |
| 69 | |
| 70 | /// Get page size |
| 71 | QPrinter::PageSize pageSize() const; |
| 72 | /// Set page size |
| 73 | void setPageSize(QPrinter::PageSize size); |
| 74 | /// Get page orientation |
| 75 | QPrinter::Orientation orientation() const; |
| 76 | /// Set page orientation |
| 77 | void setOrientation(QPrinter::Orientation orientation); |
| 78 | |
| 79 | /// Get left margin width |
| 80 | double leftMargin() const; |
| 81 | /// Set left margin width |
| 82 | void setLeftMargin(double margin); |
| 83 | /// Get right margin width |
| 84 | double rightMargin() const; |
| 85 | /// Set right margin width |
| 86 | void setRightMargin(double margin); |
| 87 | /// Get top margin width |
| 88 | double topMargin() const; |
| 89 | /// Set top margin width |
| 90 | void setTopMargin(double margin); |
| 91 | /// Get bottom margin width |
| 92 | double bottomMargin() const; |
| 93 | /// Set bottom margin width |
| 94 | void setBottomMargin(double margin); |
| 95 | /// Set all margins |
| 96 | void setMargins(double margin); |
| 97 | |
| 98 | /// Get spacing between content and header and footer |
| 99 | double spacing() const; |
| 100 | /// Set spacing between content and header and footer |
| 101 | void setSpacing(double spacing); |
| 102 | |
| 103 | /// Get header size |
| 104 | double headerSize() const; |
| 105 | /// Set header size |
| 106 | void setHeaderSize(double size); |
| 107 | /// Set header rule size |
| 108 | double headerRule() const; |
| 109 | /// Get header rule size |
| 110 | void setHeaderRule(double pointsize); |
| 111 | /// Get header text |
| 112 | const QString &headerText() const; |
| 113 | /// Set header text |
| 114 | void setHeaderText(const QString &text); |
| 115 | |
| 116 | /// Get footer size |
| 117 | double footerSize() const; |
| 118 | /// Set footer size |
| 119 | void setFooterSize(double size); |
| 120 | /// Get footer rule size |
| 121 | double footerRule() const; |
| 122 | /// Set footer rule size |
| 123 | void setFooterRule(double pointsize); |
| 124 | /// Get footer text |
| 125 | const QString &footerText() const; |
| 126 | /// Set footer text |
| 127 | void setFooterText(const QString &text); |
| 128 | |
| 129 | /// Get date format |
| 130 | const QString &dateFormat() const; |
| 131 | /// Set date format |
| 132 | void setDateFormat(const QString &format); |
| 133 | |
| 134 | private: |
| 135 | // not copyable |
| 136 | TextPrinter(const TextPrinter&); |
| 137 | TextPrinter &operator=(const TextPrinter&); |
| 138 | |
| 139 | // return paper rect |
| 140 | QRectF paperRect(QPaintDevice *device); |
| 141 | // return printable rects |
| 142 | QRectF contentRect(QPaintDevice *device); |
| 143 | QRectF headerRect(QPaintDevice *device); |
| 144 | QRectF footerRect(QPaintDevice *device); |
| 145 | |
| 146 | // paint specific page |
| 147 | void paintPage(QPainter *painter, |
| 148 | QTextDocument *document, |
| 149 | int pagenum); |
| 150 | private slots: |
| 151 | // common print routine |
| 152 | void print(QPrinter *printer); |
| 153 | |
| 154 | private: |
| 155 | QWidget *parent_; |
| 156 | QPrinter *printer_; |
| 157 | |
| 158 | QTextDocument *tempdoc_; |
| 159 | |
| 160 | double leftmargin_; |
| 161 | double rightmargin_; |
| 162 | double topmargin_; |
| 163 | double bottommargin_; |
| 164 | double spacing_; |
| 165 | |
| 166 | double headersize_; |
| 167 | double headerrule_; |
| 168 | QString headertext_; |
| 169 | double footersize_; |
| 170 | double footerrule_; |
| 171 | QString footertext_; |
| 172 | |
| 173 | QString dateformat_; |
| 174 | }; |
| 175 | |
| 176 | #endif // TEXTPRINTER_H |