/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- * * This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ #include #include #include "nsPrintSettingsQt.h" #include "nsIFile.h" #include "nsCRTGlue.h" NS_IMPL_ISUPPORTS_INHERITED1(nsPrintSettingsQt, nsPrintSettings, nsPrintSettingsQt) nsPrintSettingsQt::nsPrintSettingsQt(): mQPrinter(new QPrinter()) { } nsPrintSettingsQt::~nsPrintSettingsQt() { //smart pointer should take care of cleanup } nsPrintSettingsQt::nsPrintSettingsQt(const nsPrintSettingsQt& aPS): mQPrinter(aPS.mQPrinter) { } nsPrintSettingsQt& nsPrintSettingsQt::operator=(const nsPrintSettingsQt& rhs) { if (this == &rhs) { return *this; } nsPrintSettings::operator=(rhs); mQPrinter = rhs.mQPrinter; return *this; } nsresult nsPrintSettingsQt::_Clone(nsIPrintSettings** _retval) { NS_ENSURE_ARG_POINTER(_retval); nsPrintSettingsQt* newSettings = new nsPrintSettingsQt(*this); *_retval = newSettings; NS_ADDREF(*_retval); return NS_OK; } NS_IMETHODIMP nsPrintSettingsQt::_Assign(nsIPrintSettings* aPS) { nsPrintSettingsQt* printSettingsQt = static_cast(aPS); if (!printSettingsQt) return NS_ERROR_UNEXPECTED; *this = *printSettingsQt; return NS_OK; } NS_IMETHODIMP nsPrintSettingsQt::GetPrintRange(int16_t* aPrintRange) { NS_ENSURE_ARG_POINTER(aPrintRange); QPrinter::PrintRange range = mQPrinter->printRange(); if (range == QPrinter::PageRange) { *aPrintRange = kRangeSpecifiedPageRange; } else if (range == QPrinter::Selection) { *aPrintRange = kRangeSelection; } else { *aPrintRange = kRangeAllPages; } return NS_OK; } NS_IMETHODIMP nsPrintSettingsQt::SetPrintRange(int16_t aPrintRange) { if (aPrintRange == kRangeSelection) { mQPrinter->setPrintRange(QPrinter::Selection); } else if (aPrintRange == kRangeSpecifiedPageRange) { mQPrinter->setPrintRange(QPrinter::PageRange); } else { mQPrinter->setPrintRange(QPrinter::AllPages); } return NS_OK; } NS_IMETHODIMP nsPrintSettingsQt::GetStartPageRange(int32_t* aStartPageRange) { NS_ENSURE_ARG_POINTER(aStartPageRange); int32_t start = mQPrinter->fromPage(); *aStartPageRange = start; return NS_OK; } NS_IMETHODIMP nsPrintSettingsQt::SetStartPageRange(int32_t aStartPageRange) { int32_t endRange = mQPrinter->toPage(); mQPrinter->setFromTo(aStartPageRange, endRange); return NS_OK; } NS_IMETHODIMP nsPrintSettingsQt::GetEndPageRange(int32_t* aEndPageRange) { NS_ENSURE_ARG_POINTER(aEndPageRange); int32_t end = mQPrinter->toPage(); *aEndPageRange = end; return NS_OK; } NS_IMETHODIMP nsPrintSettingsQt::SetEndPageRange(int32_t aEndPageRange) { int32_t startRange = mQPrinter->fromPage(); mQPrinter->setFromTo(startRange, aEndPageRange); return NS_OK; } NS_IMETHODIMP nsPrintSettingsQt::GetPrintReversed(bool* aPrintReversed) { NS_ENSURE_ARG_POINTER(aPrintReversed); if (mQPrinter->pageOrder() == QPrinter::LastPageFirst) { *aPrintReversed = true; } else { *aPrintReversed = false; } return NS_OK; } NS_IMETHODIMP nsPrintSettingsQt::SetPrintReversed(bool aPrintReversed) { if (aPrintReversed) { mQPrinter->setPageOrder(QPrinter::LastPageFirst); } else { mQPrinter->setPageOrder(QPrinter::FirstPageFirst); } return NS_OK; } NS_IMETHODIMP nsPrintSettingsQt::GetPrintInColor(bool* aPrintInColor) { NS_ENSURE_ARG_POINTER(aPrintInColor); if (mQPrinter->colorMode() == QPrinter::Color) { *aPrintInColor = true; } else { *aPrintInColor = false; } return NS_OK; } NS_IMETHODIMP nsPrintSettingsQt::SetPrintInColor(bool aPrintInColor) { if (aPrintInColor) { mQPrinter->setColorMode(QPrinter::Color); } else { mQPrinter->setColorMode(QPrinter::GrayScale); } return NS_OK; } NS_IMETHODIMP nsPrintSettingsQt::GetOrientation(int32_t* aOrientation) { NS_ENSURE_ARG_POINTER(aOrientation); QPrinter::Orientation orientation = mQPrinter->orientation(); if (orientation == QPrinter::Landscape) { *aOrientation = kLandscapeOrientation; } else { *aOrientation = kPortraitOrientation; } return NS_OK; } NS_IMETHODIMP nsPrintSettingsQt::SetOrientation(int32_t aOrientation) { if (aOrientation == kLandscapeOrientation) { mQPrinter->setOrientation(QPrinter::Landscape); } else { mQPrinter->setOrientation(QPrinter::Portrait); } return NS_OK; } NS_IMETHODIMP nsPrintSettingsQt::GetToFileName(char16_t** aToFileName) { NS_ENSURE_ARG_POINTER(aToFileName); QString filename; filename = mQPrinter->outputFileName(); *aToFileName = ToNewUnicode( nsDependentString((char16_t*)filename.data())); return NS_OK; } NS_IMETHODIMP nsPrintSettingsQt::SetToFileName(const char16_t* aToFileName) { nsCOMPtr file; nsresult rv = NS_NewLocalFile(nsDependentString(aToFileName), true, getter_AddRefs(file)); NS_ENSURE_SUCCESS(rv, rv); QString filename((const QChar*)aToFileName, NS_strlen(aToFileName)); mQPrinter->setOutputFileName(filename); return NS_OK; } NS_IMETHODIMP nsPrintSettingsQt::GetPrinterName(char16_t** aPrinter) { NS_ENSURE_ARG_POINTER(aPrinter); *aPrinter = ToNewUnicode(nsDependentString( (const char16_t*)mQPrinter->printerName().constData())); return NS_OK; } NS_IMETHODIMP nsPrintSettingsQt::SetPrinterName(const char16_t* aPrinter) { QString printername((const QChar*)aPrinter, NS_strlen(aPrinter)); mQPrinter->setPrinterName(printername); return NS_OK; } NS_IMETHODIMP nsPrintSettingsQt::GetNumCopies(int32_t* aNumCopies) { NS_ENSURE_ARG_POINTER(aNumCopies); *aNumCopies = mQPrinter->numCopies(); return NS_OK; } NS_IMETHODIMP nsPrintSettingsQt::SetNumCopies(int32_t aNumCopies) { mQPrinter->setNumCopies(aNumCopies); return NS_OK; } NS_IMETHODIMP nsPrintSettingsQt::GetScaling(double* aScaling) { NS_ENSURE_ARG_POINTER(aScaling); qDebug()<paperSize(); QString name(indexToPaperName[size]); *aPaperName = ToNewUnicode(nsDependentString ((const char16_t*)name.constData())); return NS_OK; } NS_IMETHODIMP nsPrintSettingsQt::SetPaperName(const char16_t* aPaperName) { QString ref((QChar*)aPaperName, NS_strlen(aPaperName)); for (uint32_t i = 0; i < QPrinter::NPageSize; i++) { if (ref == QString(indexToPaperName[i])) { mQPrinter->setPageSize(indexToQtPaperEnum[i]); return NS_OK; } } return NS_ERROR_FAILURE; } QPrinter::Unit GetQtUnit(int16_t aGeckoUnit) { if (aGeckoUnit == nsIPrintSettings::kPaperSizeMillimeters) { return QPrinter::Millimeter; } else { return QPrinter::Inch; } } #define SETUNWRITEABLEMARGIN\ mQPrinter->setPageMargins(\ NS_TWIPS_TO_INCHES(mUnwriteableMargin.left),\ NS_TWIPS_TO_INCHES(mUnwriteableMargin.top),\ NS_TWIPS_TO_INCHES(mUnwriteableMargin.right),\ NS_TWIPS_TO_INCHES(mUnwriteableMargin.bottom),\ QPrinter::Inch); NS_IMETHODIMP nsPrintSettingsQt::SetUnwriteableMarginInTwips(nsIntMargin& aUnwriteableMargin) { nsPrintSettings::SetUnwriteableMarginInTwips(aUnwriteableMargin); SETUNWRITEABLEMARGIN return NS_OK; } NS_IMETHODIMP nsPrintSettingsQt::SetUnwriteableMarginTop(double aUnwriteableMarginTop) { nsPrintSettings::SetUnwriteableMarginTop(aUnwriteableMarginTop); SETUNWRITEABLEMARGIN return NS_OK; } NS_IMETHODIMP nsPrintSettingsQt::SetUnwriteableMarginLeft(double aUnwriteableMarginLeft) { nsPrintSettings::SetUnwriteableMarginLeft(aUnwriteableMarginLeft); SETUNWRITEABLEMARGIN return NS_OK; } NS_IMETHODIMP nsPrintSettingsQt::SetUnwriteableMarginBottom(double aUnwriteableMarginBottom) { nsPrintSettings::SetUnwriteableMarginBottom(aUnwriteableMarginBottom); SETUNWRITEABLEMARGIN return NS_OK; } NS_IMETHODIMP nsPrintSettingsQt::SetUnwriteableMarginRight(double aUnwriteableMarginRight) { nsPrintSettings::SetUnwriteableMarginRight(aUnwriteableMarginRight); SETUNWRITEABLEMARGIN return NS_OK; } NS_IMETHODIMP nsPrintSettingsQt::GetPaperWidth(double* aPaperWidth) { NS_ENSURE_ARG_POINTER(aPaperWidth); QSizeF papersize = mQPrinter->paperSize(GetQtUnit(mPaperSizeUnit)); *aPaperWidth = papersize.width(); return NS_OK; } NS_IMETHODIMP nsPrintSettingsQt::SetPaperWidth(double aPaperWidth) { QSizeF papersize = mQPrinter->paperSize(GetQtUnit(mPaperSizeUnit)); papersize.setWidth(aPaperWidth); mQPrinter->setPaperSize(papersize, GetQtUnit(mPaperSizeUnit)); return NS_OK; } NS_IMETHODIMP nsPrintSettingsQt::GetPaperHeight(double* aPaperHeight) { NS_ENSURE_ARG_POINTER(aPaperHeight); QSizeF papersize = mQPrinter->paperSize(GetQtUnit(mPaperSizeUnit)); *aPaperHeight = papersize.height(); return NS_OK; } NS_IMETHODIMP nsPrintSettingsQt::SetPaperHeight(double aPaperHeight) { QSizeF papersize = mQPrinter->paperSize(GetQtUnit(mPaperSizeUnit)); papersize.setHeight(aPaperHeight); mQPrinter->setPaperSize(papersize, GetQtUnit(mPaperSizeUnit)); return NS_OK; } NS_IMETHODIMP nsPrintSettingsQt::SetPaperSizeUnit(int16_t aPaperSizeUnit) { mPaperSizeUnit = aPaperSizeUnit; return NS_OK; } NS_IMETHODIMP nsPrintSettingsQt::GetEffectivePageSize(double* aWidth, double* aHeight) { QSizeF papersize = mQPrinter->paperSize(QPrinter::Inch); if (mQPrinter->orientation() == QPrinter::Landscape) { *aWidth = NS_INCHES_TO_INT_TWIPS(papersize.height()); *aHeight = NS_INCHES_TO_INT_TWIPS(papersize.width()); } else { *aWidth = NS_INCHES_TO_INT_TWIPS(papersize.width()); *aHeight = NS_INCHES_TO_INT_TWIPS(papersize.height()); } return NS_OK; }