Exemplo n.º 1
0
void QPrinter::interpret(PMPrintSettings *s)
{
  //get values
  UInt32 from, to;
  if (PMGetFirstPage(*s, &from) == noErr && PMGetLastPage(*s, &to) == noErr) {
    if (to == INT_MAX) //this means all pages!
      from = to = 0;
    setFromTo(from, to);
  }

  UInt32 copies;
  if (PMGetCopies(*s, &copies) == noErr)
    setNumCopies(copies);

  UInt32 max, min;
  if (PMGetPageRange(*s, &min, &max) == noErr) {
    // The problem here is that Apple's print dialog will always show a one even when you pass
    // it a zero. So take care of that. The other problem is that we can also call this because
    // we need to interpret the default settings, in that case, the min and max will both be
    // zero. In that case, leave them "as is" as some other functions will reset the max and
    // make everything OK when the dialog is actually called.
    int newMin = min;
    int newMax = max;

    if (newMin < 1 && newMax != 0)
      newMin = 1;
    setMinMax(newMin, newMax);
  }

  PMColorMode cm;
  if (PMGetColorMode(*s, &cm) == noErr)
    setColorMode(cm == kPMGray ? GrayScale : Color);
  // Get the current Printer Name
  CFIndex currPrinterIndex;
  PMPrinter currPrinter;
  CFArrayRef printerArray;
  OSStatus err = PMSessionCreatePrinterList(psession, &printerArray,
                                            &currPrinterIndex, &currPrinter);
  if (err != noErr)
    qWarning("Qt: QPrinter::interpret problem creating printer list %ld", err);
  if (printerArray) {
    setPrinterName(cfstring2qstring((CFStringRef)CFArrayGetValueAtIndex(printerArray,
                                                                        currPrinterIndex)));
    CFRelease(printerArray);
  }
}
Exemplo n.º 2
0
QVariant QMacPrintEngine::property(PrintEnginePropertyKey key) const
{
    Q_D(const QMacPrintEngine);
    QVariant ret;

    if (!d->session && d->valueCache.contains(key))
        return *d->valueCache.find(key);

    switch (key) {
    case PPK_CollateCopies:
        ret = false;
        break;
    case PPK_ColorMode:
        ret = QPrinter::Color;
        break;
    case PPK_Creator:
        break;
    case PPK_DocumentName:
        break;
    case PPK_FullPage:
        ret = d->fullPage;
        break;
    case PPK_NumberOfCopies:
        ret = 1;
        break;
    case PPK_Orientation:
        PMOrientation orientation;
        PMGetOrientation(d->format, &orientation);
        ret = orientation == kPMPortrait ? QPrinter::Portrait : QPrinter::Landscape;
        break;
    case PPK_OutputFileName:
        ret = d->outputFilename;
        break;
    case PPK_PageOrder:
        break;
    case PPK_PaperSource:
        break;
    case PPK_PageRect: {
        // PageRect is returned in device pixels
        QRect r;
        PMRect macrect, macpaper;
        qreal hRatio = d->resolution.hRes / 72;
        qreal vRatio = d->resolution.vRes / 72;
        if (d->hasCustomPaperSize) {
            r = QRect(0, 0, qRound(d->customSize.width() * hRatio), qRound(d->customSize.height() * vRatio));
            if (d->hasCustomPageMargins) {
                r.adjust(qRound(d->leftMargin * hRatio), qRound(d->topMargin * vRatio), 
                         -qRound(d->rightMargin * hRatio), -qRound(d->bottomMargin * vRatio));
            } else {
                QList<QVariant> margins = property(QPrintEngine::PPK_PageMargins).toList();
                r.adjust(qRound(margins.at(0).toDouble() * hRatio),
                         qRound(margins.at(1).toDouble() * vRatio),
                         -qRound(margins.at(2).toDouble() * hRatio),
                         -qRound(margins.at(3).toDouble()) * vRatio);
            }
        } else if (PMGetAdjustedPageRect(d->format, &macrect) == noErr
                   && PMGetAdjustedPaperRect(d->format, &macpaper) == noErr)
        {
            if (d->fullPage || d->hasCustomPageMargins) {
                r.setCoords(int(macpaper.left * hRatio), int(macpaper.top * vRatio),
                            int(macpaper.right * hRatio), int(macpaper.bottom * vRatio));
                r.translate(-r.x(), -r.y());
                if (d->hasCustomPageMargins) {
                    r.adjust(qRound(d->leftMargin * hRatio), qRound(d->topMargin * vRatio),
                             -qRound(d->rightMargin * hRatio), -qRound(d->bottomMargin * vRatio));
                }
            } else {
                r.setCoords(int(macrect.left * hRatio), int(macrect.top * vRatio),
                            int(macrect.right * hRatio), int(macrect.bottom * vRatio));
                r.translate(int(-macpaper.left * hRatio), int(-macpaper.top * vRatio));
            } 
        }
        ret = r;
        break; }
    case PPK_PaperSize:
        ret = d->paperSize();
        break;
    case PPK_PaperRect: {
        QRect r;
        PMRect macrect;
        if (d->hasCustomPaperSize) {
            r = QRect(0, 0, qRound(d->customSize.width()), qRound(d->customSize.height()));
        } else if (PMGetAdjustedPaperRect(d->format, &macrect) == noErr) {
            qreal hRatio = d->resolution.hRes / 72;
            qreal vRatio = d->resolution.vRes / 72;
            r.setCoords(int(macrect.left * hRatio), int(macrect.top * vRatio),
                        int(macrect.right * hRatio), int(macrect.bottom * vRatio));
            r.translate(-r.x(), -r.y());
        }
        ret = r;
        break; }
    case PPK_PrinterName: {
        CFIndex currIndex;
        PMPrinter unused;
        QCFType<CFArrayRef> printerList;
        OSStatus status = PMSessionCreatePrinterList(d->session, &printerList, &currIndex, &unused);
        if (status != noErr)
            qWarning("QMacPrintEngine::printerName: Problem getting list of printers: %ld", long(status));
        if (currIndex != -1 && printerList && currIndex < CFArrayGetCount(printerList)) {
            const CFStringRef name = static_cast<CFStringRef>(CFArrayGetValueAtIndex(printerList, currIndex));
            if (name)
                ret = QCFString::toQString(name);
        }
        break; }
    case PPK_Resolution: {
        ret = d->resolution.hRes;
        break;
    }
    case PPK_SupportedResolutions:
        ret = d->supportedResolutions();
        break;
    case PPK_CustomPaperSize:
        ret = d->customSize;
        break;
    case PPK_PageMargins:
    {
        QList<QVariant> margins;
        if (d->hasCustomPageMargins) {
            margins << d->leftMargin << d->topMargin
                    << d->rightMargin << d->bottomMargin;
        } else {
            PMPaperMargins paperMargins;
            PMPaper paper;
            PMGetPageFormatPaper(d->format, &paper);
            PMPaperGetMargins(paper, &paperMargins);
            margins << paperMargins.left << paperMargins.top
                    << paperMargins.right << paperMargins.bottom;
        }
        ret = margins;
        break;
    }
    default:
        break;
    }
    return ret;
}