📄 qprinter.cpp
字号:
/******************************************************************************** Copyright (C) 1992-2006 Trolltech ASA. All rights reserved.**** This file is part of the QtGui module of the Qt Toolkit.**** This file may be used under the terms of the GNU General Public** License version 2.0 as published by the Free Software Foundation** and appearing in the file LICENSE.GPL included in the packaging of** this file. Please review the following information to ensure GNU** General Public Licensing requirements will be met:** http://www.trolltech.com/products/qt/opensource.html**** If you are unsure which license is appropriate for your use, please** review the following information:** http://www.trolltech.com/products/qt/licensing.html or contact the** sales department at sales@trolltech.com.**** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.******************************************************************************/#include "qprinter_p.h"#include "qprinter.h"#include "qprintengine.h"#include "qlist.h"#include <qpagesetupdialog.h>#include <qapplication.h>#ifndef QT_NO_PRINTER#if defined (Q_WS_WIN)#include <private/qprintengine_win_p.h>#elif defined (Q_WS_MAC)#include <private/qprintengine_mac_p.h>#elif defined (Q_OS_UNIX)#include <private/qprintengine_ps_p.h>#endif#ifdef QT_PDF_SUPPORT#include "qprintengine_pdf_p.h"#endif#if defined(QT3_SUPPORT)# include "qprintdialog.h"#endif // QT3_SUPPORT#define ABORT_IF_ACTIVE(location) \ if (d->printEngine->printerState() == QPrinter::Active) { \ qWarning("%s, cannot be changed while printer is active", location); \ return; \ }void QPrinterPrivate::createDefaultEngines(){ if (outputFormat == QPrinter::NativeFormat) {#if defined (Q_WS_WIN) QWin32PrintEngine *winEngine = new QWin32PrintEngine(printerMode); paintEngine = winEngine; printEngine = winEngine;#elif defined (Q_WS_MAC) QMacPrintEngine *macEngine = new QMacPrintEngine(printerMode); paintEngine = macEngine; printEngine = macEngine;#elif defined (Q_OS_UNIX) QPSPrintEngine *psEngine = new QPSPrintEngine(printerMode); paintEngine = psEngine; printEngine = psEngine;#endif } else { QPdfEngine *pdfEngine = new QPdfEngine(printerMode); paintEngine = pdfEngine; printEngine = pdfEngine; } use_default_engine = true;}/*! \class QPrinter \brief The QPrinter class is a paint device that paints on a printer. \ingroup multimedia \mainclass On Windows or Mac OS X, QPrinter uses the built-in printer drivers. On X11, QPrinter generates postscript and sends that to lpr, lp, or another printProgram(). QPrinter can also print to any other QPrintEngine. QPrinter is used in much the same way as QWidget and QPixmap are used. The big difference is that you must keep track of pages. QPrinter supports a number of settable parameters, most of which can be changed by the end user through a \l{QAbstractPrintDialog} print dialog. In general, QPrinter passes these functions onto the underlying QPrintEngine. The most important parameters are: \list \i setOrientation() tells QPrinter which page orientation to use. \i setPageSize() tells QPrinter what page size to expect from the printer. \i setResolution() tells QPrinter what resolution you wish the printer to provide (in dots per inch -- dpi). \i setFullPage() tells QPrinter whether you want to deal with the full page or just with the part the printer can draw on. The default is false, so that by default you should be able to paint on (0,0). If true the origin of the coordinate system will be in the top left corner of the paper and most probably the printer will not be able to paint something there due to its physical margins. \i setNumCopies() tells QPrinter how many copies of the document it should print. \endlist Many of the settable functions can only be called before the actual printing begins (i.e., before QPainter::begin() is called). This usually makes sense (e.g., you can't change the number of copies when you are halfway through printing). There are also some settings that the user sets (through the printer dialog) and that applications are expected to obey. See QAbstractPrintDialog's documentation for more details. Once you start printing, calling newPage() is essential. You will probably also need to look at the device metrics for the printer. If you want to abort the print job, abort() will try its best to stop printing. It may cancel the entire job or just part of it. If your current locale converts "," to ".", you will need to set a locale that doesn't do this (e.g. the "C" locale) before using QPrinter.*//*! \enum QPrinter::PrinterState \value Idle \value Active \value Aborted \value Error*//*! \enum QPrinter::PrinterMode This enum describes the mode the printer should work in. It basically presets a certain resolution and working mode. \value ScreenResolution Sets the resolution of the print device to the screen resolution. This has the big advantage that the results obtained when painting on the printer will match more or less exactly the visible output on the screen. It is the easiest to use, as font metrics on the screen and on the printer are the same. This is the default value. ScreenResolution will produce a lower quality output than HighResolution and should only be used for drafts. \value PrinterResolution This value is deprecated. Is is equivalent to ScreenResolution on Unix and HighResolution on Windows and Mac. Due do the difference between ScreenResolution and HighResolution, use of this value may lead to non-portable printer code. \value HighResolution Use printer resolution on Windows, and set the resolution of the Postscript driver to 600dpi.*//*! \enum QPrinter::Orientation This enum type (not to be confused with \c Orientation) is used to specify each page's orientation. \value Portrait the page's height is greater than its width. \value Landscape the page's width is greater than its height. This type interacts with \l QPrinter::PageSize and QPrinter::setFullPage() to determine the final size of the page available to the application.*//*! \enum QPrinter::PrintRange Used to specify the print range selection option. \value AllPages All pages should be printed. \value Selection Only the selection should be printed. \value PageRange The specified page range should be printed. \sa QAbstractPrintDialog::PrintRange*//*! \enum QPrinter::PrinterOption \compat Use QAbstractPrintDialog::PrintDialogOption instead. \value PrintToFile \value PrintSelection \value PrintPageRange*//*! \enum QPrinter::PageSize This enum type specifies what paper size QPrinter should use. QPrinter does not check that the paper size is available; it just uses this information, together with QPrinter::Orientation and QPrinter::setFullPage(), to determine the printable area. The defined sizes (with setFullPage(true)) are: \value A0 841 x 1189 mm \value A1 594 x 841 mm \value A2 420 x 594 mm \value A3 297 x 420 mm \value A4 210 x 297 mm, 8.26 x 11.69 inches \value A5 148 x 210 mm \value A6 105 x 148 mm \value A7 74 x 105 mm \value A8 52 x 74 mm \value A9 37 x 52 mm \value B0 1030 x 1456 mm \value B1 728 x 1030 mm \value B10 32 x 45 mm \value B2 515 x 728 mm \value B3 364 x 515 mm \value B4 257 x 364 mm \value B5 182 x 257 mm, 7.17 x 10.13 inches \value B6 128 x 182 mm \value B7 91 x 128 mm \value B8 64 x 91 mm \value B9 45 x 64 mm \value C5E 163 x 229 mm \value Comm10E 105 x 241 mm, U.S. Common 10 Envelope \value DLE 110 x 220 mm \value Executive 7.5 x 10 inches, 191 x 254 mm \value Folio 210 x 330 mm \value Ledger 432 x 279 mm \value Legal 8.5 x 14 inches, 216 x 356 mm \value Letter 8.5 x 11 inches, 216 x 279 mm \value Tabloid 279 x 432 mm \value Custom Unknown size With setFullPage(false) (the default), the metrics will be a bit smaller; how much depends on the printer in use. \omitvalue NPageSize*//*! \enum QPrinter::PageOrder This enum type is used by QPrinter to tell the application program how to print. \value FirstPageFirst the lowest-numbered page should be printed first. \value LastPageFirst the highest-numbered page should be printed first.*//*! \enum QPrinter::ColorMode This enum type is used to indicate whether QPrinter should print in color or not. \value Color print in color if available, otherwise in grayscale. \value GrayScale print in grayscale, even on color printers.*//*! \enum QPrinter::PaperSource This enum type specifies what paper source QPrinter is to use. QPrinter does not check that the paper source is available; it just uses this information to try and set the paper source. Whether it will set the paper source depends on whether the printer has that particular source. \warning This is currently only implemented for Windows. \value OnlyOne \value Lower \value Middle \value Manual \value Envelope \value EnvelopeManual \value Auto \value Tractor \value SmallFormat \value LargeFormat \value LargeCapacity \value Cassette \value FormSource*//* \enum QPrinter::PrintRange This enum is used to specify which print range the application should use to print. \value AllPages All the pages should be printed. \value Selection Only the selection should be printed. \value PageRange Print according to the from page and to page options. \sa setPrintRange(), printRange()*//* \enum QPrinter::PrinterOption This enum describes various printer options that appear in the printer setup dialog. It is used to enable and disable these options in the setup dialog. \value PrintToFile Describes if print to file should be enabled. \value PrintSelection Describes if printing selections should be enabled. \value PrintPageRange Describes if printing page ranges (from, to) should be enabled \sa setOptionEnabled(), isOptionEnabled()*//*! Creates a new printer object with the given \a mode.*/QPrinter::QPrinter(PrinterMode mode) : QPaintDevice(), d_ptr(new QPrinterPrivate(this)){ if (!qApp) { qFatal("QPrinter: Must construct a QApplication before a QPaintDevice"); return; } Q_D(QPrinter); d->printerMode = mode; d->outputFormat = QPrinter::NativeFormat; d->createDefaultEngines();}/*! This function is used by subclasses of QPrinter to specify custom print engine and paint engine. QPrinter does not take ownership of the engines, so you need to manage these engine instances yourself. Note that changing the engines will reset the printer state and all its properties. \sa printEngine() paintEngine() setOutputFormat() \since 4.1*/void QPrinter::setEngines(QPrintEngine *printEngine, QPaintEngine *paintEngine){ Q_D(QPrinter); if (d->use_default_engine) delete d->printEngine; d->printEngine = printEngine; d->paintEngine = paintEngine; d->use_default_engine = false;}/*! Destroys the printer object and frees any allocated resources. If the printer is destroyed while a print job is in progress this may or may not affect the print job.*/QPrinter::~QPrinter(){ Q_D(QPrinter); if (d->use_default_engine) delete d->printEngine; delete d;}/*! \enum QPrinter::OutputFormat The OutputFormat enum is used to describe the format QPrinter should use for printing. \value NativeFormat QPrinter will print output in the method given by the platform it is running on, e.g. This is how printing was traditionally done in Qt. This mode is the default. \value PdfFormat QPrinter will generate its output as a PDF file. As of Qt 4.1.3 PDF files generated by Qt are searchable on all platforms.*//*! \since 4.1 Sets the output format for this printer to \a format. Setting the output format will reset the state of the printer*/void QPrinter::setOutputFormat(OutputFormat format){#ifdef QT_PDF_SUPPORT Q_D(QPrinter); if (d->outputFormat == format) return; d->outputFormat = format; if (d->use_default_engine) delete d->printEngine; d->createDefaultEngines();#else Q_UNUSED(format);#endif}/*! \since 4.1 Returns the output format for this printer*/QPrinter::OutputFormat QPrinter::outputFormat() const{ Q_D(const QPrinter); return d->outputFormat;}/*! \reimp */int QPrinter::devType() const{ return QInternal::Printer;}/*! Returns the printer name. This value is initially set to the name of the default printer. \sa setPrinterName()*/QString QPrinter::printerName() const{ Q_D(const QPrinter); return d->printEngine->property(QPrintEngine::PPK_PrinterName).toString();}/*! Sets the printer name to \a name. \sa printerName()*/void QPrinter::setPrinterName(const QString &name){ Q_D(QPrinter); ABORT_IF_ACTIVE("QPrinter::setPrinterName()"); d->printEngine->setProperty(QPrintEngine::PPK_PrinterName, name);}/*! \fn bool QPrinter::outputToFile() const Returns true if the output should be written to a file, or false if the output should be sent directly to the printer. The default setting is false. \sa setOutputToFile(), setOutputFileName()*//*! \fn void QPrinter::setOutputToFile(bool enable) Specifies whether the output should be written to a file or sent directly to the printer. Will output to a file if \a enable is true, or will output directly to the printer if \a enable is false. \sa outputToFile(), setOutputFileName()*//*! \fn QString QPrinter::outputFileName() const Returns the name of the output file. By default, this is an empty string (indicating that the printer shouldn't print to file). \sa setOutputFileName()*/QString QPrinter::outputFileName() const{ Q_D(const QPrinter); return d->printEngine->property(QPrintEngine::PPK_OutputFileName).toString();}/*! Sets the name of the output file to \a fileName. Setting a null or empty name (0 or "") disables printing to a file. Setting a non-empty name enables printing to a file. \sa outputFileName(), setOutputToFile()*/void QPrinter::setOutputFileName(const QString &fileName){ Q_D(QPrinter); ABORT_IF_ACTIVE("QPrinter::setOutputFileName()"); d->printEngine->setProperty(QPrintEngine::PPK_OutputFileName, fileName);}/*! Returns the name of the program that sends the print output to the printer. The default is to return an empty string; meaning that QPrinter will try to be smart in a system-dependent way. On X11 only, you can set it to something different to use a specific print program. On the other platforms, this returns an empty string. \sa setPrintProgram(), setPrinterSelectionOption()*/QString QPrinter::printProgram() const{ Q_D(const QPrinter); return d->printEngine->property(QPrintEngine::PPK_PrinterProgram).toString();}/*! Sets the name of the program that should do the print job to \a printProg. On X11, this function sets the program to call with the PostScript output. On other platforms, it has no effect. \sa printProgram()*/void QPrinter::setPrintProgram(const QString &printProg){ Q_D(QPrinter); ABORT_IF_ACTIVE("QPrinter::setPrintProgram()"); d->printEngine->setProperty(QPrintEngine::PPK_PrinterProgram, printProg);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -