src/gui/dialogs/qpagesetupdialog_unix.cpp
changeset 0 1918ee327afb
child 4 3b1da2848fc7
equal deleted inserted replaced
-1:000000000000 0:1918ee327afb
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     4 ** All rights reserved.
       
     5 ** Contact: Nokia Corporation (qt-info@nokia.com)
       
     6 **
       
     7 ** This file is part of the QtGui module of the Qt Toolkit.
       
     8 **
       
     9 ** $QT_BEGIN_LICENSE:LGPL$
       
    10 ** No Commercial Usage
       
    11 ** This file contains pre-release code and may not be distributed.
       
    12 ** You may use this file in accordance with the terms and conditions
       
    13 ** contained in the Technology Preview License Agreement accompanying
       
    14 ** this package.
       
    15 **
       
    16 ** GNU Lesser General Public License Usage
       
    17 ** Alternatively, this file may be used under the terms of the GNU Lesser
       
    18 ** General Public License version 2.1 as published by the Free Software
       
    19 ** Foundation and appearing in the file LICENSE.LGPL included in the
       
    20 ** packaging of this file.  Please review the following information to
       
    21 ** ensure the GNU Lesser General Public License version 2.1 requirements
       
    22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
       
    23 **
       
    24 ** In addition, as a special exception, Nokia gives you certain additional
       
    25 ** rights.  These rights are described in the Nokia Qt LGPL Exception
       
    26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
       
    27 **
       
    28 ** If you have questions regarding the use of this file, please contact
       
    29 ** Nokia at qt-info@nokia.com.
       
    30 **
       
    31 **
       
    32 **
       
    33 **
       
    34 **
       
    35 **
       
    36 **
       
    37 **
       
    38 ** $QT_END_LICENSE$
       
    39 **
       
    40 ****************************************************************************/
       
    41 
       
    42 #include "qpagesetupdialog.h"
       
    43 
       
    44 #ifndef QT_NO_PRINTDIALOG
       
    45 #include "qpagesetupdialog_unix_p.h"
       
    46 
       
    47 #include "qpainter.h"
       
    48 #include "qprintdialog.h"
       
    49 #include "qdialogbuttonbox.h"
       
    50 #include <ui_qpagesetupwidget.h>
       
    51 
       
    52 #include <QtGui/qprinter.h>
       
    53 #include <private/qabstractpagesetupdialog_p.h>
       
    54 #include <private/qprinter_p.h>
       
    55 
       
    56 #if !defined(QT_NO_CUPS) && !defined(QT_NO_LIBRARY)
       
    57 #  include <private/qcups_p.h>
       
    58 #  include <cups/cups.h>
       
    59 #  include <private/qpdf_p.h>
       
    60 #endif
       
    61 
       
    62 
       
    63 QT_BEGIN_NAMESPACE
       
    64 
       
    65 QSizeF qt_printerPaperSize(QPrinter::Orientation, QPrinter::PaperSize, QPrinter::Unit, int);
       
    66 
       
    67 // Disabled until we have support for papersources on unix
       
    68 // #define PSD_ENABLE_PAPERSOURCE
       
    69 
       
    70 static void populatePaperSizes(QComboBox* cb)
       
    71 {
       
    72     cb->addItem(QPrintDialog::tr("A0"), QPrinter::A0);
       
    73     cb->addItem(QPrintDialog::tr("A1"), QPrinter::A1);
       
    74     cb->addItem(QPrintDialog::tr("A2"), QPrinter::A2);
       
    75     cb->addItem(QPrintDialog::tr("A3"), QPrinter::A3);
       
    76     cb->addItem(QPrintDialog::tr("A4"), QPrinter::A4);
       
    77     cb->addItem(QPrintDialog::tr("A5"), QPrinter::A5);
       
    78     cb->addItem(QPrintDialog::tr("A6"), QPrinter::A6);
       
    79     cb->addItem(QPrintDialog::tr("A7"), QPrinter::A7);
       
    80     cb->addItem(QPrintDialog::tr("A8"), QPrinter::A8);
       
    81     cb->addItem(QPrintDialog::tr("A9"), QPrinter::A9);
       
    82     cb->addItem(QPrintDialog::tr("B0"), QPrinter::B0);
       
    83     cb->addItem(QPrintDialog::tr("B1"), QPrinter::B1);
       
    84     cb->addItem(QPrintDialog::tr("B2"), QPrinter::B2);
       
    85     cb->addItem(QPrintDialog::tr("B3"), QPrinter::B3);
       
    86     cb->addItem(QPrintDialog::tr("B4"), QPrinter::B4);
       
    87     cb->addItem(QPrintDialog::tr("B5"), QPrinter::B5);
       
    88     cb->addItem(QPrintDialog::tr("B6"), QPrinter::B6);
       
    89     cb->addItem(QPrintDialog::tr("B7"), QPrinter::B7);
       
    90     cb->addItem(QPrintDialog::tr("B8"), QPrinter::B8);
       
    91     cb->addItem(QPrintDialog::tr("B9"), QPrinter::B9);
       
    92     cb->addItem(QPrintDialog::tr("B10"), QPrinter::B10);
       
    93     cb->addItem(QPrintDialog::tr("C5E"), QPrinter::C5E);
       
    94     cb->addItem(QPrintDialog::tr("DLE"), QPrinter::DLE);
       
    95     cb->addItem(QPrintDialog::tr("Executive"), QPrinter::Executive);
       
    96     cb->addItem(QPrintDialog::tr("Folio"), QPrinter::Folio);
       
    97     cb->addItem(QPrintDialog::tr("Ledger"), QPrinter::Ledger);
       
    98     cb->addItem(QPrintDialog::tr("Legal"), QPrinter::Legal);
       
    99     cb->addItem(QPrintDialog::tr("Letter"), QPrinter::Letter);
       
   100     cb->addItem(QPrintDialog::tr("Tabloid"), QPrinter::Tabloid);
       
   101     cb->addItem(QPrintDialog::tr("US Common #10 Envelope"), QPrinter::Comm10E);
       
   102     cb->addItem(QPrintDialog::tr("Custom"), QPrinter::Custom);
       
   103 }
       
   104 
       
   105 
       
   106 static QSizeF sizeForOrientation(QPrinter::Orientation orientation, const QSizeF &size)
       
   107 {
       
   108     return (orientation == QPrinter::Portrait) ? size : QSizeF(size.height(), size.width());
       
   109 }
       
   110 
       
   111 #ifdef PSD_ENABLE_PAPERSOURCE
       
   112 static const char *paperSourceNames[] = {
       
   113     "Only One",
       
   114     "Lower",
       
   115     "Middle",
       
   116     "Manual",
       
   117     "Envelope",
       
   118     "Envelope manual",
       
   119     "Auto",
       
   120     "Tractor",
       
   121     "Small format",
       
   122     "Large format",
       
   123     "Large capacity",
       
   124     "Cassette",
       
   125     "Form source",
       
   126     0
       
   127 };
       
   128 
       
   129 struct PaperSourceNames
       
   130 {
       
   131     PaperSourceNames(const char *nam, QPrinter::PaperSource ps)
       
   132         : paperSource(ps), name(nam) {}
       
   133     QPrinter::PaperSource paperSource;
       
   134     const char *name;
       
   135 };
       
   136 #endif
       
   137 
       
   138 
       
   139 class QPagePreview : public QWidget
       
   140 {
       
   141 public:
       
   142     QPagePreview(QWidget *parent) : QWidget(parent)
       
   143     {
       
   144         setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
       
   145         setMinimumSize(50, 50);
       
   146     }
       
   147 
       
   148     void setPaperSize(const QSizeF& size)
       
   149     {
       
   150         m_size = size;
       
   151         update();
       
   152     }
       
   153 
       
   154     void setMargins(qreal left, qreal top, qreal right, qreal bottom)
       
   155     {
       
   156         m_left = left;
       
   157         m_top = top;
       
   158         m_right = right;
       
   159         m_bottom = bottom;
       
   160         update();
       
   161     }
       
   162 
       
   163 protected:
       
   164     void paintEvent(QPaintEvent *)
       
   165     {
       
   166         QRect pageRect;
       
   167         QSizeF adjustedSize(m_size);
       
   168         adjustedSize.scale(width()-10, height()-10, Qt::KeepAspectRatio);
       
   169         pageRect = QRect(QPoint(0,0), adjustedSize.toSize());
       
   170         pageRect.moveCenter(rect().center());
       
   171 
       
   172         qreal width_factor = pageRect.width() / m_size.width();
       
   173         qreal height_factor = pageRect.height() / m_size.height();
       
   174         int leftSize = qRound(m_left*width_factor);
       
   175         int topSize = qRound(m_top*height_factor);
       
   176         int rightSize = qRound(m_right*width_factor);
       
   177         int bottomSize = qRound(m_bottom * height_factor);
       
   178         QRect marginRect(pageRect.x()+leftSize,
       
   179                          pageRect.y()+topSize,
       
   180                          pageRect.width() - (leftSize+rightSize+1),
       
   181                          pageRect.height() - (topSize+bottomSize+1));
       
   182 
       
   183         QPainter p(this);
       
   184         QColor shadow(palette().mid().color());
       
   185         for (int i=1; i<6; ++i) {
       
   186             shadow.setAlpha(180-i*30);
       
   187             QRect offset(pageRect.adjusted(i, i, i, i));
       
   188             p.setPen(shadow);
       
   189             p.drawLine(offset.left(), offset.bottom(), offset.right(), offset.bottom());
       
   190             p.drawLine(offset.right(), offset.top(), offset.right(), offset.bottom()-1);
       
   191         }
       
   192         p.fillRect(pageRect, palette().light());
       
   193 
       
   194         if (marginRect.isValid()) {
       
   195             p.setPen(QPen(palette().color(QPalette::Dark), 0, Qt::DotLine));
       
   196             p.drawRect(marginRect);
       
   197 
       
   198             marginRect.adjust(2, 2, -1, -1);
       
   199             p.setClipRect(marginRect);
       
   200             QFont font;
       
   201             font.setPointSizeF(font.pointSizeF()*0.25);
       
   202             p.setFont(font);
       
   203             p.setPen(palette().color(QPalette::Dark));
       
   204             QString text(QLatin1String("Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat. Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat. Duis autem vel eum iriure dolor in hendrerit in vulputate velit esse molestie consequat, vel illum dolore eu feugiat nulla facilisis at vero eros et accumsan et iusto odio dignissim qui blandit praesent luptatum zzril delenit augue duis dolore te feugait nulla facilisi."));
       
   205             for (int i=0; i<3; ++i)
       
   206                 text += text;
       
   207             p.drawText(marginRect, Qt::TextWordWrap|Qt::AlignVCenter, text);
       
   208         }
       
   209     }
       
   210 
       
   211 private:
       
   212     // all these are in points
       
   213     qreal m_left, m_top, m_right, m_bottom;
       
   214     QSizeF m_size;
       
   215 };
       
   216 
       
   217 
       
   218 class QPageSetupDialogPrivate : public QAbstractPageSetupDialogPrivate
       
   219 {
       
   220     Q_DECLARE_PUBLIC(QPageSetupDialog)
       
   221 
       
   222 public:
       
   223     ~QPageSetupDialogPrivate();
       
   224     void init();
       
   225 
       
   226     QPageSetupWidget *widget;
       
   227 #if !defined(QT_NO_CUPS) && !defined(QT_NO_LIBRARY)
       
   228     QCUPSSupport *cups;
       
   229 #endif
       
   230 };
       
   231 
       
   232 QPageSetupDialogPrivate::~QPageSetupDialogPrivate()
       
   233 {
       
   234 #if !defined(QT_NO_CUPS) && !defined(QT_NO_LIBRARY)
       
   235     delete cups;
       
   236 #endif
       
   237 }
       
   238 
       
   239 void QPageSetupDialogPrivate::init()
       
   240 {
       
   241     Q_Q(QPageSetupDialog);
       
   242 
       
   243     widget = new QPageSetupWidget(q);
       
   244     widget->setPrinter(printer);
       
   245 #if !defined(QT_NO_CUPS) && !defined(QT_NO_LIBRARY)
       
   246     if (printer->outputFormat() == QPrinter::NativeFormat && QCUPSSupport::isAvailable()) {
       
   247         cups = new QCUPSSupport;
       
   248         widget->selectPrinter(cups);
       
   249     } else {
       
   250         cups = 0;
       
   251     }
       
   252 #endif
       
   253 
       
   254     QDialogButtonBox *buttons = new QDialogButtonBox(QDialogButtonBox::Ok
       
   255                                                      | QDialogButtonBox::Cancel,
       
   256                                                      Qt::Horizontal, q);
       
   257     QObject::connect(buttons, SIGNAL(accepted()), q, SLOT(accept()));
       
   258     QObject::connect(buttons, SIGNAL(rejected()), q, SLOT(reject()));
       
   259 
       
   260     QVBoxLayout *lay = new QVBoxLayout(q);
       
   261     lay->addWidget(widget);
       
   262     lay->addWidget(buttons);
       
   263 }
       
   264 
       
   265 QPageSetupWidget::QPageSetupWidget(QWidget *parent)
       
   266     : QWidget(parent),
       
   267     m_printer(0),
       
   268     m_blockSignals(false),
       
   269     m_cups(0)
       
   270 {
       
   271     widget.setupUi(this);
       
   272 
       
   273     QString suffix = (QLocale::system().measurementSystem() == QLocale::ImperialSystem)
       
   274                      ? QString::fromLatin1(" in")
       
   275                      : QString::fromLatin1(" mm");
       
   276     widget.topMargin->setSuffix(suffix);
       
   277     widget.bottomMargin->setSuffix(suffix);
       
   278     widget.leftMargin->setSuffix(suffix);
       
   279     widget.rightMargin->setSuffix(suffix);
       
   280     widget.paperWidth->setSuffix(suffix);
       
   281     widget.paperHeight->setSuffix(suffix);
       
   282 
       
   283     QVBoxLayout *lay = new QVBoxLayout(widget.preview);
       
   284     widget.preview->setLayout(lay);
       
   285     m_pagePreview = new QPagePreview(widget.preview);
       
   286     lay->addWidget(m_pagePreview);
       
   287 
       
   288     setAttribute(Qt::WA_WState_Polished, false);
       
   289 
       
   290 #ifdef PSD_ENABLE_PAPERSOURCE
       
   291     for (int i=0; paperSourceNames[i]; ++i)
       
   292         widget.paperSource->insertItem(paperSourceNames[i]);
       
   293 #else
       
   294     widget.paperSourceLabel->setVisible(false);
       
   295     widget.paperSource->setVisible(false);
       
   296 #endif
       
   297 
       
   298     widget.reverseLandscape->setVisible(false);
       
   299     widget.reversePortrait->setVisible(false);
       
   300 
       
   301     populatePaperSizes(widget.paperSize);
       
   302 
       
   303     QStringList units;
       
   304     units << tr("Centimeters (cm)") << tr("Millimeters (mm)") << tr("Inches (in)") << tr("Points (pt)");
       
   305     widget.unit->addItems(units);
       
   306     connect(widget.unit, SIGNAL(activated(int)), this, SLOT(unitChanged(int)));
       
   307     widget.unit->setCurrentIndex((QLocale::system().measurementSystem() == QLocale::ImperialSystem) ? 2 : 1);
       
   308 
       
   309     connect(widget.paperSize, SIGNAL(currentIndexChanged(int)), this, SLOT(_q_paperSizeChanged()));
       
   310     connect(widget.paperWidth, SIGNAL(valueChanged(double)), this, SLOT(_q_paperSizeChanged()));
       
   311     connect(widget.paperHeight, SIGNAL(valueChanged(double)), this, SLOT(_q_paperSizeChanged()));
       
   312 
       
   313     connect(widget.leftMargin, SIGNAL(valueChanged(double)), this, SLOT(setLeftMargin(double)));
       
   314     connect(widget.topMargin, SIGNAL(valueChanged(double)), this, SLOT(setTopMargin(double)));
       
   315     connect(widget.rightMargin, SIGNAL(valueChanged(double)), this, SLOT(setRightMargin(double)));
       
   316     connect(widget.bottomMargin, SIGNAL(valueChanged(double)), this, SLOT(setBottomMargin(double)));
       
   317 
       
   318     connect(widget.portrait, SIGNAL(clicked()), this, SLOT(_q_pageOrientationChanged()));
       
   319     connect(widget.landscape, SIGNAL(clicked()), this, SLOT(_q_pageOrientationChanged()));
       
   320 }
       
   321 
       
   322 void QPageSetupWidget::setPrinter(QPrinter *printer)
       
   323 {
       
   324     m_printer = printer;
       
   325     m_blockSignals = true;
       
   326     selectPdfPsPrinter(printer);
       
   327     printer->getPageMargins(&m_leftMargin, &m_topMargin, &m_rightMargin, &m_bottomMargin, QPrinter::Point);
       
   328     unitChanged(widget.unit->currentIndex());
       
   329     m_pagePreview->setMargins(m_leftMargin, m_topMargin, m_rightMargin, m_bottomMargin);
       
   330     m_paperSize = printer->paperSize(QPrinter::Point);
       
   331     widget.paperWidth->setValue(m_paperSize.width() / m_currentMultiplier);
       
   332     widget.paperHeight->setValue(m_paperSize.height() / m_currentMultiplier);
       
   333 
       
   334     widget.landscape->setChecked(printer->orientation() == QPrinter::Landscape);
       
   335 
       
   336 #ifdef PSD_ENABLE_PAPERSOURCE
       
   337     widget.paperSource->setCurrentItem(printer->paperSource());
       
   338 #endif
       
   339     Q_ASSERT(m_blockSignals);
       
   340     m_blockSignals = false;
       
   341     _q_paperSizeChanged();
       
   342 }
       
   343 
       
   344 // set gui data on printer
       
   345 void QPageSetupWidget::setupPrinter() const
       
   346 {
       
   347     QPrinter::Orientation orientation = widget.portrait->isChecked()
       
   348                                         ? QPrinter::Portrait
       
   349                                         : QPrinter::Landscape;
       
   350     m_printer->setOrientation(orientation);
       
   351     // paper format
       
   352     QVariant val = widget.paperSize->itemData(widget.paperSize->currentIndex());
       
   353     int ps = m_printer->pageSize();
       
   354     if (val.type() == QVariant::Int) {
       
   355         ps = val.toInt();
       
   356     }
       
   357 #if !defined(QT_NO_CUPS) && !defined(QT_NO_LIBRARY)
       
   358     else if (m_cups && QCUPSSupport::isAvailable() && m_cups->currentPPD()) {
       
   359         QByteArray cupsPageSize = val.toByteArray();
       
   360         QPrintEngine *engine = m_printer->printEngine();
       
   361         engine->setProperty(PPK_CupsStringPageSize, QString::fromLatin1(cupsPageSize));
       
   362         engine->setProperty(PPK_CupsOptions, m_cups->options());
       
   363 
       
   364         QRect pageRect = m_cups->pageRect(cupsPageSize);
       
   365         engine->setProperty(PPK_CupsPageRect, pageRect);
       
   366 
       
   367         QRect paperRect = m_cups->paperRect(cupsPageSize);
       
   368         engine->setProperty(PPK_CupsPaperRect, paperRect);
       
   369 
       
   370         for(ps = 0; ps < QPrinter::NPaperSize; ++ps) {
       
   371             QPdf::PaperSize size = QPdf::paperSize(QPrinter::PaperSize(ps));
       
   372             if (size.width == paperRect.width() && size.height == paperRect.height())
       
   373                 break;
       
   374         }
       
   375     }
       
   376 #endif
       
   377     if (ps == QPrinter::Custom)
       
   378         m_printer->setPaperSize(sizeForOrientation(orientation, m_paperSize), QPrinter::Point);
       
   379     else
       
   380         m_printer->setPaperSize(static_cast<QPrinter::PaperSize>(ps));
       
   381 
       
   382 #ifdef PSD_ENABLE_PAPERSOURCE
       
   383     m_printer->setPaperSource((QPrinter::PaperSource)widget.paperSource->currentIndex());
       
   384 #endif
       
   385     m_printer->setPageMargins(m_leftMargin, m_topMargin, m_rightMargin, m_bottomMargin, QPrinter::Point);
       
   386 
       
   387 }
       
   388 
       
   389 void QPageSetupWidget::selectPrinter(QCUPSSupport *cups)
       
   390 {
       
   391     m_cups = cups;
       
   392     widget.paperSize->clear();
       
   393 #if !defined(QT_NO_CUPS) && !defined(QT_NO_LIBRARY)
       
   394     if (m_cups && QCUPSSupport::isAvailable()) {
       
   395         const ppd_option_t* pageSizes = m_cups->pageSizes();
       
   396         const int numChoices = pageSizes ? pageSizes->num_choices : 0;
       
   397 
       
   398         int cupsDefaultSize = 0;
       
   399         QSize qtPreferredSize = m_printer->paperSize(QPrinter::Point).toSize();
       
   400         bool preferredSizeMatched = false;
       
   401         for (int i = 0; i < numChoices; ++i) {
       
   402             widget.paperSize->addItem(QString::fromLocal8Bit(pageSizes->choices[i].text), QByteArray(pageSizes->choices[i].choice));
       
   403             if (static_cast<int>(pageSizes->choices[i].marked) == 1)
       
   404                 cupsDefaultSize = i;
       
   405             if (m_printer->d_func()->hasUserSetPageSize) {
       
   406                 QRect cupsPaperSize = m_cups->paperRect(pageSizes->choices[i].choice);
       
   407                 QSize diff = cupsPaperSize.size() - qtPreferredSize;
       
   408                 if (qAbs(diff.width()) < 5 && qAbs(diff.height()) < 5) {
       
   409                     widget.paperSize->setCurrentIndex(i);
       
   410                     preferredSizeMatched = true;
       
   411                 }
       
   412             }
       
   413         }
       
   414         if (!preferredSizeMatched)
       
   415             widget.paperSize->setCurrentIndex(cupsDefaultSize);
       
   416         if (m_printer->d_func()->hasCustomPageMargins) {
       
   417             m_printer->getPageMargins(&m_leftMargin, &m_topMargin, &m_rightMargin, &m_bottomMargin, QPrinter::Point);
       
   418         } else {
       
   419             QByteArray cupsPaperSizeChoice = widget.paperSize->itemData(widget.paperSize->currentIndex()).toByteArray();
       
   420             QRect paper = m_cups->paperRect(cupsPaperSizeChoice);
       
   421             QRect content = m_cups->pageRect(cupsPaperSizeChoice);
       
   422 
       
   423             m_leftMargin = content.x() - paper.x();
       
   424             m_topMargin = content.y() - paper.y();
       
   425             m_rightMargin = paper.right() - content.right();
       
   426             m_bottomMargin = paper.bottom() - content.bottom();
       
   427         }
       
   428     }
       
   429 #endif
       
   430     if (widget.paperSize->count() == 0) {
       
   431         populatePaperSizes(widget.paperSize);
       
   432         widget.paperSize->setCurrentIndex(widget.paperSize->findData(
       
   433             QLocale::system().measurementSystem() == QLocale::ImperialSystem ? QPrinter::Letter : QPrinter::A4));
       
   434     }
       
   435 
       
   436     unitChanged(widget.unit->currentIndex());
       
   437     m_pagePreview->setMargins(m_leftMargin, m_topMargin, m_rightMargin, m_bottomMargin);
       
   438 }
       
   439 
       
   440 void QPageSetupWidget::selectPdfPsPrinter(const QPrinter *p)
       
   441 {
       
   442     m_cups = 0;
       
   443     widget.paperSize->clear();
       
   444     populatePaperSizes(widget.paperSize);
       
   445     widget.paperSize->setCurrentIndex(widget.paperSize->findData(p->paperSize()));
       
   446 
       
   447     m_leftMargin = 90;
       
   448     m_topMargin = 72;
       
   449     m_bottomMargin = 72;
       
   450     m_rightMargin = 90;
       
   451     unitChanged(widget.unit->currentIndex());
       
   452     m_pagePreview->setMargins(m_leftMargin, m_topMargin, m_rightMargin, m_bottomMargin);
       
   453 }
       
   454 
       
   455 // Updates size/preview after the combobox has been changed.
       
   456 void QPageSetupWidget::_q_paperSizeChanged()
       
   457 {
       
   458     QVariant val = widget.paperSize->itemData(widget.paperSize->currentIndex());
       
   459     int index = m_printer->pageSize();
       
   460     if (val.type() == QVariant::Int) {
       
   461         index = val.toInt();
       
   462     }
       
   463 
       
   464     if (m_blockSignals) return;
       
   465     m_blockSignals = true;
       
   466 
       
   467     QPrinter::PaperSize size = QPrinter::PaperSize(index);
       
   468     QPrinter::Orientation orientation = widget.portrait->isChecked()
       
   469                                         ? QPrinter::Portrait
       
   470                                         : QPrinter::Landscape;
       
   471 
       
   472     bool custom = size == QPrinter::Custom;
       
   473 
       
   474 #if !defined(QT_NO_CUPS) && !defined(QT_NO_LIBRARY)
       
   475     custom = custom ? !m_cups : custom;
       
   476 #endif
       
   477 
       
   478     widget.paperWidth->setEnabled(custom);
       
   479     widget.paperHeight->setEnabled(custom);
       
   480     widget.widthLabel->setEnabled(custom);
       
   481     widget.heightLabel->setEnabled(custom);
       
   482     if (custom) {
       
   483         m_paperSize.setWidth( widget.paperWidth->value() * m_currentMultiplier);
       
   484         m_paperSize.setHeight( widget.paperHeight->value() * m_currentMultiplier);
       
   485         m_pagePreview->setPaperSize(m_paperSize);
       
   486     } else {
       
   487         Q_ASSERT(m_printer);
       
   488 #if !defined(QT_NO_CUPS) && !defined(QT_NO_LIBRARY)
       
   489         if (m_cups) { // combobox is filled with cups based data
       
   490             QByteArray cupsPageSize = widget.paperSize->itemData(widget.paperSize->currentIndex()).toByteArray();
       
   491             m_paperSize = m_cups->paperRect(cupsPageSize).size();
       
   492             if (orientation == QPrinter::Landscape)
       
   493                 m_paperSize = QSizeF(m_paperSize.height(), m_paperSize.width()); // swap
       
   494         }
       
   495         else
       
   496 #endif
       
   497             m_paperSize = qt_printerPaperSize(orientation, size, QPrinter::Point, 1);
       
   498 
       
   499         m_pagePreview->setPaperSize(m_paperSize);
       
   500         widget.paperWidth->setValue(m_paperSize.width() / m_currentMultiplier);
       
   501         widget.paperHeight->setValue(m_paperSize.height() / m_currentMultiplier);
       
   502     }
       
   503     m_blockSignals = false;
       
   504 }
       
   505 
       
   506 void QPageSetupWidget::_q_pageOrientationChanged()
       
   507 {
       
   508     if (QPrinter::PaperSize(widget.paperSize->currentIndex()) == QPrinter::Custom) {
       
   509         double tmp = widget.paperWidth->value();
       
   510         widget.paperWidth->setValue(widget.paperHeight->value());
       
   511         widget.paperHeight->setValue(tmp);
       
   512     }
       
   513     _q_paperSizeChanged();
       
   514 }
       
   515 
       
   516 extern double qt_multiplierForUnit(QPrinter::Unit unit, int resolution);
       
   517 
       
   518 void QPageSetupWidget::unitChanged(int item)
       
   519 {
       
   520     QString suffix;
       
   521     switch(item) {
       
   522     case 0:
       
   523         m_currentMultiplier = 10 * qt_multiplierForUnit(QPrinter::Millimeter, 1);
       
   524         suffix = QString::fromLatin1(" cm");
       
   525         break;
       
   526     case 2:
       
   527         m_currentMultiplier = qt_multiplierForUnit(QPrinter::Inch, 1);
       
   528         suffix = QString::fromLatin1(" in");
       
   529         break;
       
   530     case 3:
       
   531         m_currentMultiplier = qt_multiplierForUnit(QPrinter::Point, 1);
       
   532         suffix = QString::fromLatin1(" pt");
       
   533         break;
       
   534     case 1:
       
   535     default:
       
   536         m_currentMultiplier = qt_multiplierForUnit(QPrinter::Millimeter, 1);
       
   537         suffix = QString::fromLatin1(" mm");
       
   538         break;
       
   539     }
       
   540     const bool old = m_blockSignals;
       
   541     m_blockSignals = true;
       
   542     widget.topMargin->setSuffix(suffix);
       
   543     widget.leftMargin->setSuffix(suffix);
       
   544     widget.rightMargin->setSuffix(suffix);
       
   545     widget.bottomMargin->setSuffix(suffix);
       
   546     widget.paperWidth->setSuffix(suffix);
       
   547     widget.paperHeight->setSuffix(suffix);
       
   548     widget.topMargin->setValue(m_topMargin / m_currentMultiplier);
       
   549     widget.leftMargin->setValue(m_leftMargin / m_currentMultiplier);
       
   550     widget.rightMargin->setValue(m_rightMargin / m_currentMultiplier);
       
   551     widget.bottomMargin->setValue(m_bottomMargin / m_currentMultiplier);
       
   552     widget.paperWidth->setValue(m_paperSize.width() / m_currentMultiplier);
       
   553     widget.paperHeight->setValue(m_paperSize.height() / m_currentMultiplier);
       
   554     m_blockSignals = old;
       
   555 }
       
   556 
       
   557 void QPageSetupWidget::setTopMargin(double newValue)
       
   558 {
       
   559     if (m_blockSignals) return;
       
   560     m_topMargin = newValue * m_currentMultiplier;
       
   561     m_pagePreview->setMargins(m_leftMargin, m_topMargin, m_rightMargin, m_bottomMargin);
       
   562 }
       
   563 
       
   564 void QPageSetupWidget::setBottomMargin(double newValue)
       
   565 {
       
   566     if (m_blockSignals) return;
       
   567     m_bottomMargin = newValue * m_currentMultiplier;
       
   568     m_pagePreview->setMargins(m_leftMargin, m_topMargin, m_rightMargin, m_bottomMargin);
       
   569 }
       
   570 
       
   571 void QPageSetupWidget::setLeftMargin(double newValue)
       
   572 {
       
   573     if (m_blockSignals) return;
       
   574     m_leftMargin = newValue * m_currentMultiplier;
       
   575     m_pagePreview->setMargins(m_leftMargin, m_topMargin, m_rightMargin, m_bottomMargin);
       
   576 }
       
   577 
       
   578 void QPageSetupWidget::setRightMargin(double newValue)
       
   579 {
       
   580     if (m_blockSignals) return;
       
   581     m_rightMargin = newValue * m_currentMultiplier;
       
   582     m_pagePreview->setMargins(m_leftMargin, m_topMargin, m_rightMargin, m_bottomMargin);
       
   583 }
       
   584 
       
   585 
       
   586 
       
   587 QPageSetupDialog::QPageSetupDialog(QPrinter *printer, QWidget *parent)
       
   588     : QAbstractPageSetupDialog(*(new QPageSetupDialogPrivate), printer, parent)
       
   589 {
       
   590     Q_D(QPageSetupDialog);
       
   591     d->init();
       
   592 }
       
   593 
       
   594 
       
   595 QPageSetupDialog::QPageSetupDialog(QWidget *parent)
       
   596     : QAbstractPageSetupDialog(*(new QPageSetupDialogPrivate), 0, parent)
       
   597 {
       
   598     Q_D(QPageSetupDialog);
       
   599     d->init();
       
   600 }
       
   601 
       
   602 /*!
       
   603     \internal
       
   604 */
       
   605 int QPageSetupDialog::exec()
       
   606 {
       
   607     Q_D(QPageSetupDialog);
       
   608 
       
   609     int ret = QDialog::exec();
       
   610     if (ret == Accepted)
       
   611         d->widget->setupPrinter();
       
   612     return ret;
       
   613 }
       
   614 
       
   615 
       
   616 QT_END_NAMESPACE
       
   617 
       
   618 #include "moc_qpagesetupdialog.cpp"
       
   619 
       
   620 #endif // QT_NO_PRINTDIALOG